import { Injectable } from '@angular/core' ; import { Event , NavigationStart , Router } from '@angular/router' ; import { Storage } from './storage' ; import { Subscription } from 'rxjs' ; import { Client } from './api/client' ; export type ContextServiceProduct = 'activity' | 'user' | 'object:video' | 'object:image' | 'object:blog' | 'group' ; export type ContextServiceEntity = { label : string ; id : any ; nameLabel ? : string ; } ; export type ContextServiceResponse = { product : ContextServiceProduct ; label : string ; entity ? : ContextServiceEntity ; } ; @ Injectable ( ) export class ContextService { context : ContextServiceResponse | null ; private _routerListener : Subscription ; constructor ( private router , private storage , private client ) { } static _ ( router , storage , client ) { return new ContextService ( router , storage , client ) ; } listen ( ) { this . _routerListener = this . router . events . subscribe ( ( event ) => { if ( event instanceof NavigationStart ) { this . reset ( ) ; } } ) ; return this ; } unlisten ( ) { this . _routerListener . unsubscribe ( ) ; return this ; } reset ( ) { this . context = null ; } set ( product , entity ? ) { let label = this . getProductLabel ( product ) || product ; this . context = { product , label , entity } ; if ( entity && entity . nameLabel ) { this . _storeLabel ( entity . id , entity . nameLabel ) ; } } get ( ) { return this . context ; } getProductLabel ( product ) { let label = '' ; switch ( product ) { case 'activity' : label = 'posts' ; break ; case 'user' : label = 'channels' ; break ; case 'object:video' : label = 'videos' ; break ; case 'object:image' : label = 'images' ; break ; case 'object:blog' : label = 'blogs' ; break ; case 'group' : label = 'groups' ; break ; } return label ; } resolveLabel ( guid ) { const cache = this . _fetchLabel ( guid ) ; if ( cache !== null ) { return Promise . resolve ( cache ) ; } return this . client . get ( `api/v1/entities/entity/${ guid }` ) . then ( ( response ) => { if ( ! response || ! response . entity ) { return '' ; } let label = '' ; if ( response . entity . username ) { label = `@${ response . entity . username }` ; } else if ( response . entity . name ) { label = response . entity . name ; } this . _storeLabel ( guid , label ) ; return label ; } ) ; } resolveStaticLabel ( product ) { return Promise . resolve ( this . getProductLabel ( product ) || 'Minds' ) ; } protected _storeLabel ( guid , label ) { const cache = JSON . parse ( this . storage . get ( 'context-label-cache' ) || `{}` ) ; cache [ guid ] = label ; this . storage . set ( 'context-label-cache' , JSON . stringify ( cache ) ) ; } protected _fetchLabel ( guid ) { const cache = JSON . parse ( this . storage . get ( 'context-label-cache' ) || `{}` ) ; if ( typeof cache [ guid ] !== 'undefined' ) { return cache [ guid ] ; } return null ; } }