1 import { catchError, map } 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'
29 export class VideoService {
30 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
31 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
34 private authHttp: HttpClient,
35 private restExtractor: RestExtractor,
36 private restService: RestService
39 getVideoViewUrl (uuid: string) {
40 return VideoService.BASE_VIDEO_URL + uuid + '/views'
43 getVideo (uuid: string): Observable<VideoDetails> {
44 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
46 map(videoHash => new VideoDetails(videoHash)),
47 catchError(res => this.restExtractor.handleError(res))
51 viewVideo (uuid: string): Observable<boolean> {
52 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
54 map(this.restExtractor.extractDataBool),
55 catchError(this.restExtractor.handleError)
59 updateVideo (video: VideoEdit) {
60 const language = video.language || null
61 const licence = video.licence || null
62 const category = video.category || null
63 const description = video.description || null
64 const support = video.support || null
66 const body: VideoUpdate = {
73 channelId: video.channelId,
74 privacy: video.privacy,
77 commentsEnabled: video.commentsEnabled,
78 thumbnailfile: video.thumbnailfile,
79 previewfile: video.previewfile
82 const data = objectToFormData(body)
84 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
86 map(this.restExtractor.extractDataBool),
87 catchError(this.restExtractor.handleError)
91 uploadVideo (video: FormData) {
92 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
95 .request<{ video: { id: number, uuid: string} }>(req)
96 .pipe(catchError(this.restExtractor.handleError))
99 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
100 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
102 let params = new HttpParams()
103 params = this.restService.addRestGetParams(params, pagination, sort)
105 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
107 map(this.extractVideos),
108 catchError(res => this.restExtractor.handleError(res))
114 videoPagination: ComponentPagination,
116 ): Observable<{ videos: Video[], totalVideos: number}> {
117 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
119 let params = new HttpParams()
120 params = this.restService.addRestGetParams(params, pagination, sort)
123 .get(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
125 map(this.extractVideos),
126 catchError(res => this.restExtractor.handleError(res))
130 getVideoChannelVideos (
131 videoChannel: VideoChannel,
132 videoPagination: ComponentPagination,
134 ): Observable<{ videos: Video[], totalVideos: number}> {
135 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
137 let params = new HttpParams()
138 params = this.restService.addRestGetParams(params, pagination, sort)
141 .get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
143 map(this.extractVideos),
144 catchError(res => this.restExtractor.handleError(res))
149 videoPagination: ComponentPagination,
150 sort: VideoSortField,
152 ): Observable<{ videos: Video[], totalVideos: number}> {
153 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
155 let params = new HttpParams()
156 params = this.restService.addRestGetParams(params, pagination, sort)
159 params = params.set('filter', filter)
163 .get(VideoService.BASE_VIDEO_URL, { params })
165 map(this.extractVideos),
166 catchError(res => this.restExtractor.handleError(res))
170 buildBaseFeedUrls (params: HttpParams) {
174 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
178 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
182 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
186 if (params && params.keys().length !== 0) {
187 for (const feed of feeds) {
188 feed.url += '?' + params.toString()
195 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
196 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
198 if (filter) params = params.set('filter', filter)
200 return this.buildBaseFeedUrls(params)
203 getAccountFeedUrls (accountId: number) {
204 let params = this.restService.addRestGetParams(new HttpParams())
205 params = params.set('accountId', accountId.toString())
207 return this.buildBaseFeedUrls(params)
210 getVideoChannelFeedUrls (videoChannelId: number) {
211 let params = this.restService.addRestGetParams(new HttpParams())
212 params = params.set('videoChannelId', videoChannelId.toString())
214 return this.buildBaseFeedUrls(params)
219 videoPagination: ComponentPagination,
221 ): Observable<{ videos: Video[], totalVideos: number}> {
222 const url = VideoService.BASE_VIDEO_URL + 'search'
224 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
226 let params = new HttpParams()
227 params = this.restService.addRestGetParams(params, pagination, sort)
228 params = params.append('search', search)
231 .get<ResultList<VideoServerModel>>(url, { params })
233 map(this.extractVideos),
234 catchError(res => this.restExtractor.handleError(res))
238 removeVideo (id: number) {
240 .delete(VideoService.BASE_VIDEO_URL + id)
242 map(this.restExtractor.extractDataBool),
243 catchError(res => this.restExtractor.handleError(res))
247 loadCompleteDescription (descriptionPath: string) {
249 .get(environment.apiUrl + descriptionPath)
251 map(res => res[ 'description' ]),
252 catchError(res => this.restExtractor.handleError(res))
256 setVideoLike (id: number) {
257 return this.setVideoRate(id, 'like')
260 setVideoDislike (id: number) {
261 return this.setVideoRate(id, 'dislike')
264 unsetVideoLike (id: number) {
265 return this.setVideoRate(id, 'none')
268 getUserVideoRating (id: number) {
269 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
271 return this.authHttp.get<UserVideoRate>(url)
272 .pipe(catchError(res => this.restExtractor.handleError(res)))
275 private setVideoRate (id: number, rateType: VideoRateType) {
276 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
277 const body: UserVideoRateUpdate = {
284 map(this.restExtractor.extractDataBool),
285 catchError(res => this.restExtractor.handleError(res))
289 private extractVideos (result: ResultList<VideoServerModel>) {
290 const videosJson = result.data
291 const totalVideos = result.total
294 for (const videoJson of videosJson) {
295 videos.push(new Video(videoJson))
298 return { videos, totalVideos }