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: Separate out canonical and legacy operators.

Aman Agrawal3c3e5fcc1b9c
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py
index 81d1b3253d5..c3a96076f58 100644
--- a/tools/linter_lib/custom_check.py
+++ b/tools/linter_lib/custom_check.py
@@ -127,6 +127,7 @@ js_rules = RuleList(
"web/src/message_events_util.ts",
"web/src/message_helper.ts",
"web/src/server_message.ts",
+ "web/src/state_data.ts",
"web/tests/",
},
"exclude_pattern": "emails",
diff --git a/web/src/filter.ts b/web/src/filter.ts
index e0c1c156074..f8888ac91bd 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -16,8 +16,14 @@ 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_term_schema} from "./state_data.ts";
-import type {NarrowTerm} from "./state_data.ts";
+import {
+ current_user,
+ narrow_canonical_operator_schema,
+ narrow_canonical_term_schema,
+ narrow_operator_schema,
+ narrow_term_schema,
+} from "./state_data.ts";
+import type {NarrowCanonicalOperator, NarrowCanonicalTerm, NarrowTerm} 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";
@@ -262,9 +268,7 @@ export class Filter {
this.narrow_requires_hash_change = false;
}
- static canonicalize_operator(operator: string): string {
- operator = operator.toLowerCase();
-
+ static canonicalize_operator(operator: NarrowTerm["operator"]): NarrowCanonicalOperator {
if (operator === "pm-with") {
// "pm-with:" was renamed to "dm:"
return "dm";
@@ -290,22 +294,23 @@ export class Filter {
if (util.is_channels_synonym(operator)) {
return "channels";
}
- return operator;
+
+ return narrow_canonical_operator_schema.parse(operator);
}
static canonicalize_term({
negated = false,
operator,
operand,
- }: {
- negated?: boolean | undefined;
- operator: string;
- operand: string;
- }): NarrowTerm {
+ }: NarrowTerm): NarrowCanonicalTerm {
// Make negated explicitly default to false for both clarity and
// simplifying deepEqual checks in the tests.
- operator = Filter.canonicalize_operator(operator);
- const narrow_term = narrow_term_schema.parse({negated, operator, operand});
+ const canonical_operator = Filter.canonicalize_operator(operator);
+ const narrow_term = narrow_canonical_term_schema.parse({
+ negated,
+ operator: canonical_operator,
+ operand,
+ });
switch (narrow_term.operator) {
case "is":
@@ -463,7 +468,7 @@ export class Filter {
} else {
// Looks like an operator.
negated = false;
- operator = parts.shift();
+ operator = parts.shift()?.toLowerCase();
// `split` returns a non-empty array
assert(operator !== undefined);
if (operator.startsWith("-")) {
@@ -488,26 +493,26 @@ export class Filter {
operand = people.my_current_email();
}
- // We use Filter.operator_to_prefix() to check if the
- // operator is known. If it is not known, then we treat
+ // Check if the operator is known, if not then we treat
// it as a search for the given string (which may contain
// a `:`), not as a search operator.
- if (Filter.operator_to_prefix(operator, negated) === "") {
+ const parsed_operator = narrow_operator_schema.safeParse(operator);
+ if (operator !== "" && parsed_operator.success) {
+ // If any search query was present and it is followed by some other filters
+ // then we must add that search filter in its current position in the
+ // 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({
+ negated,
+ operator: Filter.canonicalize_operator(parsed_operator.data),
+ operand,
+ });
+ terms.push(term);
+ } else {
// Put it as a search term, to not have duplicate operators
search_term.push(token);
- continue;
}
- // If any search query was present and it is followed by some other filters
- // then we must add that search filter in its current position in the
- // 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({
- negated,
- operator: Filter.canonicalize_operator(operator),
- operand,
- });
- terms.push(term);
}
}
@@ -652,7 +657,7 @@ export class Filter {
return term_types.toSorted(compare);
}
- static operator_to_prefix(operator: string, negated?: boolean): string {
+ static operator_to_prefix(operator: NarrowTerm["operator"], negated?: boolean): string {
operator = Filter.canonicalize_operator(operator);
if (operator === "search") {
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 787adcba9e6..9ceedfb1b79 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -239,7 +239,11 @@ export function parse_narrow(hash: string[]): NarrowTerm[] | undefined {
}
const operand = decode_operand(operator, raw_operand);
- terms.push({negated, operator, operand});
+ terms.push({
+ negated,
+ operator: operator.toLowerCase(),
+ operand,
+ });
}
return z.array(narrow_term_schema).parse(terms);
}
diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.ts
index 9da3d508156..247cb810754 100644
--- a/web/src/message_fetch.ts
+++ b/web/src/message_fetch.ts
@@ -23,6 +23,7 @@ import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
import * as popup_banners from "./popup_banners.ts";
import * as recent_view_ui from "./recent_view_ui.ts";
+import {narrow_operator_schema} from "./state_data.ts";
import type {NarrowTerm} from "./state_data.ts";
import * as stream_data from "./stream_data.ts";
import * as stream_list from "./stream_list.ts";
@@ -285,7 +286,10 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str
negated?: boolean | undefined;
} = raw_term;
- const canonical_operator = Filter.canonicalize_operator(raw_term.operator);
+ const parsed_narrow_operator = narrow_operator_schema.parse(
+ raw_term.operator.toLowerCase(),
+ );
+ const canonical_operator = Filter.canonicalize_operator(parsed_narrow_operator);
if (operators_supporting_ids.has(canonical_operator)) {
const user_ids_array = people.emails_strings_to_user_ids_array(raw_term.operand);
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index deabc823540..8dde7bbe928 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -119,12 +119,15 @@ export function generate_pills_html(suggestion: Suggestion, text_query: string):
if (user_pill_operators.has(term.operator) && term.operand !== "") {
return search_user_pill_data_from_term(term);
}
- const search_pill: SearchPill = {
- type: "generic_operator",
+ const narrow_term: NarrowTerm = {
operator: term.operator,
operand: term.operand,
negated: term.negated,
};
+ const search_pill: SearchPill = {
+ type: "generic_operator",
+ ...narrow_term,
+ };
if (search_pill.operator === "topic" && search_pill.operand === "") {
// There are three variants of this suggestion state:
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index 17d803b8975..0dbd96fcc11 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -20,31 +20,54 @@ const group_permission_setting_schema = z.object({
});
export type GroupPermissionSetting = z.output<typeof group_permission_setting_schema>;
-export const narrow_term_schema = z.object({
+export const narrow_canonical_operator_schema = z.enum([
+ "", // Used for search suggestions.
+ "channel",
+ "channels",
+ "dm",
+ "dm-including",
+ "has",
+ "id",
+ "in",
+ "is",
+ "near",
+ "search",
+ "sender",
+ "topic",
+ "with",
+]);
+export type NarrowCanonicalOperator = z.output<typeof narrow_canonical_operator_schema>;
+
+const narrow_legacy_operator_schema = z.enum([
+ "pm-with",
+ "group-pm-with",
+ "from",
+ "stream",
+ "streams",
+ "subject",
+]);
+
+export const narrow_operator_schema = z.union([
+ narrow_canonical_operator_schema,
+ narrow_legacy_operator_schema,
+]);
+export type NarrowOperator = z.output<typeof narrow_operator_schema>;
+
+export const narrow_canonical_term_schema = z.object({
negated: z.optional(z.boolean()),
- operator: z.enum([
- "", // Used for search suggestions.
- "channel",
- "channels",
- "dm",
- "dm-including",
- "from",
- "group-pm-with",
- "has",
- "id",
- "in",
- "is",
- "near",
- "pm-with",
- "search",
- "sender",
- "stream",
- "streams",
- "topic",
- "with",
- ]),
+ operator: narrow_canonical_operator_schema,
operand: z.string(),
});
+export type NarrowCanonicalTerm = z.output<typeof narrow_canonical_term_schema>;
+
+export const narrow_term_schema = z.union([
+ narrow_canonical_term_schema,
+ z.object({
+ negated: z.optional(z.boolean()),
+ operator: narrow_legacy_operator_schema,
+ operand: z.string(),
+ }),
+]);
export type NarrowTerm = z.output<typeof narrow_term_schema>;
export const custom_profile_field_schema = z.object({
diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjs
index 21011de9f35..304b1b5782c 100644
--- a/web/tests/filter.test.cjs
+++ b/web/tests/filter.test.cjs
@@ -971,17 +971,17 @@ test("redundancies", () => {
});
test("canonicalization", () => {
- assert.equal(Filter.canonicalize_operator("Is"), "is");
- assert.equal(Filter.canonicalize_operator("Stream"), "channel");
- assert.equal(Filter.canonicalize_operator("Subject"), "topic");
- assert.equal(Filter.canonicalize_operator("FROM"), "sender");
+ assert.equal(Filter.canonicalize_operator("is"), "is");
+ assert.equal(Filter.canonicalize_operator("stream"), "channel");
+ assert.equal(Filter.canonicalize_operator("subject"), "topic");
+ assert.equal(Filter.canonicalize_operator("from"), "sender");
let term;
- term = Filter.canonicalize_term({operator: "Stream", operand: "Denmark"});
+ term = Filter.canonicalize_term({operator: "stream", operand: "Denmark"});
assert.equal(term.operator, "channel");
assert.equal(term.operand, "Denmark");
- term = Filter.canonicalize_term({operator: "Channel", operand: "Denmark"});
+ term = Filter.canonicalize_term({operator: "channel", operand: "Denmark"});
assert.equal(term.operator, "channel");
assert.equal(term.operand, "Denmark");
@@ -1607,6 +1607,10 @@ test("parse", () => {
{operator: "is", operand: "starred"},
];
_test();
+
+ string = "https://www.google.com";
+ terms = [{operator: "search", operand: "https://www.google.com"}];
+ _test();
});
test("unparse", () => {
@@ -1772,8 +1776,11 @@ test("describe", ({mock_template, override}) => {
string = "invalid something_we_do_not_support operand for is operator";
assert.equal(Filter.search_description_as_html(narrow, false), string);
- // this should be unreachable, but just in case
- narrow = [{operator: "bogus", operand: "foo"}];
+ // All the `is` operands are handled in search_suggestions. This is just
+ // for coverage.
+ assert.equal(Filter.operator_to_prefix("is"), "messages that are");
+
+ narrow = [{operator: "with", operand: 12}];
string = "unknown operator";
assert.equal(Filter.search_description_as_html(narrow, false), string);