2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-03-17 22:06:21 +00:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2016-02-18 00:08:17 +00:00
|
|
|
var SETTINGS = {
|
|
|
|
OFF : 'off',
|
|
|
|
COUNT : 'count',
|
|
|
|
NAME : 'name',
|
|
|
|
MESSAGE : 'message'
|
|
|
|
};
|
|
|
|
|
2017-09-29 16:15:28 +00:00
|
|
|
var enabled = false;
|
2017-10-03 18:08:14 +00:00
|
|
|
var sound = new Audio('audio/NewMessage.mp3');
|
2017-09-29 16:15:28 +00:00
|
|
|
|
2015-09-14 03:25:04 +00:00
|
|
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
2017-04-28 01:21:52 +00:00
|
|
|
this.on('add', this.update);
|
2016-02-22 20:55:19 +00:00
|
|
|
this.on('remove', this.onRemove);
|
2015-09-14 03:25:04 +00:00
|
|
|
},
|
2017-09-29 16:15:28 +00:00
|
|
|
onClick: function(conversationId) {
|
|
|
|
var conversation = ConversationController.get(conversationId);
|
2017-09-01 18:30:41 +00:00
|
|
|
this.trigger('click', conversation);
|
2015-09-14 03:25:04 +00:00
|
|
|
},
|
2015-11-25 23:11:01 +00:00
|
|
|
update: function() {
|
2017-09-29 16:15:28 +00:00
|
|
|
console.log(
|
|
|
|
'updating notifications - count:', this.length,
|
|
|
|
'focused:', window.isFocused(),
|
|
|
|
'enabled:', enabled
|
|
|
|
);
|
|
|
|
if (!enabled) {
|
|
|
|
return; // wait til we are re-enabled
|
|
|
|
}
|
2016-04-11 18:24:18 +00:00
|
|
|
if (this.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
if (window.isFocused()) {
|
|
|
|
// The window is focused. Consider yourself notified.
|
|
|
|
this.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.drawAttention();
|
|
|
|
|
2017-01-28 19:33:57 +00:00
|
|
|
var audioNotification = storage.get('audio-notification') || false;
|
2017-10-03 18:08:14 +00:00
|
|
|
if (audioNotification) {
|
|
|
|
sound.play();
|
|
|
|
}
|
2017-01-28 19:33:57 +00:00
|
|
|
|
2016-02-18 00:08:17 +00:00
|
|
|
var setting = storage.get('notification-setting') || 'message';
|
|
|
|
if (setting === SETTINGS.OFF) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-28 01:31:35 +00:00
|
|
|
var title;
|
|
|
|
var message;
|
|
|
|
var iconUrl;
|
|
|
|
|
|
|
|
var newMessageCount = [
|
2016-02-18 00:08:17 +00:00
|
|
|
this.length,
|
|
|
|
this.length === 1 ? i18n('newMessage') : i18n('newMessages')
|
|
|
|
].join(' ');
|
|
|
|
|
2017-04-28 01:31:35 +00:00
|
|
|
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:
|
2017-09-29 16:15:28 +00:00
|
|
|
if (this.length === 1) {
|
|
|
|
title = last.get('title');
|
|
|
|
} else {
|
|
|
|
title = newMessageCount;
|
|
|
|
}
|
2017-04-28 01:31:35 +00:00
|
|
|
message = last.get('message');
|
|
|
|
iconUrl = last.get('iconUrl');
|
|
|
|
break;
|
2017-04-28 01:21:52 +00:00
|
|
|
}
|
2017-05-04 18:06:18 +00:00
|
|
|
var notification = new Notification(title, {
|
2017-09-07 16:57:15 +00:00
|
|
|
body : message,
|
|
|
|
icon : iconUrl,
|
|
|
|
tag : 'signal',
|
2017-10-03 18:08:14 +00:00
|
|
|
silent : true
|
2017-05-04 18:06:18 +00:00
|
|
|
});
|
2017-09-29 16:15:28 +00:00
|
|
|
|
|
|
|
notification.onclick = this.onClick.bind(this, last.get('conversationId'));
|
|
|
|
|
|
|
|
// We don't want to notify the user about these same messages again
|
|
|
|
this.clear();
|
2015-09-14 03:25:04 +00:00
|
|
|
},
|
2016-02-18 00:08:17 +00:00
|
|
|
getSetting: function() {
|
2017-09-29 16:15:28 +00:00
|
|
|
return storage.get('notification-setting') || SETTINGS.MESSAGE;
|
2016-02-18 00:08:17 +00:00
|
|
|
},
|
2016-02-22 20:55:19 +00:00
|
|
|
onRemove: function() {
|
2016-07-29 01:09:09 +00:00
|
|
|
console.log('remove notification');
|
2016-02-22 20:55:19 +00:00
|
|
|
},
|
2015-09-14 03:25:04 +00:00
|
|
|
clear: function() {
|
2017-09-29 16:15:28 +00:00
|
|
|
console.log('remove all notifications');
|
2015-09-14 03:25:04 +00:00
|
|
|
this.reset([]);
|
2017-09-29 16:15:28 +00:00
|
|
|
},
|
|
|
|
enable: function() {
|
|
|
|
var update = !enabled;
|
|
|
|
enabled = true;
|
|
|
|
if (update) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disable: function() {
|
|
|
|
enabled = false;
|
|
|
|
},
|
|
|
|
|
2015-09-14 03:25:04 +00:00
|
|
|
}))();
|
2015-03-17 22:06:21 +00:00
|
|
|
})();
|