mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 11:26:56 -08:00
Share remote_object_to_json()
This commit is contained in:
parent
4eb9259b0e
commit
2803054767
2 changed files with 2 additions and 73 deletions
|
@ -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:
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in a new issue