]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
230809ef 1import { Injectable } from '@angular/core';
de59c48f 2import { Http } 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
4fd8aa32
C
25 constructor(
26 private authService: AuthService,
bd5c83a8 27 private authHttp: AuthHttp,
de59c48f
C
28 private http: Http,
29 private restExtractor: RestExtractor,
30 private restService: RestService
4fd8aa32
C
31 ) {}
32
de59c48f 33 getVideo(id: string): Observable<Video> {
4fd8aa32 34 return this.http.get(VideoService.BASE_VIDEO_URL + id)
de59c48f 35 .map(this.restExtractor.extractDataGet)
d1992b93 36 .map(video_hash => new Video(video_hash))
de59c48f 37 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32 38 }
dc8bc31b 39
de59c48f
C
40 getVideos(pagination: RestPagination, sort: SortField) {
41 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 42
ccf6ed16 43 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
501bc6c2
C
44 .map(res => res.json())
45 .map(this.extractVideos)
de59c48f 46 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
47 }
48
dc8bc31b 49 removeVideo(id: string) {
bd5c83a8 50 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
de59c48f
C
51 .map(this.restExtractor.extractDataBool)
52 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
53 }
54
de59c48f
C
55 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
56 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 57
471bc22f 58 if (search.field) params.set('field', search.field);
cf20596c 59
ccf6ed16 60 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
de59c48f 61 .map(this.restExtractor.extractDataList)
501bc6c2 62 .map(this.extractVideos)
de59c48f 63 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32
C
64 }
65
4f8c0eb0 66 reportVideo(id: string, reason: string) {
d38b8281 67 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
4f8c0eb0
C
68 const body = {
69 reason
70 };
4f8c0eb0
C
71
72 return this.authHttp.post(url, body)
d38b8281
C
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));
4f8c0eb0
C
102 }
103
de59c48f
C
104 private extractVideos(result: ResultList) {
105 const videosJson = result.data;
106 const totalVideos = result.total;
501bc6c2 107 const videos = [];
de59c48f
C
108 for (const videoJson of videosJson) {
109 videos.push(new Video(videoJson));
501bc6c2
C
110 }
111
32294074 112 return { videos, totalVideos };
501bc6c2 113 }
dc8bc31b 114}