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