import { UAParser } from 'ua-parser-js' ; import AudioVideoController from '../audiovideocontroller/AudioVideoController' ; import AudioVideoObserver from '../audiovideoobserver/AudioVideoObserver' ; import EventReporter from '../eventreporter/EventReporter' ; import Versioning from '../versioning/Versioning' ; import AudioVideoEventAttributes from './AudioVideoEventAttributes' ; import DeviceEventAttributes from './DeviceEventAttributes' ; import EventAttributes from './EventAttributes' ; import EventController from './EventController' ; import EventName from './EventName' ; import flattenEventAttributes from './flattenEventAttributes' ; import MeetingHistoryState from './MeetingHistoryState' ; export default class DefaultEventController implements EventController { internal private static readonly UNAVAILABLE = 'Unavailable' ; internal private parserResult : any ; internal private browserMajorVersion : string ; internal private meetingHistoryStates : { name : MeetingHistoryState ; timestampMs : number ; } [ ] = [ ] ; internal private browserName : string ; internal private browserVersion : string ; internal private deviceName : string ; constructor ( private audioVideoController , private eventReporter ? ) { try { this . parserResult = navigator && navigator . userAgent ? new UAParser ( navigator . userAgent ) . getResult ( ) : null ; } catch ( error ) { audioVideoController . logger . error ( error . message ) ; } this . browserMajorVersion = this . parserResult ?. browser ?. version ?. split ( '.' ) [ 0 ] || DefaultEventController . UNAVAILABLE ; this . browserName = this . parserResult ?. browser . name || DefaultEventController . UNAVAILABLE ; this . browserVersion = this . parserResult ?. browser . version || DefaultEventController . UNAVAILABLE ; this . deviceName = [ this . parserResult ?. device . vendor || '' , this . parserResult ?. device . model || '' ] . join ( ' ' ) . trim ( ) || DefaultEventController . UNAVAILABLE ; } async publishEvent ( name , attributes ? ) { const timestampMs = Date . now ( ) ; this . meetingHistoryStates . push ( { name , timestampMs , } ) ; const eventAttributes = Object . freeze ( Object . assign ( this . getAttributes ( timestampMs ) , attributes ) ) ; this . audioVideoController . forEachObserver ( ( observer ) => { if ( observer . eventDidReceive ) { observer . eventDidReceive ( name , eventAttributes ) ; } } ) ; this . reportEvent ( name , timestampMs , attributes ) ; } private async reportEvent ( name , timestampMs , attributes ? ) { let flattenedAttributes ; try { if ( attributes ) { flattenedAttributes = flattenEventAttributes ( attributes ) ; } await this . eventReporter ?. reportEvent ( timestampMs , name , flattenedAttributes ) ; } catch ( error ) { this . audioVideoController . logger . error ( `Error reporting event ${ error }` ) ; } } async pushMeetingState ( state , timestampMs = Date . now ( ) ) { this . meetingHistoryStates . push ( { name : state , timestampMs , } ) ; this . reportEvent ( state , timestampMs ) ; } private getAttributes ( timestampMs ) { return { attendeeId : this . audioVideoController . configuration . credentials . attendeeId , browserMajorVersion : this . browserMajorVersion , browserName : this . browserName , browserVersion : this . browserVersion , deviceName : this . deviceName , externalMeetingId : typeof this . audioVideoController . configuration . externalMeetingId === 'string' ? this . audioVideoController . configuration . externalMeetingId : '' , externalUserId : this . audioVideoController . configuration . credentials . externalUserId , meetingHistory : this . meetingHistoryStates , meetingId : this . audioVideoController . configuration . meetingId , osName : this . parserResult ?. os . name || DefaultEventController . UNAVAILABLE , osVersion : this . parserResult ?. os . version || DefaultEventController . UNAVAILABLE , sdkVersion : Versioning . sdkVersion , sdkName : Versioning . sdkName , timestampMs , } ; } }