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