Skip to content

Commit

Permalink
search: Add search suggestion for "unresolved topics".
Browse files Browse the repository at this point in the history
Previously, the search suggestion only featured "resolved topics" and
filtering for "unresolved topics" was not as intuitive. Users would have
to use the negate operator "-" to filter for unresolved topics and it is
not possible to edit pills.

This change adds "unresolved topics" to the search with support to cancel
negations.

Fixes: #31725.
  • Loading branch information
Joelute committed Sep 26, 2024
1 parent 89e32bc commit 5be7bc1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 24 additions & 2 deletions web/src/search_suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,31 @@ function get_special_filter_suggestions(

const last_string = Filter.unparse([last]).toLowerCase();
suggestions = suggestions.filter((s) => {
let search_string = s.search_string;
if (match_criteria(terms, s.incompatible_patterns)) {
return false;
}
if (last_string === "") {
return true;
}

// If the suggestion is already negated and the search query is also negated,
// we should cancel out the negation while maintaining a suggestion match.
if (
(last.negated === true || is_search_operand_negated) &&
search_string.startsWith("--")
) {
search_string = search_string.slice(1);
s.search_string = s.search_string.slice(2);
}

// returns the substring after the ":" symbol.
const suggestion_operand = s.search_string.slice(s.search_string.indexOf(":") + 1);
const suggestion_operand = search_string.slice(search_string.indexOf(":") + 1);
// e.g for `att` search query, `has:attachment` should be suggested.
const show_operator_suggestions =
last.operator === "search" && suggestion_operand.toLowerCase().startsWith(last_string);
return (
s.search_string.toLowerCase().startsWith(last_string) ||
search_string.toLowerCase().startsWith(last_string) ||
show_operator_suggestions ||
s.description_html.toLowerCase().startsWith(last_string)
);
Expand Down Expand Up @@ -710,6 +721,17 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge
{operator: "dm-including"},
],
},
{
search_string: "-is:resolved",
description_html: "unresolved topics",
is_people: false,
incompatible_patterns: [
{operator: "is", operand: "resolved"},
{operator: "is", operand: "dm"},
{operator: "dm"},
{operator: "dm-including"},
],
},
];
const special_filtered_suggestions = get_special_filter_suggestions(last, terms, suggestions);
// Suggest "is:dm" to anyone with "is:private" in their muscle memory
Expand Down
2 changes: 2 additions & 0 deletions web/tests/search_suggestion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ test("empty_query_suggestions", () => {
"is:alerted",
"is:unread",
"is:resolved",
"-is:resolved",
"sender:myself@zulip.com",
`channel:${devel_id}`,
`channel:${office_id}`,
Expand Down Expand Up @@ -504,6 +505,7 @@ test("check_is_suggestions", ({override, mock_template}) => {
"-is:alerted",
"-is:unread",
"-is:resolved",
"is:resolved",
];
assert.deepEqual(suggestions.strings, expected);

Expand Down

0 comments on commit 5be7bc1

Please sign in to comment.