mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-02-03 00:31:25 -08:00
ActivityPub - process ban reversals for local users from remote mods & admins
This commit is contained in:
parent
79ab687b5b
commit
ab0a1816b3
2 changed files with 74 additions and 2 deletions
|
@ -25,7 +25,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, restore_post_or_comment, ban_local_user
|
||||
inform_followers_of_post_update, comment_model_to_json, restore_post_or_comment, ban_local_user, unban_local_user
|
||||
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, \
|
||||
|
@ -856,6 +856,14 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
|
|||
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']['object']['type'] == 'Block':
|
||||
activity_log.activity_type = 'Undo User Ban'
|
||||
deletor_ap_id = request_json['object']['object']['actor']
|
||||
user_ap_id = request_json['object']['object']['object']
|
||||
target = request_json['object']['object']['target']
|
||||
if target == request_json['actor'] and user_ap_id.startswith('https://' + current_app.config['SERVER_NAME']):
|
||||
unban_local_user(deletor_ap_id, user_ap_id, target)
|
||||
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']
|
||||
|
@ -1053,6 +1061,14 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
|
|||
if post_or_comment:
|
||||
announce_activity_to_followers(post_or_comment.community, user, request_json)
|
||||
activity_log.result = 'success'
|
||||
elif request_json['object']['type'] == 'Block': # Undoing a ban
|
||||
activity_log.activity_type = 'Undo User Ban'
|
||||
deletor_ap_id = request_json['object']['actor']
|
||||
user_ap_id = request_json['object']['object']
|
||||
target = request_json['object']['target']
|
||||
if user_ap_id.startswith('https://' + current_app.config['SERVER_NAME']):
|
||||
unban_local_user(deletor_ap_id, user_ap_id, target)
|
||||
activity_log.result = 'success'
|
||||
elif request_json['type'] == 'Delete':
|
||||
if isinstance(request_json['object'], str):
|
||||
ap_id = request_json['object'] # lemmy
|
||||
|
|
|
@ -1568,7 +1568,7 @@ def ban_local_user_task(deletor_ap_id, user_ap_id, target, request_json):
|
|||
# same info in 'Block' and 'Announce/Block' can be sent at same time, and both call this function
|
||||
ban_in_progress = cache.get(f'{deletor_ap_id} is banning {user_ap_id} from {target}')
|
||||
if not ban_in_progress:
|
||||
cache.set(f'{deletor_ap_id} is banning {user_ap_id} from {target}', True, timeout=300)
|
||||
cache.set(f'{deletor_ap_id} is banning {user_ap_id} from {target}', True, timeout=60)
|
||||
else:
|
||||
return
|
||||
|
||||
|
@ -1628,6 +1628,62 @@ def ban_local_user_task(deletor_ap_id, user_ap_id, target, request_json):
|
|||
add_to_modlog_activitypub('ban_user', deletor, community_id=community.id, link_text=user.display_name(), link=user.link())
|
||||
|
||||
|
||||
def unban_local_user(deletor_ap_id, user_ap_id, target):
|
||||
if current_app.debug:
|
||||
unban_local_user_task(deletor_ap_id, user_ap_id, target)
|
||||
else:
|
||||
unban_local_user_task.delay(deletor_ap_id, user_ap_id, target)
|
||||
|
||||
|
||||
@celery.task
|
||||
def unban_local_user_task(deletor_ap_id, user_ap_id, target):
|
||||
# same info in 'Block' and 'Announce/Block' can be sent at same time, and both call this function
|
||||
unban_in_progress = cache.get(f'{deletor_ap_id} is undoing ban of {user_ap_id} from {target}')
|
||||
if not unban_in_progress:
|
||||
cache.set(f'{deletor_ap_id} is undoing ban of {user_ap_id} from {target}', True, timeout=60)
|
||||
else:
|
||||
return
|
||||
|
||||
deletor = find_actor_or_create(deletor_ap_id, create_if_not_found=False)
|
||||
user = find_actor_or_create(user_ap_id, create_if_not_found=False)
|
||||
community = Community.query.filter_by(ap_profile_id=target).first()
|
||||
|
||||
if not deletor or not user:
|
||||
return
|
||||
|
||||
# site undo bans by admins
|
||||
if deletor.instance.user_is_admin(deletor.id) and target == f"https://{deletor.instance.domain}/":
|
||||
# need instance_ban table?
|
||||
...
|
||||
|
||||
# community undo bans by mods or admins
|
||||
elif community and (community.is_moderator(deletor) or community.is_instance_admin(deletor)):
|
||||
existing_ban = CommunityBan.query.filter_by(community_id=community.id, user_id=user.id).first()
|
||||
if existing_ban:
|
||||
db.session.delete(existing_ban)
|
||||
db.session.commit()
|
||||
|
||||
community_membership_record = CommunityMember.query.filter_by(community_id=community.id, user_id=user.id).first()
|
||||
if community_membership_record:
|
||||
community_membership_record.is_banned = False
|
||||
db.session.commit()
|
||||
|
||||
cache.delete_memoized(communities_banned_from, user.id)
|
||||
cache.delete_memoized(joined_communities, user.id)
|
||||
cache.delete_memoized(moderating_communities, user.id)
|
||||
|
||||
# Notify previously banned person
|
||||
notify = Notification(title=shorten_string('You have been un-banned from ' + community.title),
|
||||
url=f'/notifications', user_id=user.id,
|
||||
author_id=deletor.id)
|
||||
db.session.add(notify)
|
||||
if not current_app.debug: # user.unread_notifications += 1 hangs app if 'user' is the same person
|
||||
user.unread_notifications += 1 # who pressed 'Re-submit this activity'.
|
||||
db.session.commit()
|
||||
|
||||
add_to_modlog_activitypub('unban_user', deletor, community_id=community.id, link_text=user.display_name(), link=user.link())
|
||||
|
||||
|
||||
def create_post_reply(activity_log: ActivityPubLog, community: Community, in_reply_to, request_json: dict, user: User, announce_id=None) -> Union[PostReply, None]:
|
||||
if community.local_only:
|
||||
activity_log.exception_message = 'Community is local only, reply discarded'
|
||||
|
|
Loading…
Add table
Reference in a new issue