From 022d1d67dcf965bc09509e88aa189e3cd021def4 Mon Sep 17 00:00:00 2001 From: freamon Date: Fri, 17 Jan 2025 18:35:13 +0000 Subject: [PATCH] mentions in post bodies - inbound federation --- app/activitypub/util.py | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 898a509c..040a468a 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1571,8 +1571,8 @@ def create_post_reply(store_ap_json, community: Community, in_reply_to, request_ # Check for Mentions of local users reply_parent = parent_comment if parent_comment else post local_users_to_notify = [] - if 'tag' in request_json and isinstance(request_json['tag'], list): - for json_tag in request_json['tag']: + 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']): @@ -1612,6 +1612,23 @@ def create_post(store_ap_json, community: Community, request_json: dict, user: U return None try: 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 except Exception as 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() # Check for Mentions of local users (that weren't in the original) - if 'tag' in request_json and isinstance(request_json['tag'], list): - for json_tag in request_json['tag']: + 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']): @@ -1778,6 +1795,21 @@ def update_post_from_activity(post: Post, request_json: dict): hashtag = find_hashtag_or_create(json_tag['name']) if 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.edited_at = utcnow()