import { Injectable } from '@angular/core' ; import { BehaviorSubject , of } from 'rxjs' ; import { first } from 'rxjs/operators' ; import { Client } from '../../services/api' ; import { BlockListService } from './block-list.service' ; export type EntityObservable = BehaviorSubject < Object > ; type EntityObservables = Map < string , EntityObservable > ; @ Injectable ( ) export class EntitiesService { entities : EntityObservables = new Map < string , EntityObservable > ( ) ; castToActivites : boolean = false ; exportUserCounts : boolean = false ; constructor ( protected client , protected blockListService ) { } async getFromFeed ( feed ) { if ( ! feed || ! feed . length ) { return [ ] ; } const urnsToFetch = [ ] ; const urnsToResync = [ ] ; const entities = [ ] ; for ( const feedItem of feed ) { if ( feedItem . entity || feedItem . urn ) { if ( feedItem . entity ) { this . addEntity ( feedItem . entity ) ; } if ( ! this . entities . has ( feedItem . urn ) ) { urnsToFetch . push ( feedItem . urn ) ; } if ( this . entities . has ( feedItem . urn ) && ! feedItem . entity && feed . length < 20 ) { urnsToResync . push ( feedItem . urn ) ; } } } if ( urnsToFetch . length ) { await this . fetch ( urnsToFetch ) ; } if ( urnsToResync . length ) { this . fetch ( urnsToResync ) ; } for ( const feedItem of feed ) { if ( feedItem . entity || feedItem . urn ) { if ( this . entities . has ( feedItem . urn ) ) { const entity = this . entities . get ( feedItem . urn ) ; try { if ( await entity . pipe ( first ( ) ) . toPromise ( ) ) { entities . push ( entity ) ; } } catch ( err ) { } } } else { entities . push ( of ( feedItem ) ) ; } } return entities ; } param urn return single ( urn ) { if ( urn . indexOf ( 'urn:' ) < 0 ) { urn = `urn:activity:${ urn }` ; } this . entities . set ( urn , new BehaviorSubject ( null ) ) ; this . fetch ( [ urn ] ) . then ( ( response ) => { if ( ! response . entities [ 0 ] ) { return ; } const entity = response . entities [ 0 ] ; entity . require_login = response . require_login ? response . require_login : false ; if ( entity && entity . urn !== urn ) { entity . urn = urn ; this . addEntity ( entity ) ; } } ) ; return this . entities . get ( urn ) ; } param cast return setCastToActivities ( cast ) { this . castToActivites = cast ; return this ; } param cast return setExportUserCounts ( value ) { this . exportUserCounts = value ; return this ; } param urns return async fetch ( urns ) { try { const response = await this . client . get ( 'api/v2/entities/' , { urns , as_activities : this . castToActivites ? 1 : 0 , export_user_counts : this . exportUserCounts , } ) ; if ( ! response . entities . length ) { for ( const urn of urns ) { this . addNotFoundEntity ( urn ) ; } } for ( const entity of response . entities ) { this . addEntity ( entity ) ; } return response ; } catch ( err ) { } } param entity return addEntity ( entity ) { if ( this . entities . has ( entity . urn ) ) { this . entities . get ( entity . urn ) . next ( entity ) ; } else { this . entities . set ( entity . urn , new BehaviorSubject ( entity ) ) ; } } param urn return addNotFoundEntity ( urn ) { if ( ! this . entities . has ( urn ) ) { this . entities . set ( urn , new BehaviorSubject ( null ) ) ; } this . entities . get ( urn ) . error ( 'Not found' ) ; } static _ ( client , blockListService ) { return new EntitiesService ( client , blockListService ) ; } }