From 28030547674478221f44e3826d290de22a14fa6b Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 21 Jan 2025 03:04:20 +0000 Subject: [PATCH] Share remote_object_to_json() --- app/activitypub/util.py | 35 +---------------------------------- app/community/util.py | 40 +--------------------------------------- 2 files changed, 2 insertions(+), 73 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 8c0b78d8..47794cc2 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -2181,7 +2181,6 @@ def can_delete(user_ap_id, post): return can_edit(user_ap_id, post) -# TODO: import this into community/util for backfilling, instead of having 2 copies, and - also - call it from resolve_remote_post() def remote_object_to_json(uri): try: object_request = get_request(uri, headers={'Accept': 'application/activity+json'}) @@ -2233,39 +2232,7 @@ def resolve_remote_post(uri: str, community, announce_id, store_ap_json, nodebb= actor_domain = None actor = None - try: - object_request = get_request(uri, headers={'Accept': 'application/activity+json'}) - except httpx.HTTPError: - time.sleep(3) - try: - object_request = get_request(uri, headers={'Accept': 'application/activity+json'}) - except httpx.HTTPError: - return None - if object_request.status_code == 200: - try: - post_data = object_request.json() - except: - object_request.close() - return None - object_request.close() - elif object_request.status_code == 401: - try: - site = Site.query.get(1) - object_request = signed_get_request(uri, site.private_key, f"https://{current_app.config['SERVER_NAME']}/actor#main-key") - except httpx.HTTPError: - time.sleep(3) - try: - object_request = signed_get_request(uri, site.private_key, f"https://{current_app.config['SERVER_NAME']}/actor#main-key") - except httpx.HTTPError: - return None - try: - post_data = object_request.json() - except: - object_request.close() - return None - object_request.close() - else: - return None + post_data = remote_object_to_json(uri) # find the author. Make sure their domain matches the site hosting it to mitigate impersonation attempts if 'attributedTo' in post_data: diff --git a/app/community/util.py b/app/community/util.py index c2a3dba8..f190f90f 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -12,7 +12,7 @@ from pillow_heif import register_heif_opener from app import db, cache, celery from app.activitypub.signature import post_request, default_context, signed_get_request from app.activitypub.util import find_actor_or_create, actor_json_to_model, ensure_domains_match, \ - find_hashtag_or_create, create_post + find_hashtag_or_create, create_post, remote_object_to_json from app.constants import POST_TYPE_ARTICLE, POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_VIDEO, NOTIF_POST, \ POST_TYPE_POLL from app.models import Community, File, BannedInstances, PostReply, Post, utcnow, CommunityMember, Site, \ @@ -79,44 +79,6 @@ def search_for_community(address: str): return None -def remote_object_to_json(uri): - try: - object_request = get_request(uri, headers={'Accept': 'application/activity+json'}) - except httpx.HTTPError: - time.sleep(3) - try: - object_request = get_request(uri, headers={'Accept': 'application/activity+json'}) - except httpx.HTTPError: - return None - if object_request.status_code == 200: - try: - object = object_request.json() - return object - except: - object_request.close() - return None - object_request.close() - elif object_request.status_code == 401: - try: - site = Site.query.get(1) - object_request = signed_get_request(uri, site.private_key, f"https://{current_app.config['SERVER_NAME']}/actor#main-key") - except httpx.HTTPError: - time.sleep(3) - try: - object_request = signed_get_request(uri, site.private_key, f"https://{current_app.config['SERVER_NAME']}/actor#main-key") - except httpx.HTTPError: - return None - try: - object = object_request.json() - return object - except: - object_request.close() - return None - object_request.close() - else: - return None - - @celery.task def retrieve_mods_and_backfill(community_id: int, server, name, community_json=None): with current_app.app_context():