]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/video-block.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / video-block.service.ts
CommitLineData
5baee5fc
RK
1import { SortMeta } from 'primeng/api'
2import { from as observableFrom, Observable } from 'rxjs'
3afe0ec3 3import { catchError, concatMap, toArray } from 'rxjs/operators'
67ed6552
C
4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core'
6import { RestExtractor, RestPagination, RestService } from '@app/core'
e3d6c643 7import { arrayify } from '@shared/core-utils'
67ed6552 8import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models'
5baee5fc 9import { environment } from '../../../environments/environment'
5baee5fc
RK
10
11@Injectable()
12export 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 })
3afe0ec3 50 .pipe(catchError(res => this.restExtractor.handleError(res)))
5baee5fc
RK
51 }
52
53 unblockVideo (videoIdArgs: number | number[]) {
e3d6c643 54 const videoIds = arrayify(videoIdArgs)
5baee5fc
RK
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
3cfa8176
C
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 }
5baee5fc 73
3cfa8176
C
74 return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
75 }),
76 toArray(),
77 catchError(res => this.restExtractor.handleError(res))
78 )
5baee5fc
RK
79 }
80}