Skip to content

Commit

Permalink
use raise_=False in more Protocol.load calls
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Oct 4, 2024
1 parent 11fff7d commit d37f83b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
3 changes: 1 addition & 2 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ def target_for(cls, obj, shared=False):
or (obj.key and inner_id == obj.key.id())):
continue

# TODO: need a "soft" kwarg for load to suppress errors?
actor = cls.load(inner_id)
actor = cls.load(inner_id, raise_=False)
if actor and actor.as1:
target = cls.target_for(actor, shared=shared)
if target:
Expand Down
7 changes: 2 additions & 5 deletions atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,8 @@ def get_record(self, repo=None, collection=None, rkey=None):

# remote record that we may have a cached copy of
if not record:
try:
if obj := ATProto.load(uri):
record = obj.bsky
except RequestException as e:
util.interpret_http_exception(e)
if obj := ATProto.load(uri, raise_=False):
record = obj.bsky

if record:
return {
Expand Down
9 changes: 7 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,17 @@ def profile_id(self):
"""
return ids.profile_id(id=self.key.id(), proto=self)

def reload_profile(self):
def reload_profile(self, **kwargs):
"""Reloads this user's identity and profile from their native protocol.
Populates the reloaded profile :class:`Object` in ``self.obj``.
Args:
kwargs: passed through to :meth:`Protocol.load`
"""
self.obj = self.load(self.profile_id(), remote=True)
obj = self.load(self.profile_id(), remote=True, **kwargs)
if obj:
self.obj = obj

def user_page_path(self, rest=None):
"""Returns the user's Bridgy Fed user page path."""
Expand Down
6 changes: 3 additions & 3 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def receive(from_cls, obj, authed_as=None, internal=False):
if (actor and actor.keys() == set(['id'])
and obj.type not in ('delete', 'undo')):
logger.debug('Fetching actor so we have name, profile photo, etc')
actor_obj = from_cls.load(actor['id'])
actor_obj = from_cls.load(actor['id'], raise_=False)
if actor_obj and actor_obj.as1:
obj.our_as1 = {**obj.as1, 'actor': actor_obj.as1}

Expand All @@ -976,7 +976,7 @@ def receive(from_cls, obj, authed_as=None, internal=False):
and inner_obj_as1.keys() == set(['id'])
and from_cls.owns_id(inner_obj_id)):
logger.debug('Fetching object so we can render it in feeds')
inner_obj = from_cls.load(inner_obj_id)
inner_obj = from_cls.load(inner_obj_id, raise_=False)
if inner_obj and inner_obj.as1:
obj.our_as1 = {
**obj.as1,
Expand Down Expand Up @@ -1035,7 +1035,7 @@ def handle_follow(from_cls, obj):
if not from_id:
error(f'Follow activity requires actor. Got: {obj.as1}')

from_obj = from_cls.load(from_id)
from_obj = from_cls.load(from_id, raise_=False)
if not from_obj:
error(f"Couldn't load {from_id}", status=502)

Expand Down
11 changes: 4 additions & 7 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,9 @@ def verify(self):
# check home page
self.obj = None
self.has_hcard = False
try:
self.obj = Web.load(self.web_url(), remote=True, gateway=True)
if self.obj:
self.has_hcard = True
except (BadRequest, NotFound):
pass
self.reload_profile(gateway=True, raise_=False)
if self.obj:
self.has_hcard = True

self.put()
return self
Expand Down Expand Up @@ -602,7 +599,7 @@ def _convert(cls, obj, from_user=None):
for field in 'author', 'actor':
val = as1.get_object(obj.as1, field)
if val.keys() == set(['id']) and val['id']:
loaded = from_proto.load(val['id'])
loaded = from_proto.load(val['id'], raise_=False)
if loaded and loaded.as1:
obj_as1 = {**obj_as1, field: loaded.as1}
else:
Expand Down

0 comments on commit d37f83b

Please sign in to comment.