From 474ff8c19409ec4ebca505bbfe39fdb0a43092c6 Mon Sep 17 00:00:00 2001 From: freamon Date: Fri, 16 Aug 2024 15:04:54 +0000 Subject: [PATCH] Initial work for posts that have been removed and then restored #276 --- app/activitypub/routes.py | 6 +++++- app/activitypub/util.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 911fae93..74244ac3 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -22,7 +22,7 @@ from app.activitypub.util import public_key, users_total, active_half_year, acti user_removed_from_remote_server, create_post, create_post_reply, update_post_reply_from_activity, \ update_post_from_activity, undo_vote, undo_downvote, post_to_page, get_redis_connection, find_reported_object, \ process_report, ensure_domains_match, can_edit, can_delete, remove_data_from_banned_user, resolve_remote_post, \ - inform_followers_of_post_update, comment_model_to_json + inform_followers_of_post_update, comment_model_to_json, restore_post_or_comment from app.utils import gibberish, get_setting, is_image_url, allowlist_html, render_template, \ domain_from_url, markdown_to_html, community_membership, ap_datetime, ip_address, can_downvote, \ can_upvote, can_create_post, awaken_dormant_instance, shorten_string, can_create_post_reply, sha256_digest, \ @@ -839,6 +839,10 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): target_ap_id = request_json['object']['object']['object'] # object object object! post = undo_vote(activity_log, comment, post, target_ap_id, user) activity_log.result = 'success' + elif request_json['object']['object']['type'] == 'Delete': + if 'object' in request_json and 'object' in request_json['object']: + restore_post_or_comment(request_json['object']['object']) + activity_log.result = 'success' elif request_json['object']['type'] == 'Add' and 'target' in request_json['object']: activity_log.activity_type = request_json['object']['type'] target = request_json['object']['target'] diff --git a/app/activitypub/util.py b/app/activitypub/util.py index ba92da49..cafaacc1 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1472,6 +1472,33 @@ def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id link=f'post/{to_delete.post.id}#comment_{to_delete.id}') +def restore_post_or_comment(object_json): + if current_app.debug: + restore_post_or_comment_task(object_json) + else: + restore_post_or_comment_task.delay(object_json) + + +@celery.task +def restore_post_or_comment_task(object_json): + restorer = find_actor_or_create(object_json['actor']) if 'actor' in object_json else None + community = find_actor_or_create(object_json['audience'], community_only=True) if 'audience' in object_json else None + to_restore = find_liked_object(object_json['object']) if 'object' in object_json else None + + if restorer and community and to_restore: + if restorer.is_admin() or community.is_moderator(restorer) or community.is_instance_admin(restorer) or to_restore.author.id == restorer.id: + if isinstance(to_restore, Post): + # TODO: restore_dependencies() + to_restore.deleted = False + community.post_count += 1 + db.session.commit() + if to_restore.author.id != restorer.id: + add_to_modlog_activitypub('restore_post', restorer, community_id=community.id, + link_text=shorten_string(to_restore.title), link=f'post/{to_restore.id}') + + # TODO: if isinstance(to_restore, PostReply): + + def remove_data_from_banned_user(deletor_ap_id, user_ap_id, target): if current_app.debug: remove_data_from_banned_user_task(deletor_ap_id, user_ap_id, target)