]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Added pointer to the .env file
[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'
4635f59d 18import { ComponentPagination } 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'
7ce44a74 30import { ServerService } 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'
dc8bc31b 34
7f5f4152 35export interface VideosProvider {
3caf77d3 36 getVideos (parameters: {
7f5f4152
BJ
37 videoPagination: ComponentPagination,
38 sort: VideoSortField,
39 filter?: VideoFilter,
3caf77d3
C
40 categoryOneOf?: number,
41 languageOneOf?: string[]
93cae479 42 }): Observable<ResultList<Video>>
7f5f4152
BJ
43}
44
dc8bc31b 45@Injectable()
7f5f4152 46export class VideoService implements VideosProvider {
40e87e9e
C
47 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
48 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
dc8bc31b 49
df98563e 50 constructor (
d592e0a9 51 private authHttp: HttpClient,
de59c48f 52 private restExtractor: RestExtractor,
7ce44a74 53 private restService: RestService,
8cd7faaa
C
54 private serverService: ServerService,
55 private i18n: I18n
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> {
7ce44a74 67 return this.serverService.localeObservable
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,
1e74f19a 103 scheduleUpdate,
104 originallyPublishedAt
df98563e 105 }
c24ac1c1 106
6de36768
C
107 const data = objectToFormData(body)
108
109 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
db400f44
C
110 .pipe(
111 map(this.restExtractor.extractDataBool),
e4f0e92e 112 catchError(err => this.restExtractor.handleError(err))
db400f44 113 )
d8e689b8
C
114 }
115
db7af09b 116 uploadVideo (video: FormData) {
334ddfa4 117 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 118
fd45e8f4 119 return this.authHttp
2186386c 120 .request<{ video: { id: number, uuid: string } }>(req)
e4f0e92e 121 .pipe(catchError(err => this.restExtractor.handleError(err)))
bfb3a98f
C
122 }
123
93cae479 124 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<ResultList<Video>> {
4635f59d 125 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 126
d592e0a9
C
127 let params = new HttpParams()
128 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 129
7ce44a74
C
130 return this.authHttp
131 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
db400f44 132 .pipe(
7ce44a74 133 switchMap(res => this.extractVideos(res)),
e4f0e92e 134 catchError(err => this.restExtractor.handleError(err))
db400f44 135 )
dc8bc31b
C
136 }
137
0626e7af
C
138 getAccountVideos (
139 account: Account,
140 videoPagination: ComponentPagination,
141 sort: VideoSortField
93cae479 142 ): Observable<ResultList<Video>> {
0626e7af
C
143 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
144
145 let params = new HttpParams()
146 params = this.restService.addRestGetParams(params, pagination, sort)
147
148 return this.authHttp
7ce44a74 149 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
db400f44 150 .pipe(
7ce44a74 151 switchMap(res => this.extractVideos(res)),
e4f0e92e 152 catchError(err => this.restExtractor.handleError(err))
db400f44 153 )
0626e7af
C
154 }
155
170726f5
C
156 getVideoChannelVideos (
157 videoChannel: VideoChannel,
158 videoPagination: ComponentPagination,
159 sort: VideoSortField
93cae479 160 ): Observable<ResultList<Video>> {
170726f5
C
161 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
162
163 let params = new HttpParams()
164 params = this.restService.addRestGetParams(params, pagination, sort)
165
166 return this.authHttp
f5b0af50 167 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
db400f44 168 .pipe(
7ce44a74 169 switchMap(res => this.extractVideos(res)),
f0a39880
C
170 catchError(err => this.restExtractor.handleError(err))
171 )
172 }
173
93cae479 174 getUserSubscriptionVideos (parameters: {
22a16e36
C
175 videoPagination: ComponentPagination,
176 sort: VideoSortField
93cae479
C
177 }): Observable<ResultList<Video>> {
178 const { videoPagination, sort } = parameters
22a16e36
C
179 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
180
181 let params = new HttpParams()
182 params = this.restService.addRestGetParams(params, pagination, sort)
183
184 return this.authHttp
185 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
186 .pipe(
187 switchMap(res => this.extractVideos(res)),
188 catchError(err => this.restExtractor.handleError(err))
189 )
190 }
191
3caf77d3 192 getVideos (parameters: {
066e94c5 193 videoPagination: ComponentPagination,
7b87d2d5 194 sort: VideoSortField,
61b909b9 195 filter?: VideoFilter,
3caf77d3
C
196 categoryOneOf?: number,
197 languageOneOf?: string[]
93cae479 198 }): Observable<ResultList<Video>> {
3caf77d3
C
199 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
200
4635f59d 201 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
202
203 let params = new HttpParams()
204 params = this.restService.addRestGetParams(params, pagination, sort)
205
066e94c5
C
206 if (filter) {
207 params = params.set('filter', filter)
208 }
209
d59cba29
C
210 if (categoryOneOf) {
211 params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9
P
212 }
213
3caf77d3
C
214 if (languageOneOf) {
215 for (const l of languageOneOf) {
216 params = params.append('languageOneOf[]', l)
217 }
218 }
219
fd45e8f4 220 return this.authHttp
7ce44a74 221 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
db400f44 222 .pipe(
7ce44a74 223 switchMap(res => this.extractVideos(res)),
e4f0e92e 224 catchError(err => this.restExtractor.handleError(err))
db400f44 225 )
fd45e8f4
C
226 }
227
7b87d2d5 228 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
229 const feeds = [
230 {
39ba2e8e 231 format: FeedFormat.RSS,
cc1561f9
C
232 label: 'rss 2.0',
233 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
234 },
235 {
39ba2e8e 236 format: FeedFormat.ATOM,
cc1561f9
C
237 label: 'atom 1.0',
238 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
239 },
240 {
39ba2e8e 241 format: FeedFormat.JSON,
cc1561f9
C
242 label: 'json 1.0',
243 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
244 }
245 ]
246
7b87d2d5
C
247 if (params && params.keys().length !== 0) {
248 for (const feed of feeds) {
249 feed.url += '?' + params.toString()
250 }
251 }
252
cc1561f9 253 return feeds
244e76a5
RK
254 }
255
d59cba29 256 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
7b87d2d5 257 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 258
cc1561f9
C
259 if (filter) params = params.set('filter', filter)
260
d59cba29 261 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9 262
7b87d2d5 263 return this.buildBaseFeedUrls(params)
244e76a5
RK
264 }
265
cc1561f9 266 getAccountFeedUrls (accountId: number) {
244e76a5 267 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 268 params = params.set('accountId', accountId.toString())
cc1561f9 269
7b87d2d5 270 return this.buildBaseFeedUrls(params)
244e76a5
RK
271 }
272
170726f5
C
273 getVideoChannelFeedUrls (videoChannelId: number) {
274 let params = this.restService.addRestGetParams(new HttpParams())
275 params = params.set('videoChannelId', videoChannelId.toString())
276
277 return this.buildBaseFeedUrls(params)
278 }
279
d592e0a9 280 removeVideo (id: number) {
fd45e8f4 281 return this.authHttp
db400f44
C
282 .delete(VideoService.BASE_VIDEO_URL + id)
283 .pipe(
284 map(this.restExtractor.extractDataBool),
e4f0e92e 285 catchError(err => this.restExtractor.handleError(err))
db400f44 286 )
4fd8aa32
C
287 }
288
2de96f4d
C
289 loadCompleteDescription (descriptionPath: string) {
290 return this.authHttp
c199c427 291 .get<{ description: string }>(environment.apiUrl + descriptionPath)
db400f44 292 .pipe(
c199c427 293 map(res => res.description),
e4f0e92e 294 catchError(err => this.restExtractor.handleError(err))
db400f44 295 )
d38b8281
C
296 }
297
0a6658fd 298 setVideoLike (id: number) {
df98563e 299 return this.setVideoRate(id, 'like')
d38b8281
C
300 }
301
0a6658fd 302 setVideoDislike (id: number) {
df98563e 303 return this.setVideoRate(id, 'dislike')
d38b8281
C
304 }
305
57a49263
BB
306 unsetVideoLike (id: number) {
307 return this.setVideoRate(id, 'none')
308 }
309
5fcbd898 310 getUserVideoRating (id: number) {
334ddfa4 311 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 312
5fcbd898 313 return this.authHttp.get<UserVideoRate>(url)
e4f0e92e 314 .pipe(catchError(err => this.restExtractor.handleError(err)))
d38b8281
C
315 }
316
57c36b27 317 extractVideos (result: ResultList<VideoServerModel>) {
7ce44a74 318 return this.serverService.localeObservable
2186386c
C
319 .pipe(
320 map(translations => {
321 const videosJson = result.data
322 const totalVideos = result.total
323 const videos: Video[] = []
324
325 for (const videoJson of videosJson) {
326 videos.push(new Video(videoJson, translations))
327 }
328
93cae479 329 return { total: totalVideos, data: videos }
2186386c
C
330 })
331 )
501bc6c2 332 }
57c36b27 333
8cd7faaa
C
334 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
335 const newPrivacies = privacies.slice()
336
337 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
338 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
339
340 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
341 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
342
343 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
344 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
345
346 return privacies
347 }
348
5c6d985f 349 private setVideoRate (id: number, rateType: UserVideoRateType) {
57c36b27
C
350 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
351 const body: UserVideoRateUpdate = {
352 rating: rateType
353 }
354
355 return this.authHttp
356 .put(url, body)
357 .pipe(
358 map(this.restExtractor.extractDataBool),
359 catchError(err => this.restExtractor.handleError(err))
360 )
361 }
dc8bc31b 362}