# All files / src/lib/services/chat chat.service.ts

57.14% Statements 24/42
35.71% Branches 10/28
42.85% Functions 3/7
52.77% Lines 19/36

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
    801x
    1x
    1x
    1x
    1x

    1x

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

    1x

    1x

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

    import { ILogger } from '../../models/logger.model';
    import { ChatMessage } from '../../models/chat.model';
    import { INotificationOptions } from '../../models/notification-options.model';

    import { ActionService } from '../action/action.service';
    import { WebrtcService } from '../webrtc/webrtc.service';
    import { LocalUserService } from '../local-user/local-user.service';
    import { LoggerService } from '../logger/logger.service';
    import { Signal } from '../../models/signal.model';

    @Injectable({
    	providedIn: 'root'
    })
    export class ChatService {
    	messagesObs: Observable<ChatMessage[]>;
    	toggleChatObs: Observable<boolean>;

    	private _messageList = <BehaviorSubject<ChatMessage[]>>new BehaviorSubject([]);
    	private _toggleChat = <BehaviorSubject<boolean>>new BehaviorSubject(false);

    	private messageList: ChatMessage[] = [];
    	private isChatOpened: boolean = false;
    	private log: ILogger;
    	constructor(
    		private loggerSrv: LoggerService,
    		private openViduWebRTCService: WebrtcService,
    		private localUsersService: LocalUserService,
    		private actionService: ActionService
    	) {
    		this.log = this.loggerSrv.get('ChatService');
    		this.messagesObs = this._messageList.asObservable();
    		this.toggleChatObs = this._toggleChat.asObservable();
    	}

    	subscribeToChat() {
    		const session = this.openViduWebRTCService.getWebcamSession();
    		session.on(`signal:${Signal.CHAT}`, (event: any) => {
    			const connectionId = event.from.connectionId;
    			const data = JSON.parse(event.data);
    			const isMyOwnConnection = this.openViduWebRTCService.isMyOwnConnection(connectionId);
    			this.messageList.push({
    				isLocal: isMyOwnConnection,
    				nickname: data.nickname,
    				message: data.message,
    			});
    			if (!this.isChatOpened) {
    				const notificationOptions: INotificationOptions = {
    					message: `${data.nickname.toUpperCase()} sent a message`,
    					cssClassName: 'messageSnackbar',
    					buttonActionText: 'READ'
    				};
    				this.actionService.launchNotification(notificationOptions, this.toggleChat.bind(this));
    			}
    			this._messageList.next(this.messageList);
    		});
    	}

    	sendMessage(message: string) {
    		message = message.replace(/ +(?= )/g, '');
    		if (message !== '' && message !== ' ') {
    			const data = {
    				message: message,
    				nickname: this.localUsersService.getWebcamUserName()
    			};

    			this.openViduWebRTCService.sendSignal(Signal.CHAT, undefined, data);
    		}
    	}

    	toggleChat() {
    		this.log.d('Toggling chat');
    		this.isChatOpened = !this.isChatOpened;
    		this._toggleChat.next(this.isChatOpened);
    	}
    }