1 import { SortMeta } from 'primeng/api'
2 import { from as observableFrom, Observable } from 'rxjs'
3 import { catchError, concatMap, map, 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 { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models'
8 import { environment } from '../../../environments/environment'
11 export class VideoBlockService {
12 private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/'
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
20 listBlocks (options: {
21 pagination: RestPagination
24 type?: VideoBlacklistType
25 }): Observable<ResultList<VideoBlacklist>> {
26 const { pagination, sort, search, type } = options
28 let params = new HttpParams()
29 params = this.restService.addRestGetParams(params, pagination, sort)
32 const filters = this.restService.parseQueryStringFilter(search, {
36 if (v === 'manual') return VideoBlacklistType.MANUAL
37 if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED
44 params = this.restService.addObjectParams(params, filters)
46 if (type) params = params.append('type', type.toString())
48 return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params })
50 map(res => this.restExtractor.convertResultListDateToHuman(res)),
51 catchError(res => this.restExtractor.handleError(res))
55 unblockVideo (videoIdArgs: number | number[]) {
56 const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ]
58 return observableFrom(videoIds)
60 concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')),
62 catchError(err => this.restExtractor.handleError(err))
66 blockVideo (options: {
71 return observableFrom(options)
73 concatMap(({ videoId, unfederate, reason }) => {
74 const body = { unfederate, reason }
76 return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
79 catchError(res => this.restExtractor.handleError(res))