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
|
2023-11-03 18:37:36 +00:00
|
|
|
from urllib.parse import urljoin, urlparse
|
2023-09-22 18:41:30 +00:00
|
|
|
|
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
|
2023-11-03 18:37:36 +00:00
|
|
|
|
|
|
|
from common import subdomain_wrap, LOCAL_DOMAINS, PRIMARY_DOMAIN, 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
|
|
|
|
COPIES_PROTOCOLS = ('atproto', 'fake', 'other', 'nostr')
|
|
|
|
|
2023-12-24 18:04:01 +00:00
|
|
|
# Web user domains whose AP actor ids are on fed.brid.gy, not web.brid.gy, for
|
|
|
|
# historical compatibility. Loaded once at startup.
|
|
|
|
_FED_SUBDOMAIN_SITES = None
|
|
|
|
|
|
|
|
def fed_subdomain_sites():
|
|
|
|
global _FED_SUBDOMAIN_SITES
|
|
|
|
if _FED_SUBDOMAIN_SITES is None:
|
|
|
|
_FED_SUBDOMAIN_SITES = {
|
|
|
|
key.id() for key in Query('MagicKey',
|
|
|
|
filters=FilterNode('ap_subdomain', '=', 'fed')
|
|
|
|
).fetch(keys_only=True)
|
|
|
|
}
|
|
|
|
logger.info(f'Loaded {len(_FED_SUBDOMAIN_SITES)} fed subdomain Web users')
|
|
|
|
|
|
|
|
return _FED_SUBDOMAIN_SITES
|
|
|
|
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2023-10-26 20:49:42 +00:00
|
|
|
def translate_user_id(*, id, from_proto, to_proto):
|
|
|
|
"""Translate a user id from one protocol to another.
|
2023-09-21 20:37:17 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str)
|
2023-10-06 06:32:31 +00:00
|
|
|
from_proto (protocol.Protocol)
|
|
|
|
to_proto (protocol.Protocol)
|
2023-09-21 20:37:17 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the corresponding id in ``to_proto``
|
|
|
|
"""
|
|
|
|
assert id and from_proto and to_proto
|
2023-11-06 20:18:11 +00:00
|
|
|
assert from_proto.owns_id(id) is not False or from_proto.LABEL == 'ui'
|
2023-09-21 20:37:17 +00:00
|
|
|
|
2023-11-03 18:00:34 +00:00
|
|
|
parsed = urlparse(id)
|
|
|
|
if from_proto.LABEL == 'web' and parsed.path.strip('/') == '':
|
|
|
|
# home page; replace with domain
|
|
|
|
id = parsed.netloc
|
|
|
|
|
2023-09-25 13:42:31 +00:00
|
|
|
if from_proto == to_proto:
|
|
|
|
return id
|
|
|
|
|
2023-11-03 21:53:19 +00:00
|
|
|
# follow use_instead
|
|
|
|
user = from_proto.get_by_id(id)
|
|
|
|
if user:
|
|
|
|
id = user.key.id()
|
|
|
|
|
2023-11-07 04:17:23 +00:00
|
|
|
if from_proto.LABEL in COPIES_PROTOCOLS or to_proto.LABEL in COPIES_PROTOCOLS:
|
2023-11-03 21:53:19 +00:00
|
|
|
if user:
|
2023-11-02 20:08:12 +00:00
|
|
|
if copy := user.get_copy(to_proto):
|
|
|
|
return copy
|
|
|
|
if orig := models.get_original(id):
|
|
|
|
if isinstance(orig, to_proto):
|
|
|
|
return orig.key.id()
|
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
match from_proto.LABEL, to_proto.LABEL:
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'atproto' | 'nostr':
|
|
|
|
logger.warning(f"Can't translate user id {id} to {to_proto.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-11-03 18:37:36 +00:00
|
|
|
# special case web => AP for historical backward compatibility
|
2023-12-24 18:04:01 +00:00
|
|
|
# also note that Web.id_as overrides this to use Web.ap_subdomain!
|
|
|
|
if request.host in LOCAL_DOMAINS:
|
|
|
|
base = request.host_url
|
|
|
|
else:
|
|
|
|
subdomain = 'fed' if id in fed_subdomain_sites() else 'web'
|
|
|
|
base = f'https://{subdomain}{SUPERDOMAIN}/'
|
|
|
|
|
2023-11-03 18:37:36 +00:00
|
|
|
return urljoin(base, id)
|
|
|
|
|
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':
|
|
|
|
return subdomain_wrap(from_proto, f'/{to_proto.ABBREV}/{id}')
|
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'fake' | 'other':
|
|
|
|
return f'{to_proto.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
|
|
|
|
2023-09-22 18:41:30 +00:00
|
|
|
assert False, (id, from_proto, to_proto)
|
|
|
|
|
|
|
|
|
2023-11-30 00:31:33 +00:00
|
|
|
def translate_handle(*, handle, from_proto, to_proto, 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)
|
2023-10-06 06:32:31 +00:00
|
|
|
from_proto (protocol.Protocol)
|
|
|
|
to_proto (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:
|
|
|
|
str: the corresponding handle in ``to_proto``
|
|
|
|
"""
|
|
|
|
assert handle and from_proto and to_proto
|
2023-11-06 20:18:11 +00:00
|
|
|
assert from_proto.owns_handle(handle) is not False or from_proto.LABEL == 'ui'
|
2023-09-22 18:41:30 +00:00
|
|
|
|
2023-09-25 13:42:31 +00:00
|
|
|
if from_proto == to_proto:
|
|
|
|
return handle
|
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
match from_proto.LABEL, to_proto.LABEL:
|
|
|
|
case _, 'activitypub':
|
2023-11-30 00:31:33 +00:00
|
|
|
domain = handle if enhanced else f'{from_proto.ABBREV}{SUPERDOMAIN}'
|
|
|
|
return f'@{handle}@{domain}'
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'atproto' | 'nostr':
|
2023-09-22 18:41:30 +00:00
|
|
|
handle = handle.lstrip('@').replace('@', '.')
|
2023-11-30 00:31:33 +00:00
|
|
|
return (handle if enhanced
|
|
|
|
else f'{handle}.{from_proto.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?
|
2023-11-30 00:31:33 +00:00
|
|
|
return (f'https://{user}' if user == instance
|
|
|
|
else f'https://{instance}/@{user}')
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'web':
|
2023-09-22 18:41:30 +00:00
|
|
|
return handle
|
2023-11-03 18:37:36 +00:00
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'fake':
|
2023-09-22 21:53:36 +00:00
|
|
|
return f'fake:handle:{handle}'
|
2023-10-27 00:18:01 +00:00
|
|
|
case _, 'other':
|
|
|
|
return f'other:handle:{handle}'
|
2023-09-22 18:41:30 +00:00
|
|
|
|
|
|
|
assert False, (id, from_proto, to_proto)
|
2023-10-26 23:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def translate_object_id(*, id, from_proto, to_proto):
|
|
|
|
"""Translates a user handle from one protocol to another.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str)
|
|
|
|
from_proto (protocol.Protocol)
|
|
|
|
to_proto (protocol.Protocol)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: the corresponding id in ``to_proto``
|
|
|
|
"""
|
|
|
|
assert id and from_proto and to_proto
|
2023-11-06 20:18:11 +00:00
|
|
|
assert from_proto.owns_id(id) is not False or from_proto.LABEL == 'ui'
|
2023-10-26 23:20:30 +00:00
|
|
|
|
|
|
|
if from_proto == to_proto:
|
|
|
|
return id
|
|
|
|
|
2023-11-07 04:17:23 +00:00
|
|
|
if from_proto.LABEL in COPIES_PROTOCOLS or to_proto.LABEL in COPIES_PROTOCOLS:
|
2023-11-02 20:08:12 +00:00
|
|
|
if obj := from_proto.load(id, remote=False):
|
|
|
|
if copy := obj.get_copy(to_proto):
|
|
|
|
return copy
|
|
|
|
if orig := models.get_original(id):
|
|
|
|
return orig.key.id()
|
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
match from_proto.LABEL, to_proto.LABEL:
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'atproto' | 'nostr':
|
|
|
|
logger.warning(f"Can't translate object id {id} to {to_proto.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':
|
|
|
|
# special case web => AP for historical backward compatibility
|
|
|
|
base = (request.host_url if request.host in LOCAL_DOMAINS
|
|
|
|
else f'https://{PRIMARY_DOMAIN}')
|
|
|
|
return urljoin(base, f'/r/{id}')
|
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
case _, 'activitypub' | 'web':
|
2023-11-07 04:17:23 +00:00
|
|
|
return subdomain_wrap(from_proto, f'/convert/{to_proto.ABBREV}/{id}')
|
2023-10-26 23:20:30 +00:00
|
|
|
|
2023-10-27 00:18:01 +00:00
|
|
|
# only for unit tests
|
2023-11-07 04:17:23 +00:00
|
|
|
case _, 'fake' | 'other':
|
|
|
|
return f'{to_proto.LABEL}:o:{from_proto.ABBREV}:{id}'
|
2023-10-27 00:18:01 +00:00
|
|
|
|
2023-10-26 23:20:30 +00:00
|
|
|
assert False, (id, from_proto, to_proto)
|