]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Client: fix loading server configurations
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core'
2 import { Observable } from 'rxjs/Observable'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
6
7 import { Search } from '../../shared'
8 import { SortField } from './sort-field.type'
9 import {
10 RestExtractor,
11 RestService,
12 UserService
13 } from '../../shared'
14 import { Video } from './video.model'
15 import { VideoPagination } from './video-pagination.model'
16 import {
17 UserVideoRate,
18 VideoRateType,
19 VideoUpdate,
20 VideoAbuseCreate,
21 UserVideoRateUpdate,
22 Video as VideoServerModel,
23 ResultList
24 } from '../../../../../shared'
25
26 @Injectable()
27 export class VideoService {
28 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
29
30 constructor (
31 private authHttp: HttpClient,
32 private restExtractor: RestExtractor,
33 private restService: RestService
34 ) {}
35
36 getVideo (uuid: string) {
37 return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
38 .map(videoHash => new Video(videoHash))
39 .catch((res) => this.restExtractor.handleError(res))
40 }
41
42 updateVideo (video: Video) {
43 const language = video.language ? video.language : null
44
45 const body: VideoUpdate = {
46 name: video.name,
47 category: video.category,
48 licence: video.licence,
49 language,
50 description: video.description,
51 tags: video.tags,
52 nsfw: video.nsfw
53 }
54
55 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body)
56 .map(this.restExtractor.extractDataBool)
57 .catch(this.restExtractor.handleError)
58 }
59
60 uploadVideo (video: FormData) {
61 const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true })
62
63 return this.authHttp.request(req)
64 .catch(this.restExtractor.handleError)
65 }
66
67 getVideos (videoPagination: VideoPagination, sort: SortField) {
68 const pagination = this.videoPaginationToRestPagination(videoPagination)
69
70 let params = new HttpParams()
71 params = this.restService.addRestGetParams(params, pagination, sort)
72
73 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
74 .map(this.extractVideos)
75 .catch((res) => this.restExtractor.handleError(res))
76 }
77
78 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
79 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
80
81 const pagination = this.videoPaginationToRestPagination(videoPagination)
82
83 let params = new HttpParams()
84 params = this.restService.addRestGetParams(params, pagination, sort)
85
86 if (search.field) params.set('field', search.field)
87
88 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
89 .map(this.extractVideos)
90 .catch((res) => this.restExtractor.handleError(res))
91 }
92
93 removeVideo (id: number) {
94 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
95 .map(this.restExtractor.extractDataBool)
96 .catch((res) => this.restExtractor.handleError(res))
97 }
98
99 reportVideo (id: number, reason: string) {
100 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
101 const body: VideoAbuseCreate = {
102 reason
103 }
104
105 return this.authHttp.post(url, body)
106 .map(this.restExtractor.extractDataBool)
107 .catch(res => this.restExtractor.handleError(res))
108 }
109
110 setVideoLike (id: number) {
111 return this.setVideoRate(id, 'like')
112 }
113
114 setVideoDislike (id: number) {
115 return this.setVideoRate(id, 'dislike')
116 }
117
118 getUserVideoRating (id: number): Observable<UserVideoRate> {
119 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating'
120
121 return this.authHttp.get(url)
122 .catch(res => this.restExtractor.handleError(res))
123 }
124
125 blacklistVideo (id: number) {
126 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
127 .map(this.restExtractor.extractDataBool)
128 .catch(res => this.restExtractor.handleError(res))
129 }
130
131 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
132 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
133 const count: number = videoPagination.itemsPerPage
134
135 return { start, count }
136 }
137
138 private setVideoRate (id: number, rateType: VideoRateType) {
139 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
140 const body: UserVideoRateUpdate = {
141 rating: rateType
142 }
143
144 return this.authHttp.put(url, body)
145 .map(this.restExtractor.extractDataBool)
146 .catch(res => this.restExtractor.handleError(res))
147 }
148
149 private extractVideos (result: ResultList<VideoServerModel>) {
150 const videosJson = result.data
151 const totalVideos = result.total
152 const videos = []
153
154 for (const videoJson of videosJson) {
155 videos.push(new Video(videoJson))
156 }
157
158 return { videos, totalVideos }
159 }
160 }