From e8b2861d885de8857b5ad33e1dbc716339c30bbf Mon Sep 17 00:00:00 2001 From: freamon Date: Wed, 8 Jan 2025 19:23:47 +0000 Subject: [PATCH] Streamline ap routes (part 10: undo deletes) --- app/activitypub/routes.py | 46 ++++++++++++++++++++------------------- app/activitypub/util.py | 8 ++++--- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index ec7b1197..66400d20 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -1049,7 +1049,7 @@ def process_inbox_request(request_json, store_ap_json): log_incoming_ap(id, APLOG_USERBAN, APLOG_SUCCESS, request_json if store_ap_json else None) return - if request_json['type'] == 'Undo': + if core_activity['type'] == 'Undo': if request_json['object']['type'] == 'Follow': # Unsubscribe from a community or user target_ap_id = request_json['object']['object'] target = find_actor_or_create(target_ap_id, create_if_not_found=False) @@ -1079,11 +1079,11 @@ def process_inbox_request(request_json, store_ap_json): log_incoming_ap(id, APLOG_UNDO_FOLLOW, APLOG_FAILURE, request_json if store_ap_json else None, 'Unfound target') return - if request_json['object']['type'] == 'Delete': # Restore something previously deleted - if isinstance(request_json['object']['object'], str): - ap_id = request_json['object']['object'] # lemmy + if core_activity['object']['type'] == 'Delete': # Restore something previously deleted + if isinstance(core_activity['object']['object'], str): + ap_id = core_activity['object']['object'] # lemmy else: - ap_id = request_json['object']['object']['id'] # kbin + ap_id = core_activity['object']['object']['id'] # kbin restorer = user to_restore = find_liked_object(ap_id) # a user or a mod/admin is undoing the delete of a post or reply @@ -1091,8 +1091,10 @@ def process_inbox_request(request_json, store_ap_json): if not to_restore.deleted: log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Activity about local content which is already restored') else: - restore_post_or_comment(restorer, to_restore, store_ap_json, request_json) - announce_activity_to_followers(to_restore.community, user, request_json) + reason = core_activity['object']['summary'] if 'summary' in core_activity['object'] else '' + restore_post_or_comment(restorer, to_restore, store_ap_json, request_json, reason) + if not announced: + announce_activity_to_followers(to_restore.community, user, request_json) else: log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Undo delete: cannot find ' + ap_id) return @@ -1284,22 +1286,22 @@ def process_inbox_request(request_json, store_ap_json): # return if request_json['object']['type'] == 'Undo': - if request_json['object']['object']['type'] == 'Delete': # Announce of undo of Delete - if isinstance(request_json['object']['object']['object'], str): - ap_id = request_json['object']['object']['object'] # lemmy - else: - ap_id = request_json['object']['object']['object']['id'] # kbin + #if request_json['object']['object']['type'] == 'Delete': # Announce of undo of Delete + # if isinstance(request_json['object']['object']['object'], str): + # ap_id = request_json['object']['object']['object'] # lemmy + # else: + # ap_id = request_json['object']['object']['object']['id'] # kbin - restorer = user - to_restore = find_liked_object(ap_id) # a user or a mod/admin is undoing the delete of a post or reply - if to_restore: - if not to_restore.deleted: - log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Content was not deleted') - else: - restore_post_or_comment(restorer, to_restore, store_ap_json, request_json) - else: - log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Undo delete: cannot find ' + ap_id) - return + # restorer = user + # to_restore = find_liked_object(ap_id) # a user or a mod/admin is undoing the delete of a post or reply + # if to_restore: + # if not to_restore.deleted: + # log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Content was not deleted') + # else: + # restore_post_or_comment(restorer, to_restore, store_ap_json, request_json) + # else: + # log_incoming_ap(id, APLOG_UNDO_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Undo delete: cannot find ' + ap_id) + # return if request_json['object']['object']['type'] == 'Like' or request_json['object']['object']['type'] == 'Dislike': # Announce of undo of upvote or downvote post = comment = None diff --git a/app/activitypub/util.py b/app/activitypub/util.py index f7f2c95f..bae55282 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1344,11 +1344,13 @@ def delete_post_or_comment(deletor, to_delete, store_ap_json, request_json, reas log_incoming_ap(id, APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Deletor did not have permisson') -def restore_post_or_comment(restorer, to_restore, store_ap_json, request_json): +def restore_post_or_comment(restorer, to_restore, store_ap_json, request_json, reason): id = request_json['id'] community = to_restore.community - reason = request_json['object']['summary'] if 'summary' in request_json['object'] else '' - if to_restore.user_id == restorer.id or restorer.is_admin() or community.is_moderator(restorer) or community.is_instance_admin(restorer): + if (to_restore.user_id == restorer.id or + (restorer.instance_id == to_restore.author.instance_id and restorer.is_instance_admin()) or + community.is_moderator(restorer) or + community.is_instance_admin(restorer)): if isinstance(to_restore, Post): to_restore.deleted = False to_restore.deleted_by = None