]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Check activities host
[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 {
8 UserVideoRate,
9 UserVideoRateType,
10 UserVideoRateUpdate,
11 VideoConstant,
12 VideoFilter,
13 VideoPrivacy,
14 VideoUpdate
15 } from '../../../../../shared/models/videos'
16 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
17 import { environment } from '../../../environments/environment'
18 import { ComponentPagination } from '../rest/component-pagination.model'
19 import { RestExtractor } from '../rest/rest-extractor.service'
20 import { RestService } from '../rest/rest.service'
21 import { UserService } from '../users/user.service'
22 import { VideoSortField } from './sort-field.type'
23 import { VideoDetails } from './video-details.model'
24 import { VideoEdit } from './video-edit.model'
25 import { Video } from './video.model'
26 import { objectToFormData } from '@app/shared/misc/utils'
27 import { Account } from '@app/shared/account/account.model'
28 import { AccountService } from '@app/shared/account/account.service'
29 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
30 import { ServerService } from '@app/core'
31 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
32 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
33 import { I18n } from '@ngx-translate/i18n-polyfill'
34
35 export interface VideosProvider {
36 getVideos (
37 videoPagination: ComponentPagination,
38 sort: VideoSortField,
39 filter?: VideoFilter,
40 categoryOneOf?: number
41 ): Observable<{ videos: Video[], totalVideos: number }>
42 }
43
44 @Injectable()
45 export class VideoService implements VideosProvider {
46 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
47 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
48
49 constructor (
50 private authHttp: HttpClient,
51 private restExtractor: RestExtractor,
52 private restService: RestService,
53 private serverService: ServerService,
54 private i18n: I18n
55 ) {}
56
57 getVideoViewUrl (uuid: string) {
58 return VideoService.BASE_VIDEO_URL + uuid + '/views'
59 }
60
61 getUserWatchingVideoUrl (uuid: string) {
62 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
63 }
64
65 getVideo (uuid: string): Observable<VideoDetails> {
66 return this.serverService.localeObservable
67 .pipe(
68 switchMap(translations => {
69 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
70 .pipe(map(videoHash => ({ videoHash, translations })))
71 }),
72 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
73 catchError(err => this.restExtractor.handleError(err))
74 )
75 }
76
77 updateVideo (video: VideoEdit) {
78 const language = video.language || null
79 const licence = video.licence || null
80 const category = video.category || null
81 const description = video.description || null
82 const support = video.support || null
83 const scheduleUpdate = video.scheduleUpdate || null
84
85 const body: VideoUpdate = {
86 name: video.name,
87 category,
88 licence,
89 language,
90 support,
91 description,
92 channelId: video.channelId,
93 privacy: video.privacy,
94 tags: video.tags,
95 nsfw: video.nsfw,
96 waitTranscoding: video.waitTranscoding,
97 commentsEnabled: video.commentsEnabled,
98 thumbnailfile: video.thumbnailfile,
99 previewfile: video.previewfile,
100 scheduleUpdate
101 }
102
103 const data = objectToFormData(body)
104
105 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
106 .pipe(
107 map(this.restExtractor.extractDataBool),
108 catchError(err => this.restExtractor.handleError(err))
109 )
110 }
111
112 uploadVideo (video: FormData) {
113 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
114
115 return this.authHttp
116 .request<{ video: { id: number, uuid: string } }>(req)
117 .pipe(catchError(err => this.restExtractor.handleError(err)))
118 }
119
120 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
121 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
122
123 let params = new HttpParams()
124 params = this.restService.addRestGetParams(params, pagination, sort)
125
126 return this.authHttp
127 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
128 .pipe(
129 switchMap(res => this.extractVideos(res)),
130 catchError(err => this.restExtractor.handleError(err))
131 )
132 }
133
134 getAccountVideos (
135 account: Account,
136 videoPagination: ComponentPagination,
137 sort: VideoSortField
138 ): Observable<{ videos: Video[], totalVideos: number }> {
139 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
140
141 let params = new HttpParams()
142 params = this.restService.addRestGetParams(params, pagination, sort)
143
144 return this.authHttp
145 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
146 .pipe(
147 switchMap(res => this.extractVideos(res)),
148 catchError(err => this.restExtractor.handleError(err))
149 )
150 }
151
152 getVideoChannelVideos (
153 videoChannel: VideoChannel,
154 videoPagination: ComponentPagination,
155 sort: VideoSortField
156 ): Observable<{ videos: Video[], totalVideos: number }> {
157 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
158
159 let params = new HttpParams()
160 params = this.restService.addRestGetParams(params, pagination, sort)
161
162 return this.authHttp
163 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
164 .pipe(
165 switchMap(res => this.extractVideos(res)),
166 catchError(err => this.restExtractor.handleError(err))
167 )
168 }
169
170 getUserSubscriptionVideos (
171 videoPagination: ComponentPagination,
172 sort: VideoSortField
173 ): Observable<{ videos: Video[], totalVideos: number }> {
174 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
175
176 let params = new HttpParams()
177 params = this.restService.addRestGetParams(params, pagination, sort)
178
179 return this.authHttp
180 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
181 .pipe(
182 switchMap(res => this.extractVideos(res)),
183 catchError(err => this.restExtractor.handleError(err))
184 )
185 }
186
187 getVideos (
188 videoPagination: ComponentPagination,
189 sort: VideoSortField,
190 filter?: VideoFilter,
191 categoryOneOf?: number
192 ): Observable<{ videos: Video[], totalVideos: number }> {
193 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
194
195 let params = new HttpParams()
196 params = this.restService.addRestGetParams(params, pagination, sort)
197
198 if (filter) {
199 params = params.set('filter', filter)
200 }
201
202 if (categoryOneOf) {
203 params = params.set('categoryOneOf', categoryOneOf + '')
204 }
205
206 return this.authHttp
207 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
208 .pipe(
209 switchMap(res => this.extractVideos(res)),
210 catchError(err => this.restExtractor.handleError(err))
211 )
212 }
213
214 buildBaseFeedUrls (params: HttpParams) {
215 const feeds = [
216 {
217 format: FeedFormat.RSS,
218 label: 'rss 2.0',
219 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
220 },
221 {
222 format: FeedFormat.ATOM,
223 label: 'atom 1.0',
224 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
225 },
226 {
227 format: FeedFormat.JSON,
228 label: 'json 1.0',
229 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
230 }
231 ]
232
233 if (params && params.keys().length !== 0) {
234 for (const feed of feeds) {
235 feed.url += '?' + params.toString()
236 }
237 }
238
239 return feeds
240 }
241
242 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
243 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244
245 if (filter) params = params.set('filter', filter)
246
247 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
248
249 return this.buildBaseFeedUrls(params)
250 }
251
252 getAccountFeedUrls (accountId: number) {
253 let params = this.restService.addRestGetParams(new HttpParams())
254 params = params.set('accountId', accountId.toString())
255
256 return this.buildBaseFeedUrls(params)
257 }
258
259 getVideoChannelFeedUrls (videoChannelId: number) {
260 let params = this.restService.addRestGetParams(new HttpParams())
261 params = params.set('videoChannelId', videoChannelId.toString())
262
263 return this.buildBaseFeedUrls(params)
264 }
265
266 removeVideo (id: number) {
267 return this.authHttp
268 .delete(VideoService.BASE_VIDEO_URL + id)
269 .pipe(
270 map(this.restExtractor.extractDataBool),
271 catchError(err => this.restExtractor.handleError(err))
272 )
273 }
274
275 loadCompleteDescription (descriptionPath: string) {
276 return this.authHttp
277 .get<{ description: string }>(environment.apiUrl + descriptionPath)
278 .pipe(
279 map(res => res.description),
280 catchError(err => this.restExtractor.handleError(err))
281 )
282 }
283
284 setVideoLike (id: number) {
285 return this.setVideoRate(id, 'like')
286 }
287
288 setVideoDislike (id: number) {
289 return this.setVideoRate(id, 'dislike')
290 }
291
292 unsetVideoLike (id: number) {
293 return this.setVideoRate(id, 'none')
294 }
295
296 getUserVideoRating (id: number) {
297 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
298
299 return this.authHttp.get<UserVideoRate>(url)
300 .pipe(catchError(err => this.restExtractor.handleError(err)))
301 }
302
303 extractVideos (result: ResultList<VideoServerModel>) {
304 return this.serverService.localeObservable
305 .pipe(
306 map(translations => {
307 const videosJson = result.data
308 const totalVideos = result.total
309 const videos: Video[] = []
310
311 for (const videoJson of videosJson) {
312 videos.push(new Video(videoJson, translations))
313 }
314
315 return { videos, totalVideos }
316 })
317 )
318 }
319
320 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
321 const newPrivacies = privacies.slice()
322
323 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
324 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
325
326 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
327 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
328
329 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
330 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
331
332 return privacies
333 }
334
335 private setVideoRate (id: number, rateType: UserVideoRateType) {
336 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
337 const body: UserVideoRateUpdate = {
338 rating: rateType
339 }
340
341 return this.authHttp
342 .put(url, body)
343 .pipe(
344 map(this.restExtractor.extractDataBool),
345 catchError(err => this.restExtractor.handleError(err))
346 )
347 }
348 }