import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { RecentService } from '../../services/ux/recent' ; import { Client } from '../../services/api/client' ; import { Session } from '../../services/session' ; import { Storage } from '../../services/storage' ; @ Injectable ( ) export class BlockListService { blocked : BehaviorSubject < string [ ] > ; constructor ( protected client , protected session , protected storage , protected recentService ) { this . blocked = new BehaviorSubject ( [ ] ) ; } fetch ( ) { this . client . get ( 'api/v1/block' , { sync : 1 , limit : 10000 } ) . then ( ( response ) => { if ( response . guids !== this . blocked . getValue ( ) ) this . blocked . next ( response . guids ) ; this . storage . set ( 'blocked' , JSON . stringify ( response . guids ) ) ; } ) . catch ( err => null ) ; return this ; } async sync ( ) { } async prune ( ) { } async get ( ) { } async getList ( limit , offset ) { return this . client . get ( 'api/v1/block' , { limit , offset } ) ; } async add ( guid ) { const guids = this . blocked . getValue ( ) ; if ( guids . indexOf ( guid ) < 0 ) this . blocked . next ( [ ... guids , ... [ guid ] ] ) ; this . storage . set ( 'blocked' , JSON . stringify ( this . blocked . getValue ( ) ) ) ; this . recentService . storeSuggestion ( 'publisher' , { guid , blocked : true } , entry => entry . guid === guid ) ; } async remove ( guid ) { const guids = this . blocked . getValue ( ) ; const index = guids . indexOf ( guid ) ; if ( index > - 1 ) { guids . splice ( index , 1 ) ; } this . blocked . next ( guids ) ; this . storage . set ( 'blocked' , JSON . stringify ( this . blocked . getValue ( ) ) ) ; } static _ ( client , session , storage , recent ) { return new BlockListService ( client , session , storage , recent ) ; } }