1 import { omit } from 'lodash-es'
2 import { SortMeta } from 'primeng/api'
3 import { Observable } from 'rxjs'
4 import { catchError, map } from 'rxjs/operators'
5 import { HttpClient, HttpParams } from '@angular/common/http'
6 import { Injectable } from '@angular/core'
7 import { RestExtractor, RestPagination, RestService } from '@app/core'
8 import { ResultList, VideoAbuse, VideoAbuseCreate, VideoAbuseState, VideoAbuseUpdate } from '@shared/models'
9 import { environment } from '../../../environments/environment'
12 export class VideoAbuseService {
13 private static BASE_VIDEO_ABUSE_URL = environment.apiUrl + '/api/v1/videos/'
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor
21 getVideoAbuses (options: {
22 pagination: RestPagination,
25 }): Observable<ResultList<VideoAbuse>> {
26 const { pagination, sort, search } = options
27 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + 'abuse'
29 let params = new HttpParams()
30 params = this.restService.addRestGetParams(params, pagination, sort)
33 const filters = this.restService.parseQueryStringFilter(search, {
38 if (v === 'accepted') return VideoAbuseState.ACCEPTED
39 if (v === 'pending') return VideoAbuseState.PENDING
40 if (v === 'rejected') return VideoAbuseState.REJECTED
48 if (v === 'deleted') return v
49 if (v === 'blacklisted') return v
54 searchReporter: { prefix: 'reporter:' },
55 searchReportee: { prefix: 'reportee:' },
56 predefinedReason: { prefix: 'tag:' }
59 params = this.restService.addObjectParams(params, filters)
62 return this.authHttp.get<ResultList<VideoAbuse>>(url, { params })
64 catchError(res => this.restExtractor.handleError(res))
68 reportVideo (parameters: { id: number } & VideoAbuseCreate) {
69 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + parameters.id + '/abuse'
71 const body = omit(parameters, [ 'id' ])
73 return this.authHttp.post(url, body)
75 map(this.restExtractor.extractDataBool),
76 catchError(res => this.restExtractor.handleError(res))
80 updateVideoAbuse (videoAbuse: VideoAbuse, abuseUpdate: VideoAbuseUpdate) {
81 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + videoAbuse.video.uuid + '/abuse/' + videoAbuse.id
83 return this.authHttp.put(url, abuseUpdate)
85 map(this.restExtractor.extractDataBool),
86 catchError(res => this.restExtractor.handleError(res))
90 removeVideoAbuse (videoAbuse: VideoAbuse) {
91 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + videoAbuse.video.uuid + '/abuse/' + videoAbuse.id
93 return this.authHttp.delete(url)
95 map(this.restExtractor.extractDataBool),
96 catchError(res => this.restExtractor.handleError(res))