]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.service.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { from, Observable, of } from 'rxjs'
3 import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { AuthService, ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core'
7 import { objectToFormData } from '@app/helpers'
8 import {
9 BooleanBothQuery,
10 FeedFormat,
11 NSFWPolicyType,
12 ResultList,
13 UserVideoRate,
14 UserVideoRateType,
15 UserVideoRateUpdate,
16 Video as VideoServerModel,
17 VideoChannel as VideoChannelServerModel,
18 VideoConstant,
19 VideoDetails as VideoDetailsServerModel,
20 VideoFileMetadata,
21 VideoInclude,
22 VideoPrivacy,
23 VideoSortField,
24 VideoTranscodingCreate,
25 VideoUpdate
26 } from '@shared/models'
27 import { VideoSource } from '@shared/models/videos/video-source'
28 import { environment } from '../../../../environments/environment'
29 import { Account } from '../account/account.model'
30 import { AccountService } from '../account/account.service'
31 import { VideoChannel, VideoChannelService } from '../video-channel'
32 import { VideoDetails } from './video-details.model'
33 import { VideoEdit } from './video-edit.model'
34 import { Video } from './video.model'
35
36 export type CommonVideoParams = {
37 videoPagination?: ComponentPaginationLight
38 sort: VideoSortField | SortMeta
39 include?: VideoInclude
40 isLocal?: boolean
41 categoryOneOf?: number[]
42 languageOneOf?: string[]
43 privacyOneOf?: VideoPrivacy[]
44 isLive?: boolean
45 skipCount?: boolean
46
47 // FIXME: remove?
48 nsfwPolicy?: NSFWPolicyType
49 nsfw?: BooleanBothQuery
50 }
51
52 @Injectable()
53 export class VideoService {
54 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos'
55 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
56 static BASE_SUBSCRIPTION_FEEDS_URL = environment.apiUrl + '/feeds/subscriptions.'
57
58 constructor (
59 private auth: AuthService,
60 private authHttp: HttpClient,
61 private restExtractor: RestExtractor,
62 private restService: RestService,
63 private serverService: ServerService
64 ) {}
65
66 getVideoViewUrl (uuid: string) {
67 return `${VideoService.BASE_VIDEO_URL}/${uuid}/views`
68 }
69
70 getVideo (options: { videoId: string }): Observable<VideoDetails> {
71 return this.serverService.getServerLocale()
72 .pipe(
73 switchMap(translations => {
74 return this.authHttp.get<VideoDetailsServerModel>(`${VideoService.BASE_VIDEO_URL}/${options.videoId}`)
75 .pipe(map(videoHash => ({ videoHash, translations })))
76 }),
77 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
78 catchError(err => this.restExtractor.handleError(err))
79 )
80 }
81
82 updateVideo (video: VideoEdit) {
83 const language = video.language || null
84 const licence = video.licence || null
85 const category = video.category || null
86 const description = video.description || null
87 const support = video.support || null
88 const scheduleUpdate = video.scheduleUpdate || null
89 const originallyPublishedAt = video.originallyPublishedAt || null
90
91 const body: VideoUpdate = {
92 name: video.name,
93 category,
94 licence,
95 language,
96 support,
97 description,
98 channelId: video.channelId,
99 privacy: video.privacy,
100 tags: video.tags,
101 nsfw: video.nsfw,
102 waitTranscoding: video.waitTranscoding,
103 commentsEnabled: video.commentsEnabled,
104 downloadEnabled: video.downloadEnabled,
105 thumbnailfile: video.thumbnailfile,
106 previewfile: video.previewfile,
107 pluginData: video.pluginData,
108 scheduleUpdate,
109 originallyPublishedAt
110 }
111
112 const data = objectToFormData(body)
113
114 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, data)
115 .pipe(catchError(err => this.restExtractor.handleError(err)))
116 }
117
118 uploadVideo (video: FormData) {
119 const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true })
120
121 return this.authHttp
122 .request<{ video: { id: number, uuid: string } }>(req)
123 .pipe(catchError(err => this.restExtractor.handleError(err)))
124 }
125
126 getMyVideos (options: {
127 videoPagination: ComponentPaginationLight
128 sort: VideoSortField
129 userChannels?: VideoChannelServerModel[]
130 search?: string
131 }): Observable<ResultList<Video>> {
132 const { videoPagination, sort, userChannels = [], search } = options
133
134 const pagination = this.restService.componentToRestPagination(videoPagination)
135
136 let params = new HttpParams()
137 params = this.restService.addRestGetParams(params, pagination, sort)
138
139 if (search) {
140 const filters = this.restService.parseQueryStringFilter(search, {
141 isLive: {
142 prefix: 'isLive:',
143 isBoolean: true
144 },
145 channelId: {
146 prefix: 'channel:',
147 handler: (name: string) => {
148 const channel = userChannels.find(c => c.name === name)
149
150 if (channel) return channel.id
151
152 return undefined
153 }
154 }
155 })
156
157 params = this.restService.addObjectParams(params, filters)
158 }
159
160 return this.authHttp
161 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
162 .pipe(
163 switchMap(res => this.extractVideos(res)),
164 catchError(err => this.restExtractor.handleError(err))
165 )
166 }
167
168 getAccountVideos (parameters: CommonVideoParams & {
169 account: Pick<Account, 'nameWithHost'>
170 search?: string
171 }): Observable<ResultList<Video>> {
172 const { account, search } = parameters
173
174 let params = new HttpParams()
175 params = this.buildCommonVideosParams({ params, ...parameters })
176
177 if (search) params = params.set('search', search)
178
179 return this.authHttp
180 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
181 .pipe(
182 switchMap(res => this.extractVideos(res)),
183 catchError(err => this.restExtractor.handleError(err))
184 )
185 }
186
187 getVideoChannelVideos (parameters: CommonVideoParams & {
188 videoChannel: Pick<VideoChannel, 'nameWithHost'>
189 }): Observable<ResultList<Video>> {
190 const { videoChannel } = parameters
191
192 let params = new HttpParams()
193 params = this.buildCommonVideosParams({ params, ...parameters })
194
195 return this.authHttp
196 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
197 .pipe(
198 switchMap(res => this.extractVideos(res)),
199 catchError(err => this.restExtractor.handleError(err))
200 )
201 }
202
203 getVideos (parameters: CommonVideoParams): Observable<ResultList<Video>> {
204 let params = new HttpParams()
205 params = this.buildCommonVideosParams({ params, ...parameters })
206
207 return this.authHttp
208 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
209 .pipe(
210 switchMap(res => this.extractVideos(res)),
211 catchError(err => this.restExtractor.handleError(err))
212 )
213 }
214
215 buildBaseFeedUrls (params: HttpParams, base = VideoService.BASE_FEEDS_URL) {
216 const feeds = [
217 {
218 format: FeedFormat.RSS,
219 label: 'media rss 2.0',
220 url: base + FeedFormat.RSS.toLowerCase()
221 },
222 {
223 format: FeedFormat.ATOM,
224 label: 'atom 1.0',
225 url: base + FeedFormat.ATOM.toLowerCase()
226 },
227 {
228 format: FeedFormat.JSON,
229 label: 'json 1.0',
230 url: base + FeedFormat.JSON.toLowerCase()
231 }
232 ]
233
234 if (params && params.keys().length !== 0) {
235 for (const feed of feeds) {
236 feed.url += '?' + params.toString()
237 }
238 }
239
240 return feeds
241 }
242
243 getVideoFeedUrls (sort: VideoSortField, isLocal: boolean, categoryOneOf?: number[]) {
244 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
245
246 if (isLocal) params = params.set('isLocal', isLocal)
247
248 if (categoryOneOf) {
249 for (const c of categoryOneOf) {
250 params = params.append('categoryOneOf[]', c + '')
251 }
252 }
253
254 return this.buildBaseFeedUrls(params)
255 }
256
257 getAccountFeedUrls (accountId: number) {
258 let params = this.restService.addRestGetParams(new HttpParams())
259 params = params.set('accountId', accountId.toString())
260
261 return this.buildBaseFeedUrls(params)
262 }
263
264 getVideoChannelFeedUrls (videoChannelId: number) {
265 let params = this.restService.addRestGetParams(new HttpParams())
266 params = params.set('videoChannelId', videoChannelId.toString())
267
268 return this.buildBaseFeedUrls(params)
269 }
270
271 getVideoSubscriptionFeedUrls (accountId: number, feedToken: string) {
272 let params = this.restService.addRestGetParams(new HttpParams())
273 params = params.set('accountId', accountId.toString())
274 params = params.set('token', feedToken)
275
276 return this.buildBaseFeedUrls(params, VideoService.BASE_SUBSCRIPTION_FEEDS_URL)
277 }
278
279 getVideoFileMetadata (metadataUrl: string) {
280 return this.authHttp
281 .get<VideoFileMetadata>(metadataUrl)
282 .pipe(
283 catchError(err => this.restExtractor.handleError(err))
284 )
285 }
286
287 removeVideo (idArg: number | number[]) {
288 const ids = Array.isArray(idArg) ? idArg : [ idArg ]
289
290 return from(ids)
291 .pipe(
292 concatMap(id => this.authHttp.delete(`${VideoService.BASE_VIDEO_URL}/${id}`)),
293 toArray(),
294 catchError(err => this.restExtractor.handleError(err))
295 )
296 }
297
298 removeVideoFiles (videoIds: (number | string)[], type: 'hls' | 'webtorrent') {
299 return from(videoIds)
300 .pipe(
301 concatMap(id => this.authHttp.delete(VideoService.BASE_VIDEO_URL + '/' + id + '/' + type)),
302 toArray(),
303 catchError(err => this.restExtractor.handleError(err))
304 )
305 }
306
307 runTranscoding (videoIds: (number | string)[], type: 'hls' | 'webtorrent') {
308 const body: VideoTranscodingCreate = { transcodingType: type }
309
310 return from(videoIds)
311 .pipe(
312 concatMap(id => this.authHttp.post(VideoService.BASE_VIDEO_URL + '/' + id + '/transcoding', body)),
313 toArray(),
314 catchError(err => this.restExtractor.handleError(err))
315 )
316 }
317
318 loadCompleteDescription (descriptionPath: string) {
319 return this.authHttp
320 .get<{ description: string }>(environment.apiUrl + descriptionPath)
321 .pipe(
322 map(res => res.description),
323 catchError(err => this.restExtractor.handleError(err))
324 )
325 }
326
327 getSource (videoId: number) {
328 return this.authHttp
329 .get<{ source: VideoSource }>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/source')
330 .pipe(
331 catchError(err => {
332 if (err.status === 404) {
333 return of(undefined)
334 }
335
336 this.restExtractor.handleError(err)
337 })
338 )
339 }
340
341 setVideoLike (id: string) {
342 return this.setVideoRate(id, 'like')
343 }
344
345 setVideoDislike (id: string) {
346 return this.setVideoRate(id, 'dislike')
347 }
348
349 unsetVideoLike (id: string) {
350 return this.setVideoRate(id, 'none')
351 }
352
353 getUserVideoRating (id: string) {
354 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
355
356 return this.authHttp.get<UserVideoRate>(url)
357 .pipe(catchError(err => this.restExtractor.handleError(err)))
358 }
359
360 extractVideos (result: ResultList<VideoServerModel>) {
361 return this.serverService.getServerLocale()
362 .pipe(
363 map(translations => {
364 const videosJson = result.data
365 const totalVideos = result.total
366 const videos: Video[] = []
367
368 for (const videoJson of videosJson) {
369 videos.push(new Video(videoJson, translations))
370 }
371
372 return { total: totalVideos, data: videos }
373 })
374 )
375 }
376
377 explainedPrivacyLabels (serverPrivacies: VideoConstant<VideoPrivacy>[], defaultPrivacyId = VideoPrivacy.PUBLIC) {
378 const descriptions = {
379 [VideoPrivacy.PRIVATE]: $localize`Only I can see this video`,
380 [VideoPrivacy.UNLISTED]: $localize`Only shareable via a private link`,
381 [VideoPrivacy.PUBLIC]: $localize`Anyone can see this video`,
382 [VideoPrivacy.INTERNAL]: $localize`Only users of this instance can see this video`
383 }
384
385 const videoPrivacies = serverPrivacies.map(p => {
386 return {
387 ...p,
388
389 description: descriptions[p.id]
390 }
391 })
392
393 return {
394 videoPrivacies,
395 defaultPrivacyId: serverPrivacies.find(p => p.id === defaultPrivacyId)?.id || serverPrivacies[0].id
396 }
397 }
398
399 getHighestAvailablePrivacy (serverPrivacies: VideoConstant<VideoPrivacy>[]) {
400 const order = [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL, VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC ]
401
402 for (const privacy of order) {
403 if (serverPrivacies.find(p => p.id === privacy)) {
404 return privacy
405 }
406 }
407
408 throw new Error('No highest privacy available')
409 }
410
411 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
412 return nsfwPolicy === 'do_not_list'
413 ? 'false'
414 : 'both'
415 }
416
417 buildCommonVideosParams (options: CommonVideoParams & { params: HttpParams }) {
418 const {
419 params,
420 videoPagination,
421 sort,
422 isLocal,
423 include,
424 categoryOneOf,
425 languageOneOf,
426 privacyOneOf,
427 skipCount,
428 nsfwPolicy,
429 isLive,
430 nsfw
431 } = options
432
433 const pagination = videoPagination
434 ? this.restService.componentToRestPagination(videoPagination)
435 : undefined
436
437 let newParams = this.restService.addRestGetParams(params, pagination, this.buildListSort(sort))
438
439 if (skipCount) newParams = newParams.set('skipCount', skipCount + '')
440
441 if (isLocal !== undefined) newParams = newParams.set('isLocal', isLocal)
442 if (include !== undefined) newParams = newParams.set('include', include)
443 if (isLive !== undefined) newParams = newParams.set('isLive', isLive)
444 if (nsfw !== undefined) newParams = newParams.set('nsfw', nsfw)
445 if (nsfwPolicy !== undefined) newParams = newParams.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
446 if (languageOneOf !== undefined) newParams = this.restService.addArrayParams(newParams, 'languageOneOf', languageOneOf)
447 if (categoryOneOf !== undefined) newParams = this.restService.addArrayParams(newParams, 'categoryOneOf', categoryOneOf)
448 if (privacyOneOf !== undefined) newParams = this.restService.addArrayParams(newParams, 'privacyOneOf', privacyOneOf)
449
450 return newParams
451 }
452
453 private buildListSort (sortArg: VideoSortField | SortMeta) {
454 const sort = this.restService.buildSortString(sortArg)
455
456 if (typeof sort === 'string') {
457 // Silently use the best algorithm for logged in users if they chose the hot algorithm
458 if (
459 this.auth.isLoggedIn() &&
460 (sort === 'hot' || sort === '-hot')
461 ) {
462 return sort.replace('hot', 'best')
463 }
464
465 return sort
466 }
467 }
468
469 private setVideoRate (id: string, rateType: UserVideoRateType) {
470 const url = `${VideoService.BASE_VIDEO_URL}/${id}/rate`
471 const body: UserVideoRateUpdate = {
472 rating: rateType
473 }
474
475 return this.authHttp
476 .put(url, body)
477 .pipe(catchError(err => this.restExtractor.handleError(err)))
478 }
479 }