]>
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, |
066e94c5 | 161 | filter?: VideoFilter |
2186386c | 162 | ): Observable<{ videos: Video[], totalVideos: number }> { |
4635f59d | 163 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
164 | |
165 | let params = new HttpParams() | |
166 | params = this.restService.addRestGetParams(params, pagination, sort) | |
167 | ||
066e94c5 C |
168 | if (filter) { |
169 | params = params.set('filter', filter) | |
170 | } | |
171 | ||
fd45e8f4 | 172 | return this.authHttp |
7ce44a74 | 173 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) |
db400f44 | 174 | .pipe( |
7ce44a74 | 175 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
176 | catchError(res => this.restExtractor.handleError(res)) |
177 | ) | |
fd45e8f4 C |
178 | } |
179 | ||
7b87d2d5 | 180 | buildBaseFeedUrls (params: HttpParams) { |
cc1561f9 C |
181 | const feeds = [ |
182 | { | |
183 | label: 'rss 2.0', | |
184 | url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase() | |
185 | }, | |
186 | { | |
187 | label: 'atom 1.0', | |
188 | url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase() | |
189 | }, | |
190 | { | |
191 | label: 'json 1.0', | |
192 | url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase() | |
193 | } | |
194 | ] | |
195 | ||
7b87d2d5 C |
196 | if (params && params.keys().length !== 0) { |
197 | for (const feed of feeds) { | |
198 | feed.url += '?' + params.toString() | |
199 | } | |
200 | } | |
201 | ||
cc1561f9 | 202 | return feeds |
244e76a5 RK |
203 | } |
204 | ||
7b87d2d5 C |
205 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) { |
206 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) | |
244e76a5 | 207 | |
cc1561f9 C |
208 | if (filter) params = params.set('filter', filter) |
209 | ||
7b87d2d5 | 210 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
211 | } |
212 | ||
cc1561f9 | 213 | getAccountFeedUrls (accountId: number) { |
244e76a5 | 214 | let params = this.restService.addRestGetParams(new HttpParams()) |
244e76a5 | 215 | params = params.set('accountId', accountId.toString()) |
cc1561f9 | 216 | |
7b87d2d5 | 217 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
218 | } |
219 | ||
170726f5 C |
220 | getVideoChannelFeedUrls (videoChannelId: number) { |
221 | let params = this.restService.addRestGetParams(new HttpParams()) | |
222 | params = params.set('videoChannelId', videoChannelId.toString()) | |
223 | ||
224 | return this.buildBaseFeedUrls(params) | |
225 | } | |
226 | ||
4635f59d C |
227 | searchVideos ( |
228 | search: string, | |
229 | videoPagination: ComponentPagination, | |
7b87d2d5 | 230 | sort: VideoSortField |
2186386c | 231 | ): Observable<{ videos: Video[], totalVideos: number }> { |
f3aaa9a9 | 232 | const url = VideoService.BASE_VIDEO_URL + 'search' |
d592e0a9 | 233 | |
4635f59d | 234 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
d592e0a9 C |
235 | |
236 | let params = new HttpParams() | |
237 | params = this.restService.addRestGetParams(params, pagination, sort) | |
f3aaa9a9 | 238 | params = params.append('search', search) |
cf20596c | 239 | |
fd45e8f4 | 240 | return this.authHttp |
db400f44 C |
241 | .get<ResultList<VideoServerModel>>(url, { params }) |
242 | .pipe( | |
7ce44a74 | 243 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
244 | catchError(res => this.restExtractor.handleError(res)) |
245 | ) | |
d592e0a9 C |
246 | } |
247 | ||
248 | removeVideo (id: number) { | |
fd45e8f4 | 249 | return this.authHttp |
db400f44 C |
250 | .delete(VideoService.BASE_VIDEO_URL + id) |
251 | .pipe( | |
252 | map(this.restExtractor.extractDataBool), | |
253 | catchError(res => this.restExtractor.handleError(res)) | |
254 | ) | |
4fd8aa32 C |
255 | } |
256 | ||
2de96f4d C |
257 | loadCompleteDescription (descriptionPath: string) { |
258 | return this.authHttp | |
db400f44 C |
259 | .get(environment.apiUrl + descriptionPath) |
260 | .pipe( | |
261 | map(res => res[ 'description' ]), | |
262 | catchError(res => this.restExtractor.handleError(res)) | |
263 | ) | |
d38b8281 C |
264 | } |
265 | ||
0a6658fd | 266 | setVideoLike (id: number) { |
df98563e | 267 | return this.setVideoRate(id, 'like') |
d38b8281 C |
268 | } |
269 | ||
0a6658fd | 270 | setVideoDislike (id: number) { |
df98563e | 271 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
272 | } |
273 | ||
57a49263 BB |
274 | unsetVideoLike (id: number) { |
275 | return this.setVideoRate(id, 'none') | |
276 | } | |
277 | ||
5fcbd898 | 278 | getUserVideoRating (id: number) { |
334ddfa4 | 279 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 280 | |
5fcbd898 | 281 | return this.authHttp.get<UserVideoRate>(url) |
db400f44 | 282 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
d38b8281 C |
283 | } |
284 | ||
0a6658fd | 285 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 286 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 287 | const body: UserVideoRateUpdate = { |
d38b8281 | 288 | rating: rateType |
df98563e | 289 | } |
d38b8281 | 290 | |
fd45e8f4 | 291 | return this.authHttp |
db400f44 C |
292 | .put(url, body) |
293 | .pipe( | |
294 | map(this.restExtractor.extractDataBool), | |
295 | catchError(res => this.restExtractor.handleError(res)) | |
296 | ) | |
4f8c0eb0 C |
297 | } |
298 | ||
d592e0a9 | 299 | private 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 | } |
dc8bc31b | 315 | } |