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 {
39 videoPagination: ComponentPagination,
42 categoryOneOf?: number
43 ): Observable<{ videos: Video[], totalVideos: number }>
47 export class VideoService implements VideosProvider {
48 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
49 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
52 private authHttp: HttpClient,
53 private restExtractor: RestExtractor,
54 private restService: RestService,
55 private serverService: ServerService,
59 getVideoViewUrl (uuid: string) {
60 return VideoService.BASE_VIDEO_URL + uuid + '/views'
63 getUserWatchingVideoUrl (uuid: string) {
64 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
67 getVideo (uuid: string): Observable<VideoDetails> {
68 return this.serverService.localeObservable
70 switchMap(translations => {
71 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
72 .pipe(map(videoHash => ({ videoHash, translations })))
74 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
75 catchError(err => this.restExtractor.handleError(err))
79 updateVideo (video: VideoEdit) {
80 const language = video.language || null
81 const licence = video.licence || null
82 const category = video.category || null
83 const description = video.description || null
84 const support = video.support || null
85 const scheduleUpdate = video.scheduleUpdate || null
86 const originallyPublishedAt = video.originallyPublishedAt || null
88 const body: VideoUpdate = {
95 channelId: video.channelId,
96 privacy: video.privacy,
99 waitTranscoding: video.waitTranscoding,
100 commentsEnabled: video.commentsEnabled,
101 downloadEnabled: video.downloadEnabled,
102 thumbnailfile: video.thumbnailfile,
103 previewfile: video.previewfile,
105 originallyPublishedAt
108 const data = objectToFormData(body)
110 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
112 map(this.restExtractor.extractDataBool),
113 catchError(err => this.restExtractor.handleError(err))
117 uploadVideo (video: FormData) {
118 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
121 .request<{ video: { id: number, uuid: string } }>(req)
122 .pipe(catchError(err => this.restExtractor.handleError(err)))
125 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
126 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
128 let params = new HttpParams()
129 params = this.restService.addRestGetParams(params, pagination, sort)
132 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
134 switchMap(res => this.extractVideos(res)),
135 catchError(err => this.restExtractor.handleError(err))
141 videoPagination: ComponentPagination,
143 ): Observable<{ videos: Video[], totalVideos: number }> {
144 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
146 let params = new HttpParams()
147 params = this.restService.addRestGetParams(params, pagination, sort)
150 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
152 switchMap(res => this.extractVideos(res)),
153 catchError(err => this.restExtractor.handleError(err))
157 getVideoChannelVideos (
158 videoChannel: VideoChannel,
159 videoPagination: ComponentPagination,
161 ): Observable<{ videos: Video[], totalVideos: number }> {
162 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
164 let params = new HttpParams()
165 params = this.restService.addRestGetParams(params, pagination, sort)
168 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
170 switchMap(res => this.extractVideos(res)),
171 catchError(err => this.restExtractor.handleError(err))
176 videoPlaylistId: number | string,
177 videoPagination: ComponentPagination
178 ): Observable<{ videos: Video[], totalVideos: number }> {
179 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
181 let params = new HttpParams()
182 params = this.restService.addRestGetParams(params, pagination)
185 .get<ResultList<Video>>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos', { params })
187 switchMap(res => this.extractVideos(res)),
188 catchError(err => this.restExtractor.handleError(err))
192 getUserSubscriptionVideos (
193 videoPagination: ComponentPagination,
195 ): Observable<{ videos: Video[], totalVideos: number }> {
196 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
198 let params = new HttpParams()
199 params = this.restService.addRestGetParams(params, pagination, sort)
202 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
204 switchMap(res => this.extractVideos(res)),
205 catchError(err => this.restExtractor.handleError(err))
210 videoPagination: ComponentPagination,
211 sort: VideoSortField,
212 filter?: VideoFilter,
213 categoryOneOf?: number
214 ): Observable<{ videos: Video[], totalVideos: number }> {
215 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
217 let params = new HttpParams()
218 params = this.restService.addRestGetParams(params, pagination, sort)
221 params = params.set('filter', filter)
225 params = params.set('categoryOneOf', categoryOneOf + '')
229 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
231 switchMap(res => this.extractVideos(res)),
232 catchError(err => this.restExtractor.handleError(err))
236 buildBaseFeedUrls (params: HttpParams) {
239 format: FeedFormat.RSS,
241 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
244 format: FeedFormat.ATOM,
246 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
249 format: FeedFormat.JSON,
251 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
255 if (params && params.keys().length !== 0) {
256 for (const feed of feeds) {
257 feed.url += '?' + params.toString()
264 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
265 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
267 if (filter) params = params.set('filter', filter)
269 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
271 return this.buildBaseFeedUrls(params)
274 getAccountFeedUrls (accountId: number) {
275 let params = this.restService.addRestGetParams(new HttpParams())
276 params = params.set('accountId', accountId.toString())
278 return this.buildBaseFeedUrls(params)
281 getVideoChannelFeedUrls (videoChannelId: number) {
282 let params = this.restService.addRestGetParams(new HttpParams())
283 params = params.set('videoChannelId', videoChannelId.toString())
285 return this.buildBaseFeedUrls(params)
288 removeVideo (id: number) {
290 .delete(VideoService.BASE_VIDEO_URL + id)
292 map(this.restExtractor.extractDataBool),
293 catchError(err => this.restExtractor.handleError(err))
297 loadCompleteDescription (descriptionPath: string) {
299 .get<{ description: string }>(environment.apiUrl + descriptionPath)
301 map(res => res.description),
302 catchError(err => this.restExtractor.handleError(err))
306 setVideoLike (id: number) {
307 return this.setVideoRate(id, 'like')
310 setVideoDislike (id: number) {
311 return this.setVideoRate(id, 'dislike')
314 unsetVideoLike (id: number) {
315 return this.setVideoRate(id, 'none')
318 getUserVideoRating (id: number) {
319 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
321 return this.authHttp.get<UserVideoRate>(url)
322 .pipe(catchError(err => this.restExtractor.handleError(err)))
325 extractVideos (result: ResultList<VideoServerModel>) {
326 return this.serverService.localeObservable
328 map(translations => {
329 const videosJson = result.data
330 const totalVideos = result.total
331 const videos: Video[] = []
333 for (const videoJson of videosJson) {
334 videos.push(new Video(videoJson, translations))
337 return { videos, totalVideos }
342 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
343 const newPrivacies = privacies.slice()
345 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
346 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
348 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
349 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
351 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
352 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
357 private setVideoRate (id: number, rateType: UserVideoRateType) {
358 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
359 const body: UserVideoRateUpdate = {
366 map(this.restExtractor.extractDataBool),
367 catchError(err => this.restExtractor.handleError(err))