bugdown: Do only image preview if relative URL.

This commit is contained in:
Vishnu Ks
2018-02-24 01:47:29 +05:30
committed by Tim Abbott
parent 553634536c
commit 59b8f85c63
2 changed files with 20 additions and 2 deletions

View File

@@ -823,10 +823,12 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
if uncle_link not in parent_links:
return insertion_index
def is_absolute_url(self, url: Text) -> bool:
return bool(urllib.parse.urlparse(url).netloc)
def run(self, root: Element) -> None:
# Get all URLs from the blob
found_urls = walk_tree_with_family(root, self.get_url_data)
if len(found_urls) == 0 or len(found_urls) > self.INLINE_PREVIEW_LIMIT_PER_MESSAGE:
return
@@ -834,8 +836,13 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
for found_url in found_urls:
(url, text) = found_url.result
dropbox_image = self.dropbox_image(url)
if not self.is_absolute_url(url):
if self.is_image(url):
self.handle_image_inlining(root, found_url)
# We don't have a strong use case for doing url preview for relative links.
continue
dropbox_image = self.dropbox_image(url)
if dropbox_image is not None:
class_attr = "message_inline_ref"
is_image = dropbox_image["is_image"]

View File

@@ -313,6 +313,17 @@ class PreviewTestCase(ZulipTestCase):
msg = self._send_message_with_test_org_url(sender_email=self.example_email('prospero'), queue_should_run=False)
self.assertEqual(msg.rendered_content, without_preview)
@override_settings(INLINE_URL_EMBED_PREVIEW=True)
def test_inline_relative_url_embed_preview(self) -> None:
# Relative urls should not be sent for url preview.
with mock.patch('zerver.lib.actions.queue_json_publish') as patched:
self.send_personal_message(
self.example_email('prospero'),
self.example_email('cordelia'),
content="http://zulip.testserver/api/",
)
patched.assert_not_called()
def test_inline_url_embed_preview_with_relative_image_url(self) -> None:
with_preview_relative = '<p><a href="http://test.org/" target="_blank" title="http://test.org/">http://test.org/</a></p>\n<div class="message_embed"><a class="message_embed_image" href="http://test.org/" style="background-image: url(http://test.org/images/rock.jpg)" target="_blank"></a><div class="data-container"><div class="message_embed_title"><a href="http://test.org/" target="_blank" title="The Rock">The Rock</a></div><div class="message_embed_description">Description text</div></div></div>'
# Try case where the opengraph image is a relative url.