2018-05-02 20:43:32 +00:00
|
|
|
/* global Backbone: false */
|
|
|
|
/* global nodeNotifier: false */
|
|
|
|
|
|
|
|
/* global config: false */
|
|
|
|
/* global ConversationController: false */
|
2018-05-02 21:58:48 +00:00
|
|
|
/* global drawAttention: false */
|
2018-05-02 20:43:32 +00:00
|
|
|
/* global i18n: false */
|
2018-05-02 21:58:48 +00:00
|
|
|
/* global isFocused: false */
|
2018-05-02 20:43:32 +00:00
|
|
|
/* global Signal: false */
|
|
|
|
/* global storage: false */
|
|
|
|
/* global Whisper: false */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-05-02 20:43:32 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2018-05-02 20:43:32 +00:00
|
|
|
const { Settings } = Signal.Types;
|
2015-03-17 22:06:21 +00:00
|
|
|
|
2018-05-02 22:02:49 +00:00
|
|
|
const SettingNames = {
|
2018-04-27 21:25:04 +00:00
|
|
|
OFF: 'off',
|
|
|
|
COUNT: 'count',
|
|
|
|
NAME: 'name',
|
|
|
|
MESSAGE: 'message',
|
|
|
|
};
|
2016-02-18 00:08:17 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
2018-05-02 20:43:32 +00:00
|
|
|
initialize() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
this.on('add', this.update);
|
|
|
|
this.on('remove', this.onRemove);
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
onClick(conversationId) {
|
|
|
|
const conversation = ConversationController.get(conversationId);
|
2018-04-27 21:25:04 +00:00
|
|
|
this.trigger('click', conversation);
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
update() {
|
2018-04-27 21:25:04 +00:00
|
|
|
const { isEnabled } = this;
|
2018-05-02 21:58:48 +00:00
|
|
|
const isAppFocused = isFocused();
|
2018-04-27 21:25:04 +00:00
|
|
|
const isAudioNotificationEnabled =
|
|
|
|
storage.get('audio-notification') || false;
|
|
|
|
const isAudioNotificationSupported = Settings.isAudioNotificationSupported();
|
|
|
|
const shouldPlayNotificationSound =
|
|
|
|
isAudioNotificationSupported && isAudioNotificationEnabled;
|
|
|
|
const numNotifications = this.length;
|
|
|
|
console.log('Update notifications:', {
|
2018-05-02 21:58:48 +00:00
|
|
|
isAppFocused,
|
2018-04-27 21:25:04 +00:00
|
|
|
isEnabled,
|
|
|
|
numNotifications,
|
|
|
|
shouldPlayNotificationSound,
|
|
|
|
});
|
2018-02-23 21:25:11 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
if (!isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-23 21:25:19 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const hasNotifications = numNotifications > 0;
|
|
|
|
if (!hasNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-02 19:43:03 +00:00
|
|
|
|
2018-05-02 21:58:48 +00:00
|
|
|
const isNotificationOmitted = isAppFocused;
|
2018-04-27 21:25:04 +00:00
|
|
|
if (isNotificationOmitted) {
|
|
|
|
this.clear();
|
|
|
|
return;
|
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
|
2018-05-02 20:43:32 +00:00
|
|
|
const setting = this.getSetting();
|
2018-05-02 22:02:49 +00:00
|
|
|
if (setting === SettingNames.OFF) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-02-18 00:08:17 +00:00
|
|
|
|
2018-05-02 21:58:48 +00:00
|
|
|
drawAttention();
|
2017-10-30 20:55:36 +00:00
|
|
|
|
2018-05-02 20:43:32 +00:00
|
|
|
let title;
|
|
|
|
let message;
|
|
|
|
let iconUrl;
|
2017-04-28 01:31:35 +00:00
|
|
|
|
2018-04-27 21:25:04 +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
|
2018-05-02 20:43:32 +00:00
|
|
|
const newMessageCount = [
|
2018-04-27 21:25:04 +00:00
|
|
|
numNotifications,
|
|
|
|
numNotifications === 1 ? i18n('newMessage') : i18n('newMessages'),
|
|
|
|
].join(' ');
|
2016-02-18 00:08:17 +00:00
|
|
|
|
2018-05-02 20:43:32 +00:00
|
|
|
const last = this.last();
|
|
|
|
switch (setting) {
|
2018-05-02 22:02:49 +00:00
|
|
|
case SettingNames.COUNT:
|
2018-04-27 21:25:04 +00:00
|
|
|
title = 'Signal';
|
|
|
|
message = newMessageCount;
|
|
|
|
break;
|
2018-05-02 22:02:49 +00:00
|
|
|
case SettingNames.NAME:
|
2018-04-27 21:25:04 +00:00
|
|
|
title = newMessageCount;
|
2018-05-02 20:43:32 +00:00
|
|
|
message = `Most recent from ${last.get('title')}`;
|
2018-04-27 21:25:04 +00:00
|
|
|
iconUrl = last.get('iconUrl');
|
|
|
|
break;
|
2018-05-02 22:02:49 +00:00
|
|
|
case SettingNames.MESSAGE:
|
2018-04-27 21:25:04 +00:00
|
|
|
if (numNotifications === 1) {
|
|
|
|
title = last.get('title');
|
|
|
|
} else {
|
|
|
|
title = newMessageCount;
|
|
|
|
}
|
|
|
|
message = last.get('message');
|
|
|
|
iconUrl = last.get('iconUrl');
|
|
|
|
break;
|
2018-05-02 20:43:32 +00:00
|
|
|
default:
|
|
|
|
console.log(`Error: Unknown setting: '${setting}'`);
|
|
|
|
break;
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
|
2018-05-02 20:43:32 +00:00
|
|
|
if (config.polyfillNotifications) {
|
|
|
|
nodeNotifier.notify({
|
|
|
|
title,
|
|
|
|
message,
|
2018-04-27 21:25:04 +00:00
|
|
|
sound: false,
|
|
|
|
});
|
2018-05-02 20:43:32 +00:00
|
|
|
nodeNotifier.on('click', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
last.get('conversationId');
|
|
|
|
});
|
|
|
|
} else {
|
2018-05-02 20:43:32 +00:00
|
|
|
const notification = new Notification(title, {
|
2018-04-27 21:25:04 +00:00
|
|
|
body: message,
|
|
|
|
icon: iconUrl,
|
|
|
|
tag: 'signal',
|
|
|
|
silent: !shouldPlayNotificationSound,
|
|
|
|
});
|
2017-11-22 21:50:52 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
notification.onclick = this.onClick.bind(
|
|
|
|
this,
|
|
|
|
last.get('conversationId')
|
|
|
|
);
|
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
// We don't want to notify the user about these same messages again
|
|
|
|
this.clear();
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
getSetting() {
|
2018-05-02 22:02:49 +00:00
|
|
|
return storage.get('notification-setting') || SettingNames.MESSAGE;
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
onRemove() {
|
2018-04-27 21:25:04 +00:00
|
|
|
console.log('remove notification');
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
clear() {
|
2018-04-27 21:25:04 +00:00
|
|
|
console.log('remove all notifications');
|
|
|
|
this.reset([]);
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
enable() {
|
2018-04-27 21:25:04 +00:00
|
|
|
const needUpdate = !this.isEnabled;
|
|
|
|
this.isEnabled = true;
|
|
|
|
if (needUpdate) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
disable() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
},
|
|
|
|
}))();
|
2015-03-17 22:06:21 +00:00
|
|
|
})();
|