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