From d28f1ddfb49a7687b34172ba2e980039d916bb4c Mon Sep 17 00:00:00 2001 From: Bojidar Marinov Date: Thu, 8 Dec 2016 22:06:23 +0200 Subject: [PATCH] zerver: Handle `update_global_notifications` in `apply_events` Fixes #2358 --- zerver/lib/actions.py | 35 +++++++++-- zerver/tests/test_events.py | 120 ++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 4 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 8652309563..d7612843a9 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -2229,14 +2229,14 @@ def do_change_enable_offline_push_notifications(user_profile, offline_push_notif log_event(event) send_event(event, [user_profile.id]) -def do_change_enable_online_push_notifications(user_profile, online_push_notifications, log=True): +def do_change_enable_online_push_notifications(user_profile, enable_online_push_notifications, log=True): # type: (UserProfile, bool, bool) -> None - user_profile.enable_online_push_notifications = online_push_notifications + user_profile.enable_online_push_notifications = enable_online_push_notifications user_profile.save(update_fields=["enable_online_push_notifications"]) event = {'type': 'update_global_notifications', 'user': user_profile.email, - 'notification_name': 'online_push_notifications', - 'setting': online_push_notifications} + 'notification_name': 'enable_online_push_notifications', + 'setting': enable_online_push_notifications} if log: log_event(event) send_event(event, [user_profile.id]) @@ -3089,6 +3089,16 @@ def fetch_initial_state_data(user_profile, event_types, queue_id): state['default_language'] = default_language + if want('update_global_notifications'): + state['enable_stream_desktop_notifications'] = user_profile.enable_stream_desktop_notifications + state['enable_stream_sounds'] = user_profile.enable_stream_sounds + state['enable_desktop_notifications'] = user_profile.enable_desktop_notifications + state['enable_sounds'] = user_profile.enable_sounds + state['enable_offline_email_notifications'] = user_profile.enable_offline_email_notifications + state['enable_offline_push_notifications'] = user_profile.enable_offline_push_notifications + state['enable_online_push_notifications'] = user_profile.enable_online_push_notifications + state['enable_digest_emails'] = user_profile.enable_digest_emails + return state def apply_events(state, events, user_profile): @@ -3265,6 +3275,23 @@ def apply_events(state, events, user_profile): state['twenty_four_hour_time'] = event["setting"] if event['setting_name'] == 'left_side_userlist': state['left_side_userlist'] = event["setting"] + elif event['type'] == "update_global_notifications": + if event['notification_name'] == "enable_stream_desktop_notifications": + state['enable_stream_desktop_notifications'] = event['setting'] + elif event['notification_name'] == "enable_stream_sounds": + state['enable_stream_sounds'] = event['setting'] + elif event['notification_name'] == "enable_desktop_notifications": + state['enable_desktop_notifications'] = event['setting'] + elif event['notification_name'] == "enable_sounds": + state['enable_sounds'] = event['setting'] + elif event['notification_name'] == "enable_offline_email_notifications": + state['enable_offline_email_notifications'] = event['setting'] + elif event['notification_name'] == "enable_offline_push_notifications": + state['enable_offline_push_notifications'] = event['setting'] + elif event['notification_name'] == "enable_online_push_notifications": + state['enable_online_push_notifications'] = event['setting'] + elif event['notification_name'] == "enable_digest_emails": + state['enable_digest_emails'] = event['setting'] else: raise ValueError("Unexpected event type %s" % (event['type'],)) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index f3a56ee8df..51f8d8cdfe 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -47,6 +47,14 @@ from zerver.lib.actions import ( do_update_pointer, do_change_twenty_four_hour_time, do_change_left_side_userlist, + do_change_enable_stream_desktop_notifications, + do_change_enable_stream_sounds, + do_change_enable_desktop_notifications, + do_change_enable_sounds, + do_change_enable_offline_email_notifications, + do_change_enable_offline_push_notifications, + do_change_enable_online_push_notifications, + do_change_enable_digest_emails, fetch_initial_state_data, get_subscription ) @@ -641,6 +649,118 @@ class EventsRegisterTest(ZulipTestCase): error = schema_checker('events[0]', events[0]) self.assert_on_error(error) + def test_change_enable_stream_desktop_notifications(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_stream_desktop_notifications')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_stream_desktop_notifications(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_stream_sounds(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_stream_sounds')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_stream_sounds(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_desktop_notifications(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_desktop_notifications')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_desktop_notifications(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_sounds(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_sounds')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_sounds(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_offline_email_notifications(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_offline_email_notifications')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_offline_email_notifications(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_offline_push_notifications(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_offline_push_notifications')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_offline_push_notifications(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_online_push_notifications(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_online_push_notifications')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_online_push_notifications(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + + def test_change_enable_digest_emails(self): + # type: () -> None + schema_checker = check_dict([ + ('type', equals('update_global_notifications')), + ('notification_name', equals('enable_digest_emails')), + ('user', check_string), + ('setting', check_bool), + ]) + # The first False is probably a noop, then we get transitions in both directions. + for setting_value in [False, True, False]: + events = self.do_test(lambda: do_change_enable_digest_emails(self.user_profile, setting_value)) + error = schema_checker('events[0]', events[0]) + self.assert_on_error(error) + def test_realm_emoji_events(self): # type: () -> None schema_checker = check_dict([