]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map, switchMap } from 'rxjs/operators' | |
2 | import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { Observable } from 'rxjs' | |
5 | import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared' | |
6 | import { ResultList } from '../../../../../shared/models/result-list.model' | |
7 | import { | |
8 | UserVideoRate, | |
9 | UserVideoRateType, | |
10 | UserVideoRateUpdate, | |
11 | VideoConstant, | |
12 | VideoFilter, | |
13 | VideoPrivacy, | |
14 | VideoUpdate | |
15 | } from '../../../../../shared/models/videos' | |
16 | import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum' | |
17 | import { environment } from '../../../environments/environment' | |
18 | import { ComponentPagination } from '../rest/component-pagination.model' | |
19 | import { RestExtractor } from '../rest/rest-extractor.service' | |
20 | import { RestService } from '../rest/rest.service' | |
21 | import { UserService } from '../users/user.service' | |
22 | import { VideoSortField } from './sort-field.type' | |
23 | import { VideoDetails } from './video-details.model' | |
24 | import { VideoEdit } from './video-edit.model' | |
25 | import { Video } from './video.model' | |
26 | import { objectToFormData } from '@app/shared/misc/utils' | |
27 | import { Account } from '@app/shared/account/account.model' | |
28 | import { AccountService } from '@app/shared/account/account.service' | |
29 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
30 | import { ServerService } from '@app/core' | |
31 | import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service' | |
32 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
33 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
34 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
35 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
36 | ||
37 | export interface VideosProvider { | |
38 | getVideos (parameters: { | |
39 | videoPagination: ComponentPagination, | |
40 | sort: VideoSortField, | |
41 | filter?: VideoFilter, | |
42 | categoryOneOf?: number, | |
43 | languageOneOf?: string[] | |
44 | }): Observable<{ videos: Video[], totalVideos: number }> | |
45 | } | |
46 | ||
47 | @Injectable() | |
48 | export class VideoService implements VideosProvider { | |
49 | static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
50 | static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.' | |
51 | ||
52 | constructor ( | |
53 | private authHttp: HttpClient, | |
54 | private restExtractor: RestExtractor, | |
55 | private restService: RestService, | |
56 | private serverService: ServerService, | |
57 | private i18n: I18n | |
58 | ) {} | |
59 | ||
60 | getVideoViewUrl (uuid: string) { | |
61 | return VideoService.BASE_VIDEO_URL + uuid + '/views' | |
62 | } | |
63 | ||
64 | getUserWatchingVideoUrl (uuid: string) { | |
65 | return VideoService.BASE_VIDEO_URL + uuid + '/watching' | |
66 | } | |
67 | ||
68 | getVideo (uuid: string): Observable<VideoDetails> { | |
69 | return this.serverService.localeObservable | |
70 | .pipe( | |
71 | switchMap(translations => { | |
72 | return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid) | |
73 | .pipe(map(videoHash => ({ videoHash, translations }))) | |
74 | }), | |
75 | map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)), | |
76 | catchError(err => this.restExtractor.handleError(err)) | |
77 | ) | |
78 | } | |
79 | ||
80 | updateVideo (video: VideoEdit) { | |
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 | |
86 | const scheduleUpdate = video.scheduleUpdate || null | |
87 | const originallyPublishedAt = video.originallyPublishedAt || null | |
88 | ||
89 | const body: VideoUpdate = { | |
90 | name: video.name, | |
91 | category, | |
92 | licence, | |
93 | language, | |
94 | support, | |
95 | description, | |
96 | channelId: video.channelId, | |
97 | privacy: video.privacy, | |
98 | tags: video.tags, | |
99 | nsfw: video.nsfw, | |
100 | waitTranscoding: video.waitTranscoding, | |
101 | commentsEnabled: video.commentsEnabled, | |
102 | downloadEnabled: video.downloadEnabled, | |
103 | thumbnailfile: video.thumbnailfile, | |
104 | previewfile: video.previewfile, | |
105 | scheduleUpdate, | |
106 | originallyPublishedAt | |
107 | } | |
108 | ||
109 | const data = objectToFormData(body) | |
110 | ||
111 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
112 | .pipe( | |
113 | map(this.restExtractor.extractDataBool), | |
114 | catchError(err => this.restExtractor.handleError(err)) | |
115 | ) | |
116 | } | |
117 | ||
118 | uploadVideo (video: FormData) { | |
119 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) | |
120 | ||
121 | return this.authHttp | |
122 | .request<{ video: { id: number, uuid: string } }>(req) | |
123 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
124 | } | |
125 | ||
126 | getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> { | |
127 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
128 | ||
129 | let params = new HttpParams() | |
130 | params = this.restService.addRestGetParams(params, pagination, sort) | |
131 | ||
132 | return this.authHttp | |
133 | .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params }) | |
134 | .pipe( | |
135 | switchMap(res => this.extractVideos(res)), | |
136 | catchError(err => this.restExtractor.handleError(err)) | |
137 | ) | |
138 | } | |
139 | ||
140 | getAccountVideos ( | |
141 | account: Account, | |
142 | videoPagination: ComponentPagination, | |
143 | sort: VideoSortField | |
144 | ): Observable<{ videos: Video[], totalVideos: number }> { | |
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 | |
151 | .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) | |
152 | .pipe( | |
153 | switchMap(res => this.extractVideos(res)), | |
154 | catchError(err => this.restExtractor.handleError(err)) | |
155 | ) | |
156 | } | |
157 | ||
158 | getVideoChannelVideos ( | |
159 | videoChannel: VideoChannel, | |
160 | videoPagination: ComponentPagination, | |
161 | sort: VideoSortField | |
162 | ): Observable<{ videos: Video[], totalVideos: number }> { | |
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 | |
169 | .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params }) | |
170 | .pipe( | |
171 | switchMap(res => this.extractVideos(res)), | |
172 | catchError(err => this.restExtractor.handleError(err)) | |
173 | ) | |
174 | } | |
175 | ||
176 | getPlaylistVideos ( | |
177 | videoPlaylistId: number | string, | |
178 | videoPagination: ComponentPagination | |
179 | ): Observable<{ videos: Video[], totalVideos: number }> { | |
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 | ||
193 | getUserSubscriptionVideos ( | |
194 | videoPagination: ComponentPagination, | |
195 | sort: VideoSortField | |
196 | ): Observable<{ videos: Video[], totalVideos: number }> { | |
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 | ||
210 | getVideos (parameters: { | |
211 | videoPagination: ComponentPagination, | |
212 | sort: VideoSortField, | |
213 | filter?: VideoFilter, | |
214 | categoryOneOf?: number, | |
215 | languageOneOf?: string[] | |
216 | }): Observable<{ videos: Video[], totalVideos: number }> { | |
217 | const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters | |
218 | ||
219 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
220 | ||
221 | let params = new HttpParams() | |
222 | params = this.restService.addRestGetParams(params, pagination, sort) | |
223 | ||
224 | if (filter) { | |
225 | params = params.set('filter', filter) | |
226 | } | |
227 | ||
228 | if (categoryOneOf) { | |
229 | params = params.set('categoryOneOf', categoryOneOf + '') | |
230 | } | |
231 | ||
232 | if (languageOneOf) { | |
233 | for (const l of languageOneOf) { | |
234 | params = params.append('languageOneOf[]', l) | |
235 | } | |
236 | } | |
237 | ||
238 | return this.authHttp | |
239 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) | |
240 | .pipe( | |
241 | switchMap(res => this.extractVideos(res)), | |
242 | catchError(err => this.restExtractor.handleError(err)) | |
243 | ) | |
244 | } | |
245 | ||
246 | buildBaseFeedUrls (params: HttpParams) { | |
247 | const feeds = [ | |
248 | { | |
249 | format: FeedFormat.RSS, | |
250 | label: 'rss 2.0', | |
251 | url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase() | |
252 | }, | |
253 | { | |
254 | format: FeedFormat.ATOM, | |
255 | label: 'atom 1.0', | |
256 | url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase() | |
257 | }, | |
258 | { | |
259 | format: FeedFormat.JSON, | |
260 | label: 'json 1.0', | |
261 | url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase() | |
262 | } | |
263 | ] | |
264 | ||
265 | if (params && params.keys().length !== 0) { | |
266 | for (const feed of feeds) { | |
267 | feed.url += '?' + params.toString() | |
268 | } | |
269 | } | |
270 | ||
271 | return feeds | |
272 | } | |
273 | ||
274 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) { | |
275 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) | |
276 | ||
277 | if (filter) params = params.set('filter', filter) | |
278 | ||
279 | if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '') | |
280 | ||
281 | return this.buildBaseFeedUrls(params) | |
282 | } | |
283 | ||
284 | getAccountFeedUrls (accountId: number) { | |
285 | let params = this.restService.addRestGetParams(new HttpParams()) | |
286 | params = params.set('accountId', accountId.toString()) | |
287 | ||
288 | return this.buildBaseFeedUrls(params) | |
289 | } | |
290 | ||
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 | ||
298 | removeVideo (id: number) { | |
299 | return this.authHttp | |
300 | .delete(VideoService.BASE_VIDEO_URL + id) | |
301 | .pipe( | |
302 | map(this.restExtractor.extractDataBool), | |
303 | catchError(err => this.restExtractor.handleError(err)) | |
304 | ) | |
305 | } | |
306 | ||
307 | loadCompleteDescription (descriptionPath: string) { | |
308 | return this.authHttp | |
309 | .get<{ description: string }>(environment.apiUrl + descriptionPath) | |
310 | .pipe( | |
311 | map(res => res.description), | |
312 | catchError(err => this.restExtractor.handleError(err)) | |
313 | ) | |
314 | } | |
315 | ||
316 | setVideoLike (id: number) { | |
317 | return this.setVideoRate(id, 'like') | |
318 | } | |
319 | ||
320 | setVideoDislike (id: number) { | |
321 | return this.setVideoRate(id, 'dislike') | |
322 | } | |
323 | ||
324 | unsetVideoLike (id: number) { | |
325 | return this.setVideoRate(id, 'none') | |
326 | } | |
327 | ||
328 | getUserVideoRating (id: number) { | |
329 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' | |
330 | ||
331 | return this.authHttp.get<UserVideoRate>(url) | |
332 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
333 | } | |
334 | ||
335 | extractVideos (result: ResultList<VideoServerModel>) { | |
336 | return this.serverService.localeObservable | |
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 | ||
347 | return { videos, totalVideos } | |
348 | }) | |
349 | ) | |
350 | } | |
351 | ||
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 | ||
367 | private setVideoRate (id: number, rateType: UserVideoRateType) { | |
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 | } | |
380 | } |