ids.translate_handle: convert _ and ~ chars to - for Bluesky

hopefully fixes #982
pull/1020/head
Ryan Barrett 2024-05-02 15:41:41 -07:00
rodzic 533ed7441d
commit 4b95d49ccc
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
4 zmienionych plików z 22 dodań i 5 usunięć

Wyświetl plik

@ -258,14 +258,13 @@ class ATProto(User, Protocol):
# PDS URL shouldn't include trailing slash!
# https://atproto.com/specs/did#did-documents
pds_url = common.host_url().rstrip('/') if DEBUG else cls.PDS_URL
logger.info(f'Creating new did:plc for {user.key} {pds_url}')
did_plc = did.create_plc(user.handle_as('atproto'),
pds_url=pds_url, post_fn=util.requests_post)
handle = user.handle_as('atproto')
logger.info(f'Creating new did:plc for {user.key} {handle} {pds_url}')
did_plc = did.create_plc(handle, pds_url=pds_url, post_fn=util.requests_post)
Object.get_or_create(did_plc.did, raw=did_plc.doc)
# TODO: move this to ATProto.get_or_create?
add(user.copies, Target(uri=did_plc.did, protocol='atproto'))
handle = user.handle_as('atproto')
# create _atproto DNS record for handle resolution
# https://atproto.com/specs/handle#handle-resolution

15
ids.py
Wyświetl plik

@ -32,6 +32,17 @@ COPIES_PROTOCOLS = None
# Maps string domain to string subdomain (bsky, fed, or web).
_NON_WEB_SUBDOMAIN_SITES = None
# Webfinger allows all sorts of characters that ATProto handles don't,
# notably _ and ~. Map those to -.
# 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
TO_ATPROTO_CHARS = {
'_': '-',
'~': '-',
}
def web_ap_base_domain(user_domain):
"""Returns the full Bridgy Fed domain to use for a given Web user.
@ -167,6 +178,10 @@ def translate_handle(*, handle, from_, to, enhanced):
return f'@{handle}@{domain}'
case _, 'atproto' | 'nostr':
if to.LABEL == 'atproto':
for from_char, to_char in TO_ATPROTO_CHARS.items():
handle = handle.replace(from_char, to_char)
handle = handle.lstrip('@').replace('@', '.')
if enhanced or handle == PRIMARY_DOMAIN or handle in PROTOCOL_DOMAINS:
return handle

Wyświetl plik

@ -120,14 +120,17 @@ 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, 'u_se-r.com', Fake, 'fake:handle:u_se-r.com'),
(Web, 'user.com', Web, 'user.com'),
(ActivityPub, '@user@instance', ActivityPub, '@user@instance'),
(ActivityPub, '@user@instance', ATProto, 'user.instance.ap.brid.gy'),
(ActivityPub, '@u_se~r@instance', ATProto, 'u-se-r.instance.ap.brid.gy'),
(ActivityPub, '@user@instance', Fake, 'fake:handle:@user@instance'),
(ActivityPub, '@user@instance', Web, 'https://instance/@user'),
(ATProto, 'user.com', ActivityPub, '@user.com@bsky.brid.gy'),
(ATProto, 'u-se-r.com', ActivityPub, '@u-se-r.com@bsky.brid.gy'),
(ATProto, 'user.com', ATProto, 'user.com'),
(ATProto, 'user.com', Fake, 'fake:handle:user.com'),
(ATProto, 'user.com', Web, 'user.com'),

2
web.py
Wyświetl plik

@ -26,7 +26,7 @@ from werkzeug.exceptions import BadGateway, BadRequest, HTTPException, NotFound
import common
from common import add, DOMAIN_RE, PRIMARY_DOMAIN, PROTOCOL_DOMAINS, SUPERDOMAIN
from flask_app import app, cache
from ids import translate_handle, translate_object_id, translate_user_id
from ids import translate_object_id, translate_user_id
from models import Follower, Object, PROTOCOLS, Target, User
from protocol import Protocol