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