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 {
37 videoPagination: ComponentPagination,
40 categoryOneOf?: number
41 ): Observable<{ videos: Video[], totalVideos: number }>
45 export class VideoService implements VideosProvider {
46 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
47 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
50 private authHttp: HttpClient,
51 private restExtractor: RestExtractor,
52 private restService: RestService,
53 private serverService: ServerService,
57 getVideoViewUrl (uuid: string) {
58 return VideoService.BASE_VIDEO_URL + uuid + '/views'
61 getVideo (uuid: string): Observable<VideoDetails> {
62 return this.serverService.localeObservable
64 switchMap(translations => {
65 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
66 .pipe(map(videoHash => ({ videoHash, translations })))
68 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
69 catchError(err => this.restExtractor.handleError(err))
73 updateVideo (video: VideoEdit) {
74 const language = video.language || null
75 const licence = video.licence || null
76 const category = video.category || null
77 const description = video.description || null
78 const support = video.support || null
79 const scheduleUpdate = video.scheduleUpdate || null
81 const body: VideoUpdate = {
88 channelId: video.channelId,
89 privacy: video.privacy,
92 waitTranscoding: video.waitTranscoding,
93 commentsEnabled: video.commentsEnabled,
94 thumbnailfile: video.thumbnailfile,
95 previewfile: video.previewfile,
99 const data = objectToFormData(body)
101 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
103 map(this.restExtractor.extractDataBool),
104 catchError(err => this.restExtractor.handleError(err))
108 uploadVideo (video: FormData) {
109 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
112 .request<{ video: { id: number, uuid: string } }>(req)
113 .pipe(catchError(err => this.restExtractor.handleError(err)))
116 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
117 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
119 let params = new HttpParams()
120 params = this.restService.addRestGetParams(params, pagination, sort)
123 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
125 switchMap(res => this.extractVideos(res)),
126 catchError(err => this.restExtractor.handleError(err))
132 videoPagination: ComponentPagination,
134 ): Observable<{ videos: Video[], totalVideos: number }> {
135 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
137 let params = new HttpParams()
138 params = this.restService.addRestGetParams(params, pagination, sort)
141 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
143 switchMap(res => this.extractVideos(res)),
144 catchError(err => this.restExtractor.handleError(err))
148 getVideoChannelVideos (
149 videoChannel: VideoChannel,
150 videoPagination: ComponentPagination,
152 ): Observable<{ videos: Video[], totalVideos: number }> {
153 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
155 let params = new HttpParams()
156 params = this.restService.addRestGetParams(params, pagination, sort)
159 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
161 switchMap(res => this.extractVideos(res)),
162 catchError(err => this.restExtractor.handleError(err))
166 getUserSubscriptionVideos (
167 videoPagination: ComponentPagination,
169 ): Observable<{ videos: Video[], totalVideos: number }> {
170 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
172 let params = new HttpParams()
173 params = this.restService.addRestGetParams(params, pagination, sort)
176 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
178 switchMap(res => this.extractVideos(res)),
179 catchError(err => this.restExtractor.handleError(err))
184 videoPagination: ComponentPagination,
185 sort: VideoSortField,
186 filter?: VideoFilter,
187 categoryOneOf?: number
188 ): Observable<{ videos: Video[], totalVideos: number }> {
189 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
191 let params = new HttpParams()
192 params = this.restService.addRestGetParams(params, pagination, sort)
195 params = params.set('filter', filter)
199 params = params.set('categoryOneOf', categoryOneOf + '')
203 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
205 switchMap(res => this.extractVideos(res)),
206 catchError(err => this.restExtractor.handleError(err))
210 buildBaseFeedUrls (params: HttpParams) {
213 format: FeedFormat.RSS,
215 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
218 format: FeedFormat.ATOM,
220 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
223 format: FeedFormat.JSON,
225 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
229 if (params && params.keys().length !== 0) {
230 for (const feed of feeds) {
231 feed.url += '?' + params.toString()
238 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
239 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
241 if (filter) params = params.set('filter', filter)
243 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
245 return this.buildBaseFeedUrls(params)
248 getAccountFeedUrls (accountId: number) {
249 let params = this.restService.addRestGetParams(new HttpParams())
250 params = params.set('accountId', accountId.toString())
252 return this.buildBaseFeedUrls(params)
255 getVideoChannelFeedUrls (videoChannelId: number) {
256 let params = this.restService.addRestGetParams(new HttpParams())
257 params = params.set('videoChannelId', videoChannelId.toString())
259 return this.buildBaseFeedUrls(params)
262 removeVideo (id: number) {
264 .delete(VideoService.BASE_VIDEO_URL + id)
266 map(this.restExtractor.extractDataBool),
267 catchError(err => this.restExtractor.handleError(err))
271 loadCompleteDescription (descriptionPath: string) {
273 .get(environment.apiUrl + descriptionPath)
275 map(res => res[ 'description' ]),
276 catchError(err => this.restExtractor.handleError(err))
280 setVideoLike (id: number) {
281 return this.setVideoRate(id, 'like')
284 setVideoDislike (id: number) {
285 return this.setVideoRate(id, 'dislike')
288 unsetVideoLike (id: number) {
289 return this.setVideoRate(id, 'none')
292 getUserVideoRating (id: number) {
293 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
295 return this.authHttp.get<UserVideoRate>(url)
296 .pipe(catchError(err => this.restExtractor.handleError(err)))
299 extractVideos (result: ResultList<VideoServerModel>) {
300 return this.serverService.localeObservable
302 map(translations => {
303 const videosJson = result.data
304 const totalVideos = result.total
305 const videos: Video[] = []
307 for (const videoJson of videosJson) {
308 videos.push(new Video(videoJson, translations))
311 return { videos, totalVideos }
316 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
317 const newPrivacies = privacies.slice()
319 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
320 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
322 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
323 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
325 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
326 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
331 private setVideoRate (id: number, rateType: VideoRateType) {
332 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
333 const body: UserVideoRateUpdate = {
340 map(this.restExtractor.extractDataBool),
341 catchError(err => this.restExtractor.handleError(err))