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-01-28 19:33:57 +00:00
|
|
|
var sound = new Audio('/audio/NewMessage.mp3');
|
|
|
|
|
2015-09-14 03:25:04 +00:00
|
|
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
2016-04-11 18:24:18 +00:00
|
|
|
this.on('add', _.debounce(this.update.bind(this), 1000));
|
2016-02-22 20:55:19 +00:00
|
|
|
this.on('remove', this.onRemove);
|
2015-09-14 03:25:04 +00:00
|
|
|
},
|
|
|
|
onclick: function() {
|
|
|
|
var last = this.last();
|
|
|
|
if (!last) {
|
|
|
|
openInbox();
|
|
|
|
return;
|
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
var conversation = ConversationController.get(last.get('conversationId'));
|
2015-09-14 03:25:04 +00:00
|
|
|
openConversation(conversation);
|
|
|
|
this.clear();
|
|
|
|
},
|
2015-11-25 23:11:01 +00:00
|
|
|
update: function() {
|
2016-07-18 23:00:11 +00:00
|
|
|
console.log('updating notifications', this.length);
|
2016-04-11 18:24:18 +00:00
|
|
|
extension.notification.clear();
|
|
|
|
if (this.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 19:33:57 +00:00
|
|
|
var audioNotification = storage.get('audio-notification') || false;
|
|
|
|
if (audioNotification) {
|
|
|
|
sound.play();
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:08:17 +00:00
|
|
|
var setting = storage.get('notification-setting') || 'message';
|
|
|
|
if (setting === SETTINGS.OFF) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var iconUrl = 'images/icon_128.png';
|
|
|
|
var title = [
|
|
|
|
this.length,
|
|
|
|
this.length === 1 ? i18n('newMessage') : i18n('newMessages')
|
|
|
|
].join(' ');
|
|
|
|
|
|
|
|
if (setting === SETTINGS.COUNT) {
|
|
|
|
extension.notification.update({
|
|
|
|
type : 'basic',
|
|
|
|
title : title,
|
|
|
|
iconUrl : iconUrl
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 03:25:04 +00:00
|
|
|
if (this.length > 1) {
|
|
|
|
var conversationIds = _.uniq(this.map(function(m) {
|
|
|
|
return m.get('conversationId');
|
|
|
|
}));
|
2016-02-18 00:08:17 +00:00
|
|
|
if (conversationIds.length === 1 && this.showSender()) {
|
2015-09-14 03:25:04 +00:00
|
|
|
iconUrl = this.at(0).get('iconUrl');
|
|
|
|
}
|
2015-11-25 23:11:01 +00:00
|
|
|
extension.notification.update({
|
2015-09-14 03:25:04 +00:00
|
|
|
type : 'list',
|
|
|
|
iconUrl : iconUrl,
|
2016-02-18 00:08:17 +00:00
|
|
|
title : title,
|
2015-09-14 03:25:04 +00:00
|
|
|
message : 'Most recent from ' + this.last().get('title'),
|
|
|
|
items : this.map(function(m) {
|
2016-02-18 00:08:17 +00:00
|
|
|
var message, title;
|
|
|
|
if (this.showMessage()) {
|
|
|
|
return {
|
|
|
|
title : m.get('title'),
|
|
|
|
message : m.get('message')
|
|
|
|
};
|
|
|
|
} else if (this.showSender()) {
|
|
|
|
return {
|
|
|
|
title : m.get('title'),
|
|
|
|
message : i18n('newMessage')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}.bind(this)),
|
2015-09-14 03:25:04 +00:00
|
|
|
buttons : [{
|
|
|
|
title : 'Mark all as read',
|
2016-04-01 18:03:26 +00:00
|
|
|
iconUrl : 'images/check.svg'
|
2015-09-14 03:25:04 +00:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var m = this.at(0);
|
2015-09-14 20:56:02 +00:00
|
|
|
var type = 'basic';
|
2016-02-18 00:08:17 +00:00
|
|
|
var message = i18n('newMessage');
|
|
|
|
var imageUrl;
|
|
|
|
if (this.showMessage()) {
|
|
|
|
message = m.get('message');
|
|
|
|
if (m.get('imageUrl')) {
|
|
|
|
type = 'image';
|
|
|
|
imageUrl = m.get('imageUrl');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.showSender()) {
|
|
|
|
title = m.get('title');
|
|
|
|
iconUrl = m.get('iconUrl');
|
2015-09-14 20:56:02 +00:00
|
|
|
}
|
2015-11-25 23:11:01 +00:00
|
|
|
extension.notification.update({
|
2015-09-14 20:56:02 +00:00
|
|
|
type : type,
|
2016-02-18 00:08:17 +00:00
|
|
|
title : title,
|
|
|
|
message : message,
|
|
|
|
iconUrl : iconUrl,
|
|
|
|
imageUrl : imageUrl
|
2015-09-14 03:25:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2016-02-18 00:08:17 +00:00
|
|
|
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);
|
|
|
|
},
|
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
|
|
|
if (this.length === 0) {
|
|
|
|
extension.notification.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2015-09-14 03:25:04 +00:00
|
|
|
clear: function() {
|
|
|
|
this.reset([]);
|
|
|
|
}
|
|
|
|
}))();
|
2015-03-17 22:06:21 +00:00
|
|
|
})();
|