]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Add concept of video state, and add ability to wait transcoding before
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
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'
9 import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
10 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
11 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
12 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
13 import { environment } from '../../../environments/environment'
14 import { ComponentPagination } from '../rest/component-pagination.model'
15 import { RestExtractor } from '../rest/rest-extractor.service'
16 import { RestService } from '../rest/rest.service'
17 import { UserService } from '../users/user.service'
18 import { VideoSortField } from './sort-field.type'
19 import { VideoDetails } from './video-details.model'
20 import { VideoEdit } from './video-edit.model'
21 import { Video } from './video.model'
22 import { objectToFormData } from '@app/shared/misc/utils'
23 import { Account } from '@app/shared/account/account.model'
24 import { AccountService } from '@app/shared/account/account.service'
25 import { VideoChannel } from '../../../../../shared/models/videos'
26 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
27 import { ServerService } from '@app/core'
28
29 @Injectable()
30 export class VideoService {
31 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
32 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
33
34 constructor (
35 private authHttp: HttpClient,
36 private restExtractor: RestExtractor,
37 private restService: RestService,
38 private serverService: ServerService
39 ) {}
40
41 getVideoViewUrl (uuid: string) {
42 return VideoService.BASE_VIDEO_URL + uuid + '/views'
43 }
44
45 getVideo (uuid: string): Observable<VideoDetails> {
46 return this.serverService.localeObservable
47 .pipe(
48 switchMap(translations => {
49 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
50 .pipe(map(videoHash => ({ videoHash, translations })))
51 }),
52 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
53 catchError(res => this.restExtractor.handleError(res))
54 )
55 }
56
57 viewVideo (uuid: string): Observable<boolean> {
58 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
59 .pipe(
60 map(this.restExtractor.extractDataBool),
61 catchError(this.restExtractor.handleError)
62 )
63 }
64
65 updateVideo (video: VideoEdit) {
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
71
72 const body: VideoUpdate = {
73 name: video.name,
74 category,
75 licence,
76 language,
77 support,
78 description,
79 channelId: video.channelId,
80 privacy: video.privacy,
81 tags: video.tags,
82 nsfw: video.nsfw,
83 waitTranscoding: video.waitTranscoding,
84 commentsEnabled: video.commentsEnabled,
85 thumbnailfile: video.thumbnailfile,
86 previewfile: video.previewfile
87 }
88
89 const data = objectToFormData(body)
90
91 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
92 .pipe(
93 map(this.restExtractor.extractDataBool),
94 catchError(this.restExtractor.handleError)
95 )
96 }
97
98 uploadVideo (video: FormData) {
99 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
100
101 return this.authHttp
102 .request<{ video: { id: number, uuid: string } }>(req)
103 .pipe(catchError(this.restExtractor.handleError))
104 }
105
106 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
107 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
108
109 let params = new HttpParams()
110 params = this.restService.addRestGetParams(params, pagination, sort)
111
112 return this.authHttp
113 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
114 .pipe(
115 switchMap(res => this.extractVideos(res)),
116 catchError(res => this.restExtractor.handleError(res))
117 )
118 }
119
120 getAccountVideos (
121 account: Account,
122 videoPagination: ComponentPagination,
123 sort: VideoSortField
124 ): Observable<{ videos: Video[], totalVideos: number }> {
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
131 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
132 .pipe(
133 switchMap(res => this.extractVideos(res)),
134 catchError(res => this.restExtractor.handleError(res))
135 )
136 }
137
138 getVideoChannelVideos (
139 videoChannel: VideoChannel,
140 videoPagination: ComponentPagination,
141 sort: VideoSortField
142 ): Observable<{ videos: Video[], totalVideos: number }> {
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
149 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
150 .pipe(
151 switchMap(res => this.extractVideos(res)),
152 catchError(res => this.restExtractor.handleError(res))
153 )
154 }
155
156 getVideos (
157 videoPagination: ComponentPagination,
158 sort: VideoSortField,
159 filter?: VideoFilter
160 ): Observable<{ videos: Video[], totalVideos: number }> {
161 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
162
163 let params = new HttpParams()
164 params = this.restService.addRestGetParams(params, pagination, sort)
165
166 if (filter) {
167 params = params.set('filter', filter)
168 }
169
170 return this.authHttp
171 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
172 .pipe(
173 switchMap(res => this.extractVideos(res)),
174 catchError(res => this.restExtractor.handleError(res))
175 )
176 }
177
178 buildBaseFeedUrls (params: HttpParams) {
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
194 if (params && params.keys().length !== 0) {
195 for (const feed of feeds) {
196 feed.url += '?' + params.toString()
197 }
198 }
199
200 return feeds
201 }
202
203 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
204 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
205
206 if (filter) params = params.set('filter', filter)
207
208 return this.buildBaseFeedUrls(params)
209 }
210
211 getAccountFeedUrls (accountId: number) {
212 let params = this.restService.addRestGetParams(new HttpParams())
213 params = params.set('accountId', accountId.toString())
214
215 return this.buildBaseFeedUrls(params)
216 }
217
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
225 searchVideos (
226 search: string,
227 videoPagination: ComponentPagination,
228 sort: VideoSortField
229 ): Observable<{ videos: Video[], totalVideos: number }> {
230 const url = VideoService.BASE_VIDEO_URL + 'search'
231
232 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
233
234 let params = new HttpParams()
235 params = this.restService.addRestGetParams(params, pagination, sort)
236 params = params.append('search', search)
237
238 return this.authHttp
239 .get<ResultList<VideoServerModel>>(url, { params })
240 .pipe(
241 switchMap(res => this.extractVideos(res)),
242 catchError(res => this.restExtractor.handleError(res))
243 )
244 }
245
246 removeVideo (id: number) {
247 return this.authHttp
248 .delete(VideoService.BASE_VIDEO_URL + id)
249 .pipe(
250 map(this.restExtractor.extractDataBool),
251 catchError(res => this.restExtractor.handleError(res))
252 )
253 }
254
255 loadCompleteDescription (descriptionPath: string) {
256 return this.authHttp
257 .get(environment.apiUrl + descriptionPath)
258 .pipe(
259 map(res => res[ 'description' ]),
260 catchError(res => this.restExtractor.handleError(res))
261 )
262 }
263
264 setVideoLike (id: number) {
265 return this.setVideoRate(id, 'like')
266 }
267
268 setVideoDislike (id: number) {
269 return this.setVideoRate(id, 'dislike')
270 }
271
272 unsetVideoLike (id: number) {
273 return this.setVideoRate(id, 'none')
274 }
275
276 getUserVideoRating (id: number) {
277 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
278
279 return this.authHttp.get<UserVideoRate>(url)
280 .pipe(catchError(res => this.restExtractor.handleError(res)))
281 }
282
283 private setVideoRate (id: number, rateType: VideoRateType) {
284 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
285 const body: UserVideoRateUpdate = {
286 rating: rateType
287 }
288
289 return this.authHttp
290 .put(url, body)
291 .pipe(
292 map(this.restExtractor.extractDataBool),
293 catchError(res => this.restExtractor.handleError(res))
294 )
295 }
296
297 private extractVideos (result: ResultList<VideoServerModel>) {
298 return this.serverService.localeObservable
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 )
312 }
313 }