forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging.js
653 lines (589 loc) · 22.2 KB
/
messaging.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
'use strict';
var assert = require('assert');
var async = require('async');
var request = require('request');
var nconf = require('nconf');
var db = require('./mocks/databasemock');
var meta = require('../src/meta');
var User = require('../src/user');
var Groups = require('../src/groups');
var Messaging = require('../src/messaging');
var helpers = require('./helpers');
var socketModules = require('../src/socket.io/modules');
describe('Messaging Library', function () {
var fooUid;
var bazUid;
var herpUid;
var roomId;
before(function (done) {
Groups.resetCache();
// Create 3 users: 1 admin, 2 regular
async.series([
async.apply(User.create, { username: 'foo', password: 'barbar' }), // admin
async.apply(User.create, { username: 'baz', password: 'quuxquux' }), // restricted user
async.apply(User.create, { username: 'herp', password: 'derpderp' }), // regular user
], function (err, uids) {
if (err) {
return done(err);
}
fooUid = uids[0];
bazUid = uids[1];
herpUid = uids[2];
async.parallel([
async.apply(Groups.join, 'administrators', fooUid),
async.apply(User.setSetting, bazUid, 'restrictChat', '1'),
], done);
});
});
describe('.canMessage()', function () {
it('should allow messages to be sent to an unrestricted user', function (done) {
Messaging.canMessageUser(bazUid, herpUid, function (err) {
assert.ifError(err);
done();
});
});
it('should NOT allow messages to be sent to a restricted user', function (done) {
User.setSetting(bazUid, 'restrictChat', '1', function (err) {
assert.ifError(err);
Messaging.canMessageUser(herpUid, bazUid, function (err) {
assert.strictEqual(err.message, '[[error:chat-restricted]]');
socketModules.chats.addUserToRoom({ uid: herpUid }, { roomId: 1, username: 'baz' }, function (err) {
assert.equal(err.message, '[[error:chat-restricted]]');
done();
});
});
});
});
it('should always allow admins through', function (done) {
Messaging.canMessageUser(fooUid, bazUid, function (err) {
assert.ifError(err);
done();
});
});
it('should allow messages to be sent to a restricted user if restricted user follows sender', function (done) {
User.follow(bazUid, herpUid, function () {
Messaging.canMessageUser(herpUid, bazUid, function (err) {
assert.ifError(err);
done();
});
});
});
});
describe('rooms', function () {
it('should fail to create a new chat room with invalid data', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should return rate limit error on second try', function (done) {
var socketMock = { uid: fooUid };
var oldValue = meta.config.chatMessageDelay;
meta.config.chatMessageDelay = 1000;
socketModules.chats.newRoom(socketMock, { touid: bazUid }, function (err) {
assert.ifError(err);
socketModules.chats.newRoom(socketMock, { touid: bazUid }, function (err) {
assert.equal(err.message, '[[error:too-many-messages]]');
meta.configs.chatMessageDelay = oldValue;
done();
});
});
});
it('should create a new chat room', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, { touid: bazUid }, function (err, _roomId) {
roomId = _roomId;
assert.ifError(err);
assert(roomId);
socketModules.chats.canMessage({ uid: fooUid }, _roomId, function (err) {
assert.ifError(err);
done();
});
});
});
it('should fail to add user to room with invalid data', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should add a user to room', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(isInRoom);
done();
});
});
});
it('should fail to add users to room if max is reached', function (done) {
meta.config.maximumUsersInChatRoom = 2;
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'test' }, function (err) {
assert.equal(err.message, '[[error:cant-add-more-users-to-chat-room]]');
meta.config.maximumUsersInChatRoom = 0;
done();
});
});
it('should fail to add users to room if user does not exist', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'doesnotexist' }, function (err) {
assert.equal(err.message, '[[error:no-user]]');
done();
});
});
it('should fail to add self to room', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'foo' }, function (err) {
assert.equal(err.message, '[[error:cant-add-self-to-chat-room]]');
done();
});
});
it('should fail to leave room with invalid data', function (done) {
socketModules.chats.leave({ uid: null }, roomId, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.leave({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should leave the chat room', function (done) {
socketModules.chats.leave({ uid: bazUid }, roomId, function (err) {
assert.ifError(err);
Messaging.isUserInRoom(bazUid, roomId, function (err, isUserInRoom) {
assert.ifError(err);
assert.equal(isUserInRoom, false);
done();
});
});
});
it('should fail to remove user from room', function (done) {
socketModules.chats.removeUserFromRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.removeUserFromRoom({ uid: fooUid }, {}, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should fail to remove user from room if user does not exist', function (done) {
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'doesnotexist' }, function (err) {
assert.equal(err.message, '[[error:no-user]]');
done();
});
});
it('should remove user from room', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, { touid: herpUid }, function (err, roomId) {
assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(isInRoom);
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.equal(err.message, '[[error:cant-remove-last-user]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'baz' }, function (err) {
assert.ifError(err);
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(!isInRoom);
done();
});
});
});
});
});
});
});
it('should fail to send a message to room with invalid data', function (done) {
socketModules.chats.send({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.send({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.send({ uid: null }, { roomId: 1 }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should send a message to a room', function (done) {
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) {
assert.ifError(err);
assert(messageData);
assert.equal(messageData.content, 'first chat message');
assert(messageData.fromUser);
assert(messageData.roomId, roomId);
socketModules.chats.getRaw({ uid: fooUid }, { mid: messageData.mid }, function (err, raw) {
assert.ifError(err);
assert.equal(raw, 'first chat message');
setTimeout(done, 300);
});
});
});
it('should fail to send second message due to rate limit', function (done) {
var socketMock = { uid: fooUid };
var oldValue = meta.config.chatMessageDelay;
meta.config.chatMessageDelay = 1000;
socketModules.chats.send(socketMock, { roomId: roomId, message: 'first chat message' }, function (err) {
assert.ifError(err);
socketModules.chats.send(socketMock, { roomId: roomId, message: 'first chat message' }, function (err) {
assert.equal(err.message, '[[error:too-many-messages]]');
meta.config.chatMessageDelay = oldValue;
done();
});
});
});
it('should return invalid-data error', function (done) {
socketModules.chats.getRaw({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRaw({ uid: fooUid }, { }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should return not allowed error if mid is not in room', function (done) {
var myRoomId;
User.create({ username: 'dummy' }, function (err, uid) {
assert.ifError(err);
socketModules.chats.newRoom({ uid: bazUid }, { touid: uid }, function (err, _roomId) {
myRoomId = _roomId;
assert.ifError(err);
assert(myRoomId);
socketModules.chats.getRaw({ uid: bazUid }, { mid: 1 }, function (err) {
assert.equal(err.message, '[[error:not-allowed]]');
socketModules.chats.send({ uid: bazUid }, { roomId: myRoomId, message: 'admin will see this' }, function (err, message) {
assert.ifError(err);
socketModules.chats.getRaw({ uid: fooUid }, { mid: message.mid }, function (err, raw) {
assert.ifError(err);
assert.equal(raw, 'admin will see this');
done();
});
});
});
});
});
});
it('should notify offline users of message', function (done) {
Messaging.notificationSendDelay = 100;
db.sortedSetAdd('users:online', Date.now() - 350000, herpUid, function (err) {
assert.ifError(err);
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'second chat message' }, function (err) {
assert.ifError(err);
setTimeout(function () {
User.notifications.get(herpUid, function (err, data) {
assert.ifError(err);
assert(data.unread[0]);
var notification = data.unread[0];
assert.equal(notification.bodyShort, '[[notifications:new_message_from, foo]]');
assert.equal(notification.nid, 'chat_' + fooUid + '_' + roomId);
assert.equal(notification.path, '/chats/' + roomId);
done();
});
}, 1500);
});
});
});
it('should fail to get messages from room with invalid data', function (done) {
socketModules.chats.getMessages({ uid: null }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, { uid: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, { uid: 1, roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
});
it('should get messages from room', function (done) {
socketModules.chats.getMessages({ uid: fooUid }, {
uid: fooUid,
roomId: roomId,
start: 0,
}, function (err, messages) {
assert.ifError(err);
assert(Array.isArray(messages));
assert.equal(messages[0].roomId, roomId);
assert.equal(messages[0].fromuid, fooUid);
done();
});
});
it('should fail to mark read with invalid data', function (done) {
socketModules.chats.markRead({ uid: null }, roomId, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.markRead({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should not error if user is not in room', function (done) {
socketModules.chats.markRead({ uid: herpUid }, 10, function (err) {
assert.ifError(err);
done();
});
});
it('should mark room read', function (done) {
socketModules.chats.markRead({ uid: fooUid }, roomId, function (err) {
assert.ifError(err);
done();
});
});
it('should mark all rooms read', function (done) {
socketModules.chats.markAllRead({ uid: fooUid }, {}, function (err) {
assert.ifError(err);
done();
});
});
it('should fail to rename room with invalid data', function (done) {
socketModules.chats.renameRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: roomId, newName: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should rename room', function (done) {
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: roomId, newName: 'new room name' }, function (err) {
assert.ifError(err);
done();
});
});
it('should fail to load room with invalid-data', function (done) {
socketModules.chats.loadRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.loadRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should fail to load room if user is not in', function (done) {
socketModules.chats.loadRoom({ uid: 0 }, { roomId: roomId }, function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
done();
});
});
it('should load chat room', function (done) {
socketModules.chats.loadRoom({ uid: fooUid }, { roomId: roomId }, function (err, data) {
assert.ifError(err);
assert(data);
assert.equal(data.roomName, 'new room name');
done();
});
});
it('should return true if user is dnd', function (done) {
db.setObjectField('user:' + herpUid, 'status', 'dnd', function (err) {
assert.ifError(err);
socketModules.chats.isDnD({ uid: fooUid }, herpUid, function (err, isDnD) {
assert.ifError(err);
assert(isDnD);
done();
});
});
});
it('should fail to load recent chats with invalid data', function (done) {
socketModules.chats.getRecentChats({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRecentChats({ uid: fooUid }, { after: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRecentChats({ uid: fooUid }, { after: 0, uid: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should load recent chats of user', function (done) {
socketModules.chats.getRecentChats({ uid: fooUid }, { after: 0, uid: fooUid }, function (err, data) {
assert.ifError(err);
assert(Array.isArray(data.rooms));
done();
});
});
it('should escape teaser', function (done) {
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: '<svg/onload=alert(document.location);' }, function (err) {
assert.ifError(err);
socketModules.chats.getRecentChats({ uid: fooUid }, { after: 0, uid: fooUid }, function (err, data) {
assert.ifError(err);
assert.equal(data.rooms[0].teaser.content, '<svg/onload=alert(document.location);');
done();
});
});
});
it('should fail to check if user has private chat with invalid data', function (done) {
socketModules.chats.hasPrivateChat({ uid: null }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.hasPrivateChat({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should check if user has private chat with another uid', function (done) {
socketModules.chats.hasPrivateChat({ uid: fooUid }, herpUid, function (err, roomId) {
assert.ifError(err);
assert(roomId);
done();
});
});
});
describe('edit/delete', function () {
var socketModules = require('../src/socket.io/modules');
var mid;
before(function (done) {
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) {
assert.ifError(err);
mid = messageData.mid;
done();
});
});
it('should fail to edit message with invalid data', function (done) {
socketModules.chats.edit({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.edit({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.edit({ uid: fooUid }, { roomId: 1, message: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should fail to edit message if not own message', function (done) {
socketModules.chats.edit({ uid: herpUid }, { mid: 5, roomId: roomId, message: 'message edited' }, function (err) {
assert.equal(err.message, '[[error:cant-edit-chat-message]]');
done();
});
});
it('should edit message', function (done) {
socketModules.chats.edit({ uid: fooUid }, { mid: mid, roomId: roomId, message: 'message edited' }, function (err) {
assert.ifError(err);
socketModules.chats.getRaw({ uid: fooUid }, { mid: mid }, function (err, raw) {
assert.ifError(err);
assert.equal(raw, 'message edited');
done();
});
});
});
it('should fail to delete message with invalid data', function (done) {
socketModules.chats.delete({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.delete({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.delete({ uid: fooUid }, { roomId: 1, messageId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should fail to delete message if not owner', function (done) {
socketModules.chats.delete({ uid: herpUid }, { messageId: mid, roomId: roomId }, function (err) {
assert.equal(err.message, '[[error:cant-delete-chat-message]]');
done();
});
});
it('should delete message', function (done) {
socketModules.chats.delete({ uid: fooUid }, { messageId: mid, roomId: roomId }, function (err) {
assert.ifError(err);
db.exists('message:' + mid, function (err, exists) {
assert.ifError(err);
assert(!exists);
db.isSortedSetMember('uid:' + fooUid + ':chat:room:' + roomId + ':mids', mid, function (err, isMember) {
assert.ifError(err);
assert(!isMember);
done();
});
});
});
});
});
describe('controller', function () {
it('should 404 if chat is disabled', function (done) {
meta.config.disableChat = 1;
request(nconf.get('url') + '/user/baz/chats', function (err, response) {
assert.ifError(err);
assert.equal(response.statusCode, 404);
done();
});
});
it('should 500 for guest with no privilege error', function (done) {
meta.config.disableChat = 0;
request(nconf.get('url') + '/api/user/baz/chats', { json: true }, function (err, response, body) {
assert.ifError(err);
assert.equal(response.statusCode, 500);
assert.equal(body.error, '[[error:no-privileges]]');
done();
});
});
it('should 404 for non-existent user', function (done) {
request(nconf.get('url') + '/user/doesntexist/chats', function (err, response) {
assert.ifError(err);
assert.equal(response.statusCode, 404);
done();
});
});
});
describe('logged in chat controller', function () {
var jar;
before(function (done) {
helpers.loginUser('herp', 'derpderp', function (err, _jar) {
assert.ifError(err);
jar = _jar;
done();
});
});
it('should return chats page data', function (done) {
request(nconf.get('url') + '/api/user/herp/chats', { json: true, jar: jar }, function (err, response, body) {
assert.ifError(err);
assert.equal(response.statusCode, 200);
assert(Array.isArray(body.rooms));
assert.equal(body.rooms.length, 1);
assert.equal(body.title, '[[pages:chats]]');
done();
});
});
it('should return room data', function (done) {
request(nconf.get('url') + '/api/user/herp/chats/' + roomId, { json: true, jar: jar }, function (err, response, body) {
assert.ifError(err);
assert.equal(response.statusCode, 200);
assert.equal(body.roomId, roomId);
assert.equal(body.isOwner, false);
done();
});
});
it('should redirect to chats page', function (done) {
request(nconf.get('url') + '/api/chats', { jar: jar, json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(res.headers['x-redirect'], '/user/herp/chats');
assert.equal(body, '/user/herp/chats');
done();
});
});
it('should return 404 if user is not in room', function (done) {
helpers.loginUser('baz', 'quuxquux', function (err, jar) {
assert.ifError(err);
request(nconf.get('url') + '/api/user/baz/chats/' + roomId, { json: true, jar: jar }, function (err, response) {
assert.ifError(err);
assert.equal(response.statusCode, 404);
done();
});
});
});
});
});