apf part 21: Announce / Create or Update

This commit is contained in:
freamon 2024-11-23 19:26:25 +00:00
parent ea15cb6079
commit a36d5b42e9
2 changed files with 41 additions and 8 deletions

View file

@ -874,6 +874,43 @@ def process_inbox_request(request_json, store_ap_json):
log_incoming_ap(request_json['id'], APLOG_UNDO_USERBAN, APLOG_SUCCESS, request_json if store_ap_json else None)
return
# Announce is new content and votes that happened on a remote server.
if request_json['type'] == 'Announce':
if isinstance(request_json['object'], str): # Mastodon, PeerTube, A.gup.pe
post = resolve_remote_post(request_json['object'], community.id, announce_actor=community.ap_profile_id, store_ap_json=store_ap_json)
if post:
log_incoming_ap(request_json['id'], APLOG_ANNOUNCE, APLOG_SUCCESS, request_json)
else:
log_incoming_ap(request_json['id'], APLOG_ANNOUNCE, APLOG_FAILURE, request_json, 'Could not resolve post')
return
user_ap_id = request_json['object']['actor']
user = find_actor_or_create(user_ap_id)
if not user or not isinstance(user, User):
log_incoming_ap(request_json['id'], APLOG_ANNOUNCE, APLOG_FAILURE, request_json, 'Blocked or unfound user for Announce object actor ' + user_ap_id)
return
user.last_seen = site.last_active = utcnow()
user.instance.last_seen = utcnow()
user.instance.dormant = False
user.instance.gone_forever = False
user.instance.failures = 0
db.session.commit()
if request_json['object']['type'] == 'Create' or request_json['object']['type'] == 'Update':
object_type = request_json['object']['object']['type']
new_content_types = ['Page', 'Article', 'Link', 'Note', 'Question']
if object_type in new_content_types: # create or update a post
process_new_content(user, community, store_ap_json, request_json)
elif request_json['object']['type'] == 'Update' and request_json['object']['object']['type'] == 'Group':
# force refresh next time community is heard from
community.ap_fetched_at = None
db.session.commit()
log_incoming_ap(request_json['id'], APLOG_UPDATE, APLOG_SUCCESS, request_json if store_ap_json else None)
else:
log_incoming_ap(request_json['id'], APLOG_CREATE, APLOG_FAILURE, request_json if store_ap_json else None, 'Unacceptable type (create): ' + object_type)
return
# -- below this point is code that will be incrementally replaced to use log_incoming_ap() instead --

View file

@ -2291,7 +2291,7 @@ def can_delete(user_ap_id, post):
return can_edit(user_ap_id, post)
def resolve_remote_post(uri: str, community_id: int, announce_actor=None) -> Union[Post, PostReply, None]:
def resolve_remote_post(uri: str, community_id: int, announce_actor=None, store_ap_json=False) -> Union[Post, PostReply, None]:
post = Post.query.filter_by(ap_id=uri).first()
if post:
return post
@ -2371,18 +2371,14 @@ def resolve_remote_post(uri: str, community_id: int, announce_actor=None) -> Uni
if not community_found:
return None
activity_log = ActivityPubLog(direction='in', activity_id=post_data['id'], activity_type='Resolve Post', result='failure')
if site.log_activitypub_json:
activity_log.activity_json = json.dumps(post_data)
db.session.add(activity_log)
user = find_actor_or_create(actor)
if user and community and post_data:
request_json = {
'id': f"https://{uri_domain}/activities/create/gibberish(15)",
'id': f"https://{uri_domain}/activities/create/{gibberish(15)}",
'object': post_data
}
if 'inReplyTo' in request_json['object'] and request_json['object']['inReplyTo']:
post_reply = create_post_reply(activity_log, community, request_json['object']['inReplyTo'], request_json, user)
post_reply = create_post_reply(store_ap_json, community, request_json['object']['inReplyTo'], request_json, user)
if post_reply:
if 'published' in post_data:
post_reply.posted_at = post_data['published']
@ -2391,7 +2387,7 @@ def resolve_remote_post(uri: str, community_id: int, announce_actor=None) -> Uni
db.session.commit()
return post_reply
else:
post = create_post(activity_log, community, request_json, user)
post = create_post(store_ap_json, community, request_json, user)
if post:
if 'published' in post_data:
post.posted_at=post_data['published']