8
8
9
9
from flask import request
10
10
from google .cloud .ndb .query import FilterNode , Query
11
+ from oauth_dropins .webutil import util
11
12
12
13
from common import subdomain_wrap , LOCAL_DOMAINS , PRIMARY_DOMAIN , SUPERDOMAIN
13
14
import models
18
19
COPIES_PROTOCOLS = ('atproto' , 'fake' , 'other' , 'nostr' )
19
20
20
21
# 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() .
22
23
_FED_SUBDOMAIN_SITES = None
23
24
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
+
25
42
global _FED_SUBDOMAIN_SITES
26
43
if _FED_SUBDOMAIN_SITES is None :
27
44
_FED_SUBDOMAIN_SITES = {
@@ -31,7 +48,8 @@ def fed_subdomain_sites():
31
48
}
32
49
logger .info (f'Loaded { len (_FED_SUBDOMAIN_SITES )} fed subdomain Web users' )
33
50
34
- return _FED_SUBDOMAIN_SITES
51
+ subdomain = 'fed' if user_domain in _FED_SUBDOMAIN_SITES else 'web'
52
+ return f'https://{ subdomain } { SUPERDOMAIN } /'
35
53
36
54
37
55
def translate_user_id (* , id , from_proto , to_proto ):
@@ -75,15 +93,7 @@ def translate_user_id(*, id, from_proto, to_proto):
75
93
return None
76
94
77
95
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 )
87
97
88
98
case 'activitypub' , 'web' :
89
99
return id
@@ -177,10 +187,7 @@ def translate_object_id(*, id, from_proto, to_proto):
177
187
return id
178
188
179
189
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 } ' )
184
191
185
192
case _, 'activitypub' | 'web' :
186
193
return subdomain_wrap (from_proto , f'/convert/{ to_proto .ABBREV } /{ id } ' )
0 commit comments