]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/videos/shared/video.service.ts
Client: better confirm box for a beautiful world
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
... / ...
CommitLineData
1import { Injectable } from '@angular/core';
2import { Http } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4import 'rxjs/add/operator/catch';
5import 'rxjs/add/operator/map';
6
7import { Search } from '../../shared';
8import { SortField } from './sort-field.type';
9import { AuthService } from '../../core';
10import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
11import { Video } from './video.model';
12
13@Injectable()
14export class VideoService {
15 private static BASE_VIDEO_URL = '/api/v1/videos/';
16
17 constructor(
18 private authService: AuthService,
19 private authHttp: AuthHttp,
20 private http: Http,
21 private restExtractor: RestExtractor,
22 private restService: RestService
23 ) {}
24
25 getVideo(id: string): Observable<Video> {
26 return this.http.get(VideoService.BASE_VIDEO_URL + id)
27 .map(this.restExtractor.extractDataGet)
28 .map(video_hash => new Video(video_hash))
29 .catch((res) => this.restExtractor.handleError(res));
30 }
31
32 getVideos(pagination: RestPagination, sort: SortField) {
33 const params = this.restService.buildRestGetParams(pagination, sort);
34
35 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
36 .map(res => res.json())
37 .map(this.extractVideos)
38 .catch((res) => this.restExtractor.handleError(res));
39 }
40
41 removeVideo(id: string) {
42 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
43 .map(this.restExtractor.extractDataBool)
44 .catch((res) => this.restExtractor.handleError(res));
45 }
46
47 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
48 const params = this.restService.buildRestGetParams(pagination, sort);
49
50 if (search.field) params.set('field', search.field);
51
52 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
53 .map(this.restExtractor.extractDataList)
54 .map(this.extractVideos)
55 .catch((res) => this.restExtractor.handleError(res));
56 }
57
58 reportVideo(id: string, reason: string) {
59 const body = {
60 reason
61 };
62 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
63
64 return this.authHttp.post(url, body)
65 .map(this.restExtractor.extractDataBool)
66 .catch((res) => this.restExtractor.handleError(res));
67 }
68
69 private extractVideos(result: ResultList) {
70 const videosJson = result.data;
71 const totalVideos = result.total;
72 const videos = [];
73 for (const videoJson of videosJson) {
74 videos.push(new Video(videoJson));
75 }
76
77 return { videos, totalVideos };
78 }
79}