1 import { FfprobeData } from 'fluent-ffmpeg'
2 import { Observable } from 'rxjs'
3 import { catchError, map, switchMap } from 'rxjs/operators'
4 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core'
7 import { objectToFormData } from '@app/helpers'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
16 Video as VideoServerModel,
18 VideoDetails as VideoDetailsServerModel,
23 } from '@shared/models'
24 import { environment } from '../../../../environments/environment'
25 import { Account, AccountService } from '../account'
26 import { VideoChannel, VideoChannelService } from '../video-channel'
27 import { VideoDetails } from './video-details.model'
28 import { VideoEdit } from './video-edit.model'
29 import { Video } from './video.model'
31 export interface VideosProvider {
32 getVideos (parameters: {
33 videoPagination: ComponentPaginationLight,
36 categoryOneOf?: number[],
37 languageOneOf?: string[]
38 nsfwPolicy: NSFWPolicyType
39 }): Observable<ResultList<Video>>
43 export class VideoService implements VideosProvider {
44 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
45 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
48 private authHttp: HttpClient,
49 private restExtractor: RestExtractor,
50 private restService: RestService,
51 private serverService: ServerService,
55 getVideoViewUrl (uuid: string) {
56 return VideoService.BASE_VIDEO_URL + uuid + '/views'
59 getUserWatchingVideoUrl (uuid: string) {
60 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
63 getVideo (options: { videoId: string }): Observable<VideoDetails> {
64 return this.serverService.getServerLocale()
66 switchMap(translations => {
67 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
68 .pipe(map(videoHash => ({ videoHash, translations })))
70 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
71 catchError(err => this.restExtractor.handleError(err))
75 updateVideo (video: VideoEdit) {
76 const language = video.language || null
77 const licence = video.licence || null
78 const category = video.category || null
79 const description = video.description || null
80 const support = video.support || null
81 const scheduleUpdate = video.scheduleUpdate || null
82 const originallyPublishedAt = video.originallyPublishedAt || null
84 const body: VideoUpdate = {
91 channelId: video.channelId,
92 privacy: video.privacy,
95 waitTranscoding: video.waitTranscoding,
96 commentsEnabled: video.commentsEnabled,
97 downloadEnabled: video.downloadEnabled,
98 thumbnailfile: video.thumbnailfile,
99 previewfile: video.previewfile,
101 originallyPublishedAt
104 const data = objectToFormData(body)
106 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
108 map(this.restExtractor.extractDataBool),
109 catchError(err => this.restExtractor.handleError(err))
113 uploadVideo (video: FormData) {
114 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
117 .request<{ video: { id: number, uuid: string } }>(req)
118 .pipe(catchError(err => this.restExtractor.handleError(err)))
121 getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
122 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
124 let params = new HttpParams()
125 params = this.restService.addRestGetParams(params, pagination, sort)
126 params = this.restService.addObjectParams(params, { search })
129 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
131 switchMap(res => this.extractVideos(res)),
132 catchError(err => this.restExtractor.handleError(err))
138 videoPagination: ComponentPaginationLight,
140 ): Observable<ResultList<Video>> {
141 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
143 let params = new HttpParams()
144 params = this.restService.addRestGetParams(params, pagination, sort)
147 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
149 switchMap(res => this.extractVideos(res)),
150 catchError(err => this.restExtractor.handleError(err))
154 getVideoChannelVideos (
155 videoChannel: VideoChannel,
156 videoPagination: ComponentPaginationLight,
157 sort: VideoSortField,
158 nsfwPolicy?: NSFWPolicyType
159 ): Observable<ResultList<Video>> {
160 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
162 let params = new HttpParams()
163 params = this.restService.addRestGetParams(params, pagination, sort)
166 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
170 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
172 switchMap(res => this.extractVideos(res)),
173 catchError(err => this.restExtractor.handleError(err))
177 getVideos (parameters: {
178 videoPagination: ComponentPaginationLight,
179 sort: VideoSortField,
180 filter?: VideoFilter,
181 categoryOneOf?: number[],
182 languageOneOf?: string[],
184 nsfwPolicy?: NSFWPolicyType
185 }): Observable<ResultList<Video>> {
186 const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
188 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
190 let params = new HttpParams()
191 params = this.restService.addRestGetParams(params, pagination, sort)
193 if (filter) params = params.set('filter', filter)
194 if (skipCount) params = params.set('skipCount', skipCount + '')
197 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
201 for (const l of languageOneOf) {
202 params = params.append('languageOneOf[]', l)
207 for (const c of categoryOneOf) {
208 params = params.append('categoryOneOf[]', c + '')
213 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
215 switchMap(res => this.extractVideos(res)),
216 catchError(err => this.restExtractor.handleError(err))
220 buildBaseFeedUrls (params: HttpParams) {
223 format: FeedFormat.RSS,
224 label: 'media rss 2.0',
225 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
228 format: FeedFormat.ATOM,
230 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
233 format: FeedFormat.JSON,
235 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
239 if (params && params.keys().length !== 0) {
240 for (const feed of feeds) {
241 feed.url += '?' + params.toString()
248 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
249 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
251 if (filter) params = params.set('filter', filter)
254 for (const c of categoryOneOf) {
255 params = params.append('categoryOneOf[]', c + '')
259 return this.buildBaseFeedUrls(params)
262 getAccountFeedUrls (accountId: number) {
263 let params = this.restService.addRestGetParams(new HttpParams())
264 params = params.set('accountId', accountId.toString())
266 return this.buildBaseFeedUrls(params)
269 getVideoChannelFeedUrls (videoChannelId: number) {
270 let params = this.restService.addRestGetParams(new HttpParams())
271 params = params.set('videoChannelId', videoChannelId.toString())
273 return this.buildBaseFeedUrls(params)
276 getVideoFileMetadata (metadataUrl: string) {
278 .get<FfprobeData>(metadataUrl)
280 catchError(err => this.restExtractor.handleError(err))
284 removeVideo (id: number) {
286 .delete(VideoService.BASE_VIDEO_URL + id)
288 map(this.restExtractor.extractDataBool),
289 catchError(err => this.restExtractor.handleError(err))
293 loadCompleteDescription (descriptionPath: string) {
295 .get<{ description: string }>(environment.apiUrl + descriptionPath)
297 map(res => res.description),
298 catchError(err => this.restExtractor.handleError(err))
302 setVideoLike (id: number) {
303 return this.setVideoRate(id, 'like')
306 setVideoDislike (id: number) {
307 return this.setVideoRate(id, 'dislike')
310 unsetVideoLike (id: number) {
311 return this.setVideoRate(id, 'none')
314 getUserVideoRating (id: number) {
315 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
317 return this.authHttp.get<UserVideoRate>(url)
318 .pipe(catchError(err => this.restExtractor.handleError(err)))
321 extractVideos (result: ResultList<VideoServerModel>) {
322 return this.serverService.getServerLocale()
324 map(translations => {
325 const videosJson = result.data
326 const totalVideos = result.total
327 const videos: Video[] = []
329 for (const videoJson of videosJson) {
330 videos.push(new Video(videoJson, translations))
333 return { total: totalVideos, data: videos }
338 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
341 id: VideoPrivacy.PRIVATE,
342 label: this.i18n('Only I can see this video')
345 id: VideoPrivacy.UNLISTED,
346 label: this.i18n('Only people with the private link can see this video')
349 id: VideoPrivacy.PUBLIC,
350 label: this.i18n('Anyone can see this video')
353 id: VideoPrivacy.INTERNAL,
354 label: this.i18n('Only users of this instance can see this video')
358 return base.filter(o => !!privacies.find(p => p.id === o.id))
361 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
362 return nsfwPolicy === 'do_not_list'
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))