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.

filter: Use correct type for `parse`.

Aman Agrawald440f50862f2
diff --git a/web/src/filter.ts b/web/src/filter.ts
index d2dfc74034c..a9e19b6c852 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -16,13 +16,8 @@ import type {User} from "./people.ts";
import * as people from "./people.ts";
import * as resolved_topic from "./resolved_topic.ts";
import type {UserPillItem} from "./search_suggestion.ts";
-import {
- current_user,
- narrow_canonical_term_schema,
- narrow_operator_schema,
- narrow_term_schema,
-} from "./state_data.ts";
-import type {NarrowCanonicalTerm, NarrowTerm} from "./state_data.ts";
+import {current_user, narrow_canonical_term_schema, narrow_operator_schema} from "./state_data.ts";
+import type {NarrowCanonicalTerm, NarrowTerm, NarrowTermSuggestion} from "./state_data.ts";
import * as stream_data from "./stream_data.ts";
import * as sub_store from "./sub_store.ts";
import * as user_topics from "./user_topics.ts";
@@ -401,13 +396,13 @@ export class Filter {
return util.robust_url_decode(encoded);
}
- // Parse a string into a list of terms (see below).
- static parse(str: string, for_pills = false): NarrowTerm[] {
- const terms: NarrowTerm[] = [];
+ // Parse a string into a list of search terms.
+ static parse(str: string, for_pills = false): NarrowTermSuggestion[] {
+ const terms: NarrowTermSuggestion[] = [];
let search_term: string[] = [];
let negated;
let operand;
- let term: NarrowTerm;
+ let term: NarrowTermSuggestion;
function maybe_add_search_terms(): void {
if (search_term.length > 0) {
@@ -472,11 +467,11 @@ export class Filter {
// terms list. This is done so that the last active filter is correctly
// detected by the `get_search_result` function (in search_suggestions.ts).
maybe_add_search_terms();
- term = narrow_term_schema.parse({
+ term = {
negated,
operator: filter_util.canonicalize_operator(parsed_operator.data),
operand,
- });
+ };
terms.push(term);
} else {
// Put it as a search term, to not have duplicate operators
@@ -489,6 +484,22 @@ export class Filter {
return terms;
}
+ static convert_suggestion_to_term(suggestion: NarrowTermSuggestion): NarrowTerm | undefined {
+ // NOTE: We will add more logic here once `NarrowTerm`
+ // operand has different type based on operator.
+ const potential_narrow_term: NarrowTerm = {
+ operator: suggestion.operator,
+ operand: suggestion.operand,
+ negated: suggestion.negated,
+ };
+
+ if (!Filter.is_valid_search_term(potential_narrow_term)) {
+ return undefined;
+ }
+
+ return potential_narrow_term;
+ }
+
static is_valid_search_term(term: NarrowTerm): boolean {
// We don't want to raise exceptions in this function; just
// return false for invalid terms.
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index 563ce7bf4b6..ef4d352840b 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -43,14 +43,16 @@ const user_pill_operators = new Set(["dm", "dm-including", "sender"]);
export function create_item_from_search_string(search_string: string): SearchPill | undefined {
const search_term = util.the(Filter.parse(search_string));
- if (!Filter.is_valid_search_term(search_term)) {
- // This will cause pill validation to fail and trigger a shake animation.
- return undefined;
+ const potential_narrow_term = Filter.convert_suggestion_to_term(search_term);
+
+ if (potential_narrow_term) {
+ return {
+ type: "generic_operator",
+ ...potential_narrow_term,
+ };
}
- return {
- type: "generic_operator",
- ...search_term,
- };
+
+ return undefined;
}
export function get_search_string_from_item(item: SearchPill): string {
diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjs
index d6336eeb6fe..6c42f1aa84f 100644
--- a/web/tests/search.test.cjs
+++ b/web/tests/search.test.cjs
@@ -356,6 +356,12 @@ run_test("initiate_search", ({override_rewire}) => {
assert.equal($("#search_query").text(), "");
});
+run_test("create_item_from_search_string with invalid string", () => {
+ search_pill.create_item_from_search_string("is:invalid");
+ const pills = search.search_pill_widget._get_pills_for_testing();
+ assert.equal(pills.length, 0);
+});
+
run_test("set_search_bar_contents with duplicate pills", () => {
const duplicate_attachment_terms = [
{