From b32045d49460021ace355c9aa729684b452037fa Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 20 Oct 2023 13:37:38 -0700 Subject: [PATCH] postprocess_as2: duplicate content into contentMap.en for #681. this suppresses Mastodon's Translate link. longer term "right" fix is to actually detect the source's language, if available, and use it instead. --- activitypub.py | 5 +++++ tests/test_activitypub.py | 3 +++ tests/test_convert.py | 18 +++++++----------- tests/test_web.py | 13 +++++++++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/activitypub.py b/activitypub.py index 2fcdccb..08c77a3 100644 --- a/activitypub.py +++ b/activitypub.py @@ -666,6 +666,11 @@ def postprocess_as2(activity, orig_obj=None, wrap=True): if not name.startswith('#'): tag['name'] = f'#{name}' + # language, in contentMap + # https://github.com/snarfed/bridgy-fed/issues/681 + if content := obj_or_activity.get('content'): + obj_or_activity.setdefault('contentMap', {'en': content}) + activity['object'] = postprocess_as2( activity.get('object'), orig_obj=orig_obj, diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index c09719d..337b494 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -1666,10 +1666,13 @@ class ActivityPubUtilsTest(TestCase): self.assert_equals({ 'id': 'http://localhost/r/xyz', 'type': 'Note', + 'content': 'foo', + 'contentMap': {'en': 'foo'}, 'to': [as2.PUBLIC_AUDIENCE], }, postprocess_as2({ 'id': 'xyz', 'type': 'Note', + 'content': 'foo', })) def test_postprocess_as2_hashtag(self): diff --git a/tests/test_convert.py b/tests/test_convert.py index 8a8b51c..3ded060 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -15,17 +15,13 @@ from . import testutil from common import CONTENT_TYPE_HTML COMMENT_AS2 = { - **as2.to_as1(COMMENT), + **as2.from_as1(COMMENT), 'type': 'Note', - 'id': 'https://fed.brid.gy/r/tag:fake.com:123456', - 'url': 'https://fed.brid.gy/r/https://fake.com/123456', - 'name': 'A ☕ reply', - 'inReplyTo': 'https://fake.com/123', -} -COMMENT_AS2_WEB = { - **COMMENT_AS2, 'id': 'https://web.brid.gy/r/tag:fake.com:123456', 'url': 'https://web.brid.gy/r/https://fake.com/123456', + 'name': 'A ☕ reply', + 'contentMap': {'en': COMMENT['content']}, + 'inReplyTo': 'https://fake.com/123', } HTML = """\ @@ -254,7 +250,7 @@ A ☕ reply 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_WEB, resp.json, ignore=['to']) + self.assert_equals(COMMENT_AS2, resp.json, ignore=['to']) @patch('requests.get') def test_web_to_activitypub_fetch(self, mock_get): @@ -267,7 +263,7 @@ A ☕ reply 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_WEB, resp.json, ignore=['to']) + self.assert_equals(COMMENT_AS2, resp.json, ignore=['to']) def test_web_to_activitypub_no_user(self): resp = self.client.get(f'/convert/ap/http://nope.com/post', @@ -282,7 +278,7 @@ A ☕ reply resp = self.client.get(f'/convert/ap/http://user.com/a%23b', base_url='https://web.brid.gy/') self.assertEqual(200, resp.status_code) - self.assert_equals(COMMENT_AS2_WEB, resp.json, ignore=['to']) + self.assert_equals(COMMENT_AS2, resp.json, ignore=['to']) def test_fed_subdomain(self): url = 'https://user.com/post' diff --git a/tests/test_web.py b/tests/test_web.py index a88045b..278d268 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -243,6 +243,12 @@ AS2_CREATE = { foo ☕ bar """, + 'contentMap': { + 'en': """\ + +foo ☕ bar +""", + }, 'inReplyTo': 'https://mas.to/toot/id', 'to': [as2.PUBLIC_AUDIENCE], 'cc': [ @@ -347,6 +353,7 @@ NOTE_AS2 = { 'attributedTo': 'http://localhost/user.com', 'name': 'hello i am a post', 'content': 'hello i am a post', + 'contentMap': {'en': 'hello i am a post'}, 'to': [as2.PUBLIC_AUDIENCE], } CREATE_AS1 = { @@ -968,7 +975,7 @@ class WebTest(TestCase): self.assertEqual(200, got.status_code) inboxes = ['https://inbox/', 'https://public/inbox', 'https://shared/inbox'] - self.assert_deliveries(mock_post, inboxes, { + expected = { **NOTE_AS2, 'attributedTo': None, 'type': 'Create', @@ -979,7 +986,9 @@ class WebTest(TestCase): 'targetUrl': 'http://bob.com/post', 'to': ['https://www.w3.org/ns/activitystreams#Public'], }, - }) + } + del expected['contentMap'] + self.assert_deliveries(mock_post, inboxes, expected) def test_create_default_url_to_wm_source(self, mock_get, mock_post): """Source post has no u-url. AS2 id should default to webmention source."""