]>
Commit | Line | Data |
---|---|---|
1 | import { SortMeta } from 'primeng/api' | |
2 | import { from as observableFrom, Observable } from 'rxjs' | |
3 | import { catchError, concatMap, 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 { arrayify } from '@shared/core-utils' | |
8 | import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models' | |
9 | import { environment } from '../../../environments/environment' | |
10 | ||
11 | @Injectable() | |
12 | export class VideoBlockService { | |
13 | private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/' | |
14 | ||
15 | constructor ( | |
16 | private authHttp: HttpClient, | |
17 | private restService: RestService, | |
18 | private restExtractor: RestExtractor | |
19 | ) {} | |
20 | ||
21 | listBlocks (options: { | |
22 | pagination: RestPagination | |
23 | sort: SortMeta | |
24 | search?: string | |
25 | type?: VideoBlacklistType | |
26 | }): Observable<ResultList<VideoBlacklist>> { | |
27 | const { pagination, sort, search, type } = options | |
28 | ||
29 | let params = new HttpParams() | |
30 | params = this.restService.addRestGetParams(params, pagination, sort) | |
31 | ||
32 | if (search) { | |
33 | const filters = this.restService.parseQueryStringFilter(search, { | |
34 | type: { | |
35 | prefix: 'type:', | |
36 | handler: v => { | |
37 | if (v === 'manual') return VideoBlacklistType.MANUAL | |
38 | if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED | |
39 | ||
40 | return undefined | |
41 | } | |
42 | } | |
43 | }) | |
44 | ||
45 | params = this.restService.addObjectParams(params, filters) | |
46 | } | |
47 | if (type) params = params.append('type', type.toString()) | |
48 | ||
49 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params }) | |
50 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
51 | } | |
52 | ||
53 | unblockVideo (videoIdArgs: number | number[]) { | |
54 | const videoIds = arrayify(videoIdArgs) | |
55 | ||
56 | return observableFrom(videoIds) | |
57 | .pipe( | |
58 | concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')), | |
59 | toArray(), | |
60 | catchError(err => this.restExtractor.handleError(err)) | |
61 | ) | |
62 | } | |
63 | ||
64 | blockVideo (options: { | |
65 | videoId: number | |
66 | reason?: string | |
67 | unfederate: boolean | |
68 | }[]) { | |
69 | return observableFrom(options) | |
70 | .pipe( | |
71 | concatMap(({ videoId, unfederate, reason }) => { | |
72 | const body = { unfederate, reason } | |
73 | ||
74 | return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body) | |
75 | }), | |
76 | toArray(), | |
77 | catchError(res => this.restExtractor.handleError(res)) | |
78 | ) | |
79 | } | |
80 | } |