]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Client: add support for video licences
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core';
2 import { Http } from '@angular/http';
3 import { Observable } from 'rxjs/Observable';
4 import 'rxjs/add/operator/catch';
5 import 'rxjs/add/operator/map';
6
7 import { Search } from '../../shared';
8 import { SortField } from './sort-field.type';
9 import { RateType } from './rate-type.type';
10 import { AuthService } from '../../core';
11 import {
12 AuthHttp,
13 RestExtractor,
14 RestPagination,
15 RestService,
16 ResultList,
17 UserService
18 } from '../../shared';
19 import { Video } from './video.model';
20
21 @Injectable()
22 export class VideoService {
23 private static BASE_VIDEO_URL = '/api/v1/videos/';
24
25 videoCategories: Array<{ id: number, label: string }> = [];
26 videoLicences: Array<{ id: number, label: string }> = [];
27
28 constructor(
29 private authService: AuthService,
30 private authHttp: AuthHttp,
31 private http: Http,
32 private restExtractor: RestExtractor,
33 private restService: RestService
34 ) {}
35
36 loadVideoCategories() {
37 return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
38 .map(this.restExtractor.extractDataGet)
39 .subscribe(data => {
40 Object.keys(data).forEach(categoryKey => {
41 this.videoCategories.push({
42 id: parseInt(categoryKey),
43 label: data[categoryKey]
44 });
45 });
46 });
47 }
48
49 loadVideoLicences() {
50 return this.http.get(VideoService.BASE_VIDEO_URL + 'licences')
51 .map(this.restExtractor.extractDataGet)
52 .subscribe(data => {
53 Object.keys(data).forEach(licenceKey => {
54 this.videoLicences.push({
55 id: parseInt(licenceKey),
56 label: data[licenceKey]
57 });
58 });
59 });
60 }
61
62 getVideo(id: string): Observable<Video> {
63 return this.http.get(VideoService.BASE_VIDEO_URL + id)
64 .map(this.restExtractor.extractDataGet)
65 .map(video_hash => new Video(video_hash))
66 .catch((res) => this.restExtractor.handleError(res));
67 }
68
69 getVideos(pagination: RestPagination, sort: SortField) {
70 const params = this.restService.buildRestGetParams(pagination, sort);
71
72 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
73 .map(res => res.json())
74 .map(this.extractVideos)
75 .catch((res) => this.restExtractor.handleError(res));
76 }
77
78 removeVideo(id: string) {
79 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
80 .map(this.restExtractor.extractDataBool)
81 .catch((res) => this.restExtractor.handleError(res));
82 }
83
84 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
85 const params = this.restService.buildRestGetParams(pagination, sort);
86
87 if (search.field) params.set('field', search.field);
88
89 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
90 .map(this.restExtractor.extractDataList)
91 .map(this.extractVideos)
92 .catch((res) => this.restExtractor.handleError(res));
93 }
94
95 reportVideo(id: string, reason: string) {
96 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
97 const body = {
98 reason
99 };
100
101 return this.authHttp.post(url, body)
102 .map(this.restExtractor.extractDataBool)
103 .catch((res) => this.restExtractor.handleError(res));
104 }
105
106 setVideoLike(id: string) {
107 return this.setVideoRate(id, 'like');
108 }
109
110 setVideoDislike(id: string) {
111 return this.setVideoRate(id, 'dislike');
112 }
113
114 getUserVideoRating(id: string) {
115 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';
116
117 return this.authHttp.get(url)
118 .map(this.restExtractor.extractDataGet)
119 .catch((res) => this.restExtractor.handleError(res));
120 }
121
122 private setVideoRate(id: string, rateType: RateType) {
123 const url = VideoService.BASE_VIDEO_URL + id + '/rate';
124 const body = {
125 rating: rateType
126 };
127
128 return this.authHttp.put(url, body)
129 .map(this.restExtractor.extractDataBool)
130 .catch((res) => this.restExtractor.handleError(res));
131 }
132
133 private extractVideos(result: ResultList) {
134 const videosJson = result.data;
135 const totalVideos = result.total;
136 const videos = [];
137 for (const videoJson of videosJson) {
138 videos.push(new Video(videoJson));
139 }
140
141 return { videos, totalVideos };
142 }
143 }