From 10a38297b8d0689f6ba774ee2b5609aa2a5a081b Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 29 Sep 2017 09:15:28 -0700 Subject: [PATCH] Only show notifications when done with sync (#1507) This prevents the parade of notifications if a machine wakes up from sleep. Basically covers situations that the loading screen doesn't already. When disabled, notifications will be cached until they are subsequently re-enabled, at which time all the pending notifications will be summarized. From the background page, notifications are disabled during connection attempts until an empty event. This means we can always safely call conversation.notify to queue a notification for the next batch, dropping some options from message and conversation model methods. We've also moved the calls to check window focus and draw attention to the window, which were previously included in the conversation model, but are now performed by the Notification system, because the time that the notification is displayed might be some time after the message is added by the conversation, so decisions about focus and attention should be made in that moment and not before. // FREEBIE --- js/background.js | 8 +++-- js/models/conversations.js | 7 +--- js/models/messages.js | 7 ++-- js/notifications.js | 66 +++++++++++++++++++++++++------------- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/js/background.js b/js/background.js index b785a1450..ee9cb75e0 100644 --- a/js/background.js +++ b/js/background.js @@ -179,6 +179,8 @@ retryCached: connectCount === 1, }; + Whisper.Notifications.disable(); // avoid notification flood until empty + // initialize the socket and start listening for messages messageReceiver = new textsecure.MessageReceiver( SERVER_URL, USERNAME, PASSWORD, mySignalingKey, options @@ -239,6 +241,8 @@ view.onEmpty(); } }, 500); + + Whisper.Notifications.enable(); } function onProgress(ev) { var count = ev.count; @@ -509,9 +513,7 @@ } conversation.trigger('newmessage', message); - if (initialLoadComplete) { - conversation.notify(message); - } + conversation.notify(message); if (ev.confirm) { ev.confirm(); diff --git a/js/models/conversations.js b/js/models/conversations.js index c876fba74..74c538eb7 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -1073,14 +1073,9 @@ if (!message.isIncoming()) { return Promise.resolve(); } - if (window.isFocused()) { - return Promise.resolve(); - } - - window.drawAttention(); var conversationId = this.id; - ConversationController.getOrCreateAndWait(message.get('source'), 'private') + return ConversationController.getOrCreateAndWait(message.get('source'), 'private') .then(function(sender) { return sender.getNotificationIcon().then(function(iconUrl) { console.log('adding notification'); diff --git a/js/models/messages.js b/js/models/messages.js index 38cbf715c..c13e05625 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -326,10 +326,7 @@ this.send(promise); } }, - handleDataMessage: function(dataMessage, confirm, options) { - options = options || {}; - _.defaults(options, {initialLoadComplete: true}); - + handleDataMessage: function(dataMessage, confirm) { // This function is called from the background script in a few scenarios: // 1. on an incoming message // 2. on a sent message sync'd from another device @@ -496,7 +493,7 @@ // because we need to start expiration timers, etc. message.markRead(); } - if (message.get('unread') && options.initialLoadComplete) { + if (message.get('unread')) { conversation.notify(message); } diff --git a/js/notifications.js b/js/notifications.js index 8ca4576fa..b6f76eefe 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -12,25 +12,37 @@ MESSAGE : 'message' }; + var enabled = false; + Whisper.Notifications = new (Backbone.Collection.extend({ initialize: function() { this.on('add', this.update); this.on('remove', this.onRemove); }, - onclick: function() { - var conversation; - var last = this.last(); - if (last) { - conversation = ConversationController.get(last.get('conversationId')); - } + onClick: function(conversationId) { + var conversation = ConversationController.get(conversationId); this.trigger('click', conversation); - this.clear(); }, update: function() { - console.log('updating notifications', this.length); + console.log( + 'updating notifications - count:', this.length, + 'focused:', window.isFocused(), + 'enabled:', enabled + ); + if (!enabled) { + return; // wait til we are re-enabled + } if (this.length === 0) { return; } + if (window.isFocused()) { + // The window is focused. Consider yourself notified. + this.clear(); + return; + } + + window.drawAttention(); + var audioNotification = storage.get('audio-notification') || false; var setting = storage.get('notification-setting') || 'message'; @@ -59,7 +71,11 @@ iconUrl = last.get('iconUrl'); break; case SETTINGS.MESSAGE: - title = last.get('title'); + if (this.length === 1) { + title = last.get('title'); + } else { + title = newMessageCount; + } message = last.get('message'); iconUrl = last.get('iconUrl'); break; @@ -70,26 +86,32 @@ tag : 'signal', silent : !audioNotification }); - notification.onclick = this.onclick.bind(this); + + notification.onclick = this.onClick.bind(this, last.get('conversationId')); + + // We don't want to notify the user about these same messages again + this.clear(); }, getSetting: function() { - return storage.get('notification-setting') || 'message'; - }, - showMessage: function() { - return this.getSetting() === SETTINGS.MESSAGE; - }, - showSender: function() { - var setting = this.getSetting(); - return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME); + return storage.get('notification-setting') || SETTINGS.MESSAGE; }, onRemove: function() { console.log('remove notification'); - if (this.length === 0) { - return; - } }, clear: function() { + console.log('remove all notifications'); this.reset([]); - } + }, + enable: function() { + var update = !enabled; + enabled = true; + if (update) { + this.update(); + } + }, + disable: function() { + enabled = false; + }, + }))(); })();