post soft-deletion: delay calling delete_dependencies until a post is purged, to allow for restoration

This commit is contained in:
freamon 2024-10-25 06:17:10 +00:00
parent b635dc5ee8
commit 899bf834cd
5 changed files with 33 additions and 25 deletions

View file

@ -1108,21 +1108,26 @@ 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 can_delete(request_json['actor'], 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()
announce_activity_to_followers(post.community, post.author, request_json)
post.deleted = True
post.author.post_count -= 1
db.session.commit()
activity_log.result = 'success'
deletor = find_actor_or_create(request_json['actor'], create_if_not_found=False)
if deletor:
if post.author.id == deletor.id or post.community.is_moderator(deletor) or post.community.is_instance_admin(deletor):
post.deleted = True
post.delted_by = deletor.id
post.author.post_count -= 1
post.community.post_count -= 1
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)
announce_activity_to_followers(post.community, post.author, request_json)
db.session.commit()
activity_log.result = 'success'
else:
activity_log.exception_message = 'Delete attempt denied'
else:
activity_log.exception_message = 'Delete attempt denied'
activity_log.exception_message = 'Deletor did not already exist'
else:
# Delete PostReply
reply = PostReply.query.filter_by(ap_id=ap_id).first()

View file

@ -1323,24 +1323,22 @@ def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id
return
if deletor and community and to_delete:
if deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor) or to_delete.author.id == deletor.id:
if to_delete.author.id == deletor.id or deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor):
if isinstance(to_delete, Post):
to_delete.delete_dependencies()
to_delete.deleted = True
to_delete.deleted_by = deletor.id
community.post_count -= 1
to_delete.author.post_count -= 1
to_delete.deleted_by = deletor.id
db.session.commit()
if to_delete.author.id != deletor.id:
add_to_modlog_activitypub('delete_post', deletor, community_id=community.id,
link_text=shorten_string(to_delete.title), link=f'post/{to_delete.id}')
elif isinstance(to_delete, PostReply):
if not to_delete.author.bot:
to_delete.post.reply_count -= 1
to_delete.deleted = True
to_delete.deleted_by = deletor.id
to_delete.author.post_reply_count -= 1
to_delete.deleted_by = deletor.id
if not to_delete.author.bot:
to_delete.post.reply_count -= 1
db.session.commit()
if to_delete.author.id != deletor.id:
add_to_modlog_activitypub('delete_post_reply', deletor, community_id=community.id,

View file

@ -511,8 +511,8 @@ def delete_post_from_community(post_id):
def delete_post_from_community_task(post_id):
post = Post.query.get(post_id)
community = post.community
post.delete_dependencies()
post.deleted = True
post.deleted_by = current_user.id
db.session.commit()
if not community.local_only:

View file

@ -1377,10 +1377,16 @@ class Post(db.Model):
db.session.query(PollChoice).filter(PollChoice.post_id == self.id).delete()
db.session.query(Poll).filter(Poll.post_id == self.id).delete()
db.session.query(Report).filter(Report.suspect_post_id == self.id).delete()
db.session.execute(text('DELETE FROM "post_reply_vote" WHERE post_reply_id IN (SELECT id FROM post_reply WHERE post_id = :post_id)'),
{'post_id': self.id})
db.session.execute(text('DELETE FROM "post_reply" WHERE post_id = :post_id'), {'post_id': self.id})
db.session.execute(text('DELETE FROM "post_vote" WHERE post_id = :post_id'), {'post_id': self.id})
reply_ids = db.session.execute(text('SELECT id FROM "post_reply" WHERE post_id = :post_id'), {'post_id': self.id}).scalars()
reply_ids = tuple(reply_ids)
if reply_ids:
db.session.execute(text('DELETE FROM "post_reply_vote" WHERE post_reply_id IN :reply_ids'), {'reply_ids': reply_ids})
db.session.execute(text('DELETE FROM "post_reply_bookmark" WHERE post_reply_id IN :reply_ids'), {'reply_ids': reply_ids})
db.session.execute(text('DELETE FROM "report" WHERE suspect_post_reply_id IN :reply_ids'), {'reply_ids': reply_ids})
db.session.execute(text('DELETE FROM "post_reply" WHERE post_id = :post_id'), {'post_id': self.id})
if self.image_id:
file = File.query.get(self.image_id)
file.delete_from_disk()

View file

@ -1025,7 +1025,6 @@ def post_delete_post(community: Community, post: Post, user_id: int, federate_al
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)
post.delete_dependencies()
post.deleted = True
post.deleted_by = user_id
post.author.post_count -= 1