Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typeahead: Use custom profile fields in user mention suggestions.
Amit Patil0828b83635a5
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 06309388adb..7cf9a94638e 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,16 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 12.0+**Feature level 455**++* [`POST /register`](/api/register-queue), [`GET+ /events`](/api/get-events), [`POST+ /realm/profile_fields`](/api/create-custom-profile-field), [`GET+ /realm/profile_fields`](/api/get-custom-profile-fields) Added a new+ parameter `use_for_user_matching` to custom profile field objects,+ which indicates whether this custom profile field should be used to+ match users in typeahead.+**Feature level 454**- [`PATCH /realm/user_settings_defaults`](/api/update-realm-user-settings-defaults)diff --git a/version.py b/version.pyindex 8b580b51fb6..95e7774f5a1 100644--- a/version.py+++ b/version.py@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"# new level means in api_docs/changelog.md, as well as "**Changes**"# entries in the endpoint's documentation in `zulip.yaml`.-API_FEATURE_LEVEL = 454+API_FEATURE_LEVEL = 455# Bump the minor PROVISION_VERSION to indicate that folks should provision# only when going from an old version of the code to a newer version. Bumpdiff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex 7a827a6242b..86da0bba622 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -603,6 +603,7 @@ export function filter_and_sort_mentions(want_broadcast: !is_silent,filter_pills: false,filter_groups_for_mention: !is_silent,+ allow_custom_profile_field_matching: true,...opts,}).map((item) => ({...item,@@ -648,6 +649,7 @@ type PersonSuggestionOpts = {topic: string | undefined;filter_groups_for_dm?: boolean;filter_groups_for_mention?: boolean;+ allow_custom_profile_field_matching?: boolean;};function filter_persons<T>(@@ -845,7 +847,13 @@ export function get_person_suggestions();return suggestion_items.filter((item) =>- typeahead_helper.query_matches_person(query, item, should_remove_diacritics),+ typeahead_helper.query_matches_person(+ query,+ item,+ should_remove_diacritics,+ undefined,+ opts.allow_custom_profile_field_matching,+ ),);};diff --git a/web/src/settings_components.ts b/web/src/settings_components.tsindex e33b9b656ff..359c2f512a3 100644--- a/web/src/settings_components.ts+++ b/web/src/settings_components.ts@@ -180,6 +180,9 @@ export function get_custom_profile_property_value(if (property_name === "display_in_profile_summary" && value === undefined) {return false;}+ if (property_name === "use_for_user_matching" && value === undefined) {+ return false;+ }return value;}diff --git a/web/src/settings_profile_fields.ts b/web/src/settings_profile_fields.tsindex e2914b62f70..f158579b235 100644--- a/web/src/settings_profile_fields.ts+++ b/web/src/settings_profile_fields.ts@@ -143,6 +143,18 @@ function is_valid_to_display_in_summary(field_type: number): boolean {return true;}+// Checking custom profile field type is valid for showing use for user matching checkbox field.+function is_valid_to_use_for_user_matching(field_type: number): boolean {+ const field_types = realm.custom_profile_field_types;+ if (+ field_type === field_types.EXTERNAL_ACCOUNT.id ||+ field_type === field_types.SHORT_TEXT.id+ ) {+ return true;+ }+ return false;+}+function delete_profile_field(this: HTMLElement, e: JQuery.ClickEvent): void {e.preventDefault();e.stopPropagation();@@ -302,6 +314,15 @@ function update_form_for_field_type_selection(): void {$("#profile_field_display_in_profile_summary").closest(".input-group").hide();$("#profile_field_display_in_profile_summary").prop("checked", false);}++ if (is_valid_to_use_for_user_matching(profile_field_type)) {+ $("#profile_field_use_for_user_matching").closest(".input-group").show();+ const check_use_for_user_mentions = profile_field_type === field_types.EXTERNAL_ACCOUNT.id;+ $("#profile_field_use_for_user_matching").prop("checked", check_use_for_user_mentions);+ } else {+ $("#profile_field_use_for_user_matching").closest(".input-group").hide();+ $("#profile_field_use_for_user_matching").prop("checked", false);+ }}function open_custom_profile_field_creation_form_modal(): void {@@ -334,6 +355,7 @@ function open_custom_profile_field_creation_form_modal(): void {),required: $("#profile-field-required").is(":checked"),editable_by_user: $("#profile_field_editable_by_user").is(":checked"),+ use_for_user_matching: $("#profile_field_use_for_user_matching").is(":checked"),};const url = "/json/realm/profile_fields";const opts = {@@ -591,9 +613,11 @@ function open_custom_profile_field_edit_form_modal(this: HTMLElement): void {display_in_profile_summary: field.display_in_profile_summary === true,required: field.required,editable_by_user: field.editable_by_user,+ use_for_user_matching: field.use_for_user_matching,is_select_field: field.type === field_types.SELECT.id,is_external_account_field: field.type === field_types.EXTERNAL_ACCOUNT.id,valid_to_display_in_summary: is_valid_to_display_in_summary(field.type),+ valid_to_use_for_user_matching: is_valid_to_use_for_user_matching(field.type),},realm_default_external_accounts: realm.realm_default_external_accounts,});diff --git a/web/src/state_data.ts b/web/src/state_data.tsindex 6d761cf31a6..22768ff334b 100644--- a/web/src/state_data.ts+++ b/web/src/state_data.ts@@ -161,6 +161,7 @@ export const custom_profile_field_schema = z.object({order: z.number(),required: z.boolean(),type: z.number(),+ use_for_user_matching: z.optional(z.boolean()),});export type CustomProfileField = z.output<typeof custom_profile_field_schema>;diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex fc402b386d3..90663f9bc67 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -1059,6 +1059,7 @@ export function query_matches_person(person: UserPillData | UserOrMentionPillData,should_remove_diacritics?: boolean,match_prefix?: boolean,+ allow_custom_profile_field_matching = false,): boolean {if (person.type === "broadcast" &&@@ -1072,6 +1073,19 @@ export function query_matches_person(return true;}+ if (allow_custom_profile_field_matching) {+ // Check custom profile fields that are enabled for use_for_user_matching+ for (const field of realm.custom_profile_fields) {+ if (field.use_for_user_matching) {+ const field_value =+ people.get_custom_profile_data(person.user.user_id, field.id)?.value ?? "";+ if (typeahead.query_matches_string_in_order(query, field_value, " ")) {+ return true;+ }+ }+ }+ }+if (person.user.delivery_email) {return typeahead.query_matches_string_in_order(query,diff --git a/web/templates/settings/add_new_custom_profile_field_form.hbs b/web/templates/settings/add_new_custom_profile_field_form.hbsindex 4f4ef4e6c53..3959ede25d1 100644--- a/web/templates/settings/add_new_custom_profile_field_form.hbs+++ b/web/templates/settings/add_new_custom_profile_field_form.hbs@@ -56,5 +56,11 @@is_checked=truelabel=(t "Users can edit this field for their own account")}}+ {{> settings_checkbox+ prefix="profile_field_"+ setting_name="use_for_user_matching"+ is_checked=false+ label=(t "Use in @-mention suggestions")+ }}</div></form>diff --git a/web/templates/settings/edit_custom_profile_field_form.hbs b/web/templates/settings/edit_custom_profile_field_form.hbsindex d4d2cff608a..3a27082b67e 100644--- a/web/templates/settings/edit_custom_profile_field_form.hbs+++ b/web/templates/settings/edit_custom_profile_field_form.hbs@@ -64,5 +64,13 @@is_checked= editable_by_userlabel=(t "Users can edit this field for their own account")}}+ {{#if valid_to_use_for_user_matching}}+ {{> settings_checkbox+ prefix="id-custom-profile-field-"+ setting_name="use_for_user_matching"+ is_checked= use_for_user_matching+ label=(t "Use in @-mention suggestions")+ }}+ {{/if}}</form>{{/with}}diff --git a/web/tests/typeahead_helper.test.cjs b/web/tests/typeahead_helper.test.cjsindex b46906cd083..b0d81a5d687 100644--- a/web/tests/typeahead_helper.test.cjs+++ b/web/tests/typeahead_helper.test.cjs@@ -1664,3 +1664,39 @@ test("compare_stream_or_group_members_options", () => {0,);});+test("query_matches_person matches custom profile fields when they are enabled for user matching ", ({+ override,+}) => {+ a_user.profile_data ??= {};++ override(realm, "custom_profile_field_types", {+ SHORT_TEXT: {id: 1, name: "Short text"},+ EXTERNAL_ACCOUNT: {id: 2, name: "External account"},+ });++ override(realm, "custom_profile_fields", [+ {+ id: 51,+ name: "Alpha field",+ type: realm.custom_profile_field_types.SHORT_TEXT.id,+ use_for_user_matching: true,+ },+ {+ id: 52,+ name: "Beta field",+ type: realm.custom_profile_field_types.EXTERNAL_ACCOUNT.id,+ },+ ]);++ people.set_custom_profile_field_data(a_user.user_id, {+ id: 51,+ value: "Alpha",+ });+ people.set_custom_profile_field_data(a_user.user_id, {+ id: 52,+ value: "Beta",+ });++ assert.equal(th.query_matches_person("alpha", a_user_item, undefined, undefined, true), true);+ assert.equal(th.query_matches_person("beta", a_user_item, undefined, undefined, true), false);+});diff --git a/zerver/actions/custom_profile_fields.py b/zerver/actions/custom_profile_fields.pyindex 45dfc8d27ef..f9cc1dd4a96 100644--- a/zerver/actions/custom_profile_fields.py+++ b/zerver/actions/custom_profile_fields.py@@ -30,6 +30,7 @@ def try_add_realm_default_custom_profile_field(display_in_profile_summary: bool = False,required: bool = False,editable_by_user: bool = True,+ use_for_user_matching: bool = False,) -> CustomProfileField:field_data = DEFAULT_EXTERNAL_ACCOUNTS[field_subtype]custom_profile_field = CustomProfileField(@@ -41,6 +42,7 @@ def try_add_realm_default_custom_profile_field(display_in_profile_summary=display_in_profile_summary,required=required,editable_by_user=editable_by_user,+ use_for_user_matching=use_for_user_matching,)custom_profile_field.save()custom_profile_field.order = custom_profile_field.id@@ -59,6 +61,7 @@ def try_add_realm_custom_profile_field(display_in_profile_summary: bool = False,required: bool = False,editable_by_user: bool = True,+ use_for_user_matching: bool = False,) -> CustomProfileField:custom_profile_field = CustomProfileField(realm=realm,@@ -67,6 +70,7 @@ def try_add_realm_custom_profile_field(display_in_profile_summary=display_in_profile_summary,required=required,editable_by_user=editable_by_user,+ use_for_user_matching=use_for_user_matching,)custom_profile_field.hint = hintif custom_profile_field.field_type in (@@ -117,6 +121,7 @@ def try_update_realm_custom_profile_field(display_in_profile_summary: bool | None = None,required: bool | None = None,editable_by_user: bool | None = None,+ use_for_user_matching: bool | None = None,) -> None:if name is not None:field.name = name@@ -128,6 +133,8 @@ def try_update_realm_custom_profile_field(field.editable_by_user = editable_by_userif display_in_profile_summary is not None:field.display_in_profile_summary = display_in_profile_summary+ if use_for_user_matching is not None:+ field.use_for_user_matching = use_for_user_matchingif field.field_type in (CustomProfileField.SELECT,diff --git a/zerver/lib/types.py b/zerver/lib/types.pyindex d14056f178f..2163708b9c2 100644--- a/zerver/lib/types.py+++ b/zerver/lib/types.py@@ -29,6 +29,7 @@ class ProfileDataElementBase(TypedDict, total=False):display_in_profile_summary: boolrequired: booleditable_by_user: bool+ use_for_user_matching: boolfield_data: strorder: intdiff --git a/zerver/migrations/0775_customprofilefield_use_for_user_matching.py b/zerver/migrations/0775_customprofilefield_use_for_user_matching.pynew file mode 100644index 00000000000..425c77b0926--- /dev/null+++ b/zerver/migrations/0775_customprofilefield_use_for_user_matching.py@@ -0,0 +1,16 @@+# Generated by Django 5.2.8 on 2025-12-06 09:39++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("zerver", "0774_rename_recent_topics_to_recent"),+ ]+ operations = [+ migrations.AddField(+ model_name="customprofilefield",+ name="use_for_user_matching",+ field=models.BooleanField(default=False, db_default=False),+ ),+ ]diff --git a/zerver/models/custom_profile_fields.py b/zerver/models/custom_profile_fields.pyindex 58c8be6dbb2..fdd6da55821 100644--- a/zerver/models/custom_profile_fields.py+++ b/zerver/models/custom_profile_fields.py@@ -82,6 +82,10 @@ class CustomProfileField(models.Model):display_in_profile_summary = models.BooleanField(default=False)required = models.BooleanField(default=False)+ # Whether this field should be used in @-mention typeahead suggestions.+ # Only applicable for EXTERNAL_ACCOUNT and SHORT_TEXT field types.+ use_for_user_matching = models.BooleanField(default=False, db_default=False)+# Whether regular users can edit this field on their own account.editable_by_user = models.BooleanField(default=True, db_default=True)@@ -177,6 +181,8 @@ class CustomProfileField(models.Model):}if self.display_in_profile_summary:data_as_dict["display_in_profile_summary"] = True+ if self.use_for_user_matching:+ data_as_dict["use_for_user_matching"] = Truereturn data_as_dictdiff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex b966e498541..837d29b28a0 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -2063,6 +2063,7 @@ paths:"order": 8,"required": true,"editable_by_user": true,+ "use_for_user_matching": true,},],"id": 0,@@ -13781,6 +13782,7 @@ paths:"order": 8,"required": true,"editable_by_user": true,+ "use_for_user_matching": true,},{"id": 9,@@ -13929,6 +13931,17 @@ paths:**Changes**: New in Zulip 10.0 (feature level 296).type: booleanexample: true+ use_for_user_matching:+ description: |+ Whether this custom profile field should be used to match users in typeahead+ suggestions. Only allowed for Short Text and External Account+ [profile field types](/help/custom-profile-fields#profile-field-types).++ This field is only included when its value is `true`.++ **Changes**: New in Zulip 12.0 (feature level 455).+ type: boolean+ example: falserequired:- field_typeencoding:@@ -13942,6 +13955,8 @@ paths:contentType: application/jsoneditable_by_user:contentType: application/json+ use_for_user_matching:+ contentType: application/jsonresponses:"200":description: Success.@@ -25871,6 +25886,17 @@ components:**Changes**: New in Zulip 10.0 (feature level 296).default: true+ use_for_user_matching:+ type: boolean+ description: |+ Whether this custom profile field should be used to match users in typeahead+ suggestions. Only allowed for Short Text and External Account+ [profile field types](/help/custom-profile-fields#profile-field-types).++ This field is only included when its value is `true`.++ **Changes**: New in Zulip 12.0 (feature level 455).+ default: falserequired:- id- typediff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.pyindex 5eeb9f91c62..b4393b2fbac 100644--- a/zerver/tests/test_custom_profile_data.py+++ b/zerver/tests/test_custom_profile_data.py@@ -98,6 +98,13 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):result = self.client_post("/json/realm/profile_fields", info=data)self.assert_json_error(result, "A field with that label already exists.")+ data["name"] = "Phone"+ data["hint"] = "Contact number"+ data["field_type"] = CustomProfileField.LONG_TEXT+ data["use_for_user_matching"] = "true"+ result = self.client_post("/json/realm/profile_fields", info=data)+ self.assert_json_error(result, "Field type not supported for use for user matching.")+def test_create_select_field(self) -> None:self.login("iago")data: dict[str, str | int] = {}@@ -392,6 +399,16 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):result = self.client_delete("/json/realm/profile_fields/1")self.assert_json_error(result, "Must be an organization administrator")+ def test_create_field_use_for_user_matching(self) -> None:+ self.login("iago")+ data = {+ "name": "use for user matching",+ "field_type": CustomProfileField.SHORT_TEXT,+ "use_for_user_matching": "true",+ }+ result = self.client_post("/json/realm/profile_fields", info=data)+ self.assert_json_success(result)+class DeleteCustomProfileFieldTest(CustomProfileFieldTestCase):def test_delete(self) -> None:@@ -704,6 +721,59 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):result, "Only 2 custom profile fields can be displayed in the profile summary.")+ def test_update_use_for_user_matching(self) -> None:+ self.login("iago")+ realm = get_realm("zulip")++ field = CustomProfileField.objects.get(name="Phone number", realm=realm)++ result = self.client_patch(+ f"/json/realm/profile_fields/{field.id}",+ info={+ "use_for_user_matching": "invalid value",+ },+ )+ msg = "use_for_user_matching is not valid JSON"+ self.assert_json_error(result, msg)++ result = self.client_patch(+ f"/json/realm/profile_fields/{field.id}",+ info={+ "use_for_user_matching": "true",+ },+ )++ self.assert_json_success(result)+ field.refresh_from_db()+ self.assertEqual(field.use_for_user_matching, True)++ # Not sending use_for_user_matching should not set it to false.+ result = self.client_patch(+ f"/json/realm/profile_fields/{field.id}",+ info={+ "name": "Number",+ },+ )++ field.refresh_from_db()+ self.assertEqual(field.name, "Number")+ self.assertEqual(field.use_for_user_matching, True)+ self.assert_json_success(result)++ # Setting use_for_user_matching as true for field types which cannot have that value set to true.+ field = CustomProfileField.objects.get(name="Biography", realm=realm)+ result = self.client_patch(+ f"/json/realm/profile_fields/{field.id}",+ info={+ "use_for_user_matching": "true",+ },+ )++ self.assert_json_error(+ result,+ "Field type not supported for use for user matching.",+ )+def test_update_field_data(self) -> None:self.login("iago")realm = get_realm("zulip")diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.pyindex 1d63ff72415..7102ee522a3 100644--- a/zerver/views/custom_profile_fields.py+++ b/zerver/views/custom_profile_fields.py@@ -76,6 +76,15 @@ def validate_display_in_profile_summary_field(raise JsonableError(_("Field type not supported for display in profile summary."))+def validate_use_for_user_matching_field(field_type: int, use_for_user_matching: bool) -> None:+ if not use_for_user_matching:+ return++ # Only SHORT_TEXT and EXTERNAL_ACCOUNT field types are supported for user matching.+ if field_type not in (CustomProfileField.SHORT_TEXT, CustomProfileField.EXTERNAL_ACCOUNT):+ raise JsonableError(_("Field type not supported for use for user matching."))++def is_default_external_field(field_type: int, field_data: ProfileFieldData) -> bool:if field_type != CustomProfileField.EXTERNAL_ACCOUNT:return False@@ -90,6 +99,7 @@ def validate_custom_profile_field(field_type: int,field_data: ProfileFieldData,display_in_profile_summary: bool,+ use_for_user_matching: bool,) -> None:# Validate field datavalidate_custom_field_data(field_type, field_data)@@ -106,6 +116,8 @@ def validate_custom_profile_field(validate_display_in_profile_summary_field(field_type, display_in_profile_summary)+ validate_use_for_user_matching_field(field_type, use_for_user_matching)+def validate_custom_profile_field_update(field: CustomProfileField,@@ -113,6 +125,7 @@ def validate_custom_profile_field_update(field_data: ProfileFieldData | None = None,name: str | None = None,hint: str | None = None,+ use_for_user_matching: bool | None = None,) -> None:if name is None:name = field.name@@ -127,14 +140,12 @@ def validate_custom_profile_field_update(field_data = orjson.loads(field.field_data)if display_in_profile_summary is None:display_in_profile_summary = field.display_in_profile_summary+ if use_for_user_matching is None:+ use_for_user_matching = field.use_for_user_matchingassert field_data is not Nonevalidate_custom_profile_field(- name,- hint,- field.field_type,- field_data,- display_in_profile_summary,+ name, hint, field.field_type, field_data, display_in_profile_summary, use_for_user_matching)@@ -178,6 +189,7 @@ def create_realm_custom_profile_field(hint: str = "",name: Annotated[str, StringConstraints(strip_whitespace=True)] = "",required: Json[bool] = False,+ use_for_user_matching: Json[bool] = False,) -> HttpResponse:if field_data is None:field_data = {}@@ -186,7 +198,9 @@ def create_realm_custom_profile_field(_("Only 2 custom profile fields can be displayed in the profile summary."))- validate_custom_profile_field(name, hint, field_type, field_data, display_in_profile_summary)+ validate_custom_profile_field(+ name, hint, field_type, field_data, display_in_profile_summary, use_for_user_matching+ )try:if is_default_external_field(field_type, field_data):field_subtype = field_data["subtype"]@@ -197,6 +211,7 @@ def create_realm_custom_profile_field(display_in_profile_summary=display_in_profile_summary,required=required,editable_by_user=editable_by_user,+ use_for_user_matching=use_for_user_matching,)return json_success(request, data={"id": field.id})else:@@ -209,6 +224,7 @@ def create_realm_custom_profile_field(display_in_profile_summary=display_in_profile_summary,required=required,editable_by_user=editable_by_user,+ use_for_user_matching=use_for_user_matching,)return json_success(request, data={"id": field.id})except IntegrityError:@@ -241,6 +257,7 @@ def update_realm_custom_profile_field(hint: str | None = None,name: Annotated[str, StringConstraints(strip_whitespace=True)] | None = None,required: Json[bool] | None = None,+ use_for_user_matching: Json[bool] | None = None,) -> HttpResponse:realm = user_profile.realmtry:@@ -270,7 +287,9 @@ def update_realm_custom_profile_field():raise JsonableError(_("Default custom field cannot be updated."))- validate_custom_profile_field_update(field, display_in_profile_summary, field_data, name, hint)+ validate_custom_profile_field_update(+ field, display_in_profile_summary, field_data, name, hint, use_for_user_matching+ )try:try_update_realm_custom_profile_field(realm=realm,@@ -281,6 +300,7 @@ def update_realm_custom_profile_field(display_in_profile_summary=display_in_profile_summary,required=required,editable_by_user=editable_by_user,+ use_for_user_matching=use_for_user_matching,)except IntegrityError:raise JsonableError(_("A field with that label already exists."))