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