Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
channels: Add API endpoint to fetch a user's subscriptions.
Evy Kassirer7d5935bb74a3
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 710ce673479..5ed165c71e9 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,11 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 12.0+**Feature level 440**++* [`GET users/<user_id>/channels`](/api/get-user-channels)+ Added a new endpoint to get the channels another user is subscribed to.+**Feature level 439*** [`GET /events`](/api/get-events): The deprecated `update_display_settings`diff --git a/api_docs/include/rest-endpoints.md b/api_docs/include/rest-endpoints.mdindex b60cae0a452..902a29f6f0b 100644--- a/api_docs/include/rest-endpoints.md+++ b/api_docs/include/rest-endpoints.md@@ -59,6 +59,7 @@* [Unsubscribe from a channel](/api/unsubscribe)* [Get subscription status](/api/get-subscription-status)* [Get channel subscribers](/api/get-subscribers)+* [Get a user's subscribed channels](/api/get-user-channels)* [Update subscription settings](/api/update-subscription-settings)* [Get all channels](/api/get-streams)* [Get a channel by ID](/api/get-stream-by-id)diff --git a/version.py b/version.pyindex bf1a0a5e111..bbc27eda401 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 = 439+API_FEATURE_LEVEL = 440# 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/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.pyindex 0cc26a9a821..d0c74660503 100644--- a/zerver/openapi/python_examples.py+++ b/zerver/openapi/python_examples.py@@ -552,6 +552,21 @@ def update_user(client: Client) -> None:validate_against_openapi_schema(result, "/users/{user_id}", "patch", "200")+@openapi_test_function("/users/{user_id}/channels:get")+def get_user_channels(client: Client) -> None:+ user_id = 7+ ensure_users([user_id], ["zoe"])+ # {code_example|start}+ # Get channel IDs a user is subscribed to.+ result = client.call_endpoint(+ url=f"/users/{user_id}/channels",+ method="GET",+ )+ # {code_example|end}+ assert_success_response(result)+ validate_against_openapi_schema(result, "/users/{user_id}/channels", "get", "200")++@openapi_test_function("/users/{user_id}/subscriptions/{stream_id}:get")def get_subscription_status(client: Client) -> None:user_id = 7@@ -1996,6 +2011,7 @@ def test_users(client: Client, owner_client: Client) -> None:get_user_status(client)get_user_by_email(client)get_subscription_status(client)+ get_user_channels(client)get_profile(client)update_settings(client)upload_file(client)diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 755a88a7024..2c18e29bd01 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -13380,6 +13380,51 @@ paths:Whether the user is subscribed to the channel.example:{"msg": "", "result": "success", "is_subscribed": false}+ /users/{user_id}/channels:+ get:+ operationId: get-user-channels+ summary: Get a user's subscribed channels+ tags: ["channels"]+ description: |+ Gets the list of channels that the target user is subscribed to.++ The [channel security model](/help/channel-permissions) means only those+ subscribed channels to which the acting user has metadata access are+ returned.++ For organization administrators, this is guaranteed to be all channels+ the target user is subscribed to, since organization administrators+ implicitly have metadata access to all channels.++ **Changes**: New in Zulip 12.0 (feature level 440).+ parameters:+ - $ref: "#/components/parameters/UserId"+ responses:+ "200":+ description: Success+ content:+ application/json:+ schema:+ allOf:+ - $ref: "#/components/schemas/JsonSuccessBase"+ - additionalProperties: false+ properties:+ result: {}+ msg: {}+ ignored_parameters_unsupported: {}+ subscribed_channel_ids:+ type: array+ items:+ type: integer+ description: |+ IDs of the channels that the target user is subscribed to+ (limited by the acting user's permissions to view subscribers).+ example:+ {+ "msg": "",+ "result": "success",+ "subscribed_channel_ids": [3, 5, 10],+ }/realm/emoji/{emoji_name}:post:operationId: upload-custom-emojidiff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex cde24e55399..5ba1ab5adc9 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1811,6 +1811,41 @@ class UserProfileTest(ZulipTestCase):)self.assertFalse(result["is_subscribed"])+ def test_get_user_channels(self) -> None:+ self.login("hamlet")+ iago = self.example_user("iago")+ stream = get_stream("Rome", iago.realm)++ # Invalid user ID.+ result = self.client_get("/json/users/25/channels")+ self.assert_json_error(result, "No such user")++ result = orjson.loads(self.client_get(f"/json/users/{iago.id}/channels").content)+ self.assertFalse(stream.id in result["subscribed_channel_ids"])++ # Subscribe to the stream.+ self.subscribe(iago, stream.name)+ result = orjson.loads(self.client_get(f"/json/users/{iago.id}/channels").content)+ self.assertTrue(stream.id in result["subscribed_channel_ids"])++ # Test access of a Guest user.+ polonius = self.example_user("polonius")+ self.assertTrue(polonius.is_guest)+ self.assertTrue(stream.is_web_public)++ with self.assert_database_query_count(6):+ result = orjson.loads(+ self.api_get(polonius, f"/api/v1/users/{iago.id}/channels").content+ )+ self.assertTrue(stream.id in result["subscribed_channel_ids"])++ # Test case when guest cannot access all users in the realm.+ self.set_up_db_for_testing_user_access()+ cordelia = self.example_user("cordelia")+ with self.assert_database_query_count(7):+ result = self.api_get(polonius, f"/api/v1/users/{cordelia.id}/channels")+ self.assert_json_error(result, "Insufficient permission")+class ActivateTest(ZulipTestCase):def test_basics(self) -> None:diff --git a/zerver/views/users.py b/zerver/views/users.pyindex 28f1d1bec5a..41f3ebe441a 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -58,7 +58,13 @@ from zerver.lib.rate_limiter import ()from zerver.lib.response import json_successfrom zerver.lib.send_email import FromAddress, send_email-from zerver.lib.streams import access_stream_by_id, access_stream_by_name, subscribed_to_stream+from zerver.lib.stream_subscription import get_user_subscribed_streams+from zerver.lib.streams import (+ access_stream_by_id,+ access_stream_by_name,+ get_metadata_access_streams,+ subscribed_to_stream,+)from zerver.lib.typed_endpoint import (ApiParamConfig,PathOnly,@@ -69,6 +75,7 @@ from zerver.lib.typed_endpoint_validators import check_int_in_validator, check_ufrom zerver.lib.types import ProfileDataElementUpdateDictfrom zerver.lib.upload import upload_avatar_imagefrom zerver.lib.url_encoding import append_url_query_string+from zerver.lib.user_groups import UserGroupMembershipDetailsfrom zerver.lib.users import (APIUserDict,access_bot_by_id,@@ -913,6 +920,23 @@ def get_subscription_backend(return json_success(request, data=subscription_status)+@typed_endpoint+def get_subscribed_channels_backend(+ request: HttpRequest,+ user_profile: UserProfile,+ *,+ user_id: PathOnly[Json[int]],+) -> HttpResponse:+ target_user = access_user_by_id(user_profile, user_id, for_admin=False)+ streams = get_metadata_access_streams(+ user_profile,+ list(get_user_subscribed_streams(target_user)),+ UserGroupMembershipDetails(user_recursive_group_ids=None),+ )++ return json_success(request, data={"subscribed_channel_ids": [stream.id for stream in streams]})++@typed_endpointdef get_user_by_email(request: HttpRequest,diff --git a/zproject/urls.py b/zproject/urls.pyindex 8ecd2a95750..8ac2fecc4f0 100644--- a/zproject/urls.py+++ b/zproject/urls.py@@ -257,6 +257,7 @@ from zerver.views.users import (get_member_backend,get_members_backend,get_profile_backend,+ get_subscribed_channels_backend,get_subscription_backend,get_user_by_email,patch_bot_backend,@@ -362,6 +363,7 @@ v1_api_and_json_patterns = [PATCH=update_user_by_id_api,DELETE=deactivate_user_backend,),+ rest_path("users/<int:user_id>/channels", GET=get_subscribed_channels_backend),rest_path("users/<int:user_id>/subscriptions/<int:stream_id>", GET=get_subscription_backend),rest_path("users/<email>", GET=get_user_by_email, PATCH=update_user_by_email_api),rest_path("bots", GET=get_bots_backend, POST=add_bot_backend),