Streamline ap routes (part 05: add mods or sticky)

This commit is contained in:
freamon 2025-01-07 16:23:29 +00:00
parent 099f073f01
commit 3a9ffa7c53

View file

@ -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') log_incoming_ap(id, APLOG_LOCK, APLOG_FAILURE, request_json if store_ap_json else None, 'Lock: post not found')
return 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 mod = user
community = find_community(request_json) if not announced:
community = find_community(core_activity)
if community: if community:
if not community.is_moderator(mod) and not community.is_instance_admin(mod): 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') log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Does not have permission')
return return
target = request_json['target'] target = core_activity['target']
featured_url = community.ap_featured_url
moderators_url = community.ap_moderators_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: if target == moderators_url:
new_mod = find_actor_or_create(request_json['object'], create_if_not_found=False) new_mod = find_actor_or_create(core_activity['object'])
if new_mod and new_mod.is_local(): if new_mod:
existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=new_mod.id).first() existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=new_mod.id).first()
if existing_membership: if existing_membership:
existing_membership.is_moderator = True existing_membership.is_moderator = True
@ -917,10 +928,8 @@ def process_inbox_request(request_json, store_ap_json):
db.session.commit() db.session.commit()
log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None)
else: 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 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: else:
log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Add: cannot find community') log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Add: cannot find community')
@ -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') # log_incoming_ap(id, APLOG_LOCK, APLOG_FAILURE, request_json if store_ap_json else None, 'Lock: post not found')
# return # return
if request_json['object']['type'] == 'Add': # Announce of adding mods or stickying a post #if request_json['object']['type'] == 'Add': # Announce of adding mods or stickying a post
target = request_json['object']['target'] # target = request_json['object']['target']
featured_url = community.ap_featured_url # featured_url = community.ap_featured_url
moderators_url = community.ap_moderators_url # moderators_url = community.ap_moderators_url
if target == featured_url: # if target == featured_url:
post = Post.query.filter_by(ap_id=request_json['object']['object']).first() # post = Post.query.filter_by(ap_id=request_json['object']['object']).first()
if post: # if post:
post.sticky = True # post.sticky = True
db.session.commit() # db.session.commit()
log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) # log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None)
else: # else:
log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) # log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object'])
return # return
if target == moderators_url: # if target == moderators_url:
user = find_actor_or_create(request_json['object']['object']) # user = find_actor_or_create(request_json['object']['object'])
if user: # if user:
existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=user.id).first() # existing_membership = CommunityMember.query.filter_by(community_id=community.id, user_id=user.id).first()
if existing_membership: # if existing_membership:
existing_membership.is_moderator = True # existing_membership.is_moderator = True
else: # else:
new_membership = CommunityMember(community_id=community.id, user_id=user.id, is_moderator=True) # new_membership = CommunityMember(community_id=community.id, user_id=user.id, is_moderator=True)
db.session.add(new_membership) # db.session.add(new_membership)
db.session.commit() # db.session.commit()
log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None) # log_incoming_ap(id, APLOG_ADD, APLOG_SUCCESS, request_json if store_ap_json else None)
else: # else:
log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object']) # log_incoming_ap(id, APLOG_ADD, APLOG_FAILURE, request_json if store_ap_json else None, 'Cannot find: ' + request_json['object']['object'])
return # return
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')
return # return
if request_json['object']['type'] == 'Remove': # Announce of removing mods or unstickying a post if request_json['object']['type'] == 'Remove': # Announce of removing mods or unstickying a post
target = request_json['object']['target'] target = request_json['object']['target']