webmention => AP: put images into AS2 'attachment' field for Mastodon

for https://github.com/snarfed/bridgy-fed/issues/33#issuecomment-440965618
pull/43/head
Ryan Barrett 2018-11-23 22:17:37 -08:00
rodzic a52765821f
commit a9324adceb
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 36 dodań i 1 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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(
'</body>', '<img class="u-photo" src="http://im/age" />\n</body>')
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')