+import { catchError, map, concatMap, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
-import { SortMeta } from 'primeng/components/common/sortmeta'
-import 'rxjs/add/operator/catch'
-import 'rxjs/add/operator/map'
-import { Observable } from 'rxjs/Observable'
-import { BlacklistedVideo, ResultList } from '../../../../../shared'
+import { SortMeta } from 'primeng/api'
+import { from as observableFrom, Observable } from 'rxjs'
+import { VideoBlacklist, VideoBlacklistType, ResultList } from '../../../../../shared'
+import { Video } from '../video/video.model'
import { environment } from '../../../environments/environment'
import { RestExtractor, RestPagination, RestService } from '../rest'
+import { ComponentPaginationLight } from '../rest/component-pagination.model'
@Injectable()
export class VideoBlacklistService {
private restExtractor: RestExtractor
) {}
- listBlacklist (pagination: RestPagination, sort: SortMeta): Observable<ResultList<BlacklistedVideo>> {
+ listBlacklist (pagination: RestPagination, sort: SortMeta, type?: VideoBlacklistType): Observable<ResultList<VideoBlacklist>> {
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
- return this.authHttp.get<ResultList<BlacklistedVideo>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
- .map(res => this.restExtractor.convertResultListDateToHuman(res))
- .catch(res => this.restExtractor.handleError(res))
+ if (type) {
+ params = params.set('type', type.toString())
+ }
+
+ return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
+ .pipe(
+ map(res => this.restExtractor.convertResultListDateToHuman(res)),
+ catchError(res => this.restExtractor.handleError(res))
+ )
+ }
+
+ getAutoBlacklistedAsVideoList (videoPagination: ComponentPaginationLight): Observable<ResultList<Video>> {
+ const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
+
+ // prioritize first created since waiting longest
+ const AUTO_BLACKLIST_SORT = 'createdAt'
+
+ let params = new HttpParams()
+ params = this.restService.addRestGetParams(params, pagination, AUTO_BLACKLIST_SORT)
+
+ params = params.set('type', VideoBlacklistType.AUTO_BEFORE_PUBLISHED.toString())
+
+ return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
+ .pipe(
+ map(res => {
+ return {
+ total: res.total,
+ data: res.data.map(videoBlacklist => new Video(videoBlacklist.video))
+ }
+ }),
+ catchError(res => this.restExtractor.handleError(res))
+ )
}
- removeVideoFromBlacklist (videoId: number) {
- return this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist')
- .map(this.restExtractor.extractDataBool)
- .catch(res => this.restExtractor.handleError(res))
+ removeVideoFromBlacklist (videoIdArgs: number | number[]) {
+ const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ]
+
+ return observableFrom(videoIds)
+ .pipe(
+ concatMap(id => this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + id + '/blacklist')),
+ toArray(),
+ catchError(err => this.restExtractor.handleError(err))
+ )
}
- blacklistVideo (videoId: number) {
- return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', {})
- .map(this.restExtractor.extractDataBool)
- .catch(res => this.restExtractor.handleError(res))
+ blacklistVideo (videoId: number, reason: string, unfederate: boolean) {
+ const body = {
+ unfederate,
+ reason
+ }
+
+ return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
+ .pipe(
+ map(this.restExtractor.extractDataBool),
+ catchError(res => this.restExtractor.handleError(res))
+ )
}
}