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