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