podchat 12.5.2 → 12.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/test.js DELETED
@@ -1,2410 +0,0 @@
1
- var assert = require('assert'),
2
- faker = require('faker'),
3
- Chat = require('../src/chat.js'),
4
- fs = require('fs'),
5
- path = require('path');
6
-
7
- var TOKENS = {
8
- TOKEN_1: '573f0bc94cc14e258f835d40c8b40137', // Masoud
9
- TOKEN_2: 'dd84f092ba4c4ee3942439a8d2304a26' // PodDraw
10
- },
11
- P2P_THREAD = 4441, //6848,
12
- GROUP_THREAD = 7064, //6868,
13
- timingLog = true,
14
- params1 = {
15
- /**
16
- * Main Server
17
- */
18
- // socketAddress: 'wss://msg.pod.ir/ws', // {**REQUIRED**} Socket Address
19
- // ssoHost: 'https://accounts.pod.ir', // {**REQUIRED**} Socket Address
20
- // platformHost: 'https://api.pod.ir/srv/core', // {**REQUIRED**} Platform Core Address
21
- // fileServer: 'https://core.pod.ir', // {**REQUIRED**} File Server Address
22
- // podSpaceFileServer: 'https://podspace.pod.ir', // {**REQUIRED**} File Server Address
23
- // serverName: 'chat-server', // {**REQUIRED**} Server to to register on
24
-
25
- /**
26
- * Sand Box
27
- */
28
- socketAddress: "wss://chat-sandbox.pod.ir/ws", // {**REQUIRED**} Socket Address
29
- ssoHost: "https://accounts.pod.ir", // {**REQUIRED**} Socket Address
30
- platformHost: "https://sandbox.pod.ir:8043/srv/basic-platform", // {**REQUIRED**} Platform Core Address
31
- fileServer: 'https://core.pod.ir', // {**REQUIRED**} File Server Address
32
- podSpaceFileServer: 'http://172.16.110.61:8780/podspace', // {**REQUIRED**} File Server Address
33
- serverName: "chat-server", // {**REQUIRED**} Server to to register on
34
-
35
- /**
36
- * Integration
37
- */
38
- // socketAddress: "ws://172.16.110.235:8003/ws",
39
- // ssoHost: "http://172.16.110.76",
40
- // platformHost: "http://172.16.110.235:8003/srv/bptest-core",
41
- // fileServer: 'https://core.pod.ir',
42
- // podSpaceFileServer: 'http://172.16.110.61:8780/podspace', // {**REQUIRED**} File Server Address
43
- // serverName: "chatlocal",
44
-
45
- enableCache: false,
46
- token: TOKENS.TOKEN_1,
47
- asyncLogging: {
48
- // onFunction: true,
49
- // onMessageReceive: true,
50
- // onMessageSend: true,
51
- actualTiming: timingLog
52
- }
53
- },
54
- params2 = Object.assign({}, params1);
55
-
56
- params2.token = TOKENS.TOKEN_2;
57
-
58
- /**
59
- * CONNECTING AND GETTING READY
60
- */
61
- describe('Connecting and getting ready', function(done) {
62
- this.timeout(20000);
63
- var chatAgent;
64
-
65
- beforeEach(() => {
66
- chatAgent = new Chat(params1);
67
- });
68
-
69
- afterEach(() => {
70
- chatAgent.logout();
71
- });
72
-
73
- it('Should connect to server and get ready', function(done) {
74
- chatAgent.on('chatReady', function() {
75
- done();
76
- });
77
- });
78
- });
79
-
80
- /**
81
- * GETTING CURRENT USER'S INFO
82
- */
83
- describe('Working with Users', function(done) {
84
- this.timeout(20000);
85
- var chatAgent;
86
-
87
- beforeEach(() => {
88
- chatAgent = new Chat(params1);
89
- });
90
-
91
- afterEach(() => {
92
- chatAgent.logout();
93
- });
94
-
95
- it('Should get User Info', function(done) {
96
- chatAgent.on('chatReady', function() {
97
- var currentUser = chatAgent.getCurrentUser();
98
- if (currentUser && typeof currentUser.id === 'number') {
99
- done();
100
- } else {
101
- done('Can not get user info!');
102
- }
103
- });
104
- });
105
- });
106
-
107
- /**
108
- * CONTACTS FUNCTIONALITY
109
- */
110
- describe('Working with contacts', function(done) {
111
- this.timeout(20000);
112
-
113
- var chatAgent,
114
- newContactId,
115
- blockedContactId;
116
-
117
- beforeEach(() => {
118
- chatAgent = new Chat(params1);
119
- });
120
-
121
- afterEach(() => {
122
- chatAgent.logout();
123
- });
124
-
125
- it('Should GET contacts list', function(done) {
126
- chatAgent.on('chatReady', function() {
127
- var time = new Date().getTime();
128
- chatAgent.getContacts({
129
- count: 50,
130
- offset: 0
131
- }, function(contactsResult) {
132
- if (!contactsResult.hasError) {
133
- if (timingLog) {
134
- console.log('\x1b[33m ★ Get Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
135
- }
136
- done();
137
- console.log('\n');
138
- } else {
139
- done(JSON.stringify(contactsResult));
140
- }
141
- });
142
- });
143
- });
144
-
145
- it('Should ADD a new contact', function(done) {
146
- chatAgent.on('chatReady', function() {
147
- var time = new Date().getTime();
148
- chatAgent.addContacts({
149
- firstName: faker.name.firstName(),
150
- lastName: faker.name.lastName(),
151
- cellphoneNumber: '09' + Math.floor((Math.random() * 100000000) + 1),
152
- email: faker.internet.email()
153
- }, function(result) {
154
- if (!result.hasError) {
155
- newContactId = result.result.contacts[0].id;
156
- if (timingLog) {
157
- console.log('\x1b[33m ★ Add New Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
158
- }
159
- done();
160
- console.log('\n');
161
- } else {
162
- done(new Error(JSON.stringify(result)));
163
- }
164
- });
165
- });
166
- });
167
-
168
- it('Should UPDATE an existing contact', function(done) {
169
- chatAgent.on('chatReady', function() {
170
- var time = new Date().getTime();
171
- chatAgent.updateContacts({
172
- id: newContactId,
173
- firstName: faker.name.firstName(),
174
- lastName: faker.name.lastName(),
175
- cellphoneNumber: '09' + Math.floor((Math.random() * 100000000) + 1),
176
- email: faker.internet.email()
177
- }, function(result) {
178
- if (!result.hasError) {
179
- if (timingLog) {
180
- console.log('\x1b[33m ★ Update a Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
181
- }
182
- done();
183
- console.log('\n');
184
- } else {
185
- done(JSON.stringify(result));
186
- }
187
- });
188
- });
189
- });
190
-
191
- it('Should REMOVE an existing contact', function(done) {
192
- chatAgent.on('chatReady', function() {
193
- var time = new Date().getTime();
194
- chatAgent.removeContacts({
195
- id: newContactId
196
- }, function(result) {
197
- if (!result.hasError && result.result) {
198
- if (timingLog) {
199
- console.log('\x1b[33m ★ Remove a Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
200
- }
201
- done();
202
- console.log('\n');
203
- } else {
204
- done(JSON.stringify(result));
205
- }
206
- });
207
- });
208
- });
209
-
210
- it('Should Block a contact', function(done) {
211
- chatAgent.on('chatReady', function() {
212
- var time1 = new Date().getTime();
213
- chatAgent.getContacts({
214
- count: 50,
215
- offset: 0
216
- }, function(contactsResult) {
217
- if (!contactsResult.hasError) {
218
- if (timingLog) {
219
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
220
- }
221
- var time2 = new Date().getTime();
222
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
223
- if (contactsResult.result.contacts[i].hasUser) {
224
- var blockContactId = contactsResult.result.contacts[i].id;
225
-
226
- if (timingLog) {
227
- console.log('\x1b[90m ☰ Select a Valid Contact \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
228
- }
229
- var time3 = new Date().getTime();
230
-
231
- chatAgent.block({
232
- contactId: blockContactId
233
- }, function(result) {
234
- if (!result.hasError) {
235
- blockedContactId = result.result.blockId;
236
- if (timingLog) {
237
- console.log('\x1b[33m ★ Block Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3);
238
- }
239
- done();
240
- console.log('\n');
241
- } else {
242
- done(JSON.stringify(result));
243
- }
244
- });
245
-
246
- break;
247
- }
248
- }
249
- }
250
- });
251
- });
252
- });
253
-
254
- it('Should unBlock an already blocked contact', function(done) {
255
- chatAgent.on('chatReady', function() {
256
- var time1 = new Date().getTime();
257
-
258
- chatAgent.unblock({
259
- blockId: blockedContactId
260
- }, function(result) {
261
- if (!result.hasError) {
262
- if (timingLog) {
263
- console.log('\x1b[33m ★ UnBlock Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
264
- }
265
- done();
266
- console.log('\n');
267
- } else {
268
- done(JSON.stringify(result));
269
- }
270
- });
271
- });
272
- });
273
-
274
- it('Should Get Blocked contacts list', function(done) {
275
- chatAgent.on('chatReady', function() {
276
- var time1 = new Date().getTime();
277
- chatAgent.getBlockedList({
278
- count: 50,
279
- offset: 0
280
- }, function(contactsResult) {
281
- if (!contactsResult.hasError) {
282
- if (timingLog) {
283
- console.log('\x1b[33m ★ Get Blocked Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
284
- }
285
- done();
286
- console.log('\n');
287
- } else {
288
- done(JSON.stringify(contactsresult));
289
- }
290
- });
291
- });
292
- });
293
-
294
- it('Should Search in contacts list', function(done) {
295
- chatAgent.on('chatReady', function() {
296
- var time1 = new Date().getTime();
297
- chatAgent.searchContacts({
298
- cellphoneNumber: 09
299
- }, function(contactsResult) {
300
- if (!contactsResult.hasError) {
301
- if (timingLog) {
302
- console.log('\x1b[33m ★ Search in Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
303
- }
304
- done();
305
- console.log('\n');
306
- } else {
307
- done(JSON.stringify(contactsResult));
308
- }
309
- });
310
- });
311
- });
312
- });
313
-
314
- /**
315
- * THREADS FUNCTIONALITY
316
- */
317
- describe('Working with threads', function(done) {
318
- this.timeout(20000);
319
-
320
- var chatAgent,
321
- p2pThreadId,
322
- groupThreadId,
323
- muteThreadId;
324
-
325
- beforeEach(() => {
326
- chatAgent = new Chat(params1);
327
- });
328
-
329
- afterEach(() => {
330
- chatAgent.logout();
331
- });
332
-
333
- it('Should GET Threads list', function(done) {
334
- chatAgent.on('chatReady', function() {
335
- var time = new Date().getTime();
336
- chatAgent.getThreads({
337
- count: 50,
338
- offset: 0
339
- }, function(threadsResult) {
340
- if (!threadsResult.hasError) {
341
- if (timingLog) {
342
- console.log('\x1b[33m ★ Get Threads List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
343
- }
344
- done();
345
- console.log('\n');
346
- } else {
347
- done(JSON.stringify(threadsResult));
348
- }
349
- });
350
- });
351
- });
352
-
353
- it('Should GET a single thread', function(done) {
354
- chatAgent.on('chatReady', function() {
355
- var time = new Date().getTime();
356
- chatAgent.getThreads({
357
- threadIds: [P2P_THREAD]
358
- }, function(threadsResult) {
359
- if (!threadsResult.hasError) {
360
- if (timingLog) {
361
- console.log('\x1b[33m ★ Get Single Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
362
- }
363
- done();
364
- console.log('\n');
365
- } else {
366
- done(JSON.stringify(threadsResult));
367
- }
368
- });
369
- });
370
- });
371
-
372
- it('Should SEARCH in Thread names and return result', function(done) {
373
- chatAgent.on('chatReady', function() {
374
- var time = new Date().getTime();
375
- chatAgent.getThreads({
376
- count: 50,
377
- name: 'thread'
378
- }, function(threadsResult) {
379
- if (!threadsResult.hasError) {
380
- if (timingLog) {
381
- console.log('\x1b[33m ★ Search in Threads \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
382
- }
383
- done();
384
- console.log('\n');
385
- } else {
386
- done(JSON.stringify(threadsResult));
387
- }
388
- });
389
- });
390
- });
391
-
392
- it('Should CREATE a P2P thread with a contact (TYPE = NORMAL)', function(done) {
393
- chatAgent.on('chatReady', function() {
394
- var time1 = new Date().getTime();
395
- chatAgent.getContacts({
396
- count: 50,
397
- offset: 0
398
- }, function(contactsResult) {
399
- if (!contactsResult.hasError) {
400
- if (timingLog) {
401
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
402
- }
403
- var time2 = new Date().getTime();
404
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
405
- if (contactsResult.result.contacts[i].hasUser) {
406
- var p2pContactId = contactsResult.result.contacts[i].id;
407
-
408
- if (timingLog) {
409
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
410
- }
411
- var time3 = new Date().getTime();
412
-
413
- chatAgent.createThread({
414
- type: 'NORMAL',
415
- invitees: [
416
- {
417
- id: p2pContactId,
418
- idType: 'TO_BE_USER_CONTACT_ID'
419
- }]
420
- }, function(createThreadResult) {
421
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
422
- p2pThreadId = createThreadResult.result.thread.id;
423
- if (timingLog) {
424
- console.log('\x1b[33m ★ Create P2P Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3);
425
- }
426
- done();
427
- console.log('\n');
428
- } else {
429
- done(JSON.stringify(createThreadResult));
430
- }
431
- });
432
- break;
433
- }
434
- }
435
- }
436
- });
437
- });
438
- });
439
-
440
- it('Should CREATE a Group thread with a contact (TYPE = NORMAL)', function(done) {
441
- chatAgent.on('chatReady', function() {
442
- var time1 = new Date().getTime();
443
- chatAgent.getContacts({
444
- count: 50,
445
- offset: 0
446
- }, function(contactsResult) {
447
- if (!contactsResult.hasError) {
448
- if (timingLog) {
449
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
450
- }
451
- var time2 = new Date().getTime();
452
- var groupInvitees = [];
453
-
454
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
455
- if (contactsResult.result.contacts[i].hasUser) {
456
- groupInvitees.push({
457
- id: contactsResult.result.contacts[i].id,
458
- idType: 'TO_BE_USER_CONTACT_ID'
459
- });
460
-
461
- if (groupInvitees.length > 2) {
462
- break;
463
- }
464
- }
465
- }
466
-
467
- if (timingLog) {
468
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
469
- }
470
- var time3 = new Date().getTime();
471
-
472
- chatAgent.createThread({
473
- title: faker.lorem.word(),
474
- type: 'NORMAL',
475
- invitees: groupInvitees
476
- }, function(createThreadResult) {
477
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
478
- groupThreadId = createThreadResult.result.thread.id;
479
- if (timingLog) {
480
- console.log('\x1b[33m ★ Create Group Thread (type = NORMAL) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3);
481
- }
482
- done();
483
- console.log('\n');
484
- } else {
485
- done(JSON.stringify(createThreadResult));
486
- }
487
- });
488
- }
489
- });
490
- });
491
- });
492
-
493
- it('Should CREATE a Group thread with a contact (TYPE = OWNER_GROUP)', function(done) {
494
- chatAgent.on('chatReady', function() {
495
- var time1 = new Date().getTime();
496
- chatAgent.getContacts({
497
- count: 50,
498
- offset: 0
499
- }, function(contactsResult) {
500
- if (!contactsResult.hasError) {
501
- if (timingLog) {
502
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
503
- }
504
- var time2 = new Date().getTime();
505
- var groupInvitees = [];
506
-
507
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
508
- if (contactsResult.result.contacts[i].hasUser) {
509
- groupInvitees.push({
510
- id: contactsResult.result.contacts[i].id,
511
- idType: 'TO_BE_USER_CONTACT_ID'
512
- });
513
-
514
- if (groupInvitees.length > 2) {
515
- break;
516
- }
517
- }
518
- }
519
-
520
- if (timingLog) {
521
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
522
- }
523
- var time3 = new Date().getTime();
524
-
525
- chatAgent.createThread({
526
- title: faker.lorem.word(),
527
- type: 'OWNER_GROUP',
528
- invitees: groupInvitees
529
- }, function(createThreadResult) {
530
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
531
- groupThreadId = createThreadResult.result.thread.id;
532
- if (timingLog) {
533
- console.log('\x1b[33m ★ Create Group Thread (type = OWNER_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
534
- time3);
535
- }
536
- done();
537
- console.log('\n');
538
- } else {
539
- done(JSON.stringify(createThreadResult));
540
- }
541
- });
542
- }
543
- });
544
- });
545
- });
546
-
547
- it('Should CREATE a Group thread with a contact (TYPE = PUBLIC_GROUP)', function(done) {
548
- chatAgent.on('chatReady', function() {
549
- var time1 = new Date().getTime();
550
- chatAgent.getContacts({
551
- count: 50,
552
- offset: 0
553
- }, function(contactsResult) {
554
- if (!contactsResult.hasError) {
555
- if (timingLog) {
556
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
557
- }
558
- var time2 = new Date().getTime();
559
- var groupInvitees = [];
560
-
561
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
562
- if (contactsResult.result.contacts[i].hasUser) {
563
- groupInvitees.push({
564
- id: contactsResult.result.contacts[i].id,
565
- idType: 'TO_BE_USER_CONTACT_ID'
566
- });
567
-
568
- if (groupInvitees.length > 2) {
569
- break;
570
- }
571
- }
572
- }
573
-
574
- if (timingLog) {
575
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
576
- }
577
- var time3 = new Date().getTime();
578
-
579
- chatAgent.createThread({
580
- title: faker.lorem.word(),
581
- uniqueName: faker.lorem.word() + faker.lorem.word(),
582
- type: 'PUBLIC_GROUP',
583
- invitees: groupInvitees
584
- }, function(createThreadResult) {
585
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
586
- groupThreadId = createThreadResult.result.thread.id;
587
- if (timingLog) {
588
- console.log('\x1b[33m ★ Create Group Thread (type = PUBLIC_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
589
- time3);
590
- }
591
- done();
592
- console.log('\n');
593
- } else {
594
- done(JSON.stringify(createThreadResult));
595
- }
596
- });
597
- }
598
- });
599
- });
600
- });
601
-
602
- it('Should CREATE a Group thread with a contact (TYPE = CHANNEL_GROUP)', function(done) {
603
- chatAgent.on('chatReady', function() {
604
- var time1 = new Date().getTime();
605
- chatAgent.getContacts({
606
- count: 50,
607
- offset: 0
608
- }, function(contactsResult) {
609
- if (!contactsResult.hasError) {
610
- if (timingLog) {
611
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
612
- }
613
- var time2 = new Date().getTime();
614
- var groupInvitees = [];
615
-
616
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
617
- if (contactsResult.result.contacts[i].hasUser) {
618
- groupInvitees.push({
619
- id: contactsResult.result.contacts[i].id,
620
- idType: 'TO_BE_USER_CONTACT_ID'
621
- });
622
-
623
- if (groupInvitees.length > 2) {
624
- break;
625
- }
626
- }
627
- }
628
-
629
- if (timingLog) {
630
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
631
- }
632
- var time3 = new Date().getTime();
633
-
634
- chatAgent.createThread({
635
- title: faker.lorem.word(),
636
- type: 'CHANNEL_GROUP',
637
- invitees: groupInvitees
638
- }, function(createThreadResult) {
639
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
640
- groupThreadId = createThreadResult.result.thread.id;
641
- if (timingLog) {
642
- console.log('\x1b[33m ★ Create Group Thread (type = CHANNEL_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
643
- time3);
644
- }
645
- done();
646
- console.log('\n');
647
- } else {
648
- done(JSON.stringify(createThreadResult));
649
- }
650
- });
651
- }
652
- });
653
- });
654
- });
655
-
656
- it('Should CREATE a Group thread with a contact (TYPE = CHANNEL)', function(done) {
657
- chatAgent.on('chatReady', function() {
658
- var time1 = new Date().getTime();
659
- chatAgent.getContacts({
660
- count: 50,
661
- offset: 0
662
- }, function(contactsResult) {
663
- if (!contactsResult.hasError) {
664
- if (timingLog) {
665
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
666
- }
667
- var time2 = new Date().getTime();
668
- var groupInvitees = [];
669
-
670
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
671
- if (contactsResult.result.contacts[i].hasUser) {
672
- groupInvitees.push({
673
- id: contactsResult.result.contacts[i].id,
674
- idType: 'TO_BE_USER_CONTACT_ID'
675
- });
676
-
677
- if (groupInvitees.length > 2) {
678
- break;
679
- }
680
- }
681
- }
682
-
683
- if (timingLog) {
684
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
685
- }
686
- var time3 = new Date().getTime();
687
-
688
- chatAgent.createThread({
689
- title: faker.lorem.word(),
690
- type: 'CHANNEL',
691
- invitees: groupInvitees
692
- }, function(createThreadResult) {
693
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
694
- groupThreadId = createThreadResult.result.thread.id;
695
- if (timingLog) {
696
- console.log('\x1b[33m ★ Create Group Thread (type = CHANNEL) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3);
697
- }
698
- done();
699
- console.log('\n');
700
- } else {
701
- done(JSON.stringify(createThreadResult));
702
- }
703
- });
704
- }
705
- });
706
- });
707
- });
708
-
709
- it('Should GET Thread participants', function(done) {
710
- chatAgent.on('chatReady', function() {
711
- var time = new Date().getTime();
712
- chatAgent.getThreadParticipants({
713
- count: 50,
714
- offset: 0,
715
- threadId: GROUP_THREAD
716
- }, function(participantsResult) {
717
- if (!participantsResult.hasError) {
718
- if (timingLog) {
719
- console.log('\x1b[33m ★ Get Participants \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
720
- }
721
- done();
722
- console.log('\n');
723
- } else {
724
- done(JSON.stringify(participantsResult));
725
- }
726
- });
727
- });
728
- });
729
-
730
- it('Should GET Thread Admins', function(done) {
731
- chatAgent.on('chatReady', function() {
732
- var time = new Date().getTime();
733
- chatAgent.getThreadAdmins({
734
- threadId: GROUP_THREAD
735
- }, function(participantsResult) {
736
- if (!participantsResult.hasError) {
737
- if (timingLog) {
738
- console.log('\x1b[33m ★ Get Participants \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
739
- }
740
- done();
741
- console.log('\n');
742
- } else {
743
- done(JSON.stringify(participantsResult));
744
- }
745
- });
746
- });
747
- });
748
-
749
- it('Should CREATE a Group thread and set a new admin on it', function(done) {
750
- chatAgent.on('chatReady', function() {
751
- var time1 = new Date().getTime();
752
- chatAgent.getContacts({
753
- count: 50,
754
- offset: 0
755
- }, function(contactsResult) {
756
- if (!contactsResult.hasError) {
757
- if (timingLog) {
758
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
759
- }
760
- var time2 = new Date().getTime();
761
- var groupInvitees = [];
762
-
763
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
764
- if (contactsResult.result.contacts[i].hasUser) {
765
- groupInvitees.push({
766
- id: contactsResult.result.contacts[i].id,
767
- idType: 'TO_BE_USER_CONTACT_ID'
768
- });
769
-
770
- if (groupInvitees.length > 2) {
771
- break;
772
- }
773
- }
774
- }
775
-
776
- if (timingLog) {
777
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
778
- }
779
- var time3 = new Date().getTime();
780
-
781
- chatAgent.createThread({
782
- title: faker.lorem.word(),
783
- type: 'NORMAL',
784
- invitees: groupInvitees
785
- }, function(createThreadResult) {
786
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
787
- groupThreadId = createThreadResult.result.thread.id;
788
- if (timingLog) {
789
- console.log('\x1b[90m ☰ Create Group Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
790
- }
791
-
792
- var time4 = new Date().getTime();
793
- chatAgent.getThreadParticipants({
794
- threadId: groupThreadId
795
- }, function(participantsResult) {
796
- if (!participantsResult.hasError) {
797
- if (timingLog) {
798
- console.log('\x1b[90m ☰ Get Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4);
799
- }
800
-
801
- var time5 = new Date().getTime();
802
-
803
- var threadParticipants = participantsResult.result.participants;
804
- var adminCandidate;
805
- for (var i = 0; i < threadParticipants.length; i++) {
806
- if (!threadParticipants[i].admin) {
807
- adminCandidate = threadParticipants[i].id;
808
- break;
809
- }
810
- }
811
-
812
- if (adminCandidate > 0) {
813
- setTimeout(function() {
814
- chatAgent.setAdmin({
815
- threadId: groupThreadId,
816
- admins: [
817
- {
818
- userId: adminCandidate,
819
- roleOperation: 'add',
820
- roles: [
821
- 'post_channel_message',
822
- 'edit_message_of_others',
823
- 'delete_message_of_others',
824
- 'add_new_user',
825
- 'remove_user',
826
- 'thread_admin',
827
- 'add_rule_to_user',
828
- 'remove_role_from_user',
829
- 'read_thread',
830
- 'edit_thread'
831
- ]
832
- }]
833
- }, function(result) {
834
- if (!result.hasError) {
835
- if (timingLog) {
836
- console.log('\x1b[33m ★ Set Admin \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time5 - 500);
837
- }
838
- done();
839
- console.log('\n');
840
- } else {
841
- done(JSON.stringify(result));
842
- }
843
- });
844
- }, 500);
845
- }
846
- }
847
- });
848
- }
849
- });
850
- }
851
- });
852
- });
853
- });
854
-
855
- it('Should CREATE a Group thread, set a new admin on it and remove it afterwards', function(done) {
856
- chatAgent.on('chatReady', function() {
857
- var time1 = new Date().getTime();
858
- chatAgent.getContacts({
859
- count: 50,
860
- offset: 0
861
- }, function(contactsResult) {
862
- if (!contactsResult.hasError) {
863
- if (timingLog) {
864
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
865
- }
866
- var time2 = new Date().getTime();
867
- var groupInvitees = [];
868
-
869
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
870
- if (contactsResult.result.contacts[i].hasUser) {
871
- groupInvitees.push({
872
- id: contactsResult.result.contacts[i].id,
873
- idType: 'TO_BE_USER_CONTACT_ID'
874
- });
875
-
876
- if (groupInvitees.length > 2) {
877
- break;
878
- }
879
- }
880
- }
881
-
882
- if (timingLog) {
883
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
884
- }
885
- var time3 = new Date().getTime();
886
-
887
- chatAgent.createThread({
888
- title: faker.lorem.word(),
889
- type: 'NORMAL',
890
- invitees: groupInvitees
891
- }, function(createThreadResult) {
892
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
893
- groupThreadId = createThreadResult.result.thread.id;
894
- if (timingLog) {
895
- console.log('\x1b[90m ☰ Create Group Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
896
- }
897
-
898
- var time4 = new Date().getTime();
899
- chatAgent.getThreadParticipants({
900
- threadId: groupThreadId
901
- }, function(participantsResult) {
902
- if (!participantsResult.hasError) {
903
- if (timingLog) {
904
- console.log('\x1b[90m ☰ Get Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4);
905
- }
906
-
907
- var time5 = new Date().getTime();
908
-
909
- var threadParticipants = participantsResult.result.participants;
910
- var adminCandidate;
911
- for (var i = 0; i < threadParticipants.length; i++) {
912
- if (!threadParticipants[i].admin) {
913
- adminCandidate = threadParticipants[i].id;
914
- break;
915
- }
916
- }
917
-
918
- if (adminCandidate > 0) {
919
- setTimeout(function() {
920
- chatAgent.setAdmin({
921
- threadId: groupThreadId,
922
- admins: [
923
- {
924
- userId: adminCandidate,
925
- roleOperation: 'add',
926
- roles: [
927
- 'post_channel_message',
928
- 'edit_message_of_others',
929
- 'delete_message_of_others',
930
- 'add_new_user',
931
- 'remove_user',
932
- 'thread_admin',
933
- 'add_rule_to_user',
934
- 'remove_role_from_user',
935
- 'read_thread',
936
- 'edit_thread'
937
- ]
938
- }]
939
- }, function(result) {
940
- if (!result.hasError) {
941
- if (timingLog) {
942
- console.log('\x1b[90m ☰ Set Admin \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time5 - 500);
943
- }
944
-
945
- var time6 = new Date().getTime();
946
- setTimeout(function() {
947
- chatAgent.setAdmin({
948
- threadId: groupThreadId,
949
- admins: [
950
- {
951
- userId: adminCandidate,
952
- roleOperation: 'remove',
953
- roles: [
954
- 'post_channel_message',
955
- 'edit_message_of_others',
956
- 'delete_message_of_others',
957
- 'add_new_user',
958
- 'remove_user',
959
- 'thread_admin',
960
- 'add_rule_to_user',
961
- 'remove_role_from_user',
962
- 'read_thread',
963
- 'edit_thread'
964
- ]
965
- }]
966
- }, function(result) {
967
- if (!result.hasError) {
968
- if (timingLog) {
969
- console.log('\x1b[33m ★ Remove Admin \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
970
- time6 - 500);
971
- }
972
-
973
- done();
974
- console.log('\n');
975
- } else {
976
- done(JSON.stringify(result));
977
- }
978
- });
979
- }, 500);
980
- }
981
- });
982
- }, 500);
983
- }
984
- }
985
- });
986
- }
987
- });
988
- }
989
- });
990
- });
991
- });
992
-
993
- it('Should ADD A PARTICIPANT to newly created group Thread', function(done) {
994
- chatAgent.on('chatReady', function() {
995
- var time1 = new Date().getTime();
996
- chatAgent.getContacts({
997
- count: 50,
998
- offset: 0
999
- }, function(contactsResult) {
1000
- if (!contactsResult.hasError) {
1001
- if (timingLog) {
1002
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1003
- }
1004
- var time2 = new Date().getTime();
1005
- var groupInvitees = [];
1006
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
1007
- if (contactsResult.result.contacts[i].hasUser) {
1008
- groupInvitees.push({
1009
- id: contactsResult.result.contacts[i].id,
1010
- idType: 'TO_BE_USER_CONTACT_ID'
1011
- });
1012
-
1013
- if (groupInvitees.length > 2) {
1014
- break;
1015
- }
1016
- }
1017
- }
1018
-
1019
- var lastInvitee = groupInvitees.pop();
1020
-
1021
- if (timingLog) {
1022
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
1023
- }
1024
- var time3 = new Date().getTime();
1025
-
1026
- chatAgent.createThread({
1027
- title: faker.lorem.word(),
1028
- type: 'NORMAL',
1029
- invitees: groupInvitees
1030
- }, function(createThreadResult) {
1031
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
1032
- if (timingLog) {
1033
- console.log('\x1b[90m ☰ Create New Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
1034
- }
1035
- var time4 = new Date().getTime();
1036
- var newGroupThreadId = createThreadResult.result.thread.id;
1037
-
1038
- setTimeout(function() {
1039
- chatAgent.addParticipants({
1040
- threadId: newGroupThreadId,
1041
- contactIds: [lastInvitee.id]
1042
- }, function(result) {
1043
- if (!result.hasError) {
1044
- if (timingLog) {
1045
- console.log('\x1b[33m ★ Add Participant \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time4 - 500);
1046
- }
1047
- done();
1048
- console.log('\n');
1049
- } else {
1050
- done(JSON.stringify(result));
1051
- }
1052
- });
1053
- }, 500);
1054
- }
1055
- });
1056
- }
1057
- });
1058
- });
1059
- });
1060
-
1061
- it('Should REMOVE A PARTICIPANT from newly created group Thread', function(done) {
1062
- chatAgent.on('chatReady', function() {
1063
- var time1 = new Date().getTime();
1064
- chatAgent.getContacts({
1065
- count: 50,
1066
- offset: 0
1067
- }, function(contactsResult) {
1068
- if (!contactsResult.hasError) {
1069
-
1070
- if (timingLog) {
1071
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1072
- }
1073
- var time2 = new Date().getTime();
1074
-
1075
- var groupInvitees = [];
1076
-
1077
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
1078
- if (contactsResult.result.contacts[i].hasUser) {
1079
- groupInvitees.push({
1080
- id: contactsResult.result.contacts[i].id,
1081
- idType: 'TO_BE_USER_CONTACT_ID'
1082
- });
1083
-
1084
- if (groupInvitees.length > 2) {
1085
- break;
1086
- }
1087
- }
1088
- }
1089
-
1090
- if (timingLog) {
1091
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
1092
- }
1093
- var time3 = new Date().getTime();
1094
-
1095
- chatAgent.createThread({
1096
- title: faker.lorem.word(),
1097
- type: 'NORMAL',
1098
- invitees: groupInvitees
1099
- }, function(createThreadResult) {
1100
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
1101
-
1102
- if (timingLog) {
1103
- console.log('\x1b[90m ☰ Create Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
1104
- }
1105
- var time4 = new Date().getTime();
1106
-
1107
- var newGroupThreadId = createThreadResult.result.thread.id;
1108
-
1109
- chatAgent.getThreadParticipants({
1110
- count: 50,
1111
- offset: 0,
1112
- threadId: newGroupThreadId
1113
- }, function(participantsResult) {
1114
- if (!participantsResult.hasError) {
1115
-
1116
- if (timingLog) {
1117
- console.log('\x1b[90m ☰ Get Thread Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4);
1118
- }
1119
- var time5 = new Date().getTime();
1120
-
1121
- setTimeout(function() {
1122
-
1123
- var userId = participantsResult.result.participants[0];
1124
- chatAgent.removeParticipants({
1125
- threadId: newGroupThreadId,
1126
- participantIds: [userId.id]
1127
- }, function(result) {
1128
- if (!result.hasError) {
1129
- if (timingLog) {
1130
- console.log('\x1b[33m ★ Remove Participant \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time5 -
1131
- 500);
1132
- }
1133
- done();
1134
- console.log('\n');
1135
- } else {
1136
- done(JSON.stringify(result));
1137
- }
1138
- });
1139
- }, 500);
1140
- }
1141
- });
1142
- }
1143
- });
1144
- }
1145
- });
1146
- });
1147
- });
1148
-
1149
- it('Should LEAVE a newly created group Thread', function(done) {
1150
- chatAgent.on('chatReady', function() {
1151
- var time1 = new Date().getTime();
1152
-
1153
- chatAgent.getContacts({
1154
- count: 50,
1155
- offset: 0
1156
- }, function(contactsResult) {
1157
- if (!contactsResult.hasError) {
1158
- if (timingLog) {
1159
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1160
- }
1161
- var time2 = new Date().getTime();
1162
-
1163
- var groupInvitees = [];
1164
-
1165
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
1166
- if (contactsResult.result.contacts[i].hasUser) {
1167
- groupInvitees.push({
1168
- id: contactsResult.result.contacts[i].id,
1169
- idType: 'TO_BE_USER_CONTACT_ID'
1170
- });
1171
-
1172
- if (groupInvitees.length > 2) {
1173
- break;
1174
- }
1175
- }
1176
- }
1177
-
1178
- if (timingLog) {
1179
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
1180
- }
1181
- var time3 = new Date().getTime();
1182
-
1183
- chatAgent.createThread({
1184
- title: faker.lorem.word(),
1185
- type: 'NORMAL',
1186
- invitees: groupInvitees
1187
- }, function(createThreadResult) {
1188
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
1189
-
1190
- if (timingLog) {
1191
- console.log('\x1b[90m ☰ Create Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
1192
- }
1193
- var time4 = new Date().getTime();
1194
-
1195
- var newGroupThreadId = createThreadResult.result.thread.id;
1196
-
1197
- chatAgent.leaveThread({
1198
- threadId: newGroupThreadId
1199
- }, function(result) {
1200
- if (!result.hasError) {
1201
- if (timingLog) {
1202
- console.log('\x1b[33m ★ Leave Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time4);
1203
- }
1204
- done();
1205
- console.log('\n');
1206
- } else {
1207
- done(JSON.stringify(result));
1208
- }
1209
- });
1210
- }
1211
- });
1212
- }
1213
- });
1214
- });
1215
- });
1216
-
1217
- it('Should GET HISTORY of lastest active thread', function(done) {
1218
- chatAgent.on('chatReady', function() {
1219
- var time1 = new Date().getTime();
1220
- chatAgent.getThreads({
1221
- count: 1,
1222
- offset: 0
1223
- }, function(threadsResult) {
1224
- if (!threadsResult.hasError) {
1225
-
1226
- if (timingLog) {
1227
- console.log('\x1b[90m ☰ Get Threads List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1228
- }
1229
- var time2 = new Date().getTime();
1230
-
1231
- var threadId = threadsResult.result.threads[0].id;
1232
-
1233
- chatAgent.getHistory({
1234
- threadId: threadId
1235
- }, function(historyResult) {
1236
- if (!historyResult.hasError) {
1237
- if (timingLog) {
1238
- console.log('\x1b[33m ★ Get History \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
1239
- }
1240
- done();
1241
- console.log('\n');
1242
- } else {
1243
- done(JSON.stringify(historyResult));
1244
- }
1245
- });
1246
- }
1247
- });
1248
- });
1249
- });
1250
-
1251
- it('Should CLEAR HISTORY of lastest active thread', function(done) {
1252
- chatAgent.on('chatReady', function() {
1253
- var time1 = new Date().getTime();
1254
- chatAgent.getThreads({
1255
- count: 1,
1256
- offset: 0
1257
- }, function(threadsResult) {
1258
- if (!threadsResult.hasError) {
1259
-
1260
- if (timingLog) {
1261
- console.log('\x1b[90m ☰ Get Threads List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1262
- }
1263
- var time2 = new Date().getTime();
1264
-
1265
- var threadId = threadsResult.result.threads[0].id;
1266
-
1267
- chatAgent.clearHistory({
1268
- threadId: threadId
1269
- }, function(historyResult) {
1270
- if (!historyResult.hasError) {
1271
- if (timingLog) {
1272
- console.log('\x1b[33m ★ Clear History \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
1273
- }
1274
- done();
1275
- console.log('\n');
1276
- } else {
1277
- done(JSON.stringify(historyResult));
1278
- }
1279
- });
1280
- }
1281
- });
1282
- });
1283
- });
1284
-
1285
- it('Should Search in Thread Messages', function(done) {
1286
- chatAgent.on('chatReady', function() {
1287
- var time1 = new Date().getTime();
1288
-
1289
- chatAgent.getHistory({
1290
- threadId: P2P_THREAD,
1291
- query: 'sample'
1292
- }, function(historyResult) {
1293
- if (!historyResult.hasError) {
1294
- if (timingLog) {
1295
- console.log('\x1b[33m ★ Search in History \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
1296
- }
1297
- done();
1298
- console.log('\n');
1299
- } else {
1300
- done(JSON.stringify(historyResult));
1301
- }
1302
- });
1303
- });
1304
- });
1305
-
1306
- it('Should Search in Thread Messages METADATA', function(done) {
1307
- chatAgent.on('chatReady', function() {
1308
- var time = new Date().getTime();
1309
- chatAgent.sendTextMessage({
1310
- threadId: P2P_THREAD,
1311
- textMessage: faker.lorem.paragraph(),
1312
- systemMetadata: {
1313
- type: 'test'
1314
- }
1315
- }, {
1316
- onSent: function(result) {
1317
- if (timingLog) {
1318
- console.log('\x1b[90m ☰ Send Text Message with metadata \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1319
- }
1320
-
1321
- var time1 = new Date().getTime();
1322
- chatAgent.getHistory({
1323
- threadId: GROUP_THREAD,
1324
- metadataCriteria: {
1325
- 'field': 'type',
1326
- 'has': 'test'
1327
- }
1328
- }, function(historyResult) {
1329
- if (!historyResult.hasError) {
1330
- if (timingLog) {
1331
- console.log('\x1b[33m ★ Search in Messages MetaData \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1);
1332
- }
1333
- done();
1334
- console.log('\n');
1335
- } else {
1336
- done(JSON.stringify(historyResult));
1337
- }
1338
- });
1339
- }
1340
- });
1341
- });
1342
- });
1343
-
1344
- it('Should MUTE a thread', function(done) {
1345
- chatAgent.on('chatReady', function() {
1346
- var time1 = new Date().getTime();
1347
- chatAgent.getContacts({
1348
- count: 50,
1349
- offset: 0
1350
- }, function(contactsResult) {
1351
- if (!contactsResult.hasError) {
1352
- if (timingLog) {
1353
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1354
- }
1355
- var time2 = new Date().getTime();
1356
- var groupInvitees = [];
1357
-
1358
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
1359
- if (contactsResult.result.contacts[i].hasUser) {
1360
- groupInvitees.push({
1361
- id: contactsResult.result.contacts[i].id,
1362
- idType: 'TO_BE_USER_CONTACT_ID'
1363
- });
1364
-
1365
- if (groupInvitees.length > 2) {
1366
- break;
1367
- }
1368
- }
1369
- }
1370
-
1371
- if (timingLog) {
1372
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
1373
- }
1374
- var time3 = new Date().getTime();
1375
-
1376
- chatAgent.createThread({
1377
- title: faker.lorem.word(),
1378
- type: 'NORMAL',
1379
- invitees: groupInvitees
1380
- }, function(createThreadResult) {
1381
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
1382
- if (timingLog) {
1383
- console.log('\x1b[90m ☰ Create Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
1384
- }
1385
- var time4 = new Date().getTime();
1386
-
1387
- muteThreadId = createThreadResult.result.thread.id;
1388
- chatAgent.muteThread({
1389
- threadId: muteThreadId
1390
- }, function(result) {
1391
- if (!result.hasError) {
1392
- if (timingLog) {
1393
- console.log('\x1b[33m ★ Mute Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time4);
1394
- }
1395
- done();
1396
- console.log('\n');
1397
- } else {
1398
- done(JSON.stringify(result));
1399
- }
1400
- });
1401
- }
1402
- });
1403
- }
1404
- });
1405
- });
1406
- });
1407
-
1408
- it('Should UNMUTE a muted thread', function(done) {
1409
- chatAgent.on('chatReady', function() {
1410
- var time = new Date().getTime();
1411
- chatAgent.unMuteThread({
1412
- threadId: muteThreadId
1413
- }, function(result) {
1414
- if (!result.hasError) {
1415
- if (timingLog) {
1416
- console.log('\x1b[33m ★ UnMute Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1417
- }
1418
- done();
1419
- console.log('\n');
1420
- } else {
1421
- done(JSON.stringify(result));
1422
- }
1423
- });
1424
- });
1425
- });
1426
-
1427
- it('Should update a newly created thread\'s Meta Info (name, image, description, metadata)', function(done) {
1428
- chatAgent.on('chatReady', function() {
1429
- var time1 = new Date().getTime();
1430
- chatAgent.getContacts({
1431
- count: 50,
1432
- offset: 0
1433
- }, function(contactsResult) {
1434
- if (!contactsResult.hasError) {
1435
- if (timingLog) {
1436
- console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1437
- }
1438
- var time2 = new Date().getTime();
1439
- var groupInvitees = [];
1440
-
1441
- for (var i = 0; i < contactsResult.result.contacts.length; i++) {
1442
- if (contactsResult.result.contacts[i].hasUser) {
1443
- groupInvitees.push({
1444
- id: contactsResult.result.contacts[i].id,
1445
- idType: 'TO_BE_USER_CONTACT_ID'
1446
- });
1447
-
1448
- if (groupInvitees.length > 2) {
1449
- break;
1450
- }
1451
- }
1452
- }
1453
-
1454
- if (timingLog) {
1455
- console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2);
1456
- }
1457
- var time3 = new Date().getTime();
1458
-
1459
- chatAgent.createThread({
1460
- title: faker.lorem.word(),
1461
- type: 'NORMAL',
1462
- invitees: groupInvitees
1463
- }, function(createThreadResult) {
1464
- if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
1465
- if (timingLog) {
1466
- console.log('\x1b[90m ☰ Create Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
1467
- }
1468
- var time4 = new Date().getTime();
1469
-
1470
- var newGroupThreadId = createThreadResult.result.thread.id;
1471
-
1472
- setTimeout(function() {
1473
- chatAgent.updateThreadInfo({
1474
- threadId: newGroupThreadId,
1475
- image: 'https://static2.farakav.com/files/pictures/thumb/01330672.jpg',
1476
- description: faker.lorem.sentence(),
1477
- title: faker.lorem.sentence(),
1478
- metadata: {
1479
- title: 'Test',
1480
- name: 'Masoud Amjadi'
1481
- }
1482
- }, function(result) {
1483
- if (!result.hasError) {
1484
- if (timingLog) {
1485
- console.log('\x1b[33m ★ Thread Info get updated \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time4 -
1486
- 500);
1487
- }
1488
- done();
1489
- console.log('\n');
1490
- } else {
1491
- done(JSON.stringify(result));
1492
- }
1493
- });
1494
- }, 500);
1495
- }
1496
- });
1497
- }
1498
- });
1499
- });
1500
- });
1501
- });
1502
-
1503
- /**
1504
- * MESSAGING FUNCTIONS
1505
- */
1506
- describe('Messaging Functionality', function(done) {
1507
- this.timeout(60000);
1508
-
1509
- var chatAgent1,
1510
- chatAgent2;
1511
-
1512
- beforeEach(() => {
1513
- chatAgent1 = new Chat(params1);
1514
- chatAgent2 = new Chat(params2);
1515
- });
1516
-
1517
- afterEach(() => {
1518
- chatAgent1.logout();
1519
- chatAgent2.logout();
1520
- });
1521
-
1522
- it('Should SEND a text message to a P2P thread', function(done) {
1523
- chatAgent1.on('chatReady', function() {
1524
- var time = new Date().getTime();
1525
-
1526
- chatAgent1.sendTextMessage({
1527
- threadId: P2P_THREAD,
1528
- textMessage: faker.lorem.paragraph()
1529
- }, {
1530
- onSent: function(result) {
1531
- if (timingLog) {
1532
- console.log('\x1b[33m ★ Send Message \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1533
- }
1534
- done();
1535
- console.log('\n');
1536
- },
1537
- onDeliver: function(result) {
1538
- },
1539
- onSeen: function(result) {
1540
- }
1541
- });
1542
- });
1543
- });
1544
-
1545
- it('Should SEND a FILE message to a newly created P2P thread', function(done) {
1546
- chatAgent1.on('chatReady', function() {
1547
- var time = new Date().getTime();
1548
- chatAgent1.sendTextMessage({
1549
- threadId: P2P_THREAD,
1550
- textMessage: faker.lorem.paragraph(),
1551
- file: __dirname + '/test.jpg',
1552
- metadata: {
1553
- custom_name: 'John Doe'
1554
- }
1555
- }, {
1556
- onSent: function(result) {
1557
- if (timingLog) {
1558
- console.log('\x1b[33m ★ Send File Message \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1559
- }
1560
- done();
1561
- console.log('\n');
1562
- },
1563
- onDeliver: function(result) {
1564
- console.log(result.uniqueId + ' \t has been Delivered!');
1565
- },
1566
- onSeen: function(result) {
1567
- console.log(result.uniqueId + ' \t has been Seen!');
1568
- }
1569
- });
1570
- });
1571
- });
1572
-
1573
- it('Should RECEIVE a DELIVERY message for a newly message sent to a P2P thread', function(done) {
1574
- chatAgent1.on('chatReady', function() {
1575
- var time = new Date().getTime();
1576
- chatAgent1.sendTextMessage({
1577
- threadId: P2P_THREAD,
1578
- textMessage: faker.lorem.paragraph()
1579
- }, {
1580
- onSent: function(result) {
1581
- },
1582
- onDeliver: function(result) {
1583
- if (timingLog) {
1584
- console.log('\x1b[33m ★ Receiving Delivery \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1585
- }
1586
- done();
1587
- console.log('\n');
1588
- },
1589
- onSeen: function(result) {
1590
- }
1591
- });
1592
- });
1593
- });
1594
-
1595
- it('Should RECEIVE a SEEN message for a newly message sent to a P2P thread', function(done) {
1596
- chatAgent1.on('chatReady', function() {
1597
- var time = new Date().getTime();
1598
- chatAgent1.sendTextMessage({
1599
- threadId: P2P_THREAD,
1600
- textMessage: faker.lorem.paragraph()
1601
- }, {
1602
- onSent: function(result) {
1603
- },
1604
- onDeliver: function(result) {
1605
- },
1606
- onSeen: function(result) {
1607
- if (timingLog) {
1608
- console.log('\x1b[33m ★ Receiving Seen Acknowledge \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
1609
- }
1610
- done();
1611
- console.log('\n');
1612
- }
1613
- });
1614
- });
1615
-
1616
- chatAgent2.on('chatReady', function() {
1617
- chatAgent2.on('messageEvents', function(event) {
1618
- var type = event.type,
1619
- message = event.result.message;
1620
-
1621
- if (type == 'MESSAGE_NEW') {
1622
- chatAgent2.seen({
1623
- messageId: message.id,
1624
- ownerId: message.ownerId
1625
- });
1626
- }
1627
- });
1628
- });
1629
-
1630
- });
1631
-
1632
- it('Should sent a message to a P2P thread then EDIT the sent message afterwards', function(done) {
1633
- var sentMessageID;
1634
-
1635
- chatAgent1.on('chatReady', function() {
1636
- var time1 = new Date().getTime();
1637
- chatAgent1.sendTextMessage({
1638
- threadId: P2P_THREAD,
1639
- textMessage: faker.lorem.paragraph()
1640
- }, {
1641
- onSent: function(result) {
1642
- },
1643
- onDeliver: function(result) {
1644
- if (timingLog) {
1645
- console.log('\x1b[90m ☰ Send a Message to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1646
- }
1647
- var time2 = new Date().getTime();
1648
- chatAgent1.editMessage({
1649
- messageId: sentMessageID,
1650
- content: faker.lorem.paragraph()
1651
- }, function(result) {
1652
- if (!result.hasError) {
1653
- if (timingLog) {
1654
- console.log('\x1b[33m ★ Edit Message \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
1655
- }
1656
- done();
1657
- console.log('\n');
1658
- } else {
1659
- done(JSON.stringify(result));
1660
- }
1661
- });
1662
- },
1663
- onSeen: function(result) {
1664
- }
1665
- });
1666
- });
1667
-
1668
- chatAgent2.on('chatReady', function() {
1669
- chatAgent2.on('messageEvents', function(event) {
1670
- var type = event.type,
1671
- message = event.result.message;
1672
-
1673
- if (type == 'MESSAGE_NEW') {
1674
- sentMessageID = message.id;
1675
- }
1676
- });
1677
- });
1678
-
1679
- });
1680
-
1681
- it('Should sent a message to a P2P thread then DELETE the sent message afterwards (Delete For themeself Only)', function(done) {
1682
- var sentMessageID;
1683
-
1684
- chatAgent1.on('chatReady', function() {
1685
- var time1 = new Date().getTime();
1686
- chatAgent1.sendTextMessage({
1687
- threadId: P2P_THREAD,
1688
- textMessage: faker.lorem.paragraph()
1689
- }, {
1690
- onSent: function(result) {
1691
- },
1692
- onDeliver: function(result) {
1693
- if (timingLog) {
1694
- console.log('\x1b[90m ☰ Send a Message to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1695
- }
1696
- var time2 = new Date().getTime();
1697
- chatAgent1.deleteMessage({
1698
- messageId: sentMessageID,
1699
- // content: JSON.stringify({
1700
- deleteForAll: false
1701
- // })
1702
- }, function(result) {
1703
- if (!result.hasError) {
1704
- if (timingLog) {
1705
- console.log('\x1b[33m ★ Delete Message from P2P \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
1706
- }
1707
- done();
1708
- console.log('\n');
1709
- } else {
1710
- done(JSON.stringify(result));
1711
- }
1712
- });
1713
- },
1714
- onSeen: function(result) {
1715
- }
1716
- });
1717
- });
1718
-
1719
- chatAgent2.on('chatReady', function() {
1720
- chatAgent2.on('messageEvents', function(event) {
1721
- var type = event.type,
1722
- message = event.result.message;
1723
-
1724
- if (type == 'MESSAGE_NEW') {
1725
- sentMessageID = message.id;
1726
- }
1727
- });
1728
- });
1729
-
1730
- });
1731
-
1732
- it('Should sent a message to a P2P thread then DELETE the sent message afterwards (Delete For All)', function(done) {
1733
- var sentMessageID;
1734
-
1735
- chatAgent1.on('chatReady', function() {
1736
- var time1 = new Date().getTime();
1737
- chatAgent1.sendTextMessage({
1738
- threadId: P2P_THREAD,
1739
- textMessage: faker.lorem.paragraph()
1740
- }, {
1741
- onSent: function(result) {
1742
- },
1743
- onDeliver: function(result) {
1744
- if (timingLog) {
1745
- console.log('\x1b[90m ☰ Send a Message to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1746
- }
1747
- var time2 = new Date().getTime();
1748
- chatAgent1.deleteMessage({
1749
- messageId: sentMessageID,
1750
- content: JSON.stringify({
1751
- deleteForAll: true
1752
- })
1753
- }, function(result) {
1754
- if (!result.hasError) {
1755
- if (timingLog) {
1756
- console.log('\x1b[33m ★ Delete Message from P2P (Delete For All) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
1757
- time2);
1758
- }
1759
- done();
1760
- console.log('\n');
1761
- } else {
1762
- done(JSON.stringify(result));
1763
- }
1764
- });
1765
- },
1766
- onSeen: function(result) {
1767
- }
1768
- });
1769
- });
1770
-
1771
- chatAgent2.on('chatReady', function() {
1772
- chatAgent2.on('messageEvents', function(event) {
1773
- var type = event.type,
1774
- message = event.result.message;
1775
-
1776
- if (type == 'MESSAGE_NEW') {
1777
- sentMessageID = message.id;
1778
- }
1779
- });
1780
- });
1781
-
1782
- });
1783
-
1784
- it('Should sent a message to a GROUP thread then DELETE the sent message afterwards (Delete For themeself Only)', function(done) {
1785
- var sentMessageID;
1786
-
1787
- chatAgent1.on('chatReady', function() {
1788
- var time1 = new Date().getTime();
1789
- chatAgent1.sendTextMessage({
1790
- threadId: GROUP_THREAD,
1791
- textMessage: faker.lorem.paragraph()
1792
- }, {
1793
- onSent: function(result) {
1794
- },
1795
- onDeliver: function(result) {
1796
- if (timingLog) {
1797
- console.log('\x1b[90m ☰ Send a Message to GROUP Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1798
- }
1799
- var time2 = new Date().getTime();
1800
- chatAgent1.deleteMessage({
1801
- messageId: sentMessageID,
1802
- content: JSON.stringify({
1803
- deleteForAll: false
1804
- })
1805
- }, function(result) {
1806
- if (!result.hasError) {
1807
- if (timingLog) {
1808
- console.log('\x1b[33m ★ Delete Message from Group\x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
1809
- }
1810
- done();
1811
- console.log('\n');
1812
- } else {
1813
- done(JSON.stringify(result));
1814
- }
1815
- });
1816
- },
1817
- onSeen: function(result) {
1818
- }
1819
- });
1820
- });
1821
-
1822
- chatAgent2.on('chatReady', function() {
1823
- chatAgent2.on('messageEvents', function(event) {
1824
- var type = event.type,
1825
- message = event.result.message;
1826
-
1827
- if (type == 'MESSAGE_NEW') {
1828
- sentMessageID = message.id;
1829
- }
1830
- });
1831
- });
1832
-
1833
- });
1834
-
1835
- it('Should sent a message to a GROUP thread then DELETE the sent message afterwards (Delete For All)', function(done) {
1836
- var sentMessageID;
1837
-
1838
- chatAgent1.on('chatReady', function() {
1839
- var time1 = new Date().getTime();
1840
- chatAgent1.sendTextMessage({
1841
- threadId: GROUP_THREAD,
1842
- textMessage: faker.lorem.paragraph()
1843
- }, {
1844
- onSent: function(result) {
1845
- },
1846
- onDeliver: function(result) {
1847
- if (timingLog) {
1848
- console.log('\x1b[90m ☰ Send a Message to GROUP Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1849
- }
1850
- var time2 = new Date().getTime();
1851
- chatAgent1.deleteMessage({
1852
- messageId: sentMessageID,
1853
- content: JSON.stringify({
1854
- deleteForAll: true
1855
- })
1856
- }, function(result) {
1857
- if (!result.hasError) {
1858
- if (timingLog) {
1859
- console.log('\x1b[33m ★ Delete Message from Group (Delete For All) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() -
1860
- time2);
1861
- }
1862
- done();
1863
- console.log('\n');
1864
- } else {
1865
- done(JSON.stringify(result));
1866
- }
1867
- });
1868
- },
1869
- onSeen: function(result) {
1870
- }
1871
- });
1872
- });
1873
-
1874
- chatAgent2.on('chatReady', function() {
1875
- chatAgent2.on('messageEvents', function(event) {
1876
- var type = event.type,
1877
- message = event.result.message;
1878
-
1879
- if (type == 'MESSAGE_NEW') {
1880
- sentMessageID = message.id;
1881
- }
1882
- });
1883
- });
1884
-
1885
- });
1886
-
1887
- it('Should sent several messages to a P2P thread then DELETE the sent messages afterwards (Delete For themeself Only)', function(done) {
1888
- var sentMessageIDs = [],
1889
- sentMessagesCount = 0,
1890
- deletedMessagesCount = 0;
1891
-
1892
- chatAgent1.on('chatReady', function() {
1893
- var time1 = new Date().getTime();
1894
-
1895
- for (var i = 0; i < 5; i++) {
1896
- chatAgent1.sendTextMessage({
1897
- threadId: P2P_THREAD,
1898
- textMessage: faker.lorem.paragraph()
1899
- }, {
1900
- onSent: function(result) {},
1901
- onDeliver: function(result) {
1902
- sentMessagesCount++;
1903
-
1904
- if(sentMessagesCount == 4) {
1905
- if (timingLog) {
1906
- console.log('\x1b[90m ☰ Send 5 Messages to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1907
- }
1908
- var time2 = new Date().getTime();
1909
-
1910
- chatAgent1.deleteMultipleMessages({
1911
- threadId: P2P_THREAD,
1912
- messageIds: sentMessageIDs,
1913
- deleteForAll: false
1914
- }, function(result) {
1915
- if (!result.hasError) {
1916
- deletedMessagesCount++;
1917
-
1918
- if(deletedMessagesCount == 4) {
1919
- if (timingLog) {
1920
- console.log('\x1b[33m ★ Delete Multiple Messages from P2P \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
1921
- }
1922
- done();
1923
- console.log('\n');
1924
- }
1925
- } else {
1926
- done(JSON.stringify(result));
1927
- }
1928
- });
1929
- }
1930
- },
1931
- onSeen: function(result) {}
1932
- });
1933
- }
1934
- });
1935
-
1936
- chatAgent2.on('chatReady', function() {
1937
- chatAgent2.on('messageEvents', function(event) {
1938
- var type = event.type,
1939
- message = event.result.message;
1940
-
1941
- if (type == 'MESSAGE_NEW') {
1942
- sentMessageIDs.push(message.id);
1943
- }
1944
- });
1945
- });
1946
-
1947
- });
1948
-
1949
- it('Should sent several messages to a P2P thread then DELETE the sent messages afterwards (Delete For All)', function(done) {
1950
- var sentMessageIDs = [],
1951
- sentMessagesCount = 0,
1952
- deletedMessagesCount = 0;
1953
-
1954
- chatAgent1.on('chatReady', function() {
1955
- var time1 = new Date().getTime();
1956
-
1957
- for (var i = 0; i < 5; i++) {
1958
- chatAgent1.sendTextMessage({
1959
- threadId: P2P_THREAD,
1960
- textMessage: faker.lorem.paragraph()
1961
- }, {
1962
- onSent: function(result) {},
1963
- onDeliver: function(result) {
1964
- sentMessagesCount++;
1965
-
1966
- if(sentMessagesCount == 5) {
1967
- if (timingLog) {
1968
- console.log('\x1b[90m ☰ Send 5 Messages to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
1969
- }
1970
- var time2 = new Date().getTime();
1971
-
1972
- chatAgent1.deleteMultipleMessages({
1973
- threadId: P2P_THREAD,
1974
- messageIds: sentMessageIDs,
1975
- deleteForAll: true
1976
- }, function(result) {
1977
- if (!result.hasError) {
1978
- deletedMessagesCount++;
1979
-
1980
- if(deletedMessagesCount == 5) {
1981
- if (timingLog) {
1982
- console.log('\x1b[33m ★ Delete Multiple Messages from P2P \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
1983
- }
1984
- done();
1985
- console.log('\n');
1986
- }
1987
- } else {
1988
- done(JSON.stringify(result));
1989
- }
1990
- });
1991
- }
1992
- },
1993
- onSeen: function(result) {}
1994
- });
1995
- }
1996
- });
1997
-
1998
- chatAgent2.on('chatReady', function() {
1999
- chatAgent2.on('messageEvents', function(event) {
2000
- var type = event.type,
2001
- message = event.result.message;
2002
-
2003
- if (type == 'MESSAGE_NEW') {
2004
- sentMessageIDs.push(message.id);
2005
- }
2006
- });
2007
- });
2008
-
2009
- });
2010
-
2011
- it('Should sent several messages to a GROUP thread then DELETE the sent messages afterwards (Delete For themeself Only)', function(done) {
2012
- var sentMessageIDs = [],
2013
- sentMessagesCount = 0,
2014
- deletedMessagesCount = 0;
2015
-
2016
- chatAgent1.on('chatReady', function() {
2017
- var time1 = new Date().getTime();
2018
-
2019
- for (var i = 0; i < 5; i++) {
2020
- chatAgent1.sendTextMessage({
2021
- threadId: GROUP_THREAD,
2022
- textMessage: faker.lorem.paragraph()
2023
- }, {
2024
- onSent: function(result) {},
2025
- onDeliver: function(result) {
2026
- sentMessagesCount++;
2027
-
2028
- if(sentMessagesCount == 4) {
2029
- if (timingLog) {
2030
- console.log('\x1b[90m ☰ Send 5 Messages to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
2031
- }
2032
- var time2 = new Date().getTime();
2033
-
2034
- chatAgent1.deleteMultipleMessages({
2035
- threadId: GROUP_THREAD,
2036
- messageIds: sentMessageIDs,
2037
- deleteForAll: false
2038
- }, function(result) {
2039
- if (!result.hasError) {
2040
- deletedMessagesCount++;
2041
-
2042
- if(deletedMessagesCount == 4) {
2043
- if (timingLog) {
2044
- console.log('\x1b[33m ★ Delete Multiple Messages from Group Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
2045
- }
2046
- done();
2047
- console.log('\n');
2048
- }
2049
- } else {
2050
- done(JSON.stringify(result));
2051
- }
2052
- });
2053
- }
2054
- },
2055
- onSeen: function(result) {}
2056
- });
2057
- }
2058
- });
2059
-
2060
- chatAgent2.on('chatReady', function() {
2061
- chatAgent2.on('messageEvents', function(event) {
2062
- var type = event.type,
2063
- message = event.result.message;
2064
-
2065
- if (type == 'MESSAGE_NEW') {
2066
- sentMessageIDs.push(message.id);
2067
- }
2068
- });
2069
- });
2070
-
2071
- });
2072
-
2073
- it('Should sent several messages to a GROUP thread then DELETE the sent messages afterwards (Delete For All)', function(done) {
2074
- var sentMessageIDs = [],
2075
- sentMessagesCount = 0,
2076
- deletedMessagesCount = 0;
2077
-
2078
- chatAgent1.on('chatReady', function() {
2079
- var time1 = new Date().getTime();
2080
-
2081
- for (var i = 0; i < 5; i++) {
2082
- chatAgent1.sendTextMessage({
2083
- threadId: GROUP_THREAD,
2084
- textMessage: faker.lorem.paragraph()
2085
- }, {
2086
- onSent: function(result) {},
2087
- onDeliver: function(result) {
2088
- sentMessagesCount++;
2089
-
2090
- if(sentMessagesCount == 4) {
2091
- if (timingLog) {
2092
- console.log('\x1b[90m ☰ Send 5 Messages to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
2093
- }
2094
- var time2 = new Date().getTime();
2095
-
2096
- chatAgent1.deleteMultipleMessages({
2097
- threadId: GROUP_THREAD,
2098
- messageIds: sentMessageIDs,
2099
- deleteForAll: true
2100
- }, function(result) {
2101
- if (!result.hasError) {
2102
- deletedMessagesCount++;
2103
-
2104
- if(deletedMessagesCount == 4) {
2105
- if (timingLog) {
2106
- console.log('\x1b[33m ★ Delete Multiple Messages from Group Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
2107
- }
2108
- done();
2109
- console.log('\n');
2110
- }
2111
- } else {
2112
- done(JSON.stringify(result));
2113
- }
2114
- });
2115
- }
2116
- },
2117
- onSeen: function(result) {}
2118
- });
2119
- }
2120
- });
2121
-
2122
- chatAgent2.on('chatReady', function() {
2123
- chatAgent2.on('messageEvents', function(event) {
2124
- var type = event.type,
2125
- message = event.result.message;
2126
-
2127
- if (type == 'MESSAGE_NEW') {
2128
- sentMessageIDs.push(message.id);
2129
- }
2130
- });
2131
- });
2132
-
2133
- });
2134
-
2135
- it('Should receive a message from a P2P thread then REPLY to the message', function(done) {
2136
- var sentMessageID,
2137
- sentMessageUID;
2138
-
2139
- chatAgent1.on('chatReady', function() {
2140
- var time1 = new Date().getTime();
2141
- chatAgent1.sendTextMessage({
2142
- threadId: P2P_THREAD,
2143
- textMessage: faker.lorem.paragraph()
2144
- }, {
2145
- onSent: function(result) {
2146
- },
2147
- onDeliver: function(result) {
2148
- },
2149
- onSeen: function(result) {
2150
- if (timingLog) {
2151
- console.log('\x1b[90m ☰ Send a Message to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
2152
- }
2153
- var time2 = new Date().getTime();
2154
- chatAgent2.replyMessage({
2155
- threadId: P2P_THREAD,
2156
- repliedTo: sentMessageID,
2157
- content: faker.lorem.paragraph()
2158
- }, {
2159
- onSent: function(result) {
2160
- },
2161
- onDeliver: function(result) {
2162
-
2163
- if (timingLog) {
2164
- console.log('\x1b[33m ★ Reply Message Delivered \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
2165
- }
2166
- done();
2167
- console.log('\n');
2168
- },
2169
- onSeen: function(result) {
2170
- }
2171
- });
2172
- }
2173
- });
2174
- });
2175
-
2176
- chatAgent2.on('chatReady', function() {
2177
- chatAgent2.on('messageEvents', function(event) {
2178
- var type = event.type,
2179
- message = event.result.message;
2180
-
2181
- if (type == 'MESSAGE_NEW') {
2182
- sentMessageID = message.id;
2183
- chatAgent2.seen({
2184
- messageId: message.id,
2185
- ownerId: message.ownerId
2186
- });
2187
- }
2188
- });
2189
- });
2190
-
2191
- });
2192
-
2193
- it('Should send a messages to a P2P thread then FORWARD it into another thread', function(done) {
2194
- var sentMessageID,
2195
- sentMessageUID;
2196
-
2197
- chatAgent1.on('chatReady', function() {
2198
- var time1 = new Date().getTime();
2199
- chatAgent1.sendTextMessage({
2200
- threadId: P2P_THREAD,
2201
- textMessage: faker.lorem.paragraph()
2202
- }, {
2203
- onSent: function(result) {
2204
- },
2205
- onDeliver: function(result) {
2206
- },
2207
- onSeen: function(result) {
2208
- if (timingLog) {
2209
- console.log('\x1b[90m ☰ Send a Message to P2P Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1);
2210
- }
2211
- var time2 = new Date().getTime();
2212
- chatAgent2.forwardMessage({
2213
- subjectId: GROUP_THREAD,
2214
- content: JSON.stringify([sentMessageID])
2215
- }, {
2216
- onSent: function(result) {
2217
- },
2218
- onDeliver: function(result) {
2219
- if (timingLog) {
2220
- console.log('\x1b[33m ★ Forward Message Delivered \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time2);
2221
- }
2222
- done();
2223
- console.log('\n');
2224
- },
2225
- onSeen: function(result) {
2226
- }
2227
- });
2228
- }
2229
- });
2230
- });
2231
-
2232
- chatAgent2.on('chatReady', function() {
2233
- chatAgent2.on('messageEvents', function(event) {
2234
- var type = event.type,
2235
- message = event.result.message;
2236
-
2237
- if (type == 'MESSAGE_NEW') {
2238
- sentMessageID = message.id;
2239
- chatAgent2.seen({
2240
- messageId: message.id,
2241
- ownerId: message.ownerId
2242
- });
2243
- }
2244
- });
2245
- });
2246
-
2247
- });
2248
-
2249
- });
2250
-
2251
- /**
2252
- * FILE FUNCTIONS
2253
- */
2254
- describe('Uploading & Getting File Functionality', function(done) {
2255
- this.timeout(20000);
2256
-
2257
- var chatAgent1,
2258
- imageId,
2259
- imageHashCode,
2260
- fileId,
2261
- fileHashCode;
2262
-
2263
- beforeEach(() => {
2264
- chatAgent1 = new Chat(params1);
2265
- });
2266
-
2267
- afterEach(() => {
2268
- chatAgent1.logout();
2269
- });
2270
-
2271
- it('Should UPLOAD an image file to image server', function(done) {
2272
- chatAgent1.on('chatReady', function() {
2273
- var time = new Date().getTime();
2274
- chatAgent1.uploadImage({
2275
- image: __dirname + '/test.jpg',
2276
- xC: 0,
2277
- yC: 0,
2278
- hC: 400,
2279
- wC: 400
2280
- }, function(result) {
2281
- if (!result.hasError) {
2282
- imageId = result.result.id;
2283
- imageHashCode = result.result.hashCode;
2284
- if (timingLog) {
2285
- console.log('\x1b[33m ★ Image Upload \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2286
- }
2287
- done();
2288
- console.log('\n');
2289
- } else {
2290
- done(JSON.stringify(result));
2291
- }
2292
- });
2293
- });
2294
- });
2295
-
2296
- it('Should Emit an Event that image upload has been started', function(done) {
2297
- var time = new Date().getTime();
2298
-
2299
- chatAgent1.on('chatReady', function() {
2300
- chatAgent1.uploadImage({
2301
- image: __dirname + '/test.jpg',
2302
- xC: 0,
2303
- yC: 0,
2304
- hC: 400,
2305
- wC: 400
2306
- }, function(result) {
2307
- console.log(result);
2308
- if (!result.hasError) {
2309
- imageId = result.result.id;
2310
- imageHashCode = result.result.hashCode;
2311
- }
2312
- });
2313
- });
2314
-
2315
- chatAgent1.on('fileUploadEvents', function(event) {
2316
- if (event.state == 'UPLOADED') {
2317
- if (timingLog) {
2318
- console.log('\x1b[33m ★ Image Upload Event Triggered \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2319
- }
2320
- done();
2321
- console.log('\n');
2322
- }
2323
- });
2324
- });
2325
-
2326
- it('Should UPLOAD a file to file server', function(done) {
2327
- chatAgent1.on('chatReady', function() {
2328
- var time = new Date().getTime();
2329
- chatAgent1.uploadFile({
2330
- file: __dirname + '/test.txt'
2331
- }, function(result) {
2332
- if (!result.hasError) {
2333
- fileId = result.result.id;
2334
- fileHashCode = result.result.hashCode;
2335
- if (timingLog) {
2336
- console.log('\x1b[33m ★ File Upload \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2337
- }
2338
- done();
2339
- console.log('\n');
2340
- }
2341
- });
2342
- });
2343
- });
2344
-
2345
- it('Should Emit an Event that file upload has been started', function(done) {
2346
- var time = new Date().getTime();
2347
-
2348
- chatAgent1.on('chatReady', function() {
2349
- chatAgent1.uploadFile({
2350
- file: __dirname + '/test.txt'
2351
- }, function(result) {
2352
- if (!result.hasError) {
2353
- fileId = result.result.id;
2354
- fileHashCode = result.result.hashCode;
2355
- }
2356
- });
2357
- });
2358
-
2359
- chatAgent1.on('fileUploadEvents', function(event) {
2360
- if (event.state == 'UPLOADED') {
2361
- if (timingLog) {
2362
- console.log('\x1b[33m ★ File Upload Event Triggered \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2363
- }
2364
- done();
2365
- console.log('\n');
2366
- }
2367
- });
2368
- });
2369
-
2370
- it('Should GET an uploaded image from image server', function(done) {
2371
- chatAgent1.on('chatReady', function() {
2372
- var time = new Date().getTime();
2373
- chatAgent1.getImage({
2374
- imageId: imageId,
2375
- hashCode: imageHashCode
2376
- }, function(result) {
2377
- if (!result.hasError) {
2378
- if (timingLog) {
2379
- console.log('\x1b[33m ★ Get Image \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2380
- }
2381
- done();
2382
- console.log('\n');
2383
- } else {
2384
- done(JSON.stringify(result));
2385
- }
2386
- });
2387
- });
2388
- });
2389
-
2390
- it('Should GET an uploaded file from file server', function(done) {
2391
- chatAgent1.on('chatReady', function() {
2392
- var time = new Date().getTime();
2393
- chatAgent1.getFile({
2394
- fileId: fileId,
2395
- hashCode: fileHashCode
2396
- }, function(result) {
2397
- if (!result.hasError) {
2398
- if (timingLog) {
2399
- console.log('\x1b[33m ★ Get File \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time);
2400
- }
2401
- done();
2402
- console.log('\n');
2403
- } else {
2404
- done(JSON.stringify(result));
2405
- }
2406
- });
2407
- });
2408
- });
2409
-
2410
- });