From 59b8f85c6369514d6a19c76d04de3177185adc83 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Sat, 24 Feb 2018 01:47:29 +0530 Subject: [PATCH] bugdown: Do only image preview if relative URL. --- zerver/lib/bugdown/__init__.py | 11 +++++++++-- zerver/tests/test_link_embed.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 81b2c09218..6ee41af85e 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -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"] diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.py index 72ce40015d..9c604eb54d 100644 --- a/zerver/tests/test_link_embed.py +++ b/zerver/tests/test_link_embed.py @@ -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 = '

http://test.org/

\n
Description text
' # Try case where the opengraph image is a relative url.