From 3a9ffa7c53bdc01c9300fffd39bf532b6c2b39af Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 7 Jan 2025 16:23:29 +0000 Subject: [PATCH] Streamline ap routes (part 05: add mods or sticky) --- app/activitypub/routes.py | 85 ++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 90db2e9c..d52228c7 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -896,18 +896,29 @@ def process_inbox_request(request_json, store_ap_json): log_incoming_ap(id, APLOG_LOCK, APLOG_FAILURE, request_json if store_ap_json else None, 'Lock: post not found') return - if request_json['type'] == 'Add': # remote site is adding a local user as a moderator, and is sending directly rather than announcing (happens if not subscribed) + if core_activity['type'] == 'Add': # Add mods, or sticky a post mod = user - community = find_community(request_json) + if not announced: + community = find_community(core_activity) if community: if not community.is_moderator(mod) and not community.is_instance_admin(mod): log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Does not have permission') return - target = request_json['target'] + target = core_activity['target'] + featured_url = community.ap_featured_url moderators_url = community.ap_moderators_url + if target == featured_url: + post = Post.query.filter_by(ap_id=core_activity['object']).first() + if post: + post.sticky = True + db.session.commit() + log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) + else: + log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + core_activity['object']) + return if target == moderators_url: - new_mod = find_actor_or_create(request_json['object'], create_if_not_found=False) - if new_mod and new_mod.is_local(): + new_mod = find_actor_or_create(core_activity['object']) + if new_mod: existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=new_mod.id).first() if existing_membership: existing_membership.is_moderator = True @@ -917,11 +928,9 @@ def process_inbox_request(request_json, store_ap_json): db.session.commit() log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) else: - log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']) + log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + core_activity['object']) return - else: - # Lemmy might not send anything directly to sticky a post if no-one is subscribed (could not get it to generate the activity) - log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Unknown target for Add') + log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Unknown target for Add') else: log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Add: cannot find community') return @@ -1157,35 +1166,35 @@ def process_inbox_request(request_json, store_ap_json): # log_incoming_ap(id, APLOG_LOCK, APLOG_FAILURE, request_json if store_ap_json else None, 'Lock: post not found') # return - if request_json['object']['type'] == 'Add': # Announce of adding mods or stickying a post - target = request_json['object']['target'] - featured_url = community.ap_featured_url - moderators_url = community.ap_moderators_url - if target == featured_url: - post = Post.query.filter_by(ap_id=request_json['object']['object']).first() - if post: - post.sticky = True - db.session.commit() - log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) - else: - log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) - return - if target == moderators_url: - user = find_actor_or_create(request_json['object']['object']) - if user: - existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=user.id).first() - if existing_membership: - existing_membership.is_moderator = True - else: - new_membership = CommunityMember(community_id=community.id, user_id=user.id, is_moderator=True) - db.session.add(new_membership) - db.session.commit() - log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) - else: - log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) - return - log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Unknown target for Add') - return + #if request_json['object']['type'] == 'Add': # Announce of adding mods or stickying a post + # target = request_json['object']['target'] + # featured_url = community.ap_featured_url + # moderators_url = community.ap_moderators_url + # if target == featured_url: + # post = Post.query.filter_by(ap_id=request_json['object']['object']).first() + # if post: + # post.sticky = True + # db.session.commit() + # log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) + # else: + # log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) + # return + # if target == moderators_url: + # user = find_actor_or_create(request_json['object']['object']) + # if user: + # existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=user.id).first() + # if existing_membership: + # existing_membership.is_moderator = True + # else: + # new_membership = CommunityMember(community_id=community.id, user_id=user.id, is_moderator=True) + # db.session.add(new_membership) + # db.session.commit() + # log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) + # else: + # log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) + # return + # log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Unknown target for Add') + # return if request_json['object']['type'] == 'Remove': # Announce of removing mods or unstickying a post target = request_json['object']['target']