diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index e4816ad88a..2f21be2965 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -1232,6 +1232,13 @@ paths: type: integer description: The user's ID. example: 1 + profile_data: + type: object + description: A dictionary containing custom profile field data for the user. + Each entry maps the integer ID of a custom profile field in the organization + to a dictionary containing the user's data for that field. Generally the + data includes just a single `value` key; for those custom profile fields + supporting markdown, a `rendered_value` key will also be present. - example: { "avatar_url": "https://secure.gravatar.com/avatar/af4f06322c177ef4e1e9b2c424986b54?d=identicon&version=1", @@ -1245,7 +1252,36 @@ paths: "pointer": -1, "result": "success", "short_name": "iago", - "user_id": 5 + "user_id": 5, + "profile_data": { + "5": { + "value": "2000-1-1" + }, + "4": { + "value": "emacs" + }, + "7": { + "value": "[10]" + }, + "1": { + "value": "+1-234-567-8901", + "rendered_value": "
+1-234-567-8901
" + }, + "2": { + "rendered_value": "Betrayer of Othello.
", + "value": "Betrayer of Othello." + }, + "8": { + "value": "zulip" + }, + "3": { + "value": "Apples", + "rendered_value": "Apples
" + }, + "6": { + "value": "https://zulip.readthedocs.io/en/latest/" + } + } } delete: description: Delete the requesting user from the realm. diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py index e74e3f2396..e57f1de438 100644 --- a/zerver/tests/test_custom_profile_data.py +++ b/zerver/tests/test_custom_profile_data.py @@ -684,6 +684,19 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase): with self.assertRaises(KeyError): user_dict["profile_data"] + def test_get_custom_profile_fields_from_api_for_single_user(self) -> None: + self.login(self.example_email("iago")) + expected_keys = { + "result", "msg", "pointer", "client_id", "max_message_id", "user_id", + "avatar_url", "full_name", "email", "is_bot", "is_admin", "short_name", + "profile_data"} + + url = "/json/users/me" + response = self.client_get(url) + self.assertEqual(response.status_code, 200) + raw_user_data = response.json() + self.assertEqual(set(raw_user_data.keys()), expected_keys) + class ReorderCustomProfileFieldTest(CustomProfileFieldTestCase): def test_reorder(self) -> None: diff --git a/zerver/views/users.py b/zerver/views/users.py index 1f208ecccf..33d97dbf32 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -469,6 +469,21 @@ def get_profile_backend(request: HttpRequest, user_profile: UserProfile) -> Http is_admin = user_profile.is_realm_admin, short_name = user_profile.short_name) + if not user_profile.is_bot: + custom_profile_field_values = user_profile.customprofilefieldvalue_set.all() + profile_data = dict() # type: Dict[int, Dict[str, Any]] + for profile_field in custom_profile_field_values: + if profile_field.field.is_renderable(): + profile_data[profile_field.field_id] = { + "value": profile_field.value, + "rendered_value": profile_field.rendered_value + } + else: + profile_data[profile_field.field_id] = { + "value": profile_field.value + } + result["profile_data"] = profile_data + messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1] if messages: result['max_message_id'] = messages[0].id