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