ids.translate_object_id: handle Web.ap_subdomain

for https://github.com/snarfed/bridgy-fed/discussions/768
pull/770/head
Ryan Barrett 2023-12-24 10:20:04 -08:00
rodzic 2fafd83344
commit 199fb65dd6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 37 dodań i 24 usunięć

39
ids.py
Wyświetl plik

@ -8,6 +8,7 @@ from urllib.parse import urljoin, urlparse
from flask import request
from google.cloud.ndb.query import FilterNode, Query
from oauth_dropins.webutil import util
from common import subdomain_wrap, LOCAL_DOMAINS, PRIMARY_DOMAIN, SUPERDOMAIN
import models
@ -18,10 +19,26 @@ logger = logging.getLogger(__name__)
COPIES_PROTOCOLS = ('atproto', 'fake', 'other', 'nostr')
# Web user domains whose AP actor ids are on fed.brid.gy, not web.brid.gy, for
# historical compatibility. Loaded once at startup.
# historical compatibility. Loaded on first call to web_ap_subdomain().
_FED_SUBDOMAIN_SITES = None
def fed_subdomain_sites():
def web_ap_base_domain(user_domain):
"""Returns the full Bridgy Fed domain to user for a given Web user.
Specifically, returns ``http://localhost/` if we're running locally,
``https://fed.brid.gy/`` if the given Web user has ``ap_subdomain='fed'``,
otherwise ``https://web.brid.gy/``.
Args:
user_domain (str)
Returns:
str:
"""
if request.host in LOCAL_DOMAINS:
return request.host_url
global _FED_SUBDOMAIN_SITES
if _FED_SUBDOMAIN_SITES is None:
_FED_SUBDOMAIN_SITES = {
@ -31,7 +48,8 @@ def fed_subdomain_sites():
}
logger.info(f'Loaded {len(_FED_SUBDOMAIN_SITES)} fed subdomain Web users')
return _FED_SUBDOMAIN_SITES
subdomain = 'fed' if user_domain in _FED_SUBDOMAIN_SITES else 'web'
return f'https://{subdomain}{SUPERDOMAIN}/'
def translate_user_id(*, id, from_proto, to_proto):
@ -75,15 +93,7 @@ def translate_user_id(*, id, from_proto, to_proto):
return None
case 'web', 'activitypub':
# special case web => AP for historical backward compatibility
# 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}/'
return urljoin(base, id)
return urljoin(web_ap_base_domain(id), id)
case 'activitypub', 'web':
return id
@ -177,10 +187,7 @@ def translate_object_id(*, id, from_proto, to_proto):
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}')
return urljoin(web_ap_base_domain(util.domain_from_link(id)), f'/r/{id}')
case _, 'activitypub' | 'web':
return subdomain_wrap(from_proto, f'/convert/{to_proto.ABBREV}/{id}')

Wyświetl plik

@ -20,11 +20,11 @@ from web import Web
COMMENT_AS2 = {
**as2.from_as1(COMMENT),
'type': 'Note',
'id': 'https://fed.brid.gy/r/https://fake.com/123456',
'id': 'https://web.brid.gy/r/https://fake.com/123456',
'url': 'https://web.brid.gy/r/https://fake.com/123456',
'name': 'A ☕ reply',
'contentMap': {'en': COMMENT['content']},
'inReplyTo': 'https://fed.brid.gy/r/https://fake.com/123',
'inReplyTo': 'https://web.brid.gy/r/https://fake.com/123',
}
HTML = """\
<!DOCTYPE html>
@ -290,8 +290,7 @@ A ☕ reply
Object(id=url, mf2=parse_mf2(HTML_NO_ID)['items'][0]).put()
resp = self.client.get(f'/convert/ap/{url}',
base_url='https://web.brid.gy/')
resp = self.client.get(f'/convert/ap/{url}', base_url='https://web.brid.gy/')
self.assertEqual(200, resp.status_code)
self.assert_equals(COMMENT_AS2, resp.json, ignore=['to'])

Wyświetl plik

@ -161,7 +161,14 @@ class IdsTest(TestCase):
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)
@patch('ids._FED_SUBDOMAIN_SITES', new={'on-fed.com'})
def test_translate_user_id_web_ap_subdomain_fed(self):
for base_url in ['https://web.brid.gy/', 'https://fed.brid.gy/']:
with app.test_request_context('/', base_url=base_url):
got = translate_object_id(id='http://on-fed.com/post', from_proto=Web,
to_proto=ActivityPub)
self.assertEqual('https://fed.brid.gy/r/http://on-fed.com/post', got)
got = translate_object_id(id='http://on-web.com/post', from_proto=Web,
to_proto=ActivityPub)
self.assertEqual('https://web.brid.gy/r/http://on-web.com/post', got)