]>
Commit | Line | Data |
---|---|---|
29510651 RK |
1 | import { Observable } from 'rxjs' |
2 | import { catchError, map, switchMap } from 'rxjs/operators' | |
3 | import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' | |
202f6b6c | 4 | import { Injectable } from '@angular/core' |
29510651 | 5 | import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } 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:', | |
1a7d0887 | 132 | isBoolean: true |
1fd61899 C |
133 | } |
134 | }) | |
135 | ||
136 | params = this.restService.addObjectParams(params, filters) | |
137 | } | |
dc8bc31b | 138 | |
7ce44a74 | 139 | return this.authHttp |
bf64ed41 | 140 | .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params }) |
db400f44 | 141 | .pipe( |
7ce44a74 | 142 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 143 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 144 | ) |
dc8bc31b C |
145 | } |
146 | ||
0aa52e17 | 147 | getAccountVideos (parameters: { |
674d903b | 148 | account: Pick<Account, 'nameWithHost'>, |
440d39c5 | 149 | videoPagination: ComponentPaginationLight, |
0626e7af | 150 | sort: VideoSortField |
0aa52e17 C |
151 | nsfwPolicy?: NSFWPolicyType |
152 | videoFilter?: VideoFilter | |
37024082 | 153 | search?: string |
0aa52e17 | 154 | }): Observable<ResultList<Video>> { |
37024082 | 155 | const { account, videoPagination, sort, videoFilter, nsfwPolicy, search } = parameters |
0aa52e17 | 156 | |
0626e7af C |
157 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
158 | ||
159 | let params = new HttpParams() | |
160 | params = this.restService.addRestGetParams(params, pagination, sort) | |
161 | ||
0aa52e17 C |
162 | if (nsfwPolicy) { |
163 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
164 | } | |
165 | ||
166 | if (videoFilter) { | |
167 | params = params.set('filter', videoFilter) | |
168 | } | |
169 | ||
37024082 RK |
170 | if (search) { |
171 | params = params.set('search', search) | |
172 | } | |
173 | ||
0626e7af | 174 | return this.authHttp |
7ce44a74 | 175 | .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) |
db400f44 | 176 | .pipe( |
7ce44a74 | 177 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 178 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 179 | ) |
0626e7af C |
180 | } |
181 | ||
0aa52e17 | 182 | getVideoChannelVideos (parameters: { |
674d903b | 183 | videoChannel: Pick<VideoChannel, 'nameWithHost'>, |
440d39c5 | 184 | videoPagination: ComponentPaginationLight, |
5c20a455 C |
185 | sort: VideoSortField, |
186 | nsfwPolicy?: NSFWPolicyType | |
0aa52e17 C |
187 | videoFilter?: VideoFilter |
188 | }): Observable<ResultList<Video>> { | |
189 | const { videoChannel, videoPagination, sort, nsfwPolicy, videoFilter } = parameters | |
190 | ||
170726f5 C |
191 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
192 | ||
193 | let params = new HttpParams() | |
194 | params = this.restService.addRestGetParams(params, pagination, sort) | |
195 | ||
5c20a455 C |
196 | if (nsfwPolicy) { |
197 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
198 | } | |
199 | ||
0aa52e17 C |
200 | if (videoFilter) { |
201 | params = params.set('filter', videoFilter) | |
202 | } | |
203 | ||
170726f5 | 204 | return this.authHttp |
f5b0af50 | 205 | .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params }) |
db400f44 | 206 | .pipe( |
7ce44a74 | 207 | switchMap(res => this.extractVideos(res)), |
22a16e36 C |
208 | catchError(err => this.restExtractor.handleError(err)) |
209 | ) | |
210 | } | |
211 | ||
3caf77d3 | 212 | getVideos (parameters: { |
440d39c5 | 213 | videoPagination: ComponentPaginationLight, |
7b87d2d5 | 214 | sort: VideoSortField, |
61b909b9 | 215 | filter?: VideoFilter, |
5c20a455 | 216 | categoryOneOf?: number[], |
440d39c5 | 217 | languageOneOf?: string[], |
d3217560 | 218 | skipCount?: boolean, |
5c20a455 | 219 | nsfwPolicy?: NSFWPolicyType |
93cae479 | 220 | }): Observable<ResultList<Video>> { |
5c20a455 | 221 | const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters |
3caf77d3 | 222 | |
4635f59d | 223 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
224 | |
225 | let params = new HttpParams() | |
226 | params = this.restService.addRestGetParams(params, pagination, sort) | |
227 | ||
440d39c5 | 228 | if (filter) params = params.set('filter', filter) |
440d39c5 | 229 | if (skipCount) params = params.set('skipCount', skipCount + '') |
61b909b9 | 230 | |
5c20a455 C |
231 | if (nsfwPolicy) { |
232 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
d3217560 RK |
233 | } |
234 | ||
3caf77d3 C |
235 | if (languageOneOf) { |
236 | for (const l of languageOneOf) { | |
237 | params = params.append('languageOneOf[]', l) | |
238 | } | |
239 | } | |
240 | ||
5c20a455 C |
241 | if (categoryOneOf) { |
242 | for (const c of categoryOneOf) { | |
243 | params = params.append('categoryOneOf[]', c + '') | |
244 | } | |
245 | } | |
246 | ||
fd45e8f4 | 247 | return this.authHttp |
7ce44a74 | 248 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) |
db400f44 | 249 | .pipe( |
7ce44a74 | 250 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 251 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 252 | ) |
fd45e8f4 C |
253 | } |
254 | ||
5beb89f2 | 255 | buildBaseFeedUrls (params: HttpParams, base = VideoService.BASE_FEEDS_URL) { |
cc1561f9 C |
256 | const feeds = [ |
257 | { | |
39ba2e8e | 258 | format: FeedFormat.RSS, |
2d011d94 | 259 | label: 'media rss 2.0', |
5beb89f2 | 260 | url: base + FeedFormat.RSS.toLowerCase() |
cc1561f9 C |
261 | }, |
262 | { | |
39ba2e8e | 263 | format: FeedFormat.ATOM, |
cc1561f9 | 264 | label: 'atom 1.0', |
5beb89f2 | 265 | url: base + FeedFormat.ATOM.toLowerCase() |
cc1561f9 C |
266 | }, |
267 | { | |
39ba2e8e | 268 | format: FeedFormat.JSON, |
cc1561f9 | 269 | label: 'json 1.0', |
5beb89f2 | 270 | url: base + FeedFormat.JSON.toLowerCase() |
cc1561f9 C |
271 | } |
272 | ] | |
273 | ||
7b87d2d5 C |
274 | if (params && params.keys().length !== 0) { |
275 | for (const feed of feeds) { | |
276 | feed.url += '?' + params.toString() | |
277 | } | |
278 | } | |
279 | ||
cc1561f9 | 280 | return feeds |
244e76a5 RK |
281 | } |
282 | ||
5c20a455 | 283 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) { |
7b87d2d5 | 284 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) |
244e76a5 | 285 | |
cc1561f9 C |
286 | if (filter) params = params.set('filter', filter) |
287 | ||
5c20a455 C |
288 | if (categoryOneOf) { |
289 | for (const c of categoryOneOf) { | |
290 | params = params.append('categoryOneOf[]', c + '') | |
291 | } | |
292 | } | |
61b909b9 | 293 | |
7b87d2d5 | 294 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
295 | } |
296 | ||
cc1561f9 | 297 | getAccountFeedUrls (accountId: number) { |
244e76a5 | 298 | let params = this.restService.addRestGetParams(new HttpParams()) |
244e76a5 | 299 | params = params.set('accountId', accountId.toString()) |
cc1561f9 | 300 | |
7b87d2d5 | 301 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
302 | } |
303 | ||
170726f5 C |
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 | ||
5beb89f2 | 311 | getVideoSubscriptionFeedUrls (accountId: number, feedToken: string) { |
afff310e RK |
312 | let params = this.restService.addRestGetParams(new HttpParams()) |
313 | params = params.set('accountId', accountId.toString()) | |
afff310e RK |
314 | params = params.set('token', feedToken) |
315 | ||
5beb89f2 | 316 | return this.buildBaseFeedUrls(params, VideoService.BASE_SUBSCRIPTION_FEEDS_URL) |
afff310e RK |
317 | } |
318 | ||
8319d6ae RK |
319 | getVideoFileMetadata (metadataUrl: string) { |
320 | return this.authHttp | |
583eb04b | 321 | .get<VideoFileMetadata>(metadataUrl) |
8319d6ae RK |
322 | .pipe( |
323 | catchError(err => this.restExtractor.handleError(err)) | |
324 | ) | |
325 | } | |
326 | ||
d592e0a9 | 327 | removeVideo (id: number) { |
fd45e8f4 | 328 | return this.authHttp |
db400f44 C |
329 | .delete(VideoService.BASE_VIDEO_URL + id) |
330 | .pipe( | |
331 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 332 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 333 | ) |
4fd8aa32 C |
334 | } |
335 | ||
2de96f4d C |
336 | loadCompleteDescription (descriptionPath: string) { |
337 | return this.authHttp | |
c199c427 | 338 | .get<{ description: string }>(environment.apiUrl + descriptionPath) |
db400f44 | 339 | .pipe( |
c199c427 | 340 | map(res => res.description), |
e4f0e92e | 341 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 342 | ) |
d38b8281 C |
343 | } |
344 | ||
0a6658fd | 345 | setVideoLike (id: number) { |
df98563e | 346 | return this.setVideoRate(id, 'like') |
d38b8281 C |
347 | } |
348 | ||
0a6658fd | 349 | setVideoDislike (id: number) { |
df98563e | 350 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
351 | } |
352 | ||
57a49263 BB |
353 | unsetVideoLike (id: number) { |
354 | return this.setVideoRate(id, 'none') | |
355 | } | |
356 | ||
5fcbd898 | 357 | getUserVideoRating (id: number) { |
334ddfa4 | 358 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 359 | |
5fcbd898 | 360 | return this.authHttp.get<UserVideoRate>(url) |
e4f0e92e | 361 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
d38b8281 C |
362 | } |
363 | ||
57c36b27 | 364 | extractVideos (result: ResultList<VideoServerModel>) { |
ba430d75 | 365 | return this.serverService.getServerLocale() |
2186386c C |
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 | ||
93cae479 | 376 | return { total: totalVideos, data: videos } |
2186386c C |
377 | }) |
378 | ) | |
501bc6c2 | 379 | } |
57c36b27 | 380 | |
b980bcff C |
381 | explainedPrivacyLabels (serverPrivacies: VideoConstant<VideoPrivacy>[], defaultPrivacyId = VideoPrivacy.PUBLIC) { |
382 | const descriptions = [ | |
22a73cb8 C |
383 | { |
384 | id: VideoPrivacy.PRIVATE, | |
66357162 | 385 | description: $localize`Only I can see this video` |
22a73cb8 C |
386 | }, |
387 | { | |
388 | id: VideoPrivacy.UNLISTED, | |
66357162 | 389 | description: $localize`Only shareable via a private link` |
22a73cb8 C |
390 | }, |
391 | { | |
392 | id: VideoPrivacy.PUBLIC, | |
66357162 | 393 | description: $localize`Anyone can see this video` |
22a73cb8 C |
394 | }, |
395 | { | |
396 | id: VideoPrivacy.INTERNAL, | |
66357162 | 397 | description: $localize`Only users of this instance can see this video` |
22a73cb8 C |
398 | } |
399 | ] | |
8cd7faaa | 400 | |
29510651 | 401 | return { |
b980bcff C |
402 | defaultPrivacyId: serverPrivacies.find(p => p.id === defaultPrivacyId)?.id || serverPrivacies[0].id, |
403 | videoPrivacies: serverPrivacies.map(p => ({ ...p, description: descriptions.find(p => p.id).description })) | |
29510651 | 404 | } |
8cd7faaa C |
405 | } |
406 | ||
a3f45a2a C |
407 | getHighestAvailablePrivacy (serverPrivacies: VideoConstant<VideoPrivacy>[]) { |
408 | const order = [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL, VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC ] | |
409 | ||
410 | for (const privacy of order) { | |
411 | if (serverPrivacies.find(p => p.id === privacy)) { | |
412 | return privacy | |
413 | } | |
414 | } | |
415 | ||
416 | throw new Error('No highest privacy available') | |
417 | } | |
418 | ||
5c20a455 C |
419 | nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) { |
420 | return nsfwPolicy === 'do_not_list' | |
421 | ? 'false' | |
422 | : 'both' | |
423 | } | |
424 | ||
5c6d985f | 425 | private setVideoRate (id: number, rateType: UserVideoRateType) { |
57c36b27 C |
426 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
427 | const body: UserVideoRateUpdate = { | |
428 | rating: rateType | |
429 | } | |
430 | ||
431 | return this.authHttp | |
432 | .put(url, body) | |
433 | .pipe( | |
434 | map(this.restExtractor.extractDataBool), | |
435 | catchError(err => this.restExtractor.handleError(err)) | |
436 | ) | |
437 | } | |
dc8bc31b | 438 | } |