Fix lint
[oweals/peertube.git] / client / src / app / shared / video / video.service.ts
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 { 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 { SortField } 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
24 @Injectable()
25 export class VideoService {
26   private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
27
28   constructor (
29     private authHttp: HttpClient,
30     private restExtractor: RestExtractor,
31     private restService: RestService
32   ) {}
33
34   getVideoViewUrl (uuid: string) {
35     return VideoService.BASE_VIDEO_URL + uuid + '/views'
36   }
37
38   getVideo (uuid: string): Observable<VideoDetails> {
39     return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
40                         .map(videoHash => new VideoDetails(videoHash))
41                         .catch((res) => this.restExtractor.handleError(res))
42   }
43
44   viewVideo (uuid: string): Observable<VideoDetails> {
45     return this.authHttp.post(this.getVideoViewUrl(uuid), {})
46       .map(this.restExtractor.extractDataBool)
47       .catch(this.restExtractor.handleError)
48   }
49
50   updateVideo (video: VideoEdit) {
51     const language = video.language || undefined
52     const licence = video.licence || undefined
53     const category = video.category || undefined
54     const description = video.description || undefined
55     const support = video.support || undefined
56
57     const body: VideoUpdate = {
58       name: video.name,
59       category,
60       licence,
61       language,
62       support,
63       description,
64       privacy: video.privacy,
65       tags: video.tags,
66       nsfw: video.nsfw,
67       commentsEnabled: video.commentsEnabled,
68       thumbnailfile: video.thumbnailfile,
69       previewfile: video.previewfile
70     }
71
72     const data = objectToFormData(body)
73
74     return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
75                         .map(this.restExtractor.extractDataBool)
76                         .catch(this.restExtractor.handleError)
77   }
78
79   uploadVideo (video: FormData) {
80     const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
81
82     return this.authHttp
83       .request(req)
84       .catch(this.restExtractor.handleError)
85   }
86
87   getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
88     const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
89
90     let params = new HttpParams()
91     params = this.restService.addRestGetParams(params, pagination, sort)
92
93     return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
94       .map(this.extractVideos)
95       .catch((res) => this.restExtractor.handleError(res))
96   }
97
98   getVideos (
99     videoPagination: ComponentPagination,
100     sort: SortField,
101     filter?: VideoFilter
102   ): Observable<{ videos: Video[], totalVideos: number}> {
103     const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
104
105     let params = new HttpParams()
106     params = this.restService.addRestGetParams(params, pagination, sort)
107
108     if (filter) {
109       params = params.set('filter', filter)
110     }
111
112     return this.authHttp
113       .get(VideoService.BASE_VIDEO_URL, { params })
114       .map(this.extractVideos)
115       .catch((res) => this.restExtractor.handleError(res))
116   }
117
118   searchVideos (
119     search: string,
120     videoPagination: ComponentPagination,
121     sort: SortField
122   ): Observable<{ videos: Video[], totalVideos: number}> {
123     const url = VideoService.BASE_VIDEO_URL + 'search'
124
125     const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
126
127     let params = new HttpParams()
128     params = this.restService.addRestGetParams(params, pagination, sort)
129     params = params.append('search', search)
130
131     return this.authHttp
132       .get<ResultList<VideoServerModel>>(url, { params })
133       .map(this.extractVideos)
134       .catch((res) => this.restExtractor.handleError(res))
135   }
136
137   removeVideo (id: number) {
138     return this.authHttp
139       .delete(VideoService.BASE_VIDEO_URL + id)
140       .map(this.restExtractor.extractDataBool)
141       .catch((res) => this.restExtractor.handleError(res))
142   }
143
144   loadCompleteDescription (descriptionPath: string) {
145     return this.authHttp
146       .get(environment.apiUrl + descriptionPath)
147       .map(res => res['description'])
148       .catch((res) => this.restExtractor.handleError(res))
149   }
150
151   setVideoLike (id: number) {
152     return this.setVideoRate(id, 'like')
153   }
154
155   setVideoDislike (id: number) {
156     return this.setVideoRate(id, 'dislike')
157   }
158
159   unsetVideoLike (id: number) {
160     return this.setVideoRate(id, 'none')
161   }
162
163   getUserVideoRating (id: number): Observable<UserVideoRate> {
164     const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
165
166     return this.authHttp
167       .get(url)
168       .catch(res => this.restExtractor.handleError(res))
169   }
170
171   private setVideoRate (id: number, rateType: VideoRateType) {
172     const url = VideoService.BASE_VIDEO_URL + id + '/rate'
173     const body: UserVideoRateUpdate = {
174       rating: rateType
175     }
176
177     return this.authHttp
178       .put(url, body)
179       .map(this.restExtractor.extractDataBool)
180       .catch(res => this.restExtractor.handleError(res))
181   }
182
183   private extractVideos (result: ResultList<VideoServerModel>) {
184     const videosJson = result.data
185     const totalVideos = result.total
186     const videos = []
187
188     for (const videoJson of videosJson) {
189       videos.push(new Video(videoJson))
190     }
191
192     return { videos, totalVideos }
193   }
194 }