Streamline ap routes (part 17: use 'core_activity' for consistency)

This commit is contained in:
freamon 2025-01-08 22:58:10 +00:00
parent cbb2321a57
commit d9e5b14e42

View file

@ -643,9 +643,9 @@ def process_inbox_request(request_json, store_ap_json):
core_activity = request_json core_activity = request_json
# Follow: remote user wants to join/follow one of our users or communities # Follow: remote user wants to join/follow one of our users or communities
if request_json['type'] == 'Follow': if core_activity['type'] == 'Follow':
target_ap_id = request_json['object'] target_ap_id = core_activity['object']
follow_id = request_json['id'] follow_id = core_activity['id']
target = find_actor_or_create(target_ap_id, create_if_not_found=False) target = find_actor_or_create(target_ap_id, create_if_not_found=False)
if not target: if not target:
log_incoming_ap(id, APLOG_FOLLOW, APLOG_FAILURE, saved_json, 'Could not find target of Follow') log_incoming_ap(id, APLOG_FOLLOW, APLOG_FAILURE, saved_json, 'Could not find target of Follow')
@ -703,15 +703,15 @@ def process_inbox_request(request_json, store_ap_json):
return return
# Accept: remote server is accepting our previous follow request # Accept: remote server is accepting our previous follow request
if request_json['type'] == 'Accept': if core_activity['type'] == 'Accept':
user = None user = None
if isinstance(request_json['object'], str): # a.gup.pe accepts using a string with the ID of the follow request if isinstance(core_activity['object'], str): # a.gup.pe accepts using a string with the ID of the follow request
join_request_parts = request_json['object'].split('/') join_request_parts = core_activity['object'].split('/')
join_request = CommunityJoinRequest.query.get(join_request_parts[-1]) join_request = CommunityJoinRequest.query.get(join_request_parts[-1])
if join_request: if join_request:
user = User.query.get(join_request.user_id) user = User.query.get(join_request.user_id)
elif request_json['object']['type'] == 'Follow': elif core_activity['object']['type'] == 'Follow':
user_ap_id = request_json['object']['actor'] user_ap_id = core_activity['object']['actor']
user = find_actor_or_create(user_ap_id, create_if_not_found=False) user = find_actor_or_create(user_ap_id, create_if_not_found=False)
if not user: if not user:
log_incoming_ap(id, APLOG_ACCEPT, APLOG_FAILURE, saved_json, 'Could not find recipient of Accept') log_incoming_ap(id, APLOG_ACCEPT, APLOG_FAILURE, saved_json, 'Could not find recipient of Accept')
@ -729,9 +729,9 @@ def process_inbox_request(request_json, store_ap_json):
return return
# Reject: remote server is rejecting our previous follow request # Reject: remote server is rejecting our previous follow request
if request_json['type'] == 'Reject': if core_activity['type'] == 'Reject':
if request_json['object']['type'] == 'Follow': if core_activity['object']['type'] == 'Follow':
user_ap_id = request_json['object']['actor'] user_ap_id = core_activity['object']['actor']
user = find_actor_or_create(user_ap_id, create_if_not_found=False) user = find_actor_or_create(user_ap_id, create_if_not_found=False)
if not user: if not user:
log_incoming_ap(id, APLOG_ACCEPT, APLOG_FAILURE, saved_json, 'Could not find recipient of Reject') log_incoming_ap(id, APLOG_ACCEPT, APLOG_FAILURE, saved_json, 'Could not find recipient of Reject')
@ -776,10 +776,10 @@ def process_inbox_request(request_json, store_ap_json):
db.session.add(existing_conversation) db.session.add(existing_conversation)
db.session.commit() db.session.commit()
# Save ChatMessage to DB # Save ChatMessage to DB
encrypted = request_json['object']['encrypted'] if 'encrypted' in request_json['object'] else None encrypted = core_activity['object']['encrypted'] if 'encrypted' in core_activity['object'] else None
new_message = ChatMessage(sender_id=sender.id, recipient_id=recipient.id, conversation_id=existing_conversation.id, new_message = ChatMessage(sender_id=sender.id, recipient_id=recipient.id, conversation_id=existing_conversation.id,
body_html=request_json['object']['content'], body_html=core_activity['object']['content'],
body=html_to_text(request_json['object']['content']), body=html_to_text(core_activity['object']['content']),
encrypted=encrypted) encrypted=encrypted)
db.session.add(new_message) db.session.add(new_message)
existing_conversation.updated_at = utcnow() existing_conversation.updated_at = utcnow()
@ -802,7 +802,7 @@ def process_inbox_request(request_json, store_ap_json):
post_being_replied_to = Post.query.filter_by(ap_id=core_activity['object']['inReplyTo']).first() post_being_replied_to = Post.query.filter_by(ap_id=core_activity['object']['inReplyTo']).first()
if post_being_replied_to: if post_being_replied_to:
poll_data = Poll.query.get(post_being_replied_to.id) poll_data = Poll.query.get(post_being_replied_to.id)
choice = PollChoice.query.filter_by(post_id=post_being_replied_to.id, choice_text=request_json['object']['name']).first() choice = PollChoice.query.filter_by(post_id=post_being_replied_to.id, choice_text=core_activity['object']['name']).first()
if poll_data and choice: if poll_data and choice:
poll_data.vote_for_choice(choice.id, user.id) poll_data.vote_for_choice(choice.id, user.id)
db.session.commit() db.session.commit()
@ -815,7 +815,7 @@ def process_inbox_request(request_json, store_ap_json):
if not community: if not community:
log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, 'Blocked or unfound community') log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, 'Blocked or unfound community')
return return
if not ensure_domains_match(request_json['object']): if not ensure_domains_match(core_activity['object']):
log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, 'Domains do not match') log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, 'Domains do not match')
return return
if community.local_only: if community.local_only:
@ -828,7 +828,7 @@ def process_inbox_request(request_json, store_ap_json):
process_new_content(user, community, store_ap_json, request_json, announced) process_new_content(user, community, store_ap_json, request_json, announced)
return return
elif object_type == 'Video': # PeerTube: editing a video (mostly used to update post score) elif object_type == 'Video': # PeerTube: editing a video (mostly used to update post score)
post = Post.query.filter_by(ap_id=request_json['object']['id']).first() post = Post.query.filter_by(ap_id=core_activity['object']['id']).first()
if post: if post:
if user.id == post.user_id: if user.id == post.user_id:
update_post_from_activity(post, request_json) update_post_from_activity(post, request_json)
@ -1001,7 +1001,7 @@ def process_inbox_request(request_json, store_ap_json):
If / When this changes, the code below will need updating, and we'll have to do extra work If / When this changes, the code below will need updating, and we'll have to do extra work
""" """
if not announced and store_ap_json: if not announced and store_ap_json:
request_json['cc'] = [] # cut very long list of instances core_activity['cc'] = [] # cut very long list of instances
blocker = user blocker = user
blocked_ap_id = core_activity['object'].lower() blocked_ap_id = core_activity['object'].lower()
@ -1029,10 +1029,10 @@ def process_inbox_request(request_json, store_ap_json):
return return
blocked.banned = True blocked.banned = True
if 'expires' in request_json: if 'expires' in core_activity:
blocked.banned_until = request_json['expires'] blocked.banned_until = core_activity['expires']
elif 'endTime' in request_json: elif 'endTime' in core_activity:
blocked.banned_until = request_json['endTime'] blocked.banned_until = core_activity['endTime']
db.session.commit() db.session.commit()
if remove_data: if remove_data:
@ -1054,8 +1054,8 @@ def process_inbox_request(request_json, store_ap_json):
return return
if core_activity['type'] == 'Undo': if core_activity['type'] == 'Undo':
if request_json['object']['type'] == 'Follow': # Unsubscribe from a community or user if core_activity['object']['type'] == 'Follow': # Unsubscribe from a community or user
target_ap_id = request_json['object']['object'] target_ap_id = core_activity['object']['object']
target = find_actor_or_create(target_ap_id, create_if_not_found=False) target = find_actor_or_create(target_ap_id, create_if_not_found=False)
if isinstance(target, Community): if isinstance(target, Community):
community = target community = target
@ -1136,8 +1136,8 @@ def process_inbox_request(request_json, store_ap_json):
if core_activity['object']['type'] == 'Block': # Undo of user ban if core_activity['object']['type'] == 'Block': # Undo of user ban
if announced and store_ap_json: if announced and store_ap_json:
request_json['cc'] = [] # cut very long list of instances core_activity['cc'] = [] # cut very long list of instances
request_json['object']['cc'] = [] core_activity['object']['cc'] = []
unblocker = user unblocker = user
unblocked_ap_id = core_activity['object']['object'].lower() unblocked_ap_id = core_activity['object']['object'].lower()