]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
230809ef 1import { Injectable } from '@angular/core';
d8e689b8 2import { Http, Headers, RequestOptions } from '@angular/http';
5555f886 3import { Observable } from 'rxjs/Observable';
c16ce1de
C
4import 'rxjs/add/operator/catch';
5import 'rxjs/add/operator/map';
dc8bc31b 6
4a6995be 7import { Search } from '../../shared';
41a2aee3 8import { SortField } from './sort-field.type';
d38b8281 9import { RateType } from './rate-type.type';
693b1aba 10import { AuthService } from '../../core';
d38b8281
C
11import {
12 AuthHttp,
13 RestExtractor,
14 RestPagination,
15 RestService,
16 ResultList,
17 UserService
18} from '../../shared';
41a2aee3 19import { Video } from './video.model';
dc8bc31b
C
20
21@Injectable()
41a2aee3 22export class VideoService {
ccf6ed16 23 private static BASE_VIDEO_URL = '/api/v1/videos/';
dc8bc31b 24
6e07c3de 25 videoCategories: Array<{ id: number, label: string }> = [];
d07137b9 26 videoLicences: Array<{ id: number, label: string }> = [];
db216afd 27 videoLanguages: Array<{ id: number, label: string }> = [];
6e07c3de 28
4fd8aa32
C
29 constructor(
30 private authService: AuthService,
bd5c83a8 31 private authHttp: AuthHttp,
de59c48f
C
32 private http: Http,
33 private restExtractor: RestExtractor,
34 private restService: RestService
4fd8aa32
C
35 ) {}
36
6e07c3de
C
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
d07137b9
C
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
db216afd
C
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
de59c48f 76 getVideo(id: string): Observable<Video> {
4fd8aa32 77 return this.http.get(VideoService.BASE_VIDEO_URL + id)
de59c48f 78 .map(this.restExtractor.extractDataGet)
d1992b93 79 .map(video_hash => new Video(video_hash))
de59c48f 80 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32 81 }
dc8bc31b 82
d8e689b8
C
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
de59c48f
C
100 getVideos(pagination: RestPagination, sort: SortField) {
101 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 102
ccf6ed16 103 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
501bc6c2
C
104 .map(res => res.json())
105 .map(this.extractVideos)
de59c48f 106 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
107 }
108
dc8bc31b 109 removeVideo(id: string) {
bd5c83a8 110 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
de59c48f
C
111 .map(this.restExtractor.extractDataBool)
112 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
113 }
114
de59c48f
C
115 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
116 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 117
471bc22f 118 if (search.field) params.set('field', search.field);
cf20596c 119
ccf6ed16 120 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
de59c48f 121 .map(this.restExtractor.extractDataList)
501bc6c2 122 .map(this.extractVideos)
de59c48f 123 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32
C
124 }
125
4f8c0eb0 126 reportVideo(id: string, reason: string) {
d38b8281 127 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
4f8c0eb0
C
128 const body = {
129 reason
130 };
4f8c0eb0
C
131
132 return this.authHttp.post(url, body)
d38b8281
C
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));
4f8c0eb0
C
162 }
163
de59c48f
C
164 private extractVideos(result: ResultList) {
165 const videosJson = result.data;
166 const totalVideos = result.total;
501bc6c2 167 const videos = [];
de59c48f
C
168 for (const videoJson of videosJson) {
169 videos.push(new Video(videoJson));
501bc6c2
C
170 }
171
32294074 172 return { videos, totalVideos };
501bc6c2 173 }
dc8bc31b 174}