mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
apf part 13: Delete requests
This commit is contained in:
parent
046a15e617
commit
115bb8426f
2 changed files with 49 additions and 49 deletions
|
@ -721,6 +721,23 @@ def process_inbox_request(request_json, store_ap_json):
|
||||||
log_incoming_ap(request_json['id'], APLOG_CREATE, APLOG_FAILURE, request_json if store_ap_json else None, 'Unacceptable type (create): ' + object_type)
|
log_incoming_ap(request_json['id'], APLOG_CREATE, APLOG_FAILURE, request_json if store_ap_json else None, 'Unacceptable type (create): ' + object_type)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if request_json['type'] == 'Delete':
|
||||||
|
if isinstance(request_json['object'], str):
|
||||||
|
ap_id = request_json['object'] # lemmy
|
||||||
|
else:
|
||||||
|
ap_id = request_json['object']['id'] # kbin
|
||||||
|
to_delete = find_liked_object(ap_id) # Just for Posts and Replies (User deletes go through process_delete_request())
|
||||||
|
|
||||||
|
if to_delete:
|
||||||
|
if to_delete.deleted:
|
||||||
|
log_incoming_ap(request_json['id'], APLOG_DELETE, APLOG_IGNORED, request_json if store_ap_json else None, 'Activity about local content which is already deleted')
|
||||||
|
else:
|
||||||
|
delete_post_or_comment(user, to_delete, store_ap_json, request_json)
|
||||||
|
announce_activity_to_followers(to_delete.community, user, request_json)
|
||||||
|
else:
|
||||||
|
log_incoming_ap(request_json['id'], APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Delete: cannot find ' + ap_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
# -- below this point is code that will be incrementally replaced to use log_incoming_ap() instead --
|
# -- below this point is code that will be incrementally replaced to use log_incoming_ap() instead --
|
||||||
|
|
||||||
|
|
|
@ -1334,56 +1334,39 @@ def is_activitypub_request():
|
||||||
return 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get('Accept', '')
|
return 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get('Accept', '')
|
||||||
|
|
||||||
|
|
||||||
def delete_post_or_comment(user_ap_id, to_be_deleted_ap_id, aplog_id):
|
def delete_post_or_comment(deletor, to_delete, store_ap_json, request_json):
|
||||||
deletor = find_actor_or_create(user_ap_id)
|
community = to_delete.community
|
||||||
to_delete = find_liked_object(to_be_deleted_ap_id)
|
if to_delete.user_id == deletor.id or deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor):
|
||||||
aplog = ActivityPubLog.query.get(aplog_id)
|
if isinstance(to_delete, Post):
|
||||||
|
to_delete.deleted = True
|
||||||
if to_delete and to_delete.deleted:
|
to_delete.deleted_by = deletor.id
|
||||||
if aplog:
|
community.post_count -= 1
|
||||||
aplog.result = 'ignored'
|
to_delete.author.post_count -= 1
|
||||||
aplog.exception_message = 'Activity about local content which is already deleted'
|
if to_delete.url and to_delete.cross_posts is not None:
|
||||||
return
|
old_cross_posts = Post.query.filter(Post.id.in_(to_delete.cross_posts)).all()
|
||||||
|
to_delete.cross_posts.clear()
|
||||||
if deletor and to_delete:
|
for ocp in old_cross_posts:
|
||||||
community = to_delete.community
|
if ocp.cross_posts is not None and to_delete.id in ocp.cross_posts:
|
||||||
if to_delete.author.id == deletor.id or deletor.is_admin() or community.is_moderator(deletor) or community.is_instance_admin(deletor):
|
ocp.cross_posts.remove(to_delete.id)
|
||||||
if isinstance(to_delete, Post):
|
db.session.commit()
|
||||||
to_delete.deleted = True
|
if to_delete.author.id != deletor.id:
|
||||||
to_delete.deleted_by = deletor.id
|
add_to_modlog_activitypub('delete_post', deletor, community_id=community.id,
|
||||||
community.post_count -= 1
|
link_text=shorten_string(to_delete.title), link=f'post/{to_delete.id}')
|
||||||
to_delete.author.post_count -= 1
|
elif isinstance(to_delete, PostReply):
|
||||||
if to_delete.url and to_delete.cross_posts is not None:
|
to_delete.deleted = True
|
||||||
old_cross_posts = Post.query.filter(Post.id.in_(to_delete.cross_posts)).all()
|
to_delete.deleted_by = deletor.id
|
||||||
to_delete.cross_posts.clear()
|
to_delete.author.post_reply_count -= 1
|
||||||
for ocp in old_cross_posts:
|
community.post_reply_count -= 1
|
||||||
if ocp.cross_posts is not None and to_delete.id in ocp.cross_posts:
|
if not to_delete.author.bot:
|
||||||
ocp.cross_posts.remove(to_delete.id)
|
to_delete.post.reply_count -= 1
|
||||||
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_reply', deletor, community_id=community.id,
|
||||||
link_text=shorten_string(to_delete.title), link=f'post/{to_delete.id}')
|
link_text=f'comment on {shorten_string(to_delete.post.title)}',
|
||||||
elif isinstance(to_delete, PostReply):
|
link=f'post/{to_delete.post.id}#comment_{to_delete.id}')
|
||||||
to_delete.deleted = True
|
log_incoming_ap(request_json['id'], APLOG_DELETE, APLOG_SUCCESS, request_json if store_ap_json else None)
|
||||||
to_delete.deleted_by = deletor.id
|
|
||||||
to_delete.author.post_reply_count -= 1
|
|
||||||
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,
|
|
||||||
link_text=f'comment on {shorten_string(to_delete.post.title)}',
|
|
||||||
link=f'post/{to_delete.post.id}#comment_{to_delete.id}')
|
|
||||||
if aplog:
|
|
||||||
aplog.result = 'success'
|
|
||||||
else:
|
|
||||||
if aplog:
|
|
||||||
aplog.result = 'failure'
|
|
||||||
aplog.exception_message = 'Deletor did not have permission'
|
|
||||||
else:
|
else:
|
||||||
if aplog:
|
log_incoming_ap(request_json['id'], APLOG_DELETE, APLOG_FAILURE, request_json if store_ap_json else None, 'Deletor did not have permisson')
|
||||||
aplog.result = 'failure'
|
|
||||||
aplog.exception_message = 'Unable to resolve deletor, or target'
|
|
||||||
|
|
||||||
|
|
||||||
def restore_post_or_comment(object_json, aplog_id):
|
def restore_post_or_comment(object_json, aplog_id):
|
||||||
|
|
Loading…
Add table
Reference in a new issue