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