]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Decrease AP video cache
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
1 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { Observable } from 'rxjs/Observable'
6 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
7 import { ResultList } from '../../../../../shared/models/result-list.model'
8 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
9 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
10 import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
11 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
12 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
13 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
14 import { environment } from '../../../environments/environment'
15 import { ComponentPagination } from '../rest/component-pagination.model'
16 import { RestExtractor } from '../rest/rest-extractor.service'
17 import { RestService } from '../rest/rest.service'
18 import { UserService } from '../users/user.service'
19 import { VideoSortField } from './sort-field.type'
20 import { VideoDetails } from './video-details.model'
21 import { VideoEdit } from './video-edit.model'
22 import { Video } from './video.model'
23 import { objectToFormData } from '@app/shared/misc/utils'
24 import { Account } from '@app/shared/account/account.model'
25 import { AccountService } from '@app/shared/account/account.service'
26 import { VideoChannel } from '../../../../../shared/models/videos'
27 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
28
29 @Injectable()
30 export class VideoService {
31 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
32 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
33
34 constructor (
35 private authHttp: HttpClient,
36 private restExtractor: RestExtractor,
37 private restService: RestService
38 ) {}
39
40 getVideoViewUrl (uuid: string) {
41 return VideoService.BASE_VIDEO_URL + uuid + '/views'
42 }
43
44 getVideo (uuid: string): Observable<VideoDetails> {
45 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
46 .map(videoHash => new VideoDetails(videoHash))
47 .catch((res) => this.restExtractor.handleError(res))
48 }
49
50 viewVideo (uuid: string): Observable<VideoDetails> {
51 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
52 .map(this.restExtractor.extractDataBool)
53 .catch(this.restExtractor.handleError)
54 }
55
56 updateVideo (video: VideoEdit) {
57 const language = video.language || null
58 const licence = video.licence || null
59 const category = video.category || null
60 const description = video.description || null
61 const support = video.support || null
62
63 const body: VideoUpdate = {
64 name: video.name,
65 category,
66 licence,
67 language,
68 support,
69 description,
70 privacy: video.privacy,
71 tags: video.tags,
72 nsfw: video.nsfw,
73 commentsEnabled: video.commentsEnabled,
74 thumbnailfile: video.thumbnailfile,
75 previewfile: video.previewfile
76 }
77
78 const data = objectToFormData(body)
79
80 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
81 .map(this.restExtractor.extractDataBool)
82 .catch(this.restExtractor.handleError)
83 }
84
85 uploadVideo (video: FormData) {
86 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
87
88 return this.authHttp
89 .request(req)
90 .catch(this.restExtractor.handleError)
91 }
92
93 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
94 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
95
96 let params = new HttpParams()
97 params = this.restService.addRestGetParams(params, pagination, sort)
98
99 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
100 .map(this.extractVideos)
101 .catch((res) => this.restExtractor.handleError(res))
102 }
103
104 getAccountVideos (
105 account: Account,
106 videoPagination: ComponentPagination,
107 sort: VideoSortField
108 ): Observable<{ videos: Video[], totalVideos: number}> {
109 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
110
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, pagination, sort)
113
114 return this.authHttp
115 .get(AccountService.BASE_ACCOUNT_URL + account.id + '/videos', { params })
116 .map(this.extractVideos)
117 .catch((res) => this.restExtractor.handleError(res))
118 }
119
120 getVideoChannelVideos (
121 videoChannel: VideoChannel,
122 videoPagination: ComponentPagination,
123 sort: VideoSortField
124 ): Observable<{ videos: Video[], totalVideos: number}> {
125 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
126
127 let params = new HttpParams()
128 params = this.restService.addRestGetParams(params, pagination, sort)
129
130 return this.authHttp
131 .get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
132 .map(this.extractVideos)
133 .catch((res) => this.restExtractor.handleError(res))
134 }
135
136 getVideos (
137 videoPagination: ComponentPagination,
138 sort: VideoSortField,
139 filter?: VideoFilter
140 ): Observable<{ videos: Video[], totalVideos: number}> {
141 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
142
143 let params = new HttpParams()
144 params = this.restService.addRestGetParams(params, pagination, sort)
145
146 if (filter) {
147 params = params.set('filter', filter)
148 }
149
150 return this.authHttp
151 .get(VideoService.BASE_VIDEO_URL, { params })
152 .map(this.extractVideos)
153 .catch((res) => this.restExtractor.handleError(res))
154 }
155
156 buildBaseFeedUrls (params: HttpParams) {
157 const feeds = [
158 {
159 label: 'rss 2.0',
160 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
161 },
162 {
163 label: 'atom 1.0',
164 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
165 },
166 {
167 label: 'json 1.0',
168 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
169 }
170 ]
171
172 if (params && params.keys().length !== 0) {
173 for (const feed of feeds) {
174 feed.url += '?' + params.toString()
175 }
176 }
177
178 return feeds
179 }
180
181 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
182 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
183
184 if (filter) params = params.set('filter', filter)
185
186 return this.buildBaseFeedUrls(params)
187 }
188
189 getAccountFeedUrls (accountId: number) {
190 let params = this.restService.addRestGetParams(new HttpParams())
191 params = params.set('accountId', accountId.toString())
192
193 return this.buildBaseFeedUrls(params)
194 }
195
196 getVideoChannelFeedUrls (videoChannelId: number) {
197 let params = this.restService.addRestGetParams(new HttpParams())
198 params = params.set('videoChannelId', videoChannelId.toString())
199
200 return this.buildBaseFeedUrls(params)
201 }
202
203 searchVideos (
204 search: string,
205 videoPagination: ComponentPagination,
206 sort: VideoSortField
207 ): Observable<{ videos: Video[], totalVideos: number}> {
208 const url = VideoService.BASE_VIDEO_URL + 'search'
209
210 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
211
212 let params = new HttpParams()
213 params = this.restService.addRestGetParams(params, pagination, sort)
214 params = params.append('search', search)
215
216 return this.authHttp
217 .get<ResultList<VideoServerModel>>(url, { params })
218 .map(this.extractVideos)
219 .catch((res) => this.restExtractor.handleError(res))
220 }
221
222 removeVideo (id: number) {
223 return this.authHttp
224 .delete(VideoService.BASE_VIDEO_URL + id)
225 .map(this.restExtractor.extractDataBool)
226 .catch((res) => this.restExtractor.handleError(res))
227 }
228
229 loadCompleteDescription (descriptionPath: string) {
230 return this.authHttp
231 .get(environment.apiUrl + descriptionPath)
232 .map(res => res['description'])
233 .catch((res) => this.restExtractor.handleError(res))
234 }
235
236 setVideoLike (id: number) {
237 return this.setVideoRate(id, 'like')
238 }
239
240 setVideoDislike (id: number) {
241 return this.setVideoRate(id, 'dislike')
242 }
243
244 unsetVideoLike (id: number) {
245 return this.setVideoRate(id, 'none')
246 }
247
248 getUserVideoRating (id: number): Observable<UserVideoRate> {
249 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
250
251 return this.authHttp
252 .get(url)
253 .catch(res => this.restExtractor.handleError(res))
254 }
255
256 private setVideoRate (id: number, rateType: VideoRateType) {
257 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
258 const body: UserVideoRateUpdate = {
259 rating: rateType
260 }
261
262 return this.authHttp
263 .put(url, body)
264 .map(this.restExtractor.extractDataBool)
265 .catch(res => this.restExtractor.handleError(res))
266 }
267
268 private extractVideos (result: ResultList<VideoServerModel>) {
269 const videosJson = result.data
270 const totalVideos = result.total
271 const videos = []
272
273 for (const videoJson of videosJson) {
274 videos.push(new Video(videoJson))
275 }
276
277 return { videos, totalVideos }
278 }
279 }