2023-09-21 20:37:17 +00:00
|
|
|
"""Convert ids and handles between protocols.
|
|
|
|
|
|
|
|
https://fed.brid.gy/docs#translate
|
|
|
|
"""
|
2023-09-22 18:41:30 +00:00
|
|
|
import re
|
|
|
|
|
2023-09-21 20:37:17 +00:00
|
|
|
from granary.bluesky import Bluesky
|
|
|
|
|
|
|
|
from activitypub import ActivityPub
|
|
|
|
from atproto import ATProto
|
2023-09-22 18:41:30 +00:00
|
|
|
from common import DOMAIN_RE, host_url, SUPERDOMAIN
|
2023-09-21 20:37:17 +00:00
|
|
|
from models import User
|
|
|
|
from protocol import Protocol, PROTOCOLS
|
|
|
|
from web import Web
|
|
|
|
|
|
|
|
|
|
|
|
def convert_id(*, id, from_proto, to_proto):
|
|
|
|
"""Converts an id (not necessarily handle) from one protocol to another.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str)
|
|
|
|
from_proto (:class:`Protocol`)
|
|
|
|
to_proto (:class:`Protocol`)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the corresponding id in ``to_proto``
|
|
|
|
"""
|
|
|
|
assert id and from_proto and to_proto
|
|
|
|
assert from_proto != to_proto
|
2023-09-22 18:41:30 +00:00
|
|
|
assert from_proto.owns_id(id) is not False
|
2023-09-21 20:37:17 +00:00
|
|
|
|
|
|
|
match (from_proto.LABEL, to_proto.LABEL):
|
|
|
|
case (_, 'atproto'):
|
|
|
|
user = from_proto.get_by_id(id)
|
|
|
|
return user.atproto_did if user else None
|
|
|
|
case ('atproto', _):
|
|
|
|
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}')
|
|
|
|
case ('activitypub', 'web'):
|
|
|
|
return id
|
|
|
|
# fake protocol is only for unit tests
|
|
|
|
case (_, 'fake'):
|
|
|
|
return f'fake:{id}'
|
|
|
|
case ('fake', _):
|
|
|
|
return id
|
|
|
|
|
2023-09-22 18:41:30 +00:00
|
|
|
assert False, (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`)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the corresponding handle in ``to_proto``
|
|
|
|
"""
|
|
|
|
assert handle and from_proto and to_proto
|
|
|
|
assert from_proto != to_proto
|
2023-09-22 19:14:50 +00:00
|
|
|
assert from_proto.owns_handle(handle) is not False
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
match (from_proto.LABEL, to_proto.LABEL):
|
|
|
|
case (_, 'activitypub'):
|
|
|
|
if True: # basic
|
|
|
|
return f'@{handle}@{from_proto.ABBREV}{SUPERDOMAIN}'
|
|
|
|
else: # enhanced (TODO)
|
|
|
|
return f'@{handle}@{handle}'
|
|
|
|
case (_, 'atproto' | 'nostr'):
|
|
|
|
handle = handle.lstrip('@').replace('@', '.')
|
|
|
|
if True: # basic
|
|
|
|
return f'{handle}.{from_proto.ABBREV}{SUPERDOMAIN}'
|
|
|
|
else: # enhanced (TODO)
|
|
|
|
return handle
|
|
|
|
case ('activitypub', 'web'):
|
|
|
|
user, instance = handle.lstrip('@').split('@')
|
|
|
|
return f'instance/@user' # TODO
|
|
|
|
case (_, 'web'):
|
|
|
|
return handle
|
|
|
|
case (_, 'fake'):
|
2023-09-22 21:53:36 +00:00
|
|
|
return f'fake:handle:{handle}'
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
assert False, (id, from_proto, to_proto)
|