Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search_suggestion: Search topics from all subscribed channels.
apoorvapendse6d27f1073e51
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 8a96282fddf..a7bbdbd3073 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -14,6 +14,7 @@ import {type NarrowTerm} from "./state_data.ts";import * as stream_data from "./stream_data.ts";import * as stream_topic_history from "./stream_topic_history.ts";import * as stream_topic_history_util from "./stream_topic_history_util.ts";+import {type StreamSubscription} from "./sub_store.ts";import * as typeahead_helper from "./typeahead_helper.ts";import * as util from "./util.ts";@@ -25,6 +26,11 @@ export type UserPillItem = {should_add_guest_user_indicator: boolean;};+type ChannelTopicEntry = {+ channel_id: string;+ topic: string;+};+type TermPattern = Omit<NarrowTerm, "operand"> & Partial<Pick<NarrowTerm, "operand">>;const channel_incompatible_patterns: TermPattern[] = [@@ -445,12 +451,12 @@ function get_default_suggestion_line(terms: NarrowTerm[]): SuggestionLine {}export function get_topic_suggestions_from_candidates({- candidate_topics,+ candidate_topic_entries,guess,}: {- candidate_topics: string[];+ candidate_topic_entries: ChannelTopicEntry[];guess: string;-}): string[] {+}): ChannelTopicEntry[] {// This function is exported for unit testing purposes.const max_num_topics = 10;@@ -458,7 +464,7 @@ export function get_topic_suggestions_from_candidates({// In the search UI, once you autocomplete the channel,// we just show you the most recent topics before you even// need to start typing any characters.- return candidate_topics.slice(0, max_num_topics);+ return candidate_topic_entries.slice(0, max_num_topics);}// Once the user starts typing characters for a topic name,@@ -469,23 +475,29 @@ export function get_topic_suggestions_from_candidates({// The following loop can be expensive if you have lots// of topics in a channel, so we try to exit the loop as// soon as we find enough matches.- const topics: string[] = [];- for (const topic of candidate_topics) {- const topic_display_name = util.get_final_topic_display_name(topic);+ const topic_entries: ChannelTopicEntry[] = [];+ for (const candidate_topic_entry of candidate_topic_entries) {+ const topic_name = candidate_topic_entry.topic;++ const topic_display_name = util.get_final_topic_display_name(topic_name);if (common.phrase_match(guess, topic_display_name)) {- topics.push(topic);- if (topics.length >= max_num_topics) {+ topic_entries.push({channel_id: candidate_topic_entry.channel_id, topic: topic_name});+ if (topic_entries.length >= max_num_topics) {break;}}}- return topics;+ return topic_entries;}-function ignore_resolved_topic_prefix(topic_name: string): string {+function ignore_resolved_topic_prefix(entry: ChannelTopicEntry, case_insensitive = false): string {+ let topic_name = entry.topic;if (topic_name.startsWith(RESOLVED_TOPIC_PREFIX)) {- return topic_name.slice(2);+ topic_name = topic_name.slice(2);+ }+ if (case_insensitive) {+ return topic_name.toLowerCase();}return topic_name;}@@ -503,7 +515,6 @@ function get_topic_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestiolet channel_id: string | undefined;let guess: string | undefined;const filter = new Filter(terms);- const suggest_terms: NarrowTerm[] = [];// channel:Rome -> show all Rome topics// channel:Rome topic: -> show all Rome topics@@ -512,7 +523,8 @@ function get_topic_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestio// channel:Rome topic:f -> show all Rome topics with a word starting in f// When narrowed to a channel:- // topic: -> show all topics in current channel+ // topic: -> show topics from all subscribed channels with the current channel's+ // matching topics listed first.// foo -> show all topics in current channel with words starting with foo// If somebody explicitly types search:, then we might@@ -520,37 +532,59 @@ function get_topic_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestio// minor issue, and Filter.parse() is currently lossy// in terms of telling us whether they provided the operator,// i.e. "foo" and "search:foo" both become [{operator: 'search', operand: 'foo'}].++ let show_topics_from_other_channels = true;switch (operator) {case "channel":guess = "";channel_id = operand;- suggest_terms.push(last);break;case "topic":case "search":guess = operand;if (filter.has_operator("channel")) {channel_id = 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 = narrow_state.stream_id()?.toString();- if (channel_id) {- suggest_terms.push({operator: "channel", operand: channel_id});- }}break;}-- if (!channel_id) {+ if (!channel_id && !show_topics_from_other_channels) {return [];}- const subscription = stream_data.get_sub_by_id_string(channel_id);- if (!subscription) {+ let current_channel_subscription: StreamSubscription | undefined;+ let other_channel_subscriptions: StreamSubscription[] | undefined;++ if (channel_id) {+ current_channel_subscription = stream_data.get_sub_by_id_string(channel_id)!;+ }++ if (show_topics_from_other_channels) {+ other_channel_subscriptions = stream_data.get_streams_for_user(+ people.my_current_user_id(),+ ).subscribed;+ if (current_channel_subscription) {+ other_channel_subscriptions = other_channel_subscriptions.filter(+ (sub) => sub.stream_id !== current_channel_subscription.stream_id,+ );+ }+ }++ if (!current_channel_subscription && !other_channel_subscriptions) {return [];}- if (stream_data.can_access_topic_history(subscription)) {- stream_topic_history_util.get_server_history(subscription.stream_id, () => {+ const current_channel_topic_entries: ChannelTopicEntry[] = [];+ const other_channel_topic_entries: ChannelTopicEntry[] = [];+ if (+ current_channel_subscription &&+ stream_data.can_access_topic_history(current_channel_subscription)+ ) {+ stream_topic_history_util.get_server_history(current_channel_subscription.stream_id, () => {// Fetch topic history from the server, in case we will need it.// Note that we won't actually use the results from the server here// for this particular keystroke from the user, because we want to@@ -559,24 +593,77 @@ function get_topic_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestio// this function will get more candidates from calling// stream_topic_history.get_recent_topic_names.});+ assert(channel_id !== undefined);+ for (const topic of stream_topic_history.get_recent_topic_names(+ current_channel_subscription.stream_id,+ )) {+ current_channel_topic_entries.push({channel_id, topic});+ }}- const candidate_topics = stream_topic_history.get_recent_topic_names(subscription.stream_id);+ if (other_channel_subscriptions) {+ for (const other_sub of other_channel_subscriptions) {+ for (const topic of stream_topic_history.get_recent_topic_names(other_sub.stream_id)) {+ other_channel_topic_entries.push({+ channel_id: other_sub.stream_id.toString(),+ topic,+ });+ }+ }+ }assert(guess !== undefined);- const topics = get_topic_suggestions_from_candidates({candidate_topics, guess});-- // Just use alphabetical order. While recency and read/unreadness of- // topics do matter in some contexts, you can get that from the left sidebar,- // and I'm leaning toward high scannability for autocompletion. I also don't- // care about case. Also ignore the resolved icon (✔ ) while sorting.- topics.sort((a, b) =>- ignore_resolved_topic_prefix(a).localeCompare(ignore_resolved_topic_prefix(b)),- );+ let current_channel_topic_suggestion_entries = get_topic_suggestions_from_candidates({+ candidate_topic_entries: current_channel_topic_entries,+ guess,+ });+ let other_channel_topic_suggestion_entries = get_topic_suggestions_from_candidates({+ candidate_topic_entries: other_channel_topic_entries,+ guess,+ });++ // When the guess is non-empty, we rely on `typeaheader_helper.sorter` to give the same+ // experience as composebox_typeahead.+ // Else we sort in a case-insensitive fashion.+ if (guess.length > 0) {+ current_channel_topic_suggestion_entries = typeahead_helper.sorter(+ guess,+ current_channel_topic_suggestion_entries,+ ignore_resolved_topic_prefix,+ );+ other_channel_topic_suggestion_entries = typeahead_helper.sorter(+ guess,+ other_channel_topic_suggestion_entries,+ ignore_resolved_topic_prefix,+ );+ } else {+ current_channel_topic_suggestion_entries =+ current_channel_topic_suggestion_entries.toSorted((a, b) =>+ ignore_resolved_topic_prefix(a, true).localeCompare(+ ignore_resolved_topic_prefix(b, true),+ ),+ );+ other_channel_topic_suggestion_entries = other_channel_topic_suggestion_entries.toSorted(+ (a, b) =>+ ignore_resolved_topic_prefix(a, true).localeCompare(+ ignore_resolved_topic_prefix(b, true),+ ),+ );+ }+ // We want to rank topics from the current channel higher+ const topics = [+ ...current_channel_topic_suggestion_entries,+ ...other_channel_topic_suggestion_entries,+ ];return topics.map((topic) => {- const topic_term: NarrowTerm = {operator: "topic", operand: topic, negated};- const terms = [...suggest_terms, topic_term];+ const topic_term: NarrowTerm = {operator: "topic", operand: topic.topic, negated};+ const terms: NarrowTerm[] = [{operator: "channel", operand: topic.channel_id}, topic_term];+ // We don't want to have two channel pills in the search suggestion.+ if (filter.has_operator("channel")) {+ terms.splice(0, 1);+ }+return format_as_suggestion(terms);});}diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjsindex 3a08f815cc7..4b7c9150a8e 100644--- a/web/tests/search_suggestion.test.cjs+++ b/web/tests/search_suggestion.test.cjs@@ -726,27 +726,110 @@ test("topic_suggestions", ({override, mock_template}) => {suggestions = get_suggestions(`topic:REXX channel:${devel_id} topic:`);expected = [`topic:REXX channel:${devel_id} topic:`];assert.deepEqual(suggestions.strings, expected);++ suggestions = get_suggestions("topic:");+ expected = [+ "topic:",+ "channel:5 topic:✔+ice+cream",+ "channel:5 topic:ignore",+ `channel:5 topic:team`,+ "channel:5 topic:✔+team+work",+ `channel:5 topic:test`,+ ];+ assert.deepEqual(suggestions.strings, expected);+ override(narrow_state, "stream_id", () => "");++ for (const topic_name of ["a", "b", "c", "trap", "talks", "tower"]) {+ stream_topic_history.add_message({+ stream_id: devel_id,+ topic_name,+ });+ }++ stream_data.subscribe_myself(stream_data.get_sub("devel"));+ stream_data.subscribe_myself(stream_data.get_sub("office"));+ suggestions = get_suggestions("topic:");+ expected = [+ "topic:",+ "channel:6 topic:a",+ "channel:6 topic:b",+ "channel:6 topic:c",+ "channel:5 topic:✔+ice+cream",+ "channel:5 topic:ignore",+ "channel:6 topic:REXX",+ "channel:5 topic:team",+ "channel:5 topic:✔+team+work",+ "channel:5 topic:test",+ "channel:6 topic:trap",+ ];+ assert.deepEqual(suggestions.strings, expected);++ suggestions = get_suggestions("topic:t");+ expected = [+ "topic:t",+ "channel:5 topic:team",+ "channel:5 topic:✔+team+work",+ "channel:5 topic:test",+ "channel:6 topic:trap",+ "channel:6 topic:talks",+ "channel:6 topic:tower",+ ];+ assert.deepEqual(suggestions.strings, expected);++ // Prioritize topics from currently narrowed channel+ override(narrow_state, "stream_id", () => devel_id);+ suggestions = get_suggestions("topic:t");+ expected = [+ "topic:t",+ "channel:6 topic:trap",+ "channel:6 topic:talks",+ "channel:6 topic:tower",+ "channel:5 topic:team",+ "channel:5 topic:✔+team+work",+ "channel:5 topic:test",+ ];+ assert.deepEqual(suggestions.strings, expected);++ override(narrow_state, "stream_id", () => office_id);+ suggestions = get_suggestions("topic:t");+ expected = [+ "topic:t",+ "channel:5 topic:team",+ "channel:5 topic:✔+team+work",+ "channel:5 topic:test",+ "channel:6 topic:trap",+ "channel:6 topic:talks",+ "channel:6 topic:tower",+ ];+ assert.deepEqual(suggestions.strings, expected);});test("topic_suggestions (limits)", () => {- let candidate_topics = [];+ let candidate_topic_entries = [];++ function wrap(topics) {+ return topics.map((t) => ({channel_id: "1", topic: t}));+ }- function assert_result(guess, expected_topics) {+ function assert_result(guess, expected_topic_strings) {assert.deepEqual(- search.get_topic_suggestions_from_candidates({candidate_topics, guess}),- expected_topics,+ search.get_topic_suggestions_from_candidates({+ candidate_topic_entries,+ guess,+ }),+ wrap(expected_topic_strings),);}assert_result("", []);assert_result("zzz", []);- candidate_topics = ["a", "b", "c"];+ candidate_topic_entries = wrap(["a", "b", "c"]);assert_result("", ["a", "b", "c"]);assert_result("b", ["b"]);assert_result("z", []);- candidate_topics = [+ candidate_topic_entries = wrap(["a1","a2","b1",@@ -763,7 +846,8 @@ test("topic_suggestions (limits)", () => {"a10","a11","a12",- ];+ ]);+// We max out at 10 topics, so as not to overwhelm the user.assert_result("", ["a1", "a2", "b1", "b2", "a3", "a4", "a5", "c1", "a6", "a7"]);assert_result("a", ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"]);