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