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'
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'
37 export interface VideosProvider {
38 getVideos (parameters: {
39 videoPagination: ComponentPagination,
42 categoryOneOf?: number,
43 languageOneOf?: string[]
44 }): Observable<ResultList<Video>>
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.'
53 private authHttp: HttpClient,
54 private restExtractor: RestExtractor,
55 private restService: RestService,
56 private serverService: ServerService,
60 getVideoViewUrl (uuid: string) {
61 return VideoService.BASE_VIDEO_URL + uuid + '/views'
64 getUserWatchingVideoUrl (uuid: string) {
65 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
68 getVideo (options: { videoId: string }): Observable<VideoDetails> {
69 return this.serverService.localeObservable
71 switchMap(translations => {
72 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
73 .pipe(map(videoHash => ({ videoHash, translations })))
75 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
76 catchError(err => this.restExtractor.handleError(err))
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
89 const body: VideoUpdate = {
96 channelId: video.channelId,
97 privacy: video.privacy,
100 waitTranscoding: video.waitTranscoding,
101 commentsEnabled: video.commentsEnabled,
102 downloadEnabled: video.downloadEnabled,
103 thumbnailfile: video.thumbnailfile,
104 previewfile: video.previewfile,
106 originallyPublishedAt
109 const data = objectToFormData(body)
111 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
113 map(this.restExtractor.extractDataBool),
114 catchError(err => this.restExtractor.handleError(err))
118 uploadVideo (video: FormData) {
119 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
122 .request<{ video: { id: number, uuid: string } }>(req)
123 .pipe(catchError(err => this.restExtractor.handleError(err)))
126 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<ResultList<Video>> {
127 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
129 let params = new HttpParams()
130 params = this.restService.addRestGetParams(params, pagination, sort)
133 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
135 switchMap(res => this.extractVideos(res)),
136 catchError(err => this.restExtractor.handleError(err))
142 videoPagination: ComponentPagination,
144 ): Observable<ResultList<Video>> {
145 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
147 let params = new HttpParams()
148 params = this.restService.addRestGetParams(params, pagination, sort)
151 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
153 switchMap(res => this.extractVideos(res)),
154 catchError(err => this.restExtractor.handleError(err))
158 getVideoChannelVideos (
159 videoChannel: VideoChannel,
160 videoPagination: ComponentPagination,
162 ): Observable<ResultList<Video>> {
163 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
165 let params = new HttpParams()
166 params = this.restService.addRestGetParams(params, pagination, sort)
169 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
171 switchMap(res => this.extractVideos(res)),
172 catchError(err => this.restExtractor.handleError(err))
177 videoPlaylistId: number | string,
178 videoPagination: ComponentPagination
179 ): Observable<ResultList<Video>> {
180 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
182 let params = new HttpParams()
183 params = this.restService.addRestGetParams(params, pagination)
186 .get<ResultList<Video>>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos', { params })
188 switchMap(res => this.extractVideos(res)),
189 catchError(err => this.restExtractor.handleError(err))
193 getUserSubscriptionVideos (parameters: {
194 videoPagination: ComponentPagination,
196 }): Observable<ResultList<Video>> {
197 const { videoPagination, sort } = parameters
198 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
200 let params = new HttpParams()
201 params = this.restService.addRestGetParams(params, pagination, sort)
204 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
206 switchMap(res => this.extractVideos(res)),
207 catchError(err => this.restExtractor.handleError(err))
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
220 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
222 let params = new HttpParams()
223 params = this.restService.addRestGetParams(params, pagination, sort)
226 params = params.set('filter', filter)
230 params = params.set('categoryOneOf', categoryOneOf + '')
234 for (const l of languageOneOf) {
235 params = params.append('languageOneOf[]', l)
240 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
242 switchMap(res => this.extractVideos(res)),
243 catchError(err => this.restExtractor.handleError(err))
247 buildBaseFeedUrls (params: HttpParams) {
250 format: FeedFormat.RSS,
252 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
255 format: FeedFormat.ATOM,
257 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
260 format: FeedFormat.JSON,
262 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
266 if (params && params.keys().length !== 0) {
267 for (const feed of feeds) {
268 feed.url += '?' + params.toString()
275 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
276 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
278 if (filter) params = params.set('filter', filter)
280 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
282 return this.buildBaseFeedUrls(params)
285 getAccountFeedUrls (accountId: number) {
286 let params = this.restService.addRestGetParams(new HttpParams())
287 params = params.set('accountId', accountId.toString())
289 return this.buildBaseFeedUrls(params)
292 getVideoChannelFeedUrls (videoChannelId: number) {
293 let params = this.restService.addRestGetParams(new HttpParams())
294 params = params.set('videoChannelId', videoChannelId.toString())
296 return this.buildBaseFeedUrls(params)
299 removeVideo (id: number) {
301 .delete(VideoService.BASE_VIDEO_URL + id)
303 map(this.restExtractor.extractDataBool),
304 catchError(err => this.restExtractor.handleError(err))
308 loadCompleteDescription (descriptionPath: string) {
310 .get<{ description: string }>(environment.apiUrl + descriptionPath)
312 map(res => res.description),
313 catchError(err => this.restExtractor.handleError(err))
317 setVideoLike (id: number) {
318 return this.setVideoRate(id, 'like')
321 setVideoDislike (id: number) {
322 return this.setVideoRate(id, 'dislike')
325 unsetVideoLike (id: number) {
326 return this.setVideoRate(id, 'none')
329 getUserVideoRating (id: number) {
330 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
332 return this.authHttp.get<UserVideoRate>(url)
333 .pipe(catchError(err => this.restExtractor.handleError(err)))
336 extractVideos (result: ResultList<VideoServerModel>) {
337 return this.serverService.localeObservable
339 map(translations => {
340 const videosJson = result.data
341 const totalVideos = result.total
342 const videos: Video[] = []
344 for (const videoJson of videosJson) {
345 videos.push(new Video(videoJson, translations))
348 return { total: totalVideos, data: videos }
353 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
354 const newPrivacies = privacies.slice()
356 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
357 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
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')
362 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
363 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
368 private setVideoRate (id: number, rateType: UserVideoRateType) {
369 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
370 const body: UserVideoRateUpdate = {
377 map(this.restExtractor.extractDataBool),
378 catchError(err => this.restExtractor.handleError(err))