kopia lustrzana https://github.com/snarfed/bridgy-fed
AP: convert Link attachments to HTML links in content
...since some fediverse platforms (notably Mastodon) will render them as blank "Preview not available" embeds, and support on other platforms is unreliable. Most reliably generate preview embeds for HTML links in content though. fixes #958pull/1141/head
rodzic
349f4ba544
commit
980a786c6b
|
@ -735,12 +735,21 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
|
|||
# https://chrisbeckstrom.com/2018/12/27/32551/
|
||||
# assert activity.get('id') or (isinstance(obj, dict) and obj.get('id'))
|
||||
|
||||
# drop Link attachments since fediverse instances generate their own link previews
|
||||
# https://github.com/snarfed/bridgy-fed/issues/958
|
||||
obj_or_activity = obj if obj.keys() > set(['id']) else activity
|
||||
obj_or_activity['attachment'] = [
|
||||
a for a in as1.get_objects(obj_or_activity, 'attachment')
|
||||
if a.get('type') != 'Link']
|
||||
|
||||
# move Link attachments to links in text since fediverse instances generate
|
||||
# their own link previews.
|
||||
# https://github.com/snarfed/bridgy-fed/issues/958
|
||||
atts = util.pop_list(obj_or_activity, 'attachment')
|
||||
obj_or_activity['attachment'] = [a for a in atts if a.get('type') != 'Link']
|
||||
link_atts = [a for a in atts if a.get('type') == 'Link']
|
||||
|
||||
for link in link_atts:
|
||||
if url := link.get('url'):
|
||||
name = link.get('displayName')
|
||||
if obj_or_activity.setdefault('content', ''):
|
||||
obj_or_activity['content'] += '<br><br>'
|
||||
obj_or_activity['content'] += util.pretty_link(url, text=name)
|
||||
|
||||
# copy image(s) into attachment(s). may be Mastodon-specific.
|
||||
# https://github.com/snarfed/bridgy-fed/issues/33#issuecomment-440965618
|
||||
|
|
|
@ -2146,14 +2146,44 @@ class ActivityPubUtilsTest(TestCase):
|
|||
}],
|
||||
}))
|
||||
|
||||
def test_postprocess_as2_strips_link_attachment(self):
|
||||
self.assertNotIn('attachment', postprocess_as2({
|
||||
def test_postprocess_as2_moves_link_attachments_to_content(self):
|
||||
# https://github.com/snarfed/bridgy-fed/issues/958
|
||||
self.assert_equals({
|
||||
'type': 'Note',
|
||||
'content': '<p><a href="http://a/link">check it out</a><br><br><a href="http://another/link">another/link</a></p>',
|
||||
'contentMap': {
|
||||
'en': '<p><a href="http://a/link">check it out</a><br><br><a href="http://another/link">another/link</a></p>',
|
||||
},
|
||||
'content_is_html': True,
|
||||
}, postprocess_as2({
|
||||
'type': 'Note',
|
||||
'attachment': [{
|
||||
'type': 'Link',
|
||||
'url': 'http://a/link',
|
||||
'displayName': 'check it out',
|
||||
}, {
|
||||
'type': 'Link',
|
||||
'url': 'http://another/link',
|
||||
}],
|
||||
}))
|
||||
}), ignore=['to'])
|
||||
|
||||
def test_postprocess_as2_appends_link_attachments_to_content(self):
|
||||
# https://github.com/snarfed/bridgy-fed/issues/958
|
||||
self.assert_equals({
|
||||
'type': 'Note',
|
||||
'content': '<p>original<br><br><a href="http://a/link">a/link</a></p>',
|
||||
'contentMap': {
|
||||
'en': '<p>original<br><br><a href="http://a/link">a/link</a></p>',
|
||||
},
|
||||
'content_is_html': True,
|
||||
}, postprocess_as2({
|
||||
'type': 'Note',
|
||||
'content': 'original',
|
||||
'attachment': [{
|
||||
'type': 'Link',
|
||||
'url': 'http://a/link',
|
||||
}],
|
||||
}), ignore=['to'])
|
||||
|
||||
def test_postprocess_as2_actor_url_attachments(self):
|
||||
got = postprocess_as2_actor(as2.from_as1({
|
||||
|
|
Ładowanie…
Reference in New Issue