]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Improve SQL query for my special playlists
[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> {
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,
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
bf64ed41 124 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField, search?: string): 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)
bf64ed41 129 params = this.restService.addObjectParams(params, { search })
dc8bc31b 130
7ce44a74 131 return this.authHttp
bf64ed41 132 .get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
db400f44 133 .pipe(
7ce44a74 134 switchMap(res => this.extractVideos(res)),
e4f0e92e 135 catchError(err => this.restExtractor.handleError(err))
db400f44 136 )
dc8bc31b
C
137 }
138
0626e7af
C
139 getAccountVideos (
140 account: Account,
141 videoPagination: ComponentPagination,
142 sort: VideoSortField
93cae479 143 ): Observable<ResultList<Video>> {
0626e7af
C
144 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
145
146 let params = new HttpParams()
147 params = this.restService.addRestGetParams(params, pagination, sort)
148
149 return this.authHttp
7ce44a74 150 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
db400f44 151 .pipe(
7ce44a74 152 switchMap(res => this.extractVideos(res)),
e4f0e92e 153 catchError(err => this.restExtractor.handleError(err))
db400f44 154 )
0626e7af
C
155 }
156
170726f5
C
157 getVideoChannelVideos (
158 videoChannel: VideoChannel,
159 videoPagination: ComponentPagination,
160 sort: VideoSortField
93cae479 161 ): Observable<ResultList<Video>> {
170726f5
C
162 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
163
164 let params = new HttpParams()
165 params = this.restService.addRestGetParams(params, pagination, sort)
166
167 return this.authHttp
f5b0af50 168 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
db400f44 169 .pipe(
7ce44a74 170 switchMap(res => this.extractVideos(res)),
f0a39880
C
171 catchError(err => this.restExtractor.handleError(err))
172 )
173 }
174
93cae479 175 getUserSubscriptionVideos (parameters: {
22a16e36
C
176 videoPagination: ComponentPagination,
177 sort: VideoSortField
93cae479
C
178 }): Observable<ResultList<Video>> {
179 const { videoPagination, sort } = parameters
22a16e36
C
180 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
181
182 let params = new HttpParams()
183 params = this.restService.addRestGetParams(params, pagination, sort)
184
185 return this.authHttp
186 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
187 .pipe(
188 switchMap(res => this.extractVideos(res)),
189 catchError(err => this.restExtractor.handleError(err))
190 )
191 }
192
3caf77d3 193 getVideos (parameters: {
066e94c5 194 videoPagination: ComponentPagination,
7b87d2d5 195 sort: VideoSortField,
61b909b9 196 filter?: VideoFilter,
3caf77d3
C
197 categoryOneOf?: number,
198 languageOneOf?: string[]
93cae479 199 }): Observable<ResultList<Video>> {
3caf77d3
C
200 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
201
4635f59d 202 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
203
204 let params = new HttpParams()
205 params = this.restService.addRestGetParams(params, pagination, sort)
206
066e94c5
C
207 if (filter) {
208 params = params.set('filter', filter)
209 }
210
d59cba29
C
211 if (categoryOneOf) {
212 params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9
P
213 }
214
3caf77d3
C
215 if (languageOneOf) {
216 for (const l of languageOneOf) {
217 params = params.append('languageOneOf[]', l)
218 }
219 }
220
fd45e8f4 221 return this.authHttp
7ce44a74 222 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
db400f44 223 .pipe(
7ce44a74 224 switchMap(res => this.extractVideos(res)),
e4f0e92e 225 catchError(err => this.restExtractor.handleError(err))
db400f44 226 )
fd45e8f4
C
227 }
228
7b87d2d5 229 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
230 const feeds = [
231 {
39ba2e8e 232 format: FeedFormat.RSS,
cc1561f9
C
233 label: 'rss 2.0',
234 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
235 },
236 {
39ba2e8e 237 format: FeedFormat.ATOM,
cc1561f9
C
238 label: 'atom 1.0',
239 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
240 },
241 {
39ba2e8e 242 format: FeedFormat.JSON,
cc1561f9
C
243 label: 'json 1.0',
244 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
245 }
246 ]
247
7b87d2d5
C
248 if (params && params.keys().length !== 0) {
249 for (const feed of feeds) {
250 feed.url += '?' + params.toString()
251 }
252 }
253
cc1561f9 254 return feeds
244e76a5
RK
255 }
256
d59cba29 257 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
7b87d2d5 258 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 259
cc1561f9
C
260 if (filter) params = params.set('filter', filter)
261
d59cba29 262 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9 263
7b87d2d5 264 return this.buildBaseFeedUrls(params)
244e76a5
RK
265 }
266
cc1561f9 267 getAccountFeedUrls (accountId: number) {
244e76a5 268 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 269 params = params.set('accountId', accountId.toString())
cc1561f9 270
7b87d2d5 271 return this.buildBaseFeedUrls(params)
244e76a5
RK
272 }
273
170726f5
C
274 getVideoChannelFeedUrls (videoChannelId: number) {
275 let params = this.restService.addRestGetParams(new HttpParams())
276 params = params.set('videoChannelId', videoChannelId.toString())
277
278 return this.buildBaseFeedUrls(params)
279 }
280
d592e0a9 281 removeVideo (id: number) {
fd45e8f4 282 return this.authHttp
db400f44
C
283 .delete(VideoService.BASE_VIDEO_URL + id)
284 .pipe(
285 map(this.restExtractor.extractDataBool),
e4f0e92e 286 catchError(err => this.restExtractor.handleError(err))
db400f44 287 )
4fd8aa32
C
288 }
289
2de96f4d
C
290 loadCompleteDescription (descriptionPath: string) {
291 return this.authHttp
c199c427 292 .get<{ description: string }>(environment.apiUrl + descriptionPath)
db400f44 293 .pipe(
c199c427 294 map(res => res.description),
e4f0e92e 295 catchError(err => this.restExtractor.handleError(err))
db400f44 296 )
d38b8281
C
297 }
298
0a6658fd 299 setVideoLike (id: number) {
df98563e 300 return this.setVideoRate(id, 'like')
d38b8281
C
301 }
302
0a6658fd 303 setVideoDislike (id: number) {
df98563e 304 return this.setVideoRate(id, 'dislike')
d38b8281
C
305 }
306
57a49263
BB
307 unsetVideoLike (id: number) {
308 return this.setVideoRate(id, 'none')
309 }
310
5fcbd898 311 getUserVideoRating (id: number) {
334ddfa4 312 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 313
5fcbd898 314 return this.authHttp.get<UserVideoRate>(url)
e4f0e92e 315 .pipe(catchError(err => this.restExtractor.handleError(err)))
d38b8281
C
316 }
317
57c36b27 318 extractVideos (result: ResultList<VideoServerModel>) {
ba430d75 319 return this.serverService.getServerLocale()
2186386c
C
320 .pipe(
321 map(translations => {
322 const videosJson = result.data
323 const totalVideos = result.total
324 const videos: Video[] = []
325
326 for (const videoJson of videosJson) {
327 videos.push(new Video(videoJson, translations))
328 }
329
93cae479 330 return { total: totalVideos, data: videos }
2186386c
C
331 })
332 )
501bc6c2 333 }
57c36b27 334
8cd7faaa 335 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
22a73cb8
C
336 const base = [
337 {
338 id: VideoPrivacy.PRIVATE,
339 label: this.i18n('Only I can see this video')
340 },
341 {
342 id: VideoPrivacy.UNLISTED,
343 label: this.i18n('Only people with the private link can see this video')
344 },
345 {
346 id: VideoPrivacy.PUBLIC,
347 label: this.i18n('Anyone can see this video')
348 },
349 {
350 id: VideoPrivacy.INTERNAL,
351 label: this.i18n('Only users of this instance can see this video')
352 }
353 ]
8cd7faaa 354
22a73cb8 355 return base.filter(o => !!privacies.find(p => p.id === o.id))
8cd7faaa
C
356 }
357
5c6d985f 358 private setVideoRate (id: number, rateType: UserVideoRateType) {
57c36b27
C
359 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
360 const body: UserVideoRateUpdate = {
361 rating: rateType
362 }
363
364 return this.authHttp
365 .put(url, body)
366 .pipe(
367 map(this.restExtractor.extractDataBool),
368 catchError(err => this.restExtractor.handleError(err))
369 )
370 }
dc8bc31b 371}