Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Don't linkify named topics in empty-topic-only channels.
Shubham Padia93129abbf26c
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 490359108f3..4bc63610a27 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -62,6 +62,7 @@ from zerver.lib.url_preview.types import UrlEmbedData, UrlOEmbedDatafrom zerver.models import Message, Realm, UserProfilefrom zerver.models.linkifiers import linkifiers_for_realmfrom zerver.models.realm_emoji import EmojiInfo, get_name_keyed_dict_for_active_realm_emoji+from zerver.models.streams import StreamTopicsPolicyEnumReturnT = TypeVar("ReturnT")@@ -1777,6 +1778,19 @@ class StreamTopicMessageProcessor(CompiledInlineProcessor):stream_id = db_data.stream_names.get(name)return stream_id+ def is_empty_topic_only_channel(self, name: str) -> bool:+ db_data: DbData | None = self.zmd.zulip_db_data+ if db_data is None:+ return False+ channel_info = db_data.mention_data.get_channel_info(name)+ if channel_info is None:+ return False+ topics_policy = channel_info.topics_policy+ if topics_policy == StreamTopicsPolicyEnum.inherit.value:+ assert self.zmd.zulip_realm is not None+ topics_policy = self.zmd.zulip_realm.topics_policy+ return topics_policy == StreamTopicsPolicyEnum.empty_topic_only.value+class StreamPattern(StreamTopicMessageProcessor):@override@@ -1821,6 +1835,10 @@ class StreamTopicPattern(StreamTopicMessageProcessor):stream_id = self.find_stream_id(stream_name)if stream_id is None or topic_name is None:return None, None, None+ if topic_name != "" and self.is_empty_topic_only_channel(stream_name):+ # This channel only allows the empty topic, so a non-empty+ # topic can never exist in it; don't linkify it.+ return None, None, Noneel = Element("a")el.set("class", "stream-topic")el.set("data-stream-id", str(stream_id))@@ -1861,6 +1879,10 @@ class StreamTopicMessagePattern(StreamTopicMessageProcessor):stream_id = self.find_stream_id(stream_name)if stream_id is None or topic_name is None:return None, None, None+ if topic_name != "" and self.is_empty_topic_only_channel(stream_name):+ # This channel only allows the empty topic, so a non-empty+ # topic can never exist in it; don't linkify it.+ return None, None, Noneel = Element("a")el.set("class", "message-link")stream_url = encode_channel(stream_id, stream_name)diff --git a/zerver/lib/mention.py b/zerver/lib/mention.pyindex 6c6c1b2b3ba..04be2a7d9cf 100644--- a/zerver/lib/mention.py+++ b/zerver/lib/mention.py@@ -90,6 +90,7 @@ class ChannelInfo:channel_id: intrecipient_id: inthistory_public_to_subscribers: bool+ topics_policy: int# TODO: Track whether the current user has only metadata access or# content access, so that we can allow mentioning channels with# only metadata access, while still enforcing content access to@@ -203,11 +204,15 @@ class MentionBackend:"name","recipient_id","history_public_to_subscribers",+ "topics_policy",))for row in rows:self.stream_cache[row["name"]] = ChannelInfo(- row["id"], row["recipient_id"], row["history_public_to_subscribers"]+ row["id"],+ row["recipient_id"],+ row["history_public_to_subscribers"],+ row["topics_policy"],)result[row["name"]] = row["id"]else:@@ -227,7 +232,10 @@ class MentionBackend:for stream in content_access_streams:assert stream.recipient_id is not Noneself.stream_cache[stream.name] = ChannelInfo(- stream.id, stream.recipient_id, stream.history_public_to_subscribers+ stream.id,+ stream.recipient_id,+ stream.history_public_to_subscribers,+ stream.topics_policy,)result[stream.name] = stream.id@@ -462,6 +470,9 @@ class MentionData:) -> dict[str, int]:return self.mention_backend.get_stream_name_map(stream_names, acting_user=acting_user)+ def get_channel_info(self, name: str) -> ChannelInfo | None:+ return self.mention_backend.stream_cache.get(name)+def get_topic_info_map(self, channel_topics: set[ChannelTopicInfo], acting_user: UserProfile | None) -> dict[ChannelTopicInfo, int | None]:diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 46ae8fcc599..8dd2274dc0b 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -77,7 +77,7 @@ from zerver.models.clients import get_clientfrom zerver.models.groups import SystemGroupsfrom zerver.models.linkifiers import linkifiers_for_realmfrom zerver.models.realms import get_realm-from zerver.models.streams import get_stream+from zerver.models.streams import StreamTopicsPolicyEnum, get_streamfrom zerver.models.users import get_system_bot@@ -3467,6 +3467,41 @@ class MarkdownStreamTopicMentionTests(ZulipTestCase):f'<p><a class="stream-topic" data-stream-id="{denmark.id}" href="/#narrow/channel/{denmark.id}-Denmark/topic/">#{denmark.name} > <em>{Message.EMPTY_TOPIC_FALLBACK_NAME}</em></a></p>',)+ def test_topic_link_in_empty_topic_only_channel(self) -> None:+ denmark = get_stream("Denmark", get_realm("zulip"))+ denmark.topics_policy = StreamTopicsPolicyEnum.empty_topic_only.value+ denmark.save(update_fields=["topics_policy"])+ sender_user_profile = self.example_user("othello")+ msg = Message(+ sender=sender_user_profile,+ sending_client=get_client("test"),+ realm=sender_user_profile.realm,+ )+ # A non-empty topic can never exist in this channel, so its link+ # is not formed (the leftover `**` then renders as bold).+ content = "#**Denmark>some topic**"+ self.assertEqual(+ render_message_markdown(msg, content).rendered_content,+ "<p>#<strong>Denmark>some topic</strong></p>",+ )+ # The message permalink variant is likewise not linkified.+ content = "#**Denmark>some topic@12**"+ self.assertEqual(+ render_message_markdown(msg, content).rendered_content,+ "<p>#<strong>Denmark>some topic@12</strong></p>",+ )+ # The channel link and the empty-topic link are still valid.+ content = "#**Denmark**"+ self.assertEqual(+ render_message_markdown(msg, content).rendered_content,+ f'<p><a class="stream" data-stream-id="{denmark.id}" href="/#narrow/channel/{denmark.id}-Denmark">#{denmark.name}</a></p>',+ )+ content = "#**Denmark>**"+ self.assertEqual(+ render_message_markdown(msg, content).rendered_content,+ f'<p><a class="stream-topic" data-stream-id="{denmark.id}" href="/#narrow/channel/{denmark.id}-Denmark/topic/">#{denmark.name} > <em>{Message.EMPTY_TOPIC_FALLBACK_NAME}</em></a></p>',+ )+def test_topic_single_containing_messages(self) -> None:denmark = get_stream("Denmark", get_realm("zulip"))sender_user_profile = self.example_user("othello")