import { ChangeDetectionStrategy , Component , OnDestroy , OnInit , ViewChild , } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { Observable , Subscription } from 'rxjs' ; import { Client } from '../../services/api/client' ; import { MindsUser } from '../../interfaces/entities' ; import { MindsChannelResponse } from '../../interfaces/responses' ; import { ProChannelComponent } from '../pro/channel/channel.component' ; import { Session } from '../../services/session' ; import { SiteService } from '../../common/services/site.service' ; import { FeaturesService } from '../../services/features.service' ; import { ChannelComponent as ChannelV2Component } from '../channels/v2/channel.component' ; import { TRIGGER_EXCEPTION } from '../channels/v2/content/content.service' ; import { HeadersService } from '../../common/services/headers.service' ; import { AuthModalService } from '../auth/modal/auth-modal.service' ; @ Component ( { selector : 'm-channel-container' , templateUrl : 'channel-container.component.html' , changeDetection : ChangeDetectionStrategy . Default , } ) export class ChannelContainerComponent implements OnInit , OnDestroy { inProgress : boolean = false ; error : string ; channel : MindsUser ; protected username : string ; protected showPro : boolean ; protected param$ : Subscription ; @ ViewChild ( 'v2ChannelComponent' ) v2ChannelComponent : ChannelV2Component ; @ ViewChild ( 'proChannelComponent' ) proChannelComponent : ProChannelComponent ; constructor ( protected route , protected router , protected client , protected session , protected site , protected features , protected headersService , protected authModalService ) { } ngOnInit ( ) { this . param$ = this . route . params . subscribe ( params => { if ( params [ 'username' ] ) { this . username = params [ 'username' ] ; this . showPro = ! params [ 'pro' ] || params [ 'pro' ] !== '0' ; if ( this . username && ( ! this . channel || this . channel . username !== this . username ) ) { this . load ( ) ; } } } ) ; } canDeactivate ( ) { if ( this . v2ChannelComponent ) { return this . v2ChannelComponent . canDeactivate ( ) ; } return true ; } ngOnDestroy ( ) { this . param$ . unsubscribe ( ) ; } async load ( ) { if ( ! this . username || this . showPro === undefined ) { return ; } this . inProgress = true ; try { const response = ( await this . client . get ( `api/v1/channel/${ this . username }` ) ) as MindsChannelResponse ; this . channel = response . channel ; const shouldRedirectToProHandler = false ; if ( shouldRedirectToProHandler ) { this . router . navigate ( [ '/pro' , this . channel . username ] , { replaceUrl : true , } ) ; } if ( response . require_login ) { this . headersService . setCode ( 401 ) ; this . channel . require_login = true ; await this . openLoginModal ( ) ; } } catch ( e ) { this . channel = { type : 'user' , guid : '' , name : '' , username : this . username , time_created : 0 , icontime : 0 , mode : 1 , nsfw : [ ] , } ; this . headersService . setCode ( 404 ) ; switch ( e . type ) { case TRIGGER_EXCEPTION . BANNED : this . channel . banned = 'yes' ; break ; case TRIGGER_EXCEPTION . DISABLED : this . channel . enabled = 'no' ; break ; case TRIGGER_EXCEPTION . NOT_FOUND : this . channel . not_found = true ; break ; default : this . error = e . message ; console . error ( e ) ; } } this . inProgress = false ; } async openLoginModal ( ) { try { await this . authModalService . open ( ) ; this . load ( ) ; } catch { } } get isOwner ( ) { const currentUser = this . session . getLoggedInUser ( ) ; return this . channel && currentUser && this . channel . guid == currentUser . guid ; } get isAdmin ( ) { return this . session . isAdmin ( ) ; } get proEnabled ( ) { return false ; } }