]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.service.ts
0e2d36081b56a84f90e690763b73f3f9f946abb4
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video.service.ts
1 import { Observable } from 'rxjs'
2 import { catchError, map, switchMap } from 'rxjs/operators'
3 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core'
6 import { objectToFormData } from '@app/helpers'
7 import {
8 FeedFormat,
9 NSFWPolicyType,
10 ResultList,
11 UserVideoRate,
12 UserVideoRateType,
13 UserVideoRateUpdate,
14 Video as VideoServerModel,
15 VideoConstant,
16 VideoDetails as VideoDetailsServerModel,
17 VideoFileMetadata,
18 VideoFilter,
19 VideoPrivacy,
20 VideoSortField,
21 VideoUpdate,
22 VideoCreate
23 } from '@shared/models'
24 import { environment } from '../../../../environments/environment'
25 import { Account } from '../account/account.model'
26 import { AccountService } from '../account/account.service'
27 import { VideoChannel, VideoChannelService } from '../video-channel'
28 import { VideoDetails } from './video-details.model'
29 import { VideoEdit } from './video-edit.model'
30 import { Video } from './video.model'
31
32 export interface VideosProvider {
33 getVideos (parameters: {
34 videoPagination: ComponentPaginationLight,
35 sort: VideoSortField,
36 filter?: VideoFilter,
37 categoryOneOf?: number[],
38 languageOneOf?: string[]
39 nsfwPolicy: NSFWPolicyType
40 }): Observable<ResultList<Video>>
41 }
42
43 @Injectable()
44 export class VideoService implements VideosProvider {
45 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
46 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
47
48 constructor (
49 private authHttp: HttpClient,
50 private restExtractor: RestExtractor,
51 private restService: RestService,
52 private serverService: ServerService
53 ) {}
54
55 getVideoViewUrl (uuid: string) {
56 return VideoService.BASE_VIDEO_URL + uuid + '/views'
57 }
58
59 getUserWatchingVideoUrl (uuid: string) {
60 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
61 }
62
63 getVideo (options: { videoId: string }): Observable<VideoDetails> {
64 return this.serverService.getServerLocale()
65 .pipe(
66 switchMap(translations => {
67 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
68 .pipe(map(videoHash => ({ videoHash, translations })))
69 }),
70 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
71 catchError(err => this.restExtractor.handleError(err))
72 )
73 }
74
75 updateVideo (video: VideoEdit) {
76 const language = video.language || null
77 const licence = video.licence || null
78 const category = video.category || null
79 const description = video.description || null
80 const support = video.support || null
81 const scheduleUpdate = video.scheduleUpdate || null
82 const originallyPublishedAt = video.originallyPublishedAt || null
83
84 const body: VideoUpdate = {
85 name: video.name,
86 category,
87 licence,
88 language,
89 support,
90 description,
91 channelId: video.channelId,
92 privacy: video.privacy,
93 tags: video.tags,
94 nsfw: video.nsfw,
95 waitTranscoding: video.waitTranscoding,
96 commentsEnabled: video.commentsEnabled,
97 downloadEnabled: video.downloadEnabled,
98 thumbnailfile: video.thumbnailfile,
99 previewfile: video.previewfile,
100 pluginData: video.pluginData,
101 scheduleUpdate,
102 originallyPublishedAt
103 }
104
105 const data = objectToFormData(body)
106
107 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
108 .pipe(
109 map(this.restExtractor.extractDataBool),
110 catchError(err => this.restExtractor.handleError(err))
111 )
112 }
113
114 uploadVideo (video: FormData) {
115 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
116
117 return this.authHttp
118 .request<{ video: { id: number, uuid: string } }>(req)
119 .pipe(catchError(err => this.restExtractor.handleError(err)))
120 }
121
122 getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
123 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
124
125 let params = new HttpParams()
126 params = this.restService.addRestGetParams(params, pagination, sort)
127 params = this.restService.addObjectParams(params, { search })
128
129 return this.authHttp
130 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
131 .pipe(
132 switchMap(res => this.extractVideos(res)),
133 catchError(err => this.restExtractor.handleError(err))
134 )
135 }
136
137 getAccountVideos (
138 account: Account,
139 videoPagination: ComponentPaginationLight,
140 sort: VideoSortField
141 ): Observable<ResultList<Video>> {
142 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
143
144 let params = new HttpParams()
145 params = this.restService.addRestGetParams(params, pagination, sort)
146
147 return this.authHttp
148 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
149 .pipe(
150 switchMap(res => this.extractVideos(res)),
151 catchError(err => this.restExtractor.handleError(err))
152 )
153 }
154
155 getVideoChannelVideos (
156 videoChannel: VideoChannel,
157 videoPagination: ComponentPaginationLight,
158 sort: VideoSortField,
159 nsfwPolicy?: NSFWPolicyType
160 ): Observable<ResultList<Video>> {
161 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
162
163 let params = new HttpParams()
164 params = this.restService.addRestGetParams(params, pagination, sort)
165
166 if (nsfwPolicy) {
167 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
168 }
169
170 return this.authHttp
171 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
172 .pipe(
173 switchMap(res => this.extractVideos(res)),
174 catchError(err => this.restExtractor.handleError(err))
175 )
176 }
177
178 getVideos (parameters: {
179 videoPagination: ComponentPaginationLight,
180 sort: VideoSortField,
181 filter?: VideoFilter,
182 categoryOneOf?: number[],
183 languageOneOf?: string[],
184 skipCount?: boolean,
185 nsfwPolicy?: NSFWPolicyType
186 }): Observable<ResultList<Video>> {
187 const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
188
189 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
190
191 let params = new HttpParams()
192 params = this.restService.addRestGetParams(params, pagination, sort)
193
194 if (filter) params = params.set('filter', filter)
195 if (skipCount) params = params.set('skipCount', skipCount + '')
196
197 if (nsfwPolicy) {
198 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
199 }
200
201 if (languageOneOf) {
202 for (const l of languageOneOf) {
203 params = params.append('languageOneOf[]', l)
204 }
205 }
206
207 if (categoryOneOf) {
208 for (const c of categoryOneOf) {
209 params = params.append('categoryOneOf[]', c + '')
210 }
211 }
212
213 return this.authHttp
214 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
215 .pipe(
216 switchMap(res => this.extractVideos(res)),
217 catchError(err => this.restExtractor.handleError(err))
218 )
219 }
220
221 buildBaseFeedUrls (params: HttpParams) {
222 const feeds = [
223 {
224 format: FeedFormat.RSS,
225 label: 'media rss 2.0',
226 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
227 },
228 {
229 format: FeedFormat.ATOM,
230 label: 'atom 1.0',
231 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
232 },
233 {
234 format: FeedFormat.JSON,
235 label: 'json 1.0',
236 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
237 }
238 ]
239
240 if (params && params.keys().length !== 0) {
241 for (const feed of feeds) {
242 feed.url += '?' + params.toString()
243 }
244 }
245
246 return feeds
247 }
248
249 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
250 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
251
252 if (filter) params = params.set('filter', filter)
253
254 if (categoryOneOf) {
255 for (const c of categoryOneOf) {
256 params = params.append('categoryOneOf[]', c + '')
257 }
258 }
259
260 return this.buildBaseFeedUrls(params)
261 }
262
263 getAccountFeedUrls (accountId: number) {
264 let params = this.restService.addRestGetParams(new HttpParams())
265 params = params.set('accountId', accountId.toString())
266
267 return this.buildBaseFeedUrls(params)
268 }
269
270 getVideoChannelFeedUrls (videoChannelId: number) {
271 let params = this.restService.addRestGetParams(new HttpParams())
272 params = params.set('videoChannelId', videoChannelId.toString())
273
274 return this.buildBaseFeedUrls(params)
275 }
276
277 getVideoFileMetadata (metadataUrl: string) {
278 return this.authHttp
279 .get<VideoFileMetadata>(metadataUrl)
280 .pipe(
281 catchError(err => this.restExtractor.handleError(err))
282 )
283 }
284
285 removeVideo (id: number) {
286 return this.authHttp
287 .delete(VideoService.BASE_VIDEO_URL + id)
288 .pipe(
289 map(this.restExtractor.extractDataBool),
290 catchError(err => this.restExtractor.handleError(err))
291 )
292 }
293
294 loadCompleteDescription (descriptionPath: string) {
295 return this.authHttp
296 .get<{ description: string }>(environment.apiUrl + descriptionPath)
297 .pipe(
298 map(res => res.description),
299 catchError(err => this.restExtractor.handleError(err))
300 )
301 }
302
303 setVideoLike (id: number) {
304 return this.setVideoRate(id, 'like')
305 }
306
307 setVideoDislike (id: number) {
308 return this.setVideoRate(id, 'dislike')
309 }
310
311 unsetVideoLike (id: number) {
312 return this.setVideoRate(id, 'none')
313 }
314
315 getUserVideoRating (id: number) {
316 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
317
318 return this.authHttp.get<UserVideoRate>(url)
319 .pipe(catchError(err => this.restExtractor.handleError(err)))
320 }
321
322 extractVideos (result: ResultList<VideoServerModel>) {
323 return this.serverService.getServerLocale()
324 .pipe(
325 map(translations => {
326 const videosJson = result.data
327 const totalVideos = result.total
328 const videos: Video[] = []
329
330 for (const videoJson of videosJson) {
331 videos.push(new Video(videoJson, translations))
332 }
333
334 return { total: totalVideos, data: videos }
335 })
336 )
337 }
338
339 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
340 const base = [
341 {
342 id: VideoPrivacy.PRIVATE,
343 description: $localize`Only I can see this video`
344 },
345 {
346 id: VideoPrivacy.UNLISTED,
347 description: $localize`Only shareable via a private link`
348 },
349 {
350 id: VideoPrivacy.PUBLIC,
351 description: $localize`Anyone can see this video`
352 },
353 {
354 id: VideoPrivacy.INTERNAL,
355 description: $localize`Only users of this instance can see this video`
356 }
357 ]
358
359 return base
360 .filter(o => !!privacies.find(p => p.id === o.id)) // filter down to privacies that where in the input
361 .map(o => ({ ...privacies[o.id - 1], ...o })) // merge the input privacies that contain a label, and extend them with a description
362 }
363
364 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
365 return nsfwPolicy === 'do_not_list'
366 ? 'false'
367 : 'both'
368 }
369
370 private setVideoRate (id: number, rateType: UserVideoRateType) {
371 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
372 const body: UserVideoRateUpdate = {
373 rating: rateType
374 }
375
376 return this.authHttp
377 .put(url, body)
378 .pipe(
379 map(this.restExtractor.extractDataBool),
380 catchError(err => this.restExtractor.handleError(err))
381 )
382 }
383 }