Merge pull request 'Local Only Communities' (#165) from freamon/pyfedi:reject_follows into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/165
This commit is contained in:
rimu 2024-04-21 01:02:36 +00:00
commit aff00042c7

View file

@ -514,6 +514,11 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
db.session.commit()
return
community = find_actor_or_create(community_ap_id, community_only=True)
if community and community.local_only:
activity_log.exception_message = 'Remote Create in local_only community'
activity_log.result = 'ignored'
db.session.commit()
return
user = find_actor_or_create(user_ap_id)
if (user and not user.is_local()) and community:
user.last_seen = community.last_active = site.last_active = utcnow()
@ -774,9 +779,28 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
follow_id = request_json['id']
user = find_actor_or_create(user_ap_id)
community = find_actor_or_create(community_ap_id, community_only=True)
if community and community.local_only:
# todo: send Deny activity
if community and community.local_only and user:
activity_log.exception_message = 'Local only cannot be followed by remote users'
# send reject message to deny the follow
reject = {
"@context": default_context(),
"actor": community.ap_profile_id,
"to": [
user.ap_profile_id
],
"object": {
"actor": user.ap_profile_id,
"to": None,
"object": community.ap_profile_id,
"type": "Follow",
"id": follow_id
},
"type": "Reject",
"id": f"https://{current_app.config['SERVER_NAME']}/activities/reject/" + gibberish(32)
}
# Lemmy doesn't yet understand Reject/Follow, so send without worrying about response for now.
post_request(user.ap_inbox_url, reject, community.private_key, f"https://{current_app.config['SERVER_NAME']}/c/{community.name}#main-key")
else:
if user is not None and community is not None:
# check if user is banned from this community