]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Add plugin translation system
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
6 import { ResultList } from '../../../../../shared/models/result-list.model'
7 import {
8 UserVideoRate,
9 UserVideoRateType,
10 UserVideoRateUpdate,
11 VideoConstant,
12 VideoFilter,
13 VideoPrivacy,
14 VideoUpdate
15 } from '../../../../../shared/models/videos'
16 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
17 import { environment } from '../../../environments/environment'
18 import { ComponentPagination } from '../rest/component-pagination.model'
19 import { RestExtractor } from '../rest/rest-extractor.service'
20 import { RestService } from '../rest/rest.service'
21 import { UserService } from '../users/user.service'
22 import { VideoSortField } from './sort-field.type'
23 import { VideoDetails } from './video-details.model'
24 import { VideoEdit } from './video-edit.model'
25 import { Video } from './video.model'
26 import { objectToFormData } from '@app/shared/misc/utils'
27 import { Account } from '@app/shared/account/account.model'
28 import { AccountService } from '@app/shared/account/account.service'
29 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
30 import { ServerService } from '@app/core'
31 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
32 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
33 import { I18n } from '@ngx-translate/i18n-polyfill'
34 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
35
36 export interface VideosProvider {
37 getVideos (parameters: {
38 videoPagination: ComponentPagination,
39 sort: VideoSortField,
40 filter?: VideoFilter,
41 categoryOneOf?: number,
42 languageOneOf?: string[]
43 }): Observable<ResultList<Video>>
44 }
45
46 @Injectable()
47 export class VideoService implements VideosProvider {
48 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
49 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
50
51 constructor (
52 private authHttp: HttpClient,
53 private restExtractor: RestExtractor,
54 private restService: RestService,
55 private serverService: ServerService,
56 private i18n: I18n
57 ) {}
58
59 getVideoViewUrl (uuid: string) {
60 return VideoService.BASE_VIDEO_URL + uuid + '/views'
61 }
62
63 getUserWatchingVideoUrl (uuid: string) {
64 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
65 }
66
67 getVideo (options: { videoId: string }): Observable<VideoDetails> {
68 return this.serverService.localeObservable
69 .pipe(
70 switchMap(translations => {
71 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
72 .pipe(map(videoHash => ({ videoHash, translations })))
73 }),
74 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
75 catchError(err => this.restExtractor.handleError(err))
76 )
77 }
78
79 updateVideo (video: VideoEdit) {
80 const language = video.language || null
81 const licence = video.licence || null
82 const category = video.category || null
83 const description = video.description || null
84 const support = video.support || null
85 const scheduleUpdate = video.scheduleUpdate || null
86 const originallyPublishedAt = video.originallyPublishedAt || null
87
88 const body: VideoUpdate = {
89 name: video.name,
90 category,
91 licence,
92 language,
93 support,
94 description,
95 channelId: video.channelId,
96 privacy: video.privacy,
97 tags: video.tags,
98 nsfw: video.nsfw,
99 waitTranscoding: video.waitTranscoding,
100 commentsEnabled: video.commentsEnabled,
101 downloadEnabled: video.downloadEnabled,
102 thumbnailfile: video.thumbnailfile,
103 previewfile: video.previewfile,
104 scheduleUpdate,
105 originallyPublishedAt
106 }
107
108 const data = objectToFormData(body)
109
110 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
111 .pipe(
112 map(this.restExtractor.extractDataBool),
113 catchError(err => this.restExtractor.handleError(err))
114 )
115 }
116
117 uploadVideo (video: FormData) {
118 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
119
120 return this.authHttp
121 .request<{ video: { id: number, uuid: string } }>(req)
122 .pipe(catchError(err => this.restExtractor.handleError(err)))
123 }
124
125 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<ResultList<Video>> {
126 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
127
128 let params = new HttpParams()
129 params = this.restService.addRestGetParams(params, pagination, sort)
130
131 return this.authHttp
132 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
133 .pipe(
134 switchMap(res => this.extractVideos(res)),
135 catchError(err => this.restExtractor.handleError(err))
136 )
137 }
138
139 getAccountVideos (
140 account: Account,
141 videoPagination: ComponentPagination,
142 sort: VideoSortField
143 ): Observable<ResultList<Video>> {
144 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
145
146 let params = new HttpParams()
147 params = this.restService.addRestGetParams(params, pagination, sort)
148
149 return this.authHttp
150 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
151 .pipe(
152 switchMap(res => this.extractVideos(res)),
153 catchError(err => this.restExtractor.handleError(err))
154 )
155 }
156
157 getVideoChannelVideos (
158 videoChannel: VideoChannel,
159 videoPagination: ComponentPagination,
160 sort: VideoSortField
161 ): Observable<ResultList<Video>> {
162 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
163
164 let params = new HttpParams()
165 params = this.restService.addRestGetParams(params, pagination, sort)
166
167 return this.authHttp
168 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
169 .pipe(
170 switchMap(res => this.extractVideos(res)),
171 catchError(err => this.restExtractor.handleError(err))
172 )
173 }
174
175 getPlaylistVideos (
176 videoPlaylistId: number | string,
177 videoPagination: ComponentPagination
178 ): Observable<ResultList<Video>> {
179 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
180
181 let params = new HttpParams()
182 params = this.restService.addRestGetParams(params, pagination)
183
184 return this.authHttp
185 .get<ResultList<Video>>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos', { params })
186 .pipe(
187 switchMap(res => this.extractVideos(res)),
188 catchError(err => this.restExtractor.handleError(err))
189 )
190 }
191
192 getUserSubscriptionVideos (parameters: {
193 videoPagination: ComponentPagination,
194 sort: VideoSortField
195 }): Observable<ResultList<Video>> {
196 const { videoPagination, sort } = parameters
197 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
198
199 let params = new HttpParams()
200 params = this.restService.addRestGetParams(params, pagination, sort)
201
202 return this.authHttp
203 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
204 .pipe(
205 switchMap(res => this.extractVideos(res)),
206 catchError(err => this.restExtractor.handleError(err))
207 )
208 }
209
210 getVideos (parameters: {
211 videoPagination: ComponentPagination,
212 sort: VideoSortField,
213 filter?: VideoFilter,
214 categoryOneOf?: number,
215 languageOneOf?: string[]
216 }): Observable<ResultList<Video>> {
217 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
218
219 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
220
221 let params = new HttpParams()
222 params = this.restService.addRestGetParams(params, pagination, sort)
223
224 if (filter) {
225 params = params.set('filter', filter)
226 }
227
228 if (categoryOneOf) {
229 params = params.set('categoryOneOf', categoryOneOf + '')
230 }
231
232 if (languageOneOf) {
233 for (const l of languageOneOf) {
234 params = params.append('languageOneOf[]', l)
235 }
236 }
237
238 return this.authHttp
239 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
240 .pipe(
241 switchMap(res => this.extractVideos(res)),
242 catchError(err => this.restExtractor.handleError(err))
243 )
244 }
245
246 buildBaseFeedUrls (params: HttpParams) {
247 const feeds = [
248 {
249 format: FeedFormat.RSS,
250 label: 'rss 2.0',
251 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
252 },
253 {
254 format: FeedFormat.ATOM,
255 label: 'atom 1.0',
256 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
257 },
258 {
259 format: FeedFormat.JSON,
260 label: 'json 1.0',
261 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
262 }
263 ]
264
265 if (params && params.keys().length !== 0) {
266 for (const feed of feeds) {
267 feed.url += '?' + params.toString()
268 }
269 }
270
271 return feeds
272 }
273
274 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
275 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
276
277 if (filter) params = params.set('filter', filter)
278
279 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
280
281 return this.buildBaseFeedUrls(params)
282 }
283
284 getAccountFeedUrls (accountId: number) {
285 let params = this.restService.addRestGetParams(new HttpParams())
286 params = params.set('accountId', accountId.toString())
287
288 return this.buildBaseFeedUrls(params)
289 }
290
291 getVideoChannelFeedUrls (videoChannelId: number) {
292 let params = this.restService.addRestGetParams(new HttpParams())
293 params = params.set('videoChannelId', videoChannelId.toString())
294
295 return this.buildBaseFeedUrls(params)
296 }
297
298 removeVideo (id: number) {
299 return this.authHttp
300 .delete(VideoService.BASE_VIDEO_URL + id)
301 .pipe(
302 map(this.restExtractor.extractDataBool),
303 catchError(err => this.restExtractor.handleError(err))
304 )
305 }
306
307 loadCompleteDescription (descriptionPath: string) {
308 return this.authHttp
309 .get<{ description: string }>(environment.apiUrl + descriptionPath)
310 .pipe(
311 map(res => res.description),
312 catchError(err => this.restExtractor.handleError(err))
313 )
314 }
315
316 setVideoLike (id: number) {
317 return this.setVideoRate(id, 'like')
318 }
319
320 setVideoDislike (id: number) {
321 return this.setVideoRate(id, 'dislike')
322 }
323
324 unsetVideoLike (id: number) {
325 return this.setVideoRate(id, 'none')
326 }
327
328 getUserVideoRating (id: number) {
329 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
330
331 return this.authHttp.get<UserVideoRate>(url)
332 .pipe(catchError(err => this.restExtractor.handleError(err)))
333 }
334
335 extractVideos (result: ResultList<VideoServerModel>) {
336 return this.serverService.localeObservable
337 .pipe(
338 map(translations => {
339 const videosJson = result.data
340 const totalVideos = result.total
341 const videos: Video[] = []
342
343 for (const videoJson of videosJson) {
344 videos.push(new Video(videoJson, translations))
345 }
346
347 return { total: totalVideos, data: videos }
348 })
349 )
350 }
351
352 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
353 const newPrivacies = privacies.slice()
354
355 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
356 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
357
358 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
359 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
360
361 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
362 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
363
364 return privacies
365 }
366
367 private setVideoRate (id: number, rateType: UserVideoRateType) {
368 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
369 const body: UserVideoRateUpdate = {
370 rating: rateType
371 }
372
373 return this.authHttp
374 .put(url, body)
375 .pipe(
376 map(this.restExtractor.extractDataBool),
377 catchError(err => this.restExtractor.handleError(err))
378 )
379 }
380 }