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: Accept full names for the sender and mentions search operators.

Aman Agrawal4f8f178dad37
diff --git a/web/src/filter.ts b/web/src/filter.ts
index 0701b49df36..028ed7aefdb 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -247,12 +247,28 @@ const USER_OPERATORS = new Set([
"group-pm-with",
]);
+// Resolve a single user-operator operand token to a user id. The token can be
+// a numeric user id, a unique full name, or a `Full Name|user_id` string that
+// disambiguates duplicate names (the same form we accept for mentions). The
+// resulting id's validity as a real user is verified later by
+// `is_valid_canonical_term`.
+function resolve_user_id_from_operand(operand: string): number | undefined {
+ operand = operand.trim();
+ const numeric_user_id = Number(operand);
+ if (operand !== "" && !Number.isNaN(numeric_user_id)) {
+ return numeric_user_id;
+ }
+ return (
+ people.get_from_unique_full_name(operand)?.user_id ?? people.get_user_id_from_name(operand)
+ );
+}
+
function convert_single_user_id_suggestion_to_term(
suggestion: NarrowTermSuggestion,
canonical_operator: "sender" | "mentions",
): NarrowCanonicalTerm | undefined {
- const operand = Number(suggestion.operand);
- if (Number.isNaN(operand)) {
+ const operand = resolve_user_id_from_operand(suggestion.operand);
+ if (operand === undefined) {
return undefined;
}
return {
diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjs
index 1b694a84c77..227e7b76929 100644
--- a/web/tests/filter.test.cjs
+++ b/web/tests/filter.test.cjs
@@ -73,6 +73,13 @@ const annie = make_user({
role: Role.GUEST,
});
+// Multi-word names, to exercise resolving typed full names to user ids.
+const karl = make_user({
+ email: "[email protected]",
+ user_id: 36,
+ full_name: "Karl Stolley",
+});
+
// Add users to `valid_user_ids`.
const source = "server_events";
people.add_active_user(me, source);
@@ -81,6 +88,7 @@ people.add_active_user(steve, source);
people.add_active_user(alice, source);
people.add_active_user(jeff, source);
people.add_active_user(annie, source);
+people.add_active_user(karl, source);
people.initialize_current_user(me.user_id);
state_data.set_current_user(me);
muted_users.add_muted_user(jeff.user_id);
@@ -2274,12 +2282,19 @@ test("convert_suggestion_to_term", () => {
["mentions:me", true],
["mentions:-1", false],
["mentions:invalid", false],
- ];
- for (const [search_term_string, expected_is_valid] of test_data) {
- assert.equal(
- Filter.convert_suggestion_to_term(Filter.parse(search_term_string)[0]) !== undefined,
- expected_is_valid,
- );
+ // A typed full name resolves to a user id, matching the help center
+ // docs. Quotes keep a multi-word name a single operand through the
+ // parser. A third tuple element pins down the resolved term, so a
+ // name maps to the right user id and not merely to some valid term.
+ [`sender:"${karl.full_name}"`, true, {operator: "sender", operand: karl.user_id}],
+ [`mentions:"${karl.full_name}"`, true, {operator: "mentions", operand: karl.user_id}],
+ ];
+ for (const [search_term_string, expected_is_valid, expected_term] of test_data) {
+ const term = Filter.convert_suggestion_to_term(Filter.parse(search_term_string)[0]);
+ assert.equal(term !== undefined, expected_is_valid);
+ if (expected_term !== undefined) {
+ assert.deepEqual(term, {negated: false, ...expected_term});
+ }
}
// Invalid operator.
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index 986087d3d86..35fb717ee27 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -98,6 +98,15 @@ function test(label, f) {
});
}
+test("person_multi_word_completion", () => {
+ // The help center documents searching person operators by full name,
+ // e.g. `sender:Elena García`. An unquoted multi-word name parses with a
+ // trailing `search:` term, which get_suggestions merges back into the
+ // person operand before resolving the name to a user id.
+ assert.deepEqual(get_suggestions("sender:Ted Smith"), [`sender:${ted.user_id}`]);
+ assert.deepEqual(get_suggestions("mentions:Ted Smith"), [`mentions:${ted.user_id}`]);
+});
+
test("basic_get_suggestions", ({override}) => {
const query = "fred";