]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/video-block.service.ts
Add bulk action on following/followers
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / video-block.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { from as observableFrom, Observable } from 'rxjs'
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'
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(
51 map(res => this.restExtractor.convertResultListDateToHuman(res)),
52 catchError(res => this.restExtractor.handleError(res))
53 )
54 }
55
56 unblockVideo (videoIdArgs: number | number[]) {
57 const videoIds = arrayify(videoIdArgs)
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
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 }
76
77 return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
78 }),
79 toArray(),
80 catchError(res => this.restExtractor.handleError(res))
81 )
82 }
83 }