Notification Improvements (#2364)

Notification Improvements
This commit is contained in:
Scott Nonnenberg 2018-05-10 17:45:47 -07:00 committed by GitHub
commit 160de2c924
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 25 deletions

View file

@ -240,7 +240,7 @@
}); });
window.addEventListener('focus', () => Whisper.Notifications.clear()); window.addEventListener('focus', () => Whisper.Notifications.clear());
window.addEventListener('unload', () => Whisper.Notifications.clear()); window.addEventListener('unload', () => Whisper.Notifications.fastClear());
Whisper.events.on('showConversation', function(conversation) { Whisper.events.on('showConversation', function(conversation) {
if (appView) { if (appView) {

View file

@ -8,6 +8,7 @@
/* global Signal: false */ /* global Signal: false */
/* global storage: false */ /* global storage: false */
/* global Whisper: false */ /* global Whisper: false */
/* global _: false */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
(function() { (function() {
@ -29,12 +30,25 @@
this.on('remove', this.onRemove); this.on('remove', this.onRemove);
this.lastNotification = null; this.lastNotification = null;
// Testing indicated that trying to create/destroy notifications too quickly
// resulted in notifications that stuck around forever, requiring the user
// to manually close them. This introduces a minimum amount of time between calls,
// and batches up the quick successive update() calls we get from an incoming
// read sync, which might have a number of messages referenced inside of it.
this.fastUpdate = this.update;
this.update = _.debounce(this.update, 1000);
}, },
onClick(conversationId) { onClick(conversationId) {
const conversation = ConversationController.get(conversationId); const conversation = ConversationController.get(conversationId);
this.trigger('click', conversation); this.trigger('click', conversation);
}, },
update() { update() {
if (this.lastNotification) {
this.lastNotification.close();
this.lastNotification = null;
}
const { isEnabled } = this; const { isEnabled } = this;
const isAppFocused = isFocused(); const isAppFocused = isFocused();
const isAudioNotificationEnabled = const isAudioNotificationEnabled =
@ -62,7 +76,7 @@
if (status.type !== 'ok') { if (status.type !== 'ok') {
if (status.shouldClearNotifications) { if (status.shouldClearNotifications) {
this.clear(); this.reset([]);
} }
return; return;
@ -148,18 +162,19 @@
}, },
onRemove() { onRemove() {
console.log('Remove notification'); console.log('Remove notification');
if (this.length === 0) { this.update();
this.clear();
} else {
this.update();
}
}, },
clear() { clear() {
console.log('Remove all notifications'); console.log('Remove all notifications');
if (this.lastNotification) {
this.lastNotification.close();
}
this.reset([]); this.reset([]);
this.update();
},
// We don't usually call this, but when the process is shutting down, we should at
// least try to remove the notification immediately instead of waiting for the
// normal debounce.
fastClear() {
this.reset([]);
this.fastUpdate();
}, },
enable() { enable() {
const needUpdate = !this.isEnabled; const needUpdate = !this.isEnabled;

View file

@ -24,21 +24,32 @@
message.get('source') === receipt.get('sender') message.get('source') === receipt.get('sender')
); );
}); });
if (message) { const notificationForMessage = message
Whisper.Notifications.remove(message); ? Whisper.Notifications.findWhere({ messageId: message.id })
return message.markRead(receipt.get('read_at')).then( : null;
function() { const removedNotification = Whisper.Notifications.remove(
this.notifyConversation(message); notificationForMessage
this.remove(receipt); );
}.bind(this) const receiptSender = receipt.get('sender');
); const receiptTimestamp = receipt.get('timestamp');
} else { const wasMessageFound = Boolean(message);
console.log( const wasNotificationFound = Boolean(notificationForMessage);
'No message for read sync', const wasNotificationRemoved = Boolean(removedNotification);
receipt.get('sender'), console.log('Receive read sync:', {
receipt.get('timestamp') receiptSender,
); receiptTimestamp,
} wasMessageFound,
wasNotificationFound,
wasNotificationRemoved,
});
return message
? message.markRead(receipt.get('read_at')).then(
function() {
this.notifyConversation(message);
this.remove(receipt);
}.bind(this)
)
: Promise.resolve();
}.bind(this) }.bind(this)
); );
}, },