]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Fix rxjs throw
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
CommitLineData
db400f44 1import { catchError, map } from 'rxjs/operators'
7a8032bb 2import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
202f6b6c 3import { Injectable } from '@angular/core'
db400f44 4import { Observable } from 'rxjs'
202f6b6c
C
5import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
6import { ResultList } from '../../../../../shared/models/result-list.model'
7import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
8import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
066e94c5 9import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
244e76a5 10import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
202f6b6c
C
11import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
12import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
63c4db6d 13import { environment } from '../../../environments/environment'
4635f59d 14import { ComponentPagination } from '../rest/component-pagination.model'
202f6b6c
C
15import { RestExtractor } from '../rest/rest-extractor.service'
16import { RestService } from '../rest/rest.service'
202f6b6c 17import { UserService } from '../users/user.service'
7b87d2d5 18import { VideoSortField } from './sort-field.type'
404b54e1
C
19import { VideoDetails } from './video-details.model'
20import { VideoEdit } from './video-edit.model'
202f6b6c 21import { Video } from './video.model'
6de36768 22import { objectToFormData } from '@app/shared/misc/utils'
0626e7af
C
23import { Account } from '@app/shared/account/account.model'
24import { AccountService } from '@app/shared/account/account.service'
170726f5
C
25import { VideoChannel } from '../../../../../shared/models/videos'
26import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
dc8bc31b
C
27
28@Injectable()
41a2aee3 29export class VideoService {
63c4db6d 30 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
244e76a5 31 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
dc8bc31b 32
df98563e 33 constructor (
d592e0a9 34 private authHttp: HttpClient,
de59c48f
C
35 private restExtractor: RestExtractor,
36 private restService: RestService
4fd8aa32
C
37 ) {}
38
8cac1b64
C
39 getVideoViewUrl (uuid: string) {
40 return VideoService.BASE_VIDEO_URL + uuid + '/views'
41 }
42
9d9597df 43 getVideo (uuid: string): Observable<VideoDetails> {
404b54e1 44 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
db400f44
C
45 .pipe(
46 map(videoHash => new VideoDetails(videoHash)),
47 catchError(res => this.restExtractor.handleError(res))
48 )
4fd8aa32 49 }
dc8bc31b 50
1f3e9fec 51 viewVideo (uuid: string): Observable<VideoDetails> {
8cac1b64 52 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
db400f44
C
53 .pipe(
54 map(this.restExtractor.extractDataBool),
55 catchError(this.restExtractor.handleError)
56 )
1f3e9fec
C
57 }
58
404b54e1 59 updateVideo (video: VideoEdit) {
360329cc
C
60 const language = video.language || null
61 const licence = video.licence || null
62 const category = video.category || null
63 const description = video.description || null
64 const support = video.support || null
c24ac1c1 65
4771e000 66 const body: VideoUpdate = {
d8e689b8 67 name: video.name,
cadb46d8
C
68 category,
69 licence,
c24ac1c1 70 language,
3bf1ec2e 71 support,
cadb46d8 72 description,
0f320037 73 channelId: video.channelId,
fd45e8f4 74 privacy: video.privacy,
4771e000 75 tags: video.tags,
47564bbe 76 nsfw: video.nsfw,
6de36768
C
77 commentsEnabled: video.commentsEnabled,
78 thumbnailfile: video.thumbnailfile,
79 previewfile: video.previewfile
df98563e 80 }
c24ac1c1 81
6de36768
C
82 const data = objectToFormData(body)
83
84 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
db400f44
C
85 .pipe(
86 map(this.restExtractor.extractDataBool),
87 catchError(this.restExtractor.handleError)
88 )
d8e689b8
C
89 }
90
db7af09b 91 uploadVideo (video: FormData) {
334ddfa4 92 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 93
fd45e8f4 94 return this.authHttp
db400f44
C
95 .request(req)
96 .pipe(catchError(this.restExtractor.handleError))
bfb3a98f
C
97 }
98
7b87d2d5 99 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
4635f59d 100 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 101
d592e0a9
C
102 let params = new HttpParams()
103 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 104
fd45e8f4 105 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
db400f44
C
106 .pipe(
107 map(this.extractVideos),
108 catchError(res => this.restExtractor.handleError(res))
109 )
dc8bc31b
C
110 }
111
0626e7af
C
112 getAccountVideos (
113 account: Account,
114 videoPagination: ComponentPagination,
115 sort: VideoSortField
116 ): Observable<{ videos: Video[], totalVideos: number}> {
117 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
118
119 let params = new HttpParams()
120 params = this.restService.addRestGetParams(params, pagination, sort)
121
122 return this.authHttp
123 .get(AccountService.BASE_ACCOUNT_URL + account.id + '/videos', { params })
db400f44
C
124 .pipe(
125 map(this.extractVideos),
126 catchError(res => this.restExtractor.handleError(res))
127 )
0626e7af
C
128 }
129
170726f5
C
130 getVideoChannelVideos (
131 videoChannel: VideoChannel,
132 videoPagination: ComponentPagination,
133 sort: VideoSortField
134 ): Observable<{ videos: Video[], totalVideos: number}> {
135 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
136
137 let params = new HttpParams()
138 params = this.restService.addRestGetParams(params, pagination, sort)
139
140 return this.authHttp
141 .get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
db400f44
C
142 .pipe(
143 map(this.extractVideos),
144 catchError(res => this.restExtractor.handleError(res))
145 )
170726f5
C
146 }
147
066e94c5
C
148 getVideos (
149 videoPagination: ComponentPagination,
7b87d2d5 150 sort: VideoSortField,
066e94c5
C
151 filter?: VideoFilter
152 ): Observable<{ videos: Video[], totalVideos: number}> {
4635f59d 153 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
154
155 let params = new HttpParams()
156 params = this.restService.addRestGetParams(params, pagination, sort)
157
066e94c5
C
158 if (filter) {
159 params = params.set('filter', filter)
160 }
161
fd45e8f4 162 return this.authHttp
db400f44
C
163 .get(VideoService.BASE_VIDEO_URL, { params })
164 .pipe(
165 map(this.extractVideos),
166 catchError(res => this.restExtractor.handleError(res))
167 )
fd45e8f4
C
168 }
169
7b87d2d5 170 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
171 const feeds = [
172 {
173 label: 'rss 2.0',
174 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
175 },
176 {
177 label: 'atom 1.0',
178 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
179 },
180 {
181 label: 'json 1.0',
182 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
183 }
184 ]
185
7b87d2d5
C
186 if (params && params.keys().length !== 0) {
187 for (const feed of feeds) {
188 feed.url += '?' + params.toString()
189 }
190 }
191
cc1561f9 192 return feeds
244e76a5
RK
193 }
194
7b87d2d5
C
195 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
196 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 197
cc1561f9
C
198 if (filter) params = params.set('filter', filter)
199
7b87d2d5 200 return this.buildBaseFeedUrls(params)
244e76a5
RK
201 }
202
cc1561f9 203 getAccountFeedUrls (accountId: number) {
244e76a5 204 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 205 params = params.set('accountId', accountId.toString())
cc1561f9 206
7b87d2d5 207 return this.buildBaseFeedUrls(params)
244e76a5
RK
208 }
209
170726f5
C
210 getVideoChannelFeedUrls (videoChannelId: number) {
211 let params = this.restService.addRestGetParams(new HttpParams())
212 params = params.set('videoChannelId', videoChannelId.toString())
213
214 return this.buildBaseFeedUrls(params)
215 }
216
4635f59d
C
217 searchVideos (
218 search: string,
219 videoPagination: ComponentPagination,
7b87d2d5 220 sort: VideoSortField
4635f59d 221 ): Observable<{ videos: Video[], totalVideos: number}> {
f3aaa9a9 222 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9 223
4635f59d 224 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
d592e0a9
C
225
226 let params = new HttpParams()
227 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 228 params = params.append('search', search)
cf20596c 229
fd45e8f4 230 return this.authHttp
db400f44
C
231 .get<ResultList<VideoServerModel>>(url, { params })
232 .pipe(
233 map(this.extractVideos),
234 catchError(res => this.restExtractor.handleError(res))
235 )
d592e0a9
C
236 }
237
238 removeVideo (id: number) {
fd45e8f4 239 return this.authHttp
db400f44
C
240 .delete(VideoService.BASE_VIDEO_URL + id)
241 .pipe(
242 map(this.restExtractor.extractDataBool),
243 catchError(res => this.restExtractor.handleError(res))
244 )
4fd8aa32
C
245 }
246
2de96f4d
C
247 loadCompleteDescription (descriptionPath: string) {
248 return this.authHttp
db400f44
C
249 .get(environment.apiUrl + descriptionPath)
250 .pipe(
251 map(res => res[ 'description' ]),
252 catchError(res => this.restExtractor.handleError(res))
253 )
d38b8281
C
254 }
255
0a6658fd 256 setVideoLike (id: number) {
df98563e 257 return this.setVideoRate(id, 'like')
d38b8281
C
258 }
259
0a6658fd 260 setVideoDislike (id: number) {
df98563e 261 return this.setVideoRate(id, 'dislike')
d38b8281
C
262 }
263
57a49263
BB
264 unsetVideoLike (id: number) {
265 return this.setVideoRate(id, 'none')
266 }
267
0a6658fd 268 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 269 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 270
fd45e8f4 271 return this.authHttp
db400f44
C
272 .get(url)
273 .pipe(catchError(res => this.restExtractor.handleError(res)))
d38b8281
C
274 }
275
0a6658fd 276 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 277 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 278 const body: UserVideoRateUpdate = {
d38b8281 279 rating: rateType
df98563e 280 }
d38b8281 281
fd45e8f4 282 return this.authHttp
db400f44
C
283 .put(url, body)
284 .pipe(
285 map(this.restExtractor.extractDataBool),
286 catchError(res => this.restExtractor.handleError(res))
287 )
4f8c0eb0
C
288 }
289
d592e0a9 290 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
291 const videosJson = result.data
292 const totalVideos = result.total
293 const videos = []
d592e0a9 294
de59c48f 295 for (const videoJson of videosJson) {
df98563e 296 videos.push(new Video(videoJson))
501bc6c2
C
297 }
298
df98563e 299 return { videos, totalVideos }
501bc6c2 300 }
dc8bc31b 301}