mentions in post bodies - inbound federation

This commit is contained in:
freamon 2025-01-17 18:35:13 +00:00
parent 6a4e89149f
commit 022d1d67dc

View file

@ -1571,8 +1571,8 @@ def create_post_reply(store_ap_json, community: Community, in_reply_to, request_
# Check for Mentions of local users # Check for Mentions of local users
reply_parent = parent_comment if parent_comment else post reply_parent = parent_comment if parent_comment else post
local_users_to_notify = [] local_users_to_notify = []
if 'tag' in request_json and isinstance(request_json['tag'], list): if 'tag' in request_json['object'] and isinstance(request_json['object']['tag'], list):
for json_tag in request_json['tag']: for json_tag in request_json['object']['tag']:
if 'type' in json_tag and json_tag['type'] == 'Mention': if 'type' in json_tag and json_tag['type'] == 'Mention':
profile_id = json_tag['href'] if 'href' in json_tag else None profile_id = json_tag['href'] if 'href' in json_tag else None
if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']): if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']):
@ -1612,6 +1612,23 @@ def create_post(store_ap_json, community: Community, request_json: dict, user: U
return None return None
try: try:
post = Post.new(user, community, request_json, announce_id) post = Post.new(user, community, request_json, announce_id)
# can't do this in app.models, 'cos can't import blocked_users
if 'tag' in request_json['object'] and isinstance(request_json['object']['tag'], list):
for json_tag in request_json['object']['tag']:
if 'type' in json_tag and json_tag['type'] == 'Mention':
profile_id = json_tag['href'] if 'href' in json_tag else None
if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']):
profile_id = profile_id.lower()
recipient = User.query.filter_by(ap_profile_id=profile_id, ap_id=None).first()
if recipient:
blocked_senders = blocked_users(recipient.id)
if post.user_id not in blocked_senders:
notification = Notification(user_id=recipient.id, title=_(f"You have been mentioned in post {post.id}"),
url=f"https://{current_app.config['SERVER_NAME']}/post/{post.id}",
author_id=post.user_id)
recipient.unread_notifications += 1
db.session.add(notification)
db.session.commit()
return post return post
except Exception as ex: except Exception as ex:
log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, str(ex)) log_incoming_ap(id, APLOG_CREATE, APLOG_FAILURE, saved_json, str(ex))
@ -1690,8 +1707,8 @@ def update_post_reply_from_activity(reply: PostReply, request_json: dict):
reply.edited_at = utcnow() reply.edited_at = utcnow()
# Check for Mentions of local users (that weren't in the original) # Check for Mentions of local users (that weren't in the original)
if 'tag' in request_json and isinstance(request_json['tag'], list): if 'tag' in request_json['object'] and isinstance(request_json['object']['tag'], list):
for json_tag in request_json['tag']: for json_tag in request_json['object']['tag']:
if 'type' in json_tag and json_tag['type'] == 'Mention': if 'type' in json_tag and json_tag['type'] == 'Mention':
profile_id = json_tag['href'] if 'href' in json_tag else None profile_id = json_tag['href'] if 'href' in json_tag else None
if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']): if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']):
@ -1778,6 +1795,21 @@ def update_post_from_activity(post: Post, request_json: dict):
hashtag = find_hashtag_or_create(json_tag['name']) hashtag = find_hashtag_or_create(json_tag['name'])
if hashtag: if hashtag:
post.tags.append(hashtag) post.tags.append(hashtag)
if 'type' in json_tag and json_tag['type'] == 'Mention':
profile_id = json_tag['href'] if 'href' in json_tag else None
if profile_id and isinstance(profile_id, str) and profile_id.startswith('https://' + current_app.config['SERVER_NAME']):
profile_id = profile_id.lower()
recipient = User.query.filter_by(ap_profile_id=profile_id, ap_id=None).first()
if recipient:
blocked_senders = blocked_users(recipient.id)
if post.user_id not in blocked_senders:
existing_notification = Notification.query.filter(Notification.user_id == recipient.id, Notification.url == f"https://{current_app.config['SERVER_NAME']}/post/{post.id}").first()
if not existing_notification:
notification = Notification(user_id=recipient.id, title=_(f"You have been mentioned in post {post.id}"),
url=f"https://{current_app.config['SERVER_NAME']}/post/{post.id}",
author_id=post.user_id)
recipient.unread_notifications += 1
db.session.add(notification)
post.comments_enabled = request_json['object']['commentsEnabled'] if 'commentsEnabled' in request_json['object'] else True post.comments_enabled = request_json['object']['commentsEnabled'] if 'commentsEnabled' in request_json['object'] else True
post.edited_at = utcnow() post.edited_at = utcnow()