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