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: Avoid showing topic suggestions for non-id channel operands.

apoorvapendse3240c0341651
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index 4aa6d7bbf81..70a2ccb38c5 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -519,7 +519,11 @@ function get_topic_suggestions(
const operand = last.operand;
const negated = last.operator === "topic" && last.negated;
- let channel_id_str: string | undefined;
+ // For the case where the channel operator is the last
+ // term, the operand may just be a string and is not
+ // guaranteed to always be a channel id in a string format.
+ // We use a name that represents this .
+ let channel_id_or_operand_str: string | undefined;
let guess: string | undefined;
const filter = new Filter(terms);
@@ -528,6 +532,7 @@ function get_topic_suggestions(
// channel:Rome f -> show all Rome topics with a word starting in f
// channel:Rome topic:f -> show all Rome topics with a word starting in f
// channel:Rome topic:f -> show all Rome topics with a word starting in f
+ // channel:NonExistentChannel -> no topic suggestions.
// When narrowed to a channel:
// topic: -> show topics from all subscribed channels with the current channel's
@@ -544,22 +549,23 @@ function get_topic_suggestions(
switch (last.operator) {
case "channel":
guess = "";
- channel_id_str = operand;
+ channel_id_or_operand_str = operand;
break;
case "topic":
case "search":
guess = operand;
if (filter.has_operator("channel")) {
- channel_id_str = filter.terms_with_operator("channel")[0]!.operand;
+ channel_id_or_operand_str = filter.terms_with_operator("channel")[0]!.operand;
// We want to show topics that belong only to the
// channel mentioned in the `channel` operator, if it exists.
show_topics_from_other_channels = false;
} else {
- channel_id_str = narrow_state.stream_id()?.toString();
+ channel_id_or_operand_str = narrow_state.stream_id()?.toString();
}
break;
}
- if (!channel_id_str && !show_topics_from_other_channels) {
+
+ if (!channel_id_or_operand_str && !show_topics_from_other_channels) {
return [];
}
@@ -571,10 +577,16 @@ function get_topic_suggestions(
);
const current_channel_topic_entries: ChannelTopicEntry[] = [];
- if (channel_id_str && !excluded_channel_ids.has(channel_id_str)) {
+ if (channel_id_or_operand_str && !excluded_channel_ids.has(channel_id_or_operand_str)) {
// We do this outside the stream_data.subscribed_stream_ids loop,
// since we could be viewing a channel we can't read.
- const sub = stream_data.get_sub_by_id_string(channel_id_str)!;
+ const sub = stream_data.get_sub_by_id_string(channel_id_or_operand_str);
+ if (sub === undefined && last.operator === "channel") {
+ // Since the channel_id_or_operand_str is not a
+ // valid channel id we avoid sending any topic
+ // suggestions for a channel as the last term.
+ return [];
+ }
if (sub && stream_data.can_access_topic_history(sub)) {
const current_channel_id = sub.stream_id;
stream_topic_history_util.get_server_history(current_channel_id, () => {
@@ -585,7 +597,7 @@ function get_topic_suggestions(
});
for (const topic of stream_topic_history.get_recent_topic_names(current_channel_id)) {
- current_channel_topic_entries.push({channel_id: channel_id_str, topic});
+ current_channel_topic_entries.push({channel_id: channel_id_or_operand_str, topic});
}
}
}
@@ -593,7 +605,7 @@ function get_topic_suggestions(
const other_channel_topic_entries: ChannelTopicEntry[] = [];
for (const subscribed_channel_id of stream_data.subscribed_stream_ids()) {
if (
- subscribed_channel_id.toString() === channel_id_str ||
+ subscribed_channel_id.toString() === channel_id_or_operand_str ||
excluded_channel_ids.has(subscribed_channel_id.toString())
) {
continue;
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index b61033e169d..35d1e21aee2 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -819,6 +819,21 @@ test("topic_suggestions", ({override, override_rewire}) => {
"channel:6 topic:tower",
];
assert.deepEqual(suggestions, expected);
+
+ // This is the case where the last term's
+ // operand is not a stringified channel id.
+ // We avoid suggesting topics in this case.
+ suggestions = get_suggestions("channel:foobar");
+ expected = [];
+ assert.deepEqual(suggestions, expected);
+
+ // We shouldn't see topic suggestions for the case
+ // where the last term is a channel with an operand
+ // that couldn't be converted to a string id by the
+ // Filter.parse step.
+ suggestions = get_suggestions("hello channel:foobar");
+ expected = [];
+ assert.deepEqual(suggestions, expected);
});
test("topic_suggestions (limits)", () => {