]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/video-block.service.ts
Continue user mute in ban modal PR
[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'
67ed6552
C
3import { catchError, concatMap, map, toArray } from 'rxjs/operators'
4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core'
6import { RestExtractor, RestPagination, RestService } from '@app/core'
7import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models'
5baee5fc 8import { environment } from '../../../environments/environment'
5baee5fc
RK
9
10@Injectable()
11export class VideoBlockService {
12 private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/'
13
14 constructor (
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
18 ) {}
19
20 listBlocks (options: {
21 pagination: RestPagination
22 sort: SortMeta
23 search?: string
3487330d
RK
24 type?: VideoBlacklistType
25 }): Observable<ResultList<VideoBlacklist>> {
5baee5fc
RK
26 const { pagination, sort, search, type } = options
27
28 let params = new HttpParams()
29 params = this.restService.addRestGetParams(params, pagination, sort)
30
31 if (search) {
32 const filters = this.restService.parseQueryStringFilter(search, {
33 type: {
34 prefix: 'type:',
35 handler: v => {
3487330d
RK
36 if (v === 'manual') return VideoBlacklistType.MANUAL
37 if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED
5baee5fc
RK
38
39 return undefined
40 }
41 }
42 })
43
44 params = this.restService.addObjectParams(params, filters)
45 }
e95bede8 46 if (type) params = params.append('type', type.toString())
5baee5fc 47
3487330d 48 return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params })
5baee5fc
RK
49 .pipe(
50 map(res => this.restExtractor.convertResultListDateToHuman(res)),
51 catchError(res => this.restExtractor.handleError(res))
52 )
53 }
54
55 unblockVideo (videoIdArgs: number | number[]) {
56 const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ]
57
58 return observableFrom(videoIds)
59 .pipe(
60 concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')),
61 toArray(),
62 catchError(err => this.restExtractor.handleError(err))
63 )
64 }
65
3cfa8176
C
66 blockVideo (options: {
67 videoId: number
68 reason?: string
69 unfederate: boolean
70 }[]) {
71 return observableFrom(options)
72 .pipe(
73 concatMap(({ videoId, unfederate, reason }) => {
74 const body = { unfederate, reason }
5baee5fc 75
3cfa8176
C
76 return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
77 }),
78 toArray(),
79 catchError(res => this.restExtractor.handleError(res))
80 )
5baee5fc
RK
81 }
82}