notifications: Correctly convert relative narrow links to absolute URLs.

This commit is contained in:
Harshit Bansal
2017-10-11 20:41:19 +00:00
parent b0235bc458
commit 8d42f42ef2
2 changed files with 28 additions and 3 deletions

View File

@@ -71,9 +71,26 @@ def topic_narrow_url(realm, stream, topic):
def relative_to_full_url(base_url, content):
# type: (Text, Text) -> Text
# Convert relative URLs to absolute URLs.
elem = lxml.html.fromstring(content) # type: ignore # https://github.com/python/typeshed/issues/525
elem.make_links_absolute(base_url)
content = lxml.html.tostring(elem).decode("utf-8") # type: ignore # https://github.com/python/typeshed/issues/525
fragment = lxml.html.fromstring(content) # type: ignore # https://github.com/python/typeshed/issues/525
# We handle narrow URLs separately because of two reasons:
# 1: 'lxml' seems to be having an issue in dealing with URLs that begin
# `#` due to which it doesn't add a `/` before joining the base_url to
# the relative URL.
# 2: We also need to update the title attribute in the narrow links which
# is not possible with `make_links_absolute()`.
for link_info in fragment.iterlinks():
elem, attrib, link, pos = link_info
match = re.match("/?#narrow/", link)
if match is not None:
link = re.sub(r"^/?#narrow/", base_url + "/#narrow/", link)
elem.set(attrib, link)
# Only manually linked narrow URLs have title attribute set.
if elem.get('title') is not None:
elem.set('title', link)
fragment.make_links_absolute(base_url)
content = lxml.html.tostring(fragment).decode("utf-8") # type: ignore # https://github.com/python/typeshed/issues/525
# Inline images can't be displayed in the emails as the request
# from the mail server can't be authenticated because it has no

View File

@@ -420,6 +420,14 @@ class TestMissedMessages(ZulipTestCase):
expected_output = '<p>Set src="/avatar/username@example.com?s=30"</p>'
self.assertEqual(actual_output, expected_output)
# A narrow URL which begins with a '#'.
test_data = '<p><a href="#narrow/stream/test/subject/test.20topic/near/142"' + \
'title="#narrow/stream/test/subject/test.20topic/near/142">Conversation</a></p>'
actual_output = relative_to_full_url("http://example.com", test_data)
expected_output = '<p><a href="http://example.com/#narrow/stream/test/subject/test.20topic/near/142" ' + \
'title="http://example.com/#narrow/stream/test/subject/test.20topic/near/142">Conversation</a></p>'
self.assertEqual(actual_output, expected_output)
def test_fix_emoji(self):
# type: () -> None
# An emoji.