]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Client: add basic support for updating a video
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core';
2 import { Http, Headers, RequestOptions } 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 videoLanguages: Array<{ id: number, label: string }> = [];
28
29 constructor(
30 private authService: AuthService,
31 private authHttp: AuthHttp,
32 private http: Http,
33 private restExtractor: RestExtractor,
34 private restService: RestService
35 ) {}
36
37 loadVideoCategories() {
38 return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
39 .map(this.restExtractor.extractDataGet)
40 .subscribe(data => {
41 Object.keys(data).forEach(categoryKey => {
42 this.videoCategories.push({
43 id: parseInt(categoryKey),
44 label: data[categoryKey]
45 });
46 });
47 });
48 }
49
50 loadVideoLicences() {
51 return this.http.get(VideoService.BASE_VIDEO_URL + 'licences')
52 .map(this.restExtractor.extractDataGet)
53 .subscribe(data => {
54 Object.keys(data).forEach(licenceKey => {
55 this.videoLicences.push({
56 id: parseInt(licenceKey),
57 label: data[licenceKey]
58 });
59 });
60 });
61 }
62
63 loadVideoLanguages() {
64 return this.http.get(VideoService.BASE_VIDEO_URL + 'languages')
65 .map(this.restExtractor.extractDataGet)
66 .subscribe(data => {
67 Object.keys(data).forEach(languageKey => {
68 this.videoLanguages.push({
69 id: parseInt(languageKey),
70 label: data[languageKey]
71 });
72 });
73 });
74 }
75
76 getVideo(id: string): Observable<Video> {
77 return this.http.get(VideoService.BASE_VIDEO_URL + id)
78 .map(this.restExtractor.extractDataGet)
79 .map(video_hash => new Video(video_hash))
80 .catch((res) => this.restExtractor.handleError(res));
81 }
82
83 updateVideo(video: Video) {
84 const body = {
85 name: video.name,
86 category: video.category,
87 licence: video.licence,
88 language: video.language,
89 description: video.description,
90 tags: video.tags
91 };
92 const headers = new Headers({ 'Content-Type': 'application/json' });
93 const options = new RequestOptions({ headers: headers });
94
95 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options)
96 .map(this.restExtractor.extractDataBool)
97 .catch(this.restExtractor.handleError);
98 }
99
100 getVideos(pagination: RestPagination, sort: SortField) {
101 const params = this.restService.buildRestGetParams(pagination, sort);
102
103 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
104 .map(res => res.json())
105 .map(this.extractVideos)
106 .catch((res) => this.restExtractor.handleError(res));
107 }
108
109 removeVideo(id: string) {
110 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
111 .map(this.restExtractor.extractDataBool)
112 .catch((res) => this.restExtractor.handleError(res));
113 }
114
115 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
116 const params = this.restService.buildRestGetParams(pagination, sort);
117
118 if (search.field) params.set('field', search.field);
119
120 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
121 .map(this.restExtractor.extractDataList)
122 .map(this.extractVideos)
123 .catch((res) => this.restExtractor.handleError(res));
124 }
125
126 reportVideo(id: string, reason: string) {
127 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
128 const body = {
129 reason
130 };
131
132 return this.authHttp.post(url, body)
133 .map(this.restExtractor.extractDataBool)
134 .catch((res) => this.restExtractor.handleError(res));
135 }
136
137 setVideoLike(id: string) {
138 return this.setVideoRate(id, 'like');
139 }
140
141 setVideoDislike(id: string) {
142 return this.setVideoRate(id, 'dislike');
143 }
144
145 getUserVideoRating(id: string) {
146 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';
147
148 return this.authHttp.get(url)
149 .map(this.restExtractor.extractDataGet)
150 .catch((res) => this.restExtractor.handleError(res));
151 }
152
153 private setVideoRate(id: string, rateType: RateType) {
154 const url = VideoService.BASE_VIDEO_URL + id + '/rate';
155 const body = {
156 rating: rateType
157 };
158
159 return this.authHttp.put(url, body)
160 .map(this.restExtractor.extractDataBool)
161 .catch((res) => this.restExtractor.handleError(res));
162 }
163
164 private extractVideos(result: ResultList) {
165 const videosJson = result.data;
166 const totalVideos = result.total;
167 const videos = [];
168 for (const videoJson of videosJson) {
169 videos.push(new Video(videoJson));
170 }
171
172 return { videos, totalVideos };
173 }
174 }