import { Injectable } from '@angular/core' ; import { UserModel } from '../../models/user-model' ; import { StreamEvent , Subscriber , ConnectionEvent } from 'openvidu-browser' ; import { LoggerService } from '../logger/logger.service' ; import { ILogger } from '../../types/logger-type' ; import { UtilsService } from '../utils/utils.service' ; import { UserName } from '../../types/username-type' ; import { Observable } from 'rxjs/internal/Observable' ; import { BehaviorSubject } from 'rxjs' ; import { AvatarService } from '../avatar/avatar.service' ; @ Injectable ( { providedIn : 'root' } ) export class RemoteUsersService { remoteUsers : Observable < UserModel [ ] > ; remoteUserNameList : Observable < UserName [ ] > ; private _remoteUsers = < BehaviorSubject < UserModel [ ] > > new BehaviorSubject ( [ ] ) ; private _remoteUserNameList = < BehaviorSubject < UserName [ ] > > new BehaviorSubject ( [ ] ) ; private users : UserModel [ ] = [ ] ; private log : ILogger ; constructor ( private loggerSrv , private utilsSrv , private avatarService ) { this . log = this . loggerSrv . get ( 'RemoteService' ) ; this . remoteUsers = this . _remoteUsers . asObservable ( ) ; this . remoteUserNameList = this . _remoteUserNameList . asObservable ( ) ; } updateUsers ( ) { this . _remoteUsers . next ( this . users ) ; } add ( event , subscriber ) { let nickname = '' ; let avatar = '' ; const connectionId = ( < StreamEvent > event ) ?. stream ?. connection ?. connectionId || ( < ConnectionEvent > event ) ?. connection ?. connectionId ; const data = ( < StreamEvent > event ) ?. stream ?. connection ?. data || ( < ConnectionEvent > event ) ?. connection ?. data ; nickname = this . utilsSrv . getNicknameFromConnectionData ( data ) ; avatar = this . avatarService . getAvatarFromConnectionData ( data ) ; const newUser = new UserModel ( connectionId , subscriber , nickname ) ; newUser . setAvatar ( avatar ) ; this . addUser ( newUser ) ; this . updateUsers ( ) ; } removeUserByConnectionId ( connectionId ) { this . log . w ( 'Deleting user: ' , connectionId ) ; const user = this . getRemoteUserByConnectionId ( connectionId ) ; const index = this . users . indexOf ( user , 0 ) ; if ( index > - 1 ) { this . users . splice ( index , 1 ) ; this . updateUsers ( ) ; } } someoneIsSharingScreen ( ) { return this . users . some ( ( user ) => user . isScreen ( ) ) ; } toggleUserZoom ( connectionId ) { const user = this . getRemoteUserByConnectionId ( connectionId ) ; user . setVideoSizeBig ( ! user . isVideoSizeBig ( ) ) ; } resetUsersZoom ( ) { this . users . forEach ( ( u ) => u . setVideoSizeBig ( false ) ) ; } setUserZoom ( connectionId , zoom ) { this . getRemoteUserByConnectionId ( connectionId ) ?. setVideoSizeBig ( zoom ) ; } getRemoteUserByConnectionId ( connectionId ) { return this . users . find ( ( u ) => u . getConnectionId ( ) === connectionId ) ; } updateNickname ( connectionId , nickname ) { const user = this . getRemoteUserByConnectionId ( connectionId ) ; user ?. setNickname ( nickname ) ; this . _remoteUsers . next ( this . users ) ; const remoteUserNameList = this . _remoteUserNameList . getValue ( ) ; remoteUserNameList . forEach ( ( element ) => { if ( element . connectionId === connectionId ) { element . nickname = nickname ; return ; } } ) ; this . _remoteUserNameList . next ( remoteUserNameList ) ; } clear ( ) { this . _remoteUsers = < BehaviorSubject < UserModel [ ] > > new BehaviorSubject ( [ ] ) ; this . remoteUsers = this . _remoteUsers . asObservable ( ) ; this . _remoteUserNameList = < BehaviorSubject < UserName [ ] > > new BehaviorSubject ( [ ] ) ; this . remoteUserNameList = this . _remoteUserNameList . asObservable ( ) ; this . users = [ ] ; } getUserAvatar ( connectionId ) { return this . getRemoteUserByConnectionId ( connectionId ) ?. getAvatar ( ) || this . avatarService . getOpenViduAvatar ( ) ; } addUserName ( event ) { const nickname = this . utilsSrv . getNicknameFromConnectionData ( event . connection . data ) ; const connectionId = event . connection . connectionId ; const newUserNameList = this . _remoteUserNameList . getValue ( ) ; newUserNameList . push ( { nickname , connectionId } ) ; this . _remoteUserNameList . next ( newUserNameList ) ; } deleteUserName ( event ) { const oldUserNameList = this . _remoteUserNameList . getValue ( ) ; const newUserNameList = oldUserNameList . filter ( ( element ) => element . connectionId !== event . connection . connectionId ) ; this . _remoteUserNameList . next ( newUserNameList ) ; } private addUser ( user ) { const oldUser = this . getRemoteUserByConnectionId ( user . connectionId ) ; if ( ! ! oldUser ) { oldUser . setStreamManager ( user . getStreamManager ( ) ) ; return ; } this . users . push ( user ) ; } }