]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Add video category support
[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
27 constructor(
28 private authService: AuthService,
29 private authHttp: AuthHttp,
30 private http: Http,
31 private restExtractor: RestExtractor,
32 private restService: RestService
33 ) {}
34
35 loadVideoCategories() {
36 return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
37 .map(this.restExtractor.extractDataGet)
38 .subscribe(data => {
39 Object.keys(data).forEach(categoryKey => {
40 this.videoCategories.push({
41 id: parseInt(categoryKey),
42 label: data[categoryKey]
43 });
44 });
45 });
46 }
47
48 getVideo(id: string): Observable<Video> {
49 return this.http.get(VideoService.BASE_VIDEO_URL + id)
50 .map(this.restExtractor.extractDataGet)
51 .map(video_hash => new Video(video_hash))
52 .catch((res) => this.restExtractor.handleError(res));
53 }
54
55 getVideos(pagination: RestPagination, sort: SortField) {
56 const params = this.restService.buildRestGetParams(pagination, sort);
57
58 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
59 .map(res => res.json())
60 .map(this.extractVideos)
61 .catch((res) => this.restExtractor.handleError(res));
62 }
63
64 removeVideo(id: string) {
65 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
66 .map(this.restExtractor.extractDataBool)
67 .catch((res) => this.restExtractor.handleError(res));
68 }
69
70 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
71 const params = this.restService.buildRestGetParams(pagination, sort);
72
73 if (search.field) params.set('field', search.field);
74
75 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
76 .map(this.restExtractor.extractDataList)
77 .map(this.extractVideos)
78 .catch((res) => this.restExtractor.handleError(res));
79 }
80
81 reportVideo(id: string, reason: string) {
82 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
83 const body = {
84 reason
85 };
86
87 return this.authHttp.post(url, body)
88 .map(this.restExtractor.extractDataBool)
89 .catch((res) => this.restExtractor.handleError(res));
90 }
91
92 setVideoLike(id: string) {
93 return this.setVideoRate(id, 'like');
94 }
95
96 setVideoDislike(id: string) {
97 return this.setVideoRate(id, 'dislike');
98 }
99
100 getUserVideoRating(id: string) {
101 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';
102
103 return this.authHttp.get(url)
104 .map(this.restExtractor.extractDataGet)
105 .catch((res) => this.restExtractor.handleError(res));
106 }
107
108 private setVideoRate(id: string, rateType: RateType) {
109 const url = VideoService.BASE_VIDEO_URL + id + '/rate';
110 const body = {
111 rating: rateType
112 };
113
114 return this.authHttp.put(url, body)
115 .map(this.restExtractor.extractDataBool)
116 .catch((res) => this.restExtractor.handleError(res));
117 }
118
119 private extractVideos(result: ResultList) {
120 const videosJson = result.data;
121 const totalVideos = result.total;
122 const videos = [];
123 for (const videoJson of videosJson) {
124 videos.push(new Video(videoJson));
125 }
126
127 return { videos, totalVideos };
128 }
129 }