From 1e51c234941ecefcdc60ce6f83bc1704b24df525 Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Tue, 10 Aug 2021 23:34:16 +0530 Subject: [PATCH] markdown: Remove unnecessary checks for zulip_message. This commits removes some unnecessary checks for `self.md.zulip_message`, which were put there historically, as earlier we used to add the additional properties like mentions_user_ids, alert_words, etc. to Message dict only. These were later moved to MessageRenderingResult class in commit 75cea329b but the checks weren't removed. This is important because while rendering the messages imported from other chat tools (like Rocket.Chat), the Message dict is not passed to the markdown, due to which the checks for `self.md.zerver_message` fails and hence, things like user mentions, stream/topic mentions are not rendered in the imported messages properly. --- zerver/lib/markdown/__init__.py | 70 ++++++++++++++++----------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 27bd9757b3..a45df4b8ed 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -1475,7 +1475,7 @@ class Emoji(markdown.inlinepatterns.Pattern): if db_data is not None: active_realm_emoji = db_data["active_realm_emoji"] - if self.md.zulip_message and name in active_realm_emoji: + if name in active_realm_emoji: return make_realm_emoji(active_realm_emoji[name]["source_url"], orig_syntax) elif name == "zulip": return make_realm_emoji( @@ -1783,7 +1783,7 @@ class UserMentionPattern(CompiledInlineProcessor): name = m.group("match") silent = m.group("silent") == "_" db_data = self.md.zulip_db_data - if self.md.zulip_message and db_data is not None: + if db_data is not None: wildcard = mention.user_mention_matches_wildcard(name) # For @**|id** and @**name|id** mention syntaxes. @@ -1838,7 +1838,7 @@ class UserGroupMentionPattern(CompiledInlineProcessor): silent = m.group("silent") == "_" db_data = self.md.zulip_db_data - if self.md.zulip_message and db_data is not None: + if db_data is not None: user_group = db_data["mention_data"].get_user_group(name) if user_group: if not silent: @@ -1876,24 +1876,22 @@ class StreamPattern(CompiledInlineProcessor): ) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]: name = m.group("stream_name") - if self.md.zulip_message: - stream = self.find_stream_by_name(name) - if stream is None: - return None, None, None - el = Element("a") - el.set("class", "stream") - el.set("data-stream-id", str(stream["id"])) - # TODO: We should quite possibly not be specifying the - # href here and instead having the browser auto-add the - # href when it processes a message with one of these, to - # provide more clarity to API clients. - # Also do the same for StreamTopicPattern. - stream_url = encode_stream(stream["id"], name) - el.set("href", f"/#narrow/stream/{stream_url}") - text = f"#{name}" - el.text = markdown.util.AtomicString(text) - return el, m.start(), m.end() - return None, None, None + stream = self.find_stream_by_name(name) + if stream is None: + return None, None, None + el = Element("a") + el.set("class", "stream") + el.set("data-stream-id", str(stream["id"])) + # TODO: We should quite possibly not be specifying the + # href here and instead having the browser auto-add the + # href when it processes a message with one of these, to + # provide more clarity to API clients. + # Also do the same for StreamTopicPattern. + stream_url = encode_stream(stream["id"], name) + el.set("href", f"/#narrow/stream/{stream_url}") + text = f"#{name}" + el.text = markdown.util.AtomicString(text) + return el, m.start(), m.end() class StreamTopicPattern(CompiledInlineProcessor): @@ -1910,21 +1908,19 @@ class StreamTopicPattern(CompiledInlineProcessor): stream_name = m.group("stream_name") topic_name = m.group("topic_name") - if self.md.zulip_message: - stream = self.find_stream_by_name(stream_name) - if stream is None or topic_name is None: - return None, None, None - el = Element("a") - el.set("class", "stream-topic") - el.set("data-stream-id", str(stream["id"])) - stream_url = encode_stream(stream["id"], stream_name) - topic_url = hash_util_encode(topic_name) - link = f"/#narrow/stream/{stream_url}/topic/{topic_url}" - el.set("href", link) - text = f"#{stream_name} > {topic_name}" - el.text = markdown.util.AtomicString(text) - return el, m.start(), m.end() - return None, None, None + stream = self.find_stream_by_name(stream_name) + if stream is None or topic_name is None: + return None, None, None + el = Element("a") + el.set("class", "stream-topic") + el.set("data-stream-id", str(stream["id"])) + stream_url = encode_stream(stream["id"], stream_name) + topic_url = hash_util_encode(topic_name) + link = f"/#narrow/stream/{stream_url}/topic/{topic_url}" + el.set("href", link) + text = f"#{stream_name} > {topic_name}" + el.text = markdown.util.AtomicString(text) + return el, m.start(), m.end() def possible_linked_stream_names(content: str) -> Set[str]: @@ -1966,7 +1962,7 @@ class AlertWordNotificationProcessor(markdown.preprocessors.Preprocessor): def run(self, lines: List[str]) -> List[str]: db_data = self.md.zulip_db_data - if self.md.zulip_message and db_data is not None: + if db_data is not None: # We check for alert words here, the set of which are # dependent on which users may see this message. #