]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Client: redirect /videos/:id to /videos/watch/:id
[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
5 import { Search } from '../../shared';
6 import { SortField } from './sort-field.type';
7 import { AuthService } from '../../core';
8 import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
9 import { Video } from './video.model';
10
11 @Injectable()
12 export class VideoService {
13 private static BASE_VIDEO_URL = '/api/v1/videos/';
14
15 constructor(
16 private authService: AuthService,
17 private authHttp: AuthHttp,
18 private http: Http,
19 private restExtractor: RestExtractor,
20 private restService: RestService
21 ) {}
22
23 getVideo(id: string): Observable<Video> {
24 return this.http.get(VideoService.BASE_VIDEO_URL + id)
25 .map(this.restExtractor.extractDataGet)
26 .map(video_hash => new Video(video_hash))
27 .catch((res) => this.restExtractor.handleError(res));
28 }
29
30 getVideos(pagination: RestPagination, sort: SortField) {
31 const params = this.restService.buildRestGetParams(pagination, sort);
32
33 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
34 .map(res => res.json())
35 .map(this.extractVideos)
36 .catch((res) => this.restExtractor.handleError(res));
37 }
38
39 removeVideo(id: string) {
40 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
41 .map(this.restExtractor.extractDataBool)
42 .catch((res) => this.restExtractor.handleError(res));
43 }
44
45 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
46 const params = this.restService.buildRestGetParams(pagination, sort);
47
48 if (search.field) params.set('field', search.field);
49
50 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
51 .map(this.restExtractor.extractDataList)
52 .map(this.extractVideos)
53 .catch((res) => this.restExtractor.handleError(res));
54 }
55
56 private extractVideos(result: ResultList) {
57 const videosJson = result.data;
58 const totalVideos = result.total;
59 const videos = [];
60 for (const videoJson of videosJson) {
61 videos.push(new Video(videoJson));
62 }
63
64 return { videos, totalVideos };
65 }
66 }