]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.service.ts
Video views is implemented. Closes https://github.com/Chocobozzz/PeerTube/issues/41
[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';
693b1aba
C
9import { AuthService } from '../../core';
10import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
41a2aee3 11import { Video } from './video.model';
dc8bc31b
C
12
13@Injectable()
41a2aee3 14export class VideoService {
ccf6ed16 15 private static BASE_VIDEO_URL = '/api/v1/videos/';
dc8bc31b 16
4fd8aa32
C
17 constructor(
18 private authService: AuthService,
bd5c83a8 19 private authHttp: AuthHttp,
de59c48f
C
20 private http: Http,
21 private restExtractor: RestExtractor,
22 private restService: RestService
4fd8aa32
C
23 ) {}
24
de59c48f 25 getVideo(id: string): Observable<Video> {
4fd8aa32 26 return this.http.get(VideoService.BASE_VIDEO_URL + id)
de59c48f 27 .map(this.restExtractor.extractDataGet)
d1992b93 28 .map(video_hash => new Video(video_hash))
de59c48f 29 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32 30 }
dc8bc31b 31
de59c48f
C
32 getVideos(pagination: RestPagination, sort: SortField) {
33 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 34
ccf6ed16 35 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
501bc6c2
C
36 .map(res => res.json())
37 .map(this.extractVideos)
de59c48f 38 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
39 }
40
dc8bc31b 41 removeVideo(id: string) {
bd5c83a8 42 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
de59c48f
C
43 .map(this.restExtractor.extractDataBool)
44 .catch((res) => this.restExtractor.handleError(res));
dc8bc31b
C
45 }
46
de59c48f
C
47 searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
48 const params = this.restService.buildRestGetParams(pagination, sort);
cf20596c 49
471bc22f 50 if (search.field) params.set('field', search.field);
cf20596c 51
ccf6ed16 52 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
de59c48f 53 .map(this.restExtractor.extractDataList)
501bc6c2 54 .map(this.extractVideos)
de59c48f 55 .catch((res) => this.restExtractor.handleError(res));
4fd8aa32
C
56 }
57
4f8c0eb0
C
58 reportVideo(id: string, reason: string) {
59 const body = {
60 reason
61 };
62 const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
63
64 return this.authHttp.post(url, body)
65 .map(this.restExtractor.extractDataBool)
66 .catch((res) => this.restExtractor.handleError(res));
67 }
68
de59c48f
C
69 private extractVideos(result: ResultList) {
70 const videosJson = result.data;
71 const totalVideos = result.total;
501bc6c2 72 const videos = [];
de59c48f
C
73 for (const videoJson of videosJson) {
74 videos.push(new Video(videoJson));
501bc6c2
C
75 }
76
32294074 77 return { videos, totalVideos };
501bc6c2 78 }
dc8bc31b 79}