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'
7 import { UserVideoRate, UserVideoRateUpdate, VideoFilter, VideoRateType, VideoUpdate } from '../../../../../shared/models/videos'
8 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
9 import { environment } from '../../../environments/environment'
10 import { ComponentPagination } from '../rest/component-pagination.model'
11 import { RestExtractor } from '../rest/rest-extractor.service'
12 import { RestService } from '../rest/rest.service'
13 import { UserService } from '../users/user.service'
14 import { VideoSortField } from './sort-field.type'
15 import { VideoDetails } from './video-details.model'
16 import { VideoEdit } from './video-edit.model'
17 import { Video } from './video.model'
18 import { objectToFormData } from '@app/shared/misc/utils'
19 import { Account } from '@app/shared/account/account.model'
20 import { AccountService } from '@app/shared/account/account.service'
21 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
22 import { ServerService } from '@app/core'
23 import { UserSubscriptionService } from '@app/shared/user-subscription'
24 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
27 export class VideoService {
28 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
29 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
32 private authHttp: HttpClient,
33 private restExtractor: RestExtractor,
34 private restService: RestService,
35 private serverService: ServerService
38 getVideoViewUrl (uuid: string) {
39 return VideoService.BASE_VIDEO_URL + uuid + '/views'
42 getVideo (uuid: string): Observable<VideoDetails> {
43 return this.serverService.localeObservable
45 switchMap(translations => {
46 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
47 .pipe(map(videoHash => ({ videoHash, translations })))
49 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
50 catchError(err => this.restExtractor.handleError(err))
54 updateVideo (video: VideoEdit) {
55 const language = video.language || null
56 const licence = video.licence || null
57 const category = video.category || null
58 const description = video.description || null
59 const support = video.support || null
60 const scheduleUpdate = video.scheduleUpdate || null
62 const body: VideoUpdate = {
69 channelId: video.channelId,
70 privacy: video.privacy,
73 waitTranscoding: video.waitTranscoding,
74 commentsEnabled: video.commentsEnabled,
75 thumbnailfile: video.thumbnailfile,
76 previewfile: video.previewfile,
80 const data = objectToFormData(body)
82 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
84 map(this.restExtractor.extractDataBool),
85 catchError(err => this.restExtractor.handleError(err))
89 uploadVideo (video: FormData) {
90 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
93 .request<{ video: { id: number, uuid: string } }>(req)
94 .pipe(catchError(err => this.restExtractor.handleError(err)))
97 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
98 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
100 let params = new HttpParams()
101 params = this.restService.addRestGetParams(params, pagination, sort)
104 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
106 switchMap(res => this.extractVideos(res)),
107 catchError(err => this.restExtractor.handleError(err))
113 videoPagination: ComponentPagination,
115 ): Observable<{ videos: Video[], totalVideos: number }> {
116 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
118 let params = new HttpParams()
119 params = this.restService.addRestGetParams(params, pagination, sort)
122 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
124 switchMap(res => this.extractVideos(res)),
125 catchError(err => this.restExtractor.handleError(err))
129 getVideoChannelVideos (
130 videoChannel: VideoChannel,
131 videoPagination: ComponentPagination,
133 ): Observable<{ videos: Video[], totalVideos: number }> {
134 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
136 let params = new HttpParams()
137 params = this.restService.addRestGetParams(params, pagination, sort)
140 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
142 switchMap(res => this.extractVideos(res)),
143 catchError(err => this.restExtractor.handleError(err))
147 getUserSubscriptionVideos (
148 videoPagination: ComponentPagination,
150 ): Observable<{ videos: Video[], totalVideos: number }> {
151 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
153 let params = new HttpParams()
154 params = this.restService.addRestGetParams(params, pagination, sort)
157 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
159 switchMap(res => this.extractVideos(res)),
160 catchError(err => this.restExtractor.handleError(err))
165 videoPagination: ComponentPagination,
166 sort: VideoSortField,
167 filter?: VideoFilter,
168 categoryOneOf?: number
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 params = params.set('filter', filter)
180 params = params.set('categoryOneOf', categoryOneOf + '')
184 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
186 switchMap(res => this.extractVideos(res)),
187 catchError(err => this.restExtractor.handleError(err))
191 buildBaseFeedUrls (params: HttpParams) {
195 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
199 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
203 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
207 if (params && params.keys().length !== 0) {
208 for (const feed of feeds) {
209 feed.url += '?' + params.toString()
216 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
217 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
219 if (filter) params = params.set('filter', filter)
221 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
223 return this.buildBaseFeedUrls(params)
226 getAccountFeedUrls (accountId: number) {
227 let params = this.restService.addRestGetParams(new HttpParams())
228 params = params.set('accountId', accountId.toString())
230 return this.buildBaseFeedUrls(params)
233 getVideoChannelFeedUrls (videoChannelId: number) {
234 let params = this.restService.addRestGetParams(new HttpParams())
235 params = params.set('videoChannelId', videoChannelId.toString())
237 return this.buildBaseFeedUrls(params)
240 removeVideo (id: number) {
242 .delete(VideoService.BASE_VIDEO_URL + id)
244 map(this.restExtractor.extractDataBool),
245 catchError(err => this.restExtractor.handleError(err))
249 loadCompleteDescription (descriptionPath: string) {
251 .get(environment.apiUrl + descriptionPath)
253 map(res => res[ 'description' ]),
254 catchError(err => this.restExtractor.handleError(err))
258 setVideoLike (id: number) {
259 return this.setVideoRate(id, 'like')
262 setVideoDislike (id: number) {
263 return this.setVideoRate(id, 'dislike')
266 unsetVideoLike (id: number) {
267 return this.setVideoRate(id, 'none')
270 getUserVideoRating (id: number) {
271 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
273 return this.authHttp.get<UserVideoRate>(url)
274 .pipe(catchError(err => this.restExtractor.handleError(err)))
277 extractVideos (result: ResultList<VideoServerModel>) {
278 return this.serverService.localeObservable
280 map(translations => {
281 const videosJson = result.data
282 const totalVideos = result.total
283 const videos: Video[] = []
285 for (const videoJson of videosJson) {
286 videos.push(new Video(videoJson, translations))
289 return { videos, totalVideos }
294 private setVideoRate (id: number, rateType: VideoRateType) {
295 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
296 const body: UserVideoRateUpdate = {
303 map(this.restExtractor.extractDataBool),
304 catchError(err => this.restExtractor.handleError(err))