From 93c85714a9cda909909a73debd24fe3827f800f0 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Wed, 26 Aug 2020 21:40:55 +0530 Subject: [PATCH] MPC API changes, TODO : Dev Testing --- lib/base.js | 46 +++++++++----- lib/resources/multiPartyCall.js | 105 ++++++++++++++++++++++++++------ 2 files changed, 117 insertions(+), 34 deletions(-) diff --git a/lib/base.js b/lib/base.js index e374e7c..29834c8 100644 --- a/lib/base.js +++ b/lib/base.js @@ -4,9 +4,9 @@ let actionKey = Symbol('api action'); let klassKey = Symbol('constructor'); let idKey = Symbol('id filed'); let clientKey = Symbol('make api call'); -// let secondaryActionKey = Symbol('api action'); -// let secondaryKlassKey = Symbol('constructor'); -// let secondaryIdKey = Symbol('id filed'); +let secondaryActionKey = Symbol('api action'); +let secondaryKlassKey = Symbol('constructor'); +let secondaryIdKey = Symbol('id filed'); export class PlivoGenericResponse { constructor(params, idString) { @@ -126,17 +126,35 @@ export class PlivoResource { } } -// export class PlivoSecondaryResource { -// constructor(action, klass, idField, secondaryAction, secondaryKlass, secondaryIdField, request) { -// this[actionKey] = action; -// this[klassKey] = klass; -// this[idKey] = idField; -// this[clientKey] = request; -// this[secondaryActionKey] = secondaryAction; -// this[secondaryKlassKey] = secondaryKlass; -// this[secondaryIdKey] = secondaryIdField; -// } -// } +export class PlivoSecondaryResource { + constructor(action, klass, idField, secondaryAction, secondaryKlass, secondaryIdField, request) { + this[actionKey] = action; + this[klassKey] = klass; + this[idKey] = idField; + this[clientKey] = request; + this[secondaryActionKey] = secondaryAction; + this[secondaryKlassKey] = secondaryKlass; + this[secondaryIdKey] = secondaryIdField; + } + + executeAction(task = '', secondaryTask = '', method = 'GET', params = {}, action, secondaryAction) { + let client = this[clientKey]; + action = action == null ? this[actionKey] : action; + let idField = this[idKey]; + secondaryAction = secondaryAction == null ? this[secondaryActionKey] : secondaryAction; + let secondaryIdField = this[secondaryIdKey] + + return new Promise((resolve, reject) => { + client(method, action + task + '/' + secondaryAction + secondaryTask, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, secondaryIdField)); + }) + .catch(error => { + reject(error); + }); + }); + } +} export class PlivoResourceInterface { constructor(action, klass, idField, request) { diff --git a/lib/resources/multiPartyCall.js b/lib/resources/multiPartyCall.js index 6099bac..2eb71d8 100644 --- a/lib/resources/multiPartyCall.js +++ b/lib/resources/multiPartyCall.js @@ -1,5 +1,5 @@ import {extend, validate} from '../utils/common.js'; -import {PlivoResource, PlivoResourceInterface} from '../base'; +import {PlivoResource, PlivoResourceInterface, PlivoSecondaryResource} from '../base'; import { validSubAccount, validUrl, @@ -13,8 +13,8 @@ import { const clientKey = Symbol(); const action = 'MultiPartyCall/'; const idField = 'mpcUuid'; -// const secondaryAction = 'Participant/'; -// const secondaryIdField = 'participantUuid'; +const secondaryAction = 'Participant/'; +const secondaryIdField = 'participantUuid'; export class MPCError extends Error { } @@ -338,23 +338,47 @@ export class MultiPartyCall extends PlivoResource{ } } -// export class MultiPartyCallParticipant extends PlivoSecondaryResource{ -// constructor(client, data = {}) { -// super(action, MultiPartyCall, idField, secondaryAction, MultiPartyCallParticipant, secondaryIdField, client); -// -// if (idField in data) { -// this.id = data[idField]; -// } -// -// if(secondaryIdField in data){ -// this.secondaryId = data[secondaryIdField]; -// } -// -// extend(this, data); -// this[clientKey] = client; -// } -// -// } +export class MultiPartyCallParticipant extends PlivoSecondaryResource{ + constructor(client, data = {}) { + super(action, MultiPartyCall, idField, secondaryAction, MultiPartyCallParticipant, secondaryIdField, client); + + if (idField in data) { + this.id = data[idField]; + } + + if(secondaryIdField in data){ + this.secondaryId = data[secondaryIdField]; + } + + extend(this, data); + this[clientKey] = client; + } + + updateParticipant(params){ + if(params.coachMode){ + validParam('coachMode', params.coachMode, [Boolean], false) + } + + if(params.mute){ + validParam('mute', params.mute, [Boolean], false) + } + + if(params.hold){ + validParam('hold', params.hold, [Boolean], false) + } + + return super.executeAction(this.id, this.secondaryId, 'POST', params) + } + + kickParticipant(){ + return super.executeAction(this.id, this.secondaryId, 'DELETE') + } + + getParticipant(){ + return super.executeAction(this.id, this.secondaryId, 'GET') + } + +} export class MultiPartyCallInterface extends PlivoResourceInterface{ constructor(client, data = {}) { @@ -555,5 +579,46 @@ export class MultiPartyCallInterface extends PlivoResourceInterface{ return new MultiPartyCall(this[clientKey], {id: mpcId[0] + mpcId[1]}).listParticipants(params) } + updateParticipant(participantId, uuid= null, friendlyName = null, params){ + if(participantId){ + validParam('participantId', participantId, [String, Number], true) + } + if(uuid){ + validParam('uuid', uuid, [String], false) + } + if(friendlyName){ + validParam('friendlyName', friendlyName, [String], false) + } + let mpcId = this.makeMpcId(uuid, friendlyName) + return new MultiPartyCallParticipant(this[clientKey], {id: mpcId[0] + mpcId[1], secondaryId: participantId}).updateParticipant(params) + } + + kickParticipant(participantId, uuid = null, friendlyName = null){ + if(participantId){ + validParam('participantId', participantId, [String, Number], true) + } + if(uuid){ + validParam('uuid', uuid, [String], false) + } + if(friendlyName){ + validParam('friendlyName', friendlyName, [String], false) + } + let mpcId = this.makeMpcId(uuid, friendlyName) + return new MultiPartyCallParticipant(this[clientKey], {id: mpcId[0] + mpcId[1], secondaryId: participantId}).kickParticipant() + } + + getParticipant(participantId, uuid = null, friendlyName = null){ + if(participantId){ + validParam('participantId', participantId, [String, Number], true) + } + if(uuid){ + validParam('uuid', uuid, [String], false) + } + if(friendlyName){ + validParam('friendlyName', friendlyName, [String], false) + } + let mpcId = this.makeMpcId(uuid, friendlyName) + return new MultiPartyCallParticipant(this[clientKey], {id: mpcId[0] + mpcId[1], secondaryId: participantId}).getParticipant() + } }