UNPKG

fc-nexmo-client1

Version:
1,419 lines (1,405 loc) 118 kB
/** * Core application class for the SDK. * Application is the parent object holding the list of conversations, the session object. * Provides methods to create conversations and retrieve a list of the user's conversations, while it holds the listeners for * user's invitations * @class Application * @param {NexmoClient} SDK session Object * @param {object} params * @example <caption>Accessing the list of conversations</caption> * rtc.createSession(token).then((application) => { * console.log(application.conversations); * console.log(application.me.name, application.me.id); * }).catch((error) => { * console.error(error); * }); * @emits Application#member:invited * @emits Application#member:joined * @emits Application#NXM-errors * @emits Application#rtcstats:analytics */ export class Application { log: Logger; session: any; conversations: Map<string, Conversation>; synced_conversations_count: number; start_sync_time: number; stop_sync_time: number; calls: Map<string, NXMCall>; _call_draft_list: Map<string, NXMCall>; pageConfig: PageConfig; conversations_page_last: ConversationsPage | null; user_sessions_page_last: UserSessionsPage | null; me: User; synced_conversations_percentage: number; sync_progress_buffer: number; activeStreams: MediaStream[]; eventsQueue: EventsQueue; emit: any; on: any; off: any; once: any; /** * Enum for Application getConversation version. * @readonly * @enum {string} * @alias Application.CONVERSATION_API_VERSION */ static CONVERSATION_API_VERSION: { v1: string; v3: string; }; constructor(session: any, params?: Object); /** * Update Conversation instance or create a new one. * * Pre-created conversation exist from getConversations * like initialised templates. When we explicitly ask to * getConversation(), we receive members and other details * * @param {object} payload Conversation payload * @private */ updateOrCreateConversation(payload: any): Conversation; /** * Application listening for member invited events. * * @event Application#member:invited * * @property {Member} member - The invited member * @property {NXMEvent} event - The invitation event * * @example <caption>listen for member invited events on Application level</caption> * application.on("member:invited",(member, event) => { * console.log("Invited to the conversation: " + event.conversation.display_name || event.conversation.name); * // identify the sender. * console.log("Invited by: " + member.invited_by); * //accept an invitation. * application.conversations.get(event.conversation.id).join(); * //decline the invitation. * application.conversations.get(event.conversation.id).leave(); * }); */ /** * Application listening for member joined events. * * @event Application#member:joined * * @property {Member} member - the member that joined the conversation * @property {NXMEvent} event - the join event * * @example <caption>listen for member joined events on Application level</caption> * application.on("member:joined",(member, event) => { * console.log("JOINED", "Joined conversation: " + event.conversation.display_name || event.conversation.name); * }); */ /** * Entry point for queing events in Application level * @private */ _enqueueEvent(response: CAPIResponse): Promise<void>; /** * Entry point for events in Application level * @private */ _handleEvent(event: CAPIResponse): Promise<CAPIResponse>; /** * Update user's token that was generated when they were first authenticated. * @param {string} token - the new token * @returns {Promise} * @example <caption>listen for expired-token error events and then update the token on Application level</caption> * application.on('system:error:expired-token', 'NXM-errors', (error) => { * console.log('token expired'); * application.updateToken(token); * }); */ updateToken(token: string): Promise<void>; /** * Update the event to map local generated events * in case we need a more specific event to pass in the application listener * or f/w the event as it comes * @private */ _handleApplicationEvent(event: CAPIResponse): Promise<CAPIResponse>; /** * Creates a call to specified user/s. * @classdesc creates a call between the defined users * @param {string[]} usernames - the user names for those we want to call * @returns {Promise<NXMCall>} a NXMCall object with all the call properties * @example <caption>Create a call with users</caption> * application.on("call:status:changed", (nxmCall) => { * if (nxmCall.status === nxmCall.CALL_STATUS.STARTED) { * console.log('the call has started'); * } * }); * * application.inAppCall(usernames).then(() => { * console.log('Calling user(s)...'); * }).catch((error) => { * console.error(error); * }); */ inAppCall(usernames: string[]): Promise<NXMCall>; /** * Creates a call to phone a number. * The call object is created under application.calls when the call has started. * listen for it with application.on("call:status:changed") * * You don't need to start the stream, the SDK will play the audio for you * * @classdesc creates a call to a phone number * @param {string} user the phone number or the username you want to call * @param {string} [type="phone"] the type of the call you want to have. possible values "phone" or "app" ( is "phone") * @param {object} [custom_data] custom data to be included in the call object, i.e. { yourCustomKey: yourCustomValue } * @returns {Promise<NXMCall>} * @example <caption>Create a call to a phone</caption> * application.on("call:status:changed", (nxmCall) => { * if (nxmCall.status === nxmCall.CALL_STATUS.STARTED) { * console.log('the call has started'); * } * }); * * application.callServer(phone_number).then((nxmCall) => { * console.log('Calling phone ' + phone_number); * console.log('Call Object ': nxmCall); * }).catch((error) => { * console.error(error); * }); */ callServer(user: string, type?: string, custom_data?: Object, ringingMuted?: boolean): Promise<NXMCall>; /** * Reconnect a leg to an ongoing call. * You don't need to start the stream, the SDK will play the audio for you * * @classdesc reconnect leg to an ongoing call * @param {string} conversation_id the conversation that you want to reconnect * @param {string} rtc_id the id of the leg that will be reconnected * @param {object} [mediaParams] - MediaStream params (same as Media.enable()) * @returns {Promise<NXMCall>} * @example <caption>Reconnect a leg to an ongoing call</caption> * application.reconnectCall("conversation_id", "rtc_id").then((nxmCall) => { * console.log(nxmCall); * }).catch((error) => { * console.error(error); * }); * * @example <caption>Reconnect a leg to an ongoing call without auto playing audio</caption> * application.reconnectCall("conversation_id", "rtc_id", { autoPlayAudio: false }).then((nxmCall) => { * console.log(nxmCall); * }).catch((error) => { * console.error(error); * }); * * @example <caption>Reconnect a leg to an ongoing call choosing device ID</caption> * application.reconnectCall("conversation_id", "rtc_id", { audioConstraints: { deviceId: "device_id" } }).then((nxmCall) => { * console.log(nxmCall); * }).catch((error) => { * console.error(error); * }); */ reconnectCall(conversationId: string, rtcId: string, mediaParams?: Object): Promise<NXMCall>; /** * Query the service to create a new conversation * The conversation name must be unique per application. * @param {object} [params] - leave empty to get a GUID as name * @param {string} params.name - the name of the conversation. A UID will be assigned if this is skipped * @param {string} params.display_name - the display_name of the conversation. * @returns {Promise<Conversation>} - the created Conversation * @example <caption>Create a conversation and join</caption> * application.newConversation().then((conversation) => { * //join the created conversation * conversation.join().then((member) => { * //Get the user's member belonging in this conversation. * //You can also access it via conversation.me * console.log("Joined as " + member.user.name); * }); * }).catch((error) => { * console.error(error); * }); */ newConversation(data?: Object): Promise<Conversation>; /** * Query the service to create a new conversation and join it * The conversation name must be unique per application. * @param {object} [params] - leave empty to get a GUID as name * @param {string} params.name - the name of the conversation. A UID will be assigned if this is skipped * @param {string} params.display_name - the display_name of the conversation. * @returns {Promise<Conversation>} - the created Conversation * @example <caption>Create a conversation and join</caption> * application.newConversationAndJoin().then((conversation) => { * console.log("Joined as " + conversation.me.display_name); * }).catch((error) => { * console.error("Error creating a conversation and joining ", error); * }); */ newConversationAndJoin(params?: Object): Promise<Conversation>; /** * Query the service to see if this conversation exists with the * logged in user as a member and retrieve the data object * Result added (or updated) in this.conversations * * @param {string} id - the id of the conversation to fetch * @param {string} version=Application.CONVERSATION_API_VERSION.v3 {Application.CONVERSATION_API_VERSION.v1 || Application.CONVERSATION_API_VERSION.v3} - the version of the Conversation Service API to use (v1 includes the full list of the members of the conversation but v3 does not) * @returns {Promise<Conversation>} - the requested conversation * @example <caption>Get a conversation</caption> * application.getConversation(id).then((conversation) => { * console.log("Retrieved conversation: ", conversation); * }).catch((error) => { * console.error(error); * }); */ getConversation(id: string, version?: string): Promise<Conversation>; /** * Query the service to obtain a complete list of conversations of which the * logged-in user is a member with a state of `JOINED` or `INVITED`. * @param {object} params configure s for paginated conversations query * @param {string} params.order 'asc' or 'desc' ordering of resources based on creation time * @param {number} params.page_size the number of resources returned in a single request list * @param {string} [params.cursor] string to access the starting point of a dataset * * @returns {Promise<Page<Map<Conversation>>>} - Populate Application.conversations. * @example <caption>Get Conversations</caption> * application.getConversations({ page_size: 20 }).then((conversations_page) => { * conversations_page.items.forEach(conversation => { * render(conversation) * }) * }).catch((error) => { * console.error(error); * }); * */ getConversations(params?: Object): Promise<ConversationsPage>; /** * Application listening for sync status events. * * @event Application#sync:progress * * @property {number} status.sync_progress - Percentage of fetched conversations * @example <caption>listen for changes in the synchronisation progress events on Application level</caption> * application.on("sync:progress",(status) => { * console.log(status.sync_progress); * }); */ /** * Fetching all the conversations and sync progress events */ syncConversations(conversations: Conversation[]): void; /** * Get Details of a user by using their id. If no id is present, will return your own user details. * @param {string} id - the id of the user to fetch, if skipped, it returns your own user details * @returns {Promise<User>} * @example <caption>Get User details</caption> * application.getUser(id).then((user) => { * console.log('User details: 'user); * }).catch((error) => { * console.error(error); * }); */ getUser(user_id?: string): Promise<User>; /** * Query the service to obtain a complete list of userSessions of a given user * @param {object} params configure s for paginated user sessions query * @param {string} params.order 'asc' or 'desc' ordering of resources based on creation time * @param {number} params.page_size the number of resources returned in a single request list * @param {string} [params.cursor] string to access the starting point of a dataset * @param {string} [params.user_id] the user id that the sessions are being fetched * * @returns {Promise<Page<Map<UserSession>>>} * @example <caption>Get User Sessions</caption> * application.getUserSessions({ user_id: "id", page_size: 20 }).then((user_sessions_page) => { * user_sessions_page.items.forEach(user_session => { * render(user_session) * }) * }).catch((error) => { * console.error(error); * }); * */ getUserSessions(params?: Object): Promise<UserSessionsPage>; } /** * A single conversation Object. * @class Conversation * @property {Member} me - my Member object that belongs to this conversation * @property {Application} application - the parent Application * @property {string} name - the name of the Conversation (unique) * @property {string} [display_name] - the display_name of the Conversation * @property {Map<string, Member>} [members] - the members of the Conversation keyed by a member's id * @property {Map<string, NXMEvent>} [events] - the events of the Conversation keyed by an event's id * @property {number} [sequence_number] - the last event id */ export class Conversation { log: Logger; application: Application; id: string; name: string; display_name: string; timestamp: string; members?: Map<string, Member>; events: Map<string, NXMEvent>; sequence_number: number; pageConfig: PageConfig; events_page_last: EventsPage; members_page_last: MembersPage; conversationEventHandler: ConversationEventHandler; media: Media; me: Member; emit: any; on: any; off: any; once: any; releaseGroup: any; constructor(application: Application, params?: Object); /** Update Conversation object params * @property {object} params the params to update * @private */ _updateObjectInstance(params: { id?: string; name?: string; display_name?: string; members?: Member[]; timestamp?: string; sequence_number?: number; member_id?: string; state?: string; }): void; /** * Join the given User to this Conversation. Will typically be used this to join * ourselves to a Conversation we create. * Accept an invitation if our Member has state INVITED and no user_id / user_name is given * * @param {object} [params = this.application.me.id] The User to join (s to this) * @param {string} params.user_name the user_name of the User to join * @param {string} params.user_id the user_id of the User to join * @returns {Promise<Member>} * * @example <caption>join a user to the Conversation</caption> * * conversation.join().then((member) => { * console.log("joined as member: ", member) * }).catch((error) => { * console.error("error joining conversation ", error); * }); */ join(params?: { user_name?: string; user_id?: string; }): Promise<Member>; /** * Delete a conversation * @returns {Promise} * @example <caption>delete the Conversation</caption> * * conversation.del().then(() => { * console.log("conversation deleted"); * }).catch((error) => { * console.error("error deleting conversation ", error); * }); */ del(): Promise<Object>; /** * Delete an NXMEvent (e.g. Text) * @param {NXMEvent} event * @returns {Promise} * @example <caption>delete an Event</caption> * * conversation.deleteEvent(eventToBeDeleted).then(() => { * console.log("event was deleted"); * }).catch((error) => { * console.error("error deleting the event ", error); * }); * */ deleteEvent(event: NXMEvent): Promise<void>; /** * Invite the given user (id or name) to this conversation * @param {Member} params * @param {string} [params.id or user_name] - the id or the username of the User to invite * * @returns {Promise<Member>} * * @example <caption>invite a user to a Conversation</caption> * const user_id = 'id of User to invite'; * const user_name = 'username of User to invite'; * * conversation.invite({ * id: user_id, * user_name: user_name * }).then((member) => { * displayMessage(member.state + " user: " + user_id + " " + user_name); * }).catch((error) => { * console.error("error inviting user ", error); * }); * */ invite(params?: InviteParams): Promise<Member>; /** * Invite the given user (id or name) to this conversation with media audio * @param {Member} params * @param {string} [params.id or user_name] - the id or the username of the User to invite * * @returns {Promise<Member>} * * @example <caption>invite a user to a conversation</caption> * const user_id = 'id of User to invite'; * const user_name = 'username of User to invite'; * * conversation.inviteWithAudio({ * id: user_id, * user_name: user_name * }).then((member) => { * displayMessage(member.state + " user: " + user_id + " " + user_name); * }).catch((error) => { * console.error("error inviting user ", error); * }); * */ inviteWithAudio(params?: InviteParams): Promise<Member>; /** * Leave from the Conversation * @param {object} [reason] the reason for leaving the conversation * @param {string} [reason.reason_code] the code of the reason * @param {string} [reason.reason_text] the description of the reason * @returns {Promise} * @example <caption>leave the Conversation</caption> * * conversation.leave({reason_code: "mycode", reason_text: "my reason for leaving"}).then(() => { * console.log("successfully left conversation"); * }).catch((error) => { * console.error("error leaving conversation ", error); * }); * */ leave(reason?: { reason_code?: string; reason_text?: string; }): Promise<Object>; /** * Send a text message to the conversation, which will be relayed to every other member of the conversation * @param {string} text - the text message to be sent * * @returns {Promise<TextEvent>} - the text message that was sent * * @example <caption> sending a text </caption> * conversation.sendText("Hi Vonage").then((event) => { * console.log("message was sent", event); * }).catch((error)=>{ * console.error("error sending the message ", error); * }); * * @deprecated since version 8.3.0 * */ sendText(text: string): Promise<TextEvent>; /** * Send a custom event to the Conversation * @param {object} params - params of the custom event * @param {string} params.type the name of the custom event. Must not exceed 100 char length and contain only alpha numerics and '-' and '_' characters. * @param {object} params.body customizable key value pairs * * @returns {Promise<NXMEvent>} - the custom event that was sent * * @example <caption> sending a custom event </caption> * conversation.sendCustomEvent({ type: "my-event", body: { mykey: "my value" }}).then((event) => { * console.log("custom event was sent", event); * }).catch((error)=>{ * console.error("error sending the custom event", error); * }); * */ sendCustomEvent({ type, body }: { type: string; body: Object; }): Promise<NXMEvent>; /** * Uploads an Image to Media Service. * implements xhr (https://xhr.spec.whatwg.org/) - this.imageRequest * * @param {File} file single input file (jpeg/jpg) * @param {object} params - params of image sent * @param {string} [params.quality_ratio = 100] a value between 0 and 100. 0 indicates 'maximum compression' and the lowest quality, 100 will result in the highest quality image * @param {string} [params.medium_size_ratio = 50] a value between 1 and 100. 1 indicates the new image is 1% of original, 100 - same size as original * @param {string} [params.thumbnail_size_ratio = 30] a value between 1 and 100. 1 indicates the new image is 1% of original, 100 - same size as original * * @returns {Promise<XMLHttpRequest>} * * @example <caption>uploading an image</caption> * const params = { * quality_ratio : "90", * medium_size_ratio: "40", * thumbnail_size_ratio: "20" * } * conversation.uploadImage(fileInput.files[0], params).then((uploadImageRequest) => { * uploadImageRequest.onprogress = (e) => { * console.log("Image request progress: ", e); * console.log("Image progress: " + e.loaded + "/" + e.total); * }; * uploadImageRequest.onabort = (e) => { * console.log("Image request aborted: ", e); * console.log("Image: " + e.type); * }; * uploadImageRequest.onloadend = (e) => { * console.log("Image request successful: ", e); * console.log("Image: " + e.type); * }; * uploadImageRequest.onreadystatechange = () => { * if (uploadImageRequest.readyState === 4 && uploadImageRequest.status === 200) { * const representations = JSON.parse(uploadImageRequest.responseText); * console.log("Original image url: ", representations.original.url); * console.log("Medium image url: ", representations.medium.url); * console.log("Thumbnail image url: ", representations.thumbnail.url); * } * }; * }).catch((error) => { * console.error("error uploading the image ", error); * }); */ uploadImage(fileInput: File, params?: { quality_ratio: string; medium_size_ratio: string; thumbnail_size_ratio: string; }): Promise<XMLHttpRequest>; /** * Send an Image message to the conversation, which will be relayed to every other member of the conversation. * implements xhr (https://xhr.spec.whatwg.org/) - this.imageRequest * * @param {File} file single input file (jpeg/jpg) * @param {object} params - params of image sent * @param {string} [params.quality_ratio = 100] a value between 0 and 100. 0 indicates 'maximum compression' and the lowest quality, 100 will result in the highest quality image * @param {string} [params.medium_size_ratio = 50] a value between 1 and 100. 1 indicates the new image is 1% of original, 100 - same size as original * @param {string} [params.thumbnail_size_ratio = 30] a value between 1 and 100. 1 indicates the new image is 1% of original, 100 - same size as original * * @returns {Promise<XMLHttpRequest>} * * @example <caption>sending an image</caption> * const params = { * quality_ratio : "90", * medium_size_ratio: "40", * thumbnail_size_ratio: "20" * } * conversation.sendImage(fileInput.files[0], params).then((imageRequest) => { * imageRequest.onprogress = (e) => { * console.log("Image request progress: ", e); * console.log("Image progress: " + e.loaded + "/" + e.total); * }; * imageRequest.onabort = (e) => { * console.log("Image request aborted: ", e); * console.log("Image: " + e.type); * }; * imageRequest.onloadend = (e) => { * console.log("Image request successful: ", e); * console.log("Image: " + e.type); * }; * }).catch((error) => { * console.error("error sending the image ", error); * }); * * @deprecated since version 8.3.0 */ sendImage(fileInput: File, params?: { quality_ratio: string; medium_size_ratio: string; thumbnail_size_ratio: string; }): Promise<XMLHttpRequest>; /** * Cancel uploading or sending an Image message to the conversation. * * @param {XMLHttpRequest} imageRequest * * @returns void * * @example <caption>cancel sending an image</caption> * conversation.sendImage(fileInput.files[0]).then((imageRequest) => { * conversation.abortSendImage(imageRequest); * }).catch((error) => { * console.error("error sending the image ", error); * }); * * @example <caption>cancel uploading an image</caption> * conversation.uploadImage(fileInput.files[0]).then((imageRequest) => { * conversation.abortSendImage(imageRequest); * }).catch((error) => { * console.error("error uploading the image ", error); * }); */ abortSendImage(imageRequest: XMLHttpRequest): void | NexmoClientError; /** * Send a message event to the conversation, which will be relayed to every other member of the conversation * * @param {object} params the content of the message you want sent * @param {string} params.message_type the type of the message. It should be one of 'text', 'image', 'audio', 'video', 'file' * @param {string} [params.text] the text content when message type is 'text * @param {object} [params.image] * @param {string} params.image.url the image url when message type is 'image' * @param {object} [params.audio] * @param {string} params.audio.url the audio url when message type is 'audio' * @param {object} [params.video] * @param {string} params.video.url the video url when message type is 'video' * @param {object} [params.file] * @param {string} params.file.url the file url when message type is 'file' * * @returns {Promise<MessageEvent>} - the message that was sent * * @example <caption> sending a message </caption> * conversation.sendMessage({ "message_type": "text", "text": "Hi Vonage!" }).then((event) => { * console.log("message was sent", event); * }).catch((error)=>{ * console.error("error sending the message ", error); * }); * */ sendMessage(params: MessageEventParams): Promise<TextEvent>; _typing(state: string): Promise<string>; /** * Send start typing indication * * @returns {Promise} - resolves the promise on successful sent * * @example <caption>send start typing event when key is pressed</caption> * messageTextarea.addEventListener('keypress', (event) => { * conversation.startTyping(); * }); */ startTyping(): Promise<string>; /** * Send stop typing indication * * @returns {Promise} - resolves the promise on successful sent * * @example <caption>send stop typing event when a key has not been pressed for half a second</caption> * let timeout = null; * messageTextarea.addEventListener('keyup', (event) => { * clearTimeout(timeout); * timeout = setTimeout(() => { * conversation.stopTyping(); * }, 500); * }); */ stopTyping(): Promise<string>; /** * Query the service to get a list of events in this conversation. * * @param {object} params configure s for paginated events query * @param {string} params.order 'asc' or 'desc' ordering of resources based on creation time * @param {number} params.page_size the number of resources returned in a single request list * @param {string} [params.cursor] string to access the starting point of a dataset * @param {string} [params.event_type] the type of event used to filter event requests. Supports wildcard options with :* eg. 'members:*' * * @returns {Promise<EventsPage<Map<Events>>>} - Populate Conversations.events. * @example <caption>Get Events</caption> * conversation.getEvents({ event_type: 'member:*' }).then((events_page) => { * events_page.items.forEach(event => { * render(event) * }) * }).catch((error) => { * console.error("error getting the events ", error); * }); */ getEvents(params?: Object): Promise<EventsPage>; /** * Query the service to get a list of members in this conversation. * * @param {object} params configure s for paginated events query * @param {string} params.order 'asc' or 'desc' ordering of resources based on creation time * @param {number} params.page_size the number of resources returned in a single request list * @param {string} [params.cursor] string to access the starting point of a dataset * * @returns {Promise<MembersPage<Map<Member>>>} * @example <caption>Get Members</caption> * const params = { * order: "desc", * page_size: 100 * } * conversation.getMembers(params).then((members_page) => { * members_page.items.forEach(member => { * render(member) * }) * }).catch((error) => { * console.error("error getting the members ", error); * }); */ getMembers(params?: Object): Promise<MembersPage>; /** * Query the service to get my member in this conversation. * * @returns {Promise<Member>} * @example <caption>Get My Member</caption> * conversation.getMyMember().then((member) => { * render(member) * }).catch((error) => { * console.error("error getting my member", error); * }); */ getMyMember(): Promise<Member>; /** * Query the service to get a member in this conversation. * * @param {string} member_id the id of the member to return * * @returns {Promise<Member>} * @example <caption>Get Member</caption> * conversation.getMember("MEM-id").then((member) => { * render(member) * }).catch((error) => { * console.error("error getting member", error); * }); */ getMember(member_id: string): Promise<Member>; /** * Handle and event from the cloud. * using conversationEventHandler * @param {object} event * @private */ _handleEvent(event: CAPIResponse): Promise<CAPIResponse>; } /** * An image event * * @class ImageEvent * @extends NXMEvent */ export class ImageEvent extends NXMEvent { log: Logger; constructor(conversation: Conversation, params?: { body?: { timestamp?: string; representations?: ImageRepresentations; }; }); /** * Set the imageEvent status to 'seen' * @returns {Promise} * @example <caption>Set the imageEvent status to 'seen'</caption> * imageEvent.seen().then(() => { * console.log("image event status set to seen"); * }).catch((error)=>{ * console.log("error setting image event status to seen ", error); * }); */ seen(): Promise<void>; /** * Set the imageEvent status to 'delivered' * @returns {Promise} * @example <caption>Set the imageEvent status to 'delivered'</caption> * imageEvent.delivered().then(() => { * console.log("image event status set to delivered"); * }).catch((error)=>{ * console.log("error setting image event status to delivered ", error); * }); */ delivered(): Promise<void>; /** * Delete the image event, all 3 representations of it * passing only the one of the three URLs * @returns {Promise} * @example <caption>Delete the imageEvent</caption> * imageEvent.del().then(() => { * console.log("image event deleted"); * }).catch((error)=>{ * console.log("error deleting image event ", error); * }); */ del(): Promise<void>; /** * Download an Image from Media service //3 representations * @param {string} [type="thumbnail"] original, medium, or thumbnail * @param {string} [representations=this.body.representations] the ImageEvent.body for the image to download * @returns {string} the dataUrl "data:image/jpeg;base64..." * @example <caption>Downloading an image from the imageEvent</caption> * imageEvent.fetchImage("medium").then((imageData) => { * const img = new Image(); * img.src = imageData; * document.body.appendChild(img); * }).catch((error)=>{ * console.log("error getting image ", error); * }); */ fetchImage(type?: string, imageRepresentations?: ImageRepresentations): Promise<string>; } /** * A message event * * @class MessageEvent * @extends NXMEvent */ export class MessageEvent extends NXMEvent { log: Logger; constructor(conversation: Conversation, params?: { body?: { timestamp?: string; }; }); /** * Set the messageEvent status to 'seen' * @returns {Promise} * @example <caption>Set the messageEvent status to 'seen'</caption> * messageEvent.seen().then(() => { * console.log("message event status set to seen"); * }).catch((error)=>{ * console.log("error setting message event status to seen ", error); * }); */ seen(): Promise<void>; /** * Set the messageEvent status to 'delivered'. * handled by the SDK * @returns {Promise} * @example <caption>Set the messageEvent status to 'delivered'</caption> * messageEvent.delivered().then(() => { * console.log("message event status set to delivered"); * }).catch((error)=>{ * console.log("error setting message event status to delivered ", error); * }); */ delivered(): Promise<void>; /** * Delete the messageEvent * @returns {Promise} * @example <caption>Delete the messageEvent</caption> * messageEvent.del().then(() => { * console.log("message event deleted"); * }).catch((error)=>{ * console.log("error deleting message event ", error); * }); */ del(): Promise<void>; /** * Download an Image from Media service * @returns {string} the dataUrl "data:image/jpeg;base64..." * @example <caption>Downloading an image from the messageEvent</caption> * messageEvent.fetchImage().then((imageData) => { * const img = new Image(); * img.src = imageData; * document.body.appendChild(img); * }).catch((error) => { * console.log("error getting image ", error); * }); */ fetchImage(): Promise<string>; } /** * Conversation NXMEvent Object. * The super class that holds the base events that apply to * common event objects. * @class NXMEvent */ export class NXMEvent { conversation: Conversation; type: string; cid: string; from: string; timestamp: Object; id: string; state: { delivered_to?: { [key: string]: string; }; seen_by?: { [key: string]: string; }; submitted_to?: { [key: string]: string; }; rejected_by?: { [key: string]: string; }; undeliverable_to?: { [key: string]: string; }; }; index: number; streamIndex: number; body?: { user?: { user_id?: string; display_name?: string; id?: string; media?: { audio_settings?: { enabled?: boolean; }; }; }; member_id?: string; sdp?: string; digit?: number; digits?: number; representations?: any; timestamp?: Object; text?: string; channel?: Channel; invited_by?: string | null; message_type?: string; image?: { url: string; }; audio?: { url: string; }; video?: { url: string; }; file?: { url: string; }; }; digit: number; application_id: string; constructor(conversation: Conversation, params?: { type?: string; application_id?: string; cid?: string; from?: string; timestamp?: string; id?: string; state?: { delivered_to?: { [key: string]: string; }; seen_by?: { [key: string]: string; }; submitted_to?: { [key: string]: string; }; rejected_by?: { [key: string]: string; }; undeliverable_to?: { [key: string]: string; }; }; index?: number; streamIndex?: number; body?: Object; _embedded?: { from_user?: { display_name?: string; id?: string; name?: string; }; from_member?: { display_name?: string; id?: string; name?: string; }; }; }); /** * Delete the event * @param {number} [event_id=this.event_id] if the event id param is not present, "this" event will be * @returns {Promise} * @private */ del(event_id?: string): Promise<void>; /** * Mark as Delivered the event * @param {number} [event_id=this.event_id] if the event id is not provided, the this event will be used * @returns {Promise} * @private */ delivered(event_id?: string): Promise<void>; /** * Mark as Seen the event * @param {number} [event_id=this.event_id] if the event id is not provided, the this event will be used * @returns {Promise} * @private */ seen(event_id?: string): Promise<void>; } /** * A text event * * @class TextEvent * @extends NXMEvent */ export class TextEvent extends NXMEvent { constructor(conversation: Conversation, params?: { body?: { timestamp?: string; }; }); /** * Set the textEvent status to 'seen' * @returns {Promise} * @example <caption>Set the textEvent status to 'seen'</caption> * textEvent.seen().then(() => { * console.log("text event status set to seen"); * }).catch((error)=>{ * console.log("error setting text event status to seen ", error); * }); */ seen(): Promise<void>; /** * Set the textEvent status to 'delivered'. * handled by the SDK * @returns {Promise} * @example <caption>Set the textEvent status to 'delivered'</caption> * textEvent.delivered().then(() => { * console.log("text event status set to delivered"); * }).catch((error)=>{ * console.log("error setting text event status to delivered ", error); * }); */ delivered(): Promise<void>; /** * Delete the textEvent * @returns {Promise} * @example <caption>Delete the textEvent</caption> * textEvent.del().then(() => { * console.log("text event deleted"); * }).catch((error)=>{ * console.log("error deleting text event ", error); * }); */ del(): Promise<void>; } /** * Handle Application Events * * @class ApplicationEventsHandler * @param {Application} application * @param {Conversation} conversation * @private */ export class ApplicationEventsHandler { log: Logger; application: Application; _handleApplicationEventMap: { [key: string]: Function; }; constructor(application: Application); /** * Handle and event. * * Update the event to map local generated events * in case we need a more specific event to pass in the application listener * or f/w the event as it comes * @param {object} event * @private */ handleEvent(event: CAPIResponse): any; /** * case: call to PSTN, after knocking event we receive joined * @private */ private _processMemberJoined; private _processMemberInvited; } /** * Handle Conversation Events * * @class ConversationEventsHandler * @param {Application} application * @param {Conversation} conversation * @private */ export class ConversationEventHandler { log: Logger; application: Application; conversation: Conversation; constructed_event: NXMEvent; _handleEventMap: { [key: string]: Function; }; constructor(application: Application, conversation: Conversation); /** * Handle and event. * * Identify the type of the event, * create the corresponding Class instance * emit to the corresponding Objects * @param {object} event * @private */ handleEvent(event: CAPIResponse): NXMEvent; /** * Mark the requested event as delivered * use that event as constructed to update the local events' map * @param {object} event * @returns the NXMEvent that is marked as delivered * @private */ private _processDelivered; /** * Delete the requested event * empty the payload of the event (text, image or message) * use that event as constructed to update the local events map * @param {object} event * @returns the deleted events * @private */ private _processDelete; /** * Return an ImageEvent with the corresponding image data * @param {object} event * @returns {ImageEvent} */ private _processImage; /** * Handle events for member state changes (joined, invited, left) * in conversation level. * Other members are going through here too. * For .me member initial event (join, invite) use the application listener * @param {object} event * @returns {NXMEvent} * @private */ private _processMember; /** * Handle events for leg status updates in conversation level. * Other member's legs are going through here too. * @param {object} event * @returns {NXMEvent} * @private */ private _processLegStatus; /** * Handle member:media events * use a call object if and the member object * @param {object} event * @private */ private _processMedia; /** * Handle *:mute:* events * @param {object} event * @private */ private _processMuteForMedia; /** * Mark the requested event as seen * use that event as constructed to update the local Events map * @param {object} event * @private */ private _processSeen; /** * Create the TextEvent object and trigger .delivered() * @param {object} event * @private */ private _processText; /** * Create the MessageEvent object and trigger .delivered() * @param {object} event * @private */ private _processMessage; /** * Mark the requested event as submitted * use that event as constructed to update the local Events map * @param {object} event * @private */ private _processSubmitted; /** * Mark the requested event as rejected * use that event as constructed to update the local Events map * @param {object} event * @private */ private _processRejected; /** * Mark the requested event as undeliverable * use that event as constructed to update the local Events map * @param {object} event * @private */ private _processUndeliverable; } /** * Handle Mapping of Conversation Ids to ConversationEventsProcessor * * @class EventsQueue * @private */ export declare class EventsQueue { callback: any; cidMap: Map<String, ConversationEventsProcessor>; log: Logger; constructor(callback: any); enqueue(event: CAPIResponse, application: Application): Promise<any>; } /** * Handle Ordering of Conversation Events for Processing * * @class ConversationEventsProcessor * @private */ export declare class ConversationEventsProcessor { cid: string; eventsMap: Map<Number, CAPIResponse>; lastEventIdProcessed: number; largestEventIdInQueue: number; callback: any; processing: Boolean; application: Application; eventsFetchRange: number; log: Logger; constructor(cid: string, lastEventIdProcessed: number, application: Application); enqueue(eventId: number, event: CAPIResponse): CAPIResponse; dequeue(eventId: number): CAPIResponse; processEvents(): Promise<any>; processNextEvent(eventId: number): Promise<CAPIResponse>; fetchEventsAndProcess(missingEvent: number): Promise<CAPIResponse>; fetchConversationEvents(start_id: number, range: number): Promise<Array<CAPIResponse>>; } /** * Handle rtc Events * * @class RtcEventHandler * @private */ export class RtcEventHandler { log: Logger; application: Application; _handleRtcEventMap: { [key: string]: Function; }; constructor(application: Application); /** * Entry point for rtc events * @param {object} event * @private */ _handleRtcEvent(event: CAPIResponse): void; /** * on transfer event * update the conversation object in the NXMCall, * update the media object in the new conversation * set `transferred_to` <Conversation> on the member that is transferred * @param {object} event * @private */ private _processRtcTransfer; /** * Handle rtc:answer event * * @param {object} event * @private */ private _processRtcAnswer; /** * Handle rtc:hangup event * * @param {object} event * @private */ private _processRtcHangup; } /** * Handle sip Events * * @class SipEventHandler * @private */ export class SipEventHandler { log: Logger; application: Application; _handleSipCallEventMap: { [key: string]: Function; }; constructor(application: Application); /** * Entry point for sip events * The event belongs to a call Object * @private */ _handleSipCallEvent(event: CAPIResponse): any; /** * Handle sip:hangup event * * @param {object} event_call * @private */ private _processSipHangup; /** * Handle sip:ringing event * * @param {object} event_call * @private */ private _processSipRinging; } /** * An individual user (i.e. conversation member). * @class Member * @param {Conversation} conversation * @param {object} params */ export class Member { conversation: Conversation; callStatus: string | null; id: string; user?: any; channel: any; timestamp: { invited?: string; joined?: string; left?: string; }; state: string; display_name: string; invited_by: string; user_id: string; name: string; media?: Media; stream?: MediaStream; pc?: RTCPeerConnection; emit: any; on: any; off: any; once: any; transferred_to?: Conversation; transferred_from?: Conversation; [key: string]: any; constructor(conversation: Conversation, params?: Object); /** * Update object instance and align attribute names * * Handle params input to keep consistent the member object * @param {object} params member attributes * @private */ _normalise(params?: any): void; /** * Play the given stream only to this member within the conversation * * @param {string} [params] * * @returns {Promise<NXMEvent>} * @private */ playStream(params: Object): Promise<NXMEvent>; /** * Speak the given text only to this member within the Conversation. * * @param {string} [params] * * @returns {Promise<NXMEvent>} * @private */ sayText(params: { text: string; voice_name?: string; level?: number; queue?: boolean; loop?: number; ssml?: boolean; }): Promise<NXMEvent>; /** * Kick a Member from the Conversation. * * @param {object} [reason] the reason for kicking out a member * @param {string} [reason.reason_code] the code of the reason * @param {string} [reason.reason_text] the description of the reason * @example <caption>Remove a member from the Conversation.</caption> * // Remove a member * member.kick({reason_code: "Reason Code", reason_text: "Reason Text"}) * .then(() => { * console.log("Successfully removed member."); * }).catch((error) => { * console.err