96a1f1fd2eadb3cf4a8fa626f5489d95e2bc13cd
[oweals/peertube.git] / client / src / app / shared / video-abuse / video-abuse.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
6 import { Observable } from 'rxjs/Observable'
7 import { ResultList, VideoAbuse } from '../../../../../shared'
8 import { RestExtractor, RestPagination, RestService } from '../rest'
9 import { Utils } from '../utils'
10 import { environment } from '../../../environments/environment'
11
12 @Injectable()
13 export class VideoAbuseService {
14   private static BASE_VIDEO_ABUSE_URL = environment.apiUrl + '/api/v1/videos/'
15
16   constructor (
17     private authHttp: HttpClient,
18     private restService: RestService,
19     private restExtractor: RestExtractor
20   ) {}
21
22   getVideoAbuses (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoAbuse>> {
23     const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + 'abuse'
24
25     let params = new HttpParams()
26     params = this.restService.addRestGetParams(params, pagination, sort)
27
28     return this.authHttp.get<ResultList<VideoAbuse>>(url, { params })
29                         .map(res => this.restExtractor.convertResultListDateToHuman(res))
30                         .map(res => this.restExtractor.applyToResultListData(res, this.formatVideoAbuse.bind(this)))
31                         .catch(res => this.restExtractor.handleError(res))
32   }
33
34   reportVideo (id: number, reason: string) {
35     const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + id + '/abuse'
36     const body = {
37       reason
38     }
39
40     return this.authHttp.post(url, body)
41                         .map(this.restExtractor.extractDataBool)
42                         .catch(res => this.restExtractor.handleError(res))
43   }
44
45   private formatVideoAbuse (videoAbuse: VideoAbuse) {
46     return Object.assign(videoAbuse, {
47       createdAt: Utils.dateToHuman(videoAbuse.createdAt)
48     })
49   }
50
51 }