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: Surface sibling channels when an exact channel name is typed.

Evy Kassirer63c284de5854
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index f1e03feb1fc..75ea9f5ab8d 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -313,7 +313,7 @@ function sorted_channel_name_matches(query: string): string[] {
return typeahead_helper.sorter(query, unsorted, (x) => x);
}
-function channel_name_suggestion(channel_name: string, negated: boolean): Suggestion {
+function channel_name_suggestion(channel_name: string, negated: boolean | undefined): Suggestion {
const channel = stream_data.get_sub_by_name(channel_name);
assert(channel !== undefined);
return Filter.unparse([{operator: "channel", operand: channel.stream_id.toString(), negated}]);
@@ -332,23 +332,28 @@ function get_channel_suggestions(
assert(last.operator === "channel" || last.operator === "search" || last.operator === "");
- const query = last.operand;
- const channel_names = stream_data.subscribed_stream_names();
- let matching_channel_names = channel_names.filter((channel_name) =>
- channel_matches_query(channel_name, query),
+ // `Filter.parse` may have resolved an exact channel name to its
+ // `stream_id`, so match both the resolved name (surfacing siblings,
+ // e.g. "channel:core" → "core team") and the operand as literally
+ // typed (keeping a digit-named "7th floor" reachable via
+ // "channel:7"). Name first, so a real sibling outranks a channel
+ // that merely starts with the operand's id digits.
+ const queries = [last.operand];
+ if (last.operator === "channel") {
+ const sub = stream_data.get_sub_by_id_string(last.operand);
+ if (sub !== undefined) {
+ queries.unshift(sub.name);
+ }
+ }
+ const matching_channel_names = new Set<string>();
+ for (const channel_query of queries) {
+ for (const channel_name of sorted_channel_name_matches(channel_query)) {
+ matching_channel_names.add(channel_name);
+ }
+ }
+ return [...matching_channel_names].map((channel_name) =>
+ channel_name_suggestion(channel_name, last.negated),
);
- matching_channel_names = typeahead_helper.sorter(query, matching_channel_names, (x) => x);
- return matching_channel_names.map((channel_name) => {
- const channel = stream_data.get_sub_by_name(channel_name);
- assert(channel !== undefined);
- const term: NarrowTerm = {
- operator: "channel",
- operand: channel.stream_id.toString(),
- negated: last.negated,
- };
- const search_string = Filter.unparse([term]);
- return search_string;
- });
}
// Find subscribed channels whose name matches the multi-word `query`,
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index 23ae5da323c..c985e8237ca 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -1034,6 +1034,63 @@ test("channel_completion", ({override}) => {
assert.deepEqual(suggestions, expected);
});
+test("channel_exact_name_surfaces_siblings", ({override}) => {
+ override(stream_topic_history_util, "get_server_history", noop);
+
+ const core_id = new_stream_id();
+ stream_data.add_sub_for_tests(
+ make_stream({stream_id: core_id, name: "core", subscribed: true}),
+ );
+ const core_team_id = new_stream_id();
+ stream_data.add_sub_for_tests(
+ make_stream({stream_id: core_team_id, name: "core team", subscribed: true}),
+ );
+
+ // Typing the exact name "core" resolves to its stream_id via
+ // `Filter.parse`, but we still phrase-match the name so the
+ // sibling channel "core team" surfaces as an alternative
+ // interpretation, just as it would for the still-incomplete
+ // "channel:cor".
+ let query = "channel:core";
+ let suggestions = get_suggestions(query);
+ let expected = [`channel:${core_id}`, `channel:${core_team_id}`];
+ assert.deepEqual(suggestions, expected);
+
+ // The still-incomplete name behaves identically, confirming the
+ // exact-match case isn't a special-cased regression.
+ query = "channel:cor";
+ suggestions = get_suggestions(query);
+ expected = [`channel:${core_id}`, `channel:${core_team_id}`];
+ assert.deepEqual(suggestions, expected);
+
+ // A numeric operand resolves to that stream_id's channel, but we
+ // also match it as literally typed, so `channel:7` surfaces both
+ // #Venice (id 7) and a channel whose name starts with "7".
+ stream_data.add_sub_for_tests(make_stream({stream_id: 7, name: "Venice", subscribed: true}));
+ const seventh_floor_id = new_stream_id();
+ stream_data.add_sub_for_tests(
+ make_stream({stream_id: seventh_floor_id, name: "7th floor", subscribed: true}),
+ );
+ query = "channel:7";
+ suggestions = get_suggestions(query);
+ expected = ["channel:7", `channel:${seventh_floor_id}`];
+ assert.deepEqual(suggestions, expected);
+
+ // "Venice" (id 7) has a genuine sibling "Venice beach". Typing the
+ // exact name resolves the operand to the id "7", so we match both
+ // the name "Venice" and the literal "7". The genuine sibling
+ // "Venice beach" (a name match) must rank above "7th floor" (only a
+ // coincidental match on the id-7 literal).
+ const venice_beach_id = new_stream_id();
+ stream_data.add_sub_for_tests(
+ make_stream({stream_id: venice_beach_id, name: "Venice beach", subscribed: true}),
+ );
+ query = "channel:Venice";
+ suggestions = get_suggestions(query);
+ expected = ["channel:7", `channel:${venice_beach_id}`, `channel:${seventh_floor_id}`];
+ assert.deepEqual(suggestions, expected);
+});
+
test("channel_multi_word_completion", ({override}) => {
override(narrow_state, "stream_id", noop);
override(stream_topic_history_util, "get_server_history", noop);