kopia lustrzana https://github.com/snarfed/bridgy-fed
ids.translate_object_id: special case Web => AP
also preserve local hostspull/714/head
rodzic
37d8b5d4fc
commit
6410d0ef1a
25
ids.py
25
ids.py
|
|
@ -4,9 +4,11 @@ https://fed.brid.gy/docs#translate
|
|||
"""
|
||||
import logging
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
from common import subdomain_wrap, SUPERDOMAIN
|
||||
from flask import request
|
||||
|
||||
from common import subdomain_wrap, LOCAL_DOMAINS, PRIMARY_DOMAIN, SUPERDOMAIN
|
||||
import models
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -48,11 +50,16 @@ def translate_user_id(*, id, from_proto, to_proto):
|
|||
return found
|
||||
logger.warning(f"Can't translate user id {id} to {to_proto} , haven't copied it to/from there yet!")
|
||||
return None
|
||||
|
||||
case 'web', 'activitypub':
|
||||
# for historical backward compatibility
|
||||
return f'https://fed.brid.gy/{id}'
|
||||
# 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, id)
|
||||
|
||||
case _, 'activitypub':
|
||||
return subdomain_wrap(from_proto, f'/ap/{id}')
|
||||
|
||||
case 'activitypub', 'web':
|
||||
return id
|
||||
|
||||
|
|
@ -90,17 +97,21 @@ def translate_handle(*, handle, from_proto, to_proto):
|
|||
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
|
||||
|
||||
# only for unit tests
|
||||
case _, 'fake':
|
||||
return f'fake:handle:{handle}'
|
||||
|
|
@ -141,6 +152,12 @@ def translate_object_id(*, id, from_proto, to_proto):
|
|||
logger.warning(f"Can't translate object id {id} to {to_proto} , haven't copied it to/from there yet!")
|
||||
return id
|
||||
|
||||
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}')
|
||||
|
||||
case _, 'activitypub' | 'web':
|
||||
return subdomain_wrap(from_proto, f'convert/{to_proto.ABBREV}/{id}')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
"""Unit tests for ids.py."""
|
||||
from activitypub import ActivityPub
|
||||
from atproto import ATProto
|
||||
from flask_app import app
|
||||
from ids import translate_handle, translate_object_id, translate_user_id
|
||||
from models import Target
|
||||
from web import Web
|
||||
|
|
@ -29,8 +30,8 @@ class IdsTest(TestCase):
|
|||
(Fake, 'fake:user', ATProto, 'did:plc:789'),
|
||||
(Fake, 'fake:user', Fake, 'fake:user'),
|
||||
(Fake, 'fake:user', Web, 'fake:user'),
|
||||
(Web, 'user.com', ActivityPub, 'https://fed.brid.gy/user.com'),
|
||||
(Web, 'https://user.com/', ActivityPub, 'https://fed.brid.gy/user.com'),
|
||||
(Web, 'user.com', ActivityPub, 'http://localhost/user.com'),
|
||||
(Web, 'https://user.com/', ActivityPub, 'http://localhost/user.com'),
|
||||
(Web, 'user.com', ATProto, 'did:plc:123'),
|
||||
(Web, 'https://user.com', ATProto, 'did:plc:123'),
|
||||
(Web, 'user.com', Fake, 'fake:u:user.com'),
|
||||
|
|
@ -41,6 +42,10 @@ class IdsTest(TestCase):
|
|||
self.assertEqual(expected, translate_user_id(
|
||||
id=id, from_proto=from_, to_proto=to))
|
||||
|
||||
with app.test_request_context('/', base_url='https://web.brid.gy/'):
|
||||
self.assertEqual('https://fed.brid.gy/user.com', translate_user_id(
|
||||
id='user.com', from_proto=Web, to_proto=ActivityPub))
|
||||
|
||||
def test_translate_user_id_no_copy_did_stored(self):
|
||||
for proto, id in [
|
||||
(Web, 'user.com'),
|
||||
|
|
@ -113,8 +118,7 @@ class IdsTest(TestCase):
|
|||
(Fake, 'fake:post', ATProto, 'at://did/fa/post'),
|
||||
(Fake, 'fake:post', Fake, 'fake:post'),
|
||||
(Fake, 'fake:post', Web, 'https://fa.brid.gy/convert/web/fake:post'),
|
||||
(Web, 'http://post',
|
||||
ActivityPub, 'https://web.brid.gy/convert/ap/http:/post'),
|
||||
(Web, 'http://post', ActivityPub, 'http://localhost/r/http://post'),
|
||||
(Web, 'http://post', ATProto, 'at://did/web/post'),
|
||||
(Web, 'http://post', Fake, 'fake:o:web:http://post'),
|
||||
(Web, 'http://post', Web, 'http://post'),
|
||||
|
|
@ -122,3 +126,8 @@ class IdsTest(TestCase):
|
|||
with self.subTest(from_=from_.LABEL, to=to.LABEL):
|
||||
self.assertEqual(expected, translate_object_id(
|
||||
id=id, from_proto=from_, to_proto=to))
|
||||
|
||||
with app.test_request_context('/', base_url='https://web.brid.gy/'):
|
||||
got = translate_object_id(id='http://post', from_proto=Web,
|
||||
to_proto=ActivityPub)
|
||||
self.assertEqual('https://fed.brid.gy/r/http://post', got)
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue