]>
Commit | Line | Data |
---|---|---|
5baee5fc RK |
1 | import { SortMeta } from 'primeng/api' |
2 | import { from as observableFrom, Observable } from 'rxjs' | |
67ed6552 C |
3 | import { catchError, concatMap, map, 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' | |
e3d6c643 | 7 | import { arrayify } from '@shared/core-utils' |
67ed6552 | 8 | import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models' |
5baee5fc | 9 | import { environment } from '../../../environments/environment' |
5baee5fc RK |
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 | |
3487330d RK |
25 | type?: VideoBlacklistType |
26 | }): Observable<ResultList<VideoBlacklist>> { | |
5baee5fc RK |
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 => { | |
3487330d RK |
37 | if (v === 'manual') return VideoBlacklistType.MANUAL |
38 | if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED | |
5baee5fc RK |
39 | |
40 | return undefined | |
41 | } | |
42 | } | |
43 | }) | |
44 | ||
45 | params = this.restService.addObjectParams(params, filters) | |
46 | } | |
e95bede8 | 47 | if (type) params = params.append('type', type.toString()) |
5baee5fc | 48 | |
3487330d | 49 | return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params }) |
5baee5fc RK |
50 | .pipe( |
51 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
52 | catchError(res => this.restExtractor.handleError(res)) | |
53 | ) | |
54 | } | |
55 | ||
56 | unblockVideo (videoIdArgs: number | number[]) { | |
e3d6c643 | 57 | const videoIds = arrayify(videoIdArgs) |
5baee5fc RK |
58 | |
59 | return observableFrom(videoIds) | |
60 | .pipe( | |
61 | concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')), | |
62 | toArray(), | |
63 | catchError(err => this.restExtractor.handleError(err)) | |
64 | ) | |
65 | } | |
66 | ||
3cfa8176 C |
67 | blockVideo (options: { |
68 | videoId: number | |
69 | reason?: string | |
70 | unfederate: boolean | |
71 | }[]) { | |
72 | return observableFrom(options) | |
73 | .pipe( | |
74 | concatMap(({ videoId, unfederate, reason }) => { | |
75 | const body = { unfederate, reason } | |
5baee5fc | 76 | |
3cfa8176 C |
77 | return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body) |
78 | }), | |
79 | toArray(), | |
80 | catchError(res => this.restExtractor.handleError(res)) | |
81 | ) | |
5baee5fc RK |
82 | } |
83 | } |