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 get_topic_link_content_with_stream_id.

Evy Kassirer9d65f353858c
diff --git a/web/src/clipboard_handler.ts b/web/src/clipboard_handler.ts
index 0cdfa1c3209..3d61b1d0d2f 100644
--- a/web/src/clipboard_handler.ts
+++ b/web/src/clipboard_handler.ts
@@ -39,11 +39,11 @@ export async function copy_link_to_clipboard(link: string): Promise<void> {
} else {
const stream = stream_data.get_sub_by_id(stream_topic_details.stream_id);
assert(stream !== undefined);
- const {text} = topic_link_util.get_topic_link_content_with_stream_name(
- stream.name,
- stream_topic_details.topic_name,
- stream_topic_details.message_id,
- );
+ const {text} = topic_link_util.get_topic_link_content_with_stream_name({
+ stream_name: stream.name,
+ topic_name: stream_topic_details.topic_name,
+ message_id: stream_topic_details.message_id,
+ });
const copy_in_html_syntax = topic_link_util.as_html_link_syntax_unsafe(text, link);
const copy_in_markdown_syntax = topic_link_util.as_markdown_link_syntax(text, link);
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
index 7408512348f..28484236574 100644
--- a/web/src/topic_link_util.ts
+++ b/web/src/topic_link_util.ts
@@ -4,6 +4,7 @@ import assert from "minimalistic-assert";
import * as hash_util from "./hash_util.ts";
import * as stream_data from "./stream_data.ts";
+import type {StreamSubscription} from "./sub_store.ts";
import * as util from "./util.ts";
const invalid_stream_topic_regex = /[`>*&[\]]|(\$\$)/g;
@@ -54,14 +55,34 @@ export function html_escape_markdown_syntax_characters(text: string): string {
return text.replaceAll(invalid_stream_topic_regex, escape_invalid_stream_topic_characters);
}
-export function get_topic_link_content_with_stream_name(
- stream_name: string,
- topic_name?: string,
- message_id?: string,
-): {text: string; url: string} {
- const stream = stream_data.get_sub(stream_name);
- const stream_id = stream?.stream_id;
- assert(stream_id !== undefined);
+export function get_topic_link_content_with_stream_name(opts: {
+ stream_name: string;
+ topic_name: string | undefined;
+ message_id: string | undefined;
+}): {text: string; url: string} {
+ const stream = stream_data.get_sub(opts.stream_name);
+ assert(stream !== undefined);
+ return _get_topic_link_content({stream, ...opts});
+}
+
+export function get_topic_link_content_with_stream_id(opts: {
+ stream_id: number;
+ topic_name: string | undefined;
+ message_id: string | undefined;
+}): {text: string; url: string} {
+ const stream = stream_data.get_sub_by_id(opts.stream_id);
+ assert(stream !== undefined);
+ return _get_topic_link_content({stream, ...opts});
+}
+
+function _get_topic_link_content(opts: {
+ stream: StreamSubscription;
+ topic_name: string | undefined;
+ message_id: string | undefined;
+}): {text: string; url: string} {
+ const {stream, topic_name, message_id} = opts;
+ const stream_name = stream.name;
+ const stream_id = stream.stream_id;
const escape = html_escape_markdown_syntax_characters;
if (topic_name !== undefined) {
const stream_topic_url = hash_util.by_stream_topic_url(stream_id, topic_name);
@@ -103,11 +124,11 @@ export function get_fallback_markdown_link(
// Generates the vanilla markdown link syntax for a stream/topic/message link, as
// a fallback for cases where the nicer Zulip link syntax would not
// render properly due to special characters in the channel or topic name.
- const {text, url} = get_topic_link_content_with_stream_name(
+ const {text, url} = get_topic_link_content_with_stream_name({
stream_name,
topic_name,
message_id,
- );
+ });
return as_markdown_link_syntax(text, url);
}
diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjs
index 1b6353092a8..baf538aac39 100644
--- a/web/tests/topic_link_util.test.cjs
+++ b/web/tests/topic_link_util.test.cjs
@@ -157,3 +157,31 @@ run_test("stream_topic_link_syntax_test", () => {
"$$MONEY$$",
);
});
+
+run_test("get_topic_link_content_with_stream_name", () => {
+ assert.deepEqual(
+ topic_link_util.get_topic_link_content_with_stream_name({
+ stream_name: sweden_stream.name,
+ topic_name: "abc",
+ message_id: 123,
+ }),
+ {
+ text: "#Sweden > abc @ 💬",
+ url: "#narrow/channel/1-Sweden/topic/abc/near/123",
+ },
+ );
+});
+
+run_test("get_topic_link_content_with_stream_id", () => {
+ assert.deepEqual(
+ topic_link_util.get_topic_link_content_with_stream_id({
+ stream_id: sweden_stream.stream_id,
+ topic_name: "abc",
+ message_id: 123,
+ }),
+ {
+ text: "#Sweden > abc @ 💬",
+ url: "#narrow/channel/1-Sweden/topic/abc/near/123",
+ },
+ );
+});