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'
14 } from '../../../../../shared/models/videos'
15 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
16 import { environment } from '../../../environments/environment'
17 import { ComponentPagination } from '../rest/component-pagination.model'
18 import { RestExtractor } from '../rest/rest-extractor.service'
19 import { RestService } from '../rest/rest.service'
20 import { UserService } from '../users/user.service'
21 import { VideoSortField } from './sort-field.type'
22 import { VideoDetails } from './video-details.model'
23 import { VideoEdit } from './video-edit.model'
24 import { Video } from './video.model'
25 import { objectToFormData } from '@app/shared/misc/utils'
26 import { Account } from '@app/shared/account/account.model'
27 import { AccountService } from '@app/shared/account/account.service'
28 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
29 import { ServerService } from '@app/core'
30 import { UserSubscriptionService } from '@app/shared/user-subscription'
33 export class VideoService {
34 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
35 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
38 private authHttp: HttpClient,
39 private restExtractor: RestExtractor,
40 private restService: RestService,
41 private serverService: ServerService
44 getVideoViewUrl (uuid: string) {
45 return VideoService.BASE_VIDEO_URL + uuid + '/views'
48 getVideo (uuid: string): Observable<VideoDetails> {
49 return this.serverService.localeObservable
51 switchMap(translations => {
52 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
53 .pipe(map(videoHash => ({ videoHash, translations })))
55 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
56 catchError(err => this.restExtractor.handleError(err))
60 viewVideo (uuid: string): Observable<boolean> {
61 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
63 map(this.restExtractor.extractDataBool),
64 catchError(err => this.restExtractor.handleError(err))
68 updateVideo (video: VideoEdit) {
69 const language = video.language || null
70 const licence = video.licence || null
71 const category = video.category || null
72 const description = video.description || null
73 const support = video.support || null
74 const scheduleUpdate = video.scheduleUpdate || null
76 const body: VideoUpdate = {
83 channelId: video.channelId,
84 privacy: video.privacy,
87 waitTranscoding: video.waitTranscoding,
88 commentsEnabled: video.commentsEnabled,
89 thumbnailfile: video.thumbnailfile,
90 previewfile: video.previewfile,
94 const data = objectToFormData(body)
96 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
98 map(this.restExtractor.extractDataBool),
99 catchError(err => this.restExtractor.handleError(err))
103 uploadVideo (video: FormData) {
104 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
107 .request<{ video: { id: number, uuid: string } }>(req)
108 .pipe(catchError(err => this.restExtractor.handleError(err)))
111 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
112 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
114 let params = new HttpParams()
115 params = this.restService.addRestGetParams(params, pagination, sort)
118 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
120 switchMap(res => this.extractVideos(res)),
121 catchError(err => this.restExtractor.handleError(err))
127 videoPagination: ComponentPagination,
129 ): Observable<{ videos: Video[], totalVideos: number }> {
130 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
132 let params = new HttpParams()
133 params = this.restService.addRestGetParams(params, pagination, sort)
136 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
138 switchMap(res => this.extractVideos(res)),
139 catchError(err => this.restExtractor.handleError(err))
143 getVideoChannelVideos (
144 videoChannel: VideoChannel,
145 videoPagination: ComponentPagination,
147 ): Observable<{ videos: Video[], totalVideos: number }> {
148 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
150 let params = new HttpParams()
151 params = this.restService.addRestGetParams(params, pagination, sort)
154 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.name + '/videos', { params })
156 switchMap(res => this.extractVideos(res)),
157 catchError(err => this.restExtractor.handleError(err))
161 getUserSubscriptionVideos (
162 videoPagination: ComponentPagination,
164 ): Observable<{ videos: Video[], totalVideos: number }> {
165 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
167 let params = new HttpParams()
168 params = this.restService.addRestGetParams(params, pagination, sort)
171 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
173 switchMap(res => this.extractVideos(res)),
174 catchError(err => this.restExtractor.handleError(err))
179 videoPagination: ComponentPagination,
180 sort: VideoSortField,
181 filter?: VideoFilter,
182 categoryOneOf?: number
183 ): Observable<{ videos: Video[], totalVideos: number }> {
184 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
186 let params = new HttpParams()
187 params = this.restService.addRestGetParams(params, pagination, sort)
190 params = params.set('filter', filter)
194 params = params.set('categoryOneOf', categoryOneOf + '')
198 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
200 switchMap(res => this.extractVideos(res)),
201 catchError(err => this.restExtractor.handleError(err))
205 buildBaseFeedUrls (params: HttpParams) {
209 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
213 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
217 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
221 if (params && params.keys().length !== 0) {
222 for (const feed of feeds) {
223 feed.url += '?' + params.toString()
230 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
231 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
233 if (filter) params = params.set('filter', filter)
235 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
237 return this.buildBaseFeedUrls(params)
240 getAccountFeedUrls (accountId: number) {
241 let params = this.restService.addRestGetParams(new HttpParams())
242 params = params.set('accountId', accountId.toString())
244 return this.buildBaseFeedUrls(params)
247 getVideoChannelFeedUrls (videoChannelId: number) {
248 let params = this.restService.addRestGetParams(new HttpParams())
249 params = params.set('videoChannelId', videoChannelId.toString())
251 return this.buildBaseFeedUrls(params)
254 removeVideo (id: number) {
256 .delete(VideoService.BASE_VIDEO_URL + id)
258 map(this.restExtractor.extractDataBool),
259 catchError(err => this.restExtractor.handleError(err))
263 loadCompleteDescription (descriptionPath: string) {
265 .get(environment.apiUrl + descriptionPath)
267 map(res => res[ 'description' ]),
268 catchError(err => this.restExtractor.handleError(err))
272 setVideoLike (id: number) {
273 return this.setVideoRate(id, 'like')
276 setVideoDislike (id: number) {
277 return this.setVideoRate(id, 'dislike')
280 unsetVideoLike (id: number) {
281 return this.setVideoRate(id, 'none')
284 getUserVideoRating (id: number) {
285 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
287 return this.authHttp.get<UserVideoRate>(url)
288 .pipe(catchError(err => this.restExtractor.handleError(err)))
291 extractVideos (result: ResultList<VideoServerModel>) {
292 return this.serverService.localeObservable
294 map(translations => {
295 const videosJson = result.data
296 const totalVideos = result.total
297 const videos: Video[] = []
299 for (const videoJson of videosJson) {
300 videos.push(new Video(videoJson, translations))
303 return { videos, totalVideos }
308 private setVideoRate (id: number, rateType: VideoRateType) {
309 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
310 const body: UserVideoRateUpdate = {
317 map(this.restExtractor.extractDataBool),
318 catchError(err => this.restExtractor.handleError(err))