2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-03-17 22:06:21 +00:00
|
|
|
*/
|
2017-11-22 21:50:52 +00:00
|
|
|
|
2015-03-17 22:06:21 +00:00
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2018-02-26 18:28:04 +00:00
|
|
|
const { Settings } = window.Signal.Types;
|
2015-03-17 22:06:21 +00:00
|
|
|
|
2016-02-18 00:08:17 +00:00
|
|
|
var SETTINGS = {
|
|
|
|
OFF : 'off',
|
|
|
|
COUNT : 'count',
|
|
|
|
NAME : 'name',
|
|
|
|
MESSAGE : 'message'
|
|
|
|
};
|
|
|
|
|
2015-09-14 03:25:04 +00:00
|
|
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
2018-03-02 23:18:20 +00:00
|
|
|
this.isEnabled = false;
|
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() {
|
2018-03-02 19:43:03 +00:00
|
|
|
const isFocused = window.isFocused();
|
2018-03-02 00:58:51 +00:00
|
|
|
const isAudioNotificationEnabled = storage.get('audio-notification') || false;
|
|
|
|
const isAudioNotificationSupported = Settings.isAudioNotificationSupported();
|
|
|
|
const shouldPlayNotificationSound = isAudioNotificationSupported &&
|
|
|
|
isAudioNotificationEnabled;
|
2018-02-23 22:18:06 +00:00
|
|
|
const numNotifications = this.length;
|
2017-09-29 16:15:28 +00:00
|
|
|
console.log(
|
2018-03-02 00:58:51 +00:00
|
|
|
'Update notifications:',
|
2018-02-23 22:18:06 +00:00
|
|
|
'isFocused:', isFocused,
|
2018-03-02 23:18:20 +00:00
|
|
|
'isEnabled:', this.isEnabled,
|
2018-03-02 00:58:51 +00:00
|
|
|
'numNotifications:', numNotifications,
|
2018-02-23 22:18:06 +00:00
|
|
|
'shouldPlayNotificationSound:', shouldPlayNotificationSound
|
2017-09-29 16:15:28 +00:00
|
|
|
);
|
2018-02-23 21:25:11 +00:00
|
|
|
|
2018-03-02 23:18:20 +00:00
|
|
|
if (!this.isEnabled) {
|
2018-02-23 21:25:11 +00:00
|
|
|
return;
|
2017-09-29 16:15:28 +00:00
|
|
|
}
|
2018-02-23 21:25:19 +00:00
|
|
|
|
2018-02-23 22:18:06 +00:00
|
|
|
const hasNotifications = numNotifications > 0;
|
2018-02-23 21:25:19 +00:00
|
|
|
if (!hasNotifications) {
|
2016-04-11 18:24:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-02 19:43:03 +00:00
|
|
|
|
|
|
|
const isNotificationOmitted = isFocused;
|
|
|
|
if (isNotificationOmitted) {
|
2017-09-29 16:15:28 +00:00
|
|
|
this.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:08:17 +00:00
|
|
|
var setting = storage.get('notification-setting') || 'message';
|
|
|
|
if (setting === SETTINGS.OFF) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:55:36 +00:00
|
|
|
window.drawAttention();
|
|
|
|
|
2017-04-28 01:31:35 +00:00
|
|
|
var title;
|
|
|
|
var message;
|
|
|
|
var iconUrl;
|
|
|
|
|
2018-02-23 21:29:09 +00:00
|
|
|
// NOTE: i18n has more complex rules for pluralization than just
|
|
|
|
// distinguishing between zero (0) and other (non-zero),
|
|
|
|
// e.g. Russian:
|
|
|
|
// http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
|
2017-04-28 01:31:35 +00:00
|
|
|
var newMessageCount = [
|
2018-02-23 22:18:06 +00:00
|
|
|
numNotifications,
|
|
|
|
numNotifications === 1 ? i18n('newMessage') : i18n('newMessages')
|
2016-02-18 00:08:17 +00:00
|
|
|
].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:
|
2018-02-23 22:18:06 +00:00
|
|
|
if (numNotifications === 1) {
|
2017-09-29 16:15:28 +00:00
|
|
|
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-09-29 16:15:28 +00:00
|
|
|
|
2017-11-22 22:32:39 +00:00
|
|
|
if (window.config.polyfillNotifications) {
|
2017-11-22 21:50:52 +00:00
|
|
|
window.nodeNotifier.notify({
|
|
|
|
title: title,
|
|
|
|
message: message,
|
2018-03-02 00:42:50 +00:00
|
|
|
sound: false,
|
2017-11-22 21:50:52 +00:00
|
|
|
});
|
|
|
|
window.nodeNotifier.on('click', function(notifierObject, options) {
|
|
|
|
last.get('conversationId');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var notification = new Notification(title, {
|
|
|
|
body : message,
|
|
|
|
icon : iconUrl,
|
|
|
|
tag : 'signal',
|
2018-02-23 21:53:24 +00:00
|
|
|
silent : !shouldPlayNotificationSound,
|
2017-11-22 21:50:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
notification.onclick = this.onClick.bind(this, last.get('conversationId'));
|
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
|
|
|
|
// 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() {
|
2018-03-02 23:18:20 +00:00
|
|
|
const needUpdate = !this.isEnabled;
|
|
|
|
this.isEnabled = true;
|
2018-03-02 23:09:21 +00:00
|
|
|
if (needUpdate) {
|
2017-09-29 16:15:28 +00:00
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disable: function() {
|
2018-03-02 23:18:20 +00:00
|
|
|
this.isEnabled = false;
|
2017-09-29 16:15:28 +00:00
|
|
|
},
|
2015-09-14 03:25:04 +00:00
|
|
|
}))();
|
2015-03-17 22:06:21 +00:00
|
|
|
})();
|