import { Injectable } from '@angular/core' ; import { MindsUser } from '../../../interfaces/entities' ; import { BehaviorSubject , combineLatest , Observable , of } from 'rxjs' ; import { ApiService } from '../../../common/api/api.service' ; import { Session } from '../../../services/session' ; import { distinctUntilChanged , map , shareReplay , switchAll , switchMap , } from 'rxjs/operators' ; import { ActivatedRoute } from '@angular/router' ; /**
 * Service that holds a channel information using Observables
 */ @ Injectable ( ) export class ChannelsV2Service { /**
     * Channel GUID
     */ readonly guid$ : BehaviorSubject < string > = new BehaviorSubject < string > ( null ) ; /**
     * Channel username
     */ readonly username$ : BehaviorSubject < string > = new BehaviorSubject < string > ( '' ) ; /**
     * Channel data
     */ readonly channel$ : BehaviorSubject < MindsUser > = new BehaviorSubject < MindsUser > ( null ) ; /**
     * The user's email
     */ readonly email$ : BehaviorSubject < string > = new BehaviorSubject < string > ( null ) ; /**
     * Nsfw reasons
     */ readonly nsfw$ : BehaviorSubject < Array < number > > = new BehaviorSubject < Array < number > > ( [ ] ) ; /**
     * Boost rating
     */ readonly rating$ : BehaviorSubject < number > = new BehaviorSubject < number > ( 1 ) ; /**
     * Seed status
     */ readonly seed$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( null ) ; /**
     * Search query
     */ readonly query$ : BehaviorSubject < string > = new BehaviorSubject < string > ( '' ) ; /**
     * Tokens the channel received in the last period
     */ readonly tokens$ : Observable < number > ; /**
     * Tokens the current user sent to the channel
     */ readonly tokensSent$ : Observable < number > ; /**
     * Flag that checks if the current user is the channel owner
     */ readonly isOwner$ : Observable < boolean > ; /**
     * Subscription status
     */ readonly isSubscribed$ : Observable < boolean > ; /**
     * Blocked status
     */ readonly isBlocked$ : Observable < boolean > ; /**
     * Can interact with channel?
     */ readonly canInteract$ : Observable < boolean > ; /**
     * Banned status
     */ readonly isBanned$ : Observable < boolean > ; /**
     * Explicit status
     */ readonly isExplicit$ : Observable < boolean > ; /**
     * Disabled status
     */ readonly isDisabled$ : Observable < boolean > ; /**
     * Admin status
     */ readonly isAdmin$ : Observable < boolean > ; /**
     * Amount of public groups the user is member of
     */ readonly groupCount$ : Observable < number > ; param api param session constructor ( protected api , protected session , protected route ) { this . tokens$ = this . channel$ . pipe ( distinctUntilChanged ( ( a , b ) => ! a || ! b || a . guid === b . guid ) , map ( channel => channel ? this . api . get ( `api/v1/wire/sums/overview/${ channel . guid }` , { merchant : channel . merchant ? 1 : 0 , } ) : of ( null ) ) , switchAll ( ) , shareReplay ( { bufferSize : 1 , refCount : true , } ) , map ( response => parseFloat ( ( response && response . tokens ) || '0' ) ) ) ; this . tokensSent$ = this . channel$ . pipe ( distinctUntilChanged ( ( a , b ) => ! a || ! b || a . guid === b . guid ) , map ( channel => channel ? this . api . get ( `api/v1/wire/rewards/${ channel . guid }` ) : of ( null ) ) , switchAll ( ) , shareReplay ( { bufferSize : 1 , refCount : true , } ) , map ( response => parseFloat ( ( response && response . sums && response . sums . tokens ) || '0' ) ) ) ; this . groupCount$ = this . channel$ . pipe ( distinctUntilChanged ( ( a , b ) => ! a || ! b || a . guid === b . guid ) , map ( channel => channel ? this . api . get ( `api/v3/channel/${ channel . guid }/groups/count` ) : of ( null ) ) , switchAll ( ) , shareReplay ( { bufferSize : 1 , refCount : true , } ) , map ( response => ( response && response . count ) || 0 ) ) ; this . isOwner$ = combineLatest ( [ this . guid$ , this . session . user$ ] ) . pipe ( map ( ( [ guid , currentUser ] ) => guid && currentUser && guid === currentUser . guid ) ) ; this . isSubscribed$ = combineLatest ( [ this . isOwner$ , this . session . user$ , this . channel$ , ] ) . pipe ( map ( ( [ isOwner , currentUser , channel ] ) => ! isOwner && currentUser && channel && channel . subscribed ) ) ; this . isBlocked$ = combineLatest ( [ this . isOwner$ , this . session . user$ , this . channel$ , ] ) . pipe ( map ( ( [ isOwner , currentUser , channel ] ) => ! isOwner && currentUser && channel && channel . blocked ) ) ; this . canInteract$ = combineLatest ( [ this . channel$ , this . isBlocked$ ] ) . pipe ( map ( ( [ channel , isBlocked ] ) => ! isBlocked && channel && channel . guid && channel . banned !== 'yes' ) ) ; this . isBanned$ = combineLatest ( [ this . isOwner$ , this . session . user$ , this . channel$ , ] ) . pipe ( map ( ( [ isOwner , currentUser , channel ] ) => ! isOwner && currentUser && channel && channel . banned === 'yes' ) ) ; this . isExplicit$ = combineLatest ( [ this . isOwner$ , this . session . user$ , this . channel$ , ] ) . pipe ( map ( ( [ isOwner , currentUser , channel ] ) => ! isOwner && currentUser && channel && channel . is_mature ) ) ; this . isAdmin$ = this . session . user$ . pipe ( map ( channel => channel && channel . is_admin ) ) ; this . isDisabled$ = this . channel$ . pipe ( map ( channel => channel && channel . enabled === 'no' ) ) ; } param channel load ( channel ) { const params = this . route . snapshot . queryParamMap ; let query = '' ; if ( params . has ( 'query' ) ) { query = decodeURIComponent ( params . get ( 'query' ) ) ; } this . query$ . next ( query ) ; this . guid$ . next ( typeof channel === 'object' ? channel . guid : channel ) ; this . setChannel ( typeof channel === 'object' ? channel : null ) ; } /**
     * Re-sync current channel from server
     */ sync ( ) { this . api . get ( `api/v1/channel/${ this . guid$ . getValue ( ) }` ) . subscribe ( response => { this . setChannel ( response . channel ) ; } ) ; } /**
     * Sets the state based on a channel
     */ setChannel ( channel ) { this . channel$ . next ( channel ) ; this . username$ . next ( channel ? channel . username : '' ) ; this . email$ . next ( channel ? channel . email : null ) ; this . nsfw$ . next ( channel ? channel . nsfw : [ ] ) ; this . rating$ . next ( channel ? channel . rating : 1 ) ; this . seed$ . next ( channel ? channel . seed : null ) ; return this ; } }