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