status: Add away_user_ids to page_params.

(Also, any client that registers for 'user_status'
will see `away_user_ids`.)
This commit is contained in:
Steve Howell 2018-12-18 16:17:08 +00:00 committed by Tim Abbott
parent 423db23c36
commit a2614956d5
3 changed files with 39 additions and 4 deletions

View File

@ -44,6 +44,7 @@ from zerver.lib.actions import (
get_available_notification_sounds, get_available_notification_sounds,
) )
from zerver.lib.user_groups import user_groups_in_realm_serialized 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.tornado.event_queue import request_event_queue, get_user_events
from zerver.models import Client, Message, Realm, UserPresence, UserProfile, CustomProfileFieldValue, \ from zerver.models import Client, Message, Realm, UserPresence, UserProfile, CustomProfileFieldValue, \
get_user_profile_by_id, \ get_user_profile_by_id, \
@ -301,6 +302,9 @@ def fetch_initial_state_data(user_profile: UserProfile,
state[notification] = getattr(user_profile, notification) state[notification] = getattr(user_profile, notification)
state['available_notification_sounds'] = get_available_notification_sounds() 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'): if want('zulip_version'):
state['zulip_version'] = ZULIP_VERSION state['zulip_version'] = ZULIP_VERSION
@ -670,6 +674,16 @@ def apply_event(state: Dict[str, Any],
elif event['op'] == 'remove': elif event['op'] == 'remove':
state['realm_user_groups'] = [ug for ug in state['realm_user_groups'] state['realm_user_groups'] = [ug for ug in state['realm_user_groups']
if ug['id'] != event['group_id']] 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: else:
raise AssertionError("Unexpected event type %s" % (event['type'],)) raise AssertionError("Unexpected event type %s" % (event['type'],))

View File

@ -69,7 +69,9 @@ from zerver.lib.actions import (
do_remove_realm_filter, do_remove_realm_filter,
do_remove_streams_from_default_stream_group, do_remove_streams_from_default_stream_group,
do_rename_stream, do_rename_stream,
do_revoke_away_status,
do_revoke_user_invite, do_revoke_user_invite,
do_set_away_status,
do_set_realm_authentication_methods, do_set_realm_authentication_methods,
do_set_realm_message_editing, do_set_realm_message_editing,
do_set_realm_property, do_set_realm_property,
@ -1200,6 +1202,23 @@ class EventsRegisterTest(ZulipTestCase):
error = alert_words_checker('events[0]', events[0]) error = alert_words_checker('events[0]', events[0])
self.assert_on_error(error) 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: def test_user_group_events(self) -> None:
user_group_add_checker = self.check_events_dict([ user_group_add_checker = self.check_events_dict([
('type', equals('user_group')), ('type', equals('user_group')),
@ -3106,7 +3125,7 @@ class FetchQueriesTest(ZulipTestCase):
client_gravatar=False, client_gravatar=False,
) )
self.assert_length(queries, 30) self.assert_length(queries, 31)
expected_counts = dict( expected_counts = dict(
alert_words=0, alert_words=0,
@ -3132,6 +3151,7 @@ class FetchQueriesTest(ZulipTestCase):
update_display_settings=0, update_display_settings=0,
update_global_notifications=0, update_global_notifications=0,
update_message_flags=5, update_message_flags=5,
user_status=1,
zulip_version=0, zulip_version=0,
) )

View File

@ -51,6 +51,7 @@ class HomeTest(ZulipTestCase):
"avatar_source", "avatar_source",
"avatar_url", "avatar_url",
"avatar_url_medium", "avatar_url_medium",
"away_user_ids",
"bot_types", "bot_types",
"can_create_streams", "can_create_streams",
"can_subscribe_other_users", "can_subscribe_other_users",
@ -217,7 +218,7 @@ class HomeTest(ZulipTestCase):
with patch('zerver.lib.cache.cache_set') as cache_mock: with patch('zerver.lib.cache.cache_set') as cache_mock:
result = self._get_home_page(stream='Denmark') 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) self.assert_length(cache_mock.call_args_list, 7)
html = result.content.decode('utf-8') html = result.content.decode('utf-8')
@ -283,7 +284,7 @@ class HomeTest(ZulipTestCase):
result = self._get_home_page() result = self._get_home_page()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assert_length(cache_mock.call_args_list, 6) 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.") @slow("Creates and subscribes 10 users in a loop. Should use bulk queries.")
def test_num_queries_with_streams(self) -> None: def test_num_queries_with_streams(self) -> None:
@ -315,7 +316,7 @@ class HomeTest(ZulipTestCase):
with queries_captured() as queries2: with queries_captured() as queries2:
result = self._get_home_page() 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. # Do a sanity check that our new streams were in the payload.
html = result.content.decode('utf-8') html = result.content.decode('utf-8')