From b25960ca93ed25c27a76a32e2d24fe7f4f5f9ce9 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Sun, 14 Oct 2018 07:58:17 -0700 Subject: [PATCH] wrap activity ids and urls and actor ids in our /r/ endpoint for #16, #32. cc @swentel. --- common.py | 18 ++++++++++++++++++ tests/test_activitypub.py | 2 +- tests/test_webmention.py | 12 ++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/common.py b/common.py index b12866c8..f7f5a7ec 100644 --- a/common.py +++ b/common.py @@ -267,6 +267,9 @@ def postprocess_as2(activity, target=None, key=None): assert activity.get('id') or (isinstance(obj, dict) and obj.get('id')) + activity['id'] = redirect_wrap(activity['id']) + activity['url'] = redirect_wrap(activity['url']) + # cc public and target's author(s) and recipients # https://www.w3.org/TR/activitystreams-vocabulary/#audienceTargeting # https://w3c.github.io/activitypub/#delivery @@ -299,6 +302,21 @@ def postprocess_as2_actor(actor): domain = urlparse.urlparse(url).netloc actor.setdefault('preferredUsername', domain) actor['id'] = '%s/%s' % (appengine_config.HOST_URL, domain) + actor['url'] = redirect_wrap(url) + + +def redirect_wrap(url): + """Returns a URL on our domain that redirects to this URL. + + ...to satisfy Mastodon's non-standard domain matching requirement. :( + + https://github.com/snarfed/bridgy-fed/issues/16#issuecomment-424799599 + https://github.com/tootsuite/mastodon/pull/6219#issuecomment-429142747 + """ + prefix = urlparse.urljoin(appengine_config.HOST_URL, '/r/') + if url.startswith(prefix): + return url + return prefix + url def beautifulsoup_parse(html, **kwargs): diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index cad7ac56..c2c14e33 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -42,7 +42,7 @@ class ActivityPubTest(testutil.TestCase): 'name': 'Mrs. ☕ Foo', 'preferredUsername': 'foo.com', 'id': 'http://localhost/foo.com', - 'url': 'https://foo.com/about-me', + 'url': 'http://localhost/r/https://foo.com/about-me', 'inbox': 'http://localhost/foo.com/inbox', 'publicKey': { 'publicKeyPem': MagicKey.get_by_id('foo.com').public_pem(), diff --git a/tests/test_webmention.py b/tests/test_webmention.py index decc7019..66679789 100644 --- a/tests/test_webmention.py +++ b/tests/test_webmention.py @@ -110,8 +110,8 @@ class WebmentionTest(testutil.TestCase): self.repost_as2 = { '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Announce', - 'id': 'http://a/repost', - 'url': 'http://a/repost', + 'id': 'http://localhost/r/http://a/repost', + 'url': 'http://localhost/r/http://a/repost', 'name': 'reposted!', 'object': 'tag:orig,2017:as2', 'cc': [ @@ -123,7 +123,7 @@ class WebmentionTest(testutil.TestCase): 'actor': { 'type': 'Person', 'id': 'http://localhost/orig', - 'url': 'http://orig', + 'url': 'http://localhost/r/http://orig', 'name': 'Ms. ☕ Baz', 'preferredUsername': 'orig', }, @@ -157,8 +157,8 @@ class WebmentionTest(testutil.TestCase): 'object': { '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Note', - 'id': 'http://a/reply', - 'url': 'http://a/reply', + 'id': 'http://localhost/r/http://a/reply', + 'url': 'http://localhost/r/http://a/reply', 'name': 'foo ☕ bar', 'content': 'foo ☕ bar\n', 'inReplyTo': 'tag:orig,2017:as2', @@ -171,7 +171,7 @@ class WebmentionTest(testutil.TestCase): 'attributedTo': [{ 'type': 'Person', 'id': 'http://localhost/orig', - 'url': 'http://orig', + 'url': 'http://localhost/r/http://orig', 'preferredUsername': 'orig', 'name': 'Ms. ☕ Baz', }],