]>
Commit | Line | Data |
---|---|---|
7ccddd7b | 1 | import { catchError, map, concatMap, toArray } from 'rxjs/operators' |
792dbaf0 | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d C |
3 | import { Injectable } from '@angular/core' |
4 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
7ccddd7b JM |
5 | import { from as observableFrom, Observable } from 'rxjs' |
6 | import { VideoBlacklist, VideoBlacklistType, ResultList } from '../../../../../shared' | |
7 | import { Video } from '../video/video.model' | |
63c4db6d | 8 | import { environment } from '../../../environments/environment' |
35bf0c83 | 9 | import { RestExtractor, RestPagination, RestService } from '../rest' |
7ccddd7b | 10 | import { ComponentPagination } from '../rest/component-pagination.model' |
792dbaf0 GS |
11 | |
12 | @Injectable() | |
35bf0c83 | 13 | export class VideoBlacklistService { |
63c4db6d | 14 | private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/' |
792dbaf0 GS |
15 | |
16 | constructor ( | |
17 | private authHttp: HttpClient, | |
18 | private restService: RestService, | |
19 | private restExtractor: RestExtractor | |
20 | ) {} | |
21 | ||
7ccddd7b | 22 | listBlacklist (pagination: RestPagination, sort: SortMeta, type?: VideoBlacklistType): Observable<ResultList<VideoBlacklist>> { |
792dbaf0 GS |
23 | let params = new HttpParams() |
24 | params = this.restService.addRestGetParams(params, pagination, sort) | |
25 | ||
7ccddd7b JM |
26 | if (type) { |
27 | params = params.set('type', type.toString()) | |
28 | } | |
29 | ||
191764f3 | 30 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) |
db400f44 C |
31 | .pipe( |
32 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
33 | catchError(res => this.restExtractor.handleError(res)) | |
34 | ) | |
792dbaf0 GS |
35 | } |
36 | ||
7ccddd7b JM |
37 | getAutoBlacklistedAsVideoList (videoPagination: ComponentPagination): Observable<{ videos: Video[], totalVideos: number}> { |
38 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
39 | ||
40 | // prioritize first created since waiting longest | |
41 | const AUTO_BLACKLIST_SORT = 'createdAt' | |
42 | ||
43 | let params = new HttpParams() | |
44 | params = this.restService.addRestGetParams(params, pagination, AUTO_BLACKLIST_SORT) | |
45 | ||
46 | params = params.set('type', VideoBlacklistType.AUTO_BEFORE_PUBLISHED.toString()) | |
47 | ||
48 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) | |
49 | .pipe( | |
50 | map(res => { | |
51 | const videos = res.data.map(videoBlacklist => new Video(videoBlacklist.video)) | |
52 | const totalVideos = res.total | |
53 | return { videos, totalVideos } | |
54 | }), | |
55 | catchError(res => this.restExtractor.handleError(res)) | |
56 | ) | |
57 | } | |
58 | ||
59 | removeVideoFromBlacklist (videoIdArgs: number | number[]) { | |
60 | const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ] | |
61 | ||
62 | return observableFrom(videoIds) | |
63 | .pipe( | |
64 | concatMap(id => this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + id + '/blacklist')), | |
65 | toArray(), | |
66 | catchError(err => this.restExtractor.handleError(err)) | |
67 | ) | |
792dbaf0 GS |
68 | } |
69 | ||
5abb9fbb C |
70 | blacklistVideo (videoId: number, reason: string, unfederate: boolean) { |
71 | const body = { | |
72 | unfederate, | |
73 | reason | |
74 | } | |
26b7305a C |
75 | |
76 | return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', body) | |
db400f44 C |
77 | .pipe( |
78 | map(this.restExtractor.extractDataBool), | |
79 | catchError(res => this.restExtractor.handleError(res)) | |
80 | ) | |
35bf0c83 | 81 | } |
792dbaf0 | 82 | } |