Process ActivityPub post locks and unlocks

This commit is contained in:
freamon 2024-09-01 22:42:43 +00:00
parent 38b944c323
commit a0fe7a58a7
2 changed files with 31 additions and 1 deletions

View file

@ -25,7 +25,8 @@ 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, unban_local_user
inform_followers_of_post_update, comment_model_to_json, restore_post_or_comment, ban_local_user, unban_local_user, \
lock_post
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, \
@ -864,6 +865,12 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
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']['object']['type'] == 'Lock' and 'object' in request_json['object']['object']:
activity_log.activity_type = 'Post Unlock'
mod_ap_id = request_json['object']['object']['actor']
post_id = request_json['object']['object']['object']
lock_post(mod_ap_id, post_id, True)
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']
@ -918,6 +925,12 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
if user_ap_id.startswith('https://' + current_app.config['SERVER_NAME']):
ban_local_user(deletor_ap_id, user_ap_id, target, request_json['object'])
activity_log.result = 'success'
elif request_json['object']['type'] == 'Lock' and 'object' in request_json['object']:
activity_log.activity_type = 'Post Lock'
mod_ap_id = request_json['object']['actor']
post_id = request_json['object']['object']
lock_post(mod_ap_id, post_id, False)
activity_log.result = 'success'
else:
activity_log.exception_message = 'Invalid type for Announce'

View file

@ -1685,6 +1685,23 @@ def unban_local_user_task(deletor_ap_id, user_ap_id, target):
add_to_modlog_activitypub('unban_user', deletor, community_id=community.id, link_text=user.display_name(), link=user.link())
def lock_post(mod_ap_id, post_id, comments_enabled):
if current_app.debug:
lock_post_task(mod_ap_id, post_id, comments_enabled)
else:
lock_post_task.delay(mod_ap_id, post_id, comments_enabled)
@celery.task
def lock_post_task(mod_ap_id, post_id, comments_enabled):
mod = find_actor_or_create(mod_ap_id, create_if_not_found=False)
post = Post.query.filter_by(ap_id=post_id).first()
if mod and post:
if post.community.is_moderator(mod) or post.community.is_instance_admin(mod):
post.comments_enabled = comments_enabled
db.session.commit()
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'