Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
internal_url: Exclude stream name from encoded slug if it is too long.
Kislay Verma6b912c2cedfa
diff --git a/web/src/hash_util.ts b/web/src/hash_util.tsindex 011856f4a52..6fbf131fd30 100644--- a/web/src/hash_util.ts+++ b/web/src/hash_util.ts@@ -58,7 +58,7 @@ export function encode_stream_id(stream_id: number): string {// URI encoding piececonst slug = stream_data.id_to_slug(stream_id);- return internal_url.encodeHashComponent(slug);+ return internal_url.encode_slug(stream_id, slug);}export function decode_operand(diff --git a/web/src/internal_url.ts b/web/src/internal_url.tsindex 9e03cbcdca4..ac7d6758715 100644--- a/web/src/internal_url.ts+++ b/web/src/internal_url.ts@@ -46,6 +46,26 @@ export function stream_id_to_slug(return `${stream_id}-${name}`;}+export function encode_slug(stream_id: number, slug: string): string {+ // When generating slugs for channels in URLs, we like to provide+ // an encoding of the channel name in the URL along with the+ // channel ID to have the link hint what channel it is. But we+ // want to only do this when the hinting is useful and does not+ // greatly lengthen the string as a whole.+ const MAX_SLUG_LENGTH_DIFF_FROM_ENCODING = 10;+ const MAX_SLUG_LENGTH_RATIO_FROM_ENCODING = 1.5;++ const encoded_slug = encodeHashComponent(slug);+ const length_diff = encoded_slug.length - slug.length;+ if (+ length_diff > MAX_SLUG_LENGTH_DIFF_FROM_ENCODING ||+ encoded_slug.length > slug.length * MAX_SLUG_LENGTH_RATIO_FROM_ENCODING+ ) {+ return String(stream_id);+ }+ return encoded_slug;+}+export function encode_stream_id(stream_id: number,maybe_get_stream_name: MaybeGetStreamName,@@ -54,7 +74,7 @@ export function encode_stream_id(// URI encoding piece.const slug = stream_id_to_slug(stream_id, maybe_get_stream_name);- return encodeHashComponent(slug);+ return encode_slug(stream_id, slug);}export function by_stream_url(diff --git a/web/tests/internal_url.test.cjs b/web/tests/internal_url.test.cjsindex ac226285639..1ce92737c19 100644--- a/web/tests/internal_url.test.cjs+++ b/web/tests/internal_url.test.cjs@@ -64,3 +64,50 @@ run_test("test by_stream_topic_url", () => {result = internal_url.by_stream_topic_url(123, "test topic", maybe_get_stream_name, 12);assert.equal(result, "#narrow/channel/123-a-test-stream/topic/test.20topic/with/12");});++run_test("test encode_slug", () => {+ // The test cases include stream id to mirror how+ // internal_url.encode_stream_id works.+ const test_cases = [+ // diff=0, ratio=1.0 → included (pure ASCII)+ ["1-test-stream-autocomplete", true],+ // 1-books.2C-film.2C-tv.2C-and-games;+ // diff=6, ratio=1.214 → included (ASCII with commas)+ ["1-books,-film,-tv,-and-games", true],+ // 1-.E7.B2.BE.E9.80.89.20-.20.E8.AF.91.E6.96.87;+ // diff=36, ratio=5.0 → excluded (Chinese with spaces)+ ["1-精选 - 译文", false],+ // 1-go.C3.BBta.20.C3.A0.20l.27.C5.93uf.20br.C3.BBl.C3.A9;+ // diff=33, ratio=2.571 → excluded (many accented Latin chars)+ ["1-goûta à l'œuf brûlé", false],+ // 1-.E0.B0.AF.E0.B0.B6;+ // diff=16, ratio=5.0 → excluded (very short non-Latin slug)+ ["1-యశ", false],+ // 1-.282.2B3.29.255;+ // diff=8, ratio=1.889 → excluded (ASCII special chars, short slug)+ ["1-(2+3)%5", false],+ // 1-abcdefghijklmnop-.C3.A9.C3.A9;+ // diff=10, ratio=1.476 → included (at diff limit)+ ["1-abcdefghijklmnop-éé", true],+ // 1-abcdefghijklmno-.C3.A9.C3.A9;+ // diff=10, ratio=1.5 exactly → included (at ratio limit)+ ["1-abcdefghijklmno-éé", true],+ // 1-abcdefghijklmnopqrstu-.C3.A9.C3.A9.2E;+ // diff=12, ratio=1.444 → excluded (over diff limit)+ ["1-abcdefghijklmnopqrstu-éé.", false],+ // 1-abcd-.C3.A9;+ // diff=5, ratio=1.625 → excluded (over ratio limit)+ ["1-abcd-é", false],+ ];++ for (const [channel_slug, should_encode_slug] of test_cases) {+ if (should_encode_slug) {+ assert.equal(+ internal_url.encode_slug(1, channel_slug),+ internal_url.encodeHashComponent(channel_slug),+ );+ } else {+ assert.ok(Number(internal_url.encode_slug(1, channel_slug)) === 1);+ }+ }+});diff --git a/web/tests/markdown.test.cjs b/web/tests/markdown.test.cjsindex a700fdcd369..4596ed43e51 100644--- a/web/tests/markdown.test.cjs+++ b/web/tests/markdown.test.cjs@@ -664,12 +664,12 @@ test("marked", ({override}) => {{input: "#**& & &amp;**",expected:- '<p><a class="stream" data-stream-id="5" href="/#narrow/channel/5-.26-.26-.26amp.3B">#& & &amp;</a></p>',+ '<p><a class="stream" data-stream-id="5" href="/#narrow/channel/5">#& & &amp;</a></p>',},{input: "#**& & &amp;>& & &amp;**",expected:- '<p><a class="stream-topic" data-stream-id="5" href="#narrow/channel/5-.26-.26-.26amp.3B/topic/.26.20.26.20.26amp.3B">#& & &amp; > & & &amp;</a></p>',+ '<p><a class="stream-topic" data-stream-id="5" href="#narrow/channel/5/topic/.26.20.26.20.26amp.3B">#& & &amp; > & & &amp;</a></p>',},];diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjsindex b23f4a0960b..486911ea509 100644--- a/web/tests/topic_link_util.test.cjs+++ b/web/tests/topic_link_util.test.cjs@@ -60,7 +60,7 @@ run_test("stream_link_syntax_test", () => {assert.equal(topic_link_util.get_stream_link_syntax("Denmark"), "#**Denmark**");assert.equal(topic_link_util.get_stream_link_syntax("$$MONEY$$"),- "[#$$MONEY$$](#narrow/channel/6-.24.24MONEY.24.24)",+ "[#$$MONEY$$](#narrow/channel/6)",);assert.equal(topic_link_util.get_stream_link_syntax("Markdown [md]"),@@ -95,7 +95,7 @@ run_test("stream_topic_link_syntax_test", () => {);assert.equal(topic_link_util.get_stream_topic_link_syntax("$$MONEY$$", "dollar"),- "[#$$MONEY$$ > dollar](#narrow/channel/6-.24.24MONEY.24.24/topic/dollar)",+ "[#$$MONEY$$ > dollar](#narrow/channel/6/topic/dollar)",);assert.equal(topic_link_util.get_stream_topic_link_syntax("Sweden", "swe$$dish"),@@ -108,7 +108,7 @@ run_test("stream_topic_link_syntax_test", () => {assert.equal(topic_link_util.get_fallback_markdown_link("$$MONEY$$"),- "[#$$MONEY$$](#narrow/channel/6-.24.24MONEY.24.24)",+ "[#$$MONEY$$](#narrow/channel/6)",);assert.equal(topic_link_util.get_fallback_markdown_link("Markdown [md]"),@@ -133,7 +133,7 @@ run_test("stream_topic_link_syntax_test", () => {assert.equal(topic_link_util.get_stream_topic_link_syntax("$$MONEY$$", ""),- "[#$$MONEY$$ > translated: general chat](#narrow/channel/6-.24.24MONEY.24.24/topic/)",+ "[#$$MONEY$$ > translated: general chat](#narrow/channel/6/topic/)",);assert.equal(