]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.service.ts
Relax on tags (accept any characters and not required anymore)
[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
6e07c3de
C
25 videoCategories: Array<{ id: number, label: string }> = [];
26
4fd8aa32
C
27 constructor(
28 private authService: AuthService,
bd5c83a8 29 private authHttp: AuthHttp,
de59c48f
C
30 private http: Http,
31 private restExtractor: RestExtractor,
32 private restService: RestService
4fd8aa32
C
33 ) {}
34
6e07c3de
C
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
de59c48f 48 getVideo(id: string): Observable<Video> {
4fd8aa32 49 return this.http.get(VideoService.BASE_VIDEO_URL + id)
de59c48f 50 .map(this.restExtractor.extractDataGet)
d1992b93 51 .map(video_hash => new Video(video_hash))
de59c48f 52 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32 53 }
dc8bc31b 54
de59c48f
C
55 getVideos(pagination: RestPagination, sort: SortField) {
56 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 57
ccf6ed16 58 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
501bc6c2
C
59 .map(res => res.json())
60 .map(this.extractVideos)
de59c48f 61 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
62 }
63
dc8bc31b 64 removeVideo(id: string) {
bd5c83a8 65 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
de59c48f
C
66 .map(this.restExtractor.extractDataBool)
67 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
68 }
69
de59c48f
C
70 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
71 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 72
471bc22f 73 if (search.field) params.set('field', search.field);
cf20596c 74
ccf6ed16 75 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
de59c48f 76 .map(this.restExtractor.extractDataList)
501bc6c2 77 .map(this.extractVideos)
de59c48f 78 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32
C
79 }
80
4f8c0eb0 81 reportVideo(id: string, reason: string) {
d38b8281 82 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
4f8c0eb0
C
83 const body = {
84 reason
85 };
4f8c0eb0
C
86
87 return this.authHttp.post(url, body)
d38b8281
C
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));
4f8c0eb0
C
117 }
118
de59c48f
C
119 private extractVideos(result: ResultList) {
120 const videosJson = result.data;
121 const totalVideos = result.total;
501bc6c2 122 const videos = [];
de59c48f
C
123 for (const videoJson of videosJson) {
124 videos.push(new Video(videoJson));
501bc6c2
C
125 }
126
32294074 127 return { videos, totalVideos };
501bc6c2 128 }
dc8bc31b 129}