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