-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
notification.js
331 lines (274 loc) · 16.4 KB
/
notification.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*global mock, converse */
const { Strophe } = converse.env;
const $msg = converse.env.$msg;
const u = converse.env.utils;
describe("Notifications", function () {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
describe("When show_desktop_notifications is set to true", function () {
describe("And the desktop is not focused", function () {
describe("an HTML5 Notification", function () {
it("is shown when a new private message is received",
mock.initConverse([], {}, async (_converse) => {
await mock.waitForRoster(_converse, 'current');
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
const message = 'This message will show a desktop notification';
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit',
msg = $msg({
from: sender_jid,
to: _converse.api.connection.get().jid,
type: 'chat',
id: u.getUniqueId()
}).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
await _converse.handleMessageStanza(msg); // This will emit 'message'
await u.waitUntil(() => _converse.chatboxviews.get(sender_jid));
expect(window.Notification).toHaveBeenCalled();
}));
it("is shown when you are mentioned in a groupchat",
mock.initConverse([], {}, async (_converse) => {
await mock.waitForRoster(_converse, 'current');
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
const view = _converse.chatboxviews.get('lounge@montague.lit');
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
// Test mention with setting false
const nick = mock.chatroom_names[0];
const makeMsg = text => $msg({
from: 'lounge@montague.lit/'+nick,
id: u.getUniqueId(),
to: 'romeo@montague.lit',
type: 'groupchat'
}).c('body').t(text).tree();
_converse.api.connection.get()._dataRecv(mock.createRequest(makeMsg('romeo: this will NOT show a notification')));
await new Promise(resolve => view.model.messages.once('rendered', resolve));
expect(window.Notification).not.toHaveBeenCalled();
// Test reference
const message_with_ref = $msg({
from: 'lounge@montague.lit/'+nick,
id: u.getUniqueId(),
to: 'romeo@montague.lit',
type: 'groupchat'
}).c('body').t('romeo: this will show a notification').up()
.c('reference', {'xmlns':'urn:xmpp:reference:0', 'begin':'0', 'end':'5', 'type':'mention', 'uri':'xmpp:romeo@montague.lit'}).tree();
_converse.api.connection.get()._dataRecv(mock.createRequest(message_with_ref));
await new Promise(resolve => view.model.messages.once('rendered', resolve));
expect(window.Notification.calls.count()).toBe(1);
// Test mention with setting true
_converse.api.settings.set('notify_all_room_messages', true);
_converse.api.connection.get()._dataRecv(mock.createRequest(makeMsg('romeo: this will show a notification')));
await new Promise(resolve => view.model.messages.once('rendered', resolve));
expect(window.Notification.calls.count()).toBe(2);
}));
it("is shown for headline messages", mock.initConverse([], {}, async (_converse) => {
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
await mock.waitForRoster(_converse, 'current', 0);
const stanza = $msg({
'type': 'headline',
'from': 'notify.example.com',
'to': 'romeo@montague.lit',
'xml:lang': 'en'
})
.c('subject').t('SIEVE').up()
.c('body').t('<juliet@example.com> You got mail.').up()
.c('x', {'xmlns': 'jabber:x:oob'})
.c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
await u.waitUntil(() => _converse.chatboxviews.keys().length === 2);
expect(_converse.chatboxviews.keys().includes('notify.example.com')).toBeTruthy();
expect(window.Notification).toHaveBeenCalled();
}));
it("is not shown for full JID headline messages if allow_non_roster_messaging is false",
mock.initConverse([], {'allow_non_roster_messaging': false}, (_converse) => {
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
const stanza = $msg({
'type': 'headline',
'from': 'someone@notify.example.com',
'to': 'romeo@montague.lit',
'xml:lang': 'en'
})
.c('subject').t('SIEVE').up()
.c('body').t('<juliet@example.com> You got mail.').up()
.c('x', {'xmlns': 'jabber:x:oob'})
.c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
expect(_converse.chatboxviews.keys().includes('someone@notify.example.com')).toBeFalsy();
expect(window.Notification).not.toHaveBeenCalled();
}));
it("is shown when a user changes their chat state (if show_chat_state_notifications is true)",
mock.initConverse([], {show_chat_state_notifications: true},
async (_converse) => {
await mock.waitForRoster(_converse, 'current', 3);
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
const jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
_converse.roster.get(jid).presence.set('show', 'dnd');
expect(window.Notification).toHaveBeenCalled();
}));
});
});
describe("When a new contact request is received", function () {
it("an HTML5 Notification is received", mock.initConverse((_converse) => {
const stub = jasmine.createSpyObj('MyNotification', ['onclick', 'close']);
spyOn(window, 'Notification').and.returnValue(stub);
_converse.api.trigger('contactRequest', {'getDisplayName': () => 'Peter Parker'});
expect(window.Notification).toHaveBeenCalled();
}));
});
});
describe("When play_sounds is set to true", function () {
describe("A notification sound", function () {
it("is played when the current user is mentioned in a groupchat", mock.initConverse([], {}, async (_converse) => {
await mock.waitForRoster(_converse, 'current');
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
const { api } = _converse;
api.settings.set('play_sounds', true);
const stub = jasmine.createSpyObj('MyAudio', ['play', 'canPlayType']);
spyOn(window, 'Audio').and.returnValue(stub);
const view = _converse.chatboxviews.get('lounge@montague.lit');
if (!view.querySelectorAll('.chat-area').length) {
view.renderChatArea();
}
let text = 'This message will play a sound because it mentions romeo';
let message = $msg({
from: 'lounge@montague.lit/otheruser',
id: '1',
to: 'romeo@montague.lit',
type: 'groupchat'
}).c('body').t(text);
_converse.api.settings.set('notify_all_room_messages', true);
await view.model.handleMessageStanza(message.nodeTree);
await u.waitUntil(() => window.Audio.calls.count());
expect(window.Audio).toHaveBeenCalled();
text = "This message won't play a sound";
message = $msg({
from: 'lounge@montague.lit/otheruser',
id: '2',
to: 'romeo@montague.lit',
type: 'groupchat'
}).c('body').t(text);
await view.model.handleMessageStanza(message.nodeTree);
expect(window.Audio, 1);
api.settings.set('play_sounds', false);
text = "This message won't play a sound because it is sent by romeo";
message = $msg({
from: 'lounge@montague.lit/romeo',
id: '3',
to: 'romeo@montague.lit',
type: 'groupchat'
}).c('body').t(text);
await view.model.handleMessageStanza(message.nodeTree);
expect(window.Audio, 1);
}));
});
});
describe("A Favicon Message Counter", function () {
it("is incremented when the message is received and the window is not focused",
mock.initConverse([], {'show_tab_notifications': false}, async function (_converse) {
await mock.waitForRoster(_converse, 'current');
await mock.openControlBox(_converse);
const favico = jasmine.createSpyObj('favico', ['badge']);
spyOn(converse.env, 'Favico').and.returnValue(favico);
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const view = await mock.openChatBoxFor(_converse, sender_jid)
spyOn(view.model, 'isHidden').and.returnValue(true);
const msg = $msg({
from: sender_jid,
to: _converse.api.connection.get().jid,
type: 'chat',
id: u.getUniqueId()
}).c('body').t('This message will increment the message counter').up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
spyOn(_converse.api, "trigger").and.callThrough();
await _converse.handleMessageStanza(msg);
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
expect(favico.badge.calls.count()).toBe(0);
_converse.api.settings.set('show_tab_notifications', true);
const msg2 = $msg({
from: sender_jid,
to: _converse.api.connection.get().jid,
type: 'chat',
id: u.getUniqueId()
}).c('body').t('This message increment the message counter AND update the page title').up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
await _converse.handleMessageStanza(msg2);
await u.waitUntil(() => favico.badge.calls.count() === 1);
expect(favico.badge.calls.mostRecent().args.pop()).toBe(2);
expect(view.model.get('num_unread')).toBe(2);
// Check that it's cleared when the window is focused
view.model.isHidden.and.returnValue(false);
document.dispatchEvent(new Event('visibilitychange'));
await u.waitUntil(() => favico.badge.calls.count() === 2);
expect(favico.badge.calls.mostRecent().args.pop()).toBe(0);
expect(view.model.get('num_unread')).toBe(0);
}));
it("is not incremented when the message is received and the window is focused",
mock.initConverse([], {}, async function (_converse) {
await mock.waitForRoster(_converse, 'current');
await mock.openControlBox(_converse);
const favico = jasmine.createSpyObj('favico', ['badge']);
spyOn(converse.env, 'Favico').and.returnValue(favico);
document.dispatchEvent(new Event('visibilitychange'));
const message = 'This message will not increment the message counter';
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit',
msg = $msg({
from: sender_jid,
to: _converse.api.connection.get().jid,
type: 'chat',
id: u.getUniqueId()
}).c('body').t(message).up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
await _converse.handleMessageStanza(msg);
const promise = u.getOpenPromise();
setTimeout(() => {
const view = _converse.chatboxviews.get(sender_jid);
expect(view.model.get('num_unread')).toBe(0);
expect(favico.badge.calls.count()).toBe(0);
promise.resolve();
}, 500);
return promise;
}));
it("is incremented from zero when chatbox was closed after viewing previously received messages and the window is not focused now",
mock.initConverse([], {}, async function (_converse) {
await mock.waitForRoster(_converse, 'current');
const favico = jasmine.createSpyObj('favico', ['badge']);
spyOn(converse.env, 'Favico').and.returnValue(favico);
const message = 'This message will always increment the message counter from zero';
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const msgFactory = () => $msg({
from: sender_jid,
to: _converse.api.connection.get().jid,
type: 'chat',
id: u.getUniqueId()
})
.c('body').t(message).up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES})
.tree();
// leave converse-chat page
spyOn(_converse.exports.ChatBox.prototype, 'isHidden').and.returnValue(true);
await _converse.handleMessageStanza(msgFactory());
let view = _converse.chatboxviews.get(sender_jid);
await u.waitUntil(() => favico.badge.calls.count() === 1, 1000);
expect(favico.badge.calls.mostRecent().args.pop()).toBe(1);
expect(view.model.get('num_unread')).toBe(1);
view.model.isHidden.and.returnValue(false);
// come back to converse-chat page
document.dispatchEvent(new Event('visibilitychange'));
await u.waitUntil(() => u.isVisible(view));
expect(view.model.get('num_unread')).toBe(0);
await u.waitUntil(() => favico.badge.calls.count() === 2);
expect(favico.badge.calls.mostRecent().args.pop()).toBe(0);
// close chatbox and leave converse-chat page again
view.close();
view.model.isHidden.and.returnValue(true);
// check that msg_counter is incremented from zero again
await _converse.handleMessageStanza(msgFactory());
view = _converse.chatboxviews.get(sender_jid);
await u.waitUntil(() => u.isVisible(view));
await u.waitUntil(() => favico.badge.calls.count() === 3);
expect(favico.badge.calls.mostRecent().args.pop()).toBe(1);
}));
});
});