kopia lustrzana https://github.com/snarfed/bridgy-fed
ids: allow same from and to protocol in convert_id, convert_handle
rodzic
4b5bff552e
commit
39752dc7ca
29
ids.py
29
ids.py
|
@ -4,14 +4,7 @@ https://fed.brid.gy/docs#translate
|
|||
"""
|
||||
import re
|
||||
|
||||
from granary.bluesky import Bluesky
|
||||
|
||||
from activitypub import ActivityPub
|
||||
from atproto import ATProto
|
||||
from common import DOMAIN_RE, host_url, SUPERDOMAIN
|
||||
from models import User
|
||||
from protocol import Protocol, PROTOCOLS
|
||||
from web import Web
|
||||
from common import host_url, SUPERDOMAIN
|
||||
|
||||
|
||||
def convert_id(*, id, from_proto, to_proto):
|
||||
|
@ -19,16 +12,18 @@ def convert_id(*, id, from_proto, to_proto):
|
|||
|
||||
Args:
|
||||
id (str)
|
||||
from_proto (:class:`Protocol`)
|
||||
to_proto (:class:`Protocol`)
|
||||
from_proto (protocols.Protocol)
|
||||
to_proto (protocols.Protocol)
|
||||
|
||||
Returns:
|
||||
str: the corresponding id in ``to_proto``
|
||||
"""
|
||||
assert id and from_proto and to_proto
|
||||
assert from_proto != to_proto
|
||||
assert from_proto.owns_id(id) is not False
|
||||
|
||||
if from_proto == to_proto:
|
||||
return id
|
||||
|
||||
match (from_proto.LABEL, to_proto.LABEL):
|
||||
case (_, 'atproto'):
|
||||
user = from_proto.get_by_id(id)
|
||||
|
@ -37,7 +32,7 @@ def convert_id(*, id, from_proto, to_proto):
|
|||
user = from_proto.get_for_copy(id)
|
||||
return user.key.id() if user else None
|
||||
case (_, 'activitypub'):
|
||||
return host_url(f'{from_proto.ABBREV}/{ActivityPub.ABBREV}/{id}')
|
||||
return host_url(f'{from_proto.ABBREV}/ap/{id}')
|
||||
case ('activitypub', 'web'):
|
||||
return id
|
||||
# fake protocol is only for unit tests
|
||||
|
@ -52,20 +47,20 @@ def convert_id(*, id, from_proto, to_proto):
|
|||
def convert_handle(*, handle, from_proto, to_proto):
|
||||
"""Converts a handle from one protocol to another.
|
||||
|
||||
TODO: May make network requests to resolve handles!
|
||||
|
||||
Args:
|
||||
handle (str)
|
||||
from_proto (:class:`Protocol`)
|
||||
to_proto (:class:`Protocol`)
|
||||
from_proto (protocols.Protocol)
|
||||
to_proto (protocols.Protocol)
|
||||
|
||||
Returns:
|
||||
str: the corresponding handle in ``to_proto``
|
||||
"""
|
||||
assert handle and from_proto and to_proto
|
||||
assert from_proto != to_proto
|
||||
assert from_proto.owns_handle(handle) is not False
|
||||
|
||||
if from_proto == to_proto:
|
||||
return handle
|
||||
|
||||
match (from_proto.LABEL, to_proto.LABEL):
|
||||
case (_, 'activitypub'):
|
||||
if True: # basic
|
||||
|
|
|
@ -11,25 +11,28 @@ class IdsTest(TestCase):
|
|||
def test_convert_id(self):
|
||||
Web(id='user.com', atproto_did='did:plc:123',
|
||||
copies=[Target(uri='did:plc:123', protocol='atproto')]).put()
|
||||
ActivityPub(id='https://instance/user', atproto_did='did:plc:456',
|
||||
ActivityPub(id='https://inst/user', atproto_did='did:plc:456',
|
||||
copies=[Target(uri='did:plc:456', protocol='atproto')]).put()
|
||||
Fake(id='fake:user', atproto_did='did:plc:789',
|
||||
copies=[Target(uri='did:plc:789', protocol='atproto')]).put()
|
||||
|
||||
for from_, id, to, expected in [
|
||||
(ActivityPub, 'https://inst/user', ActivityPub, 'https://inst/user'),
|
||||
(ActivityPub, 'https://inst/user', ATProto, 'did:plc:456'),
|
||||
(ActivityPub, 'https://inst/user', Fake, 'fake:https://inst/user'),
|
||||
(ActivityPub, 'https://inst/user', Web, 'https://inst/user'),
|
||||
(ATProto, 'did:plc:123', Web, 'user.com'),
|
||||
(ATProto, 'did:plc:456', ActivityPub, 'https://inst/user'),
|
||||
(ATProto, 'did:plc:456', ATProto, 'did:plc:456'),
|
||||
(ATProto, 'did:plc:789', Fake, 'fake:user'),
|
||||
(Fake, 'fake:user', ActivityPub, 'http://localhost/fa/ap/fake:user'),
|
||||
(Fake, 'fake:user', ATProto, 'did:plc:789'),
|
||||
(Fake, 'fake:user', Fake, 'fake:user'),
|
||||
(Fake, 'fake:user', Web, 'fake:user'),
|
||||
(Web, 'user.com', ActivityPub, 'http://localhost/web/ap/user.com'),
|
||||
(Web, 'user.com', ATProto, 'did:plc:123'),
|
||||
(Web, 'user.com', Fake, 'fake:user.com'),
|
||||
# TODO: not a domain, is that ok?
|
||||
(ActivityPub, 'https://instance/user', Web, 'https://instance/user'),
|
||||
(ActivityPub, 'https://instance/user', ATProto, 'did:plc:456'),
|
||||
(ActivityPub, 'https://instance/user', Fake, 'fake:https://instance/user'),
|
||||
(ATProto, 'did:plc:123', Web, 'user.com'),
|
||||
(ATProto, 'did:plc:456', ActivityPub, 'https://instance/user'),
|
||||
(ATProto, 'did:plc:789', Fake, 'fake:user'),
|
||||
(Fake, 'fake:user', Web, 'fake:user'),
|
||||
(Fake, 'fake:user', ActivityPub, 'http://localhost/fa/ap/fake:user'),
|
||||
(Fake, 'fake:user', ATProto, 'did:plc:789'),
|
||||
(Web, 'user.com', Web, 'user.com'),
|
||||
]:
|
||||
with self.subTest(from_=from_.LABEL, to=to.LABEL):
|
||||
self.assertEqual(expected, convert_id(
|
||||
|
@ -53,28 +56,32 @@ class IdsTest(TestCase):
|
|||
(Web, 'user.com', ActivityPub, '@user.com@web.brid.gy'),
|
||||
(Web, 'user.com', ATProto, 'user.com.web.brid.gy'),
|
||||
(Web, 'user.com', Fake, 'fake:handle:user.com'),
|
||||
(Web, 'user.com', Web, 'user.com'),
|
||||
# # TODO: enhanced
|
||||
# (Web, 'user.com', ActivityPub, '@user.com@user.com'),
|
||||
# (Web, 'user.com', Fake, 'fake:handle:user.com'),
|
||||
|
||||
# TODO: webfinger lookup
|
||||
(ActivityPub, '@user@instance', Web, 'instance/@user'),
|
||||
(ActivityPub, '@user@instance', ActivityPub, '@user@instance'),
|
||||
(ActivityPub, '@user@instance', ATProto, 'user.instance.ap.brid.gy'),
|
||||
(ActivityPub, '@user@instance', Fake, 'fake:handle:@user@instance'),
|
||||
(ActivityPub, '@user@instance', Web, 'instance/@user'),
|
||||
# # # TODO: enhanced
|
||||
# (ActivityPub, '@user@instance', Web, 'https://instance/user'),
|
||||
# (ActivityPub, '@user@instance', Fake,
|
||||
# 'fake:handle:https://instance/user'),
|
||||
|
||||
(ATProto, 'user.com', Web, 'user.com'),
|
||||
(ATProto, 'user.com', ActivityPub, '@user.com@atproto.brid.gy'),
|
||||
(ATProto, 'user.com', ATProto, 'user.com'),
|
||||
(ATProto, 'user.com', Fake, 'fake:handle:user.com'),
|
||||
(ATProto, 'user.com', Web, 'user.com'),
|
||||
# # # TODO: enhanced
|
||||
# (ATProto, '@user@instance', ActivityPub, 'user.com'),
|
||||
|
||||
(Fake, 'fake:handle:user', Web, 'fake:handle:user'),
|
||||
(Fake, 'fake:handle:user', ActivityPub, '@fake:handle:user@fa.brid.gy'),
|
||||
(Fake, 'fake:handle:user', ATProto, 'fake:handle:user.fa.brid.gy'),
|
||||
(Fake, 'fake:handle:user', Fake, 'fake:handle:user'),
|
||||
(Fake, 'fake:handle:user', Web, 'fake:handle:user'),
|
||||
]:
|
||||
with self.subTest(from_=from_.LABEL, to=to.LABEL):
|
||||
self.assertEqual(expected, convert_handle(
|
||||
|
|
Ładowanie…
Reference in New Issue