Skip to content

Commit

Permalink
ids.translate_handle: convert _ and ~ chars to - for Bluesky
Browse files Browse the repository at this point in the history
hopefully fixes #982
  • Loading branch information
snarfed committed May 2, 2024
1 parent 533ed74 commit 4b95d49
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
7 changes: 3 additions & 4 deletions atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,13 @@ def create_for(cls, user):
# 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
Expand Down
15 changes: 15 additions & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
# 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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/test_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ def test_translate_handle(self):
(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'),
Expand Down
2 changes: 1 addition & 1 deletion web.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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

Expand Down

0 comments on commit 4b95d49

Please sign in to comment.