podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
1,098 lines (990 loc) • 98.9 kB
JavaScript
var assert = require('assert'),
faker = require('faker'),
Chat = require('../src/chat.js'),
fs = require('fs'),
path = require('path');
var TOKENS = {
TOKEN_1: '7cba09ff83554fc98726430c30afcfc6', // Masoud
TOKEN_2: 'fbd4ecedb898426394646e65c6b1d5d1' // Pooria
},
P2P_THREAD = 293,
GROUP_THREAD = 10349,//312,
timingLog = true,
params1 = {
/**
* Hamed Mehrara
*/
// socketAddress: 'ws://172.16.106.26:8003/ws', // {**REQUIRED**} Socket Address
// ssoHost: 'http://172.16.110.76', // {**REQUIRED**} Socket Address
// platformHost: 'http://172.16.106.26:8080/hamsam', // {**REQUIRED**} Platform Core Address
// fileServer: 'http://172.16.106.26:8080/hamsam', // {**REQUIRED**} File Server Address
// serverName: 'chat-server', // {**REQUIRED**} Server to to register on
/**
* Mehdi Sheikh Hosseini
*/
socketAddress: 'ws://172.16.110.131:8003/ws', // {**REQUIRED**} Socket Address
ssoHost: 'http://172.16.110.76', // {**REQUIRED**} Socket Address
platformHost: 'http://172.16.110.131:8080', // {**REQUIRED**} Platform Core Address
fileServer: 'http://172.16.110.131:8080', // {**REQUIRED**} File Server Address
serverName: 'chat-server2', // {**REQUIRED**} Server to to register on
/**
* Sand Box
*/
// socketAddress: "wss://chat-sandbox.pod.land/ws", // {**REQUIRED**} Socket Address
// ssoHost: "https://accounts.pod.land", // {**REQUIRED**} Socket Address
// platformHost: "https://sandbox.pod.land:8043/srv/basic-platform", // {**REQUIRED**} Platform Core Address
// fileServer: "https://sandbox.pod.land:8443", // {**REQUIRED**} File Server Address
// serverName: "chat-server", // {**REQUIRED**} Server to to register on
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();
}
});
});
});
/**
* 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');
}
});
});
});
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');
}
});
});
});
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');
}
});
});
});
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');
}
});
});
});
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');
}
});
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');
}
});
});
});
it('Should Get Blocked contacts list', function(done) {
chatAgent.on('chatReady', function() {
var time1 = new Date().getTime();
chatAgent.getBlocked({
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');
}
});
});
});
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');
}
});
});
});
});
/**
* 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');
}
});
});
});
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');
}
});
});
});
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');
}
});
});
});
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');
}
});
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');
}
});
}
});
});
});
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');
}
});
}
});
});
});
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(),
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');
}
});
}
});
});
});
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');
}
});
}
});
});
});
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');
}
});
}
});
});
});
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');
}
});
});
});
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');
}
});
});
});
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');
}
});
}, 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');
}
});
}, 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,
contacts: [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');
}
});
}, 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) {
if (!createThreadResult.hasError && createThreadResult.result.thread.id > 0) {
if (timingLog) {
console.log('\x1b[90m ☰ Create Thread \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time3);
}
var time4 = new Date().getTime();
var newGroupThreadId = createThreadResult.result.thread.id;
chatAgent.getThreadParticipants({
count: 50,
offset: 0,
threadId: newGroupThreadId
}, function(participantsResult) {
if (!participantsResult.hasError) {
if (timingLog) {
console.log('\x1b[90m ☰ Get Thread Participants \x1b[0m \x1b[90m(%sms)\x1b[0m', new Date().getTime() - time4);
}
var time5 = new Date().getTime();
setTimeout(function() {
var userId = participantsResult.result.participants[0];
chatAgent.removeParticipants({
threadId: newGroupThreadId,
participants: [userId.id]
}, function(result) {
if (!result.hasError) {
if (timingLog) {
console.log('\x1b[33m ★ Remove Participant \x1b[0m \x1b[33m(%sms)\x1b[0m', new Date().getTime() - time5 -
500);
}
done();
console.log('\n');
}
});
}, 500);
}
});
}
});
}
});
});
});