]>
Commit | Line | Data |
---|---|---|
d4132d3f RK |
1 | import { Observable, of, throwError } from 'rxjs' |
2 | import { catchError, map, mergeMap, switchMap } from 'rxjs/operators' | |
3 | import { HttpClient, HttpErrorResponse, HttpParams, HttpRequest } from '@angular/common/http' | |
202f6b6c | 4 | import { Injectable } from '@angular/core' |
afff310e | 5 | import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService, AuthService } from '@app/core' |
67ed6552 | 6 | import { objectToFormData } from '@app/helpers' |
8cd7faaa | 7 | import { |
67ed6552 C |
8 | FeedFormat, |
9 | NSFWPolicyType, | |
10 | ResultList, | |
8cd7faaa | 11 | UserVideoRate, |
5c6d985f | 12 | UserVideoRateType, |
8cd7faaa | 13 | UserVideoRateUpdate, |
67ed6552 | 14 | Video as VideoServerModel, |
8cd7faaa | 15 | VideoConstant, |
67ed6552 | 16 | VideoDetails as VideoDetailsServerModel, |
66357162 | 17 | VideoFileMetadata, |
8cd7faaa C |
18 | VideoFilter, |
19 | VideoPrivacy, | |
67ed6552 | 20 | VideoSortField, |
5beb89f2 | 21 | VideoUpdate |
67ed6552 C |
22 | } from '@shared/models' |
23 | import { environment } from '../../../../environments/environment' | |
b4c3c51d C |
24 | import { Account } from '../account/account.model' |
25 | import { AccountService } from '../account/account.service' | |
67ed6552 | 26 | import { VideoChannel, VideoChannelService } from '../video-channel' |
404b54e1 C |
27 | import { VideoDetails } from './video-details.model' |
28 | import { VideoEdit } from './video-edit.model' | |
202f6b6c | 29 | import { Video } from './video.model' |
dc8bc31b | 30 | |
7f5f4152 | 31 | export interface VideosProvider { |
3caf77d3 | 32 | getVideos (parameters: { |
440d39c5 | 33 | videoPagination: ComponentPaginationLight, |
7f5f4152 BJ |
34 | sort: VideoSortField, |
35 | filter?: VideoFilter, | |
5c20a455 | 36 | categoryOneOf?: number[], |
3caf77d3 | 37 | languageOneOf?: string[] |
5c20a455 | 38 | nsfwPolicy: NSFWPolicyType |
93cae479 | 39 | }): Observable<ResultList<Video>> |
7f5f4152 BJ |
40 | } |
41 | ||
dc8bc31b | 42 | @Injectable() |
7f5f4152 | 43 | export class VideoService implements VideosProvider { |
40e87e9e C |
44 | static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' |
45 | static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.' | |
5beb89f2 | 46 | static BASE_SUBSCRIPTION_FEEDS_URL = environment.apiUrl + '/feeds/subscriptions.' |
dc8bc31b | 47 | |
df98563e | 48 | constructor ( |
d592e0a9 | 49 | private authHttp: HttpClient, |
de59c48f | 50 | private restExtractor: RestExtractor, |
7ce44a74 | 51 | private restService: RestService, |
5beb89f2 | 52 | private serverService: ServerService |
4fd8aa32 C |
53 | ) {} |
54 | ||
8cac1b64 C |
55 | getVideoViewUrl (uuid: string) { |
56 | return VideoService.BASE_VIDEO_URL + uuid + '/views' | |
57 | } | |
58 | ||
6e46de09 C |
59 | getUserWatchingVideoUrl (uuid: string) { |
60 | return VideoService.BASE_VIDEO_URL + uuid + '/watching' | |
61 | } | |
62 | ||
93cae479 | 63 | getVideo (options: { videoId: string }): Observable<VideoDetails> { |
ba430d75 | 64 | return this.serverService.getServerLocale() |
db400f44 | 65 | .pipe( |
7ce44a74 | 66 | switchMap(translations => { |
93cae479 | 67 | return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId) |
74b7c6d4 | 68 | .pipe(map(videoHash => ({ videoHash, translations }))) |
7ce44a74 C |
69 | }), |
70 | map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)), | |
e4f0e92e | 71 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 72 | ) |
4fd8aa32 | 73 | } |
dc8bc31b | 74 | |
404b54e1 | 75 | updateVideo (video: VideoEdit) { |
360329cc C |
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 | |
e94fc297 | 81 | const scheduleUpdate = video.scheduleUpdate || null |
1e74f19a | 82 | const originallyPublishedAt = video.originallyPublishedAt || null |
c24ac1c1 | 83 | |
4771e000 | 84 | const body: VideoUpdate = { |
d8e689b8 | 85 | name: video.name, |
cadb46d8 C |
86 | category, |
87 | licence, | |
c24ac1c1 | 88 | language, |
3bf1ec2e | 89 | support, |
cadb46d8 | 90 | description, |
0f320037 | 91 | channelId: video.channelId, |
fd45e8f4 | 92 | privacy: video.privacy, |
4771e000 | 93 | tags: video.tags, |
47564bbe | 94 | nsfw: video.nsfw, |
2186386c | 95 | waitTranscoding: video.waitTranscoding, |
6de36768 | 96 | commentsEnabled: video.commentsEnabled, |
7f2cfe3a | 97 | downloadEnabled: video.downloadEnabled, |
6de36768 | 98 | thumbnailfile: video.thumbnailfile, |
bbe0f064 | 99 | previewfile: video.previewfile, |
7294aab0 | 100 | pluginData: video.pluginData, |
1e74f19a | 101 | scheduleUpdate, |
102 | originallyPublishedAt | |
df98563e | 103 | } |
c24ac1c1 | 104 | |
6de36768 C |
105 | const data = objectToFormData(body) |
106 | ||
107 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
db400f44 C |
108 | .pipe( |
109 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 110 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 111 | ) |
d8e689b8 C |
112 | } |
113 | ||
db7af09b | 114 | uploadVideo (video: FormData) { |
334ddfa4 | 115 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) |
bfb3a98f | 116 | |
fd45e8f4 | 117 | return this.authHttp |
2186386c | 118 | .request<{ video: { id: number, uuid: string } }>(req) |
e4f0e92e | 119 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
bfb3a98f C |
120 | } |
121 | ||
440d39c5 | 122 | getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> { |
4635f59d | 123 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
cf20596c | 124 | |
d592e0a9 C |
125 | let params = new HttpParams() |
126 | params = this.restService.addRestGetParams(params, pagination, sort) | |
1fd61899 C |
127 | |
128 | if (search) { | |
129 | const filters = this.restService.parseQueryStringFilter(search, { | |
130 | isLive: { | |
131 | prefix: 'isLive:', | |
132 | isBoolean: true, | |
133 | handler: v => { | |
134 | if (v === 'true') return v | |
135 | if (v === 'false') return v | |
136 | ||
137 | return undefined | |
138 | } | |
139 | } | |
140 | }) | |
141 | ||
142 | params = this.restService.addObjectParams(params, filters) | |
143 | } | |
dc8bc31b | 144 | |
7ce44a74 | 145 | return this.authHttp |
bf64ed41 | 146 | .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params }) |
db400f44 | 147 | .pipe( |
7ce44a74 | 148 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 149 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 150 | ) |
dc8bc31b C |
151 | } |
152 | ||
0aa52e17 | 153 | getAccountVideos (parameters: { |
0626e7af | 154 | account: Account, |
440d39c5 | 155 | videoPagination: ComponentPaginationLight, |
0626e7af | 156 | sort: VideoSortField |
0aa52e17 C |
157 | nsfwPolicy?: NSFWPolicyType |
158 | videoFilter?: VideoFilter | |
37024082 | 159 | search?: string |
0aa52e17 | 160 | }): Observable<ResultList<Video>> { |
37024082 | 161 | const { account, videoPagination, sort, videoFilter, nsfwPolicy, search } = parameters |
0aa52e17 | 162 | |
0626e7af C |
163 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
164 | ||
165 | let params = new HttpParams() | |
166 | params = this.restService.addRestGetParams(params, pagination, sort) | |
167 | ||
0aa52e17 C |
168 | if (nsfwPolicy) { |
169 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
170 | } | |
171 | ||
172 | if (videoFilter) { | |
173 | params = params.set('filter', videoFilter) | |
174 | } | |
175 | ||
37024082 RK |
176 | if (search) { |
177 | params = params.set('search', search) | |
178 | } | |
179 | ||
0626e7af | 180 | return this.authHttp |
7ce44a74 | 181 | .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) |
db400f44 | 182 | .pipe( |
7ce44a74 | 183 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 184 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 185 | ) |
0626e7af C |
186 | } |
187 | ||
0aa52e17 | 188 | getVideoChannelVideos (parameters: { |
170726f5 | 189 | videoChannel: VideoChannel, |
440d39c5 | 190 | videoPagination: ComponentPaginationLight, |
5c20a455 C |
191 | sort: VideoSortField, |
192 | nsfwPolicy?: NSFWPolicyType | |
0aa52e17 C |
193 | videoFilter?: VideoFilter |
194 | }): Observable<ResultList<Video>> { | |
195 | const { videoChannel, videoPagination, sort, nsfwPolicy, videoFilter } = parameters | |
196 | ||
170726f5 C |
197 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
198 | ||
199 | let params = new HttpParams() | |
200 | params = this.restService.addRestGetParams(params, pagination, sort) | |
201 | ||
5c20a455 C |
202 | if (nsfwPolicy) { |
203 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
204 | } | |
205 | ||
0aa52e17 C |
206 | if (videoFilter) { |
207 | params = params.set('filter', videoFilter) | |
208 | } | |
209 | ||
170726f5 | 210 | return this.authHttp |
f5b0af50 | 211 | .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params }) |
db400f44 | 212 | .pipe( |
7ce44a74 | 213 | switchMap(res => this.extractVideos(res)), |
22a16e36 C |
214 | catchError(err => this.restExtractor.handleError(err)) |
215 | ) | |
216 | } | |
217 | ||
3caf77d3 | 218 | getVideos (parameters: { |
440d39c5 | 219 | videoPagination: ComponentPaginationLight, |
7b87d2d5 | 220 | sort: VideoSortField, |
61b909b9 | 221 | filter?: VideoFilter, |
5c20a455 | 222 | categoryOneOf?: number[], |
440d39c5 | 223 | languageOneOf?: string[], |
d3217560 | 224 | skipCount?: boolean, |
5c20a455 | 225 | nsfwPolicy?: NSFWPolicyType |
93cae479 | 226 | }): Observable<ResultList<Video>> { |
5c20a455 | 227 | const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters |
3caf77d3 | 228 | |
4635f59d | 229 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
230 | |
231 | let params = new HttpParams() | |
232 | params = this.restService.addRestGetParams(params, pagination, sort) | |
233 | ||
440d39c5 | 234 | if (filter) params = params.set('filter', filter) |
440d39c5 | 235 | if (skipCount) params = params.set('skipCount', skipCount + '') |
61b909b9 | 236 | |
5c20a455 C |
237 | if (nsfwPolicy) { |
238 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
d3217560 RK |
239 | } |
240 | ||
3caf77d3 C |
241 | if (languageOneOf) { |
242 | for (const l of languageOneOf) { | |
243 | params = params.append('languageOneOf[]', l) | |
244 | } | |
245 | } | |
246 | ||
5c20a455 C |
247 | if (categoryOneOf) { |
248 | for (const c of categoryOneOf) { | |
249 | params = params.append('categoryOneOf[]', c + '') | |
250 | } | |
251 | } | |
252 | ||
fd45e8f4 | 253 | return this.authHttp |
7ce44a74 | 254 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) |
db400f44 | 255 | .pipe( |
7ce44a74 | 256 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 257 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 258 | ) |
fd45e8f4 C |
259 | } |
260 | ||
5beb89f2 | 261 | buildBaseFeedUrls (params: HttpParams, base = VideoService.BASE_FEEDS_URL) { |
cc1561f9 C |
262 | const feeds = [ |
263 | { | |
39ba2e8e | 264 | format: FeedFormat.RSS, |
2d011d94 | 265 | label: 'media rss 2.0', |
5beb89f2 | 266 | url: base + FeedFormat.RSS.toLowerCase() |
cc1561f9 C |
267 | }, |
268 | { | |
39ba2e8e | 269 | format: FeedFormat.ATOM, |
cc1561f9 | 270 | label: 'atom 1.0', |
5beb89f2 | 271 | url: base + FeedFormat.ATOM.toLowerCase() |
cc1561f9 C |
272 | }, |
273 | { | |
39ba2e8e | 274 | format: FeedFormat.JSON, |
cc1561f9 | 275 | label: 'json 1.0', |
5beb89f2 | 276 | url: base + FeedFormat.JSON.toLowerCase() |
cc1561f9 C |
277 | } |
278 | ] | |
279 | ||
7b87d2d5 C |
280 | if (params && params.keys().length !== 0) { |
281 | for (const feed of feeds) { | |
282 | feed.url += '?' + params.toString() | |
283 | } | |
284 | } | |
285 | ||
cc1561f9 | 286 | return feeds |
244e76a5 RK |
287 | } |
288 | ||
5c20a455 | 289 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) { |
7b87d2d5 | 290 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) |
244e76a5 | 291 | |
cc1561f9 C |
292 | if (filter) params = params.set('filter', filter) |
293 | ||
5c20a455 C |
294 | if (categoryOneOf) { |
295 | for (const c of categoryOneOf) { | |
296 | params = params.append('categoryOneOf[]', c + '') | |
297 | } | |
298 | } | |
61b909b9 | 299 | |
7b87d2d5 | 300 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
301 | } |
302 | ||
cc1561f9 | 303 | getAccountFeedUrls (accountId: number) { |
244e76a5 | 304 | let params = this.restService.addRestGetParams(new HttpParams()) |
244e76a5 | 305 | params = params.set('accountId', accountId.toString()) |
cc1561f9 | 306 | |
7b87d2d5 | 307 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
308 | } |
309 | ||
170726f5 C |
310 | getVideoChannelFeedUrls (videoChannelId: number) { |
311 | let params = this.restService.addRestGetParams(new HttpParams()) | |
312 | params = params.set('videoChannelId', videoChannelId.toString()) | |
313 | ||
314 | return this.buildBaseFeedUrls(params) | |
315 | } | |
316 | ||
5beb89f2 | 317 | getVideoSubscriptionFeedUrls (accountId: number, feedToken: string) { |
afff310e RK |
318 | let params = this.restService.addRestGetParams(new HttpParams()) |
319 | params = params.set('accountId', accountId.toString()) | |
afff310e RK |
320 | params = params.set('token', feedToken) |
321 | ||
5beb89f2 | 322 | return this.buildBaseFeedUrls(params, VideoService.BASE_SUBSCRIPTION_FEEDS_URL) |
afff310e RK |
323 | } |
324 | ||
8319d6ae RK |
325 | getVideoFileMetadata (metadataUrl: string) { |
326 | return this.authHttp | |
583eb04b | 327 | .get<VideoFileMetadata>(metadataUrl) |
8319d6ae RK |
328 | .pipe( |
329 | catchError(err => this.restExtractor.handleError(err)) | |
330 | ) | |
331 | } | |
332 | ||
d592e0a9 | 333 | removeVideo (id: number) { |
fd45e8f4 | 334 | return this.authHttp |
db400f44 C |
335 | .delete(VideoService.BASE_VIDEO_URL + id) |
336 | .pipe( | |
337 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 338 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 339 | ) |
4fd8aa32 C |
340 | } |
341 | ||
2de96f4d C |
342 | loadCompleteDescription (descriptionPath: string) { |
343 | return this.authHttp | |
c199c427 | 344 | .get<{ description: string }>(environment.apiUrl + descriptionPath) |
db400f44 | 345 | .pipe( |
c199c427 | 346 | map(res => res.description), |
e4f0e92e | 347 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 348 | ) |
d38b8281 C |
349 | } |
350 | ||
0a6658fd | 351 | setVideoLike (id: number) { |
df98563e | 352 | return this.setVideoRate(id, 'like') |
d38b8281 C |
353 | } |
354 | ||
0a6658fd | 355 | setVideoDislike (id: number) { |
df98563e | 356 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
357 | } |
358 | ||
57a49263 BB |
359 | unsetVideoLike (id: number) { |
360 | return this.setVideoRate(id, 'none') | |
361 | } | |
362 | ||
5fcbd898 | 363 | getUserVideoRating (id: number) { |
334ddfa4 | 364 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 365 | |
5fcbd898 | 366 | return this.authHttp.get<UserVideoRate>(url) |
e4f0e92e | 367 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
d38b8281 C |
368 | } |
369 | ||
57c36b27 | 370 | extractVideos (result: ResultList<VideoServerModel>) { |
ba430d75 | 371 | return this.serverService.getServerLocale() |
2186386c C |
372 | .pipe( |
373 | map(translations => { | |
374 | const videosJson = result.data | |
375 | const totalVideos = result.total | |
376 | const videos: Video[] = [] | |
377 | ||
378 | for (const videoJson of videosJson) { | |
379 | videos.push(new Video(videoJson, translations)) | |
380 | } | |
381 | ||
93cae479 | 382 | return { total: totalVideos, data: videos } |
2186386c C |
383 | }) |
384 | ) | |
501bc6c2 | 385 | } |
57c36b27 | 386 | |
8cd7faaa | 387 | explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) { |
22a73cb8 C |
388 | const base = [ |
389 | { | |
390 | id: VideoPrivacy.PRIVATE, | |
66357162 | 391 | description: $localize`Only I can see this video` |
22a73cb8 C |
392 | }, |
393 | { | |
394 | id: VideoPrivacy.UNLISTED, | |
66357162 | 395 | description: $localize`Only shareable via a private link` |
22a73cb8 C |
396 | }, |
397 | { | |
398 | id: VideoPrivacy.PUBLIC, | |
66357162 | 399 | description: $localize`Anyone can see this video` |
22a73cb8 C |
400 | }, |
401 | { | |
402 | id: VideoPrivacy.INTERNAL, | |
66357162 | 403 | description: $localize`Only users of this instance can see this video` |
22a73cb8 C |
404 | } |
405 | ] | |
8cd7faaa | 406 | |
02c01341 RK |
407 | return base |
408 | .filter(o => !!privacies.find(p => p.id === o.id)) // filter down to privacies that where in the input | |
409 | .map(o => ({ ...privacies[o.id - 1], ...o })) // merge the input privacies that contain a label, and extend them with a description | |
8cd7faaa C |
410 | } |
411 | ||
5c20a455 C |
412 | nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) { |
413 | return nsfwPolicy === 'do_not_list' | |
414 | ? 'false' | |
415 | : 'both' | |
416 | } | |
417 | ||
5c6d985f | 418 | private setVideoRate (id: number, rateType: UserVideoRateType) { |
57c36b27 C |
419 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
420 | const body: UserVideoRateUpdate = { | |
421 | rating: rateType | |
422 | } | |
423 | ||
424 | return this.authHttp | |
425 | .put(url, body) | |
426 | .pipe( | |
427 | map(this.restExtractor.extractDataBool), | |
428 | catchError(err => this.restExtractor.handleError(err)) | |
429 | ) | |
430 | } | |
dc8bc31b | 431 | } |