Move cross-post logic out of routes and into util for delete/restore

This commit is contained in:
freamon 2024-10-30 14:32:00 +00:00
parent bdbd33f513
commit 1611875936
2 changed files with 17 additions and 7 deletions

View file

@ -830,13 +830,6 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
activity_log.result = 'failure' activity_log.result = 'failure'
activity_log.exception_message = 'dict instead of string ' + str(to_be_deleted_ap_id) activity_log.exception_message = 'dict instead of string ' + str(to_be_deleted_ap_id)
else: 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) 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 elif request_json['object']['type'] == 'Page': # Sent for Mastodon's benefit
activity_log.result = 'ignored' activity_log.result = 'ignored'

View file

@ -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 to_delete.deleted_by = deletor.id
community.post_count -= 1 community.post_count -= 1
to_delete.author.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() db.session.commit()
if to_delete.author.id != deletor.id: if to_delete.author.id != deletor.id:
add_to_modlog_activitypub('delete_post', deletor, community_id=community.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 to_restore.deleted_by = None
community.post_count += 1 community.post_count += 1
to_restore.author.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() db.session.commit()
if to_restore.author.id != restorer.id: if to_restore.author.id != restorer.id:
add_to_modlog_activitypub('restore_post', restorer, community_id=community.id, add_to_modlog_activitypub('restore_post', restorer, community_id=community.id,