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