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'
35 export interface VideosProvider {
36 getVideos (parameters: {
37 videoPagination: ComponentPagination,
40 categoryOneOf?: number,
41 languageOneOf?: string[]
42 }): Observable<ResultList<Video>>
46 export class VideoService implements VideosProvider {
47 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
48 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
51 private authHttp: HttpClient,
52 private restExtractor: RestExtractor,
53 private restService: RestService,
54 private serverService: ServerService,
58 getVideoViewUrl (uuid: string) {
59 return VideoService.BASE_VIDEO_URL + uuid + '/views'
62 getUserWatchingVideoUrl (uuid: string) {
63 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
66 getVideo (options: { videoId: string }): Observable<VideoDetails> {
67 return this.serverService.localeObservable
69 switchMap(translations => {
70 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
71 .pipe(map(videoHash => ({ videoHash, translations })))
73 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
74 catchError(err => this.restExtractor.handleError(err))
78 updateVideo (video: VideoEdit) {
79 const language = video.language || null
80 const licence = video.licence || null
81 const category = video.category || null
82 const description = video.description || null
83 const support = video.support || null
84 const scheduleUpdate = video.scheduleUpdate || null
85 const originallyPublishedAt = video.originallyPublishedAt || null
87 const body: VideoUpdate = {
94 channelId: video.channelId,
95 privacy: video.privacy,
98 waitTranscoding: video.waitTranscoding,
99 commentsEnabled: video.commentsEnabled,
100 downloadEnabled: video.downloadEnabled,
101 thumbnailfile: video.thumbnailfile,
102 previewfile: video.previewfile,
104 originallyPublishedAt
107 const data = objectToFormData(body)
109 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
111 map(this.restExtractor.extractDataBool),
112 catchError(err => this.restExtractor.handleError(err))
116 uploadVideo (video: FormData) {
117 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
120 .request<{ video: { id: number, uuid: string } }>(req)
121 .pipe(catchError(err => this.restExtractor.handleError(err)))
124 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<ResultList<Video>> {
125 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
127 let params = new HttpParams()
128 params = this.restService.addRestGetParams(params, pagination, sort)
131 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
133 switchMap(res => this.extractVideos(res)),
134 catchError(err => this.restExtractor.handleError(err))
140 videoPagination: ComponentPagination,
142 ): Observable<ResultList<Video>> {
143 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
145 let params = new HttpParams()
146 params = this.restService.addRestGetParams(params, pagination, sort)
149 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
151 switchMap(res => this.extractVideos(res)),
152 catchError(err => this.restExtractor.handleError(err))
156 getVideoChannelVideos (
157 videoChannel: VideoChannel,
158 videoPagination: ComponentPagination,
160 ): Observable<ResultList<Video>> {
161 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
163 let params = new HttpParams()
164 params = this.restService.addRestGetParams(params, pagination, sort)
167 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
169 switchMap(res => this.extractVideos(res)),
170 catchError(err => this.restExtractor.handleError(err))
174 getUserSubscriptionVideos (parameters: {
175 videoPagination: ComponentPagination,
177 }): Observable<ResultList<Video>> {
178 const { videoPagination, sort } = parameters
179 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
181 let params = new HttpParams()
182 params = this.restService.addRestGetParams(params, pagination, sort)
185 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
187 switchMap(res => this.extractVideos(res)),
188 catchError(err => this.restExtractor.handleError(err))
192 getVideos (parameters: {
193 videoPagination: ComponentPagination,
194 sort: VideoSortField,
195 filter?: VideoFilter,
196 categoryOneOf?: number,
197 languageOneOf?: string[]
198 }): Observable<ResultList<Video>> {
199 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
201 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
203 let params = new HttpParams()
204 params = this.restService.addRestGetParams(params, pagination, sort)
207 params = params.set('filter', filter)
211 params = params.set('categoryOneOf', categoryOneOf + '')
215 for (const l of languageOneOf) {
216 params = params.append('languageOneOf[]', l)
221 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
223 switchMap(res => this.extractVideos(res)),
224 catchError(err => this.restExtractor.handleError(err))
228 buildBaseFeedUrls (params: HttpParams) {
231 format: FeedFormat.RSS,
233 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
236 format: FeedFormat.ATOM,
238 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
241 format: FeedFormat.JSON,
243 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
247 if (params && params.keys().length !== 0) {
248 for (const feed of feeds) {
249 feed.url += '?' + params.toString()
256 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
257 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
259 if (filter) params = params.set('filter', filter)
261 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
263 return this.buildBaseFeedUrls(params)
266 getAccountFeedUrls (accountId: number) {
267 let params = this.restService.addRestGetParams(new HttpParams())
268 params = params.set('accountId', accountId.toString())
270 return this.buildBaseFeedUrls(params)
273 getVideoChannelFeedUrls (videoChannelId: number) {
274 let params = this.restService.addRestGetParams(new HttpParams())
275 params = params.set('videoChannelId', videoChannelId.toString())
277 return this.buildBaseFeedUrls(params)
280 removeVideo (id: number) {
282 .delete(VideoService.BASE_VIDEO_URL + id)
284 map(this.restExtractor.extractDataBool),
285 catchError(err => this.restExtractor.handleError(err))
289 loadCompleteDescription (descriptionPath: string) {
291 .get<{ description: string }>(environment.apiUrl + descriptionPath)
293 map(res => res.description),
294 catchError(err => this.restExtractor.handleError(err))
298 setVideoLike (id: number) {
299 return this.setVideoRate(id, 'like')
302 setVideoDislike (id: number) {
303 return this.setVideoRate(id, 'dislike')
306 unsetVideoLike (id: number) {
307 return this.setVideoRate(id, 'none')
310 getUserVideoRating (id: number) {
311 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
313 return this.authHttp.get<UserVideoRate>(url)
314 .pipe(catchError(err => this.restExtractor.handleError(err)))
317 extractVideos (result: ResultList<VideoServerModel>) {
318 return this.serverService.localeObservable
320 map(translations => {
321 const videosJson = result.data
322 const totalVideos = result.total
323 const videos: Video[] = []
325 for (const videoJson of videosJson) {
326 videos.push(new Video(videoJson, translations))
329 return { total: totalVideos, data: videos }
334 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
335 const newPrivacies = privacies.slice()
337 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
338 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
340 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
341 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
343 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
344 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
349 private setVideoRate (id: number, rateType: UserVideoRateType) {
350 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
351 const body: UserVideoRateUpdate = {
358 map(this.restExtractor.extractDataBool),
359 catchError(err => this.restExtractor.handleError(err))