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.

topic_link_util: Add module to generate working topic links.

Kislay Vermaff27c568c596
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
index cb63882c6eb..4502ba74cc3 100644
--- a/web/src/topic_link_util.ts
+++ b/web/src/topic_link_util.ts
@@ -1,3 +1,5 @@
+// Keep this synchronized with zerver/lib/topic_link_util.py
+
import assert from "minimalistic-assert";
import * as hash_util from "./hash_util.ts";
diff --git a/zerver/lib/topic_link_util.py b/zerver/lib/topic_link_util.py
new file mode 100644
index 00000000000..2c110fc7429
--- /dev/null
+++ b/zerver/lib/topic_link_util.py
@@ -0,0 +1,81 @@
+# Keep this synchronized with web/src/topic_link_util.ts
+
+import re
+import urllib.parse
+
+invalid_stream_topic_regex = re.compile(r"[`>*&\[\]]|(\$\$)")
+
+
+def will_produce_broken_stream_topic_link(word: str) -> bool:
+ return bool(invalid_stream_topic_regex.search(word))
+
+
+escape_mapping = {
+ "`": "`",
+ ">": ">",
+ "*": "*",
+ "&": "&",
+ "$$": "$$",
+ "[": "[",
+ "]": "]",
+}
+
+
+def escape_invalid_stream_topic_characters(text: str) -> str:
+ return re.sub(
+ invalid_stream_topic_regex,
+ lambda match: escape_mapping.get(match.group(0), match.group(0)),
+ text,
+ )
+
+
+hash_replacements = {
+ "%": ".",
+ "(": ".28",
+ ")": ".29",
+ ".": ".2E",
+}
+
+
+def encode_hash_component(s: str) -> str:
+ encoded = urllib.parse.quote(s, safe="*")
+ return "".join(hash_replacements.get(c, c) for c in encoded)
+
+
+def channel_topic_url(stream_id: int, stream_name: str, topic_name: str | None = None) -> str:
+ link = f"#narrow/channel/{stream_id}-{encode_hash_component(stream_name.replace(' ', '-'))}"
+ if topic_name:
+ link += f"/topic/{encode_hash_component(topic_name)}"
+ return link
+
+
+def get_fallback_markdown_link(
+ stream_id: int, stream_name: str, topic_name: str | None = None
+) -> str:
+ """
+ Generates the markdown link syntax for a stream or topic link.
+ """
+ escape = escape_invalid_stream_topic_characters
+ url = channel_topic_url(stream_id, stream_name, topic_name)
+ if topic_name:
+ return f"[#{escape(stream_name)} > {escape(topic_name)}]({url})"
+
+ return f"[#{escape(stream_name)}]({url})"
+
+
+def get_stream_topic_link_syntax(stream_id: int, stream_name: str, topic_name: str) -> str:
+ # If the topic name is such that it will generate an invalid #**stream>topic** syntax,
+ # we revert to generating the normal markdown syntax for a link.
+ if will_produce_broken_stream_topic_link(topic_name) or will_produce_broken_stream_topic_link(
+ stream_name
+ ):
+ return get_fallback_markdown_link(stream_id, stream_name, topic_name)
+ return f"#**{stream_name}>{topic_name}**"
+
+
+def get_stream_link_syntax(stream_id: int, stream_name: str) -> str:
+ # If the topic name is such that it will generate an invalid #**stream>topic** syntax,
+ # we revert to generating the normal markdown syntax for a link.
+ if will_produce_broken_stream_topic_link(stream_name):
+ return get_fallback_markdown_link(stream_id, stream_name)
+ return f"#**{stream_name}**"
diff --git a/zerver/tests/test_topic_link_util.py b/zerver/tests/test_topic_link_util.py
new file mode 100644
index 00000000000..31f55772a9a
--- /dev/null
+++ b/zerver/tests/test_topic_link_util.py
@@ -0,0 +1,72 @@
+from typing_extensions import override
+
+from zerver.lib.test_classes import ZulipTestCase
+from zerver.lib.topic_link_util import get_stream_link_syntax, get_stream_topic_link_syntax
+
+
+class TestTopicLinkUtil(ZulipTestCase):
+ def test_stream_link_syntax(self) -> None:
+ sweden_id = self.make_stream("Sweden").id
+ money_id = self.make_stream("$$MONEY$$").id
+ md_id = self.make_stream("Markdown [md]").id
+
+ self.assertEqual(get_stream_link_syntax(sweden_id, "Sweden"), "#**Sweden**")
+
+ self.assertEqual(
+ get_stream_link_syntax(money_id, "$$MONEY$$"),
+ f"[#$$MONEY$$](#narrow/channel/{money_id}-.24.24MONEY.24.24)",
+ )
+
+ self.assertEqual(
+ get_stream_link_syntax(md_id, "Markdown [md]"),
+ f"[#Markdown [md]](#narrow/channel/{md_id}-Markdown-.5Bmd.5D)",
+ )
+
+ def test_stream_topic_link_syntax(self) -> None:
+ sweden_id = self.make_stream("Sweden").id
+ money_id = self.make_stream("$$MONEY$$").id
+ denmark_id = self.get_stream_id("Denmark")
+
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "topic"), "#**Sweden>topic**"
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "test `test` test"),
+ f"[#Sweden > test `test` test](#narrow/channel/{sweden_id}-Sweden/topic/test.20.60test.60.20test)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(denmark_id, "Denmark", "test `test` test`s"),
+ f"[#Denmark > test `test` test`s](#narrow/channel/{denmark_id}-Denmark/topic/test.20.60test.60.20test.60s)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "error due to *"),
+ f"[#Sweden > error due to *](#narrow/channel/{sweden_id}-Sweden/topic/error.20due.20to.20*)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "*asterisk"),
+ f"[#Sweden > *asterisk](#narrow/channel/{sweden_id}-Sweden/topic/*asterisk)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "greaterthan>"),
+ f"[#Sweden > greaterthan>](#narrow/channel/{sweden_id}-Sweden/topic/greaterthan.3E)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(money_id, "$$MONEY$$", "dollar"),
+ f"[#$$MONEY$$ > dollar](#narrow/channel/{money_id}-.24.24MONEY.24.24/topic/dollar)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "swe$$dish"),
+ f"[#Sweden > swe$$dish](#narrow/channel/{sweden_id}-Sweden/topic/swe.24.24dish)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "&ab"),
+ f"[#Sweden > &ab](#narrow/channel/{sweden_id}-Sweden/topic/.26ab)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "&ab]"),
+ f"[#Sweden > &ab]](#narrow/channel/{sweden_id}-Sweden/topic/.26ab.5D)",
+ )
+ self.assertEqual(
+ get_stream_topic_link_syntax(sweden_id, "Sweden", "&a[b"),
+ f"[#Sweden > &a[b](#narrow/channel/{sweden_id}-Sweden/topic/.26a.5Bb)",
+ )