podchat 11.3.0 → 11.4.0

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.
Files changed (3) hide show
  1. package/changelog.md +13 -0
  2. package/package.json +1 -1
  3. package/src/chat.js +2262 -1071
package/src/chat.js CHANGED
@@ -162,9 +162,34 @@
162
162
  STOP_BOT: 65,
163
163
  BOT_COMMANDS: 68,
164
164
  THREAD_ALL_BOTS: 69,
165
+
166
+ CALL_REQUEST: 70,
167
+ ACCEPT_CALL: 71,
168
+ REJECT_CALL: 72,
169
+ RECEIVE_CALL_REQUEST: 73,
170
+ START_CALL: 74,
171
+ END_CALL_REQUEST: 75,
172
+ END_CALL: 76,
173
+ GET_CALLS: 77,
174
+
165
175
  CONTACT_SYNCED: 90,
176
+
177
+ GROUP_CALL_REQUEST: 91,
178
+ LEAVE_CALL: 92,
179
+ ADD_CALL_PARTICIPANT: 93,
180
+ CALL_PARTICIPANT_JOINED: 94,
181
+ REMOVE_CALL_PARTICIPANT: 95,
182
+ TERMINATE_CALL: 96,
183
+ MUTE_CALL_PARTICIPANT: 97,
184
+ UNMUTE_CALL_PARTICIPANT: 98,
185
+
166
186
  LOGOUT: 100,
167
187
 
188
+ ACTIVE_CALL_PARTICIPANTS: 110,
189
+ CALL_SESSION_CREATED: 111,
190
+ TURN_ON_VIDEO_CALL: 113,
191
+ TURN_OFF_VIDEO_CALL: 114,
192
+
168
193
  RECORD_CALL: 121,
169
194
  END_RECORD_CALL: 122,
170
195
  DESTINATED_RECORD_CALL: 126,
@@ -336,7 +361,37 @@
336
361
  chatWaitQueue = [],
337
362
  chatUploadQueue = [],
338
363
  chatSendQueueHandlerTimeout,
339
- fullResponseObject = params.fullResponseObject || false;
364
+ fullResponseObject = params.fullResponseObject || false,
365
+
366
+ consoleLogging = (params.asyncLogging.consoleLogging && typeof params.asyncLogging.consoleLogging === 'boolean')
367
+ ? params.asyncLogging.consoleLogging
368
+ : false,
369
+
370
+ /** call variables */
371
+ callTypes = {
372
+ 'VOICE': 0x0,
373
+ 'VIDEO': 0x1
374
+ },
375
+ currentCallParams = {},
376
+ currentCallId = null,
377
+ callClientType = {
378
+ WEB: 1,
379
+ ANDROID: 2,
380
+ DESKTOP: 3,
381
+ NODE: 4
382
+ },
383
+ callRequestController = {
384
+ callRequestReceived: false,
385
+ callEstablishedInMySide: false,
386
+ iCanAcceptTheCall: function () {
387
+ return callRequestController.callRequestReceived && callRequestController.callEstablishedInMySide;
388
+ }
389
+ },
390
+ callStopQueue = {
391
+ callStarted: false,
392
+ },
393
+ callRequestTimeout = (typeof params.callRequestTimeout === 'number' && params.callRequestTimeout >= 0) ? params.callRequestTimeout : 10000;
394
+
340
395
 
341
396
  /*******************************************************
342
397
  * P R I V A T E M E T H O D S *
@@ -3120,221 +3175,561 @@
3120
3175
  break;
3121
3176
 
3122
3177
  /**
3123
- * Type 90 Contacts Synced
3178
+ * Type 70 Send Call Request
3124
3179
  */
3125
- case chatMessageVOTypes.CONTACT_SYNCED:
3126
- fireEvent('contactEvents', {
3127
- type: 'CONTACTS_SYNCED',
3180
+ case chatMessageVOTypes.CALL_REQUEST:
3181
+ callRequestController.callRequestReceived = true;
3182
+ callReceived({
3183
+ callId: messageContent.callId
3184
+ }, function (r) {
3185
+
3186
+ });
3187
+
3188
+ if (messagesCallbacks[uniqueId]) {
3189
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3190
+ }
3191
+
3192
+ fireEvent('callEvents', {
3193
+ type: 'RECEIVE_CALL',
3128
3194
  result: messageContent
3129
3195
  });
3196
+
3197
+ currentCallId = messageContent.callId;
3198
+
3130
3199
  break;
3131
3200
 
3132
3201
  /**
3133
- * Type 121 Record Call Request
3202
+ * Type 71 Accept Call Request
3134
3203
  */
3135
- case chatMessageVOTypes.RECORD_CALL:
3204
+ case chatMessageVOTypes.ACCEPT_CALL:
3136
3205
  if (messagesCallbacks[uniqueId]) {
3137
3206
  messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3138
3207
  }
3139
3208
 
3140
3209
  fireEvent('callEvents', {
3141
- type: 'START_RECORDING_CALL',
3210
+ type: 'ACCEPT_CALL',
3142
3211
  result: messageContent
3143
3212
  });
3144
3213
 
3145
3214
  break;
3146
3215
 
3147
3216
  /**
3148
- * Type 122 End Record Call Request
3217
+ * Type 72 Reject Call Request
3149
3218
  */
3150
- case chatMessageVOTypes.END_RECORD_CALL:
3219
+ case chatMessageVOTypes.REJECT_CALL:
3151
3220
  if (messagesCallbacks[uniqueId]) {
3152
3221
  messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3153
3222
  }
3154
3223
 
3155
3224
  fireEvent('callEvents', {
3156
- type: 'STOP_RECORDING_CALL',
3225
+ type: 'REJECT_CALL',
3157
3226
  result: messageContent
3158
3227
  });
3159
3228
 
3160
3229
  break;
3161
3230
 
3162
3231
  /**
3163
- * Type 126 Destined Record Call Request
3232
+ * Type 73 Receive Call Request
3164
3233
  */
3165
- case chatMessageVOTypes.DESTINATED_RECORD_CALL:
3234
+ case chatMessageVOTypes.RECEIVE_CALL_REQUEST:
3235
+ if (messagesCallbacks[uniqueId]) {
3236
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3237
+ }
3238
+
3239
+ if (messageContent.callId > 0) {
3240
+ fireEvent('callEvents', {
3241
+ type: 'RECEIVE_CALL',
3242
+ result: messageContent
3243
+ });
3244
+ } else {
3245
+ fireEvent('callEvents', {
3246
+ type: 'PARTNER_RECEIVED_YOUR_CALL',
3247
+ result: messageContent
3248
+ });
3249
+ }
3250
+
3251
+ currentCallId = messageContent.callId;
3252
+
3253
+ break;
3254
+
3255
+ /**
3256
+ * Type 74 Start Call Request
3257
+ */
3258
+ case chatMessageVOTypes.START_CALL:
3259
+ if(!callRequestController.iCanAcceptTheCall()) {
3260
+ //Do not handle the call if it is already started elsewhere for the same user
3261
+ fireEvent('callEvents', {
3262
+ type: 'CALL_STARTED_ELSEWHERE',
3263
+ message: 'Call already started somewhere else..., aborting...'
3264
+ });
3265
+ return;
3266
+ }
3267
+
3166
3268
  if (messagesCallbacks[uniqueId]) {
3167
3269
  messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3168
3270
  }
3169
3271
 
3170
3272
  fireEvent('callEvents', {
3171
- type: 'START_RECORDING_CALL',
3273
+ type: 'CALL_STARTED',
3172
3274
  result: messageContent
3173
3275
  });
3174
3276
 
3277
+ if (typeof messageContent === 'object'
3278
+ && messageContent.hasOwnProperty('chatDataDto')
3279
+ && !!messageContent.chatDataDto.kurentoAddress) {
3280
+ } else {
3281
+ fireEvent('callEvents', {
3282
+ type: 'CALL_ERROR',
3283
+ message: 'Chat Data DTO is not present!'
3284
+ });
3285
+ }
3286
+
3175
3287
  break;
3176
3288
 
3177
- /**
3178
- * Type 127 Assistant call started
3179
- *
3180
- * Tells the assistant that a call started
3289
+ /**
3290
+ * Type 75 End Call Request
3181
3291
  */
3182
- case chatMessageVOTypes.ASSISTANT_CALL_STARTED:
3183
- /*if (messagesCallbacks[uniqueId]) {
3292
+ case chatMessageVOTypes.END_CALL_REQUEST:
3293
+ if (messagesCallbacks[uniqueId]) {
3184
3294
  messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3185
- }*/
3295
+ }
3186
3296
 
3187
3297
  fireEvent('callEvents', {
3188
- type: 'ASSISTANT_CALL_STARTED',
3189
- result: {
3190
- callId: threadId
3191
- }
3298
+ type: 'END_CALL',
3299
+ result: messageContent
3192
3300
  });
3193
3301
 
3302
+ callStop();
3303
+
3194
3304
  break;
3195
3305
 
3196
- /**
3197
- * Type 128 Assistant call ended
3198
- *
3199
- * Tells the assistant that a call ended
3306
+ /**
3307
+ * Type 76 Call Ended
3200
3308
  */
3201
- case chatMessageVOTypes.ASSISTANT_CALL_ENDED:
3202
- /* if (messagesCallbacks[uniqueId]) {
3309
+ case chatMessageVOTypes.END_CALL:
3310
+ if (messagesCallbacks[uniqueId]) {
3203
3311
  messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3204
- }*/
3312
+ }
3205
3313
 
3206
- fireEvent('callEvents', {
3207
- type: 'ASSISTANT_CALL_ENDED'
3314
+ fireEvent('callEvents', {
3315
+ type: 'CALL_ENDED',
3316
+ result: messageContent
3208
3317
  });
3209
3318
 
3319
+ callStop();
3320
+
3210
3321
  break;
3211
3322
 
3212
3323
  /**
3213
- * Type 999 All unknown errors
3324
+ * Type 77 Get Calls History
3214
3325
  */
3215
- case chatMessageVOTypes.ERROR:
3326
+ case chatMessageVOTypes.GET_CALLS:
3216
3327
  if (messagesCallbacks[uniqueId]) {
3217
- messagesCallbacks[uniqueId](Utility.createReturnData(true, messageContent.message, messageContent.code, messageContent, 0));
3328
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3218
3329
  }
3219
3330
 
3220
- /**
3221
- * If error code is 21, Token is invalid &
3222
- * user should logged out
3223
- */
3224
- if (messageContent.code == 21) {
3225
- // TODO: Temporarily removed due to unknown side-effects
3226
- // chatState = false;
3227
- // asyncClient.logout();
3228
- // clearChatServerCaches();
3229
- }
3331
+ break;
3230
3332
 
3231
- fireEvent('error', {
3232
- code: messageContent.code,
3233
- message: messageContent.message,
3234
- error: messageContent
3333
+
3334
+ /**
3335
+ * Type 90 Contacts Synced
3336
+ */
3337
+ case chatMessageVOTypes.CONTACT_SYNCED:
3338
+ fireEvent('contactEvents', {
3339
+ type: 'CONTACTS_SYNCED',
3340
+ result: messageContent
3235
3341
  });
3236
3342
  break;
3237
- }
3238
- },
3239
3343
 
3240
- /**
3241
- * Send Message Callbacks Handler
3242
- *
3243
- * When you send Delivery or Seen Acknowledgements of a message
3244
- * You should send Delivery and Seen for all the Messages before
3245
- * that message so that you wont have un delivered/unseen messages
3246
- * after seeing the last message of a thread
3247
- *
3248
- * @access private
3249
- *
3250
- * @param {int} actionType Switch between Delivery or Seen
3251
- * @param {long} threadId Id of thread
3252
- * @param {string} uniqueId uniqueId of message
3253
- *
3254
- * @return {undefined}
3255
- */
3256
- sendMessageCallbacksHandler = function (actionType, threadId, uniqueId) {
3257
- switch (actionType) {
3344
+ /**
3345
+ * Type 91 Send Group Call Request
3346
+ */
3347
+ case chatMessageVOTypes.GROUP_CALL_REQUEST:
3348
+ callRequestController.callRequestReceived = true;
3349
+ callReceived({
3350
+ callId: messageContent.callId
3351
+ }, function (r) {});
3258
3352
 
3259
- case chatMessageVOTypes.DELIVERY:
3260
- if (threadCallbacks[threadId]) {
3261
- var lastThreadCallbackIndex = Object.keys(threadCallbacks[threadId])
3262
- .indexOf(uniqueId);
3263
- if (lastThreadCallbackIndex !== undefined) {
3264
- while (lastThreadCallbackIndex > -1) {
3265
- var tempUniqueId = Object.entries(threadCallbacks[threadId])[lastThreadCallbackIndex][0];
3266
- if (sendMessageCallbacks[tempUniqueId] && sendMessageCallbacks[tempUniqueId].onDeliver) {
3267
- if (threadCallbacks[threadId][tempUniqueId] && threadCallbacks[threadId][tempUniqueId].onSent) {
3268
- sendMessageCallbacks[tempUniqueId].onDeliver(
3269
- {
3270
- uniqueId: tempUniqueId
3271
- });
3272
- delete (sendMessageCallbacks[tempUniqueId].onDeliver);
3273
- threadCallbacks[threadId][tempUniqueId].onDeliver = true;
3274
- }
3275
- }
3353
+ if (messagesCallbacks[uniqueId]) {
3354
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3355
+ }
3276
3356
 
3277
- lastThreadCallbackIndex -= 1;
3278
- }
3279
- }
3357
+ fireEvent('callEvents', {
3358
+ type: 'RECEIVE_CALL',
3359
+ result: messageContent
3360
+ });
3361
+
3362
+ currentCallId = messageContent.callId;
3363
+
3364
+ break;
3365
+
3366
+ /**
3367
+ * Type 92 Call Partner Leave
3368
+ */
3369
+ case chatMessageVOTypes.LEAVE_CALL:
3370
+ if (messagesCallbacks[uniqueId]) {
3371
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3280
3372
  }
3373
+
3374
+ fireEvent('callEvents', {
3375
+ type: 'CALL_PARTICIPANT_LEFT',
3376
+ result: messageContent
3377
+ });
3378
+
3281
3379
  break;
3282
3380
 
3283
- case chatMessageVOTypes.SEEN:
3284
- if (threadCallbacks[threadId]) {
3285
- var lastThreadCallbackIndex = Object.keys(threadCallbacks[threadId])
3286
- .indexOf(uniqueId);
3287
- if (lastThreadCallbackIndex !== undefined) {
3288
- while (lastThreadCallbackIndex > -1) {
3289
- var tempUniqueId = Object.entries(threadCallbacks[threadId])[lastThreadCallbackIndex][0];
3381
+ /**
3382
+ * Type 93 Add Call Participant
3383
+ */
3384
+ case chatMessageVOTypes.ADD_CALL_PARTICIPANT:
3385
+ if (messagesCallbacks[uniqueId]) {
3386
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3387
+ }
3290
3388
 
3291
- if (sendMessageCallbacks[tempUniqueId] && sendMessageCallbacks[tempUniqueId].onSeen) {
3292
- if (threadCallbacks[threadId][tempUniqueId] && threadCallbacks[threadId][tempUniqueId].onSent) {
3293
- if (!threadCallbacks[threadId][tempUniqueId].onDeliver) {
3294
- sendMessageCallbacks[tempUniqueId].onDeliver(
3295
- {
3296
- uniqueId: tempUniqueId
3297
- });
3298
- delete (sendMessageCallbacks[tempUniqueId].onDeliver);
3299
- threadCallbacks[threadId][tempUniqueId].onDeliver = true;
3300
- }
3389
+ break;
3301
3390
 
3302
- sendMessageCallbacks[tempUniqueId].onSeen(
3303
- {
3304
- uniqueId: tempUniqueId
3305
- });
3391
+ /**
3392
+ * Type 94 Call Participant Joined
3393
+ */
3394
+ case chatMessageVOTypes.CALL_PARTICIPANT_JOINED:
3395
+ if (messagesCallbacks[uniqueId]) {
3396
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3397
+ }
3306
3398
 
3307
- delete (sendMessageCallbacks[tempUniqueId].onSeen);
3308
- threadCallbacks[threadId][tempUniqueId].onSeen = true;
3399
+ fireEvent('callEvents', {
3400
+ type: 'CALL_PARTICIPANT_JOINED',
3401
+ result: messageContent
3402
+ });
3309
3403
 
3310
- if (threadCallbacks[threadId][tempUniqueId].onSent &&
3311
- threadCallbacks[threadId][tempUniqueId].onDeliver &&
3312
- threadCallbacks[threadId][tempUniqueId].onSeen) {
3313
- delete threadCallbacks[threadId][tempUniqueId];
3314
- delete sendMessageCallbacks[tempUniqueId];
3315
- }
3316
- }
3317
- }
3404
+ break;
3318
3405
 
3319
- lastThreadCallbackIndex -= 1;
3320
- }
3321
- }
3406
+ /**
3407
+ * Type 95 Remove Call Participant
3408
+ */
3409
+ case chatMessageVOTypes.REMOVE_CALL_PARTICIPANT:
3410
+ if (messagesCallbacks[uniqueId]) {
3411
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3322
3412
  }
3413
+
3414
+ fireEvent('callEvents', {
3415
+ type: 'CALL_PARTICIPANT_REMOVED',
3416
+ result: messageContent
3417
+ });
3418
+
3323
3419
  break;
3324
3420
 
3325
- default:
3421
+ /**
3422
+ * Type 96 Terminate Call
3423
+ */
3424
+ case chatMessageVOTypes.TERMINATE_CALL:
3425
+ if (messagesCallbacks[uniqueId]) {
3426
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3427
+ }
3428
+
3429
+ fireEvent('callEvents', {
3430
+ type: 'TERMINATE_CALL',
3431
+ result: messageContent
3432
+ });
3433
+
3434
+ callStop();
3435
+
3326
3436
  break;
3327
- }
3328
- },
3329
3437
 
3330
- /**
3331
- * New Message Handler
3332
- *
3333
- * Handles Event Emitter of a newly received Chat Message
3334
- *
3335
- * @access private
3336
- *
3337
- * @param {long} threadId ID of image
3438
+ /**
3439
+ * Type 97 Mute Call Participant
3440
+ */
3441
+ case chatMessageVOTypes.MUTE_CALL_PARTICIPANT:
3442
+ if (messagesCallbacks[uniqueId]) {
3443
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3444
+ }
3445
+
3446
+
3447
+ fireEvent('callEvents', {
3448
+ type: 'CALL_PARTICIPANT_MUTE',
3449
+ result: messageContent
3450
+ });
3451
+
3452
+ break;
3453
+
3454
+ /**
3455
+ * Type 98 UnMute Call Participant
3456
+ */
3457
+ case chatMessageVOTypes.UNMUTE_CALL_PARTICIPANT:
3458
+ if (messagesCallbacks[uniqueId]) {
3459
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3460
+ } else {
3461
+ }
3462
+
3463
+ fireEvent('callEvents', {
3464
+ type: 'CALL_PARTICIPANT_UNMUTE',
3465
+ result: messageContent
3466
+ });
3467
+
3468
+ break;
3469
+
3470
+ /**
3471
+ * Type 110 Active Call Participants List
3472
+ */
3473
+ case chatMessageVOTypes.ACTIVE_CALL_PARTICIPANTS:
3474
+ if (messagesCallbacks[uniqueId]) {
3475
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3476
+ }
3477
+
3478
+ break;
3479
+
3480
+ /**
3481
+ * Type 111 Kafka Call Session Created
3482
+ */
3483
+ case chatMessageVOTypes.CALL_SESSION_CREATED:
3484
+ if(!callRequestController.callEstablishedInMySide)
3485
+ return;
3486
+
3487
+ fireEvent('callEvents', {
3488
+ type: 'CALL_SESSION_CREATED',
3489
+ result: messageContent
3490
+ });
3491
+
3492
+ currentCallId = messageContent.callId;
3493
+
3494
+ break;
3495
+
3496
+ /**
3497
+ * Type 113 Turn On Video Call
3498
+ */
3499
+ case chatMessageVOTypes.TURN_ON_VIDEO_CALL:
3500
+ if (messagesCallbacks[uniqueId]) {
3501
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3502
+ }
3503
+
3504
+ fireEvent('callEvents', {
3505
+ type: 'TURN_ON_VIDEO_CALL',
3506
+ result: messageContent
3507
+ });
3508
+
3509
+ break;
3510
+
3511
+ /**
3512
+ * Type 114 Turn Off Video Call
3513
+ */
3514
+ case chatMessageVOTypes.TURN_OFF_VIDEO_CALL:
3515
+ if (messagesCallbacks[uniqueId]) {
3516
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3517
+ }
3518
+
3519
+ fireEvent('callEvents', {
3520
+ type: 'TURN_OFF_VIDEO_CALL',
3521
+ result: messageContent
3522
+ });
3523
+
3524
+ break;
3525
+
3526
+ /**
3527
+ * Type 121 Record Call Request
3528
+ */
3529
+ case chatMessageVOTypes.RECORD_CALL:
3530
+ if (messagesCallbacks[uniqueId]) {
3531
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3532
+ }
3533
+
3534
+ fireEvent('callEvents', {
3535
+ type: 'START_RECORDING_CALL',
3536
+ result: messageContent
3537
+ });
3538
+
3539
+ break;
3540
+
3541
+ /**
3542
+ * Type 122 End Record Call Request
3543
+ */
3544
+ case chatMessageVOTypes.END_RECORD_CALL:
3545
+ if (messagesCallbacks[uniqueId]) {
3546
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3547
+ }
3548
+
3549
+ fireEvent('callEvents', {
3550
+ type: 'STOP_RECORDING_CALL',
3551
+ result: messageContent
3552
+ });
3553
+
3554
+ break;
3555
+
3556
+ /**
3557
+ * Type 126 Destined Record Call Request
3558
+ */
3559
+ case chatMessageVOTypes.DESTINATED_RECORD_CALL:
3560
+ if (messagesCallbacks[uniqueId]) {
3561
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3562
+ }
3563
+
3564
+ fireEvent('callEvents', {
3565
+ type: 'START_RECORDING_CALL',
3566
+ result: messageContent
3567
+ });
3568
+
3569
+ break;
3570
+
3571
+ /**
3572
+ * Type 127 Assistant call started
3573
+ *
3574
+ * Tells the assistant that a call started
3575
+ */
3576
+ case chatMessageVOTypes.ASSISTANT_CALL_STARTED:
3577
+ /*if (messagesCallbacks[uniqueId]) {
3578
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3579
+ }*/
3580
+
3581
+ fireEvent('callEvents', {
3582
+ type: 'ASSISTANT_CALL_STARTED',
3583
+ result: {
3584
+ callId: threadId
3585
+ }
3586
+ });
3587
+
3588
+ break;
3589
+
3590
+ /**
3591
+ * Type 128 Assistant call ended
3592
+ *
3593
+ * Tells the assistant that a call ended
3594
+ */
3595
+ case chatMessageVOTypes.ASSISTANT_CALL_ENDED:
3596
+ /* if (messagesCallbacks[uniqueId]) {
3597
+ messagesCallbacks[uniqueId](Utility.createReturnData(false, '', 0, messageContent, contentCount));
3598
+ }*/
3599
+
3600
+ fireEvent('callEvents', {
3601
+ type: 'ASSISTANT_CALL_ENDED'
3602
+ });
3603
+
3604
+ break;
3605
+
3606
+ /**
3607
+ * Type 999 All unknown errors
3608
+ */
3609
+ case chatMessageVOTypes.ERROR:
3610
+ if (messagesCallbacks[uniqueId]) {
3611
+ messagesCallbacks[uniqueId](Utility.createReturnData(true, messageContent.message, messageContent.code, messageContent, 0));
3612
+ }
3613
+
3614
+ /**
3615
+ * If error code is 21, Token is invalid &
3616
+ * user should logged out
3617
+ */
3618
+ if (messageContent.code == 21) {
3619
+ // TODO: Temporarily removed due to unknown side-effects
3620
+ // chatState = false;
3621
+ // asyncClient.logout();
3622
+ // clearChatServerCaches();
3623
+ }
3624
+
3625
+ fireEvent('error', {
3626
+ code: messageContent.code,
3627
+ message: messageContent.message,
3628
+ error: messageContent
3629
+ });
3630
+
3631
+ break;
3632
+ }
3633
+ },
3634
+
3635
+ /**
3636
+ * Send Message Callbacks Handler
3637
+ *
3638
+ * When you send Delivery or Seen Acknowledgements of a message
3639
+ * You should send Delivery and Seen for all the Messages before
3640
+ * that message so that you wont have un delivered/unseen messages
3641
+ * after seeing the last message of a thread
3642
+ *
3643
+ * @access private
3644
+ *
3645
+ * @param {int} actionType Switch between Delivery or Seen
3646
+ * @param {long} threadId Id of thread
3647
+ * @param {string} uniqueId uniqueId of message
3648
+ *
3649
+ * @return {undefined}
3650
+ */
3651
+ sendMessageCallbacksHandler = function (actionType, threadId, uniqueId) {
3652
+ switch (actionType) {
3653
+
3654
+ case chatMessageVOTypes.DELIVERY:
3655
+ if (threadCallbacks[threadId]) {
3656
+ var lastThreadCallbackIndex = Object.keys(threadCallbacks[threadId])
3657
+ .indexOf(uniqueId);
3658
+ if (lastThreadCallbackIndex !== undefined) {
3659
+ while (lastThreadCallbackIndex > -1) {
3660
+ var tempUniqueId = Object.entries(threadCallbacks[threadId])[lastThreadCallbackIndex][0];
3661
+ if (sendMessageCallbacks[tempUniqueId] && sendMessageCallbacks[tempUniqueId].onDeliver) {
3662
+ if (threadCallbacks[threadId][tempUniqueId] && threadCallbacks[threadId][tempUniqueId].onSent) {
3663
+ sendMessageCallbacks[tempUniqueId].onDeliver(
3664
+ {
3665
+ uniqueId: tempUniqueId
3666
+ });
3667
+ delete (sendMessageCallbacks[tempUniqueId].onDeliver);
3668
+ threadCallbacks[threadId][tempUniqueId].onDeliver = true;
3669
+ }
3670
+ }
3671
+
3672
+ lastThreadCallbackIndex -= 1;
3673
+ }
3674
+ }
3675
+ }
3676
+ break;
3677
+
3678
+ case chatMessageVOTypes.SEEN:
3679
+ if (threadCallbacks[threadId]) {
3680
+ var lastThreadCallbackIndex = Object.keys(threadCallbacks[threadId])
3681
+ .indexOf(uniqueId);
3682
+ if (lastThreadCallbackIndex !== undefined) {
3683
+ while (lastThreadCallbackIndex > -1) {
3684
+ var tempUniqueId = Object.entries(threadCallbacks[threadId])[lastThreadCallbackIndex][0];
3685
+
3686
+ if (sendMessageCallbacks[tempUniqueId] && sendMessageCallbacks[tempUniqueId].onSeen) {
3687
+ if (threadCallbacks[threadId][tempUniqueId] && threadCallbacks[threadId][tempUniqueId].onSent) {
3688
+ if (!threadCallbacks[threadId][tempUniqueId].onDeliver) {
3689
+ sendMessageCallbacks[tempUniqueId].onDeliver(
3690
+ {
3691
+ uniqueId: tempUniqueId
3692
+ });
3693
+ delete (sendMessageCallbacks[tempUniqueId].onDeliver);
3694
+ threadCallbacks[threadId][tempUniqueId].onDeliver = true;
3695
+ }
3696
+
3697
+ sendMessageCallbacks[tempUniqueId].onSeen(
3698
+ {
3699
+ uniqueId: tempUniqueId
3700
+ });
3701
+
3702
+ delete (sendMessageCallbacks[tempUniqueId].onSeen);
3703
+ threadCallbacks[threadId][tempUniqueId].onSeen = true;
3704
+
3705
+ if (threadCallbacks[threadId][tempUniqueId].onSent &&
3706
+ threadCallbacks[threadId][tempUniqueId].onDeliver &&
3707
+ threadCallbacks[threadId][tempUniqueId].onSeen) {
3708
+ delete threadCallbacks[threadId][tempUniqueId];
3709
+ delete sendMessageCallbacks[tempUniqueId];
3710
+ }
3711
+ }
3712
+ }
3713
+
3714
+ lastThreadCallbackIndex -= 1;
3715
+ }
3716
+ }
3717
+ }
3718
+ break;
3719
+
3720
+ default:
3721
+ break;
3722
+ }
3723
+ },
3724
+
3725
+ /**
3726
+ * New Message Handler
3727
+ *
3728
+ * Handles Event Emitter of a newly received Chat Message
3729
+ *
3730
+ * @access private
3731
+ *
3732
+ * @param {long} threadId ID of image
3338
3733
  * @param {object} messageContent Json Content of the message
3339
3734
  *
3340
3735
  * @return {undefined}
@@ -8691,16 +9086,171 @@
8691
9086
  // separate out the mime component
8692
9087
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
8693
9088
 
8694
- // write the bytes of the string to a typed array
8695
- var ia = new Uint8Array(byteString.length);
8696
- for (var i = 0; i < byteString.length; i++) {
8697
- ia[i] = byteString.charCodeAt(i);
8698
- }
9089
+ // write the bytes of the string to a typed array
9090
+ var ia = new Uint8Array(byteString.length);
9091
+ for (var i = 0; i < byteString.length; i++) {
9092
+ ia[i] = byteString.charCodeAt(i);
9093
+ }
9094
+
9095
+ return callback(new Blob([ia], {type: mimeString}));
9096
+ }
9097
+
9098
+ img.src = url;
9099
+ },
9100
+
9101
+ callReceived = function (params, callback) {
9102
+ var receiveCallData = {
9103
+ chatMessageVOType: chatMessageVOTypes.RECEIVE_CALL_REQUEST,
9104
+ typeCode: params.typeCode,
9105
+ pushMsgType: 3,
9106
+ token: token
9107
+ };
9108
+
9109
+ if (params) {
9110
+ if (typeof +params.callId === 'number' && params.callId > 0) {
9111
+ receiveCallData.subjectId = +params.callId;
9112
+ } else {
9113
+ fireEvent('error', {
9114
+ code: 999,
9115
+ message: 'Invalid call id!'
9116
+ });
9117
+ return;
9118
+ }
9119
+ } else {
9120
+ fireEvent('error', {
9121
+ code: 999,
9122
+ message: 'No params have been sent to ReceiveCall()'
9123
+ });
9124
+ return;
9125
+ }
9126
+
9127
+ return sendMessage(receiveCallData, {
9128
+ onResult: function (result) {
9129
+ callback && callback(result);
9130
+ }
9131
+ });
9132
+ },
9133
+
9134
+ endCall = function (params, callback) {
9135
+ consoleLogging && console.log('[SDK][endCall] called...');
9136
+
9137
+ var endCallData = {
9138
+ chatMessageVOType: chatMessageVOTypes.END_CALL_REQUEST,
9139
+ typeCode: params.typeCode,
9140
+ pushMsgType: 3,
9141
+ token: token
9142
+ };
9143
+
9144
+ if (!callRequestController.callEstablishedInMySide) {
9145
+ return;
9146
+ }
9147
+
9148
+ if (params) {
9149
+ if (typeof +params.callId === 'number' && params.callId > 0) {
9150
+ endCallData.subjectId = +params.callId;
9151
+ } else {
9152
+ fireEvent('error', {
9153
+ code: 999,
9154
+ message: 'Invalid call id!'
9155
+ });
9156
+ return;
9157
+ }
9158
+ } else {
9159
+ fireEvent('error', {
9160
+ code: 999,
9161
+ message: 'No params have been sent to End the call!'
9162
+ });
9163
+ return;
9164
+ }
9165
+
9166
+ callStop();
9167
+
9168
+ return sendMessage(endCallData, {
9169
+ onResult: function (result) {
9170
+ callback && callback(result);
9171
+ }
9172
+ });
9173
+ },
9174
+
9175
+ callStop = function () {
9176
+ if (callStopQueue.callStarted) {
9177
+ callStopQueue.callStarted = false;
9178
+ }
9179
+
9180
+ callRequestController.callEstablishedInMySide = false;
9181
+ callRequestController.callRequestReceived = false;
9182
+ currentCallParams = {};
9183
+ currentCallId = null;
9184
+ },
9185
+
9186
+ /**
9187
+ * Reformat Call Participants
9188
+ *
9189
+ * This functions reformats given Array of call Participants
9190
+ * into proper call participant
9191
+ *
9192
+ * @access private
9193
+ *
9194
+ * @param {object} participantsContent Array of Call Participant Objects
9195
+ * @param {int} threadId Id of call
9196
+ *
9197
+ * @return {object} Formatted Call Participant Array
9198
+ */
9199
+ reformatCallParticipants = function (participantsContent) {
9200
+ var returnData = [];
9201
+
9202
+ for (var i = 0; i < participantsContent.length; i++) {
9203
+ returnData.push(formatDataToMakeCallParticipant(participantsContent[i]));
9204
+ }
9205
+
9206
+ return returnData;
9207
+ },
9208
+
9209
+ formatDataToMakeCallParticipant = function (messageContent) {
9210
+ /**
9211
+ * + CallParticipantVO {object}
9212
+ * - id {int}
9213
+ * - joinTime {int}
9214
+ * - leaveTime {int}
9215
+ * - threadParticipant {object}
9216
+ * - sendTopic {string}
9217
+ * - receiveTopic {string}
9218
+ * - brokerAddress {string}
9219
+ * - active {boolean}
9220
+ * - callSession {object}
9221
+ * - callStatus {int}
9222
+ * - createTime {int}
9223
+ * - sendKey {string}
9224
+ * - mute {boolean}
9225
+ */
9226
+
9227
+ var participant = {
9228
+ id: messageContent.id,
9229
+ joinTime: messageContent.joinTime,
9230
+ leaveTime: messageContent.leaveTime,
9231
+ sendTopic: messageContent.sendTopic,
9232
+ receiveTopic: messageContent.receiveTopic,
9233
+ brokerAddress: messageContent.brokerAddress,
9234
+ active: messageContent.active,
9235
+ callSession: messageContent.callSession,
9236
+ callStatus: messageContent.callStatus,
9237
+ createTime: messageContent.createTime,
9238
+ sendKey: messageContent.sendKey,
9239
+ mute: messageContent.mute
9240
+ };
9241
+
9242
+ // Add Chat Participant if exist
9243
+ if (messageContent.participantVO) {
9244
+ participant.participantVO = messageContent.participantVO;
9245
+ }
8699
9246
 
8700
- return callback(new Blob([ia], {type: mimeString}));
9247
+ // Add Call Session if exist
9248
+ if (messageContent.callSession) {
9249
+ participant.callSession = messageContent.callSession;
8701
9250
  }
8702
9251
 
8703
- img.src = url;
9252
+ // return participant;
9253
+ return JSON.parse(JSON.stringify(participant));
8704
9254
  };
8705
9255
 
8706
9256
  /******************************************************
@@ -10407,218 +10957,765 @@
10407
10957
  timeout: params.timeout
10408
10958
  };
10409
10959
 
10410
- if (params) {
10411
- if (parseInt(params.contactId) > 0) {
10412
- blockData.content.contactId = params.contactId;
10413
- }
10960
+ if (params) {
10961
+ if (parseInt(params.contactId) > 0) {
10962
+ blockData.content.contactId = params.contactId;
10963
+ }
10964
+
10965
+ if (parseInt(params.threadId) > 0) {
10966
+ blockData.content.threadId = params.threadId;
10967
+ }
10968
+
10969
+ if (parseInt(params.userId) > 0) {
10970
+ blockData.content.userId = params.userId;
10971
+ }
10972
+ }
10973
+
10974
+ return sendMessage(blockData, {
10975
+ onResult: function (result) {
10976
+ if (typeof result.result == 'object') {
10977
+ result.result = formatDataToMakeBlockedUser(result.result);
10978
+ }
10979
+ callback && callback(result);
10980
+ }
10981
+ });
10982
+ };
10983
+
10984
+ this.unblock = function (params, callback) {
10985
+ var unblockData = {
10986
+ chatMessageVOType: chatMessageVOTypes.UNBLOCK,
10987
+ typeCode: params.typeCode,
10988
+ pushMsgType: 4,
10989
+ token: token,
10990
+ content: {},
10991
+ timeout: params.timeout
10992
+ };
10993
+
10994
+ if (params) {
10995
+ if (parseInt(params.blockId) > 0) {
10996
+ unblockData.subjectId = params.blockId;
10997
+ }
10998
+
10999
+ if (parseInt(params.contactId) > 0) {
11000
+ unblockData.content.contactId = params.contactId;
11001
+ }
11002
+
11003
+ if (parseInt(params.threadId) > 0) {
11004
+ unblockData.content.threadId = params.threadId;
11005
+ }
11006
+
11007
+ if (parseInt(params.userId) > 0) {
11008
+ unblockData.content.userId = params.userId;
11009
+ }
11010
+ }
11011
+
11012
+ return sendMessage(unblockData, {
11013
+ onResult: function (result) {
11014
+ if (typeof result.result == 'object') {
11015
+ result.result = formatDataToMakeBlockedUser(result.result);
11016
+ }
11017
+
11018
+ callback && callback(result);
11019
+ }
11020
+ });
11021
+ };
11022
+
11023
+ this.getBlockedList = function (params, callback) {
11024
+ var count = 50,
11025
+ offset = 0,
11026
+ content = {};
11027
+
11028
+ if (params) {
11029
+ if (parseInt(params.count) > 0) {
11030
+ count = params.count;
11031
+ }
11032
+
11033
+ if (parseInt(params.offset) > 0) {
11034
+ offset = params.offset;
11035
+ }
11036
+ }
11037
+
11038
+ content.count = count;
11039
+ content.offset = offset;
11040
+
11041
+ var getBlockedData = {
11042
+ chatMessageVOType: chatMessageVOTypes.GET_BLOCKED,
11043
+ typeCode: params.typeCode,
11044
+ content: content,
11045
+ pushMsgType: 4,
11046
+ token: token,
11047
+ timeout: params.timeout
11048
+ };
11049
+
11050
+ return sendMessage(getBlockedData, {
11051
+ onResult: function (result) {
11052
+ var returnData = {
11053
+ hasError: result.hasError,
11054
+ cache: false,
11055
+ errorMessage: result.errorMessage,
11056
+ errorCode: result.errorCode
11057
+ };
11058
+
11059
+ if (!returnData.hasError) {
11060
+ var messageContent = result.result,
11061
+ messageLength = messageContent.length,
11062
+ resultData = {
11063
+ blockedUsers: [],
11064
+ contentCount: result.contentCount,
11065
+ hasNext: (offset + count < result.contentCount && messageLength > 0),
11066
+ nextOffset: offset + messageLength
11067
+ },
11068
+ blockedUser;
11069
+
11070
+ for (var i = 0; i < messageLength; i++) {
11071
+ blockedUser = formatDataToMakeBlockedUser(messageContent[i]);
11072
+ if (blockedUser) {
11073
+ resultData.blockedUsers.push(blockedUser);
11074
+ }
11075
+ }
11076
+
11077
+ returnData.result = resultData;
11078
+ }
11079
+
11080
+ callback && callback(returnData);
11081
+ }
11082
+ });
11083
+ };
11084
+
11085
+ this.getUserNotSeenDuration = function (params, callback) {
11086
+ var content = {};
11087
+
11088
+ if (params) {
11089
+ if (Array.isArray(params.userIds)) {
11090
+ content.userIds = params.userIds;
11091
+ }
11092
+ }
11093
+
11094
+ var getNotSeenDurationData = {
11095
+ chatMessageVOType: chatMessageVOTypes.GET_NOT_SEEN_DURATION,
11096
+ typeCode: params.typeCode,
11097
+ content: content,
11098
+ pushMsgType: 4,
11099
+ token: token,
11100
+ timeout: params.timeout
11101
+ };
11102
+
11103
+ return sendMessage(getNotSeenDurationData, {
11104
+ onResult: function (result) {
11105
+ var returnData = {
11106
+ hasError: result.hasError,
11107
+ cache: false,
11108
+ errorMessage: result.errorMessage,
11109
+ errorCode: result.errorCode
11110
+ };
11111
+
11112
+ if (!returnData.hasError) {
11113
+ returnData.result = result.result;
11114
+ }
11115
+
11116
+ callback && callback(returnData);
11117
+ }
11118
+ });
11119
+ };
11120
+
11121
+ this.addContacts = function (params, callback) {
11122
+ var data = {};
11123
+
11124
+ if (params) {
11125
+ if (typeof params.firstName === 'string') {
11126
+ data.firstName = params.firstName;
11127
+ } else {
11128
+ data.firstName = '';
11129
+ }
11130
+
11131
+ if (typeof params.lastName === 'string') {
11132
+ data.lastName = params.lastName;
11133
+ } else {
11134
+ data.lastName = '';
11135
+ }
11136
+
11137
+ if (typeof params.typeCode === 'string') {
11138
+ data.typeCode = params.typeCode;
11139
+ } else if (generalTypeCode) {
11140
+ data.typeCode = generalTypeCode;
11141
+ }
11142
+
11143
+ if (typeof params.cellphoneNumber === 'string') {
11144
+ data.cellphoneNumber = params.cellphoneNumber;
11145
+ } else {
11146
+ data.cellphoneNumber = '';
11147
+ }
11148
+
11149
+ if (typeof params.email === 'string') {
11150
+ data.email = params.email;
11151
+ } else {
11152
+ data.email = '';
11153
+ }
11154
+
11155
+ if (typeof params.username === 'string') {
11156
+ data.username = params.username;
11157
+ }
11158
+
11159
+ data.uniqueId = Utility.generateUUID();
11160
+ }
11161
+
11162
+ var requestParams = {
11163
+ url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.ADD_CONTACTS,
11164
+ method: 'POST',
11165
+ data: data,
11166
+ headers: {
11167
+ '_token_': token,
11168
+ '_token_issuer_': 1
11169
+ }
11170
+ };
11171
+
11172
+ httpRequest(requestParams, function (result) {
11173
+ if (!result.hasError) {
11174
+ var responseData = JSON.parse(result.result.responseText);
11175
+
11176
+ var returnData = {
11177
+ hasError: responseData.hasError,
11178
+ cache: false,
11179
+ errorMessage: responseData.message,
11180
+ errorCode: responseData.errorCode
11181
+ };
11182
+
11183
+ if (!responseData.hasError) {
11184
+ var messageContent = responseData.result,
11185
+ messageLength = responseData.result.length,
11186
+ resultData = {
11187
+ contacts: [],
11188
+ contentCount: messageLength
11189
+ },
11190
+ contactData;
11191
+
11192
+ for (var i = 0; i < messageLength; i++) {
11193
+ contactData = formatDataToMakeContact(messageContent[i]);
11194
+ if (contactData) {
11195
+ resultData.contacts.push(contactData);
11196
+ }
11197
+ }
11198
+
11199
+ returnData.result = resultData;
11200
+
11201
+ /**
11202
+ * Add Contacts into cache database #cache
11203
+ */
11204
+ if (canUseCache && cacheSecret.length > 0) {
11205
+ if (db) {
11206
+ var cacheData = [];
11207
+
11208
+ for (var i = 0; i < resultData.contacts.length; i++) {
11209
+ try {
11210
+ var tempData = {},
11211
+ salt = Utility.generateUUID();
11212
+ tempData.id = resultData.contacts[i].id;
11213
+ tempData.owner = userInfo.id;
11214
+ tempData.uniqueId = resultData.contacts[i].uniqueId;
11215
+ tempData.userId = Utility.crypt(resultData.contacts[i].userId, cacheSecret, salt);
11216
+ tempData.cellphoneNumber = Utility.crypt(resultData.contacts[i].cellphoneNumber, cacheSecret, salt);
11217
+ tempData.email = Utility.crypt(resultData.contacts[i].email, cacheSecret, salt);
11218
+ tempData.firstName = Utility.crypt(resultData.contacts[i].firstName, cacheSecret, salt);
11219
+ tempData.lastName = Utility.crypt(resultData.contacts[i].lastName, cacheSecret, salt);
11220
+ tempData.expireTime = new Date().getTime() + cacheExpireTime;
11221
+ tempData.data = Utility.crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
11222
+ tempData.salt = salt;
10414
11223
 
10415
- if (parseInt(params.threadId) > 0) {
10416
- blockData.content.threadId = params.threadId;
10417
- }
11224
+ cacheData.push(tempData);
11225
+ } catch (error) {
11226
+ fireEvent('error', {
11227
+ code: error.code,
11228
+ message: error.message,
11229
+ error: error
11230
+ });
11231
+ }
11232
+ }
10418
11233
 
10419
- if (parseInt(params.userId) > 0) {
10420
- blockData.content.userId = params.userId;
10421
- }
10422
- }
11234
+ db.contacts.bulkPut(cacheData)
11235
+ .catch(function (error) {
11236
+ fireEvent('error', {
11237
+ code: error.code,
11238
+ message: error.message,
11239
+ error: error
11240
+ });
11241
+ });
11242
+ } else {
11243
+ fireEvent('error', {
11244
+ code: 6601,
11245
+ message: CHAT_ERRORS[6601],
11246
+ error: null
11247
+ });
11248
+ }
11249
+ }
10423
11250
 
10424
- return sendMessage(blockData, {
10425
- onResult: function (result) {
10426
- if (typeof result.result == 'object') {
10427
- result.result = formatDataToMakeBlockedUser(result.result);
10428
11251
  }
10429
- callback && callback(result);
11252
+
11253
+ callback && callback(returnData);
11254
+
11255
+ } else {
11256
+ fireEvent('error', {
11257
+ code: result.errorCode,
11258
+ message: result.errorMessage,
11259
+ error: result
11260
+ });
10430
11261
  }
10431
11262
  });
10432
11263
  };
10433
11264
 
10434
- this.unblock = function (params, callback) {
10435
- var unblockData = {
10436
- chatMessageVOType: chatMessageVOTypes.UNBLOCK,
10437
- typeCode: params.typeCode,
10438
- pushMsgType: 4,
10439
- token: token,
10440
- content: {},
10441
- timeout: params.timeout
10442
- };
11265
+ this.updateContacts = function (params, callback) {
11266
+ var data = {};
10443
11267
 
10444
11268
  if (params) {
10445
- if (parseInt(params.blockId) > 0) {
10446
- unblockData.subjectId = params.blockId;
10447
- }
10448
-
10449
- if (parseInt(params.contactId) > 0) {
10450
- unblockData.content.contactId = params.contactId;
11269
+ if (parseInt(params.id) > 0) {
11270
+ data.id = parseInt(params.id);
11271
+ } else {
11272
+ fireEvent('error', {
11273
+ code: 999,
11274
+ message: 'ID is required for Updating Contact!',
11275
+ error: undefined
11276
+ });
10451
11277
  }
10452
11278
 
10453
- if (parseInt(params.threadId) > 0) {
10454
- unblockData.content.threadId = params.threadId;
11279
+ if (typeof params.firstName === 'string') {
11280
+ data.firstName = params.firstName;
11281
+ } else {
11282
+ fireEvent('error', {
11283
+ code: 999,
11284
+ message: 'firstName is required for Updating Contact!'
11285
+ });
10455
11286
  }
10456
11287
 
10457
- if (parseInt(params.userId) > 0) {
10458
- unblockData.content.userId = params.userId;
11288
+ if (typeof params.lastName === 'string') {
11289
+ data.lastName = params.lastName;
11290
+ } else {
11291
+ fireEvent('error', {
11292
+ code: 999,
11293
+ message: 'lastName is required for Updating Contact!'
11294
+ });
10459
11295
  }
10460
- }
10461
11296
 
10462
- return sendMessage(unblockData, {
10463
- onResult: function (result) {
10464
- if (typeof result.result == 'object') {
10465
- result.result = formatDataToMakeBlockedUser(result.result);
10466
- }
10467
-
10468
- callback && callback(result);
11297
+ if (typeof params.cellphoneNumber === 'string') {
11298
+ data.cellphoneNumber = params.cellphoneNumber;
11299
+ } else {
11300
+ fireEvent('error', {
11301
+ code: 999,
11302
+ message: 'cellphoneNumber is required for Updating Contact!'
11303
+ });
10469
11304
  }
10470
- });
10471
- };
10472
11305
 
10473
- this.getBlockedList = function (params, callback) {
10474
- var count = 50,
10475
- offset = 0,
10476
- content = {};
10477
-
10478
- if (params) {
10479
- if (parseInt(params.count) > 0) {
10480
- count = params.count;
11306
+ if (typeof params.email === 'string') {
11307
+ data.email = params.email;
11308
+ } else {
11309
+ fireEvent('error', {
11310
+ code: 999,
11311
+ message: 'email is required for Updating Contact!'
11312
+ });
10481
11313
  }
10482
11314
 
10483
- if (parseInt(params.offset) > 0) {
10484
- offset = params.offset;
10485
- }
11315
+ data.uniqueId = Utility.generateUUID();
10486
11316
  }
10487
11317
 
10488
- content.count = count;
10489
- content.offset = offset;
10490
-
10491
- var getBlockedData = {
10492
- chatMessageVOType: chatMessageVOTypes.GET_BLOCKED,
10493
- typeCode: params.typeCode,
10494
- content: content,
10495
- pushMsgType: 4,
10496
- token: token,
10497
- timeout: params.timeout
11318
+ var requestParams = {
11319
+ url: SERVICE_ADDRESSES.PLATFORM_ADDRESS +
11320
+ SERVICES_PATH.UPDATE_CONTACTS,
11321
+ method: 'GET',
11322
+ data: data,
11323
+ headers: {
11324
+ '_token_': token,
11325
+ '_token_issuer_': 1
11326
+ }
10498
11327
  };
10499
11328
 
10500
- return sendMessage(getBlockedData, {
10501
- onResult: function (result) {
11329
+ httpRequest(requestParams, function (result) {
11330
+ if (!result.hasError) {
11331
+ var responseData = JSON.parse(result.result.responseText);
11332
+
10502
11333
  var returnData = {
10503
- hasError: result.hasError,
11334
+ hasError: responseData.hasError,
10504
11335
  cache: false,
10505
- errorMessage: result.errorMessage,
10506
- errorCode: result.errorCode
11336
+ errorMessage: responseData.message,
11337
+ errorCode: responseData.errorCode
10507
11338
  };
10508
11339
 
10509
- if (!returnData.hasError) {
10510
- var messageContent = result.result,
10511
- messageLength = messageContent.length,
11340
+ if (!responseData.hasError) {
11341
+ var messageContent = responseData.result,
11342
+ messageLength = responseData.result.length,
10512
11343
  resultData = {
10513
- blockedUsers: [],
10514
- contentCount: result.contentCount,
10515
- hasNext: (offset + count < result.contentCount && messageLength > 0),
10516
- nextOffset: offset + messageLength
11344
+ contacts: [],
11345
+ contentCount: messageLength
10517
11346
  },
10518
- blockedUser;
11347
+ contactData;
10519
11348
 
10520
11349
  for (var i = 0; i < messageLength; i++) {
10521
- blockedUser = formatDataToMakeBlockedUser(messageContent[i]);
10522
- if (blockedUser) {
10523
- resultData.blockedUsers.push(blockedUser);
11350
+ contactData = formatDataToMakeContact(messageContent[i]);
11351
+ if (contactData) {
11352
+ resultData.contacts.push(contactData);
10524
11353
  }
10525
11354
  }
10526
11355
 
10527
11356
  returnData.result = resultData;
11357
+
11358
+ /**
11359
+ * Add Contacts into cache database #cache
11360
+ */
11361
+ if (canUseCache && cacheSecret.length > 0) {
11362
+ if (db) {
11363
+ var cacheData = [];
11364
+
11365
+ for (var i = 0; i < resultData.contacts.length; i++) {
11366
+ try {
11367
+ var tempData = {},
11368
+ salt = Utility.generateUUID();
11369
+ tempData.id = resultData.contacts[i].id;
11370
+ tempData.owner = userInfo.id;
11371
+ tempData.uniqueId = resultData.contacts[i].uniqueId;
11372
+ tempData.userId = Utility.crypt(resultData.contacts[i].userId, cacheSecret, salt);
11373
+ tempData.cellphoneNumber = Utility.crypt(resultData.contacts[i].cellphoneNumber, cacheSecret, salt);
11374
+ tempData.email = Utility.crypt(resultData.contacts[i].email, cacheSecret, salt);
11375
+ tempData.firstName = Utility.crypt(resultData.contacts[i].firstName, cacheSecret, salt);
11376
+ tempData.lastName = Utility.crypt(resultData.contacts[i].lastName, cacheSecret, salt);
11377
+ tempData.expireTime = new Date().getTime() + cacheExpireTime;
11378
+ tempData.data = Utility.crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
11379
+ tempData.salt = salt;
11380
+
11381
+ cacheData.push(tempData);
11382
+ } catch (error) {
11383
+ fireEvent('error', {
11384
+ code: error.code,
11385
+ message: error.message,
11386
+ error: error
11387
+ });
11388
+ }
11389
+ }
11390
+
11391
+ db.contacts.bulkPut(cacheData)
11392
+ .catch(function (error) {
11393
+ fireEvent('error', {
11394
+ code: error.code,
11395
+ message: error.message,
11396
+ error: error
11397
+ });
11398
+ });
11399
+ } else {
11400
+ fireEvent('error', {
11401
+ code: 6601,
11402
+ message: CHAT_ERRORS[6601],
11403
+ error: null
11404
+ });
11405
+ }
11406
+ }
11407
+
10528
11408
  }
10529
11409
 
10530
11410
  callback && callback(returnData);
11411
+
11412
+ } else {
11413
+ fireEvent('error', {
11414
+ code: result.errorCode,
11415
+ message: result.errorMessage,
11416
+ error: result
11417
+ });
10531
11418
  }
10532
11419
  });
10533
11420
  };
10534
11421
 
10535
- this.getUserNotSeenDuration = function (params, callback) {
10536
- var content = {};
11422
+ this.removeContacts = function (params, callback) {
11423
+ var data = {};
10537
11424
 
10538
11425
  if (params) {
10539
- if (Array.isArray(params.userIds)) {
10540
- content.userIds = params.userIds;
11426
+ if (parseInt(params.id) > 0) {
11427
+ data.id = parseInt(params.id);
11428
+ } else {
11429
+ fireEvent('error', {
11430
+ code: 999,
11431
+ message: 'ID is required for Deleting Contact!',
11432
+ error: undefined
11433
+ });
10541
11434
  }
10542
11435
  }
10543
11436
 
10544
- var getNotSeenDurationData = {
10545
- chatMessageVOType: chatMessageVOTypes.GET_NOT_SEEN_DURATION,
10546
- typeCode: params.typeCode,
10547
- content: content,
10548
- pushMsgType: 4,
10549
- token: token,
10550
- timeout: params.timeout
11437
+ var requestParams = {
11438
+ url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.REMOVE_CONTACTS,
11439
+ method: 'POST',
11440
+ data: data,
11441
+ headers: {
11442
+ '_token_': token,
11443
+ '_token_issuer_': 1
11444
+ }
10551
11445
  };
10552
11446
 
10553
- return sendMessage(getNotSeenDurationData, {
10554
- onResult: function (result) {
11447
+ httpRequest(requestParams, function (result) {
11448
+ if (!result.hasError) {
11449
+ var responseData = JSON.parse(result.result.responseText);
11450
+
10555
11451
  var returnData = {
10556
- hasError: result.hasError,
11452
+ hasError: responseData.hasError,
10557
11453
  cache: false,
10558
- errorMessage: result.errorMessage,
10559
- errorCode: result.errorCode
11454
+ errorMessage: responseData.message,
11455
+ errorCode: responseData.errorCode
10560
11456
  };
10561
11457
 
10562
- if (!returnData.hasError) {
10563
- returnData.result = result.result;
11458
+ if (!responseData.hasError) {
11459
+ returnData.result = responseData.result;
11460
+ }
11461
+
11462
+ /**
11463
+ * Remove the contact from cache
11464
+ */
11465
+ if (canUseCache) {
11466
+ if (db) {
11467
+ db.contacts.where('id')
11468
+ .equals(parseInt(params.id))
11469
+ .delete()
11470
+ .catch(function (error) {
11471
+ fireEvent('error', {
11472
+ code: 6602,
11473
+ message: CHAT_ERRORS[6602],
11474
+ error: error
11475
+ });
11476
+ });
11477
+ } else {
11478
+ fireEvent('error', {
11479
+ code: 6601,
11480
+ message: CHAT_ERRORS[6601],
11481
+ error: null
11482
+ });
11483
+ }
10564
11484
  }
10565
11485
 
10566
11486
  callback && callback(returnData);
11487
+
11488
+ } else {
11489
+ fireEvent('error', {
11490
+ code: result.errorCode,
11491
+ message: result.errorMessage,
11492
+ error: result
11493
+ });
10567
11494
  }
10568
11495
  });
10569
11496
  };
10570
11497
 
10571
- this.addContacts = function (params, callback) {
10572
- var data = {};
11498
+ this.searchContacts = function (params, callback) {
11499
+ var data = {
11500
+ size: 50,
11501
+ offset: 0
11502
+ },
11503
+ whereClause = {},
11504
+ returnCache = false;
10573
11505
 
10574
11506
  if (params) {
10575
11507
  if (typeof params.firstName === 'string') {
10576
- data.firstName = params.firstName;
10577
- } else {
10578
- data.firstName = '';
11508
+ data.firstName = whereClause.firstName = params.firstName;
10579
11509
  }
10580
11510
 
10581
11511
  if (typeof params.lastName === 'string') {
10582
- data.lastName = params.lastName;
10583
- } else {
10584
- data.lastName = '';
11512
+ data.lastName = whereClause.lastName = params.lastName;
10585
11513
  }
10586
11514
 
10587
- if (typeof params.typeCode === 'string') {
10588
- data.typeCode = params.typeCode;
10589
- } else if (generalTypeCode) {
10590
- data.typeCode = generalTypeCode;
11515
+ if (parseInt(params.cellphoneNumber) > 0) {
11516
+ data.cellphoneNumber = whereClause.cellphoneNumber = params.cellphoneNumber;
10591
11517
  }
10592
11518
 
10593
- if (typeof params.cellphoneNumber === 'string') {
10594
- data.cellphoneNumber = params.cellphoneNumber;
10595
- } else {
10596
- data.cellphoneNumber = '';
11519
+ if (typeof params.email === 'string') {
11520
+ data.email = whereClause.email = params.email;
11521
+ }
11522
+
11523
+ if (typeof params.query === 'string') {
11524
+ data.q = whereClause.q = params.query;
11525
+ }
11526
+
11527
+ if (typeof params.uniqueId === 'string') {
11528
+ data.uniqueId = whereClause.uniqueId = params.uniqueId;
11529
+ }
11530
+
11531
+ if (parseInt(params.id) > 0) {
11532
+ data.id = whereClause.id = params.id;
11533
+ }
11534
+
11535
+ if (parseInt(params.typeCode) > 0) {
11536
+ data.typeCode = whereClause.typeCode = params.typeCode;
11537
+ }
11538
+
11539
+ if (parseInt(params.size) > 0) {
11540
+ data.size = params.size;
11541
+ }
11542
+
11543
+ if (parseInt(params.offset) > 0) {
11544
+ data.offset = params.offset;
11545
+ }
11546
+
11547
+ var functionLevelCache = (typeof params.cache == 'boolean') ? params.cache : true;
11548
+ }
11549
+
11550
+ var requestParams = {
11551
+ url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.SEARCH_CONTACTS,
11552
+ method: 'POST',
11553
+ data: data,
11554
+ headers: {
11555
+ '_token_': token,
11556
+ '_token_issuer_': 1
10597
11557
  }
11558
+ };
11559
+
11560
+ /**
11561
+ * Search contacts in cache #cache
11562
+ */
11563
+ if (functionLevelCache && canUseCache && cacheSecret.length > 0) {
11564
+ if (db) {
11565
+
11566
+ /**
11567
+ * First of all we delete all contacts those
11568
+ * expireTime has been expired. after that
11569
+ * we query our cache database to retrieve
11570
+ * what we wanted
11571
+ */
11572
+ db.contacts.where('expireTime')
11573
+ .below(new Date().getTime())
11574
+ .delete()
11575
+ .then(function () {
11576
+
11577
+ /**
11578
+ * Query cache database to get contacts
11579
+ */
11580
+
11581
+ var thenAble;
11582
+
11583
+ if (Object.keys(whereClause).length === 0) {
11584
+ thenAble = db.contacts.where('owner')
11585
+ .equals(parseInt(userInfo.id));
11586
+ } else {
11587
+ if (whereClause.hasOwnProperty('id')) {
11588
+ thenAble = db.contacts.where('owner')
11589
+ .equals(parseInt(userInfo.id))
11590
+ .and(function (contact) {
11591
+ return contact.id == whereClause.id;
11592
+ });
11593
+ } else if (whereClause.hasOwnProperty('uniqueId')) {
11594
+ thenAble = db.contacts.where('owner')
11595
+ .equals(parseInt(userInfo.id))
11596
+ .and(function (contact) {
11597
+ return contact.uniqueId == whereClause.uniqueId;
11598
+ });
11599
+ } else {
11600
+ if (whereClause.hasOwnProperty('firstName')) {
11601
+ thenAble = db.contacts.where('owner')
11602
+ .equals(parseInt(userInfo.id))
11603
+ .filter(function (contact) {
11604
+ var reg = new RegExp(whereClause.firstName);
11605
+ return reg.test(chatDecrypt(contact.firstName, cacheSecret, contact.salt));
11606
+ });
11607
+ }
11608
+
11609
+ if (whereClause.hasOwnProperty('lastName')) {
11610
+ thenAble = db.contacts.where('owner')
11611
+ .equals(parseInt(userInfo.id))
11612
+ .filter(function (contact) {
11613
+ var reg = new RegExp(whereClause.lastName);
11614
+ return reg.test(chatDecrypt(contact.lastName, cacheSecret, contact.salt));
11615
+ });
11616
+ }
11617
+
11618
+ if (whereClause.hasOwnProperty('email')) {
11619
+ thenAble = db.contacts.where('owner')
11620
+ .equals(parseInt(userInfo.id))
11621
+ .filter(function (contact) {
11622
+ var reg = new RegExp(whereClause.email);
11623
+ return reg.test(chatDecrypt(contact.email, cacheSecret, contact.salt));
11624
+ });
11625
+ }
11626
+
11627
+ if (whereClause.hasOwnProperty('q')) {
11628
+ thenAble = db.contacts.where('owner')
11629
+ .equals(parseInt(userInfo.id))
11630
+ .filter(function (contact) {
11631
+ var reg = new RegExp(whereClause.q);
11632
+ return reg.test(chatDecrypt(contact.firstName, cacheSecret, contact.salt) + ' ' +
11633
+ chatDecrypt(contact.lastName, cacheSecret, contact.salt) + ' ' +
11634
+ chatDecrypt(contact.email, cacheSecret, contact.salt));
11635
+ });
11636
+ }
11637
+ }
11638
+ }
11639
+
11640
+ thenAble.offset(data.offset)
11641
+ .limit(data.size)
11642
+ .toArray()
11643
+ .then(function (contacts) {
11644
+ db.contacts.where('owner')
11645
+ .equals(parseInt(userInfo.id))
11646
+ .count()
11647
+ .then(function (contactsCount) {
11648
+ var cacheData = [];
11649
+
11650
+ for (var i = 0; i < contacts.length; i++) {
11651
+ try {
11652
+ var tempData = {},
11653
+ salt = contacts[i].salt;
11654
+
11655
+ cacheData.push(formatDataToMakeContact(JSON.parse(chatDecrypt(contacts[i].data, cacheSecret, ontacts[i].salt))));
11656
+ } catch (error) {
11657
+ fireEvent('error', {
11658
+ code: error.code,
11659
+ message: error.message,
11660
+ error: error
11661
+ });
11662
+ }
11663
+ }
11664
+
11665
+ var returnData = {
11666
+ hasError: false,
11667
+ cache: true,
11668
+ errorCode: 0,
11669
+ errorMessage: '',
11670
+ result: {
11671
+ contacts: cacheData,
11672
+ contentCount: contactsCount,
11673
+ hasNext: !(contacts.length < data.size),
11674
+ nextOffset: data.offset + contacts.length
11675
+ }
11676
+ };
10598
11677
 
10599
- if (typeof params.email === 'string') {
10600
- data.email = params.email;
11678
+ if (cacheData.length > 0) {
11679
+ callback && callback(returnData);
11680
+ callback = undefined;
11681
+ returnCache = true;
11682
+ }
11683
+ })
11684
+ .catch(function (error) {
11685
+ fireEvent('error', {
11686
+ code: error.code,
11687
+ message: error.message,
11688
+ error: error
11689
+ });
11690
+ });
11691
+ })
11692
+ .catch(function (error) {
11693
+ fireEvent('error', {
11694
+ code: error.code,
11695
+ message: error.message,
11696
+ error: error
11697
+ });
11698
+ });
11699
+ })
11700
+ .catch(function (error) {
11701
+ fireEvent('error', {
11702
+ code: error.code,
11703
+ message: error.message,
11704
+ error: error
11705
+ });
11706
+ });
10601
11707
  } else {
10602
- data.email = '';
10603
- }
10604
-
10605
- if (typeof params.username === 'string') {
10606
- data.username = params.username;
11708
+ fireEvent('error', {
11709
+ code: 6601,
11710
+ message: CHAT_ERRORS[6601],
11711
+ error: null
11712
+ });
10607
11713
  }
10608
-
10609
- data.uniqueId = Utility.generateUUID();
10610
11714
  }
10611
11715
 
10612
- var requestParams = {
10613
- url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.ADD_CONTACTS,
10614
- method: 'POST',
10615
- data: data,
10616
- headers: {
10617
- '_token_': token,
10618
- '_token_issuer_': 1
10619
- }
10620
- };
10621
-
11716
+ /**
11717
+ * Get Search Contacts Result From Server
11718
+ */
10622
11719
  httpRequest(requestParams, function (result) {
10623
11720
  if (!result.hasError) {
10624
11721
  var responseData = JSON.parse(result.result.responseText);
@@ -10659,6 +11756,7 @@
10659
11756
  try {
10660
11757
  var tempData = {},
10661
11758
  salt = Utility.generateUUID();
11759
+
10662
11760
  tempData.id = resultData.contacts[i].id;
10663
11761
  tempData.owner = userInfo.id;
10664
11762
  tempData.uniqueId = resultData.contacts[i].uniqueId;
@@ -10668,7 +11766,7 @@
10668
11766
  tempData.firstName = Utility.crypt(resultData.contacts[i].firstName, cacheSecret, salt);
10669
11767
  tempData.lastName = Utility.crypt(resultData.contacts[i].lastName, cacheSecret, salt);
10670
11768
  tempData.expireTime = new Date().getTime() + cacheExpireTime;
10671
- tempData.data = Utility.crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
11769
+ tempData.data = crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
10672
11770
  tempData.salt = salt;
10673
11771
 
10674
11772
  cacheData.push(tempData);
@@ -10697,11 +11795,21 @@
10697
11795
  });
10698
11796
  }
10699
11797
  }
10700
-
10701
11798
  }
10702
11799
 
10703
11800
  callback && callback(returnData);
11801
+ /**
11802
+ * Delete callback so if server pushes response before
11803
+ * cache, cache won't send data again
11804
+ */
11805
+ callback = undefined;
10704
11806
 
11807
+ if (!returnData.hasError && returnCache) {
11808
+ fireEvent('contactEvents', {
11809
+ type: 'CONTACTS_SEARCH_RESULT_CHANGE',
11810
+ result: returnData.result
11811
+ });
11812
+ }
10705
11813
  } else {
10706
11814
  fireEvent('error', {
10707
11815
  code: result.errorCode,
@@ -10712,67 +11820,285 @@
10712
11820
  });
10713
11821
  };
10714
11822
 
10715
- this.updateContacts = function (params, callback) {
10716
- var data = {};
11823
+ this.createBot = function (params, callback) {
11824
+ var createBotData = {
11825
+ chatMessageVOType: chatMessageVOTypes.CREATE_BOT,
11826
+ typeCode: params.typeCode,
11827
+ content: '',
11828
+ pushMsgType: 4,
11829
+ token: token
11830
+ };
11831
+
11832
+ if (params) {
11833
+ if (typeof params.botName === 'string' && params.botName.length > 0) {
11834
+ if (params.botName.substr(-3) === "BOT") {
11835
+ createBotData.content = params.botName;
11836
+ } else {
11837
+ fireEvent('error', {
11838
+ code: 999,
11839
+ message: 'Bot name should end in "BOT", ex. "testBOT"'
11840
+ });
11841
+ return;
11842
+ }
11843
+ } else {
11844
+ fireEvent('error', {
11845
+ code: 999,
11846
+ message: 'Insert a bot name to create one!'
11847
+ });
11848
+ return;
11849
+ }
11850
+ } else {
11851
+ fireEvent('error', {
11852
+ code: 999,
11853
+ message: 'Insert a bot name to create one!'
11854
+ });
11855
+ return;
11856
+ }
11857
+
11858
+ return sendMessage(createBotData, {
11859
+ onResult: function (result) {
11860
+ callback && callback(result);
11861
+ }
11862
+ });
11863
+ };
11864
+
11865
+ this.defineBotCommand = function (params, callback) {
11866
+ var defineBotCommandData = {
11867
+ chatMessageVOType: chatMessageVOTypes.DEFINE_BOT_COMMAND,
11868
+ typeCode: params.typeCode,
11869
+ content: {},
11870
+ pushMsgType: 4,
11871
+ token: token
11872
+ }, commandList = [];
11873
+
11874
+ if (params) {
11875
+ if (typeof params.botName !== 'string' || params.botName.length == 0) {
11876
+ fireEvent('error', {
11877
+ code: 999,
11878
+ message: 'You need to insert a botName!'
11879
+ });
11880
+ return;
11881
+ }
11882
+
11883
+ if (!Array.isArray(params.commandList) || !params.commandList.length) {
11884
+ fireEvent('error', {
11885
+ code: 999,
11886
+ message: 'Bot Commands List has to be an array of strings.'
11887
+ });
11888
+ return;
11889
+ } else {
11890
+ for (var i = 0; i < params.commandList.length; i++) {
11891
+ commandList.push('/' + params.commandList[i].trim());
11892
+ }
11893
+ }
11894
+
11895
+ defineBotCommandData.content = {
11896
+ botName: params.botName.trim(),
11897
+ commandList: commandList
11898
+ };
11899
+
11900
+ } else {
11901
+ fireEvent('error', {
11902
+ code: 999,
11903
+ message: 'No params have been sent to create bot commands'
11904
+ });
11905
+ return;
11906
+ }
11907
+
11908
+ return sendMessage(defineBotCommandData, {
11909
+ onResult: function (result) {
11910
+ callback && callback(result);
11911
+ }
11912
+ });
11913
+ };
11914
+
11915
+ this.startBot = function (params, callback) {
11916
+ var startBotData = {
11917
+ chatMessageVOType: chatMessageVOTypes.START_BOT,
11918
+ typeCode: params.typeCode,
11919
+ content: {},
11920
+ pushMsgType: 4,
11921
+ token: token
11922
+ };
11923
+
11924
+ if (params) {
11925
+ if (typeof +params.threadId !== 'number' || params.threadId < 0) {
11926
+ fireEvent('error', {
11927
+ code: 999,
11928
+ message: 'Enter a valid Thread Id for Bot to start in!'
11929
+ });
11930
+ return;
11931
+ }
11932
+
11933
+ if (typeof params.botName !== 'string' || params.botName.length == 0) {
11934
+ fireEvent('error', {
11935
+ code: 999,
11936
+ message: 'You need to insert a botName!'
11937
+ });
11938
+ return;
11939
+ }
11940
+
11941
+ startBotData.subjectId = +params.threadId;
11942
+
11943
+ startBotData.content = JSON.stringify({
11944
+ botName: params.botName.trim()
11945
+ });
11946
+
11947
+ } else {
11948
+ fireEvent('error', {
11949
+ code: 999,
11950
+ message: 'No params have been sent to create bot commands'
11951
+ });
11952
+ return;
11953
+ }
11954
+
11955
+ return sendMessage(startBotData, {
11956
+ onResult: function (result) {
11957
+ callback && callback(result);
11958
+ }
11959
+ });
11960
+ };
11961
+
11962
+ this.stopBot = function (params, callback) {
11963
+ var stopBotData = {
11964
+ chatMessageVOType: chatMessageVOTypes.STOP_BOT,
11965
+ typeCode: params.typeCode,
11966
+ content: {},
11967
+ pushMsgType: 4,
11968
+ token: token
11969
+ }, commandList = [];
11970
+
11971
+ if (params) {
11972
+ if (typeof +params.threadId !== 'number' || params.threadId < 0) {
11973
+ fireEvent('error', {
11974
+ code: 999,
11975
+ message: 'Enter a valid Thread Id for Bot to stop on!'
11976
+ });
11977
+ return;
11978
+ }
11979
+
11980
+ if (typeof params.botName !== 'string' || params.botName.length == 0) {
11981
+ fireEvent('error', {
11982
+ code: 999,
11983
+ message: 'You need to insert a botName!'
11984
+ });
11985
+ return;
11986
+ }
11987
+
11988
+ stopBotData.subjectId = +params.threadId;
11989
+
11990
+ stopBotData.content = JSON.stringify({
11991
+ botName: params.botName.trim()
11992
+ });
11993
+
11994
+ } else {
11995
+ fireEvent('error', {
11996
+ code: 999,
11997
+ message: 'No params have been sent to create bot commands'
11998
+ });
11999
+ return;
12000
+ }
12001
+
12002
+ return sendMessage(stopBotData, {
12003
+ onResult: function (result) {
12004
+ callback && callback(result);
12005
+ }
12006
+ });
12007
+ };
12008
+
12009
+ this.getBotCommandsList = function (params, callback) {
12010
+ var getBotCommandsListData = {
12011
+ chatMessageVOType: chatMessageVOTypes.BOT_COMMANDS,
12012
+ typeCode: params.typeCode,
12013
+ content: {},
12014
+ pushMsgType: 4,
12015
+ token: token
12016
+ };
10717
12017
 
10718
12018
  if (params) {
10719
- if (parseInt(params.id) > 0) {
10720
- data.id = parseInt(params.id);
10721
- } else {
12019
+ if (typeof params.botName !== 'string' || params.botName.length == 0) {
10722
12020
  fireEvent('error', {
10723
12021
  code: 999,
10724
- message: 'ID is required for Updating Contact!',
10725
- error: undefined
12022
+ message: 'You need to insert a botName!'
10726
12023
  });
12024
+ return;
10727
12025
  }
10728
12026
 
10729
- if (typeof params.firstName === 'string') {
10730
- data.firstName = params.firstName;
10731
- } else {
10732
- fireEvent('error', {
10733
- code: 999,
10734
- message: 'firstName is required for Updating Contact!'
10735
- });
12027
+ getBotCommandsListData.content = JSON.stringify({
12028
+ botName: params.botName.trim()
12029
+ });
12030
+
12031
+ } else {
12032
+ fireEvent('error', {
12033
+ code: 999,
12034
+ message: 'No params have been sent to get bot commands'
12035
+ });
12036
+ return;
12037
+ }
12038
+
12039
+ return sendMessage(getBotCommandsListData, {
12040
+ onResult: function (result) {
12041
+ callback && callback(result);
10736
12042
  }
12043
+ });
12044
+ };
10737
12045
 
10738
- if (typeof params.lastName === 'string') {
10739
- data.lastName = params.lastName;
10740
- } else {
12046
+ this.getThreadAllBots = function (params, callback) {
12047
+ var getThreadBotsData = {
12048
+ chatMessageVOType: chatMessageVOTypes.THREAD_ALL_BOTS,
12049
+ typeCode: params.typeCode,
12050
+ content: {},
12051
+ pushMsgType: 4,
12052
+ token: token
12053
+ };
12054
+
12055
+ if (params) {
12056
+ if (typeof +params.threadId !== 'number' || params.threadId < 0) {
10741
12057
  fireEvent('error', {
10742
12058
  code: 999,
10743
- message: 'lastName is required for Updating Contact!'
12059
+ message: 'Enter a valid Thread Id to get all Bots List!'
10744
12060
  });
12061
+ return;
10745
12062
  }
10746
12063
 
10747
- if (typeof params.cellphoneNumber === 'string') {
10748
- data.cellphoneNumber = params.cellphoneNumber;
10749
- } else {
10750
- fireEvent('error', {
10751
- code: 999,
10752
- message: 'cellphoneNumber is required for Updating Contact!'
10753
- });
12064
+ getThreadBotsData.subjectId = +params.threadId;
12065
+
12066
+ } else {
12067
+ fireEvent('error', {
12068
+ code: 999,
12069
+ message: 'No params have been sent to get thread\' bots list!'
12070
+ });
12071
+ return;
12072
+ }
12073
+
12074
+ return sendMessage(getThreadBotsData, {
12075
+ onResult: function (result) {
12076
+ callback && callback(result);
10754
12077
  }
12078
+ });
12079
+ };
10755
12080
 
10756
- if (typeof params.email === 'string') {
10757
- data.email = params.email;
10758
- } else {
10759
- fireEvent('error', {
10760
- code: 999,
10761
- message: 'email is required for Updating Contact!'
10762
- });
12081
+ this.mapReverse = function (params, callback) {
12082
+ var data = {};
12083
+
12084
+ if (params) {
12085
+ if (parseFloat(params.lat) > 0) {
12086
+ data.lat = params.lat;
12087
+ }
12088
+
12089
+ if (parseFloat(params.lng) > 0) {
12090
+ data.lng = params.lng;
10763
12091
  }
10764
12092
 
10765
12093
  data.uniqueId = Utility.generateUUID();
10766
12094
  }
10767
12095
 
10768
12096
  var requestParams = {
10769
- url: SERVICE_ADDRESSES.PLATFORM_ADDRESS +
10770
- SERVICES_PATH.UPDATE_CONTACTS,
12097
+ url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.REVERSE,
10771
12098
  method: 'GET',
10772
12099
  data: data,
10773
12100
  headers: {
10774
- '_token_': token,
10775
- '_token_issuer_': 1
12101
+ 'Api-Key': mapApiKey
10776
12102
  }
10777
12103
  };
10778
12104
 
@@ -10781,82 +12107,13 @@
10781
12107
  var responseData = JSON.parse(result.result.responseText);
10782
12108
 
10783
12109
  var returnData = {
10784
- hasError: responseData.hasError,
10785
- cache: false,
10786
- errorMessage: responseData.message,
10787
- errorCode: responseData.errorCode
12110
+ hasError: result.hasError,
12111
+ cache: result.cache,
12112
+ errorMessage: result.message,
12113
+ errorCode: result.errorCode,
12114
+ result: responseData
10788
12115
  };
10789
12116
 
10790
- if (!responseData.hasError) {
10791
- var messageContent = responseData.result,
10792
- messageLength = responseData.result.length,
10793
- resultData = {
10794
- contacts: [],
10795
- contentCount: messageLength
10796
- },
10797
- contactData;
10798
-
10799
- for (var i = 0; i < messageLength; i++) {
10800
- contactData = formatDataToMakeContact(messageContent[i]);
10801
- if (contactData) {
10802
- resultData.contacts.push(contactData);
10803
- }
10804
- }
10805
-
10806
- returnData.result = resultData;
10807
-
10808
- /**
10809
- * Add Contacts into cache database #cache
10810
- */
10811
- if (canUseCache && cacheSecret.length > 0) {
10812
- if (db) {
10813
- var cacheData = [];
10814
-
10815
- for (var i = 0; i < resultData.contacts.length; i++) {
10816
- try {
10817
- var tempData = {},
10818
- salt = Utility.generateUUID();
10819
- tempData.id = resultData.contacts[i].id;
10820
- tempData.owner = userInfo.id;
10821
- tempData.uniqueId = resultData.contacts[i].uniqueId;
10822
- tempData.userId = Utility.crypt(resultData.contacts[i].userId, cacheSecret, salt);
10823
- tempData.cellphoneNumber = Utility.crypt(resultData.contacts[i].cellphoneNumber, cacheSecret, salt);
10824
- tempData.email = Utility.crypt(resultData.contacts[i].email, cacheSecret, salt);
10825
- tempData.firstName = Utility.crypt(resultData.contacts[i].firstName, cacheSecret, salt);
10826
- tempData.lastName = Utility.crypt(resultData.contacts[i].lastName, cacheSecret, salt);
10827
- tempData.expireTime = new Date().getTime() + cacheExpireTime;
10828
- tempData.data = Utility.crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
10829
- tempData.salt = salt;
10830
-
10831
- cacheData.push(tempData);
10832
- } catch (error) {
10833
- fireEvent('error', {
10834
- code: error.code,
10835
- message: error.message,
10836
- error: error
10837
- });
10838
- }
10839
- }
10840
-
10841
- db.contacts.bulkPut(cacheData)
10842
- .catch(function (error) {
10843
- fireEvent('error', {
10844
- code: error.code,
10845
- message: error.message,
10846
- error: error
10847
- });
10848
- });
10849
- } else {
10850
- fireEvent('error', {
10851
- code: 6601,
10852
- message: CHAT_ERRORS[6601],
10853
- error: null
10854
- });
10855
- }
10856
- }
10857
-
10858
- }
10859
-
10860
12117
  callback && callback(returnData);
10861
12118
 
10862
12119
  } else {
@@ -10869,69 +12126,45 @@
10869
12126
  });
10870
12127
  };
10871
12128
 
10872
- this.removeContacts = function (params, callback) {
12129
+ this.mapSearch = function (params, callback) {
10873
12130
  var data = {};
10874
12131
 
10875
12132
  if (params) {
10876
- if (parseInt(params.id) > 0) {
10877
- data.id = parseInt(params.id);
10878
- } else {
10879
- fireEvent('error', {
10880
- code: 999,
10881
- message: 'ID is required for Deleting Contact!',
10882
- error: undefined
10883
- });
12133
+ if (typeof params.term === 'string') {
12134
+ data.term = params.term;
10884
12135
  }
10885
- }
10886
12136
 
10887
- var requestParams = {
10888
- url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.REMOVE_CONTACTS,
10889
- method: 'POST',
10890
- data: data,
10891
- headers: {
10892
- '_token_': token,
10893
- '_token_issuer_': 1
12137
+ if (parseFloat(params.lat) > 0) {
12138
+ data.lat = params.lat;
10894
12139
  }
10895
- };
10896
-
10897
- httpRequest(requestParams, function (result) {
10898
- if (!result.hasError) {
10899
- var responseData = JSON.parse(result.result.responseText);
10900
12140
 
10901
- var returnData = {
10902
- hasError: responseData.hasError,
10903
- cache: false,
10904
- errorMessage: responseData.message,
10905
- errorCode: responseData.errorCode
10906
- };
12141
+ if (parseFloat(params.lng) > 0) {
12142
+ data.lng = params.lng;
12143
+ }
10907
12144
 
10908
- if (!responseData.hasError) {
10909
- returnData.result = responseData.result;
10910
- }
12145
+ data.uniqueId = Utility.generateUUID();
12146
+ }
10911
12147
 
10912
- /**
10913
- * Remove the contact from cache
10914
- */
10915
- if (canUseCache) {
10916
- if (db) {
10917
- db.contacts.where('id')
10918
- .equals(parseInt(params.id))
10919
- .delete()
10920
- .catch(function (error) {
10921
- fireEvent('error', {
10922
- code: 6602,
10923
- message: CHAT_ERRORS[6602],
10924
- error: error
10925
- });
10926
- });
10927
- } else {
10928
- fireEvent('error', {
10929
- code: 6601,
10930
- message: CHAT_ERRORS[6601],
10931
- error: null
10932
- });
10933
- }
10934
- }
12148
+ var requestParams = {
12149
+ url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.SEARCH,
12150
+ method: 'GET',
12151
+ data: data,
12152
+ headers: {
12153
+ 'Api-Key': mapApiKey
12154
+ }
12155
+ };
12156
+
12157
+ httpRequest(requestParams, function (result) {
12158
+ if (!result.hasError) {
12159
+ var responseData = JSON.parse(result.result.responseText);
12160
+
12161
+ var returnData = {
12162
+ hasError: result.hasError,
12163
+ cache: result.cache,
12164
+ errorMessage: result.message,
12165
+ errorCode: result.errorCode,
12166
+ result: responseData
12167
+ };
10935
12168
 
10936
12169
  callback && callback(returnData);
10937
12170
 
@@ -10945,913 +12178,869 @@
10945
12178
  });
10946
12179
  };
10947
12180
 
10948
- this.searchContacts = function (params, callback) {
10949
- var data = {
10950
- size: 50,
10951
- offset: 0
10952
- },
10953
- whereClause = {},
10954
- returnCache = false;
12181
+ this.mapRouting = function (params, callback) {
12182
+ var data = {};
10955
12183
 
10956
12184
  if (params) {
10957
- if (typeof params.firstName === 'string') {
10958
- data.firstName = whereClause.firstName = params.firstName;
10959
- }
10960
-
10961
- if (typeof params.lastName === 'string') {
10962
- data.lastName = whereClause.lastName = params.lastName;
10963
- }
10964
-
10965
- if (parseInt(params.cellphoneNumber) > 0) {
10966
- data.cellphoneNumber = whereClause.cellphoneNumber = params.cellphoneNumber;
10967
- }
10968
-
10969
- if (typeof params.email === 'string') {
10970
- data.email = whereClause.email = params.email;
10971
- }
10972
-
10973
- if (typeof params.query === 'string') {
10974
- data.q = whereClause.q = params.query;
10975
- }
10976
-
10977
- if (typeof params.uniqueId === 'string') {
10978
- data.uniqueId = whereClause.uniqueId = params.uniqueId;
10979
- }
10980
-
10981
- if (parseInt(params.id) > 0) {
10982
- data.id = whereClause.id = params.id;
10983
- }
10984
-
10985
- if (parseInt(params.typeCode) > 0) {
10986
- data.typeCode = whereClause.typeCode = params.typeCode;
12185
+ if (typeof params.alternative === 'boolean') {
12186
+ data.alternative = params.alternative;
12187
+ } else {
12188
+ data.alternative = true;
10987
12189
  }
10988
12190
 
10989
- if (parseInt(params.size) > 0) {
10990
- data.size = params.size;
12191
+ if (typeof params.origin === 'object') {
12192
+ if (parseFloat(params.origin.lat) > 0 && parseFloat(params.origin.lng)) {
12193
+ data.origin = params.origin.lat + ',' + parseFloat(params.origin.lng);
12194
+ } else {
12195
+ console.log('No origin has been selected!');
12196
+ }
10991
12197
  }
10992
12198
 
10993
- if (parseInt(params.offset) > 0) {
10994
- data.offset = params.offset;
12199
+ if (typeof params.destination === 'object') {
12200
+ if (parseFloat(params.destination.lat) > 0 && parseFloat(params.destination.lng)) {
12201
+ data.destination = params.destination.lat + ',' + parseFloat(params.destination.lng);
12202
+ } else {
12203
+ console.log('No destination has been selected!');
12204
+ }
10995
12205
  }
10996
12206
 
10997
- var functionLevelCache = (typeof params.cache == 'boolean') ? params.cache : true;
12207
+ data.uniqueId = Utility.generateUUID();
10998
12208
  }
10999
12209
 
11000
12210
  var requestParams = {
11001
- url: SERVICE_ADDRESSES.PLATFORM_ADDRESS + SERVICES_PATH.SEARCH_CONTACTS,
11002
- method: 'POST',
12211
+ url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.ROUTING,
12212
+ method: 'GET',
11003
12213
  data: data,
11004
12214
  headers: {
11005
- '_token_': token,
11006
- '_token_issuer_': 1
12215
+ 'Api-Key': mapApiKey
11007
12216
  }
11008
12217
  };
11009
12218
 
11010
- /**
11011
- * Search contacts in cache #cache
11012
- */
11013
- if (functionLevelCache && canUseCache && cacheSecret.length > 0) {
11014
- if (db) {
11015
-
11016
- /**
11017
- * First of all we delete all contacts those
11018
- * expireTime has been expired. after that
11019
- * we query our cache database to retrieve
11020
- * what we wanted
11021
- */
11022
- db.contacts.where('expireTime')
11023
- .below(new Date().getTime())
11024
- .delete()
11025
- .then(function () {
11026
-
11027
- /**
11028
- * Query cache database to get contacts
11029
- */
11030
-
11031
- var thenAble;
11032
-
11033
- if (Object.keys(whereClause).length === 0) {
11034
- thenAble = db.contacts.where('owner')
11035
- .equals(parseInt(userInfo.id));
11036
- } else {
11037
- if (whereClause.hasOwnProperty('id')) {
11038
- thenAble = db.contacts.where('owner')
11039
- .equals(parseInt(userInfo.id))
11040
- .and(function (contact) {
11041
- return contact.id == whereClause.id;
11042
- });
11043
- } else if (whereClause.hasOwnProperty('uniqueId')) {
11044
- thenAble = db.contacts.where('owner')
11045
- .equals(parseInt(userInfo.id))
11046
- .and(function (contact) {
11047
- return contact.uniqueId == whereClause.uniqueId;
11048
- });
11049
- } else {
11050
- if (whereClause.hasOwnProperty('firstName')) {
11051
- thenAble = db.contacts.where('owner')
11052
- .equals(parseInt(userInfo.id))
11053
- .filter(function (contact) {
11054
- var reg = new RegExp(whereClause.firstName);
11055
- return reg.test(chatDecrypt(contact.firstName, cacheSecret, contact.salt));
11056
- });
11057
- }
11058
-
11059
- if (whereClause.hasOwnProperty('lastName')) {
11060
- thenAble = db.contacts.where('owner')
11061
- .equals(parseInt(userInfo.id))
11062
- .filter(function (contact) {
11063
- var reg = new RegExp(whereClause.lastName);
11064
- return reg.test(chatDecrypt(contact.lastName, cacheSecret, contact.salt));
11065
- });
11066
- }
11067
-
11068
- if (whereClause.hasOwnProperty('email')) {
11069
- thenAble = db.contacts.where('owner')
11070
- .equals(parseInt(userInfo.id))
11071
- .filter(function (contact) {
11072
- var reg = new RegExp(whereClause.email);
11073
- return reg.test(chatDecrypt(contact.email, cacheSecret, contact.salt));
11074
- });
11075
- }
12219
+ httpRequest(requestParams, function (result) {
12220
+ if (!result.hasError) {
12221
+ var responseData = JSON.parse(result.result.responseText);
11076
12222
 
11077
- if (whereClause.hasOwnProperty('q')) {
11078
- thenAble = db.contacts.where('owner')
11079
- .equals(parseInt(userInfo.id))
11080
- .filter(function (contact) {
11081
- var reg = new RegExp(whereClause.q);
11082
- return reg.test(chatDecrypt(contact.firstName, cacheSecret, contact.salt) + ' ' +
11083
- chatDecrypt(contact.lastName, cacheSecret, contact.salt) + ' ' +
11084
- chatDecrypt(contact.email, cacheSecret, contact.salt));
11085
- });
11086
- }
11087
- }
11088
- }
12223
+ var returnData = {
12224
+ hasError: result.hasError,
12225
+ cache: result.cache,
12226
+ errorMessage: result.message,
12227
+ errorCode: result.errorCode,
12228
+ result: responseData
12229
+ };
11089
12230
 
11090
- thenAble.offset(data.offset)
11091
- .limit(data.size)
11092
- .toArray()
11093
- .then(function (contacts) {
11094
- db.contacts.where('owner')
11095
- .equals(parseInt(userInfo.id))
11096
- .count()
11097
- .then(function (contactsCount) {
11098
- var cacheData = [];
12231
+ callback && callback(returnData);
11099
12232
 
11100
- for (var i = 0; i < contacts.length; i++) {
11101
- try {
11102
- var tempData = {},
11103
- salt = contacts[i].salt;
12233
+ } else {
12234
+ fireEvent('error', {
12235
+ code: result.errorCode,
12236
+ message: result.errorMessage,
12237
+ error: result
12238
+ });
12239
+ }
12240
+ });
12241
+ };
11104
12242
 
11105
- cacheData.push(formatDataToMakeContact(JSON.parse(chatDecrypt(contacts[i].data, cacheSecret, ontacts[i].salt))));
11106
- } catch (error) {
11107
- fireEvent('error', {
11108
- code: error.code,
11109
- message: error.message,
11110
- error: error
11111
- });
11112
- }
11113
- }
12243
+ this.mapStaticImage = function (params, callback) {
12244
+ var data = {},
12245
+ url = SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.STATIC_IMAGE,
12246
+ hasError = false;
11114
12247
 
11115
- var returnData = {
11116
- hasError: false,
11117
- cache: true,
11118
- errorCode: 0,
11119
- errorMessage: '',
11120
- result: {
11121
- contacts: cacheData,
11122
- contentCount: contactsCount,
11123
- hasNext: !(contacts.length < data.size),
11124
- nextOffset: data.offset + contacts.length
11125
- }
11126
- };
12248
+ if (params) {
12249
+ if (typeof params.type === 'string') {
12250
+ data.type = params.type;
12251
+ } else {
12252
+ data.type = 'standard-night';
12253
+ }
11127
12254
 
11128
- if (cacheData.length > 0) {
11129
- callback && callback(returnData);
11130
- callback = undefined;
11131
- returnCache = true;
11132
- }
11133
- })
11134
- .catch(function (error) {
11135
- fireEvent('error', {
11136
- code: error.code,
11137
- message: error.message,
11138
- error: error
11139
- });
11140
- });
11141
- })
11142
- .catch(function (error) {
11143
- fireEvent('error', {
11144
- code: error.code,
11145
- message: error.message,
11146
- error: error
11147
- });
11148
- });
11149
- })
11150
- .catch(function (error) {
11151
- fireEvent('error', {
11152
- code: error.code,
11153
- message: error.message,
11154
- error: error
11155
- });
12255
+ if (parseInt(params.zoom) > 0) {
12256
+ data.zoom = params.zoom;
12257
+ } else {
12258
+ data.zoom = 15;
12259
+ }
12260
+
12261
+ if (parseInt(params.width) > 0) {
12262
+ data.width = params.width;
12263
+ } else {
12264
+ data.width = 800;
12265
+ }
12266
+
12267
+ if (parseInt(params.height) > 0) {
12268
+ data.height = params.height;
12269
+ } else {
12270
+ data.height = 600;
12271
+ }
12272
+
12273
+ if (typeof params.center === 'object') {
12274
+ if (parseFloat(params.center.lat) > 0 && parseFloat(params.center.lng)) {
12275
+ data.center = params.center.lat + ',' + parseFloat(params.center.lng);
12276
+ } else {
12277
+ hasError = true;
12278
+ fireEvent('error', {
12279
+ code: 6700,
12280
+ message: CHAT_ERRORS[6700],
12281
+ error: undefined
11156
12282
  });
12283
+ }
11157
12284
  } else {
12285
+ hasError = true;
11158
12286
  fireEvent('error', {
11159
- code: 6601,
11160
- message: CHAT_ERRORS[6601],
11161
- error: null
12287
+ code: 6700,
12288
+ message: CHAT_ERRORS[6700],
12289
+ error: undefined
11162
12290
  });
11163
12291
  }
12292
+
12293
+ data.key = mapApiKey;
11164
12294
  }
11165
12295
 
11166
- /**
11167
- * Get Search Contacts Result From Server
11168
- */
11169
- httpRequest(requestParams, function (result) {
11170
- if (!result.hasError) {
11171
- var responseData = JSON.parse(result.result.responseText);
12296
+ var keys = Object.keys(data);
11172
12297
 
11173
- var returnData = {
11174
- hasError: responseData.hasError,
11175
- cache: false,
11176
- errorMessage: responseData.message,
11177
- errorCode: responseData.errorCode
11178
- };
12298
+ if (keys.length > 0) {
12299
+ url += '?';
11179
12300
 
11180
- if (!responseData.hasError) {
11181
- var messageContent = responseData.result,
11182
- messageLength = responseData.result.length,
11183
- resultData = {
11184
- contacts: [],
11185
- contentCount: messageLength
11186
- },
11187
- contactData;
12301
+ for (var i = 0; i < keys.length; i++) {
12302
+ var key = keys[i];
12303
+ url += key + '=' + data[key];
12304
+ if (i < keys.length - 1) {
12305
+ url += '&';
12306
+ }
12307
+ }
12308
+ }
11188
12309
 
11189
- for (var i = 0; i < messageLength; i++) {
11190
- contactData = formatDataToMakeContact(messageContent[i]);
11191
- if (contactData) {
11192
- resultData.contacts.push(contactData);
11193
- }
11194
- }
12310
+ var returnData = {
12311
+ hasError: hasError,
12312
+ cache: false,
12313
+ errorMessage: (hasError) ? CHAT_ERRORS[6700] : '',
12314
+ errorCode: (hasError) ? 6700 : undefined,
12315
+ result: {
12316
+ link: (!hasError) ? url : ''
12317
+ }
12318
+ };
11195
12319
 
11196
- returnData.result = resultData;
12320
+ callback && callback(returnData);
12321
+ };
11197
12322
 
11198
- /**
11199
- * Add Contacts into cache database #cache
11200
- */
11201
- if (canUseCache && cacheSecret.length > 0) {
11202
- if (db) {
11203
- var cacheData = [];
12323
+ this.setAdmin = function (params, callback) {
12324
+ setRoleToUser(params, callback);
12325
+ };
11204
12326
 
11205
- for (var i = 0; i < resultData.contacts.length; i++) {
11206
- try {
11207
- var tempData = {},
11208
- salt = Utility.generateUUID();
12327
+ this.removeAdmin = function (params, callback) {
12328
+ removeRoleFromUser(params, callback);
12329
+ };
11209
12330
 
11210
- tempData.id = resultData.contacts[i].id;
11211
- tempData.owner = userInfo.id;
11212
- tempData.uniqueId = resultData.contacts[i].uniqueId;
11213
- tempData.userId = Utility.crypt(resultData.contacts[i].userId, cacheSecret, salt);
11214
- tempData.cellphoneNumber = Utility.crypt(resultData.contacts[i].cellphoneNumber, cacheSecret, salt);
11215
- tempData.email = Utility.crypt(resultData.contacts[i].email, cacheSecret, salt);
11216
- tempData.firstName = Utility.crypt(resultData.contacts[i].firstName, cacheSecret, salt);
11217
- tempData.lastName = Utility.crypt(resultData.contacts[i].lastName, cacheSecret, salt);
11218
- tempData.expireTime = new Date().getTime() + cacheExpireTime;
11219
- tempData.data = crypt(JSON.stringify(unsetNotSeenDuration(resultData.contacts[i])), cacheSecret, salt);
11220
- tempData.salt = salt;
12331
+ this.setAuditor = function (params, callback) {
12332
+ setRoleToUser(params, callback);
12333
+ };
11221
12334
 
11222
- cacheData.push(tempData);
11223
- } catch (error) {
11224
- fireEvent('error', {
11225
- code: error.code,
11226
- message: error.message,
11227
- error: error
11228
- });
11229
- }
11230
- }
12335
+ this.removeAuditor = function (params, callback) {
12336
+ removeRoleFromUser(params, callback);
12337
+ };
11231
12338
 
11232
- db.contacts.bulkPut(cacheData)
11233
- .catch(function (error) {
11234
- fireEvent('error', {
11235
- code: error.code,
11236
- message: error.message,
11237
- error: error
11238
- });
11239
- });
11240
- } else {
11241
- fireEvent('error', {
11242
- code: 6601,
11243
- message: CHAT_ERRORS[6601],
11244
- error: null
11245
- });
11246
- }
11247
- }
11248
- }
12339
+ this.clearChatServerCaches = clearChatServerCaches;
11249
12340
 
11250
- callback && callback(returnData);
11251
- /**
11252
- * Delete callback so if server pushes response before
11253
- * cache, cache won't send data again
11254
- */
11255
- callback = undefined;
12341
+ this.deleteCacheDatabases = deleteCacheDatabases;
11256
12342
 
11257
- if (!returnData.hasError && returnCache) {
11258
- fireEvent('contactEvents', {
11259
- type: 'CONTACTS_SEARCH_RESULT_CHANGE',
11260
- result: returnData.result
11261
- });
11262
- }
12343
+ this.clearCacheDatabasesOfUser = clearCacheDatabasesOfUser;
12344
+
12345
+ this.getChatState = function () {
12346
+ return chatFullStateObject;
12347
+ };
12348
+
12349
+ this.reconnect = function () {
12350
+ asyncClient.reconnectSocket();
12351
+ };
12352
+
12353
+ this.setToken = function (newToken) {
12354
+ if (typeof newToken != 'undefined') {
12355
+ token = newToken;
12356
+ }
12357
+ };
12358
+
12359
+ this.generateUUID = Utility.generateUUID;
12360
+
12361
+
12362
+ /** Call public methods */
12363
+ this.startRecordingCall = function (params, callback) {
12364
+ var recordCallData = {
12365
+ chatMessageVOType: chatMessageVOTypes.RECORD_CALL,
12366
+ typeCode: params.typeCode,
12367
+ pushMsgType: 3,
12368
+ token: token,
12369
+ content: {}
12370
+ };
12371
+
12372
+ if (params) {
12373
+ if (typeof +params.callId === 'number' && params.callId > 0) {
12374
+ recordCallData.subjectId = +params.callId;
11263
12375
  } else {
11264
12376
  fireEvent('error', {
11265
- code: result.errorCode,
11266
- message: result.errorMessage,
11267
- error: result
12377
+ code: 999,
12378
+ message: 'Invalid Call id!'
11268
12379
  });
12380
+ return;
12381
+ }
12382
+
12383
+ if(params.destinated === true) {
12384
+ recordCallData.chatMessageVOType = chatMessageVOTypes.DESTINATED_RECORD_CALL;
12385
+ recordCallData.content.recordType = typeof +params.recordType === 'number' ? params.recordType : 1;
12386
+ recordCallData.content.tags = Array.isArray(params.tags) ? params.tags : null;
12387
+ recordCallData.content.threadId = typeof +params.threadId === 'number' ? params.threadId : null;
12388
+ }
12389
+ } else {
12390
+ fireEvent('error', {
12391
+ code: 999,
12392
+ message: 'No params have been sent to Record call!'
12393
+ });
12394
+ return;
12395
+ }
12396
+
12397
+ return sendMessage(recordCallData, {
12398
+ onResult: function (result) {
12399
+ callback && callback(result);
11269
12400
  }
11270
12401
  });
11271
12402
  };
11272
12403
 
11273
- this.createBot = function (params, callback) {
11274
- var createBotData = {
11275
- chatMessageVOType: chatMessageVOTypes.CREATE_BOT,
12404
+ this.stopRecordingCall = function (params, callback) {
12405
+ var stopRecordingCallData = {
12406
+ chatMessageVOType: chatMessageVOTypes.END_RECORD_CALL,
11276
12407
  typeCode: params.typeCode,
11277
- content: '',
11278
- pushMsgType: 4,
12408
+ pushMsgType: 3,
11279
12409
  token: token
11280
12410
  };
11281
12411
 
11282
12412
  if (params) {
11283
- if (typeof params.botName === 'string' && params.botName.length > 0) {
11284
- if (params.botName.substr(-3) === "BOT") {
11285
- createBotData.content = params.botName;
11286
- } else {
11287
- fireEvent('error', {
11288
- code: 999,
11289
- message: 'Bot name should end in "BOT", ex. "testBOT"'
11290
- });
11291
- return;
11292
- }
12413
+ if (typeof +params.callId === 'number' && params.callId > 0) {
12414
+ stopRecordingCallData.subjectId = +params.callId;
11293
12415
  } else {
11294
12416
  fireEvent('error', {
11295
12417
  code: 999,
11296
- message: 'Insert a bot name to create one!'
12418
+ message: 'Invalid Call id!'
11297
12419
  });
11298
12420
  return;
11299
12421
  }
11300
12422
  } else {
11301
12423
  fireEvent('error', {
11302
12424
  code: 999,
11303
- message: 'Insert a bot name to create one!'
12425
+ message: 'No params have been sent to Stop Recording the call!'
11304
12426
  });
11305
12427
  return;
11306
12428
  }
11307
12429
 
11308
- return sendMessage(createBotData, {
12430
+ return sendMessage(stopRecordingCallData, {
11309
12431
  onResult: function (result) {
11310
12432
  callback && callback(result);
11311
12433
  }
11312
12434
  });
11313
12435
  };
11314
12436
 
11315
- this.defineBotCommand = function (params, callback) {
11316
- var defineBotCommandData = {
11317
- chatMessageVOType: chatMessageVOTypes.DEFINE_BOT_COMMAND,
12437
+ this.startCall = function (params, callback) {
12438
+ var startCallData = {
12439
+ chatMessageVOType: chatMessageVOTypes.CALL_REQUEST,
11318
12440
  typeCode: params.typeCode,
11319
- content: {},
11320
- pushMsgType: 4,
12441
+ pushMsgType: 3,
11321
12442
  token: token
11322
- }, commandList = [];
12443
+ }, content = {
12444
+ creatorClientDto: {}
12445
+ };
11323
12446
 
11324
12447
  if (params) {
11325
- if (typeof params.botName !== 'string' || params.botName.length == 0) {
11326
- fireEvent('error', {
11327
- code: 999,
11328
- message: 'You need to insert a botName!'
11329
- });
11330
- return;
12448
+ /*if (typeof params.type === 'string' && callTypes.hasOwnProperty(params.type.toUpperCase())) {
12449
+ content.type = callTypes[params.type.toUpperCase()];
12450
+ } else {
12451
+ content.type = 0x0; // Defaults to AUDIO Call
12452
+ }*/
12453
+
12454
+ content.type = 0x0;
12455
+
12456
+ //TODO: Check for mute
12457
+ content.creatorClientDto.mute = true;//(params.mute && typeof params.mute === 'boolean') ? params.mute : false;
12458
+ content.mute = true;//(params.mute && typeof params.mute === 'boolean') ? params.mute : false;
12459
+
12460
+ if (params.clientType
12461
+ && typeof params.clientType === 'string'
12462
+ && callClientType[params.clientType.toUpperCase()] > 0) {
12463
+ content.creatorClientDto.clientType = callClientType[params.clientType.toUpperCase()];
12464
+ } else {
12465
+ content.creatorClientDto.clientType = callClientType.NODE;
11331
12466
  }
11332
12467
 
11333
- if (!Array.isArray(params.commandList) || !params.commandList.length) {
11334
- fireEvent('error', {
11335
- code: 999,
11336
- message: 'Bot Commands List has to be an array of strings.'
11337
- });
11338
- return;
12468
+ if (typeof +params.threadId === 'number' && +params.threadId > 0) {
12469
+ content.threadId = +params.threadId;
11339
12470
  } else {
11340
- for (var i = 0; i < params.commandList.length; i++) {
11341
- commandList.push('/' + params.commandList[i].trim());
12471
+ if (Array.isArray(params.invitees)) {
12472
+ content.invitees = params.invitees;
12473
+ } else {
12474
+ fireEvent('error', {
12475
+ code: 999,
12476
+ message: 'Invitees list is empty! Send an array of invitees to start a call with, Or send a Thread Id to start a call with current participants'
12477
+ });
12478
+ return;
11342
12479
  }
11343
12480
  }
11344
12481
 
11345
- defineBotCommandData.content = {
11346
- botName: params.botName.trim(),
11347
- commandList: commandList
11348
- };
11349
-
12482
+ startCallData.content = JSON.stringify(content);
11350
12483
  } else {
11351
12484
  fireEvent('error', {
11352
12485
  code: 999,
11353
- message: 'No params have been sent to create bot commands'
12486
+ message: 'No params have been sent to start call!'
11354
12487
  });
11355
12488
  return;
11356
12489
  }
11357
12490
 
11358
- return sendMessage(defineBotCommandData, {
12491
+ callRequestController.callRequestReceived = true;
12492
+ callRequestController.callEstablishedInMySide = true;
12493
+
12494
+ return sendMessage(startCallData, {
11359
12495
  onResult: function (result) {
11360
12496
  callback && callback(result);
11361
12497
  }
11362
12498
  });
11363
12499
  };
11364
12500
 
11365
- this.startBot = function (params, callback) {
11366
- var startBotData = {
11367
- chatMessageVOType: chatMessageVOTypes.START_BOT,
12501
+ this.startGroupCall = function (params, callback) {
12502
+ var startCallData = {
12503
+ chatMessageVOType: chatMessageVOTypes.GROUP_CALL_REQUEST,
11368
12504
  typeCode: params.typeCode,
11369
- content: {},
11370
- pushMsgType: 4,
12505
+ pushMsgType: 3,
11371
12506
  token: token
12507
+ }, content = {
12508
+ creatorClientDto: {}
11372
12509
  };
11373
12510
 
11374
12511
  if (params) {
11375
- if (typeof +params.threadId !== 'number' || params.threadId < 0) {
11376
- fireEvent('error', {
11377
- code: 999,
11378
- message: 'Enter a valid Thread Id for Bot to start in!'
11379
- });
11380
- return;
11381
- }
12512
+ /*if (typeof params.type === 'string' && callTypes.hasOwnProperty(params.type.toUpperCase())) {
12513
+ content.type = callTypes[params.type.toUpperCase()];
12514
+ } else {
12515
+ content.type = 0x0; // Defaults to AUDIO Call
12516
+ }*/
12517
+ content.type = 0x0;
11382
12518
 
11383
- if (typeof params.botName !== 'string' || params.botName.length == 0) {
11384
- fireEvent('error', {
11385
- code: 999,
11386
- message: 'You need to insert a botName!'
11387
- });
11388
- return;
12519
+ content.creatorClientDto.mute = true;//(typeof params.mute === 'boolean') ? params.mute : false;
12520
+ content.mute = true;//(params.mute && typeof params.mute === 'boolean') ? params.mute : false;
12521
+
12522
+ if (params.clientType && typeof params.clientType === 'string' && callClientType[params.clientType.toUpperCase()] > 0) {
12523
+ content.creatorClientDto.clientType = callClientType[params.clientType.toUpperCase()];
12524
+ } else {
12525
+ content.creatorClientDto.clientType = callClientType.NODE;
11389
12526
  }
11390
12527
 
11391
- startBotData.subjectId = +params.threadId;
12528
+ if (typeof +params.threadId === 'number' && params.threadId > 0) {
12529
+ content.threadId = +params.threadId;
12530
+ } else {
12531
+ if (Array.isArray(params.invitees)) {
12532
+ content.invitees = params.invitees;
11392
12533
 
11393
- startBotData.content = JSON.stringify({
11394
- botName: params.botName.trim()
11395
- });
12534
+ } else {
12535
+ fireEvent('error', {
12536
+ code: 999,
12537
+ message: 'Invitees list is empty! Send an array of invitees to start a call with, Or send a Thread Id to start a call with current participants'
12538
+ });
12539
+ return;
12540
+ }
12541
+ }
11396
12542
 
12543
+ startCallData.content = JSON.stringify(content);
11397
12544
  } else {
11398
12545
  fireEvent('error', {
11399
12546
  code: 999,
11400
- message: 'No params have been sent to create bot commands'
12547
+ message: 'No params have been sent to start call!'
11401
12548
  });
11402
12549
  return;
11403
12550
  }
11404
12551
 
11405
- return sendMessage(startBotData, {
12552
+ callRequestController.callRequestReceived = true;
12553
+ callRequestController.callEstablishedInMySide = true;
12554
+
12555
+ return sendMessage(startCallData, {
11406
12556
  onResult: function (result) {
11407
12557
  callback && callback(result);
11408
12558
  }
11409
12559
  });
11410
12560
  };
11411
12561
 
11412
- this.stopBot = function (params, callback) {
11413
- var stopBotData = {
11414
- chatMessageVOType: chatMessageVOTypes.STOP_BOT,
12562
+ this.callReceived = callReceived;
12563
+
12564
+ this.terminateCall = function (params, callback) {
12565
+ var terminateCallData = {
12566
+ chatMessageVOType: chatMessageVOTypes.TERMINATE_CALL,
11415
12567
  typeCode: params.typeCode,
11416
- content: {},
11417
- pushMsgType: 4,
12568
+ pushMsgType: 3,
11418
12569
  token: token
11419
- }, commandList = [];
12570
+ }, content = {};
11420
12571
 
11421
12572
  if (params) {
11422
- if (typeof +params.threadId !== 'number' || params.threadId < 0) {
11423
- fireEvent('error', {
11424
- code: 999,
11425
- message: 'Enter a valid Thread Id for Bot to stop on!'
11426
- });
11427
- return;
11428
- }
11429
-
11430
- if (typeof params.botName !== 'string' || params.botName.length == 0) {
12573
+ if (typeof +params.callId === 'number' && params.callId > 0) {
12574
+ terminateCallData.subjectId = +params.callId;
12575
+ } else {
11431
12576
  fireEvent('error', {
11432
12577
  code: 999,
11433
- message: 'You need to insert a botName!'
12578
+ message: 'Invalid call id!'
11434
12579
  });
11435
12580
  return;
11436
12581
  }
11437
12582
 
11438
- stopBotData.subjectId = +params.threadId;
11439
-
11440
- stopBotData.content = JSON.stringify({
11441
- botName: params.botName.trim()
11442
- });
11443
-
12583
+ terminateCallData.content = JSON.stringify(content);
11444
12584
  } else {
11445
12585
  fireEvent('error', {
11446
12586
  code: 999,
11447
- message: 'No params have been sent to create bot commands'
12587
+ message: 'No params have been sent to terminate the call!'
11448
12588
  });
11449
12589
  return;
11450
12590
  }
11451
12591
 
11452
- return sendMessage(stopBotData, {
12592
+ return sendMessage(terminateCallData, {
11453
12593
  onResult: function (result) {
11454
12594
  callback && callback(result);
11455
12595
  }
11456
12596
  });
11457
12597
  };
11458
12598
 
11459
- this.getBotCommandsList = function (params, callback) {
11460
- var getBotCommandsListData = {
11461
- chatMessageVOType: chatMessageVOTypes.BOT_COMMANDS,
12599
+ this.acceptCall = function (params, callback) {
12600
+ var acceptCallData = {
12601
+ chatMessageVOType: chatMessageVOTypes.ACCEPT_CALL,
11462
12602
  typeCode: params.typeCode,
11463
- content: {},
11464
- pushMsgType: 4,
12603
+ pushMsgType: 3,
11465
12604
  token: token
11466
- };
12605
+ }, content = {};
11467
12606
 
11468
12607
  if (params) {
11469
- if (typeof params.botName !== 'string' || params.botName.length == 0) {
12608
+ if (typeof +params.callId === 'number' && params.callId > 0) {
12609
+ acceptCallData.subjectId = +params.callId;
12610
+ } else {
11470
12611
  fireEvent('error', {
11471
12612
  code: 999,
11472
- message: 'You need to insert a botName!'
12613
+ message: 'Invalid call id!'
11473
12614
  });
11474
12615
  return;
11475
12616
  }
11476
12617
 
11477
- getBotCommandsListData.content = JSON.stringify({
11478
- botName: params.botName.trim()
11479
- });
12618
+ content.mute = true;//(typeof params.mute === 'boolean') ? params.mute : false;
12619
+
12620
+ content.video = false;//(typeof params.video === 'boolean') ? params.video : false;
11480
12621
 
12622
+ content.videoCall = false;// content.video;
12623
+
12624
+ if (params.clientType && typeof params.clientType === 'string' && callClientType[params.clientType.toUpperCase()] > 0) {
12625
+ content.clientType = callClientType[params.clientType.toUpperCase()];
12626
+ } else {
12627
+ content.clientType = callClientType.NODE;
12628
+ }
12629
+
12630
+ acceptCallData.content = JSON.stringify(content);
11481
12631
  } else {
11482
12632
  fireEvent('error', {
11483
12633
  code: 999,
11484
- message: 'No params have been sent to get bot commands'
12634
+ message: 'No params have been sent to accept the call!'
11485
12635
  });
11486
12636
  return;
11487
12637
  }
11488
-
11489
- return sendMessage(getBotCommandsListData, {
12638
+ callRequestController.callEstablishedInMySide = true;
12639
+ return sendMessage(acceptCallData, {
11490
12640
  onResult: function (result) {
11491
12641
  callback && callback(result);
11492
12642
  }
11493
12643
  });
11494
12644
  };
11495
12645
 
11496
- this.getThreadAllBots = function (params, callback) {
11497
- var getThreadBotsData = {
11498
- chatMessageVOType: chatMessageVOTypes.THREAD_ALL_BOTS,
12646
+ this.rejectCall = this.cancelCall = function (params, callback) {
12647
+ var rejectCallData = {
12648
+ chatMessageVOType: chatMessageVOTypes.REJECT_CALL,
11499
12649
  typeCode: params.typeCode,
11500
- content: {},
11501
- pushMsgType: 4,
12650
+ pushMsgType: 3,
11502
12651
  token: token
11503
12652
  };
11504
12653
 
11505
12654
  if (params) {
11506
- if (typeof +params.threadId !== 'number' || params.threadId < 0) {
12655
+ if (typeof +params.callId === 'number' && params.callId > 0) {
12656
+ rejectCallData.subjectId = +params.callId;
12657
+ } else {
11507
12658
  fireEvent('error', {
11508
12659
  code: 999,
11509
- message: 'Enter a valid Thread Id to get all Bots List!'
12660
+ message: 'Invalid call id!'
11510
12661
  });
11511
12662
  return;
11512
12663
  }
11513
-
11514
- getThreadBotsData.subjectId = +params.threadId;
11515
-
11516
12664
  } else {
11517
12665
  fireEvent('error', {
11518
12666
  code: 999,
11519
- message: 'No params have been sent to get thread\' bots list!'
12667
+ message: 'No params have been sent to reject the call!'
11520
12668
  });
11521
12669
  return;
11522
12670
  }
11523
12671
 
11524
- return sendMessage(getThreadBotsData, {
12672
+ return sendMessage(rejectCallData, {
11525
12673
  onResult: function (result) {
11526
12674
  callback && callback(result);
11527
12675
  }
11528
12676
  });
11529
12677
  };
11530
12678
 
11531
- this.mapReverse = function (params, callback) {
11532
- var data = {};
12679
+ this.endCall = endCall;
12680
+
12681
+ this.getCallsList = function (params, callback) {
12682
+ var getCallListData = {
12683
+ chatMessageVOType: chatMessageVOTypes.GET_CALLS,
12684
+ typeCode: params.typeCode,
12685
+ pushMsgType: 3,
12686
+ token: token
12687
+ }, content = {};
11533
12688
 
11534
12689
  if (params) {
11535
- if (parseFloat(params.lat) > 0) {
11536
- data.lat = params.lat;
12690
+ if (typeof params.count === 'number' && params.count >= 0) {
12691
+ content.count = +params.count;
12692
+ } else {
12693
+ content.count = 50;
11537
12694
  }
11538
12695
 
11539
- if (parseFloat(params.lng) > 0) {
11540
- data.lng = params.lng;
12696
+ if (typeof params.offset === 'number' && params.offset >= 0) {
12697
+ content.offset = +params.offset;
12698
+ } else {
12699
+ content.offset = 0;
11541
12700
  }
11542
12701
 
11543
- data.uniqueId = Utility.generateUUID();
11544
- }
11545
-
11546
- var requestParams = {
11547
- url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.REVERSE,
11548
- method: 'GET',
11549
- data: data,
11550
- headers: {
11551
- 'Api-Key': mapApiKey
12702
+ if (typeof params.creatorCoreUserId === 'number' && params.creatorCoreUserId > 0) {
12703
+ content.creatorCoreUserId = +params.creatorCoreUserId;
11552
12704
  }
11553
- };
11554
-
11555
- httpRequest(requestParams, function (result) {
11556
- if (!result.hasError) {
11557
- var responseData = JSON.parse(result.result.responseText);
11558
-
11559
- var returnData = {
11560
- hasError: result.hasError,
11561
- cache: result.cache,
11562
- errorMessage: result.message,
11563
- errorCode: result.errorCode,
11564
- result: responseData
11565
- };
11566
12705
 
11567
- callback && callback(returnData);
12706
+ if (typeof params.creatorSsoId === 'number' && params.creatorSsoId > 0) {
12707
+ content.creatorSsoId = +params.creatorSsoId;
12708
+ }
11568
12709
 
11569
- } else {
11570
- fireEvent('error', {
11571
- code: result.errorCode,
11572
- message: result.errorMessage,
11573
- error: result
11574
- });
12710
+ if (typeof params.name === 'string') {
12711
+ content.name = params.name;
11575
12712
  }
11576
- });
11577
- };
11578
12713
 
11579
- this.mapSearch = function (params, callback) {
11580
- var data = {};
12714
+ if (typeof params.type === 'string' && callTypes.hasOwnProperty(params.type.toUpperCase())) {
12715
+ content.type = callTypes[params.type.toUpperCase()];
12716
+ }
11581
12717
 
11582
- if (params) {
11583
- if (typeof params.term === 'string') {
11584
- data.term = params.term;
12718
+ if (Array.isArray(params.callIds)) {
12719
+ content.callIds = params.callIds;
11585
12720
  }
11586
12721
 
11587
- if (parseFloat(params.lat) > 0) {
11588
- data.lat = params.lat;
12722
+ if (typeof params.contactType === 'string') {
12723
+ content.contactType = params.contactType;
11589
12724
  }
11590
12725
 
11591
- if (parseFloat(params.lng) > 0) {
11592
- data.lng = params.lng;
12726
+ if (typeof params.uniqueId === 'string') {
12727
+ content.uniqueId = params.uniqueId;
11593
12728
  }
11594
12729
 
11595
- data.uniqueId = Utility.generateUUID();
12730
+ getCallListData.content = JSON.stringify(content);
12731
+ } else {
12732
+ fireEvent('error', {
12733
+ code: 999,
12734
+ message: 'No params have been sent to End the call!'
12735
+ });
12736
+ return;
11596
12737
  }
11597
12738
 
11598
- var requestParams = {
11599
- url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.SEARCH,
11600
- method: 'GET',
11601
- data: data,
11602
- headers: {
11603
- 'Api-Key': mapApiKey
12739
+ return sendMessage(getCallListData, {
12740
+ onResult: function (result) {
12741
+ callback && callback(result);
11604
12742
  }
12743
+ });
12744
+ };
12745
+
12746
+ this.getCallParticipants = function (params, callback) {
12747
+ var sendMessageParams = {
12748
+ chatMessageVOType: chatMessageVOTypes.ACTIVE_CALL_PARTICIPANTS,
12749
+ typeCode: params.typeCode,
12750
+ content: {}
11605
12751
  };
11606
12752
 
11607
- httpRequest(requestParams, function (result) {
11608
- if (!result.hasError) {
11609
- var responseData = JSON.parse(result.result.responseText);
12753
+ if (params) {
12754
+ if (isNaN(params.callId)) {
12755
+ fireEvent('error', {
12756
+ code: 999,
12757
+ message: 'Call Id should be a valid number!'
12758
+ });
12759
+ return;
12760
+ } else {
12761
+ var callId = +params.callId;
12762
+ sendMessageParams.subjectId = callId;
11610
12763
 
11611
- var returnData = {
11612
- hasError: result.hasError,
11613
- cache: result.cache,
11614
- errorMessage: result.message,
11615
- errorCode: result.errorCode,
11616
- result: responseData
11617
- };
12764
+ var offset = (parseInt(params.offset) > 0)
12765
+ ? parseInt(params.offset)
12766
+ : 0,
12767
+ count = (parseInt(params.count) > 0)
12768
+ ? parseInt(params.count)
12769
+ : config.getHistoryCount;
11618
12770
 
11619
- callback && callback(returnData);
12771
+ sendMessageParams.content.count = count;
12772
+ sendMessageParams.content.offset = offset;
11620
12773
 
11621
- } else {
11622
- fireEvent('error', {
11623
- code: result.errorCode,
11624
- message: result.errorMessage,
11625
- error: result
12774
+ return sendMessage(sendMessageParams, {
12775
+ onResult: function (result) {
12776
+ var returnData = {
12777
+ hasError: result.hasError,
12778
+ cache: false,
12779
+ errorMessage: result.errorMessage,
12780
+ errorCode: result.errorCode
12781
+ };
12782
+
12783
+ if (!returnData.hasError) {
12784
+ var messageContent = result.result,
12785
+ messageLength = messageContent.length,
12786
+ resultData = {
12787
+ participants: reformatCallParticipants(messageContent),
12788
+ contentCount: result.contentCount,
12789
+ hasNext: (sendMessageParams.content.offset + sendMessageParams.content.count < result.contentCount && messageLength > 0),
12790
+ nextOffset: sendMessageParams.content.offset * 1 + messageLength * 1
12791
+ };
12792
+
12793
+ returnData.result = resultData;
12794
+ }
12795
+
12796
+ callback && callback(returnData);
12797
+ /**
12798
+ * Delete callback so if server pushes response before
12799
+ * cache, cache won't send data again
12800
+ */
12801
+ callback = undefined;
12802
+
12803
+ if (!returnData.hasError) {
12804
+ fireEvent('callEvents', {
12805
+ type: 'CALL_PARTICIPANTS_LIST_CHANGE',
12806
+ threadId: callId,
12807
+ result: returnData.result
12808
+ });
12809
+ }
12810
+ }
11626
12811
  });
11627
12812
  }
11628
- });
12813
+ } else {
12814
+ fireEvent('error', {
12815
+ code: 999,
12816
+ message: 'No params have been sent to Get Call Participants!'
12817
+ });
12818
+ return;
12819
+ }
11629
12820
  };
11630
12821
 
11631
- this.mapRouting = function (params, callback) {
11632
- var data = {};
12822
+ this.addCallParticipants = function (params, callback) {
12823
+ /**
12824
+ * + AddCallParticipantsRequest {object}
12825
+ * - subjectId {int}
12826
+ * + content {list} List of CONTACT IDs or inviteeVO Objects
12827
+ * - uniqueId {string}
12828
+ */
12829
+
12830
+ var sendMessageParams = {
12831
+ chatMessageVOType: chatMessageVOTypes.ADD_CALL_PARTICIPANT,
12832
+ typeCode: params.typeCode,
12833
+ content: []
12834
+ };
11633
12835
 
11634
12836
  if (params) {
11635
- if (typeof params.alternative === 'boolean') {
11636
- data.alternative = params.alternative;
11637
- } else {
11638
- data.alternative = true;
12837
+ if (typeof params.callId === 'number' && params.callId > 0) {
12838
+ sendMessageParams.subjectId = params.callId;
11639
12839
  }
11640
12840
 
11641
- if (typeof params.origin === 'object') {
11642
- if (parseFloat(params.origin.lat) > 0 && parseFloat(params.origin.lng)) {
11643
- data.origin = params.origin.lat + ',' + parseFloat(params.origin.lng);
11644
- } else {
11645
- console.log('No origin has been selected!');
11646
- }
12841
+ if (Array.isArray(params.contactIds)) {
12842
+ sendMessageParams.content = params.contactIds;
11647
12843
  }
11648
12844
 
11649
- if (typeof params.destination === 'object') {
11650
- if (parseFloat(params.destination.lat) > 0 && parseFloat(params.destination.lng)) {
11651
- data.destination = params.destination.lat + ',' + parseFloat(params.destination.lng);
11652
- } else {
11653
- console.log('No destination has been selected!');
12845
+ if (Array.isArray(params.usernames)) {
12846
+ sendMessageParams.content = [];
12847
+ for (var i = 0; i < params.usernames.length; i++) {
12848
+ sendMessageParams.content.push({
12849
+ id: params.usernames[i],
12850
+ idType: inviteeVOidTypes.TO_BE_USER_USERNAME
12851
+ });
11654
12852
  }
11655
12853
  }
11656
12854
 
11657
- data.uniqueId = Utility.generateUUID();
11658
- }
11659
-
11660
- var requestParams = {
11661
- url: SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.ROUTING,
11662
- method: 'GET',
11663
- data: data,
11664
- headers: {
11665
- 'Api-Key': mapApiKey
12855
+ if (Array.isArray(params.coreUserids)) {
12856
+ sendMessageParams.content = [];
12857
+ for (var i = 0; i < params.coreUserids.length; i++) {
12858
+ sendMessageParams.content.push({
12859
+ id: params.coreUserids[i],
12860
+ idType: inviteeVOidTypes.TO_BE_CORE_USER_ID
12861
+ });
12862
+ }
11666
12863
  }
11667
- };
11668
-
11669
- httpRequest(requestParams, function (result) {
11670
- if (!result.hasError) {
11671
- var responseData = JSON.parse(result.result.responseText);
12864
+ }
11672
12865
 
12866
+ return sendMessage(sendMessageParams, {
12867
+ onResult: function (result) {
11673
12868
  var returnData = {
11674
12869
  hasError: result.hasError,
11675
- cache: result.cache,
11676
- errorMessage: result.message,
11677
- errorCode: result.errorCode,
11678
- result: responseData
12870
+ cache: false,
12871
+ errorMessage: result.errorMessage,
12872
+ errorCode: result.errorCode
11679
12873
  };
11680
-
12874
+ if (!returnData.hasError) {
12875
+ // TODO : What is the result?!
12876
+ var messageContent = result.result;
12877
+ returnData.result = messageContent;
12878
+ }
11681
12879
  callback && callback(returnData);
11682
-
11683
- } else {
11684
- fireEvent('error', {
11685
- code: result.errorCode,
11686
- message: result.errorMessage,
11687
- error: result
11688
- });
11689
12880
  }
11690
12881
  });
11691
12882
  };
11692
12883
 
11693
- this.mapStaticImage = function (params, callback) {
11694
- var data = {},
11695
- url = SERVICE_ADDRESSES.MAP_ADDRESS + SERVICES_PATH.STATIC_IMAGE,
11696
- hasError = false;
11697
-
11698
- if (params) {
11699
- if (typeof params.type === 'string') {
11700
- data.type = params.type;
11701
- } else {
11702
- data.type = 'standard-night';
11703
- }
12884
+ this.removeCallParticipants = function (params, callback) {
12885
+ /**
12886
+ * + removeCallParticipantsRequest {object}
12887
+ * - subjectId {int}
12888
+ * + content {list} List of Participants UserIds
12889
+ */
11704
12890
 
11705
- if (parseInt(params.zoom) > 0) {
11706
- data.zoom = params.zoom;
11707
- } else {
11708
- data.zoom = 15;
11709
- }
12891
+ var sendMessageParams = {
12892
+ chatMessageVOType: chatMessageVOTypes.REMOVE_CALL_PARTICIPANT,
12893
+ typeCode: params.typeCode,
12894
+ content: []
12895
+ };
11710
12896
 
11711
- if (parseInt(params.width) > 0) {
11712
- data.width = params.width;
11713
- } else {
11714
- data.width = 800;
12897
+ if (params) {
12898
+ if (typeof params.callId === 'number' && params.callId > 0) {
12899
+ sendMessageParams.subjectId = params.callId;
11715
12900
  }
11716
12901
 
11717
- if (parseInt(params.height) > 0) {
11718
- data.height = params.height;
11719
- } else {
11720
- data.height = 600;
12902
+ if (Array.isArray(params.userIds)) {
12903
+ sendMessageParams.content = params.userIds;
11721
12904
  }
12905
+ }
11722
12906
 
11723
- if (typeof params.center === 'object') {
11724
- if (parseFloat(params.center.lat) > 0 && parseFloat(params.center.lng)) {
11725
- data.center = params.center.lat + ',' + parseFloat(params.center.lng);
11726
- } else {
11727
- hasError = true;
11728
- fireEvent('error', {
11729
- code: 6700,
11730
- message: CHAT_ERRORS[6700],
11731
- error: undefined
11732
- });
12907
+ return sendMessage(sendMessageParams, {
12908
+ onResult: function (result) {
12909
+ var returnData = {
12910
+ hasError: result.hasError,
12911
+ cache: false,
12912
+ errorMessage: result.errorMessage,
12913
+ errorCode: result.errorCode
12914
+ };
12915
+ if (!returnData.hasError) {
12916
+ // TODO : What is the result?!
12917
+ var messageContent = result.result;
12918
+ returnData.result = messageContent;
11733
12919
  }
11734
- } else {
11735
- hasError = true;
11736
- fireEvent('error', {
11737
- code: 6700,
11738
- message: CHAT_ERRORS[6700],
11739
- error: undefined
11740
- });
12920
+ callback && callback(returnData);
11741
12921
  }
12922
+ });
12923
+ };
11742
12924
 
11743
- data.key = mapApiKey;
11744
- }
12925
+ this.muteCallParticipants = function (params, callback) {
12926
+ /**
12927
+ * + muteCallParticipantsRequest {object}
12928
+ * - subjectId {int}
12929
+ * + content {list} List of Participants UserIds
12930
+ */
11745
12931
 
11746
- var keys = Object.keys(data);
12932
+ var sendMessageParams = {
12933
+ chatMessageVOType: chatMessageVOTypes.MUTE_CALL_PARTICIPANT,
12934
+ typeCode: params.typeCode,
12935
+ content: []
12936
+ };
11747
12937
 
11748
- if (keys.length > 0) {
11749
- url += '?';
12938
+ if (params) {
12939
+ if (typeof params.callId === 'number' && params.callId > 0) {
12940
+ sendMessageParams.subjectId = params.callId;
12941
+ }
11750
12942
 
11751
- for (var i = 0; i < keys.length; i++) {
11752
- var key = keys[i];
11753
- url += key + '=' + data[key];
11754
- if (i < keys.length - 1) {
11755
- url += '&';
11756
- }
12943
+ if (Array.isArray(params.userIds)) {
12944
+ sendMessageParams.content = params.userIds;
11757
12945
  }
11758
12946
  }
11759
12947
 
11760
- var returnData = {
11761
- hasError: hasError,
11762
- cache: false,
11763
- errorMessage: (hasError) ? CHAT_ERRORS[6700] : '',
11764
- errorCode: (hasError) ? 6700 : undefined,
11765
- result: {
11766
- link: (!hasError) ? url : ''
12948
+ return sendMessage(sendMessageParams, {
12949
+ onResult: function (result) {
12950
+ var returnData = {
12951
+ hasError: result.hasError,
12952
+ cache: false,
12953
+ errorMessage: result.errorMessage,
12954
+ errorCode: result.errorCode
12955
+ };
12956
+ if (!returnData.hasError) {
12957
+ // TODO : What is the result?!
12958
+ var messageContent = result.result;
12959
+ returnData.result = messageContent;
12960
+ }
12961
+ callback && callback(returnData);
11767
12962
  }
11768
- };
11769
-
11770
- callback && callback(returnData);
11771
- };
11772
-
11773
- this.setAdmin = function (params, callback) {
11774
- setRoleToUser(params, callback);
11775
- };
11776
-
11777
- this.removeAdmin = function (params, callback) {
11778
- removeRoleFromUser(params, callback);
11779
- };
11780
-
11781
- this.setAuditor = function (params, callback) {
11782
- setRoleToUser(params, callback);
11783
- };
11784
-
11785
- this.removeAuditor = function (params, callback) {
11786
- removeRoleFromUser(params, callback);
12963
+ });
11787
12964
  };
11788
12965
 
11789
- this.clearChatServerCaches = clearChatServerCaches;
11790
-
11791
- this.deleteCacheDatabases = deleteCacheDatabases;
11792
-
11793
- this.clearCacheDatabasesOfUser = clearCacheDatabasesOfUser;
12966
+ this.unMuteCallParticipants = function (params, callback) {
12967
+ /**
12968
+ * + unMuteCallParticipantsRequest {object}
12969
+ * - subjectId {int}
12970
+ * + content {list} List of Participants UserIds
12971
+ */
11794
12972
 
11795
- this.getChatState = function () {
11796
- return chatFullStateObject;
11797
- };
12973
+ var sendMessageParams = {
12974
+ chatMessageVOType: chatMessageVOTypes.UNMUTE_CALL_PARTICIPANT,
12975
+ typeCode: params.typeCode,
12976
+ content: []
12977
+ };
11798
12978
 
11799
- this.reconnect = function () {
11800
- asyncClient.reconnectSocket();
11801
- };
12979
+ if (params) {
12980
+ if (typeof params.callId === 'number' && params.callId > 0) {
12981
+ sendMessageParams.subjectId = params.callId;
12982
+ }
11802
12983
 
11803
- this.setToken = function (newToken) {
11804
- if (typeof newToken != 'undefined') {
11805
- token = newToken;
12984
+ if (Array.isArray(params.userIds)) {
12985
+ sendMessageParams.content = params.userIds;
12986
+ }
11806
12987
  }
11807
- };
12988
+ var myId = userInfo.id;
11808
12989
 
11809
- this.generateUUID = Utility.generateUUID;
12990
+ return sendMessage(sendMessageParams, {
12991
+ onResult: function (result) {
12992
+ var returnData = {
12993
+ hasError: result.hasError,
12994
+ cache: false,
12995
+ errorMessage: result.errorMessage,
12996
+ errorCode: result.errorCode
12997
+ };
12998
+ if (!returnData.hasError) {
12999
+ // TODO : What is the result?!
13000
+ var messageContent = result.result;
13001
+ returnData.result = messageContent;
13002
+ }
13003
+ callback && callback(returnData);
13004
+ }
13005
+ });
13006
+ };
11810
13007
 
11811
- this.startRecordingCall = function (params, callback) {
11812
- var recordCallData = {
11813
- chatMessageVOType: chatMessageVOTypes.RECORD_CALL,
13008
+ this.turnOnVideoCall = function (params, callback) {
13009
+ var turnOnVideoData = {
13010
+ chatMessageVOType: chatMessageVOTypes.TURN_ON_VIDEO_CALL,
11814
13011
  typeCode: params.typeCode,
11815
13012
  pushMsgType: 3,
11816
- token: token,
11817
- content: {}
13013
+ token: token
11818
13014
  };
11819
13015
 
11820
13016
  if (params) {
11821
13017
  if (typeof +params.callId === 'number' && params.callId > 0) {
11822
- recordCallData.subjectId = +params.callId;
13018
+ turnOnVideoData.subjectId = +params.callId;
11823
13019
  } else {
11824
13020
  fireEvent('error', {
11825
13021
  code: 999,
11826
- message: 'Invalid Call id!'
13022
+ message: 'Invalid call id!'
11827
13023
  });
11828
13024
  return;
11829
13025
  }
11830
-
11831
- if(params.destinated === true) {
11832
- recordCallData.chatMessageVOType = chatMessageVOTypes.DESTINATED_RECORD_CALL;
11833
- recordCallData.content.recordType = typeof +params.recordType === 'number' ? params.recordType : 1;
11834
- recordCallData.content.tags = Array.isArray(params.tags) ? params.tags : null;
11835
- recordCallData.content.threadId = typeof +params.threadId === 'number' ? params.threadId : null;
11836
- }
11837
13026
  } else {
11838
13027
  fireEvent('error', {
11839
13028
  code: 999,
11840
- message: 'No params have been sent to Record call!'
13029
+ message: 'No params have been sent to turn on the video call!'
11841
13030
  });
11842
13031
  return;
11843
13032
  }
11844
13033
 
11845
- return sendMessage(recordCallData, {
13034
+ return sendMessage(turnOnVideoData, {
11846
13035
  onResult: function (result) {
11847
13036
  callback && callback(result);
11848
13037
  }
11849
13038
  });
11850
13039
  };
11851
13040
 
11852
- this.stopRecordingCall = function (params, callback) {
11853
- var stopRecordingCallData = {
11854
- chatMessageVOType: chatMessageVOTypes.END_RECORD_CALL,
13041
+ this.turnOffVideoCall = function (params, callback) {
13042
+ var turnOffVideoData = {
13043
+ chatMessageVOType: chatMessageVOTypes.TURN_OFF_VIDEO_CALL,
11855
13044
  typeCode: params.typeCode,
11856
13045
  pushMsgType: 3,
11857
13046
  token: token
@@ -11859,29 +13048,31 @@
11859
13048
 
11860
13049
  if (params) {
11861
13050
  if (typeof +params.callId === 'number' && params.callId > 0) {
11862
- stopRecordingCallData.subjectId = +params.callId;
13051
+ turnOffVideoData.subjectId = +params.callId;
11863
13052
  } else {
11864
13053
  fireEvent('error', {
11865
13054
  code: 999,
11866
- message: 'Invalid Call id!'
13055
+ message: 'Invalid call id!'
11867
13056
  });
11868
13057
  return;
11869
13058
  }
11870
13059
  } else {
11871
13060
  fireEvent('error', {
11872
13061
  code: 999,
11873
- message: 'No params have been sent to Stop Recording the call!'
13062
+ message: 'No params have been sent to turn off the video call!'
11874
13063
  });
11875
13064
  return;
11876
13065
  }
11877
13066
 
11878
- return sendMessage(stopRecordingCallData, {
13067
+
13068
+ return sendMessage(turnOffVideoData, {
11879
13069
  onResult: function (result) {
11880
13070
  callback && callback(result);
11881
13071
  }
11882
13072
  });
11883
13073
  };
11884
13074
 
13075
+ this.callStop = callStop;
11885
13076
 
11886
13077
  this.logout = function () {
11887
13078
  clearChatServerCaches();