Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email_notifications: Use stable, unhashed, URLs for emoji in emails.
Alex Vandiver6806bec2e9fc
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex cf09144ff3f..3c3c8a4eea1 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -99,7 +99,7 @@ def relative_to_full_url(fragment: lxml.html.HtmlElement, base_url: str) -> Nonefragment.make_links_absolute(base_url)-def fix_emojis(fragment: lxml.html.HtmlElement, base_url: str, emojiset: str) -> None:+def fix_emojis(fragment: lxml.html.HtmlElement, emojiset: str) -> None:def make_emoji_img_elem(emoji_span_elem: lxml.html.HtmlElement) -> Dict[str, Any]:# Convert the emoji spans to img tags.classes = emoji_span_elem.get("class")@@ -111,7 +111,14 @@ def fix_emojis(fragment: lxml.html.HtmlElement, base_url: str, emojiset: str) ->emoji_code = match.group("emoji_code")emoji_name = emoji_span_elem.get("title")alt_code = emoji_span_elem.text- image_url = base_url + f"/static/generated/emoji/images-{emojiset}-64/{emoji_code}.png"+ # We intentionally do not use staticfiles_storage.url here, so+ # that we don't get any hashed version -- we want a path which+ # may give us content which changes over time, but one which+ # is guaranteed to keep working even if the prod-static+ # directory is cleaned out (or a new server is rotated in+ # which does not have historical content with old hashed+ # filenames).+ image_url = f"{settings.STATIC_URL}generated/emoji/images-{emojiset}-64/{emoji_code}.png"img_elem = e.IMG(alt=alt_code, src=image_url, title=emoji_name, style="height: 20px;")img_elem.tail = emoji_span_elem.tailreturn img_elem@@ -238,7 +245,7 @@ def build_message_list(assert message.rendered_content is not Nonefragment = lxml.html.fragment_fromstring(message.rendered_content, create_parent=True)relative_to_full_url(fragment, user.realm.uri)- fix_emojis(fragment, user.realm.uri, user.emojiset)+ fix_emojis(fragment, user.emojiset)fix_spoilers_in_html(fragment, user.default_language)html = lxml.html.tostring(fragment, encoding="unicode")if sender:diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.pyindex 4945c8c2456..ec84c15a628 100644--- a/zerver/tests/test_email_notifications.py+++ b/zerver/tests/test_email_notifications.py@@ -1204,7 +1204,7 @@ class TestMissedMessages(ZulipTestCase):"Extremely personal message with a hamburger :hamburger:!",)verify_body_include = [- '<img alt=":hamburger:" src="http://zulip.testserver/static/generated/emoji/images-twitter-64/1f354.png" title="hamburger" style="height: 20px;">'+ '<img alt=":hamburger:" src="http://testserver/static/generated/emoji/images-twitter-64/1f354.png" title="hamburger" style="height: 20px;">']email_subject = "PMs with Othello, the Moor of Venice"self._test_cases(@@ -1570,11 +1570,11 @@ class TestMissedMessages(ZulipTestCase):' rain">:cloud_with_lightning_and_rain:</span>.</p>')fragment = lxml.html.fromstring(test_data)- fix_emojis(fragment, "http://example.com", "google")+ fix_emojis(fragment, "google")actual_output = lxml.html.tostring(fragment, encoding="unicode")expected_output = ('<p>See <img alt=":cloud_with_lightning_and_rain:"'- ' src="http://example.com/static/generated/emoji/images-google-64/26c8.png"'+ ' src="http://testserver/static/generated/emoji/images-google-64/26c8.png"'' title="cloud with lightning and rain" style="height: 20px;">.</p>')self.assertEqual(actual_output, expected_output)