1 import { SortMeta } from 'primeng/api'
2 import { from as observableFrom, Observable } from 'rxjs'
3 import { catchError, concatMap, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { arrayify } from '@shared/core-utils'
8 import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models'
9 import { environment } from '../../../environments/environment'
12 export class VideoBlockService {
13 private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/'
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor
21 listBlocks (options: {
22 pagination: RestPagination
25 type?: VideoBlacklistType
26 }): Observable<ResultList<VideoBlacklist>> {
27 const { pagination, sort, search, type } = options
29 let params = new HttpParams()
30 params = this.restService.addRestGetParams(params, pagination, sort)
33 const filters = this.restService.parseQueryStringFilter(search, {
37 if (v === 'manual') return VideoBlacklistType.MANUAL
38 if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED
45 params = this.restService.addObjectParams(params, filters)
47 if (type) params = params.append('type', type.toString())
49 return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params })
50 .pipe(catchError(res => this.restExtractor.handleError(res)))
53 unblockVideo (videoIdArgs: number | number[]) {
54 const videoIds = arrayify(videoIdArgs)
56 return observableFrom(videoIds)
58 concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')),
60 catchError(err => this.restExtractor.handleError(err))
64 blockVideo (options: {
69 return observableFrom(options)
71 concatMap(({ videoId, unfederate, reason }) => {
72 const body = { unfederate, reason }
74 return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
77 catchError(res => this.restExtractor.handleError(res))