From d9dc0d2960d51c3afefc3086fa525633e4e7c4de Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 2 Apr 2024 17:44:26 +0100 Subject: [PATCH] Fixup cross posts if post is removed --- app/activitypub/routes.py | 13 +++++++++++++ app/post/routes.py | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index cae7a644..fe5950ae 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -629,6 +629,13 @@ 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: + ocp.cross_posts.remove(post.id) delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id) activity_log.result = 'success' elif request_json['object']['type'] == 'Page': # Editing a post @@ -858,6 +865,12 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): post = Post.query.filter_by(ap_id=ap_id).first() # Delete post if post: + if 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: + ocp.cross_posts.remove(post.id) post.delete_dependencies() post.community.post_count -= 1 db.session.delete(post) diff --git a/app/post/routes.py b/app/post/routes.py index 6647d271..d2777673 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -831,6 +831,13 @@ def post_delete(post_id: int): post = Post.query.get_or_404(post_id) community = post.community if post.user_id == current_user.id or community.is_moderator() or current_user.is_admin(): + if post.url: + if 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: + ocp.cross_posts.remove(post.id) post.delete_dependencies() post.flush_cache() db.session.delete(post)