From a9324adcebe9776c733f25ab89e0dc59579ff314 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 23 Nov 2018 22:17:37 -0800 Subject: [PATCH] webmention => AP: put images into AS2 'attachment' field for Mastodon for https://github.com/snarfed/bridgy-fed/issues/33#issuecomment-440965618 --- common.py | 8 +++++++- tests/test_webmention.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/common.py b/common.py index 23cdad4..0ca2f28 100644 --- a/common.py +++ b/common.py @@ -277,7 +277,7 @@ def postprocess_as2(activity, target=None, key=None): }) # activity objects (for Like, Announce, etc): prefer id over url - obj = activity.get('object', {}) + obj = activity.get('object') if obj: if isinstance(obj, dict) and not obj.get('id'): obj['id'] = target_id or obj.get('url') @@ -293,6 +293,12 @@ def postprocess_as2(activity, target=None, key=None): activity['id'] = redirect_wrap(activity['id']) activity['url'] = redirect_wrap(activity['url']) + # copy image(s) into attachment(s). may be Mastodon-specific. + # https://github.com/snarfed/bridgy-fed/issues/33#issuecomment-440965618 + obj_or_activity = obj if isinstance(obj, dict) else activity + obj_or_activity.setdefault('attachment', []).extend( + obj_or_activity.get('image', [])) + # cc public and target's author(s) and recipients # https://www.w3.org/TR/activitystreams-vocabulary/#audienceTargeting # https://w3c.github.io/activitypub/#delivery diff --git a/tests/test_webmention.py b/tests/test_webmention.py index a16ef5b..1583589 100644 --- a/tests/test_webmention.py +++ b/tests/test_webmention.py @@ -508,6 +508,35 @@ class WebmentionTest(testutil.TestCase): self.assertEqual('complete', resp.status, inbox) self.assertEqual(self.create_mf2, json.loads(resp.source_mf2), inbox) + def test_activitypub_create_with_image(self, mock_get, mock_post): + create_html = self.create_html.replace( + '', '\n') + mock_get.side_effect = [ + requests_response(create_html, content_type=CONTENT_TYPE_HTML), + self.actor, + ] + mock_post.return_value = requests_response('abc xyz ') + + Follower.get_or_create( + 'orig', 'https://mastodon/aaa', + last_follow=json.dumps({'actor': {'inbox': 'https://inbox'}})) + self.datastore_stub.Flush() + + got = app.get_response( + '/webmention', method='POST', body=urllib.urlencode({ + 'source': 'http://orig/post', + 'target': 'https://fed.brid.gy/', + })) + self.assertEquals(200, got.status_int) + + self.assertEquals(('https://inbox',), mock_post.call_args[0]) + create = copy.deepcopy(self.create_as2) + create['object'].update({ + 'image': [{'url': 'http://im/age', 'type': 'Image'}], + 'attachment': [{'url': 'http://im/age', 'type': 'Image'}], + }) + self.assertEquals(create, mock_post.call_args[1]['json']) + def test_activitypub_follow(self, mock_get, mock_post): mock_get.side_effect = [self.follow, self.actor] mock_post.return_value = requests_response('abc xyz')