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