Skip to content

Commit

Permalink
AP: put Accept activity id on same BF domain as actor
Browse files Browse the repository at this point in the history
for #1093
  • Loading branch information
snarfed committed Nov 2, 2024
1 parent aab40d0 commit 3ffe589
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,13 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
activity['id'] = util.get_first(activity, 'url')

if wrap:
# TODO: for getting Accept off fed.brid.gy, should this use actor's subdomain?
# some fediverse servers (eg Misskey) require activity id and actor id
# to be on the same domain
# https://github.com/snarfed/bridgy-fed/issues/1093#issuecomment-2299247639
activity['id'] = redirect_wrap(activity.get('id'))
redirect_domain = util.domain_from_link(as1.get_id(activity, 'actor'))
if redirect_domain not in DOMAINS:
redirect_domain = None
activity['id'] = redirect_wrap(activity.get('id'), domain=redirect_domain)
activity['url'] = [redirect_wrap(u) for u in util.get_list(activity, 'url')]
if len(activity['url']) == 1:
activity['url'] = activity['url'][0]
Expand Down
11 changes: 9 additions & 2 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ def content_type(resp):
return type.split(';')[0]


def redirect_wrap(url):
def redirect_wrap(url, domain=None):
"""Returns a URL on our domain that redirects to this URL.
...to satisfy Mastodon's non-standard domain matching requirement. :(
Args:
url (str)
domain (str): optional Bridgy Fed domain to use. Must be in :attr:`DOMAINS`
* https://github.com/snarfed/bridgy-fed/issues/16#issuecomment-424799599
* https://github.com/tootsuite/mastodon/pull/6219#issuecomment-429142747
Expand All @@ -214,7 +215,13 @@ def redirect_wrap(url):
if not url or util.domain_from_link(url) in DOMAINS:
return url

return host_url('/r/') + url
path = '/r/' + url

if domain:
assert domain in DOMAINS, (domain, url)
return urljoin(f'https://{domain}/', path)

return host_url(path)


def subdomain_wrap(proto, path=None):
Expand Down
3 changes: 3 additions & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ def translate_object_id(*, id, from_, to):
return id

case 'web', 'activitypub':
# domain = util.domain_from_link(id)
# STATE: redirect_wrap w/domain ? wait no
# same for user_id
return urljoin(web_ap_base_domain(util.domain_from_link(id)), f'/r/{id}')

case _, 'activitypub' | 'web':
Expand Down
8 changes: 4 additions & 4 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
ACCEPT = {
'@context': ['https://www.w3.org/ns/activitystreams'],
'type': 'Accept',
'id': 'http://localhost/r/user.com/followers#accept-https://mas.to/follow',
'id': 'https://localhost/r/user.com/followers#accept-https://mas.to/follow',
'actor': 'http://localhost/user.com',
'object': {
'type': 'Follow',
Expand Down Expand Up @@ -1198,7 +1198,7 @@ def test_inbox_follow_accept_inner_follow_to(self, *mocks):
accept = {
'@context': ['https://www.w3.org/ns/activitystreams'],
'type': 'Accept',
'id': 'http://localhost/r/user.com/followers#accept-https://mas.to/follow',
'id': 'https://localhost/r/user.com/followers#accept-https://mas.to/follow',
'actor': 'http://localhost/user.com',
'object': {
'type': 'Follow',
Expand Down Expand Up @@ -1365,7 +1365,7 @@ def test_inbox_follow_web_brid_gy_subdomain(self, mock_head, mock_get, mock_post
self.assert_equals(('http://mas.to/inbox',), args)
self.assert_equals({
'type': 'Accept',
'id': 'http://localhost/r/user.com/followers#accept-https://mas.to/follow',
'id': 'https://localhost/r/user.com/followers#accept-https://mas.to/follow',
'actor': 'http://localhost/user.com',
'object': {
'type': 'Follow',
Expand Down Expand Up @@ -2614,7 +2614,7 @@ def test_postprocess_as2_dm_create(self):
}
self.assertEqual({
**dm,
'id': 'http://localhost/r/https://inst/dm#create',
'id': 'https://web.brid.gy/r/https://inst/dm#create',
}, postprocess_as2(copy.deepcopy(dm)))

@patch('requests.get')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def test_activitypub_follow_bsky_bot_user_enables_protocol(self, mock_get, mock_
self.assert_equals(('http://inst/inbox',), args)
self.assert_equals({
'type': 'Accept',
'id': 'http://localhost/r/bsky.brid.gy/followers#accept-http://inst/follow',
'id': 'https://bsky.brid.gy/r/bsky.brid.gy/followers#accept-http://inst/follow',
'actor': 'https://bsky.brid.gy/bsky.brid.gy',
'object': {
'actor': 'https://inst/alice',
Expand Down

0 comments on commit 3ffe589

Please sign in to comment.