add fallback kwargs to User.user_link, Protocol.bridged_web_url_for

for #966
pull/1319/head
Ryan Barrett 2024-09-10 16:44:18 -07:00
rodzic b24071576d
commit 36a9f94cb8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
6 zmienionych plików z 24 dodań i 12 usunięć

Wyświetl plik

@ -281,17 +281,11 @@ class ATProto(User, Protocol):
return did.resolve_handle(handle, get_fn=util.requests_get) return did.resolve_handle(handle, get_fn=util.requests_get)
@classmethod @classmethod
def bridged_web_url_for(cls, user): def bridged_web_url_for(cls, user, fallback=False):
"""Returns a bridged user's profile URL on bsky.app. """Returns a bridged user's profile URL on bsky.app.
For example, returns ``https://bsky.app/profile/alice.com.web.brid.gy`` For example, returns ``https://bsky.app/profile/alice.com.web.brid.gy``
for Web user ``alice.com``. for Web user ``alice.com``.
Args:
user (models.User)
Returns:
str, or None if there isn't a canonical URL
""" """
if not isinstance(user, ATProto): if not isinstance(user, ATProto):
if did := user.get_copy(ATProto): if did := user.get_copy(ATProto):

Wyświetl plik

@ -710,7 +710,8 @@ Welcome to Bridgy Fed! Your account will soon be bridged to {to_proto.PHRASE} at
if copy.protocol in (proto.LABEL, proto.ABBREV): if copy.protocol in (proto.LABEL, proto.ABBREV):
return copy.uri return copy.uri
def user_link(self, name=True, handle=True, pictures=False, proto=None): def user_link(self, name=True, handle=True, pictures=False, proto=None,
proto_fallback=False):
"""Returns a pretty HTML link to the user's profile. """Returns a pretty HTML link to the user's profile.
Can optionally include display name, handle, profile Can optionally include display name, handle, profile
@ -724,12 +725,15 @@ Welcome to Bridgy Fed! Your account will soon be bridged to {to_proto.PHRASE} at
pictures (bool): include profile picture and protocol logo pictures (bool): include profile picture and protocol logo
proto (protocol.Protocol): link to this protocol instead of the user's proto (protocol.Protocol): link to this protocol instead of the user's
native protocol native protocol
proto_fallback (bool): if True, and ``proto`` is provided and has no
no canonical profile URL for bridged users, uses the user's profile
URL in their native protocol
""" """
img = name_str = handle_str = dot = logo = a_open = a_close = '' img = name_str = handle_str = dot = logo = a_open = a_close = ''
if proto: if proto:
assert self.is_enabled(proto), f"{proto.LABEL} isn't enabled" assert self.is_enabled(proto), f"{proto.LABEL} isn't enabled"
url = proto.bridged_web_url_for(self) url = proto.bridged_web_url_for(self, fallback=proto_fallback)
else: else:
proto = self.__class__ proto = self.__class__
url = self.web_url() url = self.web_url()

Wyświetl plik

@ -414,7 +414,7 @@ class Protocol:
return (None, None) return (None, None)
@classmethod @classmethod
def bridged_web_url_for(cls, user): def bridged_web_url_for(cls, user, fallback=False):
"""Returns the web URL for a user's bridged profile in this protocol. """Returns the web URL for a user's bridged profile in this protocol.
For example, for Web user ``alice.com``, :meth:`ATProto.bridged_web_url_for` For example, for Web user ``alice.com``, :meth:`ATProto.bridged_web_url_for`
@ -422,11 +422,14 @@ class Protocol:
Args: Args:
user (models.User) user (models.User)
fallback (bool): if True, and bridged users have no canonical user
profile URL in this protocol, return the native protocol's profile URL
Returns: Returns:
str, or None if there isn't a canonical URL str, or None if there isn't a canonical URL
""" """
return None if fallback:
return user.web_url()
@classmethod @classmethod
def actor_key(cls, obj): def actor_key(cls, obj):

Wyświetl plik

@ -220,6 +220,12 @@ class UserTest(TestCase):
'<a class="h-card u-author" rel="me" href="web:fake:y.za" title="Mrs. ☕ Foo &middot; fake:handle:y.za">Mrs. ☕ Foo &middot; fake:handle:y.za</a>', '<a class="h-card u-author" rel="me" href="web:fake:y.za" title="Mrs. ☕ Foo &middot; fake:handle:y.za">Mrs. ☕ Foo &middot; fake:handle:y.za</a>',
self.user.user_link(proto=Fake, handle=True)) self.user.user_link(proto=Fake, handle=True))
def test_user_link_proto_fallback(self):
self.user.obj = Object(id='y.za', as2=ACTOR)
self.assert_multiline_equals(
'<a class="h-card u-author" rel="me" href="https://y.za/" title="Mrs. ☕ Foo &middot; @y.za@web.brid.gy">Mrs. ☕ Foo &middot; @y.za@web.brid.gy</a>',
self.user.user_link(proto=ActivityPub, proto_fallback=True, handle=True))
def test_user_link_proto_not_enabled(self): def test_user_link_proto_not_enabled(self):
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
self.user.user_link(proto=ExplicitEnableFake) self.user.user_link(proto=ExplicitEnableFake)

Wyświetl plik

@ -409,6 +409,11 @@ class ProtocolTest(TestCase):
self.user.obj.put() self.user.obj.put()
self.assertIsNone(Protocol.key_for(self.user.key.id())) self.assertIsNone(Protocol.key_for(self.user.key.id()))
def test_bridged_web_url_for(self):
self.assertIsNone(Protocol.bridged_web_url_for(self.user))
self.assertEqual('https://foo.com/',
Protocol.bridged_web_url_for(self.user, fallback=True))
def test_targets_checks_blocklisted_per_protocol(self): def test_targets_checks_blocklisted_per_protocol(self):
"""_targets should call the target protocol's is_blocklisted().""" """_targets should call the target protocol's is_blocklisted()."""
# non-ATProto account, ATProto target (PDS) is bsky.brid.gy # non-ATProto account, ATProto target (PDS) is bsky.brid.gy

Wyświetl plik

@ -98,7 +98,7 @@ class Fake(User, protocol.Protocol):
return f'web:{self.key.id()}' return f'web:{self.key.id()}'
@classmethod @classmethod
def bridged_web_url_for(cls, user): def bridged_web_url_for(cls, user, fallback=False):
return f'web:{cls.LABEL}:{user.key.id()}' return f'web:{cls.LABEL}:{user.key.id()}'
@classmethod @classmethod