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.

search_pill: Fix crash rendering suggestions for unknown channels.

Evy Kassirerb61432941fee
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index 72307a51765..0ae39369453 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -168,7 +168,14 @@ function maybe_generate_combined_channel_topic_pill(
const sign = search_pill.negated ? "-" : "";
const channel_operand = search_terms[index - 1]!.operand;
- const sub = stream_data.get_valid_sub_by_id_string(channel_operand);
+ const sub = stream_data.get_sub_by_id_string(channel_operand);
+ // Pill terms are only validated at pill creation, so a suggestion
+ // can reference a channel this client has no data for, e.g. a
+ // channel deleted while a pill referenced it. Fall back to
+ // separate pills, where the channel term is rendered as invalid.
+ if (sub === undefined) {
+ return undefined;
+ }
return {
...search_pill,
sign,
diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjs
index 6564215d7da..3a7671660e9 100644
--- a/web/tests/search.test.cjs
+++ b/web/tests/search.test.cjs
@@ -356,6 +356,32 @@ run_test("create_item_from_search_string with invalid string", () => {
assert.equal(pills.length, 0);
});
+run_test("generate_pills_html with unknown channel", ({mock_template, override}) => {
+ mock_template("search_list_item.hbs", true, (_data, html) => html);
+ override(realm, "realm_empty_topic_display_name", "general chat");
+
+ // A known channel followed by a topic is rendered as a combined
+ // `#channel > topic` pill.
+ let html = search_pill.generate_pills_html(`channel:${verona.stream_id} topic:lunch`, "");
+ assert.ok(html.includes("decorated-channel-name"));
+
+ // The channel can be unknown to this client, e.g. a channel
+ // deleted while a pill referenced it. We render separate pills,
+ // with the channel term rendered as invalid.
+ const unknown_stream_id_string = "999";
+ assert.equal(stream_data.get_sub_by_id_string(unknown_stream_id_string), undefined);
+ html = search_pill.generate_pills_html(`channel:${unknown_stream_id_string} topic:lunch`, "");
+ assert.ok(!html.includes("decorated-channel-name"));
+ assert.ok(html.includes(`channel:${unknown_stream_id_string}`));
+ assert.ok(html.includes("topic: lunch"));
+
+ // The same, for an empty string topic.
+ html = search_pill.generate_pills_html(`channel:${unknown_stream_id_string} topic:`, "");
+ assert.ok(!html.includes("decorated-channel-name"));
+ assert.ok(html.includes(`channel:${unknown_stream_id_string}`));
+ assert.ok(html.includes("empty-topic-display"));
+});
+
run_test("set_search_bar_contents with duplicate pills", () => {
const duplicate_attachment_terms = [
{