]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Use TS_NODE_FILES instead of --files
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
CommitLineData
7ce44a74 1import { catchError, map, switchMap } 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'
8cd7faaa
C
7import {
8 UserVideoRate,
5c6d985f 9 UserVideoRateType,
8cd7faaa
C
10 UserVideoRateUpdate,
11 VideoConstant,
12 VideoFilter,
13 VideoPrivacy,
8cd7faaa
C
14 VideoUpdate
15} from '../../../../../shared/models/videos'
244e76a5 16import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
63c4db6d 17import { environment } from '../../../environments/environment'
440d39c5 18import { ComponentPaginationLight } from '../rest/component-pagination.model'
202f6b6c
C
19import { RestExtractor } from '../rest/rest-extractor.service'
20import { RestService } from '../rest/rest.service'
202f6b6c 21import { UserService } from '../users/user.service'
7b87d2d5 22import { VideoSortField } from './sort-field.type'
404b54e1
C
23import { VideoDetails } from './video-details.model'
24import { VideoEdit } from './video-edit.model'
202f6b6c 25import { Video } from './video.model'
6de36768 26import { objectToFormData } from '@app/shared/misc/utils'
0626e7af
C
27import { Account } from '@app/shared/account/account.model'
28import { AccountService } from '@app/shared/account/account.service'
170726f5 29import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
d3217560 30import { ServerService, AuthService } from '@app/core'
39ba2e8e 31import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
f5b0af50 32import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
8cd7faaa 33import { I18n } from '@ngx-translate/i18n-polyfill'
d3217560 34import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
8319d6ae 35import { FfprobeData } from 'fluent-ffmpeg'
dc8bc31b 36
7f5f4152 37export interface VideosProvider {
3caf77d3 38 getVideos (parameters: {
440d39c5 39 videoPagination: ComponentPaginationLight,
7f5f4152
BJ
40 sort: VideoSortField,
41 filter?: VideoFilter,
5c20a455 42 categoryOneOf?: number[],
3caf77d3 43 languageOneOf?: string[]
5c20a455 44 nsfwPolicy: NSFWPolicyType
93cae479 45 }): Observable<ResultList<Video>>
7f5f4152
BJ
46}
47
dc8bc31b 48@Injectable()
7f5f4152 49export class VideoService implements VideosProvider {
40e87e9e
C
50 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
51 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
dc8bc31b 52
df98563e 53 constructor (
d592e0a9 54 private authHttp: HttpClient,
d3217560
RK
55 private authService: AuthService,
56 private userService: UserService,
de59c48f 57 private restExtractor: RestExtractor,
7ce44a74 58 private restService: RestService,
8cd7faaa
C
59 private serverService: ServerService,
60 private i18n: I18n
4fd8aa32
C
61 ) {}
62
8cac1b64
C
63 getVideoViewUrl (uuid: string) {
64 return VideoService.BASE_VIDEO_URL + uuid + '/views'
65 }
66
6e46de09
C
67 getUserWatchingVideoUrl (uuid: string) {
68 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
69 }
70
93cae479 71 getVideo (options: { videoId: string }): Observable<VideoDetails> {
ba430d75 72 return this.serverService.getServerLocale()
db400f44 73 .pipe(
7ce44a74 74 switchMap(translations => {
93cae479 75 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
74b7c6d4 76 .pipe(map(videoHash => ({ videoHash, translations })))
7ce44a74
C
77 }),
78 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
e4f0e92e 79 catchError(err => this.restExtractor.handleError(err))
db400f44 80 )
4fd8aa32 81 }
dc8bc31b 82
404b54e1 83 updateVideo (video: VideoEdit) {
360329cc
C
84 const language = video.language || null
85 const licence = video.licence || null
86 const category = video.category || null
87 const description = video.description || null
88 const support = video.support || null
e94fc297 89 const scheduleUpdate = video.scheduleUpdate || null
1e74f19a 90 const originallyPublishedAt = video.originallyPublishedAt || null
c24ac1c1 91
4771e000 92 const body: VideoUpdate = {
d8e689b8 93 name: video.name,
cadb46d8
C
94 category,
95 licence,
c24ac1c1 96 language,
3bf1ec2e 97 support,
cadb46d8 98 description,
0f320037 99 channelId: video.channelId,
fd45e8f4 100 privacy: video.privacy,
4771e000 101 tags: video.tags,
47564bbe 102 nsfw: video.nsfw,
2186386c 103 waitTranscoding: video.waitTranscoding,
6de36768 104 commentsEnabled: video.commentsEnabled,
7f2cfe3a 105 downloadEnabled: video.downloadEnabled,
6de36768 106 thumbnailfile: video.thumbnailfile,
bbe0f064 107 previewfile: video.previewfile,
1e74f19a 108 scheduleUpdate,
109 originallyPublishedAt
df98563e 110 }
c24ac1c1 111
6de36768
C
112 const data = objectToFormData(body)
113
114 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
db400f44
C
115 .pipe(
116 map(this.restExtractor.extractDataBool),
e4f0e92e 117 catchError(err => this.restExtractor.handleError(err))
db400f44 118 )
d8e689b8
C
119 }
120
db7af09b 121 uploadVideo (video: FormData) {
334ddfa4 122 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 123
fd45e8f4 124 return this.authHttp
2186386c 125 .request<{ video: { id: number, uuid: string } }>(req)
e4f0e92e 126 .pipe(catchError(err => this.restExtractor.handleError(err)))
bfb3a98f
C
127 }
128
440d39c5 129 getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
4635f59d 130 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 131
d592e0a9
C
132 let params = new HttpParams()
133 params = this.restService.addRestGetParams(params, pagination, sort)
bf64ed41 134 params = this.restService.addObjectParams(params, { search })
dc8bc31b 135
7ce44a74 136 return this.authHttp
bf64ed41 137 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
db400f44 138 .pipe(
7ce44a74 139 switchMap(res => this.extractVideos(res)),
e4f0e92e 140 catchError(err => this.restExtractor.handleError(err))
db400f44 141 )
dc8bc31b
C
142 }
143
0626e7af
C
144 getAccountVideos (
145 account: Account,
440d39c5 146 videoPagination: ComponentPaginationLight,
0626e7af 147 sort: VideoSortField
93cae479 148 ): Observable<ResultList<Video>> {
0626e7af
C
149 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
150
151 let params = new HttpParams()
152 params = this.restService.addRestGetParams(params, pagination, sort)
153
154 return this.authHttp
7ce44a74 155 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
db400f44 156 .pipe(
7ce44a74 157 switchMap(res => this.extractVideos(res)),
e4f0e92e 158 catchError(err => this.restExtractor.handleError(err))
db400f44 159 )
0626e7af
C
160 }
161
170726f5
C
162 getVideoChannelVideos (
163 videoChannel: VideoChannel,
440d39c5 164 videoPagination: ComponentPaginationLight,
5c20a455
C
165 sort: VideoSortField,
166 nsfwPolicy?: NSFWPolicyType
93cae479 167 ): Observable<ResultList<Video>> {
170726f5
C
168 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
169
170 let params = new HttpParams()
171 params = this.restService.addRestGetParams(params, pagination, sort)
172
5c20a455
C
173 if (nsfwPolicy) {
174 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
175 }
176
170726f5 177 return this.authHttp
f5b0af50 178 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
db400f44 179 .pipe(
7ce44a74 180 switchMap(res => this.extractVideos(res)),
f0a39880
C
181 catchError(err => this.restExtractor.handleError(err))
182 )
183 }
184
93cae479 185 getUserSubscriptionVideos (parameters: {
440d39c5
C
186 videoPagination: ComponentPaginationLight,
187 sort: VideoSortField,
188 skipCount?: boolean
93cae479 189 }): Observable<ResultList<Video>> {
440d39c5 190 const { videoPagination, sort, skipCount } = parameters
22a16e36
C
191 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
192
193 let params = new HttpParams()
194 params = this.restService.addRestGetParams(params, pagination, sort)
195
440d39c5
C
196 if (skipCount) params = params.set('skipCount', skipCount + '')
197
22a16e36
C
198 return this.authHttp
199 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
200 .pipe(
201 switchMap(res => this.extractVideos(res)),
202 catchError(err => this.restExtractor.handleError(err))
203 )
204 }
205
3caf77d3 206 getVideos (parameters: {
440d39c5 207 videoPagination: ComponentPaginationLight,
7b87d2d5 208 sort: VideoSortField,
61b909b9 209 filter?: VideoFilter,
5c20a455 210 categoryOneOf?: number[],
440d39c5 211 languageOneOf?: string[],
d3217560 212 skipCount?: boolean,
5c20a455 213 nsfwPolicy?: NSFWPolicyType
93cae479 214 }): Observable<ResultList<Video>> {
5c20a455 215 const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
3caf77d3 216
4635f59d 217 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
218
219 let params = new HttpParams()
220 params = this.restService.addRestGetParams(params, pagination, sort)
221
440d39c5 222 if (filter) params = params.set('filter', filter)
440d39c5 223 if (skipCount) params = params.set('skipCount', skipCount + '')
61b909b9 224
5c20a455
C
225 if (nsfwPolicy) {
226 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
d3217560
RK
227 }
228
3caf77d3
C
229 if (languageOneOf) {
230 for (const l of languageOneOf) {
231 params = params.append('languageOneOf[]', l)
232 }
233 }
234
5c20a455
C
235 if (categoryOneOf) {
236 for (const c of categoryOneOf) {
237 params = params.append('categoryOneOf[]', c + '')
238 }
239 }
240
fd45e8f4 241 return this.authHttp
7ce44a74 242 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
db400f44 243 .pipe(
7ce44a74 244 switchMap(res => this.extractVideos(res)),
e4f0e92e 245 catchError(err => this.restExtractor.handleError(err))
db400f44 246 )
fd45e8f4
C
247 }
248
7b87d2d5 249 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
250 const feeds = [
251 {
39ba2e8e 252 format: FeedFormat.RSS,
cc1561f9
C
253 label: 'rss 2.0',
254 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
255 },
256 {
39ba2e8e 257 format: FeedFormat.ATOM,
cc1561f9
C
258 label: 'atom 1.0',
259 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
260 },
261 {
39ba2e8e 262 format: FeedFormat.JSON,
cc1561f9
C
263 label: 'json 1.0',
264 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
265 }
266 ]
267
7b87d2d5
C
268 if (params && params.keys().length !== 0) {
269 for (const feed of feeds) {
270 feed.url += '?' + params.toString()
271 }
272 }
273
cc1561f9 274 return feeds
244e76a5
RK
275 }
276
5c20a455 277 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
7b87d2d5 278 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 279
cc1561f9
C
280 if (filter) params = params.set('filter', filter)
281
5c20a455
C
282 if (categoryOneOf) {
283 for (const c of categoryOneOf) {
284 params = params.append('categoryOneOf[]', c + '')
285 }
286 }
61b909b9 287
7b87d2d5 288 return this.buildBaseFeedUrls(params)
244e76a5
RK
289 }
290
cc1561f9 291 getAccountFeedUrls (accountId: number) {
244e76a5 292 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 293 params = params.set('accountId', accountId.toString())
cc1561f9 294
7b87d2d5 295 return this.buildBaseFeedUrls(params)
244e76a5
RK
296 }
297
170726f5
C
298 getVideoChannelFeedUrls (videoChannelId: number) {
299 let params = this.restService.addRestGetParams(new HttpParams())
300 params = params.set('videoChannelId', videoChannelId.toString())
301
302 return this.buildBaseFeedUrls(params)
303 }
304
8319d6ae
RK
305 getVideoFileMetadata (metadataUrl: string) {
306 return this.authHttp
307 .get<FfprobeData>(metadataUrl)
308 .pipe(
309 catchError(err => this.restExtractor.handleError(err))
310 )
311 }
312
d592e0a9 313 removeVideo (id: number) {
fd45e8f4 314 return this.authHttp
db400f44
C
315 .delete(VideoService.BASE_VIDEO_URL + id)
316 .pipe(
317 map(this.restExtractor.extractDataBool),
e4f0e92e 318 catchError(err => this.restExtractor.handleError(err))
db400f44 319 )
4fd8aa32
C
320 }
321
2de96f4d
C
322 loadCompleteDescription (descriptionPath: string) {
323 return this.authHttp
c199c427 324 .get<{ description: string }>(environment.apiUrl + descriptionPath)
db400f44 325 .pipe(
c199c427 326 map(res => res.description),
e4f0e92e 327 catchError(err => this.restExtractor.handleError(err))
db400f44 328 )
d38b8281
C
329 }
330
0a6658fd 331 setVideoLike (id: number) {
df98563e 332 return this.setVideoRate(id, 'like')
d38b8281
C
333 }
334
0a6658fd 335 setVideoDislike (id: number) {
df98563e 336 return this.setVideoRate(id, 'dislike')
d38b8281
C
337 }
338
57a49263
BB
339 unsetVideoLike (id: number) {
340 return this.setVideoRate(id, 'none')
341 }
342
5fcbd898 343 getUserVideoRating (id: number) {
334ddfa4 344 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 345
5fcbd898 346 return this.authHttp.get<UserVideoRate>(url)
e4f0e92e 347 .pipe(catchError(err => this.restExtractor.handleError(err)))
d38b8281
C
348 }
349
57c36b27 350 extractVideos (result: ResultList<VideoServerModel>) {
ba430d75 351 return this.serverService.getServerLocale()
2186386c
C
352 .pipe(
353 map(translations => {
354 const videosJson = result.data
355 const totalVideos = result.total
356 const videos: Video[] = []
357
358 for (const videoJson of videosJson) {
359 videos.push(new Video(videoJson, translations))
360 }
361
93cae479 362 return { total: totalVideos, data: videos }
2186386c
C
363 })
364 )
501bc6c2 365 }
57c36b27 366
8cd7faaa 367 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
22a73cb8
C
368 const base = [
369 {
370 id: VideoPrivacy.PRIVATE,
371 label: this.i18n('Only I can see this video')
372 },
373 {
374 id: VideoPrivacy.UNLISTED,
375 label: this.i18n('Only people with the private link can see this video')
376 },
377 {
378 id: VideoPrivacy.PUBLIC,
379 label: this.i18n('Anyone can see this video')
380 },
381 {
382 id: VideoPrivacy.INTERNAL,
383 label: this.i18n('Only users of this instance can see this video')
384 }
385 ]
8cd7faaa 386
22a73cb8 387 return base.filter(o => !!privacies.find(p => p.id === o.id))
8cd7faaa
C
388 }
389
5c20a455
C
390 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
391 return nsfwPolicy === 'do_not_list'
392 ? 'false'
393 : 'both'
394 }
395
5c6d985f 396 private setVideoRate (id: number, rateType: UserVideoRateType) {
57c36b27
C
397 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
398 const body: UserVideoRateUpdate = {
399 rating: rateType
400 }
401
402 return this.authHttp
403 .put(url, body)
404 .pipe(
405 map(this.restExtractor.extractDataBool),
406 catchError(err => this.restExtractor.handleError(err))
407 )
408 }
dc8bc31b 409}