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