From 4790316b57810d7f1ed5da1261597a1416b85378 Mon Sep 17 00:00:00 2001 From: Tomasz Kolek Date: Mon, 19 Sep 2016 22:55:18 +0200 Subject: [PATCH] Add user setting option to always send push notifications. Add option in user's settings for getting mobile push notifications even if a Zulip browser is online. Default is False. Fixes: #1596. --- frontend_tests/node_tests/i18n.js | 1 + frontend_tests/node_tests/templates.js | 4 ++- static/js/notifications.js | 4 ++- static/js/settings.js | 7 +++++- .../settings/notification-settings.handlebars | 11 ++++++++ zerver/lib/actions.py | 25 ++++++++++++------- ...rofile_enable_online_push_notifications.py | 19 ++++++++++++++ zerver/models.py | 6 +++++ zerver/tests/tests.py | 2 ++ zerver/views/__init__.py | 2 ++ zerver/views/user_settings.py | 13 +++++++--- zilencer/management/commands/populate_db.py | 6 +++++ 12 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 zerver/migrations/0034_userprofile_enable_online_push_notifications.py diff --git a/frontend_tests/node_tests/i18n.js b/frontend_tests/node_tests/i18n.js index 53abc7c096..54a4de8a8a 100644 --- a/frontend_tests/node_tests/i18n.js +++ b/frontend_tests/node_tests/i18n.js @@ -75,6 +75,7 @@ function render(template_name, args) { "sounds_enabled": false, "enable_offline_email_notifications": false, "enable_offline_push_notifications": false, + "enable_online_push_notifications": false, "enable_digest_emails": false, "domain": "zulip.com", "autoscroll_forever": false, diff --git a/frontend_tests/node_tests/templates.js b/frontend_tests/node_tests/templates.js index 8b5fb7a6b2..61646edd76 100644 --- a/frontend_tests/node_tests/templates.js +++ b/frontend_tests/node_tests/templates.js @@ -539,7 +539,8 @@ fs.readdirSync(path.join(__dirname, "../../static/templates/", "settings")).forE stream_desktop_notifications_enabled: true, stream_sounds_enabled: true, desktop_notifications_enabled: true, sounds_enabled: true, enable_offline_email_notifications: true, - enable_offline_push_notifications: true, enable_digest_emails: true, + enable_offline_push_notifications: true, enable_online_push_notifications: true, + enable_digest_emails: true, autoscroll_forever: true, default_desktop_notifications: true }; var page_params = $.extend(page_param_checkbox_options, { @@ -551,6 +552,7 @@ fs.readdirSync(path.join(__dirname, "../../static/templates/", "settings")).forE var checkbox_ids = ["enable_stream_desktop_notifications", "enable_stream_sounds", "enable_desktop_notifications", "enable_sounds", "enable_offline_push_notifications", + "enable_online_push_notifications", "enable_digest_emails", "autoscroll_forever", "default_desktop_notifications"]; diff --git a/static/js/notifications.js b/static/js/notifications.js index f5269aee51..6a416a46ff 100644 --- a/static/js/notifications.js +++ b/static/js/notifications.js @@ -608,7 +608,9 @@ exports.handle_global_notification_updates = function (notification_name, settin } else if (notification_name === "enable_offline_email_notifications") { page_params.enable_offline_email_notifications = setting; } else if (notification_name === "enable_offline_push_notifications") { - page_params.enable_offline_push_notifications= setting; + page_params.enable_offline_push_notifications = setting; + } else if (notification_name === "enable_online_push_notifications") { + page_params.enable_online_push_notifications = setting; } else if (notification_name === "enable_digest_emails") { page_params.enable_digest_emails = setting; } diff --git a/static/js/settings.js b/static/js/settings.js index 8ba558188e..8dd661bde7 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -284,6 +284,10 @@ function _setup_page() { page_params.enable_offline_push_notifications = result.enable_offline_push_notifications; } + if (result.enable_online_push_notifications !== undefined) { + page_params.enable_online_push_notifications = result.enable_online_push_notifications; + } + // Other notification settings. if (result.enable_digest_emails !== undefined) { @@ -312,7 +316,8 @@ function _setup_page() { _.each(["enable_stream_desktop_notifications", "enable_stream_sounds", "enable_desktop_notifications", "enable_sounds", "enable_offline_email_notifications", - "enable_offline_push_notifications", "enable_digest_emails"], + "enable_offline_push_notifications", "enable_online_push_notifications", + "enable_digest_emails"], function (setting) { updated_settings[setting] = $("#" + setting).is(":checked"); }); diff --git a/static/templates/settings/notification-settings.handlebars b/static/templates/settings/notification-settings.handlebars index fc9164d22a..f2130e9d09 100644 --- a/static/templates/settings/notification-settings.handlebars +++ b/static/templates/settings/notification-settings.handlebars @@ -85,6 +85,17 @@ {{t "Mobile push notifications when offline" }} + +
+ + +
+
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 201c223d12..aed484adc5 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -35,7 +35,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, UserActivityInterval, get_active_user_dicts_in_realm, get_active_streams, \ realm_filters_for_domain, RealmFilter, receives_offline_notifications, \ ScheduledJob, realm_filters_for_domain, get_owned_bot_dicts, \ - get_old_unclaimed_attachments, get_cross_realm_users + get_old_unclaimed_attachments, get_cross_realm_users, receives_online_notifications from zerver.lib.alert_words import alert_words_in_realm from zerver.lib.avatar import get_avatar_url, avatar_url @@ -625,12 +625,6 @@ def log_message(message): if not message.sending_client.name.startswith("test:"): log_event(message.to_log_dict()) -def always_push_notify(user): - # type: (UserProfile) -> bool - # robinhood.io asked to get push notifications for **all** notifyable - # messages, regardless of idle status - return user.realm.domain in ['robinhood.io'] - # Helper function. Defaults here are overriden by those set in do_send_messages def do_send_message(message, rendered_content = None, no_log = False, stream = None, local_id = None): # type: (Union[int, Message], Optional[text_type], bool, Optional[Stream], Optional[int]) -> int @@ -701,6 +695,7 @@ def do_send_messages(messages): 'user_profile__id', 'user_profile__email', 'user_profile__is_active', + 'user_profile__enable_online_push_notifications', 'user_profile__realm__domain' ] query = Subscription.objects.select_related("user_profile", "user_profile__realm").only(*fields).filter( @@ -783,7 +778,7 @@ def do_send_messages(messages): presences = presences) users = [{'id': user.id, 'flags': user_flags.get(user.id, []), - 'always_push_notify': always_push_notify(user)} + 'always_push_notify': user.enable_online_push_notifications} for user in message['active_recipients']] if message['message'].recipient.type == Recipient.STREAM: # Note: This is where authorization for single-stream @@ -2098,6 +2093,18 @@ 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): + # type: (UserProfile, bool, bool) -> None + user_profile.enable_online_push_notifications = 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} + if log: + log_event(event) + send_event(event, [user_profile.id]) + def do_change_enable_digest_emails(user_profile, enable_digest_emails, log=True): # type: (UserProfile, bool, bool) -> None user_profile.enable_digest_emails = enable_digest_emails @@ -3132,7 +3139,7 @@ def handle_push_notification(user_profile_id, missed_message): # type: (int, Dict[str, Any]) -> None try: user_profile = get_user_profile_by_id(user_profile_id) - if not receives_offline_notifications(user_profile): + if not (receives_offline_notifications(user_profile) or receives_online_notifications(user_profile)): return umessage = UserMessage.objects.get(user_profile=user_profile, diff --git a/zerver/migrations/0034_userprofile_enable_online_push_notifications.py b/zerver/migrations/0034_userprofile_enable_online_push_notifications.py new file mode 100644 index 0000000000..673dff2607 --- /dev/null +++ b/zerver/migrations/0034_userprofile_enable_online_push_notifications.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('zerver', '0033_migrate_domain_to_realmalias'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='enable_online_push_notifications', + field=models.BooleanField(default=False), + ), + ] diff --git a/zerver/models.py b/zerver/models.py index 4f73824d12..d71640c290 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -446,6 +446,7 @@ class UserProfile(ModelReprMixin, AbstractBaseUser, PermissionsMixin): enable_sounds = models.BooleanField(default=True) # type: bool enable_offline_email_notifications = models.BooleanField(default=True) # type: bool enable_offline_push_notifications = models.BooleanField(default=True) # type: bool + enable_online_push_notifications = models.BooleanField(default=False) # type: bool enable_digest_emails = models.BooleanField(default=True) # type: bool @@ -556,6 +557,11 @@ def receives_offline_notifications(user_profile): user_profile.enable_offline_push_notifications) and not user_profile.is_bot) +def receives_online_notifications(user_profile): + # type: (UserProfile) -> bool + return (user_profile.enable_online_push_notifications and + not user_profile.is_bot) + # Make sure we flush the UserProfile object from our remote cache # whenever we save it. post_save.connect(flush_user_profile, sender=UserProfile) diff --git a/zerver/tests/tests.py b/zerver/tests/tests.py index 09c76a685b..e15bba2f85 100644 --- a/zerver/tests/tests.py +++ b/zerver/tests/tests.py @@ -1564,6 +1564,7 @@ class ChangeSettingsTest(ZulipTestCase): self.check_for_toggle_param("/json/notify_settings/change", "enable_sounds") self.check_for_toggle_param("/json/notify_settings/change", "enable_offline_email_notifications") self.check_for_toggle_param("/json/notify_settings/change", "enable_offline_push_notifications") + self.check_for_toggle_param("/json/notify_settings/change", "enable_online_push_notifications") self.check_for_toggle_param("/json/notify_settings/change", "enable_digest_emails") def test_ui_settings(self): @@ -1785,6 +1786,7 @@ class HomeTest(ZulipTestCase): "enable_digest_emails", "enable_offline_email_notifications", "enable_offline_push_notifications", + "enable_online_push_notifications", "enter_sends", "event_queue_id", "first_in_realm", diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 7edd6e9d5c..842a447059 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -569,6 +569,8 @@ def home(request): user_profile.enable_offline_email_notifications, enable_offline_push_notifications = user_profile.enable_offline_push_notifications, + enable_online_push_notifications = + user_profile.enable_online_push_notifications, twenty_four_hour_time = register_ret['twenty_four_hour_time'], enable_digest_emails = user_profile.enable_digest_emails, diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index d363d46842..ca51523e51 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -12,8 +12,8 @@ from zerver.lib.actions import do_change_password, \ do_change_full_name, do_change_enable_desktop_notifications, \ do_change_enter_sends, do_change_enable_sounds, \ do_change_enable_offline_email_notifications, do_change_enable_digest_emails, \ - do_change_enable_offline_push_notifications, do_change_autoscroll_forever, \ - do_change_default_desktop_notifications, \ + do_change_enable_offline_push_notifications, do_change_enable_online_push_notifications, \ + do_change_default_desktop_notifications, do_change_autoscroll_forever, \ do_change_enable_stream_desktop_notifications, do_change_enable_stream_sounds, \ do_regenerate_api_key, do_change_avatar_source, do_change_twenty_four_hour_time, \ do_change_left_side_userlist, do_change_default_language @@ -138,9 +138,11 @@ def json_change_notify_settings(request, user_profile, default=None), enable_offline_push_notifications=REQ(validator=check_bool, default=None), + enable_online_push_notifications=REQ(validator=check_bool, + default=None), enable_digest_emails=REQ(validator=check_bool, default=None)): - # type: (HttpRequest, UserProfile, Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool]) -> HttpResponse + # type: (HttpRequest, UserProfile, Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool]) -> HttpResponse result = {} # Stream notification settings. @@ -178,6 +180,11 @@ def json_change_notify_settings(request, user_profile, do_change_enable_offline_push_notifications(user_profile, enable_offline_push_notifications) result['enable_offline_push_notifications'] = enable_offline_push_notifications + if enable_online_push_notifications is not None and \ + user_profile.enable_online_push_notifications != enable_online_push_notifications: + do_change_enable_online_push_notifications(user_profile, enable_online_push_notifications) + result['enable_online_push_notifications'] = enable_online_push_notifications + if enable_digest_emails is not None and \ user_profile.enable_digest_emails != enable_digest_emails: do_change_enable_digest_emails(user_profile, enable_digest_emails) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 537456d652..69db571817 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -618,6 +618,12 @@ def restore_saved_messages(): old_message["enable_offline_push_notifications"] != "false") user_profile.save(update_fields=["enable_offline_push_notifications"]) continue + elif message_type == "enable_online_push_notifications_changed": + user_profile = users[old_message["user"]] + user_profile.enable_online_push_notifications_changed = ( + old_message["enable_online_push_notifications_changed"] != "false") + user_profile.save(update_fields=["enable_online_push_notifications_changed"]) + continue elif message_type == "default_streams": set_default_streams(get_realm(old_message["domain"]), old_message["streams"])