Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
filter: Add validation function for search pill terms.
evykassirer9dd36b4727ea
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 8fee434d7dc..a303efb632f 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -5,6 +5,7 @@ import assert from "minimalistic-assert";import * as resolved_topic from "../shared/src/resolved_topic";import render_search_description from "../templates/search_description.hbs";+import * as blueslip from "./blueslip";import * as hash_parser from "./hash_parser";import {$t} from "./i18n";import * as message_parser from "./message_parser";@@ -481,6 +482,74 @@ export class Filter {return terms;}+ static is_valid_search_term(+ term: NarrowTerm,+ allow_valid_operator_with_empty_operand = false,+ ): boolean {+ // `allow_valid_operator_with_empty_operand` is used for validating+ // partial terms for search suggestions, e.g. "dm:" shows+ // "Direct messages with" so it's valid. Note that we don't show messages+ // in search suggestions for empty "is:" or "has:".+ if (allow_valid_operator_with_empty_operand && term.operand === "") {+ return [+ "in",+ "id",+ "near",+ "channel",+ "stream",+ "topic",+ "sender",+ "from",+ "dm",+ "pm",+ "pm-with",+ "dm-including",+ "pm-including",+ "search",+ ].includes(term.operator);+ }+ switch (term.operator) {+ case "has":+ return ["image", "link", "attachment", "reaction"].includes(term.operand);+ case "is":+ return [+ "dm",+ "private",+ "starred",+ "mentioned",+ "alerted",+ "unread",+ "resolved",+ "followed",+ ].includes(term.operand);+ case "in":+ return ["home", "all"].includes(term.operand);+ case "id":+ case "near":+ return Number.isInteger(Number(term.operand));+ case "channel":+ case "stream":+ return stream_data.get_sub(term.operand) !== undefined;+ case "topic":+ return true;+ case "sender":+ case "from":+ case "dm":+ case "pm":+ case "pm-with":+ case "dm-including":+ case "pm-including":+ return term.operand+ .split(",")+ .every((email) => people.get_by_email(email) !== undefined);+ case "search":+ return true;+ default:+ blueslip.error("Unexpected search term operator: " + term.operator);+ return false;+ }+ }+/* Convert a list of search terms to a string.Each operator is a key-value pair likediff --git a/web/tests/filter.test.js b/web/tests/filter.test.jsindex daff0cbda59..4e8dbbcda25 100644--- a/web/tests/filter.test.js+++ b/web/tests/filter.test.js@@ -1657,6 +1657,53 @@ test("first_valid_id_from", ({override}) => {assert.equal(filter.first_valid_id_from(msg_ids), 20);});+test("is_valid_search_term", () => {+ const denmark = {+ stream_id: 100,+ name: "Denmark",+ };+ stream_data.add_sub(denmark);++ const test_data = [+ ["has: image", true],+ ["has: nonsense", false],+ ["is: unread", true],+ ["is: nonsense", false],+ ["in: home", true],+ ["in: nowhere", false],+ ["id: 4", true],+ ["near: home", false],+ ["channel: Denmark", true],+ ["channel: GhostTown", false],+ ["topic: GhostTown", true],+ ["dm-including: [email protected]", true],+ ["sender: [email protected]", false],+ ["dm: [email protected],[email protected]", false],+ ["dm: [email protected],[email protected]", true],+ ];+ for (const [search_term_string, expected_is_valid] of test_data) {+ assert.equal(+ Filter.is_valid_search_term(Filter.parse(search_term_string)[0]),+ expected_is_valid,+ );+ }++ blueslip.expect("error", "Unexpected search term operator: foo");+ assert.equal(+ Filter.is_valid_search_term({+ operator: "foo",+ operand: "bar",+ }),+ false,+ );++ // test `allow_valid_operator_with_empty_operand`+ assert.equal(Filter.is_valid_search_term(Filter.parse("dm: ")[0]), false);+ assert.equal(Filter.is_valid_search_term(Filter.parse("dm: ")[0], true), true);+ // Note that this is parsed as a search operator term and therefore is valid.+ assert.equal(Filter.is_valid_search_term(Filter.parse("foo: ")[0], true), true);+});+test("update_email", () => {const terms = [{operator: "dm", operand: "[email protected]"},