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