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.

compose_reply: Fix invalid topic link in forward message syntax.

PieterCK0cfaeb280ce1
diff --git a/web/src/compose_reply.ts b/web/src/compose_reply.ts
index 0ce4ed500a0..53541fd13d1 100644
--- a/web/src/compose_reply.ts
+++ b/web/src/compose_reply.ts
@@ -326,6 +326,7 @@ export function quote_message(opts: {
const topic_link_syntax = topic_link_util.get_stream_topic_link_syntax(
channel_name,
message.topic,
+ true,
);
// Final message looks like:
// @_**Iago|5** [said](link to message) in [#channel > topic](link to topic):
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
index 2ed548e2c9b..d9640e6441b 100644
--- a/web/src/topic_link_util.ts
+++ b/web/src/topic_link_util.ts
@@ -126,6 +126,7 @@ export function get_fallback_markdown_link(
stream_name: string,
topic_name?: string,
message_id?: string,
+ only_link_syntax = false,
): string {
// Helper that should only be called by other methods in this file.
@@ -137,17 +138,23 @@ export function get_fallback_markdown_link(
topic_name,
message_id,
});
- return as_markdown_link_syntax(label_text_markdown, url);
+ return only_link_syntax
+ ? label_text_markdown
+ : as_markdown_link_syntax(label_text_markdown, url);
}
-export function get_stream_topic_link_syntax(stream_name: string, topic_name: string): string {
- // 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.
+export function get_stream_topic_link_syntax(
+ stream_name: string,
+ topic_name: string,
+ only_link_syntax = false,
+): string {
+ // If the topic/stream name would produce an invalid #**stream>topic** syntax, fall back
+ // to markdown link syntax. If only_link_syntax is true, only the link label is returned.
if (
will_produce_broken_stream_topic_link(topic_name) ||
will_produce_broken_stream_topic_link(stream_name)
) {
- return get_fallback_markdown_link(stream_name, topic_name);
+ return get_fallback_markdown_link(stream_name, topic_name, undefined, only_link_syntax);
}
return `#**${stream_name}>${topic_name}**`;
}
diff --git a/web/tests/compose_actions.test.cjs b/web/tests/compose_actions.test.cjs
index 285692d7f29..1e0d26384c6 100644
--- a/web/tests/compose_actions.test.cjs
+++ b/web/tests/compose_actions.test.cjs
@@ -20,6 +20,8 @@ const $ = require("./lib/zjquery.cjs");
const {set_current_user} = zrequire("state_data");
const user_groups = zrequire("user_groups");
const {initialize_user_settings} = zrequire("user_settings");
+const {will_produce_broken_stream_topic_link} = zrequire("topic_link_util");
+
initialize_user_settings({
user_settings: {
default_language: "en",
@@ -886,6 +888,43 @@ test("quote_message", ({disallow, override, override_rewire}) => {
quote_message(opts);
assert.ok(replaced);
+ const topic_with_invalid_characters = "[zulip/zulip>topic]";
+ assert.ok(will_produce_broken_stream_topic_link(topic_with_invalid_characters));
+
+ selected_message = {
+ type: "stream",
+ stream_id: denmark_stream.stream_id,
+ topic: topic_with_invalid_characters,
+ sender_full_name: steve.full_name,
+ sender_id: steve.user_id,
+ raw_content: "test invalid topic",
+ id: 11,
+ };
+ opts = {
+ reply_type: "personal",
+ forward_message: true,
+ message_id: selected_message.id,
+ };
+
+ const html_entity_encoded_topic_name = "[zulip/zulip>topic]";
+ const url_encoded_topic_name = ".5Bzulip.2Fzulip.3Etopic.5D";
+
+ const near_url = `http://zulip.zulipdev.com/#narrow/channel/${selected_message.stream_id}-${channel_object.name}/topic/${url_encoded_topic_name}/near/${selected_message.id}`;
+ const with_url = `#narrow/channel/${selected_message.stream_id}-${channel_object.name}/topic/${url_encoded_topic_name}/with/${selected_message.id}`;
+ const topic_link_syntax = `[#${channel_object.name} > ${html_entity_encoded_topic_name}](${with_url})`;
+
+ const fence = "```";
+ expected_replacement = `translated: @_**${selected_message.sender_full_name}|${selected_message.sender_id}** [said](${near_url}) in ${topic_link_syntax}:
+${fence}quote
+${selected_message.raw_content}
+${fence}`;
+ replaced = false;
+ override(message_lists.current, "get", (id) =>
+ id === selected_message.id ? selected_message : undefined,
+ );
+ quote_message(opts);
+ assert.ok(replaced);
+
// Quoting a highlighted(selected) part of a message using the ">" hotkey trigger
// should pass the message_id of the message whose text is highlighted in
// the `opts` to respond_to_message, to ensure the recipients of that message