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