]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { FfprobeData } from 'fluent-ffmpeg' |
2 | import { Observable } from 'rxjs' | |
7ce44a74 | 3 | import { catchError, map, switchMap } from 'rxjs/operators' |
7a8032bb | 4 | import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' |
202f6b6c | 5 | import { Injectable } from '@angular/core' |
67ed6552 C |
6 | import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core' |
7 | import { objectToFormData } from '@app/helpers' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8cd7faaa | 9 | import { |
67ed6552 C |
10 | FeedFormat, |
11 | NSFWPolicyType, | |
12 | ResultList, | |
8cd7faaa | 13 | UserVideoRate, |
5c6d985f | 14 | UserVideoRateType, |
8cd7faaa | 15 | UserVideoRateUpdate, |
67ed6552 | 16 | Video as VideoServerModel, |
8cd7faaa | 17 | VideoConstant, |
67ed6552 | 18 | VideoDetails as VideoDetailsServerModel, |
8cd7faaa C |
19 | VideoFilter, |
20 | VideoPrivacy, | |
67ed6552 | 21 | VideoSortField, |
8cd7faaa | 22 | VideoUpdate |
67ed6552 C |
23 | } from '@shared/models' |
24 | import { environment } from '../../../../environments/environment' | |
25 | import { Account, AccountService } from '../account' | |
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.' | |
dc8bc31b | 46 | |
df98563e | 47 | constructor ( |
d592e0a9 | 48 | private authHttp: HttpClient, |
de59c48f | 49 | private restExtractor: RestExtractor, |
7ce44a74 | 50 | private restService: RestService, |
8cd7faaa C |
51 | private serverService: ServerService, |
52 | private i18n: I18n | |
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, |
1e74f19a | 100 | scheduleUpdate, |
101 | originallyPublishedAt | |
df98563e | 102 | } |
c24ac1c1 | 103 | |
6de36768 C |
104 | const data = objectToFormData(body) |
105 | ||
106 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
db400f44 C |
107 | .pipe( |
108 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 109 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 110 | ) |
d8e689b8 C |
111 | } |
112 | ||
db7af09b | 113 | uploadVideo (video: FormData) { |
334ddfa4 | 114 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) |
bfb3a98f | 115 | |
fd45e8f4 | 116 | return this.authHttp |
2186386c | 117 | .request<{ video: { id: number, uuid: string } }>(req) |
e4f0e92e | 118 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
bfb3a98f C |
119 | } |
120 | ||
440d39c5 | 121 | getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> { |
4635f59d | 122 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
cf20596c | 123 | |
d592e0a9 C |
124 | let params = new HttpParams() |
125 | params = this.restService.addRestGetParams(params, pagination, sort) | |
bf64ed41 | 126 | params = this.restService.addObjectParams(params, { search }) |
dc8bc31b | 127 | |
7ce44a74 | 128 | return this.authHttp |
bf64ed41 | 129 | .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params }) |
db400f44 | 130 | .pipe( |
7ce44a74 | 131 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 132 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 133 | ) |
dc8bc31b C |
134 | } |
135 | ||
0626e7af C |
136 | getAccountVideos ( |
137 | account: Account, | |
440d39c5 | 138 | videoPagination: ComponentPaginationLight, |
0626e7af | 139 | sort: VideoSortField |
93cae479 | 140 | ): Observable<ResultList<Video>> { |
0626e7af C |
141 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
142 | ||
143 | let params = new HttpParams() | |
144 | params = this.restService.addRestGetParams(params, pagination, sort) | |
145 | ||
146 | return this.authHttp | |
7ce44a74 | 147 | .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) |
db400f44 | 148 | .pipe( |
7ce44a74 | 149 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 150 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 151 | ) |
0626e7af C |
152 | } |
153 | ||
170726f5 C |
154 | getVideoChannelVideos ( |
155 | videoChannel: VideoChannel, | |
440d39c5 | 156 | videoPagination: ComponentPaginationLight, |
5c20a455 C |
157 | sort: VideoSortField, |
158 | nsfwPolicy?: NSFWPolicyType | |
93cae479 | 159 | ): Observable<ResultList<Video>> { |
170726f5 C |
160 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
161 | ||
162 | let params = new HttpParams() | |
163 | params = this.restService.addRestGetParams(params, pagination, sort) | |
164 | ||
5c20a455 C |
165 | if (nsfwPolicy) { |
166 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
167 | } | |
168 | ||
170726f5 | 169 | return this.authHttp |
f5b0af50 | 170 | .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params }) |
db400f44 | 171 | .pipe( |
7ce44a74 | 172 | switchMap(res => this.extractVideos(res)), |
22a16e36 C |
173 | catchError(err => this.restExtractor.handleError(err)) |
174 | ) | |
175 | } | |
176 | ||
3caf77d3 | 177 | getVideos (parameters: { |
440d39c5 | 178 | videoPagination: ComponentPaginationLight, |
7b87d2d5 | 179 | sort: VideoSortField, |
61b909b9 | 180 | filter?: VideoFilter, |
5c20a455 | 181 | categoryOneOf?: number[], |
440d39c5 | 182 | languageOneOf?: string[], |
d3217560 | 183 | skipCount?: boolean, |
5c20a455 | 184 | nsfwPolicy?: NSFWPolicyType |
93cae479 | 185 | }): Observable<ResultList<Video>> { |
5c20a455 | 186 | const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters |
3caf77d3 | 187 | |
4635f59d | 188 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
189 | |
190 | let params = new HttpParams() | |
191 | params = this.restService.addRestGetParams(params, pagination, sort) | |
192 | ||
440d39c5 | 193 | if (filter) params = params.set('filter', filter) |
440d39c5 | 194 | if (skipCount) params = params.set('skipCount', skipCount + '') |
61b909b9 | 195 | |
5c20a455 C |
196 | if (nsfwPolicy) { |
197 | params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy)) | |
d3217560 RK |
198 | } |
199 | ||
3caf77d3 C |
200 | if (languageOneOf) { |
201 | for (const l of languageOneOf) { | |
202 | params = params.append('languageOneOf[]', l) | |
203 | } | |
204 | } | |
205 | ||
5c20a455 C |
206 | if (categoryOneOf) { |
207 | for (const c of categoryOneOf) { | |
208 | params = params.append('categoryOneOf[]', c + '') | |
209 | } | |
210 | } | |
211 | ||
fd45e8f4 | 212 | return this.authHttp |
7ce44a74 | 213 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) |
db400f44 | 214 | .pipe( |
7ce44a74 | 215 | switchMap(res => this.extractVideos(res)), |
e4f0e92e | 216 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 217 | ) |
fd45e8f4 C |
218 | } |
219 | ||
7b87d2d5 | 220 | buildBaseFeedUrls (params: HttpParams) { |
cc1561f9 C |
221 | const feeds = [ |
222 | { | |
39ba2e8e | 223 | format: FeedFormat.RSS, |
2d011d94 | 224 | label: 'media rss 2.0', |
cc1561f9 C |
225 | url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase() |
226 | }, | |
227 | { | |
39ba2e8e | 228 | format: FeedFormat.ATOM, |
cc1561f9 C |
229 | label: 'atom 1.0', |
230 | url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase() | |
231 | }, | |
232 | { | |
39ba2e8e | 233 | format: FeedFormat.JSON, |
cc1561f9 C |
234 | label: 'json 1.0', |
235 | url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase() | |
236 | } | |
237 | ] | |
238 | ||
7b87d2d5 C |
239 | if (params && params.keys().length !== 0) { |
240 | for (const feed of feeds) { | |
241 | feed.url += '?' + params.toString() | |
242 | } | |
243 | } | |
244 | ||
cc1561f9 | 245 | return feeds |
244e76a5 RK |
246 | } |
247 | ||
5c20a455 | 248 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) { |
7b87d2d5 | 249 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) |
244e76a5 | 250 | |
cc1561f9 C |
251 | if (filter) params = params.set('filter', filter) |
252 | ||
5c20a455 C |
253 | if (categoryOneOf) { |
254 | for (const c of categoryOneOf) { | |
255 | params = params.append('categoryOneOf[]', c + '') | |
256 | } | |
257 | } | |
61b909b9 | 258 | |
7b87d2d5 | 259 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
260 | } |
261 | ||
cc1561f9 | 262 | getAccountFeedUrls (accountId: number) { |
244e76a5 | 263 | let params = this.restService.addRestGetParams(new HttpParams()) |
244e76a5 | 264 | params = params.set('accountId', accountId.toString()) |
cc1561f9 | 265 | |
7b87d2d5 | 266 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
267 | } |
268 | ||
170726f5 C |
269 | getVideoChannelFeedUrls (videoChannelId: number) { |
270 | let params = this.restService.addRestGetParams(new HttpParams()) | |
271 | params = params.set('videoChannelId', videoChannelId.toString()) | |
272 | ||
273 | return this.buildBaseFeedUrls(params) | |
274 | } | |
275 | ||
8319d6ae RK |
276 | getVideoFileMetadata (metadataUrl: string) { |
277 | return this.authHttp | |
278 | .get<FfprobeData>(metadataUrl) | |
279 | .pipe( | |
280 | catchError(err => this.restExtractor.handleError(err)) | |
281 | ) | |
282 | } | |
283 | ||
d592e0a9 | 284 | removeVideo (id: number) { |
fd45e8f4 | 285 | return this.authHttp |
db400f44 C |
286 | .delete(VideoService.BASE_VIDEO_URL + id) |
287 | .pipe( | |
288 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 289 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 290 | ) |
4fd8aa32 C |
291 | } |
292 | ||
2de96f4d C |
293 | loadCompleteDescription (descriptionPath: string) { |
294 | return this.authHttp | |
c199c427 | 295 | .get<{ description: string }>(environment.apiUrl + descriptionPath) |
db400f44 | 296 | .pipe( |
c199c427 | 297 | map(res => res.description), |
e4f0e92e | 298 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 299 | ) |
d38b8281 C |
300 | } |
301 | ||
0a6658fd | 302 | setVideoLike (id: number) { |
df98563e | 303 | return this.setVideoRate(id, 'like') |
d38b8281 C |
304 | } |
305 | ||
0a6658fd | 306 | setVideoDislike (id: number) { |
df98563e | 307 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
308 | } |
309 | ||
57a49263 BB |
310 | unsetVideoLike (id: number) { |
311 | return this.setVideoRate(id, 'none') | |
312 | } | |
313 | ||
5fcbd898 | 314 | getUserVideoRating (id: number) { |
334ddfa4 | 315 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 316 | |
5fcbd898 | 317 | return this.authHttp.get<UserVideoRate>(url) |
e4f0e92e | 318 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
d38b8281 C |
319 | } |
320 | ||
57c36b27 | 321 | extractVideos (result: ResultList<VideoServerModel>) { |
ba430d75 | 322 | return this.serverService.getServerLocale() |
2186386c C |
323 | .pipe( |
324 | map(translations => { | |
325 | const videosJson = result.data | |
326 | const totalVideos = result.total | |
327 | const videos: Video[] = [] | |
328 | ||
329 | for (const videoJson of videosJson) { | |
330 | videos.push(new Video(videoJson, translations)) | |
331 | } | |
332 | ||
93cae479 | 333 | return { total: totalVideos, data: videos } |
2186386c C |
334 | }) |
335 | ) | |
501bc6c2 | 336 | } |
57c36b27 | 337 | |
8cd7faaa | 338 | explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) { |
22a73cb8 C |
339 | const base = [ |
340 | { | |
341 | id: VideoPrivacy.PRIVATE, | |
342 | label: this.i18n('Only I can see this video') | |
343 | }, | |
344 | { | |
345 | id: VideoPrivacy.UNLISTED, | |
346 | label: this.i18n('Only people with the private link can see this video') | |
347 | }, | |
348 | { | |
349 | id: VideoPrivacy.PUBLIC, | |
350 | label: this.i18n('Anyone can see this video') | |
351 | }, | |
352 | { | |
353 | id: VideoPrivacy.INTERNAL, | |
354 | label: this.i18n('Only users of this instance can see this video') | |
355 | } | |
356 | ] | |
8cd7faaa | 357 | |
22a73cb8 | 358 | return base.filter(o => !!privacies.find(p => p.id === o.id)) |
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 | } |