Skip to content

Commit

Permalink
Protocol.receive: on create/update without full object, fetch it
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 20, 2025
1 parent 9e800a4 commit 5603e46
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
13 changes: 9 additions & 4 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,12 +1046,15 @@ def receive(from_cls, obj, authed_as=None, internal=False, received_at=None):
if actor_obj and actor_obj.as1:
obj.our_as1 = {**obj.as1, 'actor': actor_obj.as1}

# fetch object if necessary so we can render it in feeds
if (obj.type == 'share'
# fetch object if necessary
if (obj.type in ('post', 'update', 'share')
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, raise_=False)
logger.debug('Fetching inner object')
inner_obj = from_cls.load(inner_obj_id, raise_=False,
remote=(obj.type in ('post', 'update')))
if obj.type in ('post', 'update'):
crud_obj = inner_obj
if inner_obj and inner_obj.as1:
obj.our_as1 = {
**obj.as1,
Expand All @@ -1060,6 +1063,8 @@ def receive(from_cls, obj, authed_as=None, internal=False, received_at=None):
**inner_obj.as1,
}
}
elif obj.type in ('post', 'update'):
error("Need object {inner_obj_id} but couldn't fetch, giving up")

if obj.type == 'follow':
if proto := Protocol.for_bridgy_subdomain(inner_obj_id):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,49 @@ def test_update_post_bare_object(self):
'fake:post#bridgy-fed-update-2022-01-02T03:04:05+00:00'))
self.assertEqual([], Fake.sent)

def test_update_post_fetch_object(self):
post = {
'id': 'fake:post',
'objectType': 'note',
'author': 'fake:user',
}
Fake.fetchable['fake:post'] = post

self.store_object(id='fake:post', source_protocol='fake')

update = {
'id': 'fake:update',
'objectType': 'activity',
'verb': 'update',
'actor': 'fake:user',
'object': 'fake:post',
}
_, status = Fake.receive_as1(update)
self.assertEqual(204, status)

self.assertEqual(['fake:post'], Fake.fetched)
self.assert_object('fake:post',
our_as1=post,
type='note',
)
self.assertIsNone(Object.get_by_id('fake:update'))

def test_update_post_fetch_object_fails(self):
self.store_object(id='fake:post', source_protocol='fake')

update = {
'id': 'fake:update',
'objectType': 'activity',
'verb': 'update',
'actor': 'fake:user',
'object': 'fake:post',
}
with self.assertRaises(ErrorButDoNotRetryTask):
Fake.receive_as1(update)

self.assertEqual(['fake:post'], Fake.fetched)
self.assertIsNone(Object.get_by_id('fake:update'))

def test_create_reply(self):
eve = self.make_user('other:eve', cls=OtherFake, obj_id='other:eve')
frank = self.make_user('other:frank', cls=OtherFake, obj_id='other:frank')
Expand Down

0 comments on commit 5603e46

Please sign in to comment.