Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

markdown: Update max inline preview from 10 to 24.

Trident Pancakec6ea673cc940
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index 38bfe0503ed..86fda7101ec 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -628,7 +628,7 @@ IMAGE_EXTENSIONS = [".bmp", ".gif", ".jpe", ".jpeg", ".jpg", ".png", ".webp"]
class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
TWITTER_MAX_IMAGE_HEIGHT = 400
TWITTER_MAX_TO_PREVIEW = 3
- INLINE_PREVIEW_LIMIT_PER_MESSAGE = 10
+ INLINE_PREVIEW_LIMIT_PER_MESSAGE = 24
def __init__(self, zmd: "ZulipMarkdown") -> None:
super().__init__(zmd)
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index 5903ea5e1cb..912aa5dde01 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -7,6 +7,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple, cast
from unittest import mock
import orjson
+from bs4 import BeautifulSoup
from django.conf import settings
from django.test import override_settings
from markdown import Markdown
@@ -23,6 +24,7 @@ from zerver.lib.create_user import create_user
from zerver.lib.emoji import get_emoji_url
from zerver.lib.exceptions import JsonableError, MarkdownRenderingError
from zerver.lib.markdown import (
+ InlineInterestingLinkProcessor,
MarkdownListPreprocessor,
MessageRenderingResult,
clear_state_for_testing,
@@ -661,6 +663,38 @@ class MarkdownTest(ZulipTestCase):
converted = markdown_convert_wrapper(content)
self.assertIn(converted, thumbnail_img)
+ @override_settings(INLINE_IMAGE_PREVIEW=True)
+ def test_max_inline_preview(self) -> None:
+ image_links = []
+ # Add a youtube link within a spoiler to ensure other link types are counted
+ image_links.append(
+ """```spoiler Check out this PyCon video\nhttps://www.youtube.com/watch?v=0c46YHS3RY8\n```"""
+ )
+ # Add a link within blockquote to test that it does NOT get counted
+ image_links.append("> http://cdn.wallpapersafari.com/spoiler/dont_count.jpeg\n")
+ # Using INLINE_PREVIEW_LIMIT_PER_MESSAGE - 1 because of the one link in a spoiler added already
+ for x in range(InlineInterestingLinkProcessor.INLINE_PREVIEW_LIMIT_PER_MESSAGE - 1):
+ image_links.append("http://cdn.wallpapersafari.com/{}/6/16eVjx.jpeg".format(x))
+ within_limit_content = "\n".join(image_links)
+ above_limit_content = (
+ within_limit_content + "\nhttp://cdn.wallpapersafari.com/above/0/6/16eVjx.jpeg"
+ )
+
+ # When the number of image links is within the preview limit, the
+ # output should contain the same number of inline images.
+ converted = markdown_convert_wrapper(within_limit_content)
+ soup = BeautifulSoup(converted, "html.parser")
+ self.assert_length(
+ soup(class_="message_inline_image"),
+ InlineInterestingLinkProcessor.INLINE_PREVIEW_LIMIT_PER_MESSAGE,
+ )
+
+ # When the number of image links is over the limit, then there should
+ # be zero inline images.
+ converted = markdown_convert_wrapper(above_limit_content)
+ soup = BeautifulSoup(converted, "html.parser")
+ self.assert_length(soup(class_="message_inline_image"), 0)
+
@override_settings(INLINE_IMAGE_PREVIEW=True)
def test_inline_image_quoted_blocks(self) -> None:
content = "http://cdn.wallpapersafari.com/13/6/16eVjx.jpeg"