From ecbb763e795245d4b60b08e2b63dfdddbd618144 Mon Sep 17 00:00:00 2001
From: Aditya Bansal
Date: Wed, 24 Jan 2018 23:53:51 +0530
Subject: [PATCH] notifications: Handle an edge case in relative_to_full_url.
For messages where the entire rendered body is a message_inline_image
object, we actually don't display any text and just display the
image. These messages may have links to images which might or might
not be internal to Zulip but in both cases there is a chance of this
links being broken when accessed by an email server like Gmail that
doesn't possess the recipient user's cookies.
We don't want to have ugly looking broken images displayed in email
notifications. So we patch this by inserting a replacement for the
`message_inline_image` block in which we essentially replace the
content with the textual link.
Edited for clarity by tabbott.
---
zerver/lib/notifications.py | 12 ++++++++++++
zerver/tests/test_notifications.py | 12 ++++++++++++
2 files changed, 24 insertions(+)
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 ' + \