From b2c29274f34191a6f4548e0863b4ef725c8941b6 Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Sun, 14 Oct 2018 10:22:04 -0700 Subject: [PATCH] presence: Allow bots to fetch realm presence data. Before, presence information for an entire realm could only be queried via the `POST /api/v1/users/me/presence` endpoint. However, this endpoint also updates the presence information for the user making the request. Therefore, bot users are not allowed to access this endpoint because they don't have any presence data. This commit adds a new endpoint `GET /api/v1/realm/presence` that just returns the presence information for the realm of the caller. Fixes #10651. --- zerver/tests/test_presence.py | 18 ++++++++++++++++++ zerver/views/presence.py | 3 +++ zproject/urls.py | 3 +++ 3 files changed, 24 insertions(+) diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index 9a74303b8b..5f8c686b95 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -485,3 +485,21 @@ class UserPresenceAggregationTests(ZulipTestCase): "timestamp": datetime_to_timestamp(validate_time - datetime.timedelta(seconds=2)) } ) + +class GetRealmStatusesTest(ZulipTestCase): + def test_get_statuses(self) -> None: + # Setup the test by simulating users reporting their presence data. + othello_email = self.example_email("othello") + result = self.api_post(othello_email, "/api/v1/users/me/presence", {'status': 'active'}, + HTTP_USER_AGENT="ZulipAndroid/1.0") + + hamlet_email = self.example_email("hamlet") + result = self.api_post(hamlet_email, "/api/v1/users/me/presence", {'status': 'idle'}, + HTTP_USER_AGENT="ZulipDesktop/1.0") + self.assert_json_success(result) + + # Check that a bot can fetch the presence data for the realm. + result = self.api_get(self.example_email("welcome_bot"), "/api/v1/realm/presence") + self.assert_json_success(result) + json = result.json() + self.assertEqual(sorted(json['presences'].keys()), [hamlet_email, othello_email]) diff --git a/zerver/views/presence.py b/zerver/views/presence.py index d07af8bbc5..e66a1bc017 100644 --- a/zerver/views/presence.py +++ b/zerver/views/presence.py @@ -79,3 +79,6 @@ def update_active_status_backend(request: HttpRequest, user_profile: UserProfile ret['zephyr_mirror_active'] = False return json_success(ret) + +def get_statuses_for_realm(request: HttpRequest, user_profile: UserProfile) -> HttpResponse: + return json_success(get_status_list(user_profile)) diff --git a/zproject/urls.py b/zproject/urls.py index 26759b961d..9cb89ff784 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -116,6 +116,9 @@ v1_api_and_json_patterns = [ url(r'^realm/deactivate$', rest_dispatch, {'POST': 'zerver.views.realm.deactivate_realm'}), + url(r'^realm/presence$', rest_dispatch, + {'GET': 'zerver.views.presence.get_statuses_for_realm'}), + # users -> zerver.views.users # # Since some of these endpoints do something different if used on