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