diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 3d9bac1f..79223186 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -449,7 +449,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): activity_log.activity_type = 'exception' db.session.commit() return - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) 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() @@ -501,7 +501,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): activity_log.activity_type = 'exception' db.session.commit() return - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) 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() @@ -674,7 +674,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): community_ap_id = request_json['object'] follow_id = request_json['id'] user = find_actor_or_create(user_ap_id) - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) if community and community.local_only: # todo: send Deny activity activity_log.exception_message = 'Local only cannot be followed by remote users' @@ -718,7 +718,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): community_ap_id = request_json['actor'] user_ap_id = request_json['object']['actor'] user = find_actor_or_create(user_ap_id) - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) if user and community: join_request = CommunityJoinRequest.query.filter_by(user_id=user.id, community_id=community.id).first() if join_request: @@ -734,7 +734,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): community_ap_id = request_json['object']['object'] user_ap_id = request_json['object']['actor'] user = find_actor_or_create(user_ap_id) - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) if user and community: user.last_seen = utcnow() member = CommunityMember.query.filter_by(user_id=user.id, community_id=community.id).first() diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 2209dfa5..b01c4bf2 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -196,7 +196,7 @@ def instance_allowed(host: str) -> bool: return instance is not None -def find_actor_or_create(actor: str, create_if_not_found=True) -> Union[User, Community, None]: +def find_actor_or_create(actor: str, create_if_not_found=True, community_only=False) -> Union[User, Community, None]: actor = actor.strip().lower() user = None # actor parameter must be formatted as https://server/u/actor or https://server/c/actor @@ -248,7 +248,10 @@ def find_actor_or_create(actor: str, create_if_not_found=True) -> Union[User, Co if actor_data.status_code == 200: actor_json = actor_data.json() actor_data.close() - return actor_json_to_model(actor_json, address, server) + actor_model = actor_json_to_model(actor_json, address, server) + if community_only and not isinstance(actor_model, Community): + return None + return actor_model else: # retrieve user details via webfinger, etc try: @@ -274,7 +277,10 @@ def find_actor_or_create(actor: str, create_if_not_found=True) -> Union[User, Co if actor_data.status_code == 200: actor_json = actor_data.json() actor_data.close() - return actor_json_to_model(actor_json, address, server) + actor_model = actor_json_to_model(actor_json, address, server) + if community_only and not isinstance(actor_model, Community): + return None + return actor_model return None @@ -477,7 +483,7 @@ def actor_json_to_model(activity_json, address, server): ap_id=f"{address}@{server}", ap_public_url=activity_json['id'], ap_profile_id=activity_json['id'].lower(), - ap_inbox_url=activity_json['endpoints']['sharedInbox'], + ap_inbox_url=activity_json['endpoints']['sharedInbox'] if 'endpoints' in activity_json else activity_json['inbox'] if 'inbox' in activity_json else '', ap_followers_url=activity_json['followers'] if 'followers' in activity_json else None, ap_preferred_username=activity_json['preferredUsername'], ap_manually_approves_followers=activity_json['manuallyApprovesFollowers'] if 'manuallyApprovesFollowers' in activity_json else False, @@ -1061,7 +1067,7 @@ def delete_post_or_comment(user_ap_id, community_ap_id, to_be_deleted_ap_id): @celery.task def delete_post_or_comment_task(user_ap_id, community_ap_id, to_be_deleted_ap_id): deletor = find_actor_or_create(user_ap_id) - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) to_delete = find_liked_object(to_be_deleted_ap_id) if deletor and community and to_delete: diff --git a/app/user/routes.py b/app/user/routes.py index 49bc2211..091d2338 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -551,7 +551,7 @@ def import_settings_task(user_id, filename): # Follow communities for community_ap_id in contents_json['followed_communities'] if 'followed_communities' in contents_json else []: - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) if community: if community.posts.count() == 0: if current_app.debug: @@ -585,7 +585,7 @@ def import_settings_task(user_id, filename): cache.delete_memoized(community_membership, current_user, community) for community_ap_id in contents_json['blocked_communities'] if 'blocked_communities' in contents_json else []: - community = find_actor_or_create(community_ap_id) + community = find_actor_or_create(community_ap_id, community_only=True) if community: existing_block = CommunityBlock.query.filter_by(user_id=user.id, community_id=community.id).first() if not existing_block: