Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search_suggestion: Don't suggest searching inaccessible conversations.
Evy Kassirer42b9d852d5f1
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 7ffb566b418..baafe54537a 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -1001,6 +1001,13 @@ function suggestion_search_string(suggestion_line: SuggestionLine): string {}function suggestions_for_empty_search_query(): SuggestionLine[] {+ const current_narrow_terms = narrow_state.search_terms();+ // Don't suggest searching the current conversation when some of+ // its terms are invalid, e.g. if the user visited a link to a+ // channel that they can't access, and then opened search.+ if (current_narrow_terms.some((term) => !Filter.is_valid_canonical_term(term))) {+ return [];+ }// Since the context here is an **empty** search query, we assume// that there is no `near:` operator. So it's safe to use// functions like narrowed_by_topic_reply that return false on@@ -1013,7 +1020,7 @@ function suggestions_for_empty_search_query(): SuggestionLine[] {operand: narrow_state.stream_id()!.toString(),},]),- get_default_suggestion_line(narrow_state.search_terms()),+ get_default_suggestion_line(current_narrow_terms),];}if (narrow_state.narrowed_by_pm_reply()) {@@ -1024,10 +1031,10 @@ function suggestions_for_empty_search_query(): SuggestionLine[] {operand: "dm",},]),- get_default_suggestion_line(narrow_state.search_terms()),+ get_default_suggestion_line(current_narrow_terms),];}- return [get_default_suggestion_line(narrow_state.search_terms())];+ return [get_default_suggestion_line(current_narrow_terms)];}class Attacher {diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjsindex d38e1f8cb5f..54e61d2ef45 100644--- a/web/tests/search_suggestion.test.cjs+++ b/web/tests/search_suggestion.test.cjs@@ -1207,3 +1207,39 @@ test("queries_with_spaces", () => {expected = [`channel:${dev_help_id}`];assert.deepEqual(suggestions, expected);});++test("empty query suggestions in an inaccessible narrow", ({override}) => {+ const office_id = new_stream_id();+ stream_data.add_sub_for_tests(+ make_stream({+ stream_id: office_id,+ name: "office",+ subscribed: true,+ }),+ );+ const unknown_stream_id = new_stream_id();++ let narrow_terms;+ override(narrow_state, "search_terms", () => narrow_terms);+ override(narrow_state, "narrowed_by_topic_reply", () => false);+ override(narrow_state, "narrowed_by_pm_reply", () => false);++ // An empty query in an accessible narrow suggests searching the+ // current conversation.+ narrow_terms = [+ {operator: "channel", operand: office_id.toString(), negated: false},+ {operator: "topic", operand: "lunch", negated: false},+ ];+ let suggestions = search.get_suggestions([], [], true);+ assert.equal(suggestions[0], `channel:${office_id} topic:lunch`);++ // When the narrow's channel is unknown to this client,+ // we don't suggest searching the current conversation, and the+ // suggestions are the same as if there were no current filter.+ narrow_terms = [+ {operator: "channel", operand: unknown_stream_id.toString(), negated: false},+ {operator: "topic", operand: "lunch", negated: false},+ ];+ suggestions = search.get_suggestions([], [], true);+ assert.deepEqual(suggestions, search.get_suggestions([], [], false));+});