]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Fix client error logging
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
6 import { ResultList } from '../../../../../shared/models/result-list.model'
7 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
8 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
9 import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
10 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
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 { VideoSortField } 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 import { Account } from '@app/shared/account/account.model'
24 import { AccountService } from '@app/shared/account/account.service'
25 import { VideoChannel } from '../../../../../shared/models/videos'
26 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
27 import { ServerService } from '@app/core'
28
29 @Injectable()
30 export class VideoService {
31 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
32 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
33
34 constructor (
35 private authHttp: HttpClient,
36 private restExtractor: RestExtractor,
37 private restService: RestService,
38 private serverService: ServerService
39 ) {}
40
41 getVideoViewUrl (uuid: string) {
42 return VideoService.BASE_VIDEO_URL + uuid + '/views'
43 }
44
45 getVideo (uuid: string): Observable<VideoDetails> {
46 return this.serverService.localeObservable
47 .pipe(
48 switchMap(translations => {
49 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
50 .pipe(map(videoHash => ({ videoHash, translations })))
51 }),
52 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
53 catchError(err => this.restExtractor.handleError(err))
54 )
55 }
56
57 viewVideo (uuid: string): Observable<boolean> {
58 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
59 .pipe(
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
62 )
63 }
64
65 updateVideo (video: VideoEdit) {
66 const language = video.language || null
67 const licence = video.licence || null
68 const category = video.category || null
69 const description = video.description || null
70 const support = video.support || null
71 const scheduleUpdate = video.scheduleUpdate || null
72
73 const body: VideoUpdate = {
74 name: video.name,
75 category,
76 licence,
77 language,
78 support,
79 description,
80 channelId: video.channelId,
81 privacy: video.privacy,
82 tags: video.tags,
83 nsfw: video.nsfw,
84 waitTranscoding: video.waitTranscoding,
85 commentsEnabled: video.commentsEnabled,
86 thumbnailfile: video.thumbnailfile,
87 previewfile: video.previewfile,
88 scheduleUpdate
89 }
90
91 const data = objectToFormData(body)
92
93 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
94 .pipe(
95 map(this.restExtractor.extractDataBool),
96 catchError(err => this.restExtractor.handleError(err))
97 )
98 }
99
100 uploadVideo (video: FormData) {
101 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
102
103 return this.authHttp
104 .request<{ video: { id: number, uuid: string } }>(req)
105 .pipe(catchError(err => this.restExtractor.handleError(err)))
106 }
107
108 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
109 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
110
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, pagination, sort)
113
114 return this.authHttp
115 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
116 .pipe(
117 switchMap(res => this.extractVideos(res)),
118 catchError(err => this.restExtractor.handleError(err))
119 )
120 }
121
122 getAccountVideos (
123 account: Account,
124 videoPagination: ComponentPagination,
125 sort: VideoSortField
126 ): Observable<{ videos: Video[], totalVideos: number }> {
127 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
128
129 let params = new HttpParams()
130 params = this.restService.addRestGetParams(params, pagination, sort)
131
132 return this.authHttp
133 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
134 .pipe(
135 switchMap(res => this.extractVideos(res)),
136 catchError(err => this.restExtractor.handleError(err))
137 )
138 }
139
140 getVideoChannelVideos (
141 videoChannel: VideoChannel,
142 videoPagination: ComponentPagination,
143 sort: VideoSortField
144 ): Observable<{ videos: Video[], totalVideos: number }> {
145 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
146
147 let params = new HttpParams()
148 params = this.restService.addRestGetParams(params, pagination, sort)
149
150 return this.authHttp
151 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
152 .pipe(
153 switchMap(res => this.extractVideos(res)),
154 catchError(err => this.restExtractor.handleError(err))
155 )
156 }
157
158 getVideos (
159 videoPagination: ComponentPagination,
160 sort: VideoSortField,
161 filter?: VideoFilter,
162 category?: number
163 ): Observable<{ videos: Video[], totalVideos: number }> {
164 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
165
166 let params = new HttpParams()
167 params = this.restService.addRestGetParams(params, pagination, sort)
168
169 if (filter) {
170 params = params.set('filter', filter)
171 }
172
173 if (category) {
174 params = params.set('category', category + '')
175 }
176
177 return this.authHttp
178 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
179 .pipe(
180 switchMap(res => this.extractVideos(res)),
181 catchError(err => this.restExtractor.handleError(err))
182 )
183 }
184
185 buildBaseFeedUrls (params: HttpParams) {
186 const feeds = [
187 {
188 label: 'rss 2.0',
189 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
190 },
191 {
192 label: 'atom 1.0',
193 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
194 },
195 {
196 label: 'json 1.0',
197 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
198 }
199 ]
200
201 if (params && params.keys().length !== 0) {
202 for (const feed of feeds) {
203 feed.url += '?' + params.toString()
204 }
205 }
206
207 return feeds
208 }
209
210 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, category?: number) {
211 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
212
213 if (filter) params = params.set('filter', filter)
214
215 if (category) params = params.set('category', category + '')
216
217 return this.buildBaseFeedUrls(params)
218 }
219
220 getAccountFeedUrls (accountId: number) {
221 let params = this.restService.addRestGetParams(new HttpParams())
222 params = params.set('accountId', accountId.toString())
223
224 return this.buildBaseFeedUrls(params)
225 }
226
227 getVideoChannelFeedUrls (videoChannelId: number) {
228 let params = this.restService.addRestGetParams(new HttpParams())
229 params = params.set('videoChannelId', videoChannelId.toString())
230
231 return this.buildBaseFeedUrls(params)
232 }
233
234 searchVideos (
235 search: string,
236 videoPagination: ComponentPagination,
237 sort: VideoSortField
238 ): Observable<{ videos: Video[], totalVideos: number }> {
239 const url = VideoService.BASE_VIDEO_URL + 'search'
240
241 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
242
243 let params = new HttpParams()
244 params = this.restService.addRestGetParams(params, pagination, sort)
245 params = params.append('search', search)
246
247 return this.authHttp
248 .get<ResultList<VideoServerModel>>(url, { params })
249 .pipe(
250 switchMap(res => this.extractVideos(res)),
251 catchError(err => this.restExtractor.handleError(err))
252 )
253 }
254
255 removeVideo (id: number) {
256 return this.authHttp
257 .delete(VideoService.BASE_VIDEO_URL + id)
258 .pipe(
259 map(this.restExtractor.extractDataBool),
260 catchError(err => this.restExtractor.handleError(err))
261 )
262 }
263
264 loadCompleteDescription (descriptionPath: string) {
265 return this.authHttp
266 .get(environment.apiUrl + descriptionPath)
267 .pipe(
268 map(res => res[ 'description' ]),
269 catchError(err => this.restExtractor.handleError(err))
270 )
271 }
272
273 setVideoLike (id: number) {
274 return this.setVideoRate(id, 'like')
275 }
276
277 setVideoDislike (id: number) {
278 return this.setVideoRate(id, 'dislike')
279 }
280
281 unsetVideoLike (id: number) {
282 return this.setVideoRate(id, 'none')
283 }
284
285 getUserVideoRating (id: number) {
286 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
287
288 return this.authHttp.get<UserVideoRate>(url)
289 .pipe(catchError(err => this.restExtractor.handleError(err)))
290 }
291
292 private setVideoRate (id: number, rateType: VideoRateType) {
293 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
294 const body: UserVideoRateUpdate = {
295 rating: rateType
296 }
297
298 return this.authHttp
299 .put(url, body)
300 .pipe(
301 map(this.restExtractor.extractDataBool),
302 catchError(err => this.restExtractor.handleError(err))
303 )
304 }
305
306 private extractVideos (result: ResultList<VideoServerModel>) {
307 return this.serverService.localeObservable
308 .pipe(
309 map(translations => {
310 const videosJson = result.data
311 const totalVideos = result.total
312 const videos: Video[] = []
313
314 for (const videoJson of videosJson) {
315 videos.push(new Video(videoJson, translations))
316 }
317
318 return { videos, totalVideos }
319 })
320 )
321 }
322 }