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