Home Reference Source

src/conferences/ConferenceLogEntry.js

import {cloneDeep} from 'lodash-es';
/**
 * @typedef Candidate
 * @type {object}
 * @property {string} address Identifier of the user that received the invite
 * @property {603 | 408 | 487 | 200} endReason A code that indicates if the candidates accepted or rejected
 * the invite and his reason
 */

/**
 * @typedef MediaType
 * @type {object}
 * @property {boolean} audio Indicate if the creator of the invite wants to use audio in the conference
 * @property {boolean} video Indicate if the creator of the invite wants to use video in the conference
 */

/**
 * @typedef {Object} Invite
 * @property {string} from who sent the invite
 * @property {string} to who received the invite
 * @property {string} state specify what happened with the invite
 * @property {Array<Candidate>} candidates List of candidates that received the invite
 * @property {boolean} group Set to true when the invite is sent to a group
 * @property {boolean} inviteRequest Set to true when the invite is related with an invitation request
 * @property {MediaType} mediaType Indicates the media used in the conference
 * @property {603 | 408 | 487 | ''} error Code error when an invite is declined
 */

/**
 * @typedef Transfer
 * @type {object}
 * @property {string} from Who creates the call-transfer
 * @property {string} to Who has to receive the call transferred
 * @property {string} state State of the call transfer
 * @property {string} roomId Identifier of the room
 * @property {string} idInvite Identifier of the invite related with the transfer
 */

/**
 * @typedef Participant
 * @type {object}
 * @property {string} address - Identifier of the user that entried in the room
 * @property {number} createdAt - Timestamp of when user inserted in the room
 * @property {number} finishedAt - Timestamp of when user left the room
 */

/**
 * @private
 */
const getState = (state, direction) => {
	switch (state) {
		case 'ACCEPTED':
		case 200:
			return 'accepted';
		case 'DECLINED':
		case 603:
			return 'rejected';
		case 'DECLINED_BY_CREATOR':
			return direction === 'outgoing' ?
				'rejected' :
				'missed';
		case 'DECLINED_BY_TIMEOUT':
		case 408:
			return 'missed';
		// no default
	}
};

/**
 * This class allows access to the details of each entry of the conference log
 * @link {ConferenceLog#.getEntries()} method should be used to obtain the list
 * of instances of this class.
 */
export class ConferenceLogEntry {
	/**
	 * @public
	 * @return {ConferenceLogEntry}
	 */
	static of(data) {
		return Object.freeze(new ConferenceLogEntry(data));
	}

	/** @private */
	constructor(conference) {
		/** @private */
		this.raw = cloneDeep(conference);

		/** @private */
		this.invites = Object.freeze((this.raw.invites || []).map((invite) => {
			invite.getCandidates = () => invite.candidates.map(candidate => ({
				address: candidate.address,
				state: getState(invite.group ? candidate.endReason : invite.state, 'incoming'),
				direction: 'incoming',
				type: invite.inviteRequest ? 'inviteRequest' : 'invite',
				group: invite.group,
			}));
			return invite;
		}));

		/** @private */
		this.transfers = Object.freeze((this.raw.transfers || []).map((transfer) => {
			transfer.getParticipants = () => [{
				address: transfer.from,
				state: getState(transfer.state, 'outgoing'),
				direction: 'outgoing',
			}, {
				address: transfer.to,
				state: getState(transfer.state, 'incoming'),
				direction: 'incoming',
			}];
			return transfer;
		}));

		/** @private */
		this.participants = Object.freeze(this.raw.participants);
	}

	/**
	 * The unique id that identifies the conference log entry
	 * @return {string}
	 */
	getId() {
		return this.raw.id;
	}

	/**
	 * Conference length in ms
	 * @return {number}
	 */
	getDuration() {
		return this.raw.finishedAt - this.raw.createdAt;
	}

	/**
	 * The time the conference started
	 * @return {number}
	 */
	getStartTimestamp() {
		return this.raw.createdAt;
	}

	/**
	 * The time the conference started
	 * @return {Date}
	 */
	getStartDateTime() {
		return new Date(this.raw.createdAt);
	}

	/**
	 * The time the conference ended
	 * @return {Date}
	 */
	getEndDateTime() {
		return new Date(this.raw.finishedAt);
	}

	/**
	 * Get a list of users that has been inside the room
	 * @return {Participant[]}
	 */
	getParticipants() {
		return this.participants;
	}

	/**
	 * Get a list of all invites that have been sent in the room
	 * @return {Invite[]}
	 */
	getInvites() {
		return this.invites;
	}

	/**
	 * Get a list of all transfers that have been performed in the room
	 * @return {Transfer[]}
	 */
	getTransfers() {
		return this.transfers;
	}
}