2023-10-26 20:49:42 +00:00
|
|
|
"""Translates user ids, handles, and object ids between protocols.
|
2023-09-21 20:37:17 +00:00
|
|
|
|
|
|
|
https://fed.brid.gy/docs#translate
|
|
|
|
"""
|
2023-10-26 23:20:30 +00:00
|
|
|
import logging
|
2023-09-22 18:41:30 +00:00
|
|
|
import re
|
2024-06-26 23:15:32 +00:00
|
|
|
from threading import Lock
|
2023-11-03 18:37:36 +00:00
|
|
|
from urllib.parse import urljoin, urlparse
|
2023-09-22 18:41:30 +00:00
|
|
|
|
2024-06-26 23:15:32 +00:00
|
|
|
from cachetools import cached, LRUCache
|
2023-11-03 18:37:36 +00:00
|
|
|
from flask import request
|
2023-12-24 18:04:01 +00:00
|
|
|
from google.cloud.ndb.query import FilterNode, Query
|
2024-04-09 17:04:16 +00:00
|
|
|
from granary.bluesky import BSKY_APP_URL_RE, web_url_to_at_uri
|
2023-12-24 18:20:04 +00:00
|
|
|
from oauth_dropins.webutil import util
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2024-04-22 20:24:24 +00:00
|
|
|
from common import (
|
|
|
|
LOCAL_DOMAINS,
|
|
|
|
PRIMARY_DOMAIN,
|
|
|
|
PROTOCOL_DOMAINS,
|
|
|
|
subdomain_wrap,
|
|
|
|
SUPERDOMAIN,
|
|
|
|
)
|
2023-10-26 23:00:03 +00:00
|
|
|
import models
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-11-07 04:17:23 +00:00
|
|
|
# Protocols to check User.copies and Object.copies before translating
|
2024-04-23 23:52:53 +00:00
|
|
|
# populated in models.reset_protocol_properties
|
|
|
|
COPIES_PROTOCOLS = None
|
2023-11-07 04:17:23 +00:00
|
|
|
|
2024-05-02 22:41:41 +00:00
|
|
|
# Webfinger allows all sorts of characters that ATProto handles don't,
|
|
|
|
# notably _ and ~. Map those to -.
|
2024-05-03 21:51:26 +00:00
|
|
|
# ( : (colon) is mostly just used in the fake protocols in unit tests.)
|
2024-05-02 22:41:41 +00:00
|
|
|
# https://www.rfc-editor.org/rfc/rfc7565.html#section-7
|
|
|
|
# https://atproto.com/specs/handle
|
|
|
|
# https://github.com/snarfed/bridgy-fed/issues/982
|
|
|
|
# https://github.com/swicg/activitypub-webfinger/issues/9
|
2024-05-03 21:51:26 +00:00
|
|
|
ATPROTO_DASH_CHARS = ('_', '~', ':')
|
2024-05-02 22:41:41 +00:00
|
|
|
|
2024-06-02 15:21:18 +00:00
|
|
|
# can't use translate_user_id because Web.owns_id checks valid_domain, which
|
|
|
|
# doesn't allow our protocol subdomains
|
|
|
|
BOT_ACTOR_AP_IDS = tuple(f'https://{domain}/{domain}' for domain in PROTOCOL_DOMAINS)
|
|
|
|
|
2023-12-24 18:20:04 +00:00
|
|
|
|
2024-06-26 23:15:32 +00:00
|
|
|
@cached(LRUCache(10000), lock=Lock())
|
2023-12-24 18:20:04 +00:00
|
|
|
def web_ap_base_domain(user_domain):
|
2024-04-09 16:48:16 +00:00
|
|
|
"""Returns the full Bridgy Fed domain to use for a given Web user.
|
2023-12-24 18:20:04 +00:00
|
|
|
|
|
|
|
Specifically, returns ``http://localhost/` if we're running locally,
|
2024-06-26 23:15:32 +00:00
|
|
|
``https://[ap_subdomain].brid.gy/`` for the Web entity for this domain if it
|
|
|
|
exists, otherwise ``https://web.brid.gy/``.
|
2023-12-24 18:20:04 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_domain (str)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str:
|
|
|
|
"""
|
2024-04-25 21:29:48 +00:00
|
|
|
if (request.host in LOCAL_DOMAINS and
|
|
|
|
not (user_domain == PRIMARY_DOMAIN or user_domain in PROTOCOL_DOMAINS)):
|
2023-12-24 18:20:04 +00:00
|
|
|
return request.host_url
|
|
|
|
|
2024-06-26 23:15:32 +00:00
|
|
|
from web import Web
|
|
|
|
if user := Web.get_by_id(user_domain):
|
|
|
|
return f'https://{user.ap_subdomain}{SUPERDOMAIN}/'
|
|
|
|
|
|
|
|
return f'https://web{SUPERDOMAIN}/'
|
2023-12-24 18:04:01 +00:00
|
|
|
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
def translate_user_id(*, id, from_, to):
|
2023-10-26 20:49:42 +00:00
|
|
|
"""Translate a user id from one protocol to another.
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2024-04-09 17:04:16 +00:00
|
|
|
TODO: unify with :func:`translate_object_id`.
|
|
|
|
|
2023-09-21 20:37:17 +00:00
|
|
|
Args:
|
|
|
|
id (str)
|
2024-04-21 04:03:06 +00:00
|
|
|
from_ (protocol.Protocol)
|
|
|
|
to (protocol.Protocol)
|
2023-09-21 20:37:17 +00:00
|
|
|
|
|
|
|
Returns:
|
2024-04-21 04:03:06 +00:00
|
|
|
str: the corresponding id in ``to``
|
2023-09-21 20:37:17 +00:00
|
|
|
"""
|
2024-04-21 04:03:06 +00:00
|
|
|
assert id and from_ and to, (id, from_, to)
|
|
|
|
assert from_.owns_id(id) is not False or from_.LABEL == 'ui', \
|
|
|
|
(id, from_.LABEL, to.LABEL)
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2023-11-03 18:00:34 +00:00
|
|
|
parsed = urlparse(id)
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_.LABEL == 'web' and parsed.path.strip('/') == '':
|
2023-11-03 18:00:34 +00:00
|
|
|
# home page; replace with domain
|
|
|
|
id = parsed.netloc
|
2024-04-09 16:48:16 +00:00
|
|
|
|
|
|
|
# bsky.app profile URL to DID
|
2024-04-21 04:03:06 +00:00
|
|
|
if to.LABEL == 'atproto':
|
2024-04-09 16:48:16 +00:00
|
|
|
if match := BSKY_APP_URL_RE.match(id):
|
|
|
|
repo = match.group('id')
|
|
|
|
if repo.startswith('did:'):
|
|
|
|
return repo
|
|
|
|
|
|
|
|
from atproto import ATProto
|
|
|
|
try:
|
|
|
|
return ATProto.handle_to_id(repo)
|
|
|
|
except (AssertionError, ValueError) as e:
|
|
|
|
logger.warning(e)
|
|
|
|
return None
|
2023-11-03 18:00:34 +00:00
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_ == to:
|
2023-09-25 13:42:31 +00:00
|
|
|
return id
|
|
|
|
|
2023-11-03 21:53:19 +00:00
|
|
|
# follow use_instead
|
2024-04-21 04:03:06 +00:00
|
|
|
user = from_.get_by_id(id)
|
2023-11-03 21:53:19 +00:00
|
|
|
if user:
|
|
|
|
id = user.key.id()
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_.LABEL in COPIES_PROTOCOLS or to.LABEL in COPIES_PROTOCOLS:
|
2023-11-03 21:53:19 +00:00
|
|
|
if user:
|
2024-04-21 04:03:06 +00:00
|
|
|
if copy := user.get_copy(to):
|
2023-11-02 20:08:12 +00:00
|
|
|
return copy
|
|
|
|
if orig := models.get_original(id):
|
2024-04-21 04:03:06 +00:00
|
|
|
if isinstance(orig, to):
|
2023-11-02 20:08:12 +00:00
|
|
|
return orig.key.id()
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
match from_.LABEL, to.LABEL:
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'atproto' | 'nostr':
|
2024-08-06 18:05:40 +00:00
|
|
|
logger.debug(f"Can't translate user id {id} to {to.LABEL} , haven't copied it there yet!")
|
2023-11-02 20:08:12 +00:00
|
|
|
return None
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-11-03 00:53:07 +00:00
|
|
|
case 'web', 'activitypub':
|
2023-12-24 18:20:04 +00:00
|
|
|
return urljoin(web_ap_base_domain(id), id)
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case 'activitypub', 'web':
|
2023-09-21 20:37:17 +00:00
|
|
|
return id
|
2023-11-02 20:08:12 +00:00
|
|
|
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'activitypub' | 'web':
|
2024-04-21 04:03:06 +00:00
|
|
|
return subdomain_wrap(from_, f'/{to.ABBREV}/{id}')
|
2023-11-07 04:17:23 +00:00
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2024-04-17 23:43:10 +00:00
|
|
|
case _, 'fake' | 'other' | 'eefake':
|
2024-04-21 04:03:06 +00:00
|
|
|
return f'{to.LABEL}:u:{id}'
|
2023-10-27 00:18:01 +00:00
|
|
|
case 'fake' | 'other', _:
|
2023-11-07 04:17:23 +00:00
|
|
|
return id
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
assert False, (id, from_.LABEL, to.LABEL)
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
|
2024-05-29 19:08:10 +00:00
|
|
|
def normalize_user_id(*, id, proto):
|
|
|
|
"""Normalizes a user id to its canonical representation in a given protocol.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
* Web:
|
|
|
|
* user.com => user.com
|
|
|
|
* www.user.com => user.com
|
|
|
|
* https://user.com/ => user.com
|
|
|
|
* ATProto:
|
|
|
|
* did:plc:123 => did:plc:123
|
|
|
|
* https://bsky.app/profile/did:plc:123 => did:plc:123
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str)
|
|
|
|
proto (protocol.Protocol)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the normalized user id
|
|
|
|
"""
|
|
|
|
normalized = translate_user_id(id=id, from_=proto, to=proto)
|
|
|
|
|
|
|
|
if proto.LABEL == 'web':
|
|
|
|
normalized = util.domain_from_link(normalized)
|
|
|
|
|
|
|
|
return normalized
|
|
|
|
|
|
|
|
|
2024-05-29 23:18:15 +00:00
|
|
|
def profile_id(*, id, proto):
|
|
|
|
"""Returns the profile object id for a given user id.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
* Web: user.com => https:///user.com/
|
|
|
|
* ActivityPub: https://inst.ance/alice => https://inst.ance/alice
|
|
|
|
* ATProto: did:plc:123 => at://did:plc:123/app.bsky.actor.profile/self
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str)
|
|
|
|
proto (protocol.Protocol)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the profile id
|
|
|
|
"""
|
2024-05-29 23:38:56 +00:00
|
|
|
assert proto.owns_id(id) is not False, (id, proto.LABEL)
|
2024-05-29 23:18:15 +00:00
|
|
|
|
|
|
|
match proto.LABEL:
|
|
|
|
case 'atproto':
|
|
|
|
return f'at://{id}/app.bsky.actor.profile/self'
|
|
|
|
|
|
|
|
case 'fake':
|
|
|
|
return id.replace('fake:', 'fake:profile:')
|
|
|
|
|
|
|
|
case 'web':
|
|
|
|
return f'https://{id}/'
|
|
|
|
|
|
|
|
case _:
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
def translate_handle(*, handle, from_, to, enhanced):
|
2023-10-26 20:49:42 +00:00
|
|
|
"""Translates a user handle from one protocol to another.
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
handle (str)
|
2024-04-21 04:03:06 +00:00
|
|
|
from_ (protocol.Protocol)
|
|
|
|
to (protocol.Protocol)
|
2023-11-30 00:31:33 +00:00
|
|
|
enhanced (bool): whether to convert to an "enhanced" handle based on the
|
|
|
|
user's domain
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
Returns:
|
2024-04-21 04:03:06 +00:00
|
|
|
str: the corresponding handle in ``to``
|
2024-05-03 22:18:16 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: if the user's handle is invalid, eg begins or ends with an
|
|
|
|
underscore or dash
|
2023-09-22 18:41:30 +00:00
|
|
|
"""
|
2024-04-21 04:03:06 +00:00
|
|
|
assert handle and from_ and to, (handle, from_, to)
|
2024-05-03 22:18:16 +00:00
|
|
|
if not from_.LABEL == 'ui':
|
|
|
|
if from_.owns_handle(handle, allow_internal=True) is False:
|
|
|
|
raise ValueError(f'input handle {handle} is not valid for {from_.LABEL}')
|
2024-05-03 21:51:26 +00:00
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_ == to:
|
2023-09-25 13:42:31 +00:00
|
|
|
return handle
|
|
|
|
|
2024-05-03 22:18:16 +00:00
|
|
|
output = None
|
2024-04-21 04:03:06 +00:00
|
|
|
match from_.LABEL, to.LABEL:
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'activitypub':
|
2024-04-22 20:24:24 +00:00
|
|
|
domain = f'{from_.ABBREV}{SUPERDOMAIN}'
|
|
|
|
if enhanced or handle == PRIMARY_DOMAIN or handle in PROTOCOL_DOMAINS:
|
|
|
|
domain = handle
|
2024-05-03 22:18:16 +00:00
|
|
|
output = f'@{handle}@{domain}'
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2024-05-03 21:51:26 +00:00
|
|
|
case _, 'atproto':
|
2024-05-03 22:18:16 +00:00
|
|
|
output = handle.lstrip('@').replace('@', '.')
|
2024-05-03 21:51:26 +00:00
|
|
|
for from_char in ATPROTO_DASH_CHARS:
|
2024-05-03 22:18:16 +00:00
|
|
|
output = output.replace(from_char, '-')
|
2024-05-02 22:41:41 +00:00
|
|
|
|
2024-04-23 22:01:29 +00:00
|
|
|
if enhanced or handle == PRIMARY_DOMAIN or handle in PROTOCOL_DOMAINS:
|
2024-05-03 21:51:26 +00:00
|
|
|
pass
|
|
|
|
else:
|
2024-05-03 22:18:16 +00:00
|
|
|
output = f'{output}.{from_.ABBREV}{SUPERDOMAIN}'
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case 'activitypub', 'web':
|
2023-09-22 18:41:30 +00:00
|
|
|
user, instance = handle.lstrip('@').split('@')
|
2023-11-15 22:23:08 +00:00
|
|
|
# TODO: get this from the actor object's url field?
|
2024-05-03 22:18:16 +00:00
|
|
|
output = (f'https://{user}' if user == instance
|
2023-11-30 00:31:33 +00:00
|
|
|
else f'https://{instance}/@{user}')
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'web':
|
2024-05-03 22:18:16 +00:00
|
|
|
output = handle
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2024-04-17 23:43:10 +00:00
|
|
|
case _, 'fake' | 'other' | 'eefake':
|
2024-05-03 22:18:16 +00:00
|
|
|
output = f'{to.LABEL}:handle:{handle}'
|
|
|
|
|
|
|
|
assert output, (handle, from_.LABEL, to.LABEL)
|
|
|
|
# don't check Web handles because they're sometimes URLs, eg
|
|
|
|
# @user@instance => https://instance/@user
|
|
|
|
if to.LABEL != 'web' and to.owns_handle(output, allow_internal=True) is False:
|
|
|
|
raise ValueError(f'translated handle {output} is not valid for {to.LABEL}')
|
2023-09-22 18:41:30 +00:00
|
|
|
|
2024-05-03 22:18:16 +00:00
|
|
|
return output
|
2023-10-26 23:20:30 +00:00
|
|
|
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
def translate_object_id(*, id, from_, to):
|
2023-10-26 23:20:30 +00:00
|
|
|
"""Translates a user handle from one protocol to another.
|
|
|
|
|
2024-04-09 17:04:16 +00:00
|
|
|
TODO: unify with :func:`translate_user_id`.
|
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
Args:
|
|
|
|
id (str)
|
2024-04-21 04:03:06 +00:00
|
|
|
from_ (protocol.Protocol)
|
|
|
|
to (protocol.Protocol)
|
2023-10-26 23:20:30 +00:00
|
|
|
|
|
|
|
Returns:
|
2024-04-21 04:03:06 +00:00
|
|
|
str: the corresponding id in ``to``
|
2023-10-26 23:20:30 +00:00
|
|
|
"""
|
2024-04-21 04:03:06 +00:00
|
|
|
assert id and from_ and to, (id, from_, to)
|
|
|
|
assert from_.owns_id(id) is not False or from_.LABEL == 'ui'
|
2023-10-26 23:20:30 +00:00
|
|
|
|
2024-04-09 17:04:16 +00:00
|
|
|
# bsky.app profile URL to DID
|
2024-04-21 04:03:06 +00:00
|
|
|
if to.LABEL == 'atproto':
|
2024-04-09 17:04:16 +00:00
|
|
|
if match := BSKY_APP_URL_RE.match(id):
|
|
|
|
repo = match.group('id')
|
|
|
|
handle = None
|
|
|
|
if not repo.startswith('did:'):
|
|
|
|
handle = repo
|
|
|
|
from atproto import ATProto
|
|
|
|
try:
|
|
|
|
repo = ATProto.handle_to_id(repo)
|
|
|
|
except (AssertionError, ValueError) as e:
|
|
|
|
logger.warning(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return web_url_to_at_uri(id, handle=handle, did=repo)
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_ == to:
|
2023-10-26 23:20:30 +00:00
|
|
|
return id
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
if from_.LABEL in COPIES_PROTOCOLS or to.LABEL in COPIES_PROTOCOLS:
|
|
|
|
if obj := from_.load(id, remote=False):
|
|
|
|
if copy := obj.get_copy(to):
|
2023-11-02 20:08:12 +00:00
|
|
|
return copy
|
|
|
|
if orig := models.get_original(id):
|
|
|
|
return orig.key.id()
|
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
match from_.LABEL, to.LABEL:
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'atproto' | 'nostr':
|
2024-08-06 18:05:40 +00:00
|
|
|
logger.debug(f"Can't translate object id {id} to {to.LABEL} , haven't copied it there yet!")
|
2023-10-26 23:20:30 +00:00
|
|
|
return id
|
|
|
|
|
2023-11-03 18:37:36 +00:00
|
|
|
case 'web', 'activitypub':
|
2023-12-24 18:20:04 +00:00
|
|
|
return urljoin(web_ap_base_domain(util.domain_from_link(id)), f'/r/{id}')
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'activitypub' | 'web':
|
2024-04-21 04:03:06 +00:00
|
|
|
return subdomain_wrap(from_, f'/convert/{to.ABBREV}/{id}')
|
2023-10-26 23:20:30 +00:00
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2024-04-17 23:43:10 +00:00
|
|
|
case _, 'fake' | 'other' | 'eefake':
|
2024-04-21 04:03:06 +00:00
|
|
|
return f'{to.LABEL}:o:{from_.ABBREV}:{id}'
|
2023-10-27 00:18:01 +00:00
|
|
|
|
2024-04-21 04:03:06 +00:00
|
|
|
assert False, (id, from_.LABEL, to.LABEL)
|