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