From 0364ece4a8cd45289d019e900a961c01d9957822 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Sun, 12 Nov 2023 13:21:35 -0800 Subject: [PATCH] stop resolving ids in Protocol.targets, since Protocol.receive already did it --- protocol.py | 12 +---- tests/test_protocol.py | 104 +++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/protocol.py b/protocol.py index fc817b7..5c0670e 100644 --- a/protocol.py +++ b/protocol.py @@ -907,7 +907,7 @@ class Protocol: if not targets: obj.status = 'ignored' obj.put() - error('No targets, nothing to do ¯\_(ツ)_/¯', status=204) + error(r'No targets, nothing to do ¯\_(ツ)_/¯', status=204) sorted_targets = sorted(targets.items(), key=lambda t: t[0].uri) obj.populate( @@ -946,14 +946,6 @@ class Protocol: target_uris = set(as1.targets(obj.as1)) logger.info(f'Raw targets: {target_uris}') - - # TODO: remove this? seems unnecessary now that receive calls resolve_ids? - if target_uris: - origs = {key.id() for key in get_originals(target_uris, keys_only=True)} - if origs: - target_uris |= origs - logger.info(f'Added originals: {origs}') - orig_obj = None targets = {} # maps Target to Object or None owner = as1.get_owner(obj.as1) @@ -1234,7 +1226,7 @@ def send_task(): if (target not in obj.undelivered and target not in obj.failed and 'force' not in request.values): logger.info(f"{url} not in {obj.key.id()} undelivered or failed, giving up") - return '¯\_(ツ)_/¯', 204 + return r'¯\_(ツ)_/¯', 204 if user_key := form.get('user'): g.user = ndb.Key(urlsafe=user_key).get() diff --git a/tests/test_protocol.py b/tests/test_protocol.py index f02c3e6..e8444b2 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -380,48 +380,6 @@ class ProtocolTest(TestCase): Target(protocol='atproto', uri='https://atproto.brid.gy/'), ], Protocol.targets(obj).keys()) - @patch('requests.get', return_value=requests_response({})) - def test_targets_converts_copies_to_originals(self, mock_get): - """targets should convert User/Object.copies to their originals.""" - alice = self.make_user('fake:alice', cls=Fake, - copies=[Target(uri='did:plc:alice', protocol='atproto')]) - bob = self.make_user( - 'fake:bob', cls=OtherFake, - copies=[Target(uri='other:bob', protocol='other')]) - obj = self.store_object( - id='fake:post', our_as1={'foo': 9}, - copies=[Target(uri='at://did:plc:eve/post/789', protocol='fake')]) - - Fake.fetchable = { - 'fake:alice': {'foo': 1}, - 'fake:bob': {'foo': 2}, - } - OtherFake.fetchable = { - 'other:bob': {'foo': 3}, - } - - obj = Object(our_as1={ - 'id': 'other:reply', - 'objectType': 'note', - 'inReplyTo': [ - 'at://did:web:unknown/post/123', - 'at://did:plc:eve/post/789', - ], - 'tags': [{ - 'objectType': 'mention', - 'url': 'did:plc:alice', - }, { - 'objectType': 'mention', - 'url': 'other:bob', - }], - }) - self.assertCountEqual([ - Target(uri='fake:post:target', protocol='fake'), - Target(uri='fake:alice:target', protocol='fake'), - Target(uri='fake:bob:target', protocol='fake'), - Target(uri='other:bob:target', protocol='other'), - ], Protocol.targets(obj).keys()) - def test_translate_ids_follow(self): self.assert_equals({ 'id': 'other:o:fa:fake:follow', @@ -1518,7 +1476,7 @@ class ProtocolReceiveTest(TestCase): ) self.assertEqual(2, Follower.query().count()) - def test_replace_actor_copies_with_originals_follow(self): + def test_resolve_ids_follow(self): follow = { 'id': 'fake:follow', 'objectType': 'activity', @@ -1529,7 +1487,7 @@ class ProtocolReceiveTest(TestCase): # no matching copy users obj = Object(id='fake:follow', our_as1=follow, source_protocol='fake') - Fake.receive(obj) + self.assertEqual(('OK', 202), Fake.receive(obj)) self.assert_equals(follow, obj.our_as1) # matching copy user @@ -1542,14 +1500,14 @@ class ProtocolReceiveTest(TestCase): 'other:bob': {}, } - Fake.receive(obj) + self.assertEqual(('OK', 202), Fake.receive(obj)) self.assert_equals({ **follow, 'actor': {'id': 'fake:alice'}, 'object': 'other:bob', }, Object.get_by_id('fake:follow').our_as1) - def test_replace_actor_copies_with_originals_share(self): + def test_resolve_ids_share(self): share = { 'objectType': 'activity', 'verb': 'share', @@ -1578,6 +1536,60 @@ class ProtocolReceiveTest(TestCase): 'object': 'other:post', }, obj.our_as1) + def test_resolve_ids_reply(self): + reply = { + 'id': 'other:reply', + 'objectType': 'note', + 'inReplyTo': [ + 'other:unknown-post', + 'other:post', + ], + 'tags': [{ + 'objectType': 'mention', + 'url': 'other:alice', + }, { + 'objectType': 'mention', + 'url': 'other:bob', + }], + } + + # no matching copies + obj = Object(id='other:reply', our_as1=reply, source_protocol='other') + with self.assertRaises(NoContent): + Fake.receive(obj) + self.assert_equals(reply, obj.our_as1) + + # matching copies + self.make_user( + 'fake:alice', cls=Fake, + copies=[Target(uri='other:alice', protocol='other')]) + self.make_user( + 'fake:bob', cls=Fake, + copies=[Target(uri='other:bob', protocol='other')]) + self.store_object( + id='fake:post', our_as1={'foo': 9}, source_protocol='fake', + copies=[Target(uri='other:post', protocol='other')]) + + protocol.seen_ids.clear() + obj.new = True + self.assertEqual(('OK', 202), Fake.receive(obj)) + self.assertEqual({ + 'id': 'other:reply', + 'objectType': 'note', + 'inReplyTo': [ + 'other:unknown-post', + 'fake:post', + ], + 'tags': [{ + 'objectType': 'mention', + 'url': 'fake:alice', + }, { + 'objectType': 'mention', + 'url': 'fake:bob', + }], + 'updated': '2022-01-02T03:04:05+00:00', + }, obj.key.get().our_as1) + def test_receive_task_handler(self): note = { 'id': 'fake:post',