diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index 2e26411328..e4a62f5eac 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -90,6 +90,18 @@ def relative_to_full_url(base_url: Text, content: Text) -> Text: for container in inline_image_containers: container.drop_tree() + # The previous block handles most inline images, but for messages + # where the entire markdown input was just the URL of an image + # (i.e. the entire body is a message_inline_image object), the + # entire message body will be that image element; here, we need a + # more drastic edit to the content. + if fragment.get('class') == 'message_inline_image': + content_template = '

%s

' + image_link = fragment.find('a').get('href') + image_title = fragment.find('a').get('title') + new_content = (content_template % (image_link, image_title, image_link)) + fragment = lxml.html.fromstring(new_content) + fragment.make_links_absolute(base_url) content = lxml.html.tostring(fragment).decode("utf-8") diff --git a/zerver/tests/test_notifications.py b/zerver/tests/test_notifications.py index dede8a0e44..537c3ff997 100644 --- a/zerver/tests/test_notifications.py +++ b/zerver/tests/test_notifications.py @@ -448,6 +448,18 @@ class TestMissedMessages(ZulipTestCase): 'title="avatar_103.jpeg">avatar_103.jpeg.

' self.assertEqual(actual_output, expected_output) + # A message containing only an inline image URL preview, we do + # somewhat more extensive surgery. + test_data = '
' + \ + '
' + actual_output = relative_to_full_url("http://example.com", test_data) + expected_output = '

' + \ + 'https://www.google.com/images/srpr/logo4w.png

' + self.assertEqual(actual_output, expected_output) + def test_fix_emoji(self) -> None: # An emoji. test_data = '

See ' + \