]>
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, | |
bbe0f064 C |
86 | previewfile: video.previewfile, |
87 | scheduleUpdate: video.scheduleUpdate || undefined | |
df98563e | 88 | } |
c24ac1c1 | 89 | |
6de36768 C |
90 | const data = objectToFormData(body) |
91 | ||
92 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
db400f44 C |
93 | .pipe( |
94 | map(this.restExtractor.extractDataBool), | |
95 | catchError(this.restExtractor.handleError) | |
96 | ) | |
d8e689b8 C |
97 | } |
98 | ||
db7af09b | 99 | uploadVideo (video: FormData) { |
334ddfa4 | 100 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) |
bfb3a98f | 101 | |
fd45e8f4 | 102 | return this.authHttp |
2186386c | 103 | .request<{ video: { id: number, uuid: string } }>(req) |
db400f44 | 104 | .pipe(catchError(this.restExtractor.handleError)) |
bfb3a98f C |
105 | } |
106 | ||
2186386c | 107 | getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> { |
4635f59d | 108 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
cf20596c | 109 | |
d592e0a9 C |
110 | let params = new HttpParams() |
111 | params = this.restService.addRestGetParams(params, pagination, sort) | |
dc8bc31b | 112 | |
7ce44a74 C |
113 | return this.authHttp |
114 | .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params }) | |
db400f44 | 115 | .pipe( |
7ce44a74 | 116 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
117 | catchError(res => this.restExtractor.handleError(res)) |
118 | ) | |
dc8bc31b C |
119 | } |
120 | ||
0626e7af C |
121 | getAccountVideos ( |
122 | account: Account, | |
123 | videoPagination: ComponentPagination, | |
124 | sort: VideoSortField | |
2186386c | 125 | ): Observable<{ videos: Video[], totalVideos: number }> { |
0626e7af C |
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 | |
7ce44a74 | 132 | .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) |
db400f44 | 133 | .pipe( |
7ce44a74 | 134 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
135 | catchError(res => this.restExtractor.handleError(res)) |
136 | ) | |
0626e7af C |
137 | } |
138 | ||
170726f5 C |
139 | getVideoChannelVideos ( |
140 | videoChannel: VideoChannel, | |
141 | videoPagination: ComponentPagination, | |
142 | sort: VideoSortField | |
2186386c | 143 | ): Observable<{ videos: Video[], totalVideos: number }> { |
170726f5 C |
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 | |
7ce44a74 | 150 | .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params }) |
db400f44 | 151 | .pipe( |
7ce44a74 | 152 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
153 | catchError(res => this.restExtractor.handleError(res)) |
154 | ) | |
170726f5 C |
155 | } |
156 | ||
066e94c5 C |
157 | getVideos ( |
158 | videoPagination: ComponentPagination, | |
7b87d2d5 | 159 | sort: VideoSortField, |
066e94c5 | 160 | filter?: VideoFilter |
2186386c | 161 | ): Observable<{ videos: Video[], totalVideos: number }> { |
4635f59d | 162 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
163 | |
164 | let params = new HttpParams() | |
165 | params = this.restService.addRestGetParams(params, pagination, sort) | |
166 | ||
066e94c5 C |
167 | if (filter) { |
168 | params = params.set('filter', filter) | |
169 | } | |
170 | ||
fd45e8f4 | 171 | return this.authHttp |
7ce44a74 | 172 | .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params }) |
db400f44 | 173 | .pipe( |
7ce44a74 | 174 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
175 | catchError(res => this.restExtractor.handleError(res)) |
176 | ) | |
fd45e8f4 C |
177 | } |
178 | ||
7b87d2d5 | 179 | buildBaseFeedUrls (params: HttpParams) { |
cc1561f9 C |
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 | ||
7b87d2d5 C |
195 | if (params && params.keys().length !== 0) { |
196 | for (const feed of feeds) { | |
197 | feed.url += '?' + params.toString() | |
198 | } | |
199 | } | |
200 | ||
cc1561f9 | 201 | return feeds |
244e76a5 RK |
202 | } |
203 | ||
7b87d2d5 C |
204 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) { |
205 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) | |
244e76a5 | 206 | |
cc1561f9 C |
207 | if (filter) params = params.set('filter', filter) |
208 | ||
7b87d2d5 | 209 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
210 | } |
211 | ||
cc1561f9 | 212 | getAccountFeedUrls (accountId: number) { |
244e76a5 | 213 | let params = this.restService.addRestGetParams(new HttpParams()) |
244e76a5 | 214 | params = params.set('accountId', accountId.toString()) |
cc1561f9 | 215 | |
7b87d2d5 | 216 | return this.buildBaseFeedUrls(params) |
244e76a5 RK |
217 | } |
218 | ||
170726f5 C |
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 | ||
4635f59d C |
226 | searchVideos ( |
227 | search: string, | |
228 | videoPagination: ComponentPagination, | |
7b87d2d5 | 229 | sort: VideoSortField |
2186386c | 230 | ): Observable<{ videos: Video[], totalVideos: number }> { |
f3aaa9a9 | 231 | const url = VideoService.BASE_VIDEO_URL + 'search' |
d592e0a9 | 232 | |
4635f59d | 233 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
d592e0a9 C |
234 | |
235 | let params = new HttpParams() | |
236 | params = this.restService.addRestGetParams(params, pagination, sort) | |
f3aaa9a9 | 237 | params = params.append('search', search) |
cf20596c | 238 | |
fd45e8f4 | 239 | return this.authHttp |
db400f44 C |
240 | .get<ResultList<VideoServerModel>>(url, { params }) |
241 | .pipe( | |
7ce44a74 | 242 | switchMap(res => this.extractVideos(res)), |
db400f44 C |
243 | catchError(res => this.restExtractor.handleError(res)) |
244 | ) | |
d592e0a9 C |
245 | } |
246 | ||
247 | removeVideo (id: number) { | |
fd45e8f4 | 248 | return this.authHttp |
db400f44 C |
249 | .delete(VideoService.BASE_VIDEO_URL + id) |
250 | .pipe( | |
251 | map(this.restExtractor.extractDataBool), | |
252 | catchError(res => this.restExtractor.handleError(res)) | |
253 | ) | |
4fd8aa32 C |
254 | } |
255 | ||
2de96f4d C |
256 | loadCompleteDescription (descriptionPath: string) { |
257 | return this.authHttp | |
db400f44 C |
258 | .get(environment.apiUrl + descriptionPath) |
259 | .pipe( | |
260 | map(res => res[ 'description' ]), | |
261 | catchError(res => this.restExtractor.handleError(res)) | |
262 | ) | |
d38b8281 C |
263 | } |
264 | ||
0a6658fd | 265 | setVideoLike (id: number) { |
df98563e | 266 | return this.setVideoRate(id, 'like') |
d38b8281 C |
267 | } |
268 | ||
0a6658fd | 269 | setVideoDislike (id: number) { |
df98563e | 270 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
271 | } |
272 | ||
57a49263 BB |
273 | unsetVideoLike (id: number) { |
274 | return this.setVideoRate(id, 'none') | |
275 | } | |
276 | ||
5fcbd898 | 277 | getUserVideoRating (id: number) { |
334ddfa4 | 278 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 279 | |
5fcbd898 | 280 | return this.authHttp.get<UserVideoRate>(url) |
db400f44 | 281 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
d38b8281 C |
282 | } |
283 | ||
0a6658fd | 284 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 285 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 286 | const body: UserVideoRateUpdate = { |
d38b8281 | 287 | rating: rateType |
df98563e | 288 | } |
d38b8281 | 289 | |
fd45e8f4 | 290 | return this.authHttp |
db400f44 C |
291 | .put(url, body) |
292 | .pipe( | |
293 | map(this.restExtractor.extractDataBool), | |
294 | catchError(res => this.restExtractor.handleError(res)) | |
295 | ) | |
4f8c0eb0 C |
296 | } |
297 | ||
d592e0a9 | 298 | private extractVideos (result: ResultList<VideoServerModel>) { |
7ce44a74 | 299 | return this.serverService.localeObservable |
2186386c C |
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 | ) | |
501bc6c2 | 313 | } |
dc8bc31b | 314 | } |