genericize User.get_by_atproto_did to get_for_copy

pull/642/head
Ryan Barrett 2023-09-19 16:48:16 -07:00
rodzic bb18b5ee0d
commit 134416cd7b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 26 dodań i 41 usunięć

Wyświetl plik

@ -182,31 +182,27 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
return user
@staticmethod
def get_by_atproto_did(did):
"""Fetches the user across all protocols with the given ATProto DID.
def get_for_copy(copy_id):
"""Fetches any user (across all protocols) with the given id in `copies`.
Prefers bridged (ie not :class:`ATProto`) users to :class:`ATProto`
users.
If more than one :class:`ATProto` user exists, this returns an
arbitrary one!
If more than one matching user exists, this returns an arbitrary one!
Args:
did: str
copy_id: str
Returns:
:class:`User` subclass instance, or None if not found
"""
assert did
assert copy_id
for cls in set(PROTOCOLS.values()):
if not cls or cls.ABBREV == 'atproto':
continue
user = cls.query(cls.atproto_did == did).get()
if user:
return user
if cls:
user = cls.query(cls.copies.uri == copy_id).get()
if user:
return user
return PROTOCOLS['atproto'].get_by_id(did)
# TODO: default to lookup by id, across protocols? is that useful
# anywhere?
@classmethod
@ndb.transactional()
@ -507,7 +503,7 @@ class Object(StringIdModel):
obj.setdefault(field, repo)
# load matching user. prefer bridged non-ATProto user
# to ATProto user
user = User.get_by_atproto_did(repo)
user = User.get_for_copy(repo)
if user:
logger.debug(f'Filling in {field} from {user}')
if user.obj and user.obj.as1:

Wyświetl plik

@ -53,15 +53,12 @@ class UserTest(TestCase):
user.atproto_did = 'did:plc:123'
user.atproto_did = None
def test_get_by_atproto_did(self):
self.assertIsNone(User.get_by_atproto_did('did:plc:foo'))
def test_get_for_copy(self):
self.assertIsNone(User.get_for_copy('did:plc:foo'))
atp_user = self.make_user('did:plc:foo', cls=ATProto)
self.assertEqual(atp_user, User.get_by_atproto_did('did:plc:foo'))
# prefer non-ATProto user, if available
fake_user = self.make_user('fake:user', cls=Fake, atproto_did='did:plc:foo')
self.assertEqual(fake_user, User.get_by_atproto_did('did:plc:foo'))
target = Target(uri='did:plc:foo', protocol='atproto')
fake_user = self.make_user('fake:user', cls=Fake, copies=[target])
self.assertEqual(fake_user, User.get_for_copy('did:plc:foo'))
def test_get_or_create_use_instead(self):
user = Fake.get_or_create('a.b')
@ -439,31 +436,23 @@ class ObjectTest(TestCase):
obj = Object(id='at://did:plc:foo/co.ll/123', bsky=like_bsky)
self.assert_equals(like_as1, obj.as1)
# ATProto user without Object
user = ATProto(id='did:plc:foo')
user.put()
self.assertEqual(like_as1, obj.as1)
# matching user without Object
user = self.make_user(id='fake:user', cls=Fake,
copies=[Target(uri='did:plc:foo', protocol='atproto')])
self.assertEqual({
**like_as1,
'actor': 'fake:user',
}, obj.as1)
# ATProto user with Object
# matching user with Object
user.obj = self.store_object(id='at://did:plc:foo/profile/self',
our_as1={'foo': 'bar'})
user.put()
self.assertEqual({
**like_as1,
'actor': {
'id': 'did:plc:foo',
'foo': 'bar',
},
}, obj.as1)
# Fake user, should prefer to ATProto user
user = self.make_user(id='fake:user', cls=Fake, atproto_did='did:plc:foo',
obj_as1={'baz': 'biff'})
self.assertEqual({
**like_as1,
'actor': {
'id': 'fake:user',
'baz': 'biff',
'foo': 'bar',
},
}, obj.as1)