1 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { Observable } from 'rxjs/Observable'
6 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
7 import { ResultList } from '../../../../../shared/models/result-list.model'
8 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
9 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
10 import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
11 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
12 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
13 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
14 import { environment } from '../../../environments/environment'
15 import { ComponentPagination } from '../rest/component-pagination.model'
16 import { RestExtractor } from '../rest/rest-extractor.service'
17 import { RestService } from '../rest/rest.service'
18 import { UserService } from '../users/user.service'
19 import { SortField } from './sort-field.type'
20 import { VideoDetails } from './video-details.model'
21 import { VideoEdit } from './video-edit.model'
22 import { Video } from './video.model'
23 import { objectToFormData } from '@app/shared/misc/utils'
26 export class VideoService {
27 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
28 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
31 private authHttp: HttpClient,
32 private restExtractor: RestExtractor,
33 private restService: RestService
36 getVideoViewUrl (uuid: string) {
37 return VideoService.BASE_VIDEO_URL + uuid + '/views'
40 getVideo (uuid: string): Observable<VideoDetails> {
41 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
42 .map(videoHash => new VideoDetails(videoHash))
43 .catch((res) => this.restExtractor.handleError(res))
46 viewVideo (uuid: string): Observable<VideoDetails> {
47 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
48 .map(this.restExtractor.extractDataBool)
49 .catch(this.restExtractor.handleError)
52 updateVideo (video: VideoEdit) {
53 const language = video.language || undefined
54 const licence = video.licence || undefined
55 const category = video.category || undefined
56 const description = video.description || undefined
57 const support = video.support || undefined
59 const body: VideoUpdate = {
66 privacy: video.privacy,
69 commentsEnabled: video.commentsEnabled,
70 thumbnailfile: video.thumbnailfile,
71 previewfile: video.previewfile
74 const data = objectToFormData(body)
76 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
77 .map(this.restExtractor.extractDataBool)
78 .catch(this.restExtractor.handleError)
81 uploadVideo (video: FormData) {
82 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
86 .catch(this.restExtractor.handleError)
89 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
90 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
92 let params = new HttpParams()
93 params = this.restService.addRestGetParams(params, pagination, sort)
95 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
96 .map(this.extractVideos)
97 .catch((res) => this.restExtractor.handleError(res))
101 videoPagination: ComponentPagination,
104 ): Observable<{ videos: Video[], totalVideos: number}> {
105 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
107 let params = new HttpParams()
108 params = this.restService.addRestGetParams(params, pagination, sort)
111 params = params.set('filter', filter)
115 .get(VideoService.BASE_VIDEO_URL, { params })
116 .map(this.extractVideos)
117 .catch((res) => this.restExtractor.handleError(res))
120 buildBaseFeedUrls () {
124 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
128 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
132 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
139 getVideoFeedUrls (filter?: VideoFilter) {
140 let params = this.restService.addRestGetParams(new HttpParams())
141 const feeds = this.buildBaseFeedUrls()
143 if (filter) params = params.set('filter', filter)
145 if (params.keys().length !== 0) {
146 for (let item of feeds) {
147 item.url += `?${params.toString()}`
154 getAccountFeedUrls (accountId: number) {
155 let params = this.restService.addRestGetParams(new HttpParams())
156 const feeds = this.buildBaseFeedUrls()
158 params = params.set('accountId', accountId.toString())
160 if (params.keys().length !== 0) {
161 for (let item of feeds) {
162 item.url += `?${params.toString()}`
171 videoPagination: ComponentPagination,
173 ): Observable<{ videos: Video[], totalVideos: number}> {
174 const url = VideoService.BASE_VIDEO_URL + 'search'
176 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
178 let params = new HttpParams()
179 params = this.restService.addRestGetParams(params, pagination, sort)
180 params = params.append('search', search)
183 .get<ResultList<VideoServerModel>>(url, { params })
184 .map(this.extractVideos)
185 .catch((res) => this.restExtractor.handleError(res))
188 removeVideo (id: number) {
190 .delete(VideoService.BASE_VIDEO_URL + id)
191 .map(this.restExtractor.extractDataBool)
192 .catch((res) => this.restExtractor.handleError(res))
195 loadCompleteDescription (descriptionPath: string) {
197 .get(environment.apiUrl + descriptionPath)
198 .map(res => res['description'])
199 .catch((res) => this.restExtractor.handleError(res))
202 setVideoLike (id: number) {
203 return this.setVideoRate(id, 'like')
206 setVideoDislike (id: number) {
207 return this.setVideoRate(id, 'dislike')
210 unsetVideoLike (id: number) {
211 return this.setVideoRate(id, 'none')
214 getUserVideoRating (id: number): Observable<UserVideoRate> {
215 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
219 .catch(res => this.restExtractor.handleError(res))
222 private setVideoRate (id: number, rateType: VideoRateType) {
223 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
224 const body: UserVideoRateUpdate = {
230 .map(this.restExtractor.extractDataBool)
231 .catch(res => this.restExtractor.handleError(res))
234 private extractVideos (result: ResultList<VideoServerModel>) {
235 const videosJson = result.data
236 const totalVideos = result.total
239 for (const videoJson of videosJson) {
240 videos.push(new Video(videoJson))
243 return { videos, totalVideos }