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