Skip to content

Commit 199fb65

Browse files
committed
ids.translate_object_id: handle Web.ap_subdomain
for #768
1 parent 2fafd83 commit 199fb65

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

Diff for: ids.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from flask import request
1010
from google.cloud.ndb.query import FilterNode, Query
11+
from oauth_dropins.webutil import util
1112

1213
from common import subdomain_wrap, LOCAL_DOMAINS, PRIMARY_DOMAIN, SUPERDOMAIN
1314
import models
@@ -18,10 +19,26 @@
1819
COPIES_PROTOCOLS = ('atproto', 'fake', 'other', 'nostr')
1920

2021
# Web user domains whose AP actor ids are on fed.brid.gy, not web.brid.gy, for
21-
# historical compatibility. Loaded once at startup.
22+
# historical compatibility. Loaded on first call to web_ap_subdomain().
2223
_FED_SUBDOMAIN_SITES = None
2324

24-
def fed_subdomain_sites():
25+
26+
def web_ap_base_domain(user_domain):
27+
"""Returns the full Bridgy Fed domain to user for a given Web user.
28+
29+
Specifically, returns ``http://localhost/` if we're running locally,
30+
``https://fed.brid.gy/`` if the given Web user has ``ap_subdomain='fed'``,
31+
otherwise ``https://web.brid.gy/``.
32+
33+
Args:
34+
user_domain (str)
35+
36+
Returns:
37+
str:
38+
"""
39+
if request.host in LOCAL_DOMAINS:
40+
return request.host_url
41+
2542
global _FED_SUBDOMAIN_SITES
2643
if _FED_SUBDOMAIN_SITES is None:
2744
_FED_SUBDOMAIN_SITES = {
@@ -31,7 +48,8 @@ def fed_subdomain_sites():
3148
}
3249
logger.info(f'Loaded {len(_FED_SUBDOMAIN_SITES)} fed subdomain Web users')
3350

34-
return _FED_SUBDOMAIN_SITES
51+
subdomain = 'fed' if user_domain in _FED_SUBDOMAIN_SITES else 'web'
52+
return f'https://{subdomain}{SUPERDOMAIN}/'
3553

3654

3755
def translate_user_id(*, id, from_proto, to_proto):
@@ -75,15 +93,7 @@ def translate_user_id(*, id, from_proto, to_proto):
7593
return None
7694

7795
case 'web', 'activitypub':
78-
# special case web => AP for historical backward compatibility
79-
# also note that Web.id_as overrides this to use Web.ap_subdomain!
80-
if request.host in LOCAL_DOMAINS:
81-
base = request.host_url
82-
else:
83-
subdomain = 'fed' if id in fed_subdomain_sites() else 'web'
84-
base = f'https://{subdomain}{SUPERDOMAIN}/'
85-
86-
return urljoin(base, id)
96+
return urljoin(web_ap_base_domain(id), id)
8797

8898
case 'activitypub', 'web':
8999
return id
@@ -177,10 +187,7 @@ def translate_object_id(*, id, from_proto, to_proto):
177187
return id
178188

179189
case 'web', 'activitypub':
180-
# special case web => AP for historical backward compatibility
181-
base = (request.host_url if request.host in LOCAL_DOMAINS
182-
else f'https://{PRIMARY_DOMAIN}')
183-
return urljoin(base, f'/r/{id}')
190+
return urljoin(web_ap_base_domain(util.domain_from_link(id)), f'/r/{id}')
184191

185192
case _, 'activitypub' | 'web':
186193
return subdomain_wrap(from_proto, f'/convert/{to_proto.ABBREV}/{id}')

Diff for: tests/test_convert.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
COMMENT_AS2 = {
2121
**as2.from_as1(COMMENT),
2222
'type': 'Note',
23-
'id': 'https://fed.brid.gy/r/https://fake.com/123456',
23+
'id': 'https://web.brid.gy/r/https://fake.com/123456',
2424
'url': 'https://web.brid.gy/r/https://fake.com/123456',
2525
'name': 'A ☕ reply',
2626
'contentMap': {'en': COMMENT['content']},
27-
'inReplyTo': 'https://fed.brid.gy/r/https://fake.com/123',
27+
'inReplyTo': 'https://web.brid.gy/r/https://fake.com/123',
2828
}
2929
HTML = """\
3030
<!DOCTYPE html>
@@ -290,8 +290,7 @@ def test_web_to_activitypub_fetch(self, mock_get):
290290

291291
Object(id=url, mf2=parse_mf2(HTML_NO_ID)['items'][0]).put()
292292

293-
resp = self.client.get(f'/convert/ap/{url}',
294-
base_url='https://web.brid.gy/')
293+
resp = self.client.get(f'/convert/ap/{url}', base_url='https://web.brid.gy/')
295294
self.assertEqual(200, resp.status_code)
296295
self.assert_equals(COMMENT_AS2, resp.json, ignore=['to'])
297296

Diff for: tests/test_ids.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,14 @@ def test_translate_object_id(self):
161161
self.assertEqual(expected, translate_object_id(
162162
id=id, from_proto=from_, to_proto=to))
163163

164-
with app.test_request_context('/', base_url='https://web.brid.gy/'):
165-
got = translate_object_id(id='http://post', from_proto=Web,
166-
to_proto=ActivityPub)
167-
self.assertEqual('https://fed.brid.gy/r/http://post', got)
164+
@patch('ids._FED_SUBDOMAIN_SITES', new={'on-fed.com'})
165+
def test_translate_user_id_web_ap_subdomain_fed(self):
166+
for base_url in ['https://web.brid.gy/', 'https://fed.brid.gy/']:
167+
with app.test_request_context('/', base_url=base_url):
168+
got = translate_object_id(id='http://on-fed.com/post', from_proto=Web,
169+
to_proto=ActivityPub)
170+
self.assertEqual('https://fed.brid.gy/r/http://on-fed.com/post', got)
171+
172+
got = translate_object_id(id='http://on-web.com/post', from_proto=Web,
173+
to_proto=ActivityPub)
174+
self.assertEqual('https://web.brid.gy/r/http://on-web.com/post', got)

0 commit comments

Comments
 (0)