UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

908 lines (906 loc) 41.5 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. // Multi-device Conversation is a Preview feature. var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var Exports_1 = require("../../common.speech/Exports"); var Contracts_1 = require("../Contracts"); var Exports_2 = require("../Exports"); var Conversation = /** @class */ (function () { function Conversation() { } /** * Create a conversation * @param speechConfig * @param cb * @param err */ Conversation.createConversationAsync = function (speechConfig, cb, err) { Contracts_1.Contracts.throwIfNullOrUndefined(speechConfig, Exports_1.ConversationConnectionConfig.restErrors.invalidArgs.replace("{arg}", "config")); Contracts_1.Contracts.throwIfNullOrUndefined(speechConfig.region, Exports_1.ConversationConnectionConfig.restErrors.invalidArgs.replace("{arg}", "SpeechServiceConnection_Region")); if (!speechConfig.subscriptionKey && !speechConfig.getProperty(Exports_2.PropertyId[Exports_2.PropertyId.SpeechServiceAuthorization_Token])) { Contracts_1.Contracts.throwIfNullOrUndefined(speechConfig.subscriptionKey, Exports_1.ConversationConnectionConfig.restErrors.invalidArgs.replace("{arg}", "SpeechServiceConnection_Key")); } var conversationImpl = new ConversationImpl(speechConfig); conversationImpl.createConversationAsync((function () { if (!!cb) { cb(); } }), function (error) { if (!!err) { err(error); } }); return conversationImpl; }; return Conversation; }()); exports.Conversation = Conversation; // tslint:disable-next-line:max-classes-per-file var ConversationImpl = /** @class */ (function (_super) { __extends(ConversationImpl, _super); /** * Create a conversation impl * @param speechConfig */ function ConversationImpl(speechConfig) { var _this = _super.call(this) || this; _this.privIsDisposed = false; _this.privIsConnected = false; _this.privErrors = Exports_1.ConversationConnectionConfig.restErrors; /** websocket callbacks */ _this.onConnected = function (e) { _this.privIsConnected = true; try { if (!!_this.privConversationTranslator.sessionStarted) { _this.privConversationTranslator.sessionStarted(_this.privConversationTranslator, e); } } catch (e) { // } }; _this.onDisconnected = function (e) { _this.close(false); try { if (!!_this.privConversationTranslator.sessionStopped) { _this.privConversationTranslator.sessionStopped(_this.privConversationTranslator, e); } } catch (e) { // } }; _this.onCanceled = function (r, e) { _this.close(false); // ? try { if (!!_this.privConversationTranslator.canceled) { _this.privConversationTranslator.canceled(_this.privConversationTranslator, e); } } catch (e) { // } }; _this.onParticipantUpdateCommandReceived = function (r, e) { var _a, _b; try { var updatedParticipant = _this.privParticipants.getParticipant(e.id); if (updatedParticipant !== undefined) { switch (e.key) { case Exports_1.ConversationTranslatorCommandTypes.changeNickname: updatedParticipant.displayName = e.value; break; case Exports_1.ConversationTranslatorCommandTypes.setUseTTS: updatedParticipant.useTts = e.value; break; case Exports_1.ConversationTranslatorCommandTypes.setProfanityFiltering: updatedParticipant.profanity = e.value; break; case Exports_1.ConversationTranslatorCommandTypes.setMute: updatedParticipant.isMuted = e.value; break; case Exports_1.ConversationTranslatorCommandTypes.setTranslateToLanguages: updatedParticipant.translateToLanguages = e.value; break; } _this.privParticipants.addOrUpdateParticipant(updatedParticipant); if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.participantsChanged)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.participantsChanged(_this.privConversationTranslator, new Exports_2.ConversationParticipantsChangedEventArgs(Exports_2.ParticipantChangedReason.Updated, [_this.toParticipant(updatedParticipant)], e.sessionId)); } } } catch (e) { // } }; _this.onLockRoomCommandReceived = function (r, e) { // TODO }; _this.onMuteAllCommandReceived = function (r, e) { var _a, _b; try { _this.privParticipants.participants.forEach(function (p) { return p.isMuted = (p.isHost ? false : e.isMuted); }); if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.participantsChanged)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.participantsChanged(_this.privConversationTranslator, new Exports_2.ConversationParticipantsChangedEventArgs(Exports_2.ParticipantChangedReason.Updated, _this.toParticipants(false), e.sessionId)); } } catch (e) { // } }; _this.onParticipantJoinCommandReceived = function (r, e) { var _a, _b; try { var newParticipant = _this.privParticipants.addOrUpdateParticipant(e.participant); if (newParticipant !== undefined) { if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.participantsChanged)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.participantsChanged(_this.privConversationTranslator, new Exports_2.ConversationParticipantsChangedEventArgs(Exports_2.ParticipantChangedReason.JoinedConversation, [_this.toParticipant(newParticipant)], e.sessionId)); } } } catch (e) { // } }; _this.onParticipantLeaveCommandReceived = function (r, e) { var _a, _b; try { var ejectedParticipant = _this.privParticipants.getParticipant(e.participant.id); if (ejectedParticipant !== undefined) { // remove the participant from the internal participants list _this.privParticipants.deleteParticipant(e.participant.id); if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.participantsChanged)) { // notify subscribers that the participant has left the conversation (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.participantsChanged(_this.privConversationTranslator, new Exports_2.ConversationParticipantsChangedEventArgs(Exports_2.ParticipantChangedReason.LeftConversation, [_this.toParticipant(ejectedParticipant)], e.sessionId)); } } } catch (e) { // } }; _this.onTranslationReceived = function (r, e) { var _a, _b, _c, _d, _e, _f; try { switch (e.command) { case Exports_1.ConversationTranslatorMessageTypes.final: if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.transcribed)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.transcribed(_this.privConversationTranslator, new Exports_2.ConversationTranslationEventArgs(e.payload, undefined, e.sessionId)); } break; case Exports_1.ConversationTranslatorMessageTypes.partial: if (!!((_c = _this.privConversationTranslator) === null || _c === void 0 ? void 0 : _c.transcribing)) { (_d = _this.privConversationTranslator) === null || _d === void 0 ? void 0 : _d.transcribing(_this.privConversationTranslator, new Exports_2.ConversationTranslationEventArgs(e.payload, undefined, e.sessionId)); } break; case Exports_1.ConversationTranslatorMessageTypes.instantMessage: if (!!((_e = _this.privConversationTranslator) === null || _e === void 0 ? void 0 : _e.textMessageReceived)) { (_f = _this.privConversationTranslator) === null || _f === void 0 ? void 0 : _f.textMessageReceived(_this.privConversationTranslator, new Exports_2.ConversationTranslationEventArgs(e.payload, undefined, e.sessionId)); } break; } } catch (e) { // } }; _this.onParticipantsListReceived = function (r, e) { var _a, _b; try { // check if the session token needs to be updated if (e.sessionToken !== undefined && e.sessionToken !== null) { _this.privRoom.token = e.sessionToken; } // save the participants _this.privParticipants.participants = __spreadArrays(e.participants); // enable the conversation if (_this.privParticipants.me !== undefined) { _this.privIsReady = true; } if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.participantsChanged)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.participantsChanged(_this.privConversationTranslator, new Exports_2.ConversationParticipantsChangedEventArgs(Exports_2.ParticipantChangedReason.JoinedConversation, _this.toParticipants(true), e.sessionId)); } // if this is the host, update the nickname if needed if (_this.me.isHost) { var nickname = _this.privConversationTranslator.properties.getProperty(Exports_2.PropertyId.ConversationTranslator_Name); if (nickname !== undefined && nickname.length > 0 && nickname !== _this.me.displayName) { // issue a change nickname request _this.changeNicknameAsync(nickname); } } } catch (e) { // } }; _this.onConversationExpiration = function (r, e) { var _a, _b; try { if (!!((_a = _this.privConversationTranslator) === null || _a === void 0 ? void 0 : _a.conversationExpiration)) { (_b = _this.privConversationTranslator) === null || _b === void 0 ? void 0 : _b.conversationExpiration(_this.privConversationTranslator, e); } } catch (e) { // } }; _this.privProperties = new Exports_2.PropertyCollection(); _this.privManager = new Exports_1.ConversationManager(); // check the speech language var language = speechConfig.getProperty(Exports_2.PropertyId[Exports_2.PropertyId.SpeechServiceConnection_RecoLanguage]); if (!language) { speechConfig.setProperty(Exports_2.PropertyId[Exports_2.PropertyId.SpeechServiceConnection_RecoLanguage], Exports_1.ConversationConnectionConfig.defaultLanguageCode); } _this.privLanguage = speechConfig.getProperty(Exports_2.PropertyId[Exports_2.PropertyId.SpeechServiceConnection_RecoLanguage]); // check the target language(s) if (speechConfig.targetLanguages.length === 0) { speechConfig.addTargetLanguage(_this.privLanguage); } // check the profanity setting: speech and conversationTranslator should be in sync var profanity = speechConfig.getProperty(Exports_2.PropertyId[Exports_2.PropertyId.SpeechServiceResponse_ProfanityOption]); if (!profanity) { speechConfig.setProfanity(Exports_2.ProfanityOption.Masked); } // check the nickname: it should pass this regex: ^\w+([\s-][\w\(\)]+)*$" // TODO: specify the regex required. Nicknames must be unique or get the duplicate nickname error // TODO: check what the max length is and if a truncation is required or if the service handles it without an error var hostNickname = speechConfig.getProperty(Exports_2.PropertyId[Exports_2.PropertyId.ConversationTranslator_Name]); if (hostNickname === undefined || hostNickname === null || hostNickname.length <= 1 || hostNickname.length > 50) { hostNickname = "Host"; } speechConfig.setProperty(Exports_2.PropertyId[Exports_2.PropertyId.ConversationTranslator_Name], hostNickname); // save the speech config for future usage _this.privConfig = speechConfig; // save the config properties var configImpl = speechConfig; Contracts_1.Contracts.throwIfNull(configImpl, "speechConfig"); _this.privProperties = configImpl.properties.clone(); _this.privIsConnected = false; _this.privParticipants = new Exports_1.InternalParticipants(); _this.privIsReady = false; _this.privTextMessageMaxLength = 1000; return _this; } Object.defineProperty(ConversationImpl.prototype, "conversationTranslator", { set: function (value) { this.privConversationTranslator = value; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "room", { // get the internal data about a conversation get: function () { return this.privRoom; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "connection", { // get the wrapper for connecting to the websockets get: function () { return this.privConversationRecognizer; // this.privConnection; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "authorizationToken", { // get / set the speech auth token get: function () { return this.privToken; }, set: function (value) { Contracts_1.Contracts.throwIfNullOrWhitespace(value, "authorizationToken"); this.privToken = value; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "config", { // get the config get: function () { return this.privConfig; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "conversationId", { // get the conversation Id get: function () { return this.privRoom ? this.privRoom.roomId : ""; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "properties", { // get the properties get: function () { return this.privProperties; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "speechRecognitionLanguage", { // get the speech language get: function () { return this.privLanguage; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "isMutedByHost", { get: function () { var _a, _b; return ((_a = this.privParticipants.me) === null || _a === void 0 ? void 0 : _a.isHost) ? false : (_b = this.privParticipants.me) === null || _b === void 0 ? void 0 : _b.isMuted; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "isConnected", { get: function () { return this.privIsConnected && this.privIsReady; }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "participants", { get: function () { return this.toParticipants(true); }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "me", { get: function () { return this.toParticipant(this.privParticipants.me); }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "host", { get: function () { return this.toParticipant(this.privParticipants.host); }, enumerable: true, configurable: true }); /** * Create a new conversation as Host * @param cb * @param err */ ConversationImpl.prototype.createConversationAsync = function (cb, err) { var _this = this; try { if (!!this.privConversationRecognizer) { this.handleError(new Error(this.privErrors.permissionDeniedStart), err); } this.privManager.createOrJoin(this.privProperties, undefined, (function (room) { if (!room) { _this.handleError(new Error(_this.privErrors.permissionDeniedConnect), err); } _this.privRoom = room; _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Starts a new conversation as host. * @param cb * @param err */ ConversationImpl.prototype.startConversationAsync = function (cb, err) { var _this = this; try { // check if there is already a recognizer if (!!this.privConversationRecognizer) { this.handleError(new Error(this.privErrors.permissionDeniedStart), err); } // check if there is conversation data available Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedConnect); // connect to the conversation websocket this.privParticipants.meId = this.privRoom.participantId; this.privConversationRecognizer = new Exports_1.ConversationTranslatorRecognizer(this.privConfig); this.privConversationRecognizer.conversation = this.privRoom; this.privConversationRecognizerConnection = Exports_2.Connection.fromRecognizer(this.privConversationRecognizer); this.privConversationRecognizerConnection.connected = this.onConnected; this.privConversationRecognizerConnection.disconnected = this.onDisconnected; this.privConversationRecognizer.canceled = this.onCanceled; this.privConversationRecognizer.participantUpdateCommandReceived = this.onParticipantUpdateCommandReceived; this.privConversationRecognizer.lockRoomCommandReceived = this.onLockRoomCommandReceived; this.privConversationRecognizer.muteAllCommandReceived = this.onMuteAllCommandReceived; this.privConversationRecognizer.participantJoinCommandReceived = this.onParticipantJoinCommandReceived; this.privConversationRecognizer.participantLeaveCommandReceived = this.onParticipantLeaveCommandReceived; this.privConversationRecognizer.translationReceived = this.onTranslationReceived; this.privConversationRecognizer.participantsListReceived = this.onParticipantsListReceived; this.privConversationRecognizer.conversationExpiration = this.onConversationExpiration; this.privConversationRecognizer.connect(this.privRoom.token, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Join a conversation as a participant. * @param conversation * @param nickname * @param lang * @param cb * @param err */ ConversationImpl.prototype.joinConversationAsync = function (conversationId, nickname, lang, cb, err) { var _this = this; try { // TODO // if (!!this.privConversationRecognizer) { // throw new Error(this.privErrors.permissionDeniedStart); // } Contracts_1.Contracts.throwIfNullOrWhitespace(conversationId, this.privErrors.invalidArgs.replace("{arg}", "conversationId")); Contracts_1.Contracts.throwIfNullOrWhitespace(nickname, this.privErrors.invalidArgs.replace("{arg}", "nickname")); Contracts_1.Contracts.throwIfNullOrWhitespace(lang, this.privErrors.invalidArgs.replace("{arg}", "language")); // join the conversation this.privManager.createOrJoin(this.privProperties, conversationId, (function (room) { Contracts_1.Contracts.throwIfNullOrUndefined(room, _this.privErrors.permissionDeniedConnect); _this.privRoom = room; _this.privConfig.authorizationToken = room.cognitiveSpeechAuthToken; // join callback if (!!cb) { cb(room.cognitiveSpeechAuthToken); } }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Deletes a conversation * @param cb * @param err */ ConversationImpl.prototype.deleteConversationAsync = function (cb, err) { var _this = this; try { Contracts_1.Contracts.throwIfNullOrUndefined(this.privProperties, this.privErrors.permissionDeniedConnect); Contracts_1.Contracts.throwIfNullOrWhitespace(this.privRoom.token, this.privErrors.permissionDeniedConnect); this.privManager.leave(this.privProperties, this.privRoom.token, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); this.dispose(); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to close the client websockets * @param cb * @param err */ ConversationImpl.prototype.endConversationAsync = function (cb, err) { try { this.close(true); this.handleCallback(cb, err); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to lock the conversation * @param cb * @param err */ ConversationImpl.prototype.lockConversationAsync = function (cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSendAsHost) { this.handleError(new Error(this.privErrors.permissionDeniedConversation.replace("{command}", "lock")), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendLockRequest(true, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to mute the conversation * @param cb * @param err */ ConversationImpl.prototype.muteAllParticipantsAsync = function (cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrUndefined(this.privConversationRecognizer, this.privErrors.permissionDeniedSend); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); // check the user's permissions if (!this.canSendAsHost) { this.handleError(new Error(this.privErrors.permissionDeniedConversation.replace("{command}", "mute")), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendMuteAllRequest(true, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to mute a participant in the conversation * @param userId * @param cb * @param err */ ConversationImpl.prototype.muteParticipantAsync = function (userId, cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrWhitespace(userId, this.privErrors.invalidArgs.replace("{arg}", "userId")); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); // check the connection is open (host + participant can perform the mute command) if (!this.canSend) { this.handleError(new Error(this.privErrors.permissionDeniedSend), err); } // if not host, check the participant is not muting another participant if (!this.me.isHost && this.me.id !== userId) { this.handleError(new Error(this.privErrors.permissionDeniedParticipant.replace("{command}", "mute")), err); } // check the user exists var exists = this.privParticipants.getParticipantIndex(userId); if (exists === -1) { this.handleError(new Error(this.privErrors.invalidParticipantRequest), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendMuteRequest(userId, true, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to remove a participant from the conversation * @param userId * @param cb * @param err */ ConversationImpl.prototype.removeParticipantAsync = function (userId, cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSendAsHost) { this.handleError(new Error(this.privErrors.permissionDeniedParticipant.replace("{command}", "remove")), err); } var participantId_1 = ""; if (typeof userId === "string") { participantId_1 = userId; } else if (userId.hasOwnProperty("id")) { var participant = userId; participantId_1 = participant.id; } else if (userId.hasOwnProperty("userId")) { var user = userId; participantId_1 = user.userId; } Contracts_1.Contracts.throwIfNullOrWhitespace(participantId_1, this.privErrors.invalidArgs.replace("{arg}", "userId")); // check the participant exists var index = this.participants.findIndex(function (p) { return p.id === participantId_1; }); if (index === -1) { this.handleError(new Error(this.privErrors.invalidParticipantRequest), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendEjectRequest(participantId_1, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to unlock the conversation * @param cb * @param err */ ConversationImpl.prototype.unlockConversationAsync = function (cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSendAsHost) { this.handleError(new Error(this.privErrors.permissionDeniedConversation.replace("{command}", "unlock")), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendLockRequest(false, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to unmute all participants in the conversation * @param cb * @param err */ ConversationImpl.prototype.unmuteAllParticipantsAsync = function (cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSendAsHost) { this.handleError(new Error(this.privErrors.permissionDeniedConversation.replace("{command}", "unmute all")), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendMuteAllRequest(false, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Issues a request to unmute a participant in the conversation * @param userId * @param cb * @param err */ ConversationImpl.prototype.unmuteParticipantAsync = function (userId, cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrWhitespace(userId, this.privErrors.invalidArgs.replace("{arg}", "userId")); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); // check the connection is open (host + participant can perform the mute command) if (!this.canSend) { this.handleError(new Error(this.privErrors.permissionDeniedSend), err); } // if not host, check the participant is not muting another participant if (!this.me.isHost && this.me.id !== userId) { this.handleError(new Error(this.privErrors.permissionDeniedParticipant.replace("{command}", "mute")), err); } // check the user exists var exists = this.privParticipants.getParticipantIndex(userId); if (exists === -1) { this.handleError(new Error(this.privErrors.invalidParticipantRequest), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendMuteRequest(userId, false, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Send a text message * @param message * @param cb * @param err */ ConversationImpl.prototype.sendTextMessageAsync = function (message, cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrWhitespace(message, this.privErrors.invalidArgs.replace("{arg}", "message")); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSend) { this.handleError(new Error(this.privErrors.permissionDeniedSend), err); } // TODO: is a max length check required? if (message.length > this.privTextMessageMaxLength) { this.handleError(new Error(this.privErrors.invalidArgs.replace("{arg}", "message length")), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendMessageRequest(message, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; /** * Change nickname * @param message * @param cb * @param err */ ConversationImpl.prototype.changeNicknameAsync = function (nickname, cb, err) { var _this = this; var _a; try { Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed); Contracts_1.Contracts.throwIfDisposed(this.privConversationRecognizer.isDisposed()); Contracts_1.Contracts.throwIfNullOrWhitespace(nickname, this.privErrors.invalidArgs.replace("{arg}", "nickname")); Contracts_1.Contracts.throwIfNullOrUndefined(this.privRoom, this.privErrors.permissionDeniedSend); if (!this.canSend) { this.handleError(new Error(this.privErrors.permissionDeniedSend), err); } (_a = this.privConversationRecognizer) === null || _a === void 0 ? void 0 : _a.sendChangeNicknameRequest(nickname, (function () { _this.handleCallback(cb, err); }), (function (error) { _this.handleError(error, err); })); } catch (error) { this.handleError(error, err); } }; ConversationImpl.prototype.isDisposed = function () { return this.privIsDisposed; }; ConversationImpl.prototype.dispose = function (reason) { var _a; if (this.isDisposed) { return; } this.privIsDisposed = true; (_a = this.config) === null || _a === void 0 ? void 0 : _a.close(); if (this.privConversationRecognizerConnection) { this.privConversationRecognizerConnection.closeConnection(); this.privConversationRecognizerConnection.close(); this.privConversationRecognizerConnection = undefined; } this.privConfig = undefined; this.privLanguage = undefined; this.privProperties = undefined; this.privRoom = undefined; this.privToken = undefined; this.privManager = undefined; this.privConversationRecognizer = undefined; this.privIsConnected = false; this.privIsReady = false; this.privParticipants = undefined; this.privRoom = undefined; }; ConversationImpl.prototype.close = function (dispose) { var _a, _b, _c; try { this.privIsConnected = false; (_a = this.privConversationRecognizerConnection) === null || _a === void 0 ? void 0 : _a.closeConnection(); (_b = this.privConversationRecognizerConnection) === null || _b === void 0 ? void 0 : _b.close(); this.privConversationRecognizer.close(); this.privConversationRecognizerConnection = undefined; this.privConversationRecognizer = undefined; (_c = this.privConversationTranslator) === null || _c === void 0 ? void 0 : _c.dispose(); } catch (e) { // ignore error } if (dispose) { this.dispose(); } }; Object.defineProperty(ConversationImpl.prototype, "canSend", { /** Helpers */ get: function () { var _a; return this.privIsConnected && !((_a = this.privParticipants.me) === null || _a === void 0 ? void 0 : _a.isMuted); }, enumerable: true, configurable: true }); Object.defineProperty(ConversationImpl.prototype, "canSendAsHost", { get: function () { var _a; return this.privIsConnected && ((_a = this.privParticipants.me) === null || _a === void 0 ? void 0 : _a.isHost); }, enumerable: true, configurable: true }); ConversationImpl.prototype.handleCallback = function (cb, err) { if (!!cb) { try { cb(); } catch (e) { if (!!err) { err(e); } } cb = undefined; } }; ConversationImpl.prototype.handleError = function (error, err) { if (!!err) { if (error instanceof Error) { var typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } }; /** Participant Helpers */ ConversationImpl.prototype.toParticipants = function (includeHost) { var _this = this; var participants = this.privParticipants.participants.map(function (p) { return _this.toParticipant(p); }); if (!includeHost) { return participants.filter(function (p) { return p.isHost === false; }); } else { return participants; } }; ConversationImpl.prototype.toParticipant = function (p) { return new Exports_2.Participant(p.id, p.avatar, p.displayName, p.isHost, p.isMuted, p.isUsingTts, p.preferredLanguage); }; return ConversationImpl; }(Conversation)); exports.ConversationImpl = ConversationImpl; //# sourceMappingURL=Conversation.js.map