post-reply soft-deletion: activitypub fixes

This commit is contained in:
freamon 2024-10-19 21:13:36 +00:00
parent 148d230527
commit 3196c2cb79
3 changed files with 48 additions and 29 deletions

View file

@ -835,8 +835,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
for ocp in old_cross_posts:
if ocp.cross_posts is not None and post.id in ocp.cross_posts:
ocp.cross_posts.remove(post.id)
delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id)
activity_log.result = 'success'
delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id, activity_log.id)
elif request_json['object']['type'] == 'Page': # Sent for Mastodon's benefit
activity_log.result = 'ignored'
activity_log.exception_message = 'Intended for Mastodon'

View file

@ -1295,18 +1295,24 @@ def is_activitypub_request():
return 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get('Accept', '')
def delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id):
def delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id, aplog_id):
if current_app.debug:
delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id)
delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id, aplog_id)
else:
delete_post_or_comment_task.delay(user_ap_id, community_ap_id, to_be_deleted_ap_id)
delete_post_or_comment_task.delay(user_ap_id, community_ap_id, to_be_deleted_ap_id, aplog_id)
@celery.task
def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id):
def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id, aplog_id):
deletor = find_actor_or_create(user_ap_id)
community = find_actor_or_create(community_ap_id, community_only=True)
to_delete = find_liked_object(to_be_deleted_ap_id)
if to_delete.deleted:
aplog = ActivityPubLog.query.get(aplog_id)
if aplog:
aplog.result = 'ignored'
aplog.exception_message = 'Activity about local content which is already present'
return
if deletor and community and to_delete:
if deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor) or to_delete.author.id == deletor.id:

View file

@ -1626,7 +1626,8 @@ def post_reply_delete(post_id: int, comment_id: int):
if post_reply.user_id != current_user.id:
delete_json['summary'] = 'Deleted by mod'
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
if not post.community.is_local():
if post_reply.user_id == current_user.id or post.community.is_moderator(current_user):
success = post_request(post.community.ap_inbox_url, delete_json, current_user.private_key,
current_user.public_url() + '#main-key')
if success is False or isinstance(success, str):
@ -1664,6 +1665,10 @@ def post_reply_restore(post_id: int, comment_id: int):
post_reply = PostReply.query.get_or_404(comment_id)
community = post.community
if post_reply.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
if post_reply.deleted_by == post_reply.user_id:
was_mod_deletion = False
else:
was_mod_deletion = True
post_reply.deleted = False
post_reply.deleted_by = None
if not post_reply.author.bot:
@ -1673,7 +1678,7 @@ def post_reply_restore(post_id: int, comment_id: int):
flash(_('Comment restored.'))
# Federate un-delete
if post.is_local():
if not post.community.local_only:
delete_json = {
"actor": current_user.public_url(),
"to": ["https://www.w3.org/ns/activitystreams#Public"],
@ -1689,14 +1694,23 @@ def post_reply_restore(post_id: int, comment_id: int):
],
'object': post_reply.ap_id,
'uri': post_reply.ap_id,
"summary": "bad post reply",
},
"cc": [post.community.public_url()],
"audience": post.community.public_url(),
"type": "Undo",
"id": f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}"
}
if was_mod_deletion:
delete_json['object']['summary'] = "Deleted by mod"
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
if not was_mod_deletion or (was_mod_deletion and post.community.is_moderator(current_user)):
success = post_request(post.community.ap_inbox_url, delete_json, current_user.private_key,
current_user.public_url() + '#main-key')
if success is False or isinstance(success, str):
flash('Failed to send delete to remote server', 'error')
else: # local community - send it to followers on remote instances
announce = {
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
"type": 'Announce',