]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/video/video.service.ts
Merge remote-tracking branch 'weblate/develop' into develop
[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,
7294aab0 99 pluginData: video.pluginData,
1e74f19a 100 scheduleUpdate,
101 originallyPublishedAt
df98563e 102 }
c24ac1c1 103
6de36768
C
104 const data = objectToFormData(body)
105
106 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
db400f44
C
107 .pipe(
108 map(this.restExtractor.extractDataBool),
e4f0e92e 109 catchError(err => this.restExtractor.handleError(err))
db400f44 110 )
d8e689b8
C
111 }
112
db7af09b 113 uploadVideo (video: FormData) {
334ddfa4 114 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 115
fd45e8f4 116 return this.authHttp
2186386c 117 .request<{ video: { id: number, uuid: string } }>(req)
e4f0e92e 118 .pipe(catchError(err => this.restExtractor.handleError(err)))
bfb3a98f
C
119 }
120
440d39c5 121 getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
4635f59d 122 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 123
d592e0a9
C
124 let params = new HttpParams()
125 params = this.restService.addRestGetParams(params, pagination, sort)
bf64ed41 126 params = this.restService.addObjectParams(params, { search })
dc8bc31b 127
7ce44a74 128 return this.authHttp
bf64ed41 129 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
db400f44 130 .pipe(
7ce44a74 131 switchMap(res => this.extractVideos(res)),
e4f0e92e 132 catchError(err => this.restExtractor.handleError(err))
db400f44 133 )
dc8bc31b
C
134 }
135
0626e7af
C
136 getAccountVideos (
137 account: Account,
440d39c5 138 videoPagination: ComponentPaginationLight,
0626e7af 139 sort: VideoSortField
93cae479 140 ): Observable<ResultList<Video>> {
0626e7af
C
141 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
142
143 let params = new HttpParams()
144 params = this.restService.addRestGetParams(params, pagination, sort)
145
146 return this.authHttp
7ce44a74 147 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
db400f44 148 .pipe(
7ce44a74 149 switchMap(res => this.extractVideos(res)),
e4f0e92e 150 catchError(err => this.restExtractor.handleError(err))
db400f44 151 )
0626e7af
C
152 }
153
170726f5
C
154 getVideoChannelVideos (
155 videoChannel: VideoChannel,
440d39c5 156 videoPagination: ComponentPaginationLight,
5c20a455
C
157 sort: VideoSortField,
158 nsfwPolicy?: NSFWPolicyType
93cae479 159 ): Observable<ResultList<Video>> {
170726f5
C
160 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
161
162 let params = new HttpParams()
163 params = this.restService.addRestGetParams(params, pagination, sort)
164
5c20a455
C
165 if (nsfwPolicy) {
166 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
167 }
168
170726f5 169 return this.authHttp
f5b0af50 170 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
db400f44 171 .pipe(
7ce44a74 172 switchMap(res => this.extractVideos(res)),
22a16e36
C
173 catchError(err => this.restExtractor.handleError(err))
174 )
175 }
176
3caf77d3 177 getVideos (parameters: {
440d39c5 178 videoPagination: ComponentPaginationLight,
7b87d2d5 179 sort: VideoSortField,
61b909b9 180 filter?: VideoFilter,
5c20a455 181 categoryOneOf?: number[],
440d39c5 182 languageOneOf?: string[],
d3217560 183 skipCount?: boolean,
5c20a455 184 nsfwPolicy?: NSFWPolicyType
93cae479 185 }): Observable<ResultList<Video>> {
5c20a455 186 const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
3caf77d3 187
4635f59d 188 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
189
190 let params = new HttpParams()
191 params = this.restService.addRestGetParams(params, pagination, sort)
192
440d39c5 193 if (filter) params = params.set('filter', filter)
440d39c5 194 if (skipCount) params = params.set('skipCount', skipCount + '')
61b909b9 195
5c20a455
C
196 if (nsfwPolicy) {
197 params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
d3217560
RK
198 }
199
3caf77d3
C
200 if (languageOneOf) {
201 for (const l of languageOneOf) {
202 params = params.append('languageOneOf[]', l)
203 }
204 }
205
5c20a455
C
206 if (categoryOneOf) {
207 for (const c of categoryOneOf) {
208 params = params.append('categoryOneOf[]', c + '')
209 }
210 }
211
fd45e8f4 212 return this.authHttp
7ce44a74 213 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
db400f44 214 .pipe(
7ce44a74 215 switchMap(res => this.extractVideos(res)),
e4f0e92e 216 catchError(err => this.restExtractor.handleError(err))
db400f44 217 )
fd45e8f4
C
218 }
219
7b87d2d5 220 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
221 const feeds = [
222 {
39ba2e8e 223 format: FeedFormat.RSS,
2d011d94 224 label: 'media rss 2.0',
cc1561f9
C
225 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
226 },
227 {
39ba2e8e 228 format: FeedFormat.ATOM,
cc1561f9
C
229 label: 'atom 1.0',
230 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
231 },
232 {
39ba2e8e 233 format: FeedFormat.JSON,
cc1561f9
C
234 label: 'json 1.0',
235 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
236 }
237 ]
238
7b87d2d5
C
239 if (params && params.keys().length !== 0) {
240 for (const feed of feeds) {
241 feed.url += '?' + params.toString()
242 }
243 }
244
cc1561f9 245 return feeds
244e76a5
RK
246 }
247
5c20a455 248 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
7b87d2d5 249 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 250
cc1561f9
C
251 if (filter) params = params.set('filter', filter)
252
5c20a455
C
253 if (categoryOneOf) {
254 for (const c of categoryOneOf) {
255 params = params.append('categoryOneOf[]', c + '')
256 }
257 }
61b909b9 258
7b87d2d5 259 return this.buildBaseFeedUrls(params)
244e76a5
RK
260 }
261
cc1561f9 262 getAccountFeedUrls (accountId: number) {
244e76a5 263 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 264 params = params.set('accountId', accountId.toString())
cc1561f9 265
7b87d2d5 266 return this.buildBaseFeedUrls(params)
244e76a5
RK
267 }
268
170726f5
C
269 getVideoChannelFeedUrls (videoChannelId: number) {
270 let params = this.restService.addRestGetParams(new HttpParams())
271 params = params.set('videoChannelId', videoChannelId.toString())
272
273 return this.buildBaseFeedUrls(params)
274 }
275
8319d6ae
RK
276 getVideoFileMetadata (metadataUrl: string) {
277 return this.authHttp
583eb04b 278 .get<VideoFileMetadata>(metadataUrl)
8319d6ae
RK
279 .pipe(
280 catchError(err => this.restExtractor.handleError(err))
281 )
282 }
283
d592e0a9 284 removeVideo (id: number) {
fd45e8f4 285 return this.authHttp
db400f44
C
286 .delete(VideoService.BASE_VIDEO_URL + id)
287 .pipe(
288 map(this.restExtractor.extractDataBool),
e4f0e92e 289 catchError(err => this.restExtractor.handleError(err))
db400f44 290 )
4fd8aa32
C
291 }
292
2de96f4d
C
293 loadCompleteDescription (descriptionPath: string) {
294 return this.authHttp
c199c427 295 .get<{ description: string }>(environment.apiUrl + descriptionPath)
db400f44 296 .pipe(
c199c427 297 map(res => res.description),
e4f0e92e 298 catchError(err => this.restExtractor.handleError(err))
db400f44 299 )
d38b8281
C
300 }
301
0a6658fd 302 setVideoLike (id: number) {
df98563e 303 return this.setVideoRate(id, 'like')
d38b8281
C
304 }
305
0a6658fd 306 setVideoDislike (id: number) {
df98563e 307 return this.setVideoRate(id, 'dislike')
d38b8281
C
308 }
309
57a49263
BB
310 unsetVideoLike (id: number) {
311 return this.setVideoRate(id, 'none')
312 }
313
5fcbd898 314 getUserVideoRating (id: number) {
334ddfa4 315 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 316
5fcbd898 317 return this.authHttp.get<UserVideoRate>(url)
e4f0e92e 318 .pipe(catchError(err => this.restExtractor.handleError(err)))
d38b8281
C
319 }
320
57c36b27 321 extractVideos (result: ResultList<VideoServerModel>) {
ba430d75 322 return this.serverService.getServerLocale()
2186386c
C
323 .pipe(
324 map(translations => {
325 const videosJson = result.data
326 const totalVideos = result.total
327 const videos: Video[] = []
328
329 for (const videoJson of videosJson) {
330 videos.push(new Video(videoJson, translations))
331 }
332
93cae479 333 return { total: totalVideos, data: videos }
2186386c
C
334 })
335 )
501bc6c2 336 }
57c36b27 337
8cd7faaa 338 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
22a73cb8
C
339 const base = [
340 {
341 id: VideoPrivacy.PRIVATE,
66357162 342 description: $localize`Only I can see this video`
22a73cb8
C
343 },
344 {
345 id: VideoPrivacy.UNLISTED,
66357162 346 description: $localize`Only shareable via a private link`
22a73cb8
C
347 },
348 {
349 id: VideoPrivacy.PUBLIC,
66357162 350 description: $localize`Anyone can see this video`
22a73cb8
C
351 },
352 {
353 id: VideoPrivacy.INTERNAL,
66357162 354 description: $localize`Only users of this instance can see this video`
22a73cb8
C
355 }
356 ]
8cd7faaa 357
02c01341
RK
358 return base
359 .filter(o => !!privacies.find(p => p.id === o.id)) // filter down to privacies that where in the input
360 .map(o => ({ ...privacies[o.id - 1], ...o })) // merge the input privacies that contain a label, and extend them with a description
8cd7faaa
C
361 }
362
5c20a455
C
363 nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
364 return nsfwPolicy === 'do_not_list'
365 ? 'false'
366 : 'both'
367 }
368
5c6d985f 369 private setVideoRate (id: number, rateType: UserVideoRateType) {
57c36b27
C
370 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
371 const body: UserVideoRateUpdate = {
372 rating: rateType
373 }
374
375 return this.authHttp
376 .put(url, body)
377 .pipe(
378 map(this.restExtractor.extractDataBool),
379 catchError(err => this.restExtractor.handleError(err))
380 )
381 }
dc8bc31b 382}