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'
32 export class VideoService {
33 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
34 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
37 private authHttp: HttpClient,
38 private restExtractor: RestExtractor,
39 private restService: RestService,
40 private serverService: ServerService
43 getVideoViewUrl (uuid: string) {
44 return VideoService.BASE_VIDEO_URL + uuid + '/views'
47 getVideo (uuid: string): Observable<VideoDetails> {
48 return this.serverService.localeObservable
50 switchMap(translations => {
51 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
52 .pipe(map(videoHash => ({ videoHash, translations })))
54 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
55 catchError(err => this.restExtractor.handleError(err))
59 viewVideo (uuid: string): Observable<boolean> {
60 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
62 map(this.restExtractor.extractDataBool),
63 catchError(err => this.restExtractor.handleError(err))
67 updateVideo (video: VideoEdit) {
68 const language = video.language || null
69 const licence = video.licence || null
70 const category = video.category || null
71 const description = video.description || null
72 const support = video.support || null
73 const scheduleUpdate = video.scheduleUpdate || null
75 const body: VideoUpdate = {
82 channelId: video.channelId,
83 privacy: video.privacy,
86 waitTranscoding: video.waitTranscoding,
87 commentsEnabled: video.commentsEnabled,
88 thumbnailfile: video.thumbnailfile,
89 previewfile: video.previewfile,
93 const data = objectToFormData(body)
95 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
97 map(this.restExtractor.extractDataBool),
98 catchError(err => this.restExtractor.handleError(err))
102 uploadVideo (video: FormData) {
103 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
106 .request<{ video: { id: number, uuid: string } }>(req)
107 .pipe(catchError(err => this.restExtractor.handleError(err)))
110 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
111 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
113 let params = new HttpParams()
114 params = this.restService.addRestGetParams(params, pagination, sort)
117 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
119 switchMap(res => this.extractVideos(res)),
120 catchError(err => this.restExtractor.handleError(err))
126 videoPagination: ComponentPagination,
128 ): Observable<{ videos: Video[], totalVideos: number }> {
129 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
131 let params = new HttpParams()
132 params = this.restService.addRestGetParams(params, pagination, sort)
135 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
137 switchMap(res => this.extractVideos(res)),
138 catchError(err => this.restExtractor.handleError(err))
142 getVideoChannelVideos (
143 videoChannel: VideoChannel,
144 videoPagination: ComponentPagination,
146 ): Observable<{ videos: Video[], totalVideos: number }> {
147 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
149 let params = new HttpParams()
150 params = this.restService.addRestGetParams(params, pagination, sort)
153 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.name + '/videos', { params })
155 switchMap(res => this.extractVideos(res)),
156 catchError(err => this.restExtractor.handleError(err))
161 videoPagination: ComponentPagination,
162 sort: VideoSortField,
163 filter?: VideoFilter,
164 categoryOneOf?: number
165 ): Observable<{ videos: Video[], totalVideos: number }> {
166 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
168 let params = new HttpParams()
169 params = this.restService.addRestGetParams(params, pagination, sort)
172 params = params.set('filter', filter)
176 params = params.set('categoryOneOf', categoryOneOf + '')
180 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
182 switchMap(res => this.extractVideos(res)),
183 catchError(err => this.restExtractor.handleError(err))
187 buildBaseFeedUrls (params: HttpParams) {
191 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
195 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
199 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
203 if (params && params.keys().length !== 0) {
204 for (const feed of feeds) {
205 feed.url += '?' + params.toString()
212 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
213 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
215 if (filter) params = params.set('filter', filter)
217 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
219 return this.buildBaseFeedUrls(params)
222 getAccountFeedUrls (accountId: number) {
223 let params = this.restService.addRestGetParams(new HttpParams())
224 params = params.set('accountId', accountId.toString())
226 return this.buildBaseFeedUrls(params)
229 getVideoChannelFeedUrls (videoChannelId: number) {
230 let params = this.restService.addRestGetParams(new HttpParams())
231 params = params.set('videoChannelId', videoChannelId.toString())
233 return this.buildBaseFeedUrls(params)
236 removeVideo (id: number) {
238 .delete(VideoService.BASE_VIDEO_URL + id)
240 map(this.restExtractor.extractDataBool),
241 catchError(err => this.restExtractor.handleError(err))
245 loadCompleteDescription (descriptionPath: string) {
247 .get(environment.apiUrl + descriptionPath)
249 map(res => res[ 'description' ]),
250 catchError(err => this.restExtractor.handleError(err))
254 setVideoLike (id: number) {
255 return this.setVideoRate(id, 'like')
258 setVideoDislike (id: number) {
259 return this.setVideoRate(id, 'dislike')
262 unsetVideoLike (id: number) {
263 return this.setVideoRate(id, 'none')
266 getUserVideoRating (id: number) {
267 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
269 return this.authHttp.get<UserVideoRate>(url)
270 .pipe(catchError(err => this.restExtractor.handleError(err)))
273 extractVideos (result: ResultList<VideoServerModel>) {
274 return this.serverService.localeObservable
276 map(translations => {
277 const videosJson = result.data
278 const totalVideos = result.total
279 const videos: Video[] = []
281 for (const videoJson of videosJson) {
282 videos.push(new Video(videoJson, translations))
285 return { videos, totalVideos }
290 private setVideoRate (id: number, rateType: VideoRateType) {
291 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
292 const body: UserVideoRateUpdate = {
299 map(this.restExtractor.extractDataBool),
300 catchError(err => this.restExtractor.handleError(err))