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