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