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