From 33c6edac7f6cd011f2e6e1d7db6d132d59341ec5 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sun, 27 Dec 2020 22:59:21 +0200 Subject: [PATCH] Fix wrongly linkified image urls with markdown AP content When we get a Markdown source in AP, we prefer that over HTML. Our linkifying of urls however should not be run in this case, we want the markdown to be as it was sent. Fixes broken images between Socialhome instances. --- federation/entities/activitypub/entities.py | 4 ++++ .../tests/entities/activitypub/test_entities.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/federation/entities/activitypub/entities.py b/federation/entities/activitypub/entities.py index 5473fca..26957a4 100644 --- a/federation/entities/activitypub/entities.py +++ b/federation/entities/activitypub/entities.py @@ -67,6 +67,10 @@ class CleanContentMixin(RawContentMixin): return return attrs + if self._media_type == "text/markdown": + # Skip when markdown + return + self.raw_content = bleach.linkify( self.raw_content, callbacks=[remove_tag_links], diff --git a/federation/tests/entities/activitypub/test_entities.py b/federation/tests/entities/activitypub/test_entities.py index fe86b9d..a893ae9 100644 --- a/federation/tests/entities/activitypub/test_entities.py +++ b/federation/tests/entities/activitypub/test_entities.py @@ -481,6 +481,17 @@ class TestEntitiesPostReceive: "public": False, }] + @patch("federation.entities.activitypub.entities.bleach.linkify", autospec=True) + def test_post_post_receive__linkifies_if_not_markdown(self, mock_linkify, activitypubpost): + activitypubpost._media_type = 'text/html' + activitypubpost.post_receive() + mock_linkify.assert_called_once() + + @patch("federation.entities.activitypub.entities.bleach.linkify", autospec=True) + def test_post_post_receive__skips_linkify_if_markdown(self, mock_linkify, activitypubpost): + activitypubpost.post_receive() + mock_linkify.assert_not_called() + class TestEntitiesPreSend: def test_post_inline_images_are_attached(self, activitypubpost_embedded_images):