User/Object.get_copy: return key id if proto is the source protocol

pull/708/head
Ryan Barrett 2023-11-02 12:45:25 -07:00
rodzic 0ca49e837a
commit 72e180f854
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -146,6 +146,7 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
# Proxy copies of this user elsewhere, eg DIDs for ATProto records, bech32
# npub Nostr ids, etc. Similar to rel-me links in microformats2, alsoKnownAs
# in DID docs (and now AS2), etc.
# TODO: switch to using Object.copies on the user profile object?
copies = ndb.StructuredProperty(Target, repeated=True)
# whether this user signed up or otherwise explicitly, deliberately
@ -470,7 +471,8 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
def get_copy(self, proto):
"""Returns the id for the copy of this user in a given protocol.
...or None if no such copy exists.
...or None if no such copy exists. If ``proto`` is this user, returns
this user's key id.
Args:
proto: :class:`Protocol` subclass
@ -478,6 +480,9 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
Returns:
str:
"""
if isinstance(self, proto):
return self.key.id()
for copy in self.copies:
if copy.protocol in (proto.LABEL, proto.ABBREV):
return copy.uri
@ -898,7 +903,8 @@ class Object(StringIdModel):
def get_copy(self, proto):
"""Returns the id for the copy of this object in a given protocol.
...or None if no such copy exists.
...or None if no such copy exists. If ``proto`` is ``source_protocol``,
returns this object's key id.
Args:
proto: :class:`Protocol` subclass
@ -906,6 +912,9 @@ class Object(StringIdModel):
Returns:
str:
"""
if self.source_protocol in (proto.LABEL, proto.ABBREV):
return self.key.id()
for copy in self.copies:
if copy.protocol in (proto.LABEL, proto.ABBREV):
return copy.uri
@ -913,6 +922,9 @@ class Object(StringIdModel):
def resolve_ids(self):
"""Resolves "copy" ids, subdomain ids, etc with their originals.
The end result is that all ids are original "source" ids, ie in the
protocol that they first came from.
Specifically, resolves:
* ids in :class:`User.copies` and :class:`Object.copies`, eg ATProto
@ -934,8 +946,8 @@ class Object(StringIdModel):
* ``object.inReplyTo``
* ``tags.[objectType=mention].url``
This is the inverse of :meth:`protocol.Protocol.translate_ids`. Much of the
same logic is duplicated there!
:meth:`protocol.Protocol.translate_ids` is partly the inverse of this.
Much of the same logic is duplicated there!
"""
if not self.as1:
return
@ -963,7 +975,6 @@ class Object(StringIdModel):
if not ids:
return
origs = get_originals(ids)
replaced = False

Wyświetl plik

@ -217,6 +217,7 @@ class UserTest(TestCase):
def test_get_copy(self):
user = Fake(id='x')
self.assertEqual('x', user.get_copy(Fake))
self.assertIsNone(user.get_copy(OtherFake))
user.copies.append(Target(uri='fake:foo', protocol='fake'))
@ -815,6 +816,9 @@ class ObjectTest(TestCase):
obj = Object(id='x')
self.assertIsNone(obj.get_copy(Fake))
obj.source_protocol = 'other'
self.assertEqual('x', obj.get_copy(OtherFake))
obj.copies = [Target(uri='other:foo', protocol='other')]
self.assertIsNone(obj.get_copy(Fake))