Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
filter: Accept full names for the dm and dm-including search operators.
Aman Agrawalad83886fc5be
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 028ed7aefdb..121c9da1420 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -263,6 +263,20 @@ function resolve_user_id_from_operand(operand: string): number | undefined {);}+// Resolve a comma-separated `dm`/`dm-including` operand to user ids, returning+// undefined if any token cannot be resolved to a user.+function resolve_user_ids_from_operand(operand: string): number[] | undefined {+ const user_ids = [];+ for (const token of operand.split(",")) {+ const user_id = resolve_user_id_from_operand(token);+ if (user_id === undefined) {+ return undefined;+ }+ user_ids.push(user_id);+ }+ return user_ids;+}+function convert_single_user_id_suggestion_to_term(suggestion: NarrowTermSuggestion,canonical_operator: "sender" | "mentions",@@ -547,15 +561,19 @@ export class Filter {switch (canonical_operator) {case "dm":case "dm-including": {+ // An empty operand is invalid for dm and dm-including.+ if (suggestion.operand === "") {+ return undefined;+ }let operand: number[];if (suggestion.operand.toLowerCase() === "me") {operand = [people.my_current_user_id()];} else {- operand = people.user_ids_string_to_ids_array(suggestion.operand);- }- // An empty operand is invalid for dm and dm-including.- if (suggestion.operand === "") {- return undefined;+ const user_ids = resolve_user_ids_from_operand(suggestion.operand);+ if (user_ids === undefined) {+ return undefined;+ }+ operand = user_ids;}potential_narrow_term = {operator: canonical_operator,diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjsindex 227e7b76929..cdeebfa55c3 100644--- a/web/tests/filter.test.cjs+++ b/web/tests/filter.test.cjs@@ -80,6 +80,25 @@ const karl = make_user({full_name: "Karl Stolley",});+const shubham = make_user({+ email: "[email protected]",+ user_id: 37,+ full_name: "Shubham Padia",+});++// Two users sharing a full name, to exercise duplicate-name handling.+const sam_one = make_user({+ email: "[email protected]",+ user_id: 38,+ full_name: "Sam Park",+});++const sam_two = make_user({+ email: "[email protected]",+ user_id: 39,+ full_name: "Sam Park",+});+// Add users to `valid_user_ids`.const source = "server_events";people.add_active_user(me, source);@@ -89,6 +108,9 @@ 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.add_active_user(shubham, source);+people.add_active_user(sam_one, source);+people.add_active_user(sam_two, source);people.initialize_current_user(me.user_id);state_data.set_current_user(me);muted_users.add_muted_user(jeff.user_id);@@ -2288,6 +2310,35 @@ test("convert_suggestion_to_term", () => {// 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}],+ // dm and dm-including resolve a comma-separated list of names to the+ // corresponding user ids.+ [`dm:"${karl.full_name}"`, true, {operator: "dm", operand: [karl.user_id]}],+ [+ `dm:"${karl.full_name}, ${shubham.full_name}"`,+ true,+ {operator: "dm", operand: [karl.user_id, shubham.user_id]},+ ],+ [+ `dm-including:"${karl.full_name}, ${shubham.full_name}"`,+ true,+ {operator: "dm-including", operand: [karl.user_id, shubham.user_id]},+ ],+ // A name that matches no user is invalid.+ ['dm:"No Such Person"', false],+ // An ambiguous (duplicate) full name is invalid on its own...+ [`dm:"${sam_one.full_name}"`, false],+ // ...but the `Full Name|user_id` form disambiguates it, resolving to+ // exactly the requested user and not the other duplicate.+ [+ `dm:"${sam_one.full_name}|${sam_one.user_id}"`,+ true,+ {operator: "dm", operand: [sam_one.user_id]},+ ],+ [+ `dm:"${sam_two.full_name}|${sam_two.user_id}"`,+ true,+ {operator: "dm", operand: [sam_two.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]);diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjsindex 35fb717ee27..897441642cf 100644--- a/web/tests/search_suggestion.test.cjs+++ b/web/tests/search_suggestion.test.cjs@@ -100,11 +100,29 @@ 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.+ // e.g. `sender:Elena García` and the multi-user `dm:Bo Lin, 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 each 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}`]);+ assert.deepEqual(get_suggestions("dm:Ted Smith"), [`dm:${ted.user_id}`]);+ assert.deepEqual(get_suggestions("dm-including:Ted Smith"), [`dm-including:${ted.user_id}`]);++ // dm and dm-including resolve a comma-separated list of names, with or+ // without a space after the comma.+ assert.deepEqual(get_suggestions("dm:Ted Smith, Alice Ignore"), [+ `dm:${ted.user_id},${alice.user_id}`,+ ]);+ assert.deepEqual(get_suggestions("dm:Ted Smith,Alice Ignore"), [+ `dm:${ted.user_id},${alice.user_id}`,+ ]);+ assert.deepEqual(get_suggestions("dm-including:Ted Smith, Alice Ignore"), [+ `dm-including:${ted.user_id},${alice.user_id}`,+ ]);+ assert.deepEqual(get_suggestions("dm-including:Ted Smith,Alice Ignore"), [+ `dm-including:${ted.user_id},${alice.user_id}`,+ ]);});test("basic_get_suggestions", ({override}) => {