Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
pill_typeahead: Differentiate between pills with type attribute.
evykassirer45e9c046d8ef
diff --git a/web/src/composebox_typeahead.js b/web/src/composebox_typeahead.jsindex 95c2f1b0133..eab820e6870 100644--- a/web/src/composebox_typeahead.js+++ b/web/src/composebox_typeahead.js@@ -521,8 +521,16 @@ export function get_person_suggestions(query, opts) {if (opts.want_broadcast) {persons = [...persons, ...broadcast_mentions()];}-- return persons.filter((item) => typeahead_helper.query_matches_person(query, item));+ // `sort_recipients` and other functions like `user_pill.get_user_ids`+ // are shared with the pill typeahead which has only users, and we+ // need a way to differentiate these mentons-or-users from just users,+ // to help with typing.+ const person_items = persons.map((person) => ({+ ...person,+ type: "user_or_mention",+ }));++ return person_items.filter((item) => typeahead_helper.query_matches_person(query, item));}let groups;diff --git a/web/src/pill_typeahead.js b/web/src/pill_typeahead.jsindex cbd04bc04df..ed68ff471af 100644--- a/web/src/pill_typeahead.js+++ b/web/src/pill_typeahead.js@@ -57,7 +57,11 @@ export function set_up($input, pills, opts) {// If user_source is specified in opts, it// is given priority. Otherwise we use// default user_pill.typeahead_source.- source = [...source, ...opts.user_source()];+ const users = opts.user_source().map((user) => ({+ ...user,+ type: "user",+ }));+ source = [...source, ...users];} else {source = [...source, ...user_pill.typeahead_source(pills, exclude_bots)];}@@ -65,11 +69,11 @@ export function set_up($input, pills, opts) {return source;},highlighter_html(item, query) {- if (include_streams(query)) {+ if (include_streams(query) && item.type === "stream") {return typeahead_helper.render_stream(item);}- if (include_user_groups && user_groups.is_user_group(item)) {+ if (include_user_groups && item.type === "user_group") {return typeahead_helper.render_user_group(item);}@@ -83,17 +87,17 @@ export function set_up($input, pills, opts) {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- if (include_streams(query)) {+ if (include_streams(query) && item.type === "stream") {query = query.trim().slice(1);return item.name.toLowerCase().includes(query);}let matches = false;- if (include_user_groups) {+ if (include_user_groups && item.type === "user_group") {matches = matches || group_matcher(query, item);}- if (include_users) {+ if (include_users && item.type === "user") {matches = matches || person_matcher(query, item);}return matches;@@ -103,15 +107,24 @@ export function set_up($input, pills, opts) {return typeahead_helper.sort_streams(matches, query.trim().slice(1));}- let users = [];+ const users = [];if (include_users) {- users = matches.filter((ele) => people.is_known_user(ele));+ for (const match of matches) {+ if (match.type === "user" && people.is_known_user(match)) {+ users.push(match);+ }+ }}- let groups;+ const groups = [];if (include_user_groups) {- groups = matches.filter((ele) => user_groups.is_user_group(ele));+ for (const match of matches) {+ if (match.type === "user_group") {+ groups.push(match);+ }+ }}+return typeahead_helper.sort_recipients({users,query,@@ -122,11 +135,11 @@ export function set_up($input, pills, opts) {});},updater(item, query) {- if (include_streams(query)) {+ if (include_streams(query) && item.type === "stream") {stream_pill.append_stream(item, pills);- } else if (include_user_groups && user_groups.is_user_group(item)) {+ } else if (include_user_groups && item.type === "user_group") {user_group_pill.append_user_group(item, pills);- } else if (include_users && people.is_known_user(item)) {+ } else if (include_users && item.type === "user" && people.is_known_user(item)) {user_pill.append_user(item, pills);}diff --git a/web/src/stream_pill.ts b/web/src/stream_pill.tsindex b21e0295ae1..db6b819ecdb 100644--- a/web/src/stream_pill.ts+++ b/web/src/stream_pill.ts@@ -10,6 +10,8 @@ type StreamPill = {type StreamPillWidget = InputPillContainer<StreamPill>;+export type StreamPillData = StreamSubscription & {type: "stream"};+function display_pill(sub: StreamSubscription): string {const sub_count = peer_data.get_subscriber_count(sub.stream_id);return "#" + sub.name + ": " + sub_count + " users";@@ -97,7 +99,10 @@ export function filter_taken_streams(return items;}-export function typeahead_source(pill_widget: StreamPillWidget): StreamSubscription[] {+export function typeahead_source(pill_widget: StreamPillWidget): StreamPillData[] {const potential_streams = stream_data.get_unsorted_subs();- return filter_taken_streams(potential_streams, pill_widget);+ return filter_taken_streams(potential_streams, pill_widget).map((stream) => ({+ ...stream,+ type: "stream",+ }));}diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex 61432a6f9c7..d2575dda24b 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -15,14 +15,18 @@ import * as recent_senders from "./recent_senders";import {realm} from "./state_data";import * as stream_data from "./stream_data";import * as stream_list_sort from "./stream_list_sort";+import type {StreamPillData} from "./stream_pill";import type {StreamSubscription} from "./sub_store";+import type {UserGroupPillData} from "./user_group_pill";import * as user_groups from "./user_groups";import type {UserGroup} from "./user_groups";+import type {UserPillData} from "./user_pill";import * as user_status from "./user_status";import type {UserStatusEmojiInfo} from "./user_status";import * as util from "./util";export type UserOrMention = PseudoMentionUser | (User & {is_broadcast: undefined});+export type UserOrMentionPillData = UserOrMention & {type: "user_or_mention"};// Returns an array of direct message recipients, removing empty elements.// For example, "a,,b, " => ["a", "b"]@@ -241,8 +245,8 @@ export function compare_by_pms(user_a: User, user_b: User): number {}export function compare_people_for_relevance(- person_a: UserOrMention,- person_b: UserOrMention,+ person_a: UserOrMentionPillData | UserPillData,+ person_b: UserOrMentionPillData | UserPillData,compare_by_current_conversation?: (user_a: User, user_b: User) => number,current_stream_id?: number,): number {@@ -250,27 +254,35 @@ export function compare_people_for_relevance(// We use is_broadcast for a quick check. It will// true for all/everyone/stream and undefined (falsy)// for actual people.+ const person_a_is_broadcast = person_a.type === "user_or_mention" && person_a.is_broadcast;+ const person_b_is_broadcast = person_b.type === "user_or_mention" && person_b.is_broadcast;if (compose_state.get_message_type() !== "private") {- if (person_a.is_broadcast) {- if (person_b.is_broadcast) {+ if (person_a_is_broadcast) {+ if (person_b_is_broadcast) {return person_a.idx - person_b.idx;}return -1;- } else if (person_b.is_broadcast) {+ } else if (person_b_is_broadcast) {return 1;}} else {- if (person_a.is_broadcast) {- if (person_b.is_broadcast) {+ if (person_a_is_broadcast) {+ if (person_b_is_broadcast) {return person_a.idx - person_b.idx;}return 1;- } else if (person_b.is_broadcast) {+ } else if (person_b_is_broadcast) {return -1;}}// Now handle actual people users.+ assert(+ (person_a.type === "user_or_mention" && !person_a.is_broadcast) || person_a.type === "user",+ );+ assert(+ (person_b.type === "user_or_mention" && !person_b.is_broadcast) || person_b.type === "user",+ );// give preference to subscribed users firstif (current_stream_id !== undefined) {@@ -294,11 +306,11 @@ export function compare_people_for_relevance(return compare_by_pms(person_a, person_b);}-export function sort_people_for_relevance(- objs: UserOrMention[],+export function sort_people_for_relevance<UserType extends UserOrMentionPillData | UserPillData>(+ objs: UserType[],current_stream_id?: number,current_topic?: string,-): UserOrMention[] {+): UserType[] {// If sorting for recipientbox typeahead and not viewing a stream / topic, then current_stream = ""let current_stream = null;if (current_stream_id) {@@ -413,7 +425,7 @@ export function sort_languages(matches: string[], query: string): string[] {return retain_unique_language_aliases([...results.matches, ...results.rest]);}-export function sort_recipients({+export function sort_recipients<UserType extends UserOrMentionPillData | UserPillData>({users,query,current_stream_id,@@ -421,14 +433,14 @@ export function sort_recipients({groups = [],max_num_items = 20,}: {- users: UserOrMention[];+ users: UserType[];query: string;current_stream_id?: number;current_topic?: string;- groups?: UserGroup[];+ groups?: UserGroupPillData[];max_num_items?: number;-}): (UserOrMention | UserGroup)[] {- function sort_relevance(items: UserOrMention[]): UserOrMention[] {+}): (UserType | UserGroupPillData)[] {+ function sort_relevance(items: UserType[]): UserType[] {return sort_people_for_relevance(items, current_stream_id, current_topic);}@@ -460,25 +472,25 @@ export function sort_recipients({];const groups_okay_matches = [...groups_results.word_boundary_matches];- const best_users = (): UserOrMention[] => [+ const best_users = (): UserType[] => [...sort_relevance(users_name_good_matches),...sort_relevance(users_name_okay_matches),];- const best_groups = (): UserGroup[] => [...groups_good_matches, ...groups_okay_matches];- const ok_users = (): UserOrMention[] => [+ const best_groups = (): UserGroupPillData[] => [...groups_good_matches, ...groups_okay_matches];+ const ok_users = (): UserType[] => [...sort_relevance(email_good_matches),...sort_relevance(email_okay_matches),];- const worst_users = (): UserOrMention[] => sort_relevance(email_results.no_matches);- const worst_groups = (): UserGroup[] => groups_results.no_matches;+ const worst_users = (): UserType[] => sort_relevance(email_results.no_matches);+ const worst_groups = (): UserGroupPillData[] => groups_results.no_matches;const getters: (| {- getter: () => UserOrMention[];+ getter: () => UserType[];type: "users";}| {- getter: () => UserGroup[];+ getter: () => UserGroupPillData[];type: "groups";})[] = [@@ -506,22 +518,23 @@ export function sort_recipients({// We suggest only the first matching stream wildcard mention,// irrespective of how many equivalent stream wildcard mentions match.- const recipients: (UserOrMention | UserGroup)[] = [];+ const recipients: (UserType | UserGroupPillData)[] = [];let stream_wildcard_mention_included = false;- function add_user_recipients(items: UserOrMention[]): void {+ function add_user_recipients(items: UserType[]): void {for (const item of items) {+ const item_is_broadcast = item.type === "user_or_mention" && item.is_broadcast;const topic_wildcard_mention = item.email === "topic";- if (!item.is_broadcast || topic_wildcard_mention || !stream_wildcard_mention_included) {+ if (!item_is_broadcast || topic_wildcard_mention || !stream_wildcard_mention_included) {recipients.push(item);- if (item.is_broadcast && !topic_wildcard_mention) {+ if (item_is_broadcast && !topic_wildcard_mention) {stream_wildcard_mention_included = true;}}}}- function add_group_recipients(items: UserGroup[]): void {+ function add_group_recipients(items: UserGroupPillData[]): void {for (const item of items) {recipients.push(item);}@@ -622,7 +635,7 @@ export function compare_by_activity(return util.strcmp(stream_a.name, stream_b.name);}-export function sort_streams(matches: StreamSubscription[], query: string): StreamSubscription[] {+export function sort_streams(matches: StreamPillData[], query: string): StreamPillData[] {const name_results = typeahead.triage(query, matches, (x) => x.name, compare_by_activity);const desc_results = typeahead.triage(query,diff --git a/web/src/user_group_pill.ts b/web/src/user_group_pill.tsindex d275f7d7696..5d069f7572a 100644--- a/web/src/user_group_pill.ts+++ b/web/src/user_group_pill.ts@@ -9,6 +9,8 @@ type UserGroupPill = {type UserGroupPillWidget = InputPillContainer<UserGroupPill>;+export type UserGroupPillData = UserGroup & {type: "user_group"};+function display_pill(group: UserGroup): string {return `${group.name}: ${group.members.size} users`;}@@ -86,7 +88,10 @@ export function filter_taken_groups(return items;}-export function typeahead_source(pill_widget: UserGroupPillWidget): UserGroup[] {+export function typeahead_source(pill_widget: UserGroupPillWidget): UserGroupPillData[] {const groups = user_groups.get_realm_user_groups();- return filter_taken_groups(groups, pill_widget);+ return filter_taken_groups(groups, pill_widget).map((user_group) => ({+ ...user_group,+ type: "user_group",+ }));}diff --git a/web/src/user_pill.ts b/web/src/user_pill.tsindex 433249dacba..6557af28ce5 100644--- a/web/src/user_pill.ts+++ b/web/src/user_pill.ts@@ -16,6 +16,8 @@ type UserPill = {export type UserPillWidget = InputPillContainer<UserPill>;+export type UserPillData = User & {type: "user"};+export function create_item_from_email(email: string,current_items: InputPillItem<UserPill>[],@@ -128,9 +130,15 @@ export function has_unconverted_data(pill_widget: UserPillWidget): boolean {return has_unknown_items;}-export function typeahead_source(pill_widget: UserPillWidget, exclude_bots?: boolean): User[] {+export function typeahead_source(+ pill_widget: UserPillWidget,+ exclude_bots?: boolean,+): UserPillData[] {const users = exclude_bots ? people.get_realm_active_human_users() : people.get_realm_users();- return filter_taken_users(users, pill_widget);+ return filter_taken_users(users, pill_widget).map((user) => ({+ ...user,+ type: "user",+ }));}export function filter_taken_users(items: User[], pill_widget: UserPillWidget): User[] {diff --git a/web/tests/composebox_typeahead.test.js b/web/tests/composebox_typeahead.test.jsindex 4f3cce6b458..2a07b5c7e8c 100644--- a/web/tests/composebox_typeahead.test.js+++ b/web/tests/composebox_typeahead.test.js@@ -61,6 +61,13 @@ const ct = composebox_typeahead;// broadcast-mentions/persons/groups.ct.__Rewire__("max_num_items", 15);+function user_or_mention_item(item) {+ return {+ ...item,+ type: "user_or_mention",+ };+}+run_test("verify wildcard mentions typeahead for stream message", () => {compose_state.set_message_type("stream");const mention_all = ct.broadcast_mentions()[0];@@ -285,6 +292,7 @@ const ali = {full_name: "Ali",is_moderator: false,};+const ali_item = user_or_mention_item(ali);const alice = {email: "[email protected]",@@ -292,6 +300,7 @@ const alice = {full_name: "Alice",is_moderator: false,};+const alice_item = user_or_mention_item(alice);const hamlet = {email: "[email protected]",@@ -299,6 +308,7 @@ const hamlet = {full_name: "King Hamlet",is_moderator: false,};+const hamlet_item = user_or_mention_item(hamlet);const othello = {email: "[email protected]",@@ -307,24 +317,31 @@ const othello = {is_moderator: false,delivery_email: null,};+const othello_item = user_or_mention_item(othello);+const cordelia = {email: "[email protected]",user_id: 102,full_name: "Cordelia, Lear's daughter",is_moderator: false,};+const cordelia_item = user_or_mention_item(cordelia);+const deactivated_user = {email: "[email protected]",user_id: 103,full_name: "Deactivated User",is_moderator: false,};+const deactivated_user_item = user_or_mention_item(deactivated_user);+const lear = {email: "[email protected]",user_id: 104,full_name: "King Lear",is_moderator: false,};+const lear_item = user_or_mention_item(lear);const twin1 = {full_name: "Mark Twin",@@ -332,6 +349,7 @@ const twin1 = {user_id: 105,email: "[email protected]",};+const twin1_item = user_or_mention_item(twin1);const twin2 = {full_name: "Mark Twin",@@ -339,6 +357,7 @@ const twin2 = {user_id: 106,email: "[email protected]",};+const twin2_item = user_or_mention_item(twin2);const gael = {full_name: "Gaël Twin",@@ -346,6 +365,7 @@ const gael = {user_id: 107,email: "[email protected]",};+const gael_item = user_or_mention_item(gael);const hal = {full_name: "Earl Hal",@@ -353,6 +373,7 @@ const hal = {user_id: 108,email: "[email protected]",};+const hal_item = user_or_mention_item(hal);const harry = {full_name: "Harry",@@ -360,6 +381,7 @@ const harry = {user_id: 109,email: "[email protected]",};+const harry_item = user_or_mention_item(harry);const hamletcharacters = {name: "hamletcharacters",@@ -851,17 +873,17 @@ test("initialize", ({override, override_rewire, mock_template}) => {// This should match the users added at the beginning of this test file.let actual_value = options.source("");let expected_value = [- ali,- alice,- cordelia,- hal,- gael,- harry,- hamlet,- lear,- twin1,- twin2,- othello,+ user_or_mention_item(ali),+ user_or_mention_item(alice),+ user_or_mention_item(cordelia),+ user_or_mention_item(hal),+ user_or_mention_item(gael),+ user_or_mention_item(harry),+ user_or_mention_item(hamlet),+ user_or_mention_item(lear),+ user_or_mention_item(twin1),+ user_or_mention_item(twin2),+ user_or_mention_item(othello),hamletcharacters,backend,call_center,@@ -930,19 +952,19 @@ test("initialize", ({override, override_rewire, mock_template}) => {// beginning first, and then the rest of them in REVERSE order of// the input.query = "othello";- actual_value = sorter(query, [othello]);- expected_value = [othello];+ actual_value = sorter(query, [othello_item]);+ expected_value = [othello_item];assert.deepEqual(actual_value, expected_value);query = "Ali";- actual_value = sorter(query, [alice, ali]);- expected_value = [ali, alice];+ actual_value = sorter(query, [alice_item, ali_item]);+ expected_value = [ali_item, alice_item];assert.deepEqual(actual_value, expected_value);// A literal match at the beginning of an element puts it at the top.query = "co"; // Matches everything ("[email protected]")- actual_value = sorter(query, [othello, deactivated_user, cordelia]);- expected_value = [cordelia, deactivated_user, othello];+ actual_value = sorter(query, [othello_item, deactivated_user_item, cordelia_item]);+ expected_value = [cordelia_item, deactivated_user_item, othello_item];actual_value.sort((a, b) => a.user_id - b.user_id);expected_value.sort((a, b) => a.user_id - b.user_id);assert.deepEqual(actual_value, expected_value);@@ -989,7 +1011,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {assert.deepEqual(appended_names, ["King Lear"]);assert.ok(cleared);- pill_items = [{user_id: lear.user_id}];+ pill_items = [{user_id: lear.user_id, type: "user"}];appended_names = [];cleared = false;options.updater(hamletcharacters, event);@@ -1643,20 +1665,21 @@ test("filter_and_sort_mentions (normal)", () => {let suggestions = ct.filter_and_sort_mentions(is_silent, "al");const mention_all = ct.broadcast_mentions()[0];- assert.deepEqual(suggestions, [mention_all, ali, alice, hal, call_center]);+ const mention_all_item = user_or_mention_item(mention_all);+ assert.deepEqual(suggestions, [mention_all_item, ali_item, alice_item, hal_item, call_center]);// call_center group is shown in typeahead even when user is member of// one of the subgroups of can_mention_group.current_user.user_id = 104;suggestions = ct.filter_and_sort_mentions(is_silent, "al");- assert.deepEqual(suggestions, [mention_all, ali, alice, hal, call_center]);+ assert.deepEqual(suggestions, [mention_all_item, ali_item, alice_item, hal_item, call_center]);// call_center group is not shown in typeahead when user is neither// a direct member of can_mention_group nor a member of any of its// recursive subgroups.current_user.user_id = 102;suggestions = ct.filter_and_sort_mentions(is_silent, "al");- assert.deepEqual(suggestions, [mention_all, ali, alice, hal]);+ assert.deepEqual(suggestions, [mention_all_item, ali_item, alice_item, hal_item]);});test("filter_and_sort_mentions (silent)", () => {@@ -1664,14 +1687,14 @@ test("filter_and_sort_mentions (silent)", () => {let suggestions = ct.filter_and_sort_mentions(is_silent, "al");- assert.deepEqual(suggestions, [ali, alice, hal, call_center]);+ assert.deepEqual(suggestions, [ali_item, alice_item, hal_item, call_center]);// call_center group is shown in typeahead irrespective of whether// user is member of can_mention_group or its subgroups for a// silent mention.current_user.user_id = 102;suggestions = ct.filter_and_sort_mentions(is_silent, "al");- assert.deepEqual(suggestions, [ali, alice, hal, call_center]);+ assert.deepEqual(suggestions, [ali_item, alice_item, hal_item, call_center]);});test("typeahead_results", () => {@@ -1760,22 +1783,22 @@ test("typeahead_results", () => {assert_emoji_matches("notaemoji", []);// Autocomplete user mentions by user name.- assert_mentions_matches("cordelia", [cordelia]);- assert_mentions_matches("cordelia, le", [cordelia]);+ assert_mentions_matches("cordelia", [cordelia_item]);+ assert_mentions_matches("cordelia, le", [cordelia_item]);assert_mentions_matches("cordelia, le ", []);- assert_mentions_matches("moor", [othello]);- assert_mentions_matches("moor ", [othello]);- assert_mentions_matches("moor of", [othello]);- assert_mentions_matches("moor of ven", [othello]);- assert_mentions_matches("oor", [othello]);+ assert_mentions_matches("moor", [othello_item]);+ assert_mentions_matches("moor ", [othello_item]);+ assert_mentions_matches("moor of", [othello_item]);+ assert_mentions_matches("moor of ven", [othello_item]);+ assert_mentions_matches("oor", [othello_item]);assert_mentions_matches("oor ", []);assert_mentions_matches("oor o", []);assert_mentions_matches("oor of venice", []);- assert_mentions_matches("King ", [hamlet, lear]);- assert_mentions_matches("King H", [hamlet]);- assert_mentions_matches("King L", [lear]);+ assert_mentions_matches("King ", [hamlet_item, lear_item]);+ assert_mentions_matches("King H", [hamlet_item]);+ assert_mentions_matches("King L", [lear_item]);assert_mentions_matches("delia lear", []);- assert_mentions_matches("Mark Tw", [twin1, twin2]);+ assert_mentions_matches("Mark Tw", [twin1_item, twin2_item]);// Earlier user group and stream mentions were autocompleted by their// description too. This is now removed as it often led to unexpected@@ -1790,17 +1813,18 @@ test("typeahead_results", () => {// Verify we suggest only the first matching stream wildcard mention,// irrespective of how many equivalent stream wildcard mentions match.const mention_everyone = ct.broadcast_mentions()[1];+ const mention_everyone_item = user_or_mention_item(mention_everyone);// Here, we suggest only "everyone" instead of both the matching// "everyone" and "stream" wildcard mentions.assert_mentions_matches("e", [- mention_everyone,- hal,- alice,- cordelia,- gael,- hamlet,- lear,- othello,+ mention_everyone_item,+ hal_item,+ alice_item,+ cordelia_item,+ gael_item,+ hamlet_item,+ lear_item,+ othello_item,hamletcharacters,call_center,]);@@ -1808,8 +1832,14 @@ test("typeahead_results", () => {// Verify we suggest both 'the first matching stream wildcard' and// 'topic wildcard' mentions. Not only one matching wildcard mention.const mention_topic = ct.broadcast_mentions()[4];+ const mention_topic_item = user_or_mention_item(mention_topic);// Here, we suggest both "everyone" and "topic".- assert_mentions_matches("o", [othello, mention_everyone, mention_topic, cordelia]);+ assert_mentions_matches("o", [+ othello_item,+ mention_everyone_item,+ mention_topic_item,+ cordelia_item,+ ]);// Autocomplete by slash commands.assert_slash_matches("me", [me_slash]);@@ -1851,20 +1881,20 @@ test("message people", ({override, override_rewire}) => {};results = ct.get_person_suggestions("Ha", opts);- assert.deepEqual(results, [harry, hal]);+ assert.deepEqual(results, [harry_item, hal_item]);// Now let's exclude Hal and include King Hamlet.user_ids = [hamlet.user_id, harry.user_id];results = ct.get_person_suggestions("Ha", opts);- assert.deepEqual(results, [harry, hamlet]);+ assert.deepEqual(results, [harry_item, hamlet_item]);// Reincluding Hal and deactivating harryuser_ids = [hamlet.user_id, harry.user_id, hal.user_id];people.deactivate(harry);results = ct.get_person_suggestions("Ha", opts);// harry is excluded since it has been deactivated.- assert.deepEqual(results, [hal, hamlet]);+ assert.deepEqual(results, [hal_item, hamlet_item]);});test("muted users excluded from results", () => {@@ -1877,7 +1907,7 @@ test("muted users excluded from results", () => {// Nobody is mutedresults = ct.get_person_suggestions("corde", opts);- assert.deepEqual(results, [cordelia]);+ assert.deepEqual(results, [cordelia_item]);// Mute Cordelia, and test that she's excluded from results.muted_users.add_muted_user(cordelia.user_id);@@ -1888,7 +1918,8 @@ test("muted users excluded from results", () => {// or user group mentions.results = ct.get_person_suggestions("all", opts);const mention_all = ct.broadcast_mentions()[0];- assert.deepEqual(results, [mention_all, call_center]);+ const mention_all_item = user_or_mention_item(mention_all);+ assert.deepEqual(results, [mention_all_item, call_center]);});test("direct message recipients sorted according to stream / topic being viewed", ({@@ -1912,12 +1943,12 @@ test("direct message recipients sorted according to stream / topic being viewed"// When viewing no stream, sorting is alphabeticalcompose_state.set_stream_id("");results = ct.get_pm_people("li");- assert.deepEqual(results, [ali, alice, cordelia]);+ assert.deepEqual(results, [ali_item, alice_item, cordelia_item]);// When viewing denmark stream, subscriber cordelia is placed highercompose_state.set_stream_id(denmark_stream.stream_id);results = ct.get_pm_people("li");- assert.deepEqual(results, [cordelia, ali, alice]);+ assert.deepEqual(results, [cordelia_item, ali_item, alice_item]);// Simulating just alice being subscribed to denmark.override_rewire(@@ -1929,5 +1960,5 @@ test("direct message recipients sorted according to stream / topic being viewed"// When viewing denmark stream to which alice is subscribed, ali is not// 1st despite having an exact name match with the query.results = ct.get_pm_people("ali");- assert.deepEqual(results, [alice, ali]);+ assert.deepEqual(results, [alice_item, ali_item]);});diff --git a/web/tests/pill_typeahead.test.js b/web/tests/pill_typeahead.test.jsindex 2a861af8d54..68b0cff0924 100644--- a/web/tests/pill_typeahead.test.js+++ b/web/tests/pill_typeahead.test.js@@ -44,31 +44,50 @@ function override_typeahead_helper(override_rewire) {});}+function user_item(user) {+ return {+ ...user,+ type: "user",+ };+}+const jill = {email: "[email protected]",user_id: 10,full_name: "Jill Hill",};+const jill_item = user_item(jill);const mark = {email: "[email protected]",user_id: 20,full_name: "Marky Mark",};+const mark_item = user_item(mark);const fred = {email: "[email protected]",user_id: 30,full_name: "Fred Flintstone",};+const fred_item = user_item(fred);const me = {email: "[email protected]",user_id: 40,full_name: "me",};+const me_item = user_item(me);const persons = [jill, mark, fred, me];for (const person of persons) {people.add_active_user(person);}+const person_items = persons.map((person) => user_item(person));++function user_group_item(user_group) {+ return {+ ...user_group,+ type: "user_group",+ };+}const admins = {name: "Admins",@@ -76,17 +95,27 @@ const admins = {id: 1,members: [jill.user_id, mark.user_id],};+const admins_item = user_group_item(admins);const testers = {name: "Testers",description: "bar",id: 2,members: [mark.user_id, fred.user_id, me.user_id],};+const testers_item = user_group_item(testers);const groups = [admins, testers];for (const group of groups) {user_groups.add(group);}+const group_items = [admins_item, testers_item];++function stream_item(stream) {+ return {+ ...stream,+ type: "stream",+ };+}const denmark = {stream_id: 1,@@ -94,6 +123,7 @@ const denmark = {subscribed: true,render_subscribers: true,};+const denmark_item = stream_item(denmark);peer_data.set_subscribers(denmark.stream_id, [me.user_id, mark.user_id]);const sweden = {@@ -101,6 +131,7 @@ const sweden = {name: "Sweden",subscribed: false,};+const sweden_item = stream_item(sweden);peer_data.set_subscribers(sweden.stream_id, [mark.user_id, jill.user_id]);const subs = [denmark, sweden];@@ -155,28 +186,37 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {(function test_highlighter() {if (opts.stream) {// Test stream highlighter_html for widgets that allow stream pills.- assert.equal(config.highlighter_html(denmark, stream_query), $fake_rendered_stream);+ assert.equal(+ config.highlighter_html(denmark_item, stream_query),+ $fake_rendered_stream,+ );}if (opts.user_group && opts.user) {// If user is also allowed along with user_group// then we should check that each of them rendered correctly.- assert.equal(config.highlighter_html(testers, group_query), $fake_rendered_group);- assert.equal(config.highlighter_html(me, person_query), $fake_rendered_person);+ assert.equal(+ config.highlighter_html(testers_item, group_query),+ $fake_rendered_group,+ );+ assert.equal(config.highlighter_html(me_item, person_query), $fake_rendered_person);}if (opts.user && !opts.user_group) {- assert.equal(config.highlighter_html(me, person_query), $fake_rendered_person);+ assert.equal(config.highlighter_html(me_item, person_query), $fake_rendered_person);}if (!opts.user && opts.user_group) {- assert.equal(config.highlighter_html(testers, group_query), $fake_rendered_group);+ assert.equal(+ config.highlighter_html(testers_item, group_query),+ $fake_rendered_group,+ );}})();(function test_matcher() {let result;if (opts.stream) {- result = config.matcher(denmark, stream_query);+ result = config.matcher(denmark_item, stream_query);assert.ok(result);- result = config.matcher(sweden, stream_query);+ result = config.matcher(sweden_item, stream_query);assert.ok(!result);}if (opts.user_group && opts.user) {@@ -185,28 +225,28 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {or group is returned. */// group query, with correct item.- result = config.matcher(testers, group_query);+ result = config.matcher(testers_item, group_query);assert.ok(result);// group query, with wrong item.- result = config.matcher(admins, group_query);+ result = config.matcher(admins_item, group_query);assert.ok(!result);// person query with correct item.- result = config.matcher(me, person_query);+ result = config.matcher(me_item, person_query);assert.ok(result);// person query with wrong item.- result = config.matcher(jill, person_query);+ result = config.matcher(jill_item, person_query);assert.ok(!result);}if (opts.user_group && !opts.user) {- result = config.matcher(testers, group_query);+ result = config.matcher(testers_item, group_query);assert.ok(result);- result = config.matcher(admins, group_query);+ result = config.matcher(admins_item, group_query);assert.ok(!result);}if (opts.user && !opts.user_group) {- result = config.matcher(me, person_query);+ result = config.matcher(me_item, person_query);assert.ok(result);- result = config.matcher(jill, person_query);+ result = config.matcher(jill_item, person_query);assert.ok(!result);}})();@@ -219,12 +259,12 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {}if (opts.user_group) {sort_recipients_called = false;- config.sorter([testers], group_query);+ config.sorter([testers_item], group_query);assert.ok(sort_recipients_called);}if (opts.user) {sort_recipients_called = false;- config.sorter([me], person_query);+ config.sorter([me_item], person_query);assert.ok(sort_recipients_called);}})();@@ -253,13 +293,13 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {}).filter(Boolean);if (opts.user_group) {- expected_result = [...expected_result, ...groups];+ expected_result = [...expected_result, ...group_items];}if (opts.user) {if (opts.user_source) {expected_result = [...expected_result, ...opts.user_source()];} else {- expected_result = [...expected_result, ...persons];+ expected_result = [...expected_result, ...person_items];}}expected_result = expected_result@@ -286,11 +326,11 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {return pills.length;}assert.equal(number_of_pills(), 0);- config.updater(denmark, stream_query);+ config.updater(denmark_item, stream_query);assert.equal(number_of_pills(), 1);- config.updater(me, person_query);+ config.updater(me_item, person_query);assert.equal(number_of_pills(), 2);- config.updater(testers, group_query);+ config.updater(testers_item, group_query);assert.equal(number_of_pills(), 3);assert.ok(update_func_called);@@ -315,7 +355,7 @@ run_test("set_up", ({mock_template, override, override_rewire}) => {{user: true},// user and custom user source.- {user: true, user_source: () => [fred, mark]},+ {user: true, user_source: () => [fred_item, mark_item]},{stream: true},{user_group: true},{user_group: true, stream: true},diff --git a/web/tests/typeahead_helper.test.js b/web/tests/typeahead_helper.test.jsindex 2b5cdf512c5..b3bfd1cf977 100644--- a/web/tests/typeahead_helper.test.js+++ b/web/tests/typeahead_helper.test.js@@ -32,6 +32,20 @@ function assertSameEmails(lst1, lst2) {);}+function user_item(user) {+ return {+ ...user,+ type: "user",+ };+}++function user_or_mention_item(user_or_mention) {+ return {+ ...user_or_mention,+ type: "user_or_mention",+ };+}+const a_bot = {email: "[email protected]",full_name: "A Zulip test bot",@@ -55,6 +69,7 @@ const b_user_1 = {is_bot: false,user_id: 3,};+const b_user_1_item = user_item(b_user_1);const b_user_2 = {email: "[email protected]",@@ -71,6 +86,7 @@ const b_user_3 = {is_bot: false,user_id: 5,};+const b_user_3_item = user_item(b_user_3);const b_bot = {email: "[email protected]",@@ -87,6 +103,7 @@ const zman = {is_bot: false,user_id: 7,};+const zman_item = user_item(zman);const matches = [a_bot, a_user, b_user_1, b_user_2, b_user_3, b_bot, zman];@@ -337,8 +354,12 @@ test("sort_languages on actual data", () => {});function get_typeahead_result(query, current_stream_id, current_topic) {+ const users = people.get_realm_users().map((user) => ({+ ...user,+ type: "user",+ }));const result = th.sort_recipients({- users: people.get_realm_users(),+ users,query,current_stream_id,current_topic,@@ -447,9 +468,11 @@ test("sort_recipients all mention", () => {// Test person email is "all" or "everyone"const test_objs = [...matches, all_obj];-+ const user_and_mention_items = test_objs.map((user_or_mention) =>+ user_or_mention_item(user_or_mention),+ );const results = th.sort_recipients({- users: test_objs,+ users: user_and_mention_items,query: "a",current_stream_id: linux_sub.stream_id,current_topic: "Linux topic",@@ -497,20 +520,23 @@ test("sort_recipients pm counts", () => {// get some line coverageassert.equal(- th.compare_people_for_relevance(b_user_1, b_user_3, compare, linux_sub.stream_id),+ th.compare_people_for_relevance(b_user_1_item, b_user_3_item, compare, linux_sub.stream_id),1,);assert.equal(- th.compare_people_for_relevance(b_user_3, b_user_1, compare, linux_sub.stream_id),+ th.compare_people_for_relevance(b_user_3_item, b_user_1_item, compare, linux_sub.stream_id),-1,);});test("sort_recipients dup bots", () => {const dup_objects = [...matches, a_bot];-+ const user_items = dup_objects.map((user) => ({+ ...user,+ type: "user",+ }));const recipients = th.sort_recipients({- users: dup_objects,+ users: user_items,query: "b",current_stream_id: undefined,current_topic: "",@@ -535,9 +561,11 @@ test("sort_recipients dup alls", () => {// full_name starts with same character but emails are 'all'const test_objs = [all_obj, a_user, all_obj];-+ const user_and_mention_items = test_objs.map((user_or_mention) =>+ user_or_mention_item(user_or_mention),+ );const recipients = th.sort_recipients({- users: test_objs,+ users: user_and_mention_items,query: "a",current_stream_id: linux_sub.stream_id,current_topic: "Linux topic",@@ -553,9 +581,11 @@ test("sort_recipients dup alls direct message", () => {// full_name starts with same character but emails are 'all'const test_objs = [all_obj, a_user, all_obj];-+ const user_and_mention_items = test_objs.map((user_or_mention) =>+ user_or_mention_item(user_or_mention),+ );const recipients = th.sort_recipients({- users: test_objs,+ users: user_and_mention_items,query: "a",});@@ -567,8 +597,12 @@ test("sort_recipients subscribers", () => {// b_user_2 is a subscriber and b_user_1 is not.peer_data.add_subscriber(dev_sub.stream_id, b_user_2.user_id);const small_matches = [b_user_2, b_user_1];+ const user_items = small_matches.map((user) => ({+ ...user,+ type: "user",+ }));const recipients = th.sort_recipients({- users: small_matches,+ users: user_items,query: "b",current_stream_id: dev_sub.stream_id,current_topic: "Dev topic",@@ -592,8 +626,12 @@ test("sort_recipients recent senders", () => {id: (next_id += 1),});pm_conversations.set_partner(b_user_3.user_id);+ const user_items = small_matches.map((user) => ({+ ...user,+ type: "user",+ }));const recipients = th.sort_recipients({- users: small_matches,+ users: user_items,query: "b",current_stream_id: linux_sub.stream_id,current_topic: "Linux topic",@@ -609,8 +647,12 @@ test("sort_recipients pm partners", () => {// both are not subscribed to the stream Linux.pm_conversations.set_partner(b_user_3.user_id);const small_matches = [b_user_3, b_user_2];+ const user_items = small_matches.map((user) => ({+ ...user,+ type: "user",+ }));const recipients = th.sort_recipients({- users: small_matches,+ users: user_items,query: "b",current_stream_id: linux_sub.stream_id,current_topic: "Linux topic",@@ -626,7 +668,9 @@ test("sort broadcast mentions for stream message type", () => {// actually had a bug where the sort would// randomly rearrange them)compose_state.set_message_type("stream");- const results = th.sort_people_for_relevance(ct.broadcast_mentions().reverse(), "", "");+ const mentions = ct.broadcast_mentions().reverse();+ const mention_items = mentions.map((mention) => user_or_mention_item(mention));+ const results = th.sort_people_for_relevance(mention_items, "", "");assert.deepEqual(results.map((r) => r.email),@@ -639,8 +683,10 @@ test("sort broadcast mentions for stream message type", () => {const test_objs = [...ct.broadcast_mentions()].reverse();test_objs.unshift(zman);test_objs.push(a_user);-- const results2 = th.sort_people_for_relevance(test_objs, "", "");+ const user_or_mention_items = test_objs.map((user_or_mention) =>+ user_or_mention_item(user_or_mention),+ );+ const results2 = th.sort_people_for_relevance(user_or_mention_items, "", "");assert.deepEqual(results2.map((r) => r.email),@@ -650,7 +696,9 @@ test("sort broadcast mentions for stream message type", () => {test("sort broadcast mentions for direct message type", () => {compose_state.set_message_type("private");- const results = th.sort_people_for_relevance(ct.broadcast_mentions().reverse(), "", "");+ const mentions = ct.broadcast_mentions().reverse();+ const mention_items = mentions.map((mention) => user_or_mention_item(mention));+ const results = th.sort_people_for_relevance(mention_items, "", "");assert.deepEqual(results.map((r) => r.email),@@ -660,8 +708,10 @@ test("sort broadcast mentions for direct message type", () => {const test_objs = [...ct.broadcast_mentions()].reverse();test_objs.unshift(zman);test_objs.push(a_user);-- const results2 = th.sort_people_for_relevance(test_objs, "", "");+ const user_or_mention_items = test_objs.map((user_or_mention) =>+ user_or_mention_item(user_or_mention),+ );+ const results2 = th.sort_people_for_relevance(user_or_mention_items, "", "");assert.deepEqual(results2.map((r) => r.email),@@ -675,19 +725,21 @@ test("test compare directly for stream message type", () => {// coverage is subject to the whims of how JS sorts.compose_state.set_message_type("stream");const all_obj = ct.broadcast_mentions()[0];+ const all_obj_item = user_or_mention_item(all_obj);- assert.equal(th.compare_people_for_relevance(all_obj, all_obj), 0);- assert.equal(th.compare_people_for_relevance(all_obj, zman), -1);- assert.equal(th.compare_people_for_relevance(zman, all_obj), 1);+ assert.equal(th.compare_people_for_relevance(all_obj_item, all_obj_item), 0);+ assert.equal(th.compare_people_for_relevance(all_obj_item, zman_item), -1);+ assert.equal(th.compare_people_for_relevance(zman_item, all_obj_item), 1);});test("test compare directly for direct message", () => {compose_state.set_message_type("private");const all_obj = ct.broadcast_mentions()[0];+ const all_obj_item = user_or_mention_item(all_obj);- assert.equal(th.compare_people_for_relevance(all_obj, all_obj), 0);- assert.equal(th.compare_people_for_relevance(all_obj, zman), 1);- assert.equal(th.compare_people_for_relevance(zman, all_obj), -1);+ assert.equal(th.compare_people_for_relevance(all_obj_item, all_obj_item), 0);+ assert.equal(th.compare_people_for_relevance(all_obj_item, zman_item), 1);+ assert.equal(th.compare_people_for_relevance(zman_item, all_obj_item), -1);});test("highlight_with_escaping", () => {diff --git a/web/tests/user_pill.test.js b/web/tests/user_pill.test.jsindex fda85e86431..522a8316e0a 100644--- a/web/tests/user_pill.test.js+++ b/web/tests/user_pill.test.js@@ -147,5 +147,10 @@ test("typeahead", () => {// excluded by virtue of already being one of the widget items.// And then bogus_item is just a red herring to test robustness.const result = user_pill.typeahead_source(pill_widget);- assert.deepEqual(result, [alice]);+ assert.deepEqual(result, [+ {+ ...alice,+ type: "user",+ },+ ]);});