]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Add popover autoclose
[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'
f5b0af50 7import { UserVideoRate, UserVideoRateUpdate, VideoFilter, VideoRateType, VideoUpdate } from '../../../../../shared/models/videos'
244e76a5 8import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
63c4db6d 9import { environment } from '../../../environments/environment'
4635f59d 10import { ComponentPagination } from '../rest/component-pagination.model'
202f6b6c
C
11import { RestExtractor } from '../rest/rest-extractor.service'
12import { RestService } from '../rest/rest.service'
202f6b6c 13import { UserService } from '../users/user.service'
7b87d2d5 14import { VideoSortField } from './sort-field.type'
404b54e1
C
15import { VideoDetails } from './video-details.model'
16import { VideoEdit } from './video-edit.model'
202f6b6c 17import { Video } from './video.model'
6de36768 18import { objectToFormData } from '@app/shared/misc/utils'
0626e7af
C
19import { Account } from '@app/shared/account/account.model'
20import { AccountService } from '@app/shared/account/account.service'
170726f5 21import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
7ce44a74 22import { ServerService } from '@app/core'
22a16e36 23import { UserSubscriptionService } from '@app/shared/user-subscription'
f5b0af50 24import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
dc8bc31b
C
25
26@Injectable()
41a2aee3 27export class VideoService {
40e87e9e
C
28 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
29 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
dc8bc31b 30
df98563e 31 constructor (
d592e0a9 32 private authHttp: HttpClient,
de59c48f 33 private restExtractor: RestExtractor,
7ce44a74
C
34 private restService: RestService,
35 private serverService: ServerService
4fd8aa32
C
36 ) {}
37
8cac1b64
C
38 getVideoViewUrl (uuid: string) {
39 return VideoService.BASE_VIDEO_URL + uuid + '/views'
40 }
41
9d9597df 42 getVideo (uuid: string): Observable<VideoDetails> {
7ce44a74 43 return this.serverService.localeObservable
db400f44 44 .pipe(
7ce44a74 45 switchMap(translations => {
74b7c6d4
C
46 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
47 .pipe(map(videoHash => ({ videoHash, translations })))
7ce44a74
C
48 }),
49 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
e4f0e92e 50 catchError(err => this.restExtractor.handleError(err))
db400f44 51 )
4fd8aa32 52 }
dc8bc31b 53
5fcbd898 54 viewVideo (uuid: string): Observable<boolean> {
8cac1b64 55 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
db400f44
C
56 .pipe(
57 map(this.restExtractor.extractDataBool),
e4f0e92e 58 catchError(err => this.restExtractor.handleError(err))
db400f44 59 )
1f3e9fec
C
60 }
61
404b54e1 62 updateVideo (video: VideoEdit) {
360329cc
C
63 const language = video.language || null
64 const licence = video.licence || null
65 const category = video.category || null
66 const description = video.description || null
67 const support = video.support || null
e94fc297 68 const scheduleUpdate = video.scheduleUpdate || null
c24ac1c1 69
4771e000 70 const body: VideoUpdate = {
d8e689b8 71 name: video.name,
cadb46d8
C
72 category,
73 licence,
c24ac1c1 74 language,
3bf1ec2e 75 support,
cadb46d8 76 description,
0f320037 77 channelId: video.channelId,
fd45e8f4 78 privacy: video.privacy,
4771e000 79 tags: video.tags,
47564bbe 80 nsfw: video.nsfw,
2186386c 81 waitTranscoding: video.waitTranscoding,
6de36768
C
82 commentsEnabled: video.commentsEnabled,
83 thumbnailfile: video.thumbnailfile,
bbe0f064 84 previewfile: video.previewfile,
e94fc297 85 scheduleUpdate
df98563e 86 }
c24ac1c1 87
6de36768
C
88 const data = objectToFormData(body)
89
90 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
db400f44
C
91 .pipe(
92 map(this.restExtractor.extractDataBool),
e4f0e92e 93 catchError(err => this.restExtractor.handleError(err))
db400f44 94 )
d8e689b8
C
95 }
96
db7af09b 97 uploadVideo (video: FormData) {
334ddfa4 98 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 99
fd45e8f4 100 return this.authHttp
2186386c 101 .request<{ video: { id: number, uuid: string } }>(req)
e4f0e92e 102 .pipe(catchError(err => this.restExtractor.handleError(err)))
bfb3a98f
C
103 }
104
2186386c 105 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
4635f59d 106 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 107
d592e0a9
C
108 let params = new HttpParams()
109 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 110
7ce44a74
C
111 return this.authHttp
112 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
db400f44 113 .pipe(
7ce44a74 114 switchMap(res => this.extractVideos(res)),
e4f0e92e 115 catchError(err => this.restExtractor.handleError(err))
db400f44 116 )
dc8bc31b
C
117 }
118
0626e7af
C
119 getAccountVideos (
120 account: Account,
121 videoPagination: ComponentPagination,
122 sort: VideoSortField
2186386c 123 ): Observable<{ videos: Video[], totalVideos: number }> {
0626e7af
C
124 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
125
126 let params = new HttpParams()
127 params = this.restService.addRestGetParams(params, pagination, sort)
128
129 return this.authHttp
7ce44a74 130 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
db400f44 131 .pipe(
7ce44a74 132 switchMap(res => this.extractVideos(res)),
e4f0e92e 133 catchError(err => this.restExtractor.handleError(err))
db400f44 134 )
0626e7af
C
135 }
136
170726f5
C
137 getVideoChannelVideos (
138 videoChannel: VideoChannel,
139 videoPagination: ComponentPagination,
140 sort: VideoSortField
2186386c 141 ): Observable<{ videos: Video[], totalVideos: number }> {
170726f5
C
142 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
143
144 let params = new HttpParams()
145 params = this.restService.addRestGetParams(params, pagination, sort)
146
147 return this.authHttp
f5b0af50 148 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
db400f44 149 .pipe(
7ce44a74 150 switchMap(res => this.extractVideos(res)),
e4f0e92e 151 catchError(err => this.restExtractor.handleError(err))
db400f44 152 )
170726f5
C
153 }
154
22a16e36
C
155 getUserSubscriptionVideos (
156 videoPagination: ComponentPagination,
157 sort: VideoSortField
158 ): Observable<{ videos: Video[], totalVideos: number }> {
159 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
160
161 let params = new HttpParams()
162 params = this.restService.addRestGetParams(params, pagination, sort)
163
164 return this.authHttp
165 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
166 .pipe(
167 switchMap(res => this.extractVideos(res)),
168 catchError(err => this.restExtractor.handleError(err))
169 )
170 }
171
066e94c5
C
172 getVideos (
173 videoPagination: ComponentPagination,
7b87d2d5 174 sort: VideoSortField,
61b909b9 175 filter?: VideoFilter,
d59cba29 176 categoryOneOf?: number
2186386c 177 ): Observable<{ videos: Video[], totalVideos: number }> {
4635f59d 178 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
179
180 let params = new HttpParams()
181 params = this.restService.addRestGetParams(params, pagination, sort)
182
066e94c5
C
183 if (filter) {
184 params = params.set('filter', filter)
185 }
186
d59cba29
C
187 if (categoryOneOf) {
188 params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9
P
189 }
190
fd45e8f4 191 return this.authHttp
7ce44a74 192 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
db400f44 193 .pipe(
7ce44a74 194 switchMap(res => this.extractVideos(res)),
e4f0e92e 195 catchError(err => this.restExtractor.handleError(err))
db400f44 196 )
fd45e8f4
C
197 }
198
7b87d2d5 199 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
200 const feeds = [
201 {
202 label: 'rss 2.0',
203 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
204 },
205 {
206 label: 'atom 1.0',
207 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
208 },
209 {
210 label: 'json 1.0',
211 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
212 }
213 ]
214
7b87d2d5
C
215 if (params && params.keys().length !== 0) {
216 for (const feed of feeds) {
217 feed.url += '?' + params.toString()
218 }
219 }
220
cc1561f9 221 return feeds
244e76a5
RK
222 }
223
d59cba29 224 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
7b87d2d5 225 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 226
cc1561f9
C
227 if (filter) params = params.set('filter', filter)
228
d59cba29 229 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
61b909b9 230
7b87d2d5 231 return this.buildBaseFeedUrls(params)
244e76a5
RK
232 }
233
cc1561f9 234 getAccountFeedUrls (accountId: number) {
244e76a5 235 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 236 params = params.set('accountId', accountId.toString())
cc1561f9 237
7b87d2d5 238 return this.buildBaseFeedUrls(params)
244e76a5
RK
239 }
240
170726f5
C
241 getVideoChannelFeedUrls (videoChannelId: number) {
242 let params = this.restService.addRestGetParams(new HttpParams())
243 params = params.set('videoChannelId', videoChannelId.toString())
244
245 return this.buildBaseFeedUrls(params)
246 }
247
d592e0a9 248 removeVideo (id: number) {
fd45e8f4 249 return this.authHttp
db400f44
C
250 .delete(VideoService.BASE_VIDEO_URL + id)
251 .pipe(
252 map(this.restExtractor.extractDataBool),
e4f0e92e 253 catchError(err => this.restExtractor.handleError(err))
db400f44 254 )
4fd8aa32
C
255 }
256
2de96f4d
C
257 loadCompleteDescription (descriptionPath: string) {
258 return this.authHttp
db400f44
C
259 .get(environment.apiUrl + descriptionPath)
260 .pipe(
261 map(res => res[ 'description' ]),
e4f0e92e 262 catchError(err => this.restExtractor.handleError(err))
db400f44 263 )
d38b8281
C
264 }
265
0a6658fd 266 setVideoLike (id: number) {
df98563e 267 return this.setVideoRate(id, 'like')
d38b8281
C
268 }
269
0a6658fd 270 setVideoDislike (id: number) {
df98563e 271 return this.setVideoRate(id, 'dislike')
d38b8281
C
272 }
273
57a49263
BB
274 unsetVideoLike (id: number) {
275 return this.setVideoRate(id, 'none')
276 }
277
5fcbd898 278 getUserVideoRating (id: number) {
334ddfa4 279 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 280
5fcbd898 281 return this.authHttp.get<UserVideoRate>(url)
e4f0e92e 282 .pipe(catchError(err => this.restExtractor.handleError(err)))
d38b8281
C
283 }
284
57c36b27 285 extractVideos (result: ResultList<VideoServerModel>) {
7ce44a74 286 return this.serverService.localeObservable
2186386c
C
287 .pipe(
288 map(translations => {
289 const videosJson = result.data
290 const totalVideos = result.total
291 const videos: Video[] = []
292
293 for (const videoJson of videosJson) {
294 videos.push(new Video(videoJson, translations))
295 }
296
297 return { videos, totalVideos }
298 })
299 )
501bc6c2 300 }
57c36b27
C
301
302 private setVideoRate (id: number, rateType: VideoRateType) {
303 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
304 const body: UserVideoRateUpdate = {
305 rating: rateType
306 }
307
308 return this.authHttp
309 .put(url, body)
310 .pipe(
311 map(this.restExtractor.extractDataBool),
312 catchError(err => this.restExtractor.handleError(err))
313 )
314 }
dc8bc31b 315}