-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
notifications.js
136 lines (120 loc) · 3.63 KB
/
notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* global Backbone: false */
/* global ConversationController: false */
/* global drawAttention: false */
/* global i18n: false */
/* global isFocused: false */
/* global Signal: false */
/* global storage: false */
/* global Whisper: false */
// eslint-disable-next-line func-names
(function() {
'use strict';
window.Whisper = window.Whisper || {};
const { Settings } = Signal.Types;
const SettingNames = {
OFF: 'off',
COUNT: 'count',
NAME: 'name',
MESSAGE: 'message',
};
Whisper.Notifications = new (Backbone.Collection.extend({
initialize() {
this.isEnabled = false;
this.on('add', this.update);
this.on('remove', this.onRemove);
},
onClick(conversationId) {
const conversation = ConversationController.get(conversationId);
this.trigger('click', conversation);
},
update() {
const { isEnabled } = this;
const isAppFocused = isFocused();
const isAudioNotificationEnabled =
storage.get('audio-notification') || false;
const isAudioNotificationSupported = Settings.isAudioNotificationSupported();
const numNotifications = this.length;
const userSetting = this.getUserSetting();
const status = Signal.Notifications.getStatus({
isAppFocused,
isAudioNotificationEnabled,
isAudioNotificationSupported,
isEnabled,
numNotifications,
userSetting,
});
console.log('Update notifications:', status);
if (status.type !== 'ok') {
if (status.shouldClearNotifications) {
this.clear();
}
return;
}
let title;
let message;
let iconUrl;
// 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
const newMessageCount = [
numNotifications,
numNotifications === 1 ? i18n('newMessage') : i18n('newMessages'),
].join(' ');
const last = this.last();
switch (userSetting) {
case SettingNames.COUNT:
title = 'Signal';
message = newMessageCount;
break;
case SettingNames.NAME:
title = newMessageCount;
message = `Most recent from ${last.get('title')}`;
iconUrl = last.get('iconUrl');
break;
case SettingNames.MESSAGE:
if (numNotifications === 1) {
title = last.get('title');
} else {
title = newMessageCount;
}
message = last.get('message');
iconUrl = last.get('iconUrl');
break;
default:
console.log(`Error: Unknown user setting: '${userSetting}'`);
break;
}
drawAttention();
const notification = new Notification(title, {
body: message,
icon: iconUrl,
tag: 'signal',
silent: !status.shouldPlayNotificationSound,
});
notification.onclick = () => this.onClick(last.get('conversationId'));
// We don't want to notify the user about these same messages again
this.clear();
},
getUserSetting() {
return storage.get('notification-setting') || SettingNames.MESSAGE;
},
onRemove() {
console.log('Remove notification');
},
clear() {
console.log('Remove all notifications');
this.reset([]);
},
enable() {
const needUpdate = !this.isEnabled;
this.isEnabled = true;
if (needUpdate) {
this.update();
}
},
disable() {
this.isEnabled = false;
},
}))();
})();