diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 9ffadb5200..e76f453ba7 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -44,6 +44,7 @@ from zerver.lib.actions import ( get_available_notification_sounds, ) from zerver.lib.user_groups import user_groups_in_realm_serialized +from zerver.lib.user_status import get_away_user_ids from zerver.tornado.event_queue import request_event_queue, get_user_events from zerver.models import Client, Message, Realm, UserPresence, UserProfile, CustomProfileFieldValue, \ get_user_profile_by_id, \ @@ -301,6 +302,9 @@ def fetch_initial_state_data(user_profile: UserProfile, state[notification] = getattr(user_profile, notification) state['available_notification_sounds'] = get_available_notification_sounds() + if want('user_status'): + state['away_user_ids'] = sorted(list(get_away_user_ids(realm_id=realm.id))) + if want('zulip_version'): state['zulip_version'] = ZULIP_VERSION @@ -670,6 +674,16 @@ def apply_event(state: Dict[str, Any], elif event['op'] == 'remove': state['realm_user_groups'] = [ug for ug in state['realm_user_groups'] if ug['id'] != event['group_id']] + elif event['type'] == 'user_status': + away_user_ids = set(state['away_user_ids']) + user_id = event['user_id'] + + if event['away']: + away_user_ids.add(user_id) + else: + away_user_ids.discard(user_id) + + state['away_user_ids'] = sorted(list(away_user_ids)) else: raise AssertionError("Unexpected event type %s" % (event['type'],)) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 32f8b5fdde..b7380d1db2 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -69,7 +69,9 @@ from zerver.lib.actions import ( do_remove_realm_filter, do_remove_streams_from_default_stream_group, do_rename_stream, + do_revoke_away_status, do_revoke_user_invite, + do_set_away_status, do_set_realm_authentication_methods, do_set_realm_message_editing, do_set_realm_property, @@ -1200,6 +1202,23 @@ class EventsRegisterTest(ZulipTestCase): error = alert_words_checker('events[0]', events[0]) self.assert_on_error(error) + def test_away_events(self) -> None: + checker = self.check_events_dict([ + ('type', equals('user_status')), + ('user_id', check_int), + ('away', check_bool), + ]) + + client = get_client("website") + events = self.do_test(lambda: do_set_away_status(user_profile=self.user_profile, + client_id=client.id)) + error = checker('events[0]', events[0]) + self.assert_on_error(error) + + events = self.do_test(lambda: do_revoke_away_status(user_profile=self.user_profile)) + error = checker('events[0]', events[0]) + self.assert_on_error(error) + def test_user_group_events(self) -> None: user_group_add_checker = self.check_events_dict([ ('type', equals('user_group')), @@ -3106,7 +3125,7 @@ class FetchQueriesTest(ZulipTestCase): client_gravatar=False, ) - self.assert_length(queries, 30) + self.assert_length(queries, 31) expected_counts = dict( alert_words=0, @@ -3132,6 +3151,7 @@ class FetchQueriesTest(ZulipTestCase): update_display_settings=0, update_global_notifications=0, update_message_flags=5, + user_status=1, zulip_version=0, ) diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index d74e9f2b92..14e51d1be0 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -51,6 +51,7 @@ class HomeTest(ZulipTestCase): "avatar_source", "avatar_url", "avatar_url_medium", + "away_user_ids", "bot_types", "can_create_streams", "can_subscribe_other_users", @@ -217,7 +218,7 @@ class HomeTest(ZulipTestCase): with patch('zerver.lib.cache.cache_set') as cache_mock: result = self._get_home_page(stream='Denmark') - self.assert_length(queries, 42) + self.assert_length(queries, 43) self.assert_length(cache_mock.call_args_list, 7) html = result.content.decode('utf-8') @@ -283,7 +284,7 @@ class HomeTest(ZulipTestCase): result = self._get_home_page() self.assertEqual(result.status_code, 200) self.assert_length(cache_mock.call_args_list, 6) - self.assert_length(queries, 39) + self.assert_length(queries, 40) @slow("Creates and subscribes 10 users in a loop. Should use bulk queries.") def test_num_queries_with_streams(self) -> None: @@ -315,7 +316,7 @@ class HomeTest(ZulipTestCase): with queries_captured() as queries2: result = self._get_home_page() - self.assert_length(queries2, 36) + self.assert_length(queries2, 37) # Do a sanity check that our new streams were in the payload. html = result.content.decode('utf-8')