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