switch most uses of common.get_as2 to common.get_object

pull/424/head
Ryan Barrett 2023-02-14 14:56:27 -08:00
rodzic c2e6174330
commit 588598c5ff
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
5 zmienionych plików z 17 dodań i 21 usunięć

Wyświetl plik

@ -185,12 +185,13 @@ def inbox(domain=None):
# fetch actor if necessary so we have name, profile photo, etc
if actor and isinstance(actor, str):
actor = activity['actor'] = common.get_as2(actor, user=user).json()
actor = activity['actor'] = \
json_loads(common.get_object(actor, user=user).as2)
# fetch object if necessary so we can render it in feeds
if type in FETCH_OBJECT_TYPES and isinstance(activity.get('object'), str):
obj_as2 = activity['object'] = \
common.get_as2(activity['object'], user=user).json()
json_loads(common.get_object(activity['object'], user=user).as2)
activity_unwrapped = redirect_unwrap(activity)
if type == 'Follow':

Wyświetl plik

@ -119,7 +119,7 @@ def pretty_link(url, text=None, user=None):
return util.pretty_link(url, text=text)
@cached(LRUCache(1000), lock=threading.Lock())
@cached(LRUCache(1000), key=lambda id, user=None: id, lock=threading.Lock())
def get_object(id, user=None):
"""Loads and returns an Object from memory cache, datastore, or HTTP fetch.

Wyświetl plik

@ -147,8 +147,7 @@ class FollowCallback(indieauth.Callback):
flash(f"Couldn't find ActivityPub profile link for {addr}")
return redirect(f'/user/{domain}/following')
resp = common.get_as2(as2_url, user=user)
followee = resp.json()
followee = json_loads(common.get_object(as2_url, user=user).as2)
id = followee.get('id')
inbox = followee.get('inbox')
if not id or not inbox:
@ -219,12 +218,11 @@ class UnfollowCallback(indieauth.Callback):
if isinstance(followee, str):
# fetch as AS2 to get full followee with inbox
followee_id = followee
resp = common.get_as2(followee_id, user=user)
followee = resp.json()
followee = json_loads(common.get_object(followee_id, user=user).as2)
inbox = followee.get('inbox')
if not inbox:
flash(f"AS2 profile {followee_id} missing id or inbox")
flash(f"AS2 profile {followee_id} missing inbox")
return redirect(f'/user/{domain}/following')
timestamp = NOW.replace(microsecond=0, tzinfo=None).isoformat()

Wyświetl plik

@ -304,8 +304,10 @@ class Object(StringIdModel):
def _post_put_hook(self, future):
"""Update :func:`common.get_object` cache."""
# TODO: assert that as1 id is same as key id? in pre put hook?
if self.type != 'activity':
common.get_object.cache[self.key.id()] = self
key = common.get_object.cache_key(self.key.id())
common.get_object.cache[key] = self
def proxy_url(self):
"""Returns the Bridgy Fed proxy URL to render this post as HTML."""

Wyświetl plik

@ -43,7 +43,6 @@ class Webmention(View):
source_mf2 = None # parsed mf2 dict
source_as1 = None # AS1 dict
source_as2 = None # AS2 dict
target_resp = None # requests.Response
user = None # User
def dispatch_request(self):
@ -302,20 +301,16 @@ class Webmention(View):
for target in targets:
# fetch target page as AS2 object
try:
self.target_resp = common.get_as2(target, user=self.user)
target_obj = json_loads(
common.get_object(target, user=self.user).as2)
except (requests.HTTPError, BadGateway) as e:
self.target_resp = getattr(e, 'requests_response', None)
if self.target_resp and self.target_resp.status_code // 100 == 2:
content_type = common.content_type(self.target_resp) or ''
if content_type.startswith('text/html'):
resp = getattr(e, 'requests_response', None)
if resp and resp.ok:
if (common.content_type(resp) or '').startswith('text/html'):
continue # give up
raise
target_url = self.target_resp.url or target
# find target's inbox
target_obj = self.target_resp.json()
inbox_url = target_obj.get('inbox')
if not inbox_url:
# TODO: test actor/attributedTo and not, with/without inbox
actor = (util.get_first(target_obj, 'actor') or
@ -330,7 +325,7 @@ class Webmention(View):
if not inbox_url:
# fetch actor as AS object
actor = common.get_as2(actor, user=self.user).json()
actor = json_loads(common.get_object(actor, user=self.user).as2)
inbox_url = actor.get('inbox')
if not inbox_url:
@ -338,7 +333,7 @@ class Webmention(View):
logging.error('Target actor has no inbox')
continue
inbox_url = urllib.parse.urljoin(target_url, inbox_url)
inbox_url = urllib.parse.urljoin(target, inbox_url)
inboxes_to_targets[inbox_url] = target_obj
logger.info(f"Delivering to targets' inboxes: {inboxes_to_targets.keys()}")