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