UNPKG

podchat

Version:
1,100 lines (1,000 loc) 103 kB
var assert = require('assert'), faker = require('faker'), Chat = require('../src/chat.js'), fs = require('fs'), path = require('path'); var TOKENS = { TOKEN_1: '573f0bc94cc14e258f835d40c8b40137', // Masoud TOKEN_2: 'dd84f092ba4c4ee3942439a8d2304a26' // PodDraw }, P2P_THREAD = 4441, //6848, GROUP_THREAD = 7064, //6868, timingLog = true, params1 = { /** * Main Server */ // socketAddress: 'wss://msg.pod.ir/ws', // {**REQUIRED**} Socket Address // ssoHost: 'https://accounts.pod.ir', // {**REQUIRED**} Socket Address // platformHost: 'https://api.pod.ir/srv/core', // {**REQUIRED**} Platform Core Address // fileServer: 'https://core.pod.ir', // {**REQUIRED**} File Server Address // podSpaceFileServer: 'https://podspace.pod.ir', // {**REQUIRED**} File Server Address // serverName: 'chat-server', // {**REQUIRED**} Server to to register on /** * Sand Box */ socketAddress: "wss://chat-sandbox.pod.ir/ws", // {**REQUIRED**} Socket Address ssoHost: "https://accounts.pod.ir", // {**REQUIRED**} Socket Address platformHost: "https://sandbox.pod.ir:8043/srv/basic-platform", // {**REQUIRED**} Platform Core Address fileServer: 'https://core.pod.ir', // {**REQUIRED**} File Server Address podSpaceFileServer: 'http://172.16.110.61:8780/podspace', // {**REQUIRED**} File Server Address serverName: "chat-server", // {**REQUIRED**} Server to to register on /** * Integration */ // socketAddress: "ws://172.16.110.235:8003/ws", // ssoHost: "http://172.16.110.76", // platformHost: "http://172.16.110.235:8003/srv/bptest-core", // fileServer: 'https://core.pod.ir', // podSpaceFileServer: 'http://172.16.110.61:8780/podspace', // {**REQUIRED**} File Server Address // serverName: "chatlocal", enableCache: false, token: TOKENS.TOKEN_1, asyncLogging: { // onFunction: true, // onMessageReceive: true, // onMessageSend: true, actualTiming: timingLog } }, params2 = Object.assign({}, params1); params2.token = TOKENS.TOKEN_2; /** * CONNECTING AND GETTING READY */ describe('Connecting and getting ready', function(done) { this.timeout(20000); var chatAgent; beforeEach(() => { chatAgent = new Chat(params1); }); afterEach(() => { chatAgent.logout(); }); it('Should connect to server and get ready', function(done) { chatAgent.on('chatReady', function() { done(); }); }); }); /** * GETTING CURRENT USER'S INFO */ describe('Working with Users', function(done) { this.timeout(20000); var chatAgent; beforeEach(() => { chatAgent = new Chat(params1); }); afterEach(() => { chatAgent.logout(); }); it('Should get User Info', function(done) { chatAgent.on('chatReady', function() { var currentUser = chatAgent.getCurrentUser(); if (currentUser && typeof currentUser.id === 'number') { done(); } else { done('Can not get user info!'); } }); }); }); /** * CONTACTS FUNCTIONALITY */ describe('Working with contacts', function(done) { this.timeout(20000); var chatAgent, newContactId, blockedContactId; beforeEach(() => { chatAgent = new Chat(params1); }); afterEach(() => { chatAgent.logout(); }); it('Should GET contacts list', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(contactsResult)); } }); }); }); it('Should ADD a new contact', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.addContacts({ firstName: faker.name.firstName(), lastName: faker.name.lastName(), cellphoneNumber: '09' + Math.floor((Math.random() * 100000000) + 1), email: faker.internet.email() }, function(result) { if (!result.hasError) { newContactId = result.result.contacts[0].id; if (timingLog) { console.log('\x1b[33m ★ Add New Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(new Error(JSON.stringify(result))); } }); }); }); it('Should UPDATE an existing contact', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.updateContacts({ id: newContactId, firstName: faker.name.firstName(), lastName: faker.name.lastName(), cellphoneNumber: '09' + Math.floor((Math.random() * 100000000) + 1), email: faker.internet.email() }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[33m ★ Update a Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }); }); it('Should REMOVE an existing contact', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.removeContacts({ id: newContactId }, function(result) { if (!result.hasError && result.result) { if (timingLog) { console.log('\x1b[33m ★ Remove a Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }); }); it('Should Block a contact', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { var blockContactId = contactsResult.result.contacts[i].id; if (timingLog) { console.log('\x1b[90m ☰ Select a Valid Contact \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.block({ contactId: blockContactId }, function(result) { if (!result.hasError) { blockedContactId = result.result.blockId; if (timingLog) { console.log('\x1b[33m ★ Block Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); break; } } } }); }); }); it('Should unBlock an already blocked contact', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.unblock({ blockId: blockedContactId }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[33m ★ UnBlock Contact \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }); }); it('Should Get Blocked contacts list', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getBlockedList({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Blocked Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1); } done(); console.log('\n'); } else { done(JSON.stringify(contactsresult)); } }); }); }); it('Should Search in contacts list', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.searchContacts({ cellphoneNumber: 09 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Search in Contacts List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time1); } done(); console.log('\n'); } else { done(JSON.stringify(contactsResult)); } }); }); }); }); /** * THREADS FUNCTIONALITY */ describe('Working with threads', function(done) { this.timeout(20000); var chatAgent, p2pThreadId, groupThreadId, muteThreadId; beforeEach(() => { chatAgent = new Chat(params1); }); afterEach(() => { chatAgent.logout(); }); it('Should GET Threads list', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getThreads({ count: 50, offset: 0 }, function(threadsResult) { if (!threadsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Threads List \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(threadsResult)); } }); }); }); it('Should GET a single thread', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getThreads({ threadIds: [P2P_THREAD] }, function(threadsResult) { if (!threadsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Single Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(threadsResult)); } }); }); }); it('Should SEARCH in Thread names and return result', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getThreads({ count: 50, name: 'thread' }, function(threadsResult) { if (!threadsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Search in Threads \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(threadsResult)); } }); }); }); it('Should CREATE a P2P thread with a contact (TYPE = NORMAL)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { var p2pContactId = contactsResult.result.contacts[i].id; if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ type: 'NORMAL', invitees: [ { id: p2pContactId, idType: 'TO_BE_USER_CONTACT_ID' }] }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { p2pThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create P2P Thread \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); break; } } } }); }); }); it('Should CREATE a Group thread with a contact (TYPE = NORMAL)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'NORMAL', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create Group Thread (type = NORMAL) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); } }); }); }); it('Should CREATE a Group thread with a contact (TYPE = OWNER_GROUP)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'OWNER_GROUP', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create Group Thread (type = OWNER_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); } }); }); }); it('Should CREATE a Group thread with a contact (TYPE = PUBLIC_GROUP)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), uniqueName: faker.lorem.word() + faker.lorem.word(), type: 'PUBLIC_GROUP', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create Group Thread (type = PUBLIC_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); } }); }); }); it('Should CREATE a Group thread with a contact (TYPE = CHANNEL_GROUP)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'CHANNEL_GROUP', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create Group Thread (type = CHANNEL_GROUP) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); } }); }); }); it('Should CREATE a Group thread with a contact (TYPE = CHANNEL)', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'CHANNEL', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[33m ★ Create Group Thread (type = CHANNEL) \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time3); } done(); console.log('\n'); } else { done(JSON.stringify(createThreadResult)); } }); } }); }); }); it('Should GET Thread participants', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getThreadParticipants({ count: 50, offset: 0, threadId: GROUP_THREAD }, function(participantsResult) { if (!participantsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Participants \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(participantsResult)); } }); }); }); it('Should GET Thread Admins', function(done) { chatAgent.on('chatReady', function() { var time = new Date().getTime(); chatAgent.getThreadAdmins({ threadId: GROUP_THREAD }, function(participantsResult) { if (!participantsResult.hasError) { if (timingLog) { console.log('\x1b[33m ★ Get Participants \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time); } done(); console.log('\n'); } else { done(JSON.stringify(participantsResult)); } }); }); }); it('Should CREATE a Group thread and set a new admin on it', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'NORMAL', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[90m ☰ Create Group Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3); } var time4 = new Date().getTime(); chatAgent.getThreadParticipants({ threadId: groupThreadId }, function(participantsResult) { if (!participantsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4); } var time5 = new Date().getTime(); var threadParticipants = participantsResult.result.participants; var adminCandidate; for (var i = 0; i < threadParticipants.length; i++) { if (!threadParticipants[i].admin) { adminCandidate = threadParticipants[i].id; break; } } if (adminCandidate > 0) { setTimeout(function() { chatAgent.setAdmin({ threadId: groupThreadId, admins: [ { userId: adminCandidate, roleOperation: 'add', roles: [ 'post_channel_message', 'edit_message_of_others', 'delete_message_of_others', 'add_new_user', 'remove_user', 'thread_admin', 'add_rule_to_user', 'remove_role_from_user', 'read_thread', 'edit_thread' ] }] }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[33m ★ Set Admin \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time5 - 500); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }, 500); } } }); } }); } }); }); }); it('Should CREATE a Group thread, set a new admin on it and remove it afterwards', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'NORMAL', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { groupThreadId = createThreadResult.result.thread.id; if (timingLog) { console.log('\x1b[90m ☰ Create Group Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3); } var time4 = new Date().getTime(); chatAgent.getThreadParticipants({ threadId: groupThreadId }, function(participantsResult) { if (!participantsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4); } var time5 = new Date().getTime(); var threadParticipants = participantsResult.result.participants; var adminCandidate; for (var i = 0; i < threadParticipants.length; i++) { if (!threadParticipants[i].admin) { adminCandidate = threadParticipants[i].id; break; } } if (adminCandidate > 0) { setTimeout(function() { chatAgent.setAdmin({ threadId: groupThreadId, admins: [ { userId: adminCandidate, roleOperation: 'add', roles: [ 'post_channel_message', 'edit_message_of_others', 'delete_message_of_others', 'add_new_user', 'remove_user', 'thread_admin', 'add_rule_to_user', 'remove_role_from_user', 'read_thread', 'edit_thread' ] }] }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Set Admin \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time5 - 500); } var time6 = new Date().getTime(); setTimeout(function() { chatAgent.setAdmin({ threadId: groupThreadId, admins: [ { userId: adminCandidate, roleOperation: 'remove', roles: [ 'post_channel_message', 'edit_message_of_others', 'delete_message_of_others', 'add_new_user', 'remove_user', 'thread_admin', 'add_rule_to_user', 'remove_role_from_user', 'read_thread', 'edit_thread' ] }] }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[33m ★ Remove Admin \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time6 - 500); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }, 500); } }); }, 500); } } }); } }); } }); }); }); it('Should ADD A PARTICIPANT to newly created group Thread', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } var lastInvitee = groupInvitees.pop(); if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'NORMAL', invitees: groupInvitees }, function(createThreadResult) { if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) { if (timingLog) { console.log('\x1b[90m ☰ Create New Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3); } var time4 = new Date().getTime(); var newGroupThreadId = createThreadResult.result.thread.id; setTimeout(function() { chatAgent.addParticipants({ threadId: newGroupThreadId, contactIds: [lastInvitee.id] }, function(result) { if (!result.hasError) { if (timingLog) { console.log('\x1b[33m ★ Add Participant \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time4 - 500); } done(); console.log('\n'); } else { done(JSON.stringify(result)); } }); }, 500); } }); } }); }); }); it('Should REMOVE A PARTICIPANT from newly created group Thread', function(done) { chatAgent.on('chatReady', function() { var time1 = new Date().getTime(); chatAgent.getContacts({ count: 50, offset: 0 }, function(contactsResult) { if (!contactsResult.hasError) { if (timingLog) { console.log('\x1b[90m ☰ Get Contacts List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time1); } var time2 = new Date().getTime(); var groupInvitees = []; for (var i = 0; i < contactsResult.result.contacts.length; i++) { if (contactsResult.result.contacts[i].hasUser) { groupInvitees.push({ id: contactsResult.result.contacts[i].id, idType: 'TO_BE_USER_CONTACT_ID' }); if (groupInvitees.length > 2) { break; } } } if (timingLog) { console.log('\x1b[90m ☰ Create Invitees List \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time2); } var time3 = new Date().getTime(); chatAgent.createThread({ title: faker.lorem.word(), type: 'NORMAL', invitees: groupInvitees }, function(createThreadResult) {