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.
This commit is contained in:
Priyansh Garg 2021-08-10 23:34:16 +05:30 committed by Tim Abbott
parent 5e5166d872
commit 1e51c23494
1 changed files with 33 additions and 37 deletions

View File

@ -1475,7 +1475,7 @@ class Emoji(markdown.inlinepatterns.Pattern):
if db_data is not None: if db_data is not None:
active_realm_emoji = db_data["active_realm_emoji"] 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) return make_realm_emoji(active_realm_emoji[name]["source_url"], orig_syntax)
elif name == "zulip": elif name == "zulip":
return make_realm_emoji( return make_realm_emoji(
@ -1783,7 +1783,7 @@ class UserMentionPattern(CompiledInlineProcessor):
name = m.group("match") name = m.group("match")
silent = m.group("silent") == "_" silent = m.group("silent") == "_"
db_data = self.md.zulip_db_data 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) wildcard = mention.user_mention_matches_wildcard(name)
# For @**|id** and @**name|id** mention syntaxes. # For @**|id** and @**name|id** mention syntaxes.
@ -1838,7 +1838,7 @@ class UserGroupMentionPattern(CompiledInlineProcessor):
silent = m.group("silent") == "_" silent = m.group("silent") == "_"
db_data = self.md.zulip_db_data 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) user_group = db_data["mention_data"].get_user_group(name)
if user_group: if user_group:
if not silent: if not silent:
@ -1876,24 +1876,22 @@ class StreamPattern(CompiledInlineProcessor):
) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]: ) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:
name = m.group("stream_name") name = m.group("stream_name")
if self.md.zulip_message: stream = self.find_stream_by_name(name)
stream = self.find_stream_by_name(name) if stream is None:
if stream is None: return None, None, None
return None, None, None el = Element("a")
el = Element("a") el.set("class", "stream")
el.set("class", "stream") el.set("data-stream-id", str(stream["id"]))
el.set("data-stream-id", str(stream["id"])) # TODO: We should quite possibly not be specifying the
# TODO: We should quite possibly not be specifying the # href here and instead having the browser auto-add the
# href here and instead having the browser auto-add the # href when it processes a message with one of these, to
# href when it processes a message with one of these, to # provide more clarity to API clients.
# provide more clarity to API clients. # Also do the same for StreamTopicPattern.
# Also do the same for StreamTopicPattern. stream_url = encode_stream(stream["id"], name)
stream_url = encode_stream(stream["id"], name) el.set("href", f"/#narrow/stream/{stream_url}")
el.set("href", f"/#narrow/stream/{stream_url}") text = f"#{name}"
text = f"#{name}" el.text = markdown.util.AtomicString(text)
el.text = markdown.util.AtomicString(text) return el, m.start(), m.end()
return el, m.start(), m.end()
return None, None, None
class StreamTopicPattern(CompiledInlineProcessor): class StreamTopicPattern(CompiledInlineProcessor):
@ -1910,21 +1908,19 @@ class StreamTopicPattern(CompiledInlineProcessor):
stream_name = m.group("stream_name") stream_name = m.group("stream_name")
topic_name = m.group("topic_name") topic_name = m.group("topic_name")
if self.md.zulip_message: stream = self.find_stream_by_name(stream_name)
stream = self.find_stream_by_name(stream_name) if stream is None or topic_name is None:
if stream is None or topic_name is None: return None, None, None
return None, None, None el = Element("a")
el = Element("a") el.set("class", "stream-topic")
el.set("class", "stream-topic") el.set("data-stream-id", str(stream["id"]))
el.set("data-stream-id", str(stream["id"])) stream_url = encode_stream(stream["id"], stream_name)
stream_url = encode_stream(stream["id"], stream_name) topic_url = hash_util_encode(topic_name)
topic_url = hash_util_encode(topic_name) link = f"/#narrow/stream/{stream_url}/topic/{topic_url}"
link = f"/#narrow/stream/{stream_url}/topic/{topic_url}" el.set("href", link)
el.set("href", link) text = f"#{stream_name} > {topic_name}"
text = f"#{stream_name} > {topic_name}" el.text = markdown.util.AtomicString(text)
el.text = markdown.util.AtomicString(text) return el, m.start(), m.end()
return el, m.start(), m.end()
return None, None, None
def possible_linked_stream_names(content: str) -> Set[str]: 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]: def run(self, lines: List[str]) -> List[str]:
db_data = self.md.zulip_db_data 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 # We check for alert words here, the set of which are
# dependent on which users may see this message. # dependent on which users may see this message.
# #