# All files / src/lib/services/remote-user remote-user.service.ts

26.31% Statements 20/76
12.5% Branches 10/80
13.63% Functions 3/22
22.72% Lines 15/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    1401x
    1x
    1x
    1x
    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

     import { Injectable } from '@angular/core';
    import { BehaviorSubject } from 'rxjs';
    impoIrt { Observable } from 'rxjs/internal/Observable';

    import { StreamEvent, Subscriber, ConnectionEvent } from 'openvidu-browser';

    import { UserModel } from '../../models/user.model';
    import { UserName } from '../../models/username.model';
    import { ILogger } from '../../models/logger.model';

    import { LoggerService } from '../logger/logger.service';

    @Injectable({
    	providedIn: 'root'
    })
    export class RemoteUserService {
    	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: LoggerService) {
    		this.log = this.loggerSrv.get('RemoteService');
    		this.remoteUsers = this._remoteUsers.asObservable();
    		this.remoteUserNameList = this._remoteUserNameList.asObservable();
    	}

    	updateUsers() {
    		this._remoteUsers.next(this.users);
    	}

    	add(event: StreamEvent | ConnectionEvent, subscriber: Subscriber) {
    		const connectionId = (<StreamEvent>event)?.stream?.connection?.connectionId || (<ConnectionEvent>event)?.connection?.connectionId;
    		const data = (<StreamEvent>event)?.stream?.connection?.data || (<ConnectionEvent>event)?.connection?.data;
    		const nickname = this.getNicknameFromConnectionData(data);
    		const newUser = new UserModel(connectionId, subscriber, nickname);
    		// Add new user (connectionCreated Event) or assign the streamManager to old user when the connnectionId exists (streamCreated Event)
    		this.addUser(newUser);
    		this.updateUsers();
    	}

    	removeUserByConnectionId(connectionId: string) {
    		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(): boolean {
    		return this.users.some((user) => user.isScreen());
    	}

    	toggleUserZoom(connectionId: string) {
    		const user = this.getRemoteUserByConnectionId(connectionId);
    		user.setVideoSizeBig(!user.isVideoSizeBig());
    	}

    	resetUsersZoom() {
    		this.users.forEach((u) => u.setVideoSizeBig(false));
    	}

    	setUserZoom(connectionId: string, zoom: boolean) {
    		this.getRemoteUserByConnectionId(connectionId)?.setVideoSizeBig(zoom);
    	}

    	getRemoteUserByConnectionId(connectionId: string): UserModel {
    		return this.users.find((u) => u.getConnectionId() === connectionId);
    	}

    	updateNickname(connectionId: any, data: any) {
    		const user = this.getRemoteUserByConnectionId(connectionId);
    		const nickname = this.getNicknameFromConnectionData(data);
    		user?.setNickname(nickname);
    		this._remoteUsers.next(this.users);

    		// Update nickname in remote nickname list
    		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 = [];
    	}

    	addUserName(event: ConnectionEvent) {
    		const nickname = this.getNicknameFromConnectionData(event.connection.data);
    		const connectionId = event.connection.connectionId;
    		const newUserNameList = this._remoteUserNameList.getValue();

    		newUserNameList.push({ nickname, connectionId });
    		this._remoteUserNameList.next(newUserNameList);
    	}

    	deleteUserName(event: ConnectionEvent) {
    		const oldUserNameList: UserName[] = this._remoteUserNameList.getValue();
    		const newUserNameList: UserName[] = oldUserNameList.filter((element) => element.connectionId !== event.connection.connectionId);

    		this._remoteUserNameList.next(newUserNameList);
    	}

    	getNicknameFromConnectionData(data: string): string {
    		let nickname: string;
    		try {
    			nickname = JSON.parse(data).clientData;
    		} catch (error) {
    			nickname = 'Unknown';
    		}
    		return nickname;
    	}

    	private addUser(user: UserModel) {
    		const oldUser = this.getRemoteUserByConnectionId(user.connectionId);

    		// Assign streamManager if user exists due to connectionCreated Event
    		if (!!oldUser) {
    			oldUser.setStreamManager(user.getStreamManager());
    			return;
    		}

    		this.users.push(user);
    	}
    }