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<{ videos: Video[], totalVideos: number }>
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 (uuid: string): Observable<VideoDetails> {
69 return this.serverService.localeObservable
71 switchMap(translations => {
72 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
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<{ videos: Video[], totalVideos: number }> {
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<{ videos: Video[], totalVideos: number }> {
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<{ videos: Video[], totalVideos: number }> {
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<{ videos: Video[], totalVideos: number }> {
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 (
194 videoPagination: ComponentPagination,
196 ): Observable<{ videos: Video[], totalVideos: number }> {
197 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
199 let params = new HttpParams()
200 params = this.restService.addRestGetParams(params, pagination, sort)
203 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
205 switchMap(res => this.extractVideos(res)),
206 catchError(err => this.restExtractor.handleError(err))
210 getVideos (parameters: {
211 videoPagination: ComponentPagination,
212 sort: VideoSortField,
213 filter?: VideoFilter,
214 categoryOneOf?: number,
215 languageOneOf?: string[]
216 }): Observable<{ videos: Video[], totalVideos: number }> {
217 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
219 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
221 let params = new HttpParams()
222 params = this.restService.addRestGetParams(params, pagination, sort)
225 params = params.set('filter', filter)
229 params = params.set('categoryOneOf', categoryOneOf + '')
233 for (const l of languageOneOf) {
234 params = params.append('languageOneOf[]', l)
239 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
241 switchMap(res => this.extractVideos(res)),
242 catchError(err => this.restExtractor.handleError(err))
246 buildBaseFeedUrls (params: HttpParams) {
249 format: FeedFormat.RSS,
251 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
254 format: FeedFormat.ATOM,
256 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
259 format: FeedFormat.JSON,
261 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
265 if (params && params.keys().length !== 0) {
266 for (const feed of feeds) {
267 feed.url += '?' + params.toString()
274 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
275 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
277 if (filter) params = params.set('filter', filter)
279 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
281 return this.buildBaseFeedUrls(params)
284 getAccountFeedUrls (accountId: number) {
285 let params = this.restService.addRestGetParams(new HttpParams())
286 params = params.set('accountId', accountId.toString())
288 return this.buildBaseFeedUrls(params)
291 getVideoChannelFeedUrls (videoChannelId: number) {
292 let params = this.restService.addRestGetParams(new HttpParams())
293 params = params.set('videoChannelId', videoChannelId.toString())
295 return this.buildBaseFeedUrls(params)
298 removeVideo (id: number) {
300 .delete(VideoService.BASE_VIDEO_URL + id)
302 map(this.restExtractor.extractDataBool),
303 catchError(err => this.restExtractor.handleError(err))
307 loadCompleteDescription (descriptionPath: string) {
309 .get<{ description: string }>(environment.apiUrl + descriptionPath)
311 map(res => res.description),
312 catchError(err => this.restExtractor.handleError(err))
316 setVideoLike (id: number) {
317 return this.setVideoRate(id, 'like')
320 setVideoDislike (id: number) {
321 return this.setVideoRate(id, 'dislike')
324 unsetVideoLike (id: number) {
325 return this.setVideoRate(id, 'none')
328 getUserVideoRating (id: number) {
329 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
331 return this.authHttp.get<UserVideoRate>(url)
332 .pipe(catchError(err => this.restExtractor.handleError(err)))
335 extractVideos (result: ResultList<VideoServerModel>) {
336 return this.serverService.localeObservable
338 map(translations => {
339 const videosJson = result.data
340 const totalVideos = result.total
341 const videos: Video[] = []
343 for (const videoJson of videosJson) {
344 videos.push(new Video(videoJson, translations))
347 return { videos, totalVideos }
352 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
353 const newPrivacies = privacies.slice()
355 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
356 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
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')
361 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
362 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
367 private setVideoRate (id: number, rateType: UserVideoRateType) {
368 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
369 const body: UserVideoRateUpdate = {
376 map(this.restExtractor.extractDataBool),
377 catchError(err => this.restExtractor.handleError(err))