From 9e1acf1cf7ad0edb8578ed05bf7d0357b967e919 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 18 Oct 2023 11:18:20 -0700 Subject: [PATCH] replace copy ids with originals in Protocol.load honestly not sure if this is necessary yet, and it's moderately expensive, two serial datastore queries with an IN filter. we'll see. --- models.py | 3 +++ protocol.py | 2 ++ tests/test_protocol.py | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/models.py b/models.py index a59a50e..557c26f 100644 --- a/models.py +++ b/models.py @@ -951,6 +951,9 @@ class Object(StringIdModel): + [as1.get_object(inner_obj, f).get('id') for f in fields] + [inner_obj.get('id')] + [m.get('url') for m in mention_tags]) + if not ids: + return + origs = (User.get_for_copies(ids) + Object.query(Object.copies.uri.IN(ids)).fetch()) diff --git a/protocol.py b/protocol.py index f9fc2d3..fc7acad 100644 --- a/protocol.py +++ b/protocol.py @@ -1120,6 +1120,8 @@ class Protocol: if not fetched: return None + obj.replace_copies_with_originals() + if obj.new is False: obj.changed = obj.activity_changed(orig_as1) diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 4f9af31..3956b84 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -287,6 +287,32 @@ class ProtocolTest(TestCase): with self.assertRaises(AssertionError): Fake.load('nope', local=False, remote=False) + def test_load_replace_copies_with_originals(self): + follow = { + 'objectType': 'activity', + 'verb': 'follow', + 'actor': 'fake:alice', + 'object': 'fake:bob', + } + Fake.fetchable = { + 'fake:follow': follow, + } + + # no matching copy users + obj = Fake.load('fake:follow', remote=True) + self.assert_equals(follow, obj.our_as1) + + # matching copy user + self.make_user('other:bob', cls=OtherFake, + copies=[Target(uri='fake:bob', protocol='fake')]) + + obj = Fake.load('fake:follow', remote=True) + self.assert_equals({ + **follow, + 'id': 'fake:follow', + 'object': {'id': 'other:bob'}, + }, obj.our_as1) + def test_actor_key(self): user = self.make_user(id='fake:a', cls=Fake) a_key = user.key