Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search: Allow adding users to dm-including pills.
Evy Kassirer2a7368e1e689
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 369eb30f70e..b757906b8c0 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -207,15 +207,15 @@ function message_matches_search_term(message: Message, operator: string, operand}case "dm-including": {- const operand_user = people.get_by_email(operand);- if (operand_user === undefined) {+ const operand_ids = people.pm_with_operand_ids(operand);+ if (!operand_ids) {return false;}const user_ids = people.all_user_ids_in_pm(message);if (!user_ids) {return false;}- return user_ids.includes(operand_user.user_id);+ return operand_ids.every((operand_id) => user_ids.includes(operand_id));}}diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex 3555803e4c7..9da3d508156 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -329,7 +329,20 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str}export function get_narrow_for_message_fetch(filter: Filter): string {- let narrow_data = filter.public_terms();+ let narrow_data: NarrowTerm[] = [];+ for (const term of filter.public_terms()) {+ if (term.operator === "dm-including") {+ for (const operand of term.operand.split(",")) {+ narrow_data.push({+ ...term,+ operand,+ });+ }+ } else {+ narrow_data.push(term);+ }+ }+if (page_params.narrow !== undefined) {narrow_data = [...narrow_data, ...page_params.narrow];}diff --git a/web/src/narrow_banner.ts b/web/src/narrow_banner.tsindex 6517fdc48bf..88ba97bf2fe 100644--- a/web/src/narrow_banner.ts+++ b/web/src/narrow_banner.ts@@ -466,13 +466,30 @@ export function pick_empty_narrow_banner(current_filter: Filter): NarrowBannerDa};}case "dm-including": {- const person_in_dms = people.get_by_email(first_operand);- if (!person_in_dms) {+ const people_in_dms = first_operand+ .split(",")+ .map((email) => people.get_by_email(email));+ if (people_in_dms.length === 1 && !people_in_dms[0]) {return {title: $t({defaultMessage: "This user does not exist!"}),};}- const person_id_string = person_in_dms.user_id.toString();+ const valid_people_in_dms: people.User[] = [];+ for (const user of people_in_dms.values()) {+ if (user === undefined) {+ return {+ // We intentionally give the same non-specific+ // error message as the single user case,+ // since we don't display API email addresses+ // or user IDs typically in UI errors, and we+ // don't have any other handle as to which+ // user this was supposed to be.+ title: $t({defaultMessage: "This user does not exist!"}),+ };+ }+ valid_people_in_dms.push(user);+ }+ const person_id_string = valid_people_in_dms.map((user) => user.user_id).join(",");const direct_message_error_string =compose_validate.check_dm_permissions_and_get_error_string(person_id_string);if (direct_message_error_string) {@@ -489,7 +506,10 @@ export function pick_empty_narrow_banner(current_filter: Filter): NarrowBannerDa),};}- if (people.is_my_user_id(person_in_dms.user_id)) {+ if (+ valid_people_in_dms.length === 1 &&+ people.is_my_user_id(valid_people_in_dms[0]!.user_id)+ ) {return {title: $t({defaultMessage: "You don't have any direct message conversations yet.",@@ -499,9 +519,9 @@ export function pick_empty_narrow_banner(current_filter: Filter): NarrowBannerDareturn {title: $t({- defaultMessage: "You have no direct messages including {person} yet.",+ defaultMessage: "You have no direct messages including {people} yet.",},- {person: person_in_dms.full_name},+ {people: valid_people_in_dms.map((user) => user.full_name).join(", ")},),};}diff --git a/web/src/search_pill.ts b/web/src/search_pill.tsindex aa71861b9f5..deabc823540 100644--- a/web/src/search_pill.ts+++ b/web/src/search_pill.ts@@ -386,7 +386,8 @@ function get_search_operand(item: SearchPill, for_display: boolean): string {export function get_current_search_pill_terms(pill_widget: SearchPillWidget): NarrowTerm[] {return pill_widget.items().map((item) => ({- ...item,+ operator: item.operator,operand: get_search_operand(item, false),+ negated: item.negated,}));}diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 0960503fd44..52b0214d393 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -218,114 +218,116 @@ function get_channel_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggest});}-function get_group_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestion[] {- // We only suggest groups once a term with a valid user already exists- if (terms.length === 0) {- return [];- }- const last_complete_term = terms.at(-1)!;- // For users with "pm-with" in their muscle memory, still- // have group direct message suggestions with "dm:" operator.- if (- !check_validity(- last_complete_term,- terms.slice(-1),- ["dm", "pm-with"],- [{operator: "channel"}],- )- ) {- return [];- }-- // If they started typing since a user pill, we'll parse that as "search"- // but they might actually want to parse that as a user instead to add to- // the most recent pill. So we shuffle some things around to support that.- if (last.operator === "search") {- const text_input = last.operand;- const operand = `${last_complete_term.operand},${text_input}`;- last = {- ...last_complete_term,- operand,- };- terms = terms.slice(-1);- } else if (last.operator === "") {- last = last_complete_term;- } else {- // If they already started another term with an other operator, we're- // no longer dealing with a group DM situation.- return [];- }-- const operand = last.operand;- const negated = last.negated;-- // The operand has the form "part1,part2,pa", where all but the last part- // are emails, and the last part is an arbitrary query.- //- // We only generate group suggestions when there's more than one part, and- // we only use the last part to generate suggestions.-- const last_comma_index = operand.lastIndexOf(",");- let all_but_last_part;- let last_part;- if (last_comma_index === -1) {- all_but_last_part = operand;- last_part = "";- } else {- // Neither all_but_last_part nor last_part include the final comma.- all_but_last_part = operand.slice(0, last_comma_index);- last_part = operand.slice(last_comma_index + 1);- }+function get_group_suggestions(+ group_operator: "dm" | "dm-including",+): (last: NarrowTerm, terms: NarrowTerm[]) => Suggestion[] {+ return (last: NarrowTerm, terms: NarrowTerm[]): Suggestion[] => {+ // We only suggest groups once a term with a valid user already exists+ if (terms.length === 0) {+ return [];+ }+ const last_complete_term = terms.at(-1)!;+ // For users with "pm-with" in their muscle memory, still+ // have group direct message suggestions with "dm:" operator.+ const valid_terms = group_operator === "dm" ? ["dm", "pm-with"] : [group_operator];+ if (+ !check_validity(last_complete_term, terms.slice(-1), valid_terms, [+ {operator: "channel"},+ ])+ ) {+ return [];+ }- // We don't suggest a person if their email is already present in the- // operand (not including the last part).- const parts = [...all_but_last_part.split(","), people.my_current_email()];-- const all_users_but_last_part = [];- for (const email of all_but_last_part.split(",")) {- const user = people.get_by_email(email);- // Somehow an invalid email is showing up earlier in the group.- // This can happen if e.g. the user manually enters multiple emails.- // We won't have group suggestions built from an invalid user, so- // return an empty list.- if (user === undefined) {+ // If they started typing since a user pill, we'll parse that as "search"+ // but they might actually want to parse that as a user instead to add to+ // the most recent pill. So we shuffle some things around to support that.+ if (last.operator === "search") {+ const text_input = last.operand;+ const operand = `${last_complete_term.operand},${text_input}`;+ last = {+ ...last_complete_term,+ operand,+ };+ terms = terms.slice(-1);+ } else if (last.operator === "") {+ last = last_complete_term;+ } else {+ // If they already started another term with an other operator, we're+ // no longer dealing with a group DM situation.return [];}- all_users_but_last_part.push(user);- }- const person_matcher = people.build_person_matcher(last_part);- let persons = people.filter_all_persons((person) => {- if (parts.includes(person.email)) {- return false;+ const operand = last.operand;+ const negated = last.negated;++ // The operand has the form "part1,part2,pa", where all but the last part+ // are emails, and the last part is an arbitrary query.+ //+ // We only generate group suggestions when there's more than one part, and+ // we only use the last part to generate suggestions.++ const last_comma_index = operand.lastIndexOf(",");+ let all_but_last_part;+ let last_part;+ if (last_comma_index === -1) {+ all_but_last_part = operand;+ last_part = "";+ } else {+ // Neither all_but_last_part nor last_part include the final comma.+ all_but_last_part = operand.slice(0, last_comma_index);+ last_part = operand.slice(last_comma_index + 1);}- return last_part === "" || person_matcher(person);- });- persons.sort(compare_by_direct_message_group(parts));+ // We don't suggest a person if their email is already present in the+ // operand (not including the last part).+ const parts = [...all_but_last_part.split(","), people.my_current_email()];++ const all_users_but_last_part = [];+ for (const email of all_but_last_part.split(",")) {+ const user = people.get_by_email(email);+ // Somehow an invalid email is showing up earlier in the group.+ // This can happen if e.g. the user manually enters multiple emails.+ // We won't have group suggestions built from an invalid user, so+ // return an empty list.+ if (user === undefined) {+ return [];+ }+ all_users_but_last_part.push(user);+ }- // Take top 15 persons, since they're ordered by pm_recipient_count.- persons = persons.slice(0, 15);+ const person_matcher = people.build_person_matcher(last_part);+ let persons = people.filter_all_persons((person) => {+ if (parts.includes(person.email)) {+ return false;+ }+ return last_part === "" || person_matcher(person);+ });- const prefix = Filter.operator_to_prefix("dm", negated);+ persons.sort(compare_by_direct_message_group(parts));- return persons.map((person) => {- const term: NarrowTerm = {- operator: "dm",- operand: all_but_last_part + "," + person.email,- negated,- };+ // Take top 15 persons, since they're ordered by pm_recipient_count.+ persons = persons.slice(0, 15);- let terms: NarrowTerm[] = [term];- if (negated) {- terms = [{operator: "is", operand: "dm"}, term];- }+ const prefix = Filter.operator_to_prefix(group_operator, negated);- return {- description_html: prefix,- search_string: Filter.unparse(terms),- };- });+ return persons.map((person) => {+ const term: NarrowTerm = {+ operator: group_operator,+ operand: all_but_last_part + "," + person.email,+ negated,+ };++ let terms: NarrowTerm[] = [term];+ if (group_operator === "dm" && negated) {+ terms = [{operator: "is", operand: "dm"}, term];+ }++ return {+ description_html: prefix,+ search_string: Filter.unparse(terms),+ };+ });+ };}function make_people_getter(last: NarrowTerm): () => User[] {@@ -938,7 +940,8 @@ class Attacher {const last_base_string = last_base_term.search_string;const new_search_string = suggestion.search_string;if (- new_search_string.startsWith("dm:") &&+ (new_search_string.startsWith("dm:") ||+ new_search_string.startsWith("dm-including:")) &&new_search_string.includes(last_base_string)) {suggestion_line = [...this.base.slice(0, -1), suggestion];@@ -1060,7 +1063,8 @@ export function get_search_result(// because both are valid suggestions for typing a user's// name, and if there's already has a DM pill then the// searching user probably is looking to make a group DM.- get_group_suggestions,+ get_group_suggestions("dm"),+ get_group_suggestions("dm-including"),get_channels_filter_suggestions,get_operator_suggestions,get_is_filter_suggestions,diff --git a/web/tests/message_view.test.cjs b/web/tests/message_view.test.cjsindex 855091f6817..1d299f10777 100644--- a/web/tests/message_view.test.cjs+++ b/web/tests/message_view.test.cjs@@ -511,6 +511,13 @@ run_test("show_empty_narrow_message", ({mock_template, override}) => {empty_narrow_html("translated: This user does not exist!"),);+ current_filter = set_filter([["dm-including", "[email protected],[email protected]"]]);+ narrow_banner.show_empty_narrow_message(current_filter);+ assert.equal(+ $(".empty_feed_notice_main").html(),+ empty_narrow_html("translated: User [email protected] does not exist!"),+ );+current_filter = set_filter([["dm-including", "[email protected]"]]);narrow_banner.show_empty_narrow_message(current_filter);assert.equal(