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