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.

typeahead: Show matched custom profile field in secondary text.

Amit Patil3e54ae9f4d5c
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts
index 86da0bba622..890ac851dcd 100644
--- a/web/src/composebox_typeahead.ts
+++ b/web/src/composebox_typeahead.ts
@@ -1239,7 +1239,7 @@ export function content_item_html(item: TypeaheadSuggestion): string | undefined
case "user_group":
case "user":
case "broadcast":
- return typeahead_helper.render_person_or_user_group(item);
+ return typeahead_helper.render_person_or_user_group(item, token);
case "slash":
return typeahead_helper.render_typeahead_item({
primary: item.text,
diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.ts
index 90663f9bc67..48dddd5718c 100644
--- a/web/src/typeahead_helper.ts
+++ b/web/src/typeahead_helper.ts
@@ -93,7 +93,10 @@ export function rewire_render_typeahead_item(value: typeof render_typeahead_item
render_typeahead_item = value;
}
-export let render_person = (person: UserPillData | UserOrMentionPillData): string => {
+export let render_person = (
+ person: UserPillData | UserOrMentionPillData,
+ query?: string,
+): string => {
if (person.type === "broadcast") {
return render_typeahead_item({
primary: person.user.special_item_text,
@@ -112,6 +115,38 @@ export let render_person = (person: UserPillData | UserOrMentionPillData): strin
const pronouns = pronouns_list?.[0]?.value;
+ // Show the email or a matched custom profile field in the secondary text.
+ // If both the email and a custom profile field match the query, show both.
+ let secondary_text = person.user.delivery_email;
+
+ if (query) {
+ const email_matches = typeahead.query_matches_string_in_order(
+ query,
+ secondary_text ?? "",
+ "",
+ );
+ let matched_custom_field;
+
+ for (const field of realm.custom_profile_fields) {
+ if (!field.use_for_user_matching) {
+ continue;
+ }
+
+ const value = people.get_custom_profile_data(person.user.user_id, field.id)?.value;
+
+ if (typeahead.query_matches_string_in_order(query, value ?? "", "")) {
+ matched_custom_field = value;
+ break;
+ }
+ }
+
+ // if both email and custom field matches, show both.
+ if (email_matches && matched_custom_field) {
+ secondary_text = `${secondary_text}, ${matched_custom_field}`;
+ } else if (matched_custom_field) {
+ secondary_text = matched_custom_field;
+ }
+ }
const typeahead_arguments = {
primary: person.user.full_name,
img_src: avatar_url,
@@ -123,7 +158,7 @@ export let render_person = (person: UserPillData | UserOrMentionPillData): strin
person.user.user_id,
),
pronouns,
- secondary: person.user.delivery_email,
+ secondary: secondary_text,
};
return render_typeahead_item(typeahead_arguments);
@@ -155,12 +190,13 @@ export function rewire_render_user_group(value: typeof render_user_group): void
export function render_person_or_user_group(
item: UserGroupPillData | UserPillData | UserOrMentionPillData,
+ query?: string,
): string {
if (item.type === "user_group") {
return render_user_group(item);
}
- return render_person(item);
+ return render_person(item, query);
}
export let render_stream = (stream: StreamData): string =>
diff --git a/web/tests/typeahead_helper.test.cjs b/web/tests/typeahead_helper.test.cjs
index b0d81a5d687..53bd8467acd 100644
--- a/web/tests/typeahead_helper.test.cjs
+++ b/web/tests/typeahead_helper.test.cjs
@@ -1664,6 +1664,113 @@ test("compare_stream_or_group_members_options", () => {
0,
);
});
+
+test("render_person shows value of custom profile fields in secondary", ({
+ mock_template,
+ override,
+}) => {
+ a_user.profile_data ??= {};
+
+ override(realm, "custom_profile_field_types", {
+ SHORT_TEXT: {id: 1, name: "Short text"},
+ PRONOUNS: {id: 3, name: "Pronouns"},
+ });
+
+ override(realm, "custom_profile_fields", [
+ {
+ id: 1,
+ name: "Alpha field",
+ type: realm.custom_profile_field_types.SHORT_TEXT.id,
+ use_for_user_matching: true,
+ },
+ ]);
+
+ people.set_custom_profile_field_data(a_user.user_id, {
+ id: 1,
+ value: "Alpha",
+ });
+
+ let rendered = false;
+
+ mock_template("typeahead_list_item.hbs", false, (args) => {
+ assert.equal(args.secondary, "Alpha");
+ rendered = true;
+ return "typeahead-item-stub";
+ });
+
+ assert.equal(th.render_person(a_user_item, "Alpha"), "typeahead-item-stub");
+ assert.ok(rendered);
+});
+
+test("render_person shows both email and custom profile fields as secondary if both matches", ({
+ mock_template,
+ override,
+}) => {
+ override(realm, "custom_profile_field_types", {
+ SHORT_TEXT: {id: 1, name: "Short text"},
+ PRONOUNS: {id: 3, name: "Pronouns"},
+ });
+
+ override(realm, "custom_profile_fields", [
+ {
+ id: 1,
+ name: "Alpha field",
+ type: realm.custom_profile_field_types.SHORT_TEXT.id,
+ use_for_user_matching: true,
+ },
+ ]);
+
+ people.set_custom_profile_field_data(a_user.user_id, {
+ id: 1,
+ value: "a_user",
+ });
+
+ let rendered = false;
+ mock_template("typeahead_list_item.hbs", false, (args) => {
+ assert.equal(args.secondary, "[email protected], a_user");
+ rendered = true;
+ return "typeahead-item-stub";
+ });
+
+ assert.equal(th.render_person(a_user_item, "a_user"), "typeahead-item-stub");
+ assert.ok(rendered);
+});
+
+test("render_person skips custom profile fields not used for user matching", ({
+ mock_template,
+ override,
+}) => {
+ a_user.profile_data ??= {};
+
+ override(realm, "custom_profile_field_types", {
+ PRONOUNS: {id: 3, name: "Pronouns"},
+ });
+
+ override(realm, "custom_profile_fields", [
+ {
+ id: 1,
+ name: "Alpha field",
+ type: realm.custom_profile_field_types.PRONOUNS.id,
+ },
+ ]);
+
+ people.set_custom_profile_field_data(a_user.user_id, {
+ id: 1,
+ value: "Alpha",
+ });
+
+ let rendered = false;
+
+ mock_template("typeahead_list_item.hbs", false, (args) => {
+ assert.notEqual(args.secondary, "Alpha");
+ rendered = true;
+ return "typeahead-item-stub";
+ });
+
+ assert.equal(th.render_person(a_user_item, "Alpha"), "typeahead-item-stub");
+ assert.ok(rendered);
+});
+
test("query_matches_person matches custom profile fields when they are enabled for user matching ", ({
override,
}) => {