Skip to content

Commit

Permalink
add User.get_by_atproto_did
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Sep 18, 2023
1 parent b49f49b commit 48e47f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 22 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ def get_by_id(cls, id):

return user

@staticmethod
def get_by_atproto_did(did):
"""Fetches the user across all protocols with the given atproto_did.
If more than one user has the given atproto_did, this returns an
arbitrary one!
Args:
did: str
Returns:
:class:`User` subclass instance, or None if not found
"""
assert did

for cls in set(PROTOCOLS.values()):
if not cls:
continue
user = cls.query(cls.atproto_did == did).get()
if user:
return user

@classmethod
@ndb.transactional()
def get_or_create(cls, id, **kwargs):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def test_validate_atproto_did(self):
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'))
user = self.make_user('fake:user', cls=Fake, atproto_did='did:plc:foo')
self.assertEqual(user, User.get_by_atproto_did('did:plc:foo'))

def test_get_or_create_use_instead(self):
user = Fake.get_or_create('a.b')
user.use_instead = g.user.key
Expand Down Expand Up @@ -155,7 +160,6 @@ def test_target_hashable(self):

# just check that these don't crash
assert isinstance(id(target), int)
{target: 'foo'}

def test_ndb_in_memory_cache_off(self):
"""It has a weird bug that we want to avoid.
Expand Down

0 comments on commit 48e47f5

Please sign in to comment.