From 3e3aca1910e8f86687a0dde416163bde76deba5e Mon Sep 17 00:00:00 2001 From: freamon Date: Sat, 18 Jan 2025 00:59:26 +0000 Subject: [PATCH] move check for incoming Mentions into post.new() --- app/activitypub/util.py | 17 ----------------- app/models.py | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 040a468a..06ff7843 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1612,23 +1612,6 @@ 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)) diff --git a/app/models.py b/app/models.py index 5c14121a..05017f65 100644 --- a/app/models.py +++ b/app/models.py @@ -1182,7 +1182,7 @@ class Post(db.Model): find_licence_or_create, make_image_sizes, notify_about_post from app.utils import allowlist_html, markdown_to_html, html_to_text, microblog_content_to_title, blocked_phrases, \ is_image_url, is_video_url, domain_from_url, opengraph_parse, shorten_string, remove_tracking_from_link, \ - is_video_hosting_site, communities_banned_from, recently_upvoted_posts + is_video_hosting_site, communities_banned_from, recently_upvoted_posts, blocked_users microblog = False if 'name' not in request_json['object']: # Microblog posts @@ -1376,6 +1376,24 @@ class Post(db.Model): db.session.rollback() return Post.query.filter_by(ap_id=request_json['object']['id'].lower()).one() + # Mentions also need a post_id + 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() + # Polls need to be processed quite late because they need a post_id to refer to if request_json['object']['type'] == 'Question': post.type = constants.POST_TYPE_POLL