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 { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
8 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
9 import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
10 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
11 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
12 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
13 import { environment } from '../../../environments/environment'
14 import { ComponentPagination } from '../rest/component-pagination.model'
15 import { RestExtractor } from '../rest/rest-extractor.service'
16 import { RestService } from '../rest/rest.service'
17 import { UserService } from '../users/user.service'
18 import { VideoSortField } from './sort-field.type'
19 import { VideoDetails } from './video-details.model'
20 import { VideoEdit } from './video-edit.model'
21 import { Video } from './video.model'
22 import { objectToFormData } from '@app/shared/misc/utils'
23 import { Account } from '@app/shared/account/account.model'
24 import { AccountService } from '@app/shared/account/account.service'
25 import { VideoChannel } from '../../../../../shared/models/videos'
26 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
27 import { ServerService } from '@app/core'
30 export class VideoService {
31 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
32 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
35 private authHttp: HttpClient,
36 private restExtractor: RestExtractor,
37 private restService: RestService,
38 private serverService: ServerService
41 getVideoViewUrl (uuid: string) {
42 return VideoService.BASE_VIDEO_URL + uuid + '/views'
45 getVideo (uuid: string): Observable<VideoDetails> {
46 return this.serverService.localeObservable
48 switchMap(translations => {
49 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
50 .pipe(map(videoHash => ({ videoHash, translations })))
52 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
53 catchError(err => this.restExtractor.handleError(err))
57 viewVideo (uuid: string): Observable<boolean> {
58 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
65 updateVideo (video: VideoEdit) {
66 const language = video.language || null
67 const licence = video.licence || null
68 const category = video.category || null
69 const description = video.description || null
70 const support = video.support || null
71 const scheduleUpdate = video.scheduleUpdate || null
73 const body: VideoUpdate = {
80 channelId: video.channelId,
81 privacy: video.privacy,
84 waitTranscoding: video.waitTranscoding,
85 commentsEnabled: video.commentsEnabled,
86 thumbnailfile: video.thumbnailfile,
87 previewfile: video.previewfile,
91 const data = objectToFormData(body)
93 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
95 map(this.restExtractor.extractDataBool),
96 catchError(err => this.restExtractor.handleError(err))
100 uploadVideo (video: FormData) {
101 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
104 .request<{ video: { id: number, uuid: string } }>(req)
105 .pipe(catchError(err => this.restExtractor.handleError(err)))
108 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
109 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, pagination, sort)
115 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
117 switchMap(res => this.extractVideos(res)),
118 catchError(err => this.restExtractor.handleError(err))
124 videoPagination: ComponentPagination,
126 ): Observable<{ videos: Video[], totalVideos: number }> {
127 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
129 let params = new HttpParams()
130 params = this.restService.addRestGetParams(params, pagination, sort)
133 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
135 switchMap(res => this.extractVideos(res)),
136 catchError(err => this.restExtractor.handleError(err))
140 getVideoChannelVideos (
141 videoChannel: VideoChannel,
142 videoPagination: ComponentPagination,
144 ): Observable<{ videos: Video[], totalVideos: number }> {
145 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
147 let params = new HttpParams()
148 params = this.restService.addRestGetParams(params, pagination, sort)
151 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
153 switchMap(res => this.extractVideos(res)),
154 catchError(err => this.restExtractor.handleError(err))
159 videoPagination: ComponentPagination,
160 sort: VideoSortField,
161 filter?: VideoFilter,
163 ): Observable<{ videos: Video[], totalVideos: number }> {
164 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
166 let params = new HttpParams()
167 params = this.restService.addRestGetParams(params, pagination, sort)
170 params = params.set('filter', filter)
174 params = params.set('category', category + '')
178 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
180 switchMap(res => this.extractVideos(res)),
181 catchError(err => this.restExtractor.handleError(err))
185 buildBaseFeedUrls (params: HttpParams) {
189 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
193 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
197 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
201 if (params && params.keys().length !== 0) {
202 for (const feed of feeds) {
203 feed.url += '?' + params.toString()
210 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, category?: number) {
211 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
213 if (filter) params = params.set('filter', filter)
215 if (category) params = params.set('category', category + '')
217 return this.buildBaseFeedUrls(params)
220 getAccountFeedUrls (accountId: number) {
221 let params = this.restService.addRestGetParams(new HttpParams())
222 params = params.set('accountId', accountId.toString())
224 return this.buildBaseFeedUrls(params)
227 getVideoChannelFeedUrls (videoChannelId: number) {
228 let params = this.restService.addRestGetParams(new HttpParams())
229 params = params.set('videoChannelId', videoChannelId.toString())
231 return this.buildBaseFeedUrls(params)
234 removeVideo (id: number) {
236 .delete(VideoService.BASE_VIDEO_URL + id)
238 map(this.restExtractor.extractDataBool),
239 catchError(err => this.restExtractor.handleError(err))
243 loadCompleteDescription (descriptionPath: string) {
245 .get(environment.apiUrl + descriptionPath)
247 map(res => res[ 'description' ]),
248 catchError(err => this.restExtractor.handleError(err))
252 setVideoLike (id: number) {
253 return this.setVideoRate(id, 'like')
256 setVideoDislike (id: number) {
257 return this.setVideoRate(id, 'dislike')
260 unsetVideoLike (id: number) {
261 return this.setVideoRate(id, 'none')
264 getUserVideoRating (id: number) {
265 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
267 return this.authHttp.get<UserVideoRate>(url)
268 .pipe(catchError(err => this.restExtractor.handleError(err)))
271 extractVideos (result: ResultList<VideoServerModel>) {
272 return this.serverService.localeObservable
274 map(translations => {
275 const videosJson = result.data
276 const totalVideos = result.total
277 const videos: Video[] = []
279 for (const videoJson of videosJson) {
280 videos.push(new Video(videoJson, translations))
283 return { videos, totalVideos }
288 private setVideoRate (id: number, rateType: VideoRateType) {
289 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
290 const body: UserVideoRateUpdate = {
297 map(this.restExtractor.extractDataBool),
298 catchError(err => this.restExtractor.handleError(err))