]>
Commit | Line | Data |
---|---|---|
7ccddd7b | 1 | import { catchError, map, concatMap, toArray } from 'rxjs/operators' |
792dbaf0 | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d | 3 | import { Injectable } from '@angular/core' |
f77eb73b | 4 | import { SortMeta } from 'primeng/api' |
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' |
440d39c5 | 10 | import { ComponentPaginationLight } 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 | ||
e0a92917 RK |
22 | listBlacklist (options: { |
23 | pagination: RestPagination, | |
24 | sort: SortMeta, | |
25 | search?: string | |
26 | type?: VideoBlacklistType | |
27 | }): Observable<ResultList<VideoBlacklist>> { | |
28 | const { pagination, sort, search, type } = options | |
29 | ||
792dbaf0 GS |
30 | let params = new HttpParams() |
31 | params = this.restService.addRestGetParams(params, pagination, sort) | |
32 | ||
e0a92917 RK |
33 | if (search) params = params.append('search', search) |
34 | if (type) params = params.append('type', type.toString()) | |
7ccddd7b | 35 | |
191764f3 | 36 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) |
db400f44 C |
37 | .pipe( |
38 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
39 | catchError(res => this.restExtractor.handleError(res)) | |
40 | ) | |
792dbaf0 GS |
41 | } |
42 | ||
440d39c5 | 43 | getAutoBlacklistedAsVideoList (videoPagination: ComponentPaginationLight): Observable<ResultList<Video>> { |
7ccddd7b JM |
44 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
45 | ||
46 | // prioritize first created since waiting longest | |
47 | const AUTO_BLACKLIST_SORT = 'createdAt' | |
48 | ||
49 | let params = new HttpParams() | |
50 | params = this.restService.addRestGetParams(params, pagination, AUTO_BLACKLIST_SORT) | |
51 | ||
52 | params = params.set('type', VideoBlacklistType.AUTO_BEFORE_PUBLISHED.toString()) | |
53 | ||
54 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) | |
55 | .pipe( | |
56 | map(res => { | |
dedc7abb C |
57 | return { |
58 | total: res.total, | |
59 | data: res.data.map(videoBlacklist => new Video(videoBlacklist.video)) | |
60 | } | |
7ccddd7b JM |
61 | }), |
62 | catchError(res => this.restExtractor.handleError(res)) | |
63 | ) | |
64 | } | |
65 | ||
66 | removeVideoFromBlacklist (videoIdArgs: number | number[]) { | |
67 | const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ] | |
68 | ||
69 | return observableFrom(videoIds) | |
70 | .pipe( | |
71 | concatMap(id => this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + id + '/blacklist')), | |
72 | toArray(), | |
73 | catchError(err => this.restExtractor.handleError(err)) | |
74 | ) | |
792dbaf0 GS |
75 | } |
76 | ||
5abb9fbb C |
77 | blacklistVideo (videoId: number, reason: string, unfederate: boolean) { |
78 | const body = { | |
79 | unfederate, | |
80 | reason | |
81 | } | |
26b7305a C |
82 | |
83 | return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', body) | |
db400f44 C |
84 | .pipe( |
85 | map(this.restExtractor.extractDataBool), | |
86 | catchError(res => this.restExtractor.handleError(res)) | |
87 | ) | |
35bf0c83 | 88 | } |
792dbaf0 | 89 | } |