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