replace User.atproto_handle with handle_as('atproto')

pull/649/head
Ryan Barrett 2023-09-25 10:57:16 -07:00
rodzic 40ba007e9e
commit 6cdb04b53f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
9 zmienionych plików z 55 dodań i 37 usunięć

Wyświetl plik

@ -108,11 +108,6 @@ class ActivityPub(User, Protocol):
"""
return self.key.id()
def atproto_handle(self):
"""Returns `[USERNAME].[INSTANCE].AP.brid.gy`."""
username, instance = self.ap_address().strip('@').split('@')
return f'{username}.{instance}.{self.ABBREV}{common.SUPERDOMAIN}'
@classmethod
def owns_id(cls, id):
"""Returns None if id is an http(s) URL, False otherwise.

Wyświetl plik

@ -48,7 +48,7 @@ class ATProto(User, Protocol):
@ndb.ComputedProperty
def readable_id(self):
"""Prefers handle, then DID."""
return self.atproto_handle() or self.key.id()
return self.handle() or self.key.id()
def _pre_put_hook(self):
"""Validate id, require did:plc or non-blocklisted did:web.
@ -71,7 +71,7 @@ class ATProto(User, Protocol):
assert not self.atproto_did, \
f"{self.key} shouldn't have atproto_did {self.atproto_did}"
def atproto_handle(self):
def handle(self):
"""Returns handle if the DID document includes one, otherwise None."""
did_obj = ATProto.load(self.key.id(), remote=False)
if did_obj:
@ -80,8 +80,6 @@ class ATProto(User, Protocol):
if handle:
return handle
handle = atproto_handle
def web_url(self):
return bluesky.Bluesky.user_url(self.readable_id)
@ -235,7 +233,7 @@ class ATProto(User, Protocol):
else:
# create new DID, repo
logger.info(f'Creating new did:plc for {user.key}')
did_plc = did.create_plc(user.atproto_handle(),
did_plc = did.create_plc(user.handle_as('atproto'),
pds_url=common.host_url(),
post_fn=util.requests_post)
@ -249,7 +247,7 @@ class ATProto(User, Protocol):
assert not storage.load_repo(user.atproto_did)
nonlocal repo
repo = Repo.create(storage, user.atproto_did,
handle=user.atproto_handle(),
handle=user.handle_as('atproto'),
callback=create_atproto_commit_task,
signing_key=did_plc.signing_key,
rotation_key=did_plc.rotation_key)

Wyświetl plik

@ -289,7 +289,7 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
def readable_id(self):
"""This user's human-readable unique id, eg ``@me@snarfed.org``.
TODO: rename to handle? Need to backfill then.
TODO: rename to handle! And keep readable_id in queries for backcompat
To be implemented by subclasses.
"""
@ -302,6 +302,36 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
"""
raise NotImplementedError()
def handle_as(self, to_proto):
"""Returns this user's handle in a different protocol.
Args:
to_proto (str or Protocol)
Returns:
str
"""
if isinstance(to_proto, str):
to_proto = PROTOCOLS[to_proto]
return ids.convert_handle(handle=self.handle(), from_proto=self.__class__,
to_proto=to_proto)
def id_as(self, to_proto):
"""Returns this user's id in a different protocol.
Args:
to_proto (str or Protocol)
Returns:
str
"""
if isinstance(to_proto, str):
to_proto = PROTOCOLS[to_proto]
return ids.convert_id(id=self.key.id(), from_proto=self.__class__,
to_proto=to_proto)
def readable_or_key_id(self):
"""Returns readable_id if set, otherwise key id."""
return self.readable_id or self.key.id()
@ -398,16 +428,6 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
return url
def atproto_handle(self):
"""Returns this user's AT Protocol handle, eg 'foo.com'.
To be implemented by subclasses.
Returns:
str
"""
raise NotImplementedError()
def profile_id(self):
"""Returns the id of this user's profile object in its native protocol.
@ -530,7 +550,7 @@ class Object(StringIdModel):
elif self.bsky:
owner, _, _ = arroba.util.parse_at_uri(self.key.id())
ATProto = PROTOCOLS['atproto']
handle = ATProto(id=owner).atproto_handle()
handle = ATProto(id=owner).handle()
obj = bluesky.to_as1(self.bsky, repo_did=owner, repo_handle=handle,
pds=ATProto.target_for(self))

Wyświetl plik

@ -1940,12 +1940,12 @@ class ActivityPubUtilsTest(TestCase):
user = self.make_user('http://foo/actor', cls=ActivityPub)
self.assertEqual('http://foo/actor', user.ap_actor())
def test_atproto_handle(self):
def test_handle_as(self):
user = self.make_user('http://a', cls=ActivityPub, obj_as2={
'id': 'https://mas.to/users/foo',
'preferredUsername': 'me',
})
self.assertEqual('me.mas.to.ap.brid.gy', user.atproto_handle())
self.assertEqual('me.mas.to.ap.brid.gy', user.handle_as('atproto'))
def test_web_url(self):
user = self.make_user('http://foo/actor', cls=ActivityPub)

Wyświetl plik

@ -228,11 +228,11 @@ class ATProtoTest(TestCase):
@patch('requests.get', return_value=requests_response('', status=404))
def test_handle_and_readable_id(self, mock_get):
user = self.make_user('did:plc:foo', cls=ATProto)
self.assertIsNone(user.atproto_handle())
self.assertIsNone(user.handle())
self.assertEqual('did:plc:foo', user.readable_id)
self.store_object(id='did:plc:foo', raw=DID_DOC)
self.assertEqual('han.dull', user.atproto_handle())
self.assertEqual('han.dull', user.handle())
self.assertEqual('han.dull', user.readable_id)
def test_ap_address(self):
@ -291,7 +291,7 @@ class ATProtoTest(TestCase):
},
'rotationKeys': [encode_did_key(repo.rotation_key.public_key())],
'alsoKnownAs': [
'at://user.fake.brid.gy',
'at://fake:handle:user.fa.brid.gy',
],
'services': {
'atproto_pds': {

Wyświetl plik

@ -128,6 +128,18 @@ class UserTest(TestCase):
obj.put()
self.assertEqual({'foo': 'bar'}, g.user.as2())
def test_id_as(self):
user = self.make_user('fake:user', cls=Fake)
self.assertEqual('fake:user', user.id_as(Fake))
self.assertEqual('fake:user', user.id_as('fake'))
self.assertEqual('http://localhost/fa/ap/fake:user', user.id_as('ap'))
def test_handle_as(self):
user = self.make_user('fake:user', cls=Fake)
self.assertEqual('fake:handle:user', user.handle_as(Fake))
self.assertEqual('fake:handle:user', user.handle_as('fake'))
self.assertEqual('@fake:handle:user@fa.brid.gy', user.handle_as('ap'))
def test_ap_actor(self):
user = self.make_user('did:plc:abc', cls=ATProto)
self.assertEqual('http://localhost/ap/atproto/did:plc:abc',

Wyświetl plik

@ -1795,8 +1795,8 @@ http://this/404s
self.assertEqual('http://localhost/user.com', g.user.ap_actor())
self.assertEqual('http://localhost/user.com/inbox', g.user.ap_actor('inbox'))
def test_atproto_handle(self, *_):
self.assertEqual('user.com.web.brid.gy', g.user.atproto_handle())
def test_handle_as(self, *_):
self.assertEqual('user.com.web.brid.gy', g.user.handle_as('atproto'))
def test_check_web_site(self, mock_get, _):
redir = 'http://localhost/.well-known/webfinger?resource=acct:user.com@user.com'

Wyświetl plik

@ -82,9 +82,6 @@ class Fake(User, protocol.Protocol):
def ap_actor(self, rest=None):
return f'http://bf/fake/{self.key.id()}/ap' + (f'/{rest}' if rest else '')
def atproto_handle(self):
return self.key.id().removeprefix('fake:') + '.fake.brid.gy'
@classmethod
def owns_id(cls, id):
if id.startswith('nope') or id == 'fake:nope':

4
web.py
Wyświetl plik

@ -133,10 +133,6 @@ class Web(User, Protocol):
url += f'/{rest}'
return url
def atproto_handle(self):
"""Returns ``[DOMAIN].web.brid.gy``."""
return f'{self.key.id()}.{self.ABBREV}{common.SUPERDOMAIN}'
def user_page_path(self, rest=None):
"""Always use domain."""
path = f'/{self.ABBREV}/{self.key.id()}'