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