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