From 1611875936bdbe6ba11b41568a5e5ae9810156f5 Mon Sep 17 00:00:00 2001 From: freamon Date: Wed, 30 Oct 2024 14:32:00 +0000 Subject: [PATCH] Move cross-post logic out of routes and into util for delete/restore --- app/activitypub/routes.py | 7 ------- app/activitypub/util.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 6c36afde..92d069cd 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -830,13 +830,6 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): activity_log.result = 'failure' activity_log.exception_message = 'dict instead of string ' + str(to_be_deleted_ap_id) else: - post = Post.query.filter_by(ap_id=to_be_deleted_ap_id).first() - if post and post.url and post.cross_posts is not None: - old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all() - post.cross_posts.clear() - for ocp in old_cross_posts: - if ocp.cross_posts is not None and post.id in ocp.cross_posts: - ocp.cross_posts.remove(post.id) delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id, activity_log.id) elif request_json['object']['type'] == 'Page': # Sent for Mastodon's benefit activity_log.result = 'ignored' diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 2c8cdf99..5c759396 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1321,6 +1321,12 @@ def delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id, apl to_delete.deleted_by = deletor.id community.post_count -= 1 to_delete.author.post_count -= 1 + if to_delete.url and to_delete.cross_posts is not None: + old_cross_posts = Post.query.filter(Post.id.in_(to_delete.cross_posts)).all() + to_delete.cross_posts.clear() + for ocp in old_cross_posts: + if ocp.cross_posts is not None and to_delete.id in ocp.cross_posts: + ocp.cross_posts.remove(to_delete.id) db.session.commit() if to_delete.author.id != deletor.id: add_to_modlog_activitypub('delete_post', deletor, community_id=community.id, @@ -1369,6 +1375,17 @@ def restore_post_or_comment(object_json, aplog_id): to_restore.deleted_by = None community.post_count += 1 to_restore.author.post_count += 1 + new_cross_posts = Post.query.filter(Post.id != to_restore.id, Post.url == to_restore.url, Post.deleted == False, + Post.posted_at > utcnow() - timedelta(days=6)).all() + for ncp in new_cross_posts: + if ncp.cross_posts is None: + ncp.cross_posts = [to_restore.id] + else: + ncp.cross_posts.append(to_restore.id) + if to_restore.cross_posts is None: + to_restore.cross_posts = [ncp.id] + else: + to_restore.cross_posts.append(ncp.id) db.session.commit() if to_restore.author.id != restorer.id: add_to_modlog_activitypub('restore_post', restorer, community_id=community.id,