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 { ComponentPaginationLight } 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, AuthService } 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 { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
35 import { FfprobeData } from 'fluent-ffmpeg'
37 export interface VideosProvider {
38 getVideos (parameters: {
39 videoPagination: ComponentPaginationLight,
42 categoryOneOf?: number[],
43 languageOneOf?: string[]
44 nsfwPolicy: NSFWPolicyType
45 }): Observable<ResultList<Video>>
49 export class VideoService implements VideosProvider {
50 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
51 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
54 private authHttp: HttpClient,
55 private authService: AuthService,
56 private userService: UserService,
57 private restExtractor: RestExtractor,
58 private restService: RestService,
59 private serverService: ServerService,
63 getVideoViewUrl (uuid: string) {
64 return VideoService.BASE_VIDEO_URL + uuid + '/views'
67 getUserWatchingVideoUrl (uuid: string) {
68 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
71 getVideo (options: { videoId: string }): Observable<VideoDetails> {
72 return this.serverService.getServerLocale()
74 switchMap(translations => {
75 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
76 .pipe(map(videoHash => ({ videoHash, translations })))
78 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
79 catchError(err => this.restExtractor.handleError(err))
83 updateVideo (video: VideoEdit) {
84 const language = video.language || null
85 const licence = video.licence || null
86 const category = video.category || null
87 const description = video.description || null
88 const support = video.support || null
89 const scheduleUpdate = video.scheduleUpdate || null
90 const originallyPublishedAt = video.originallyPublishedAt || null
92 const body: VideoUpdate = {
99 channelId: video.channelId,
100 privacy: video.privacy,
103 waitTranscoding: video.waitTranscoding,
104 commentsEnabled: video.commentsEnabled,
105 downloadEnabled: video.downloadEnabled,
106 thumbnailfile: video.thumbnailfile,
107 previewfile: video.previewfile,
109 originallyPublishedAt
112 const data = objectToFormData(body)
114 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
116 map(this.restExtractor.extractDataBool),
117 catchError(err => this.restExtractor.handleError(err))
121 uploadVideo (video: FormData) {
122 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
125 .request<{ video: { id: number, uuid: string } }>(req)
126 .pipe(catchError(err => this.restExtractor.handleError(err)))
129 getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
130 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
132 let params = new HttpParams()
133 params = this.restService.addRestGetParams(params, pagination, sort)
134 params = this.restService.addObjectParams(params, { search })
137 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
139 switchMap(res => this.extractVideos(res)),
140 catchError(err => this.restExtractor.handleError(err))
146 videoPagination: ComponentPaginationLight,
148 ): Observable<ResultList<Video>> {
149 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
151 let params = new HttpParams()
152 params = this.restService.addRestGetParams(params, pagination, sort)
155 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
157 switchMap(res => this.extractVideos(res)),
158 catchError(err => this.restExtractor.handleError(err))
162 getVideoChannelVideos (
163 videoChannel: VideoChannel,
164 videoPagination: ComponentPaginationLight,
165 sort: VideoSortField,
166 nsfwPolicy?: NSFWPolicyType
167 ): Observable<ResultList<Video>> {
168 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
170 let params = new HttpParams()
171 params = this.restService.addRestGetParams(params, pagination, sort)
174 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
178 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
180 switchMap(res => this.extractVideos(res)),
181 catchError(err => this.restExtractor.handleError(err))
185 getUserSubscriptionVideos (parameters: {
186 videoPagination: ComponentPaginationLight,
187 sort: VideoSortField,
189 }): Observable<ResultList<Video>> {
190 const { videoPagination, sort, skipCount } = parameters
191 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
193 let params = new HttpParams()
194 params = this.restService.addRestGetParams(params, pagination, sort)
196 if (skipCount) params = params.set('skipCount', skipCount + '')
199 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
201 switchMap(res => this.extractVideos(res)),
202 catchError(err => this.restExtractor.handleError(err))
206 getVideos (parameters: {
207 videoPagination: ComponentPaginationLight,
208 sort: VideoSortField,
209 filter?: VideoFilter,
210 categoryOneOf?: number[],
211 languageOneOf?: string[],
213 nsfwPolicy?: NSFWPolicyType
214 }): Observable<ResultList<Video>> {
215 const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
217 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
219 let params = new HttpParams()
220 params = this.restService.addRestGetParams(params, pagination, sort)
222 if (filter) params = params.set('filter', filter)
223 if (skipCount) params = params.set('skipCount', skipCount + '')
226 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
230 for (const l of languageOneOf) {
231 params = params.append('languageOneOf[]', l)
236 for (const c of categoryOneOf) {
237 params = params.append('categoryOneOf[]', c + '')
242 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
244 switchMap(res => this.extractVideos(res)),
245 catchError(err => this.restExtractor.handleError(err))
249 buildBaseFeedUrls (params: HttpParams) {
252 format: FeedFormat.RSS,
254 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
257 format: FeedFormat.ATOM,
259 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
262 format: FeedFormat.JSON,
264 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
268 if (params && params.keys().length !== 0) {
269 for (const feed of feeds) {
270 feed.url += '?' + params.toString()
277 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
278 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
280 if (filter) params = params.set('filter', filter)
283 for (const c of categoryOneOf) {
284 params = params.append('categoryOneOf[]', c + '')
288 return this.buildBaseFeedUrls(params)
291 getAccountFeedUrls (accountId: number) {
292 let params = this.restService.addRestGetParams(new HttpParams())
293 params = params.set('accountId', accountId.toString())
295 return this.buildBaseFeedUrls(params)
298 getVideoChannelFeedUrls (videoChannelId: number) {
299 let params = this.restService.addRestGetParams(new HttpParams())
300 params = params.set('videoChannelId', videoChannelId.toString())
302 return this.buildBaseFeedUrls(params)
305 getVideoFileMetadata (metadataUrl: string) {
307 .get<FfprobeData>(metadataUrl)
309 catchError(err => this.restExtractor.handleError(err))
313 removeVideo (id: number) {
315 .delete(VideoService.BASE_VIDEO_URL + id)
317 map(this.restExtractor.extractDataBool),
318 catchError(err => this.restExtractor.handleError(err))
322 loadCompleteDescription (descriptionPath: string) {
324 .get<{ description: string }>(environment.apiUrl + descriptionPath)
326 map(res => res.description),
327 catchError(err => this.restExtractor.handleError(err))
331 setVideoLike (id: number) {
332 return this.setVideoRate(id, 'like')
335 setVideoDislike (id: number) {
336 return this.setVideoRate(id, 'dislike')
339 unsetVideoLike (id: number) {
340 return this.setVideoRate(id, 'none')
343 getUserVideoRating (id: number) {
344 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
346 return this.authHttp.get<UserVideoRate>(url)
347 .pipe(catchError(err => this.restExtractor.handleError(err)))
350 extractVideos (result: ResultList<VideoServerModel>) {
351 return this.serverService.getServerLocale()
353 map(translations => {
354 const videosJson = result.data
355 const totalVideos = result.total
356 const videos: Video[] = []
358 for (const videoJson of videosJson) {
359 videos.push(new Video(videoJson, translations))
362 return { total: totalVideos, data: videos }
367 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
370 id: VideoPrivacy.PRIVATE,
371 label: this.i18n('Only I can see this video')
374 id: VideoPrivacy.UNLISTED,
375 label: this.i18n('Only people with the private link can see this video')
378 id: VideoPrivacy.PUBLIC,
379 label: this.i18n('Anyone can see this video')
382 id: VideoPrivacy.INTERNAL,
383 label: this.i18n('Only users of this instance can see this video')
387 return base.filter(o => !!privacies.find(p => p.id === o.id))
390 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
391 return nsfwPolicy === 'do_not_list'
396 private setVideoRate (id: number, rateType: UserVideoRateType) {
397 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
398 const body: UserVideoRateUpdate = {
405 map(this.restExtractor.extractDataBool),
406 catchError(err => this.restExtractor.handleError(err))