import { Injectable , OnDestroy } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { Subscription } from 'rxjs' ; import { environment } from '@environments/environment' ; import { Doc } from '../../core/Doc' ; import { PulsarService } from '../network/pulsar.service' ; import { LogsStrategy } from './LogsStrategy' ; import { SendAllLogsStrategy } from './SendAllLogsStrategy' ; import { SendIfActivateLogsStrategy } from './SendIfActivateLogsStrategy' ; @ Injectable ( ) export class LogsService implements OnDestroy { private subs : Subscription [ ] ; private docKey : string ; private displayLogs : boolean ; private strategy : LogsStrategy ; private shareLogs : boolean ; constructor ( route ) { this . displayLogs = false ; this . subs = [ ] ; this . subs . push ( route . data . subscribe ( ( { doc } ) => { this . docKey = doc . signalingKey ; this . shareLogs = doc . shareLogs ; this . setLogsStrategy ( 'sendall' ) ; } ) ) ; } setStreamLogsPulsar ( pulsarService ) { this . strategy . setStreamLogsPulsar ( pulsarService ) ; } log ( obj ) { if ( this . displayLogs ) { log . info ( 'DOC LOGS' , obj [ 'type' ] , obj ) ; } if ( obj [ 'context' ] ) { const tab = { } ; obj [ 'context' ] . forEach ( ( v , k ) => { tab [ k ] = v ; } ) ; obj [ 'context' ] = tab ; } if ( environment . logSystem . anonimyze ) { this . strategy . sendLogs ( this . anonymize ( obj ) , this . shareLogs ) ; } else { this . strategy . sendLogs ( obj , this . shareLogs ) ; } } anonymize ( obj ) { const anonymObj = JSON . parse ( JSON . stringify ( obj ) ) ; if ( obj [ 'type' ] === 'localInsertion' ) { const content = anonymObj [ 'operation' ] [ 'content' ] ; let anonymContent = '' ; for ( let i = 0 ; i < content . length ; i ++ ) { anonymContent += this . randomizeChar ( content . charAt ( i ) ) ; } anonymObj [ 'operation' ] [ 'content' ] = anonymContent ; anonymObj [ 'content' ] = anonymContent ; } else if ( obj [ 'type' ] === 'remoteInsertion' ) { const content = anonymObj [ 'operation' ] [ 'content' ] ; let anonymContent = '' ; for ( let i = 0 ; i < content . length ; i ++ ) { anonymContent += this . randomizeChar ( content . charAt ( i ) ) ; } anonymObj [ 'operation' ] [ 'content' ] = anonymContent ; const tab = anonymObj [ 'textOperation' ] ; let cpt = 0 ; tab . sort ( ( a , b ) => { return a [ 'position' ] - b [ 'position' ] ; } ) . forEach ( ( value ) => { const l = value [ 'content' ] . length ; value [ 'content' ] = anonymContent . substr ( cpt , l ) ; cpt += l ; } ) ; } return anonymObj ; } private randomizeChar ( c ) { const character = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,?;.:/!%*$&()[]_-<>' ; const min = 0 ; const max = character . length - 1 ; return character . charAt ( Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ) ; } getLogs ( ) { return this . strategy . getLocalLogs ( ) ; } public setDisplayLogs ( display ) { this . displayLogs = display ; } setShareLogs ( share , state ) { if ( this . shareLogs !== share ) { this . shareLogs = share ; this . strategy . setShareLogs ( share , state ) ; } } get isSharingLogs ( ) : boolean { return this . shareLogs ; } setLogsStrategy ( logsStrategy ) { switch ( logsStrategy ) { case 'sendall' : if ( ! ( this . strategy instanceof SendAllLogsStrategy ) ) { this . strategy = new SendAllLogsStrategy ( this . docKey ) ; } break ; case 'sendifactivate' : if ( ! ( this . strategy instanceof SendIfActivateLogsStrategy ) ) { this . strategy = new SendIfActivateLogsStrategy ( this . docKey ) ; } break ; default : console . error ( 'No Strategy Found !!' ) ; break ; } } ngOnDestroy ( ) { this . subs . forEach ( ( s ) => s . unsubscribe ( ) ) ; } }