From 9dc1589783e6b60b5a6ac2d8d8ef78cafe7af281 Mon Sep 17 00:00:00 2001 From: freamon Date: Wed, 8 Jan 2025 18:28:27 +0000 Subject: [PATCH] Streamline ap routes (part 08: deletes) --- app/activitypub/routes.py | 42 ++++++++++++++++++++------------------- app/activitypub/util.py | 8 +++++--- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 22a79a69..9d5e269a 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -839,19 +839,21 @@ def process_inbox_request(request_json, store_ap_json): log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, request_json if store_ap_json else None, 'Unacceptable type (create): ' + object_type) return - if request_json['type'] == 'Delete': - if isinstance(request_json['object'], str): - ap_id = request_json['object'] # lemmy + if core_activity['type'] == 'Delete': + if isinstance(core_activity['object'], str): + ap_id = core_activity['object'] # lemmy else: - ap_id = request_json['object']['id'] # kbin + ap_id = core_activity['object']['id'] # kbin to_delete = find_liked_object(ap_id) # Just for Posts and Replies (User deletes go through process_delete_request()) if to_delete: if to_delete.deleted: log_incoming_ap(id, APLOG_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Activity about local content which is already deleted') else: - delete_post_or_comment(user, to_delete, store_ap_json, request_json) - announce_activity_to_followers(to_delete.community, user, request_json) + reason = core_activity['summary'] if 'summary' in core_activity else '' + delete_post_or_comment(user, to_delete, store_ap_json, request_json, reason) + if not announced: + announce_activity_to_followers(to_delete.community, user, request_json) else: log_incoming_ap(id, APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Delete: cannot find ' + ap_id) return @@ -1141,21 +1143,21 @@ def process_inbox_request(request_json, store_ap_json): log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, request_json if store_ap_json else None, 'Unacceptable type (create): ' + object_type) return - if request_json['object']['type'] == 'Delete': # Announced Delete - if isinstance(request_json['object']['object'], str): - ap_id = request_json['object']['object'] # lemmy - else: - ap_id = request_json['object']['object']['id'] # kbin - to_delete = find_liked_object(ap_id) # Just for Posts and Replies (User deletes aren't announced) + #if request_json['object']['type'] == 'Delete': # Announced Delete + # if isinstance(request_json['object']['object'], str): + # ap_id = request_json['object']['object'] # lemmy + # else: + # ap_id = request_json['object']['object']['id'] # kbin + # to_delete = find_liked_object(ap_id) # Just for Posts and Replies (User deletes aren't announced) - if to_delete: - if to_delete.deleted: - log_incoming_ap(id, APLOG_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Activity about local content which is already deleted') - else: - delete_post_or_comment(user, to_delete, store_ap_json, request_json) - else: - log_incoming_ap(id, APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Delete: cannot find ' + ap_id) - return + # if to_delete: + # if to_delete.deleted: + # log_incoming_ap(id, APLOG_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Activity about local content which is already deleted') + # else: + # delete_post_or_comment(user, to_delete, store_ap_json, request_json) + # else: + # log_incoming_ap(id, APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Delete: cannot find ' + ap_id) + # return #if request_json['object']['type'] == 'Like' or request_json['object']['type'] == 'EmojiReact': # Announced Upvote # process_upvote(user, store_ap_json, request_json) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 45325161..f7f2c95f 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1303,11 +1303,13 @@ 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(deletor, to_delete, store_ap_json, request_json): +def delete_post_or_comment(deletor, to_delete, store_ap_json, request_json, reason): id = request_json['id'] community = to_delete.community - reason = request_json['object']['summary'] if 'summary' in request_json['object'] else '' - if to_delete.user_id == deletor.id or deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor): + if (to_delete.user_id == deletor.id or + (deletor.instance_id == to_delete.author.instance_id and deletor.is_instance_admin()) or + community.is_moderator(deletor) or + community.is_instance_admin(deletor)): if isinstance(to_delete, Post): to_delete.deleted = True to_delete.deleted_by = deletor.id