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