1 import { catchError, map, concatMap, toArray } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { SortMeta } from 'primeng/components/common/sortmeta'
5 import { from as observableFrom, Observable } from 'rxjs'
6 import { VideoBlacklist, VideoBlacklistType, ResultList } from '../../../../../shared'
7 import { Video } from '../video/video.model'
8 import { environment } from '../../../environments/environment'
9 import { RestExtractor, RestPagination, RestService } from '../rest'
10 import { ComponentPaginationLight } from '../rest/component-pagination.model'
13 export class VideoBlacklistService {
14 private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/'
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
22 listBlacklist (pagination: RestPagination, sort: SortMeta, type?: VideoBlacklistType): Observable<ResultList<VideoBlacklist>> {
23 let params = new HttpParams()
24 params = this.restService.addRestGetParams(params, pagination, sort)
27 params = params.set('type', type.toString())
30 return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
32 map(res => this.restExtractor.convertResultListDateToHuman(res)),
33 catchError(res => this.restExtractor.handleError(res))
37 getAutoBlacklistedAsVideoList (videoPagination: ComponentPaginationLight): Observable<ResultList<Video>> {
38 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
40 // prioritize first created since waiting longest
41 const AUTO_BLACKLIST_SORT = 'createdAt'
43 let params = new HttpParams()
44 params = this.restService.addRestGetParams(params, pagination, AUTO_BLACKLIST_SORT)
46 params = params.set('type', VideoBlacklistType.AUTO_BEFORE_PUBLISHED.toString())
48 return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
53 data: res.data.map(videoBlacklist => new Video(videoBlacklist.video))
56 catchError(res => this.restExtractor.handleError(res))
60 removeVideoFromBlacklist (videoIdArgs: number | number[]) {
61 const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ]
63 return observableFrom(videoIds)
65 concatMap(id => this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + id + '/blacklist')),
67 catchError(err => this.restExtractor.handleError(err))
71 blacklistVideo (videoId: number, reason: string, unfederate: boolean) {
77 return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
79 map(this.restExtractor.extractDataBool),
80 catchError(res => this.restExtractor.handleError(res))