kopia lustrzana https://github.com/snarfed/bridgy-fed
add User.handle()
rodzic
d008f8f8ee
commit
40ba007e9e
|
@ -91,7 +91,7 @@ class ActivityPub(User, Protocol):
|
||||||
return self.ap_actor()
|
return self.ap_actor()
|
||||||
|
|
||||||
def ap_address(self):
|
def ap_address(self):
|
||||||
"""Returns this user's ActivityPub address, eg '@user@foo.com'."""
|
"""Returns this user's ActivityPub address, eg ``@user@foo.com``."""
|
||||||
if self.obj and self.obj.as1:
|
if self.obj and self.obj.as1:
|
||||||
addr = as2.address(self.as2())
|
addr = as2.address(self.as2())
|
||||||
if addr:
|
if addr:
|
||||||
|
@ -99,6 +99,8 @@ class ActivityPub(User, Protocol):
|
||||||
|
|
||||||
return as2.address(self.key.id())
|
return as2.address(self.key.id())
|
||||||
|
|
||||||
|
handle = ap_address
|
||||||
|
|
||||||
def ap_actor(self, rest=None):
|
def ap_actor(self, rest=None):
|
||||||
"""Returns this user's ActivityPub actor id URL.
|
"""Returns this user's ActivityPub actor id URL.
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,8 @@ class ATProto(User, Protocol):
|
||||||
if handle:
|
if handle:
|
||||||
return handle
|
return handle
|
||||||
|
|
||||||
|
handle = atproto_handle
|
||||||
|
|
||||||
def web_url(self):
|
def web_url(self):
|
||||||
return bluesky.Bluesky.user_url(self.readable_id)
|
return bluesky.Bluesky.user_url(self.readable_id)
|
||||||
|
|
||||||
|
|
10
ids.py
10
ids.py
|
@ -8,12 +8,12 @@ from common import host_url, SUPERDOMAIN
|
||||||
|
|
||||||
|
|
||||||
def convert_id(*, id, from_proto, to_proto):
|
def convert_id(*, id, from_proto, to_proto):
|
||||||
"""Converts an id (not necessarily handle) from one protocol to another.
|
"""Converts an id from one protocol to another.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
id (str)
|
id (str)
|
||||||
from_proto (protocols.Protocol)
|
from_proto (Protocol)
|
||||||
to_proto (protocols.Protocol)
|
to_proto (Protocol)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: the corresponding id in ``to_proto``
|
str: the corresponding id in ``to_proto``
|
||||||
|
@ -49,8 +49,8 @@ def convert_handle(*, handle, from_proto, to_proto):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
handle (str)
|
handle (str)
|
||||||
from_proto (protocols.Protocol)
|
from_proto (Protocol)
|
||||||
to_proto (protocols.Protocol)
|
to_proto (Protocol)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: the corresponding handle in ``to_proto``
|
str: the corresponding handle in ``to_proto``
|
||||||
|
|
12
models.py
12
models.py
|
@ -25,6 +25,7 @@ from oauth_dropins.webutil.util import json_dumps, json_loads
|
||||||
|
|
||||||
import common
|
import common
|
||||||
from common import add, base64_to_long, long_to_base64, redirect_unwrap
|
from common import add, base64_to_long, long_to_base64, redirect_unwrap
|
||||||
|
import ids
|
||||||
|
|
||||||
# maps string label to Protocol subclass. populated by ProtocolUserMeta.
|
# maps string label to Protocol subclass. populated by ProtocolUserMeta.
|
||||||
# seed with old and upcoming protocols that don't have their own classes (yet).
|
# seed with old and upcoming protocols that don't have their own classes (yet).
|
||||||
|
@ -286,12 +287,21 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
|
||||||
|
|
||||||
@ndb.ComputedProperty
|
@ndb.ComputedProperty
|
||||||
def readable_id(self):
|
def readable_id(self):
|
||||||
"""This user's human-readable unique id, eg '@me@snarfed.org'.
|
"""This user's human-readable unique id, eg ``@me@snarfed.org``.
|
||||||
|
|
||||||
|
TODO: rename to handle? Need to backfill then.
|
||||||
|
|
||||||
To be implemented by subclasses.
|
To be implemented by subclasses.
|
||||||
"""
|
"""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
"""Returns this user's handle, eg ``@me@snarfed.org``.
|
||||||
|
|
||||||
|
To be implemented by subclasses.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def readable_or_key_id(self):
|
def readable_or_key_id(self):
|
||||||
"""Returns readable_id if set, otherwise key id."""
|
"""Returns readable_id if set, otherwise key id."""
|
||||||
return self.readable_id or self.key.id()
|
return self.readable_id or self.key.id()
|
||||||
|
|
|
@ -1869,7 +1869,7 @@ class WebUtilTest(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
g.user = self.make_user('user.com')
|
g.user = self.make_user('user.com', cls=Web)
|
||||||
|
|
||||||
def test_key_for(self, *_):
|
def test_key_for(self, *_):
|
||||||
for id in 'user.com', 'http://user.com', 'https://user.com/':
|
for id in 'user.com', 'http://user.com', 'https://user.com/':
|
||||||
|
@ -1882,6 +1882,9 @@ class WebUtilTest(TestCase):
|
||||||
with self.subTest(bad=bad):
|
with self.subTest(bad=bad):
|
||||||
self.assertIsNone(Web.key_for(bad))
|
self.assertIsNone(Web.key_for(bad))
|
||||||
|
|
||||||
|
def test_handle(self, *_):
|
||||||
|
self.assertEqual('user.com', g.user.handle())
|
||||||
|
|
||||||
def test_owns_id(self, *_):
|
def test_owns_id(self, *_):
|
||||||
self.assertIsNone(Web.owns_id('http://foo.com'))
|
self.assertIsNone(Web.owns_id('http://foo.com'))
|
||||||
self.assertIsNone(Web.owns_id('https://bar.com/'))
|
self.assertIsNone(Web.owns_id('https://bar.com/'))
|
||||||
|
|
|
@ -70,6 +70,9 @@ class Fake(User, protocol.Protocol):
|
||||||
# in-order list of ids
|
# in-order list of ids
|
||||||
fetched = []
|
fetched = []
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
return self.key.id().replace('fake:', 'fake:handle:')
|
||||||
|
|
||||||
def web_url(self):
|
def web_url(self):
|
||||||
return self.key.id()
|
return self.key.id()
|
||||||
|
|
||||||
|
|
14
web.py
14
web.py
|
@ -100,8 +100,12 @@ class Web(User, Protocol):
|
||||||
"""
|
"""
|
||||||
return super().get_or_create(id.lower().strip('.'), **kwargs)
|
return super().get_or_create(id.lower().strip('.'), **kwargs)
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
"""Returns this user's domain, eg ``user.com``."""
|
||||||
|
return self.key.id()
|
||||||
|
|
||||||
def web_url(self):
|
def web_url(self):
|
||||||
"""Returns this user's web URL aka web_url, eg 'https://foo.com/'."""
|
"""Returns this user's web URL aka web_url, eg ``https://foo.com/``."""
|
||||||
return f'https://{self.key.id()}/'
|
return f'https://{self.key.id()}/'
|
||||||
|
|
||||||
profile_id = web_url
|
profile_id = web_url
|
||||||
|
@ -119,10 +123,10 @@ class Web(User, Protocol):
|
||||||
def ap_actor(self, rest=None):
|
def ap_actor(self, rest=None):
|
||||||
"""Returns this user's ActivityPub/AS2 actor id.
|
"""Returns this user's ActivityPub/AS2 actor id.
|
||||||
|
|
||||||
Eg 'https://fed.brid.gy/foo.com'
|
Eg ``https://fed.brid.gy/foo.com``
|
||||||
|
|
||||||
Web users are special cased to not have an /ap/web/ prefix, for backward
|
Web users are special cased to not have an ``/ap/web/`` prefix, for
|
||||||
compatibility.
|
backward compatibility.
|
||||||
"""
|
"""
|
||||||
url = common.host_url(self.key.id())
|
url = common.host_url(self.key.id())
|
||||||
if rest:
|
if rest:
|
||||||
|
@ -130,7 +134,7 @@ class Web(User, Protocol):
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def atproto_handle(self):
|
def atproto_handle(self):
|
||||||
"""Returns `[DOMAIN].web.brid.gy`."""
|
"""Returns ``[DOMAIN].web.brid.gy``."""
|
||||||
return f'{self.key.id()}.{self.ABBREV}{common.SUPERDOMAIN}'
|
return f'{self.key.id()}.{self.ABBREV}{common.SUPERDOMAIN}'
|
||||||
|
|
||||||
def user_page_path(self, rest=None):
|
def user_page_path(self, rest=None):
|
||||||
|
|
Ładowanie…
Reference in New Issue