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