session-desktop/js/notifications.js
Scott Nonnenberg 10a38297b8 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
2017-09-29 09:15:28 -07:00

117 lines
3.4 KiB
JavaScript

/*
* vim: ts=4:sw=4:expandtab
*/
;(function() {
'use strict';
window.Whisper = window.Whisper || {};
var SETTINGS = {
OFF : 'off',
COUNT : 'count',
NAME : 'name',
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(conversationId) {
var conversation = ConversationController.get(conversationId);
this.trigger('click', conversation);
},
update: function() {
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';
if (setting === SETTINGS.OFF) {
return;
}
var title;
var message;
var iconUrl;
var newMessageCount = [
this.length,
this.length === 1 ? i18n('newMessage') : i18n('newMessages')
].join(' ');
var last = this.last();
switch (this.getSetting()) {
case SETTINGS.COUNT:
title = 'Signal';
message = newMessageCount;
break;
case SETTINGS.NAME:
title = newMessageCount;
message = 'Most recent from ' + last.get('title');
iconUrl = last.get('iconUrl');
break;
case SETTINGS.MESSAGE:
if (this.length === 1) {
title = last.get('title');
} else {
title = newMessageCount;
}
message = last.get('message');
iconUrl = last.get('iconUrl');
break;
}
var notification = new Notification(title, {
body : message,
icon : iconUrl,
tag : 'signal',
silent : !audioNotification
});
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') || SETTINGS.MESSAGE;
},
onRemove: function() {
console.log('remove notification');
},
clear: function() {
console.log('remove all notifications');
this.reset([]);
},
enable: function() {
var update = !enabled;
enabled = true;
if (update) {
this.update();
}
},
disable: function() {
enabled = false;
},
}))();
})();