]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Add account view
[github/Chocobozzz/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 { 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 { VideoSortField } 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'
24 import { Account } from '@app/shared/account/account.model'
25 import { AccountService } from '@app/shared/account/account.service'
26
27 @Injectable()
28 export class VideoService {
29 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
30 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
31
32 constructor (
33 private authHttp: HttpClient,
34 private restExtractor: RestExtractor,
35 private restService: RestService
36 ) {}
37
38 getVideoViewUrl (uuid: string) {
39 return VideoService.BASE_VIDEO_URL + uuid + '/views'
40 }
41
42 getVideo (uuid: string): Observable<VideoDetails> {
43 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
44 .map(videoHash => new VideoDetails(videoHash))
45 .catch((res) => this.restExtractor.handleError(res))
46 }
47
48 viewVideo (uuid: string): Observable<VideoDetails> {
49 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
50 .map(this.restExtractor.extractDataBool)
51 .catch(this.restExtractor.handleError)
52 }
53
54 updateVideo (video: VideoEdit) {
55 const language = video.language || undefined
56 const licence = video.licence || undefined
57 const category = video.category || undefined
58 const description = video.description || undefined
59 const support = video.support || undefined
60
61 const body: VideoUpdate = {
62 name: video.name,
63 category,
64 licence,
65 language,
66 support,
67 description,
68 privacy: video.privacy,
69 tags: video.tags,
70 nsfw: video.nsfw,
71 commentsEnabled: video.commentsEnabled,
72 thumbnailfile: video.thumbnailfile,
73 previewfile: video.previewfile
74 }
75
76 const data = objectToFormData(body)
77
78 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
79 .map(this.restExtractor.extractDataBool)
80 .catch(this.restExtractor.handleError)
81 }
82
83 uploadVideo (video: FormData) {
84 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
85
86 return this.authHttp
87 .request(req)
88 .catch(this.restExtractor.handleError)
89 }
90
91 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
92 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
93
94 let params = new HttpParams()
95 params = this.restService.addRestGetParams(params, pagination, sort)
96
97 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
98 .map(this.extractVideos)
99 .catch((res) => this.restExtractor.handleError(res))
100 }
101
102 getAccountVideos (
103 account: Account,
104 videoPagination: ComponentPagination,
105 sort: VideoSortField
106 ): Observable<{ videos: Video[], totalVideos: number}> {
107 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
108
109 let params = new HttpParams()
110 params = this.restService.addRestGetParams(params, pagination, sort)
111
112 return this.authHttp
113 .get(AccountService.BASE_ACCOUNT_URL + account.id + '/videos', { params })
114 .map(this.extractVideos)
115 .catch((res) => this.restExtractor.handleError(res))
116 }
117
118 getVideos (
119 videoPagination: ComponentPagination,
120 sort: VideoSortField,
121 filter?: VideoFilter
122 ): Observable<{ videos: Video[], totalVideos: number}> {
123 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
124
125 let params = new HttpParams()
126 params = this.restService.addRestGetParams(params, pagination, sort)
127
128 if (filter) {
129 params = params.set('filter', filter)
130 }
131
132 return this.authHttp
133 .get(VideoService.BASE_VIDEO_URL, { params })
134 .map(this.extractVideos)
135 .catch((res) => this.restExtractor.handleError(res))
136 }
137
138 buildBaseFeedUrls (params: HttpParams) {
139 const feeds = [
140 {
141 label: 'rss 2.0',
142 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
143 },
144 {
145 label: 'atom 1.0',
146 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
147 },
148 {
149 label: 'json 1.0',
150 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
151 }
152 ]
153
154 if (params && params.keys().length !== 0) {
155 for (const feed of feeds) {
156 feed.url += '?' + params.toString()
157 }
158 }
159
160 return feeds
161 }
162
163 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
164 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
165
166 if (filter) params = params.set('filter', filter)
167
168 return this.buildBaseFeedUrls(params)
169 }
170
171 getAccountFeedUrls (accountId: number) {
172 let params = this.restService.addRestGetParams(new HttpParams())
173 params = params.set('accountId', accountId.toString())
174
175 return this.buildBaseFeedUrls(params)
176 }
177
178 searchVideos (
179 search: string,
180 videoPagination: ComponentPagination,
181 sort: VideoSortField
182 ): Observable<{ videos: Video[], totalVideos: number}> {
183 const url = VideoService.BASE_VIDEO_URL + 'search'
184
185 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
186
187 let params = new HttpParams()
188 params = this.restService.addRestGetParams(params, pagination, sort)
189 params = params.append('search', search)
190
191 return this.authHttp
192 .get<ResultList<VideoServerModel>>(url, { params })
193 .map(this.extractVideos)
194 .catch((res) => this.restExtractor.handleError(res))
195 }
196
197 removeVideo (id: number) {
198 return this.authHttp
199 .delete(VideoService.BASE_VIDEO_URL + id)
200 .map(this.restExtractor.extractDataBool)
201 .catch((res) => this.restExtractor.handleError(res))
202 }
203
204 loadCompleteDescription (descriptionPath: string) {
205 return this.authHttp
206 .get(environment.apiUrl + descriptionPath)
207 .map(res => res['description'])
208 .catch((res) => this.restExtractor.handleError(res))
209 }
210
211 setVideoLike (id: number) {
212 return this.setVideoRate(id, 'like')
213 }
214
215 setVideoDislike (id: number) {
216 return this.setVideoRate(id, 'dislike')
217 }
218
219 unsetVideoLike (id: number) {
220 return this.setVideoRate(id, 'none')
221 }
222
223 getUserVideoRating (id: number): Observable<UserVideoRate> {
224 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
225
226 return this.authHttp
227 .get(url)
228 .catch(res => this.restExtractor.handleError(res))
229 }
230
231 private setVideoRate (id: number, rateType: VideoRateType) {
232 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
233 const body: UserVideoRateUpdate = {
234 rating: rateType
235 }
236
237 return this.authHttp
238 .put(url, body)
239 .map(this.restExtractor.extractDataBool)
240 .catch(res => this.restExtractor.handleError(res))
241 }
242
243 private extractVideos (result: ResultList<VideoServerModel>) {
244 const videosJson = result.data
245 const totalVideos = result.total
246 const videos = []
247
248 for (const videoJson of videosJson) {
249 videos.push(new Video(videoJson))
250 }
251
252 return { videos, totalVideos }
253 }
254 }