]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Replace current state when changing page
[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'
dc8bc31b
C
24
25@Injectable()
41a2aee3 26export class VideoService {
63c4db6d 27 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
244e76a5 28 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
dc8bc31b 29
df98563e 30 constructor (
d592e0a9 31 private authHttp: HttpClient,
de59c48f
C
32 private restExtractor: RestExtractor,
33 private restService: RestService
4fd8aa32
C
34 ) {}
35
8cac1b64
C
36 getVideoViewUrl (uuid: string) {
37 return VideoService.BASE_VIDEO_URL + uuid + '/views'
38 }
39
9d9597df 40 getVideo (uuid: string): Observable<VideoDetails> {
404b54e1
C
41 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
42 .map(videoHash => new VideoDetails(videoHash))
d592e0a9 43 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32 44 }
dc8bc31b 45
1f3e9fec 46 viewVideo (uuid: string): Observable<VideoDetails> {
8cac1b64 47 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
1f3e9fec
C
48 .map(this.restExtractor.extractDataBool)
49 .catch(this.restExtractor.handleError)
50 }
51
404b54e1 52 updateVideo (video: VideoEdit) {
6de36768
C
53 const language = video.language || undefined
54 const licence = video.licence || undefined
55 const category = video.category || undefined
56 const description = video.description || undefined
3bf1ec2e 57 const support = video.support || undefined
c24ac1c1 58
4771e000 59 const body: VideoUpdate = {
d8e689b8 60 name: video.name,
cadb46d8
C
61 category,
62 licence,
c24ac1c1 63 language,
3bf1ec2e 64 support,
cadb46d8 65 description,
fd45e8f4 66 privacy: video.privacy,
4771e000 67 tags: video.tags,
47564bbe 68 nsfw: video.nsfw,
6de36768
C
69 commentsEnabled: video.commentsEnabled,
70 thumbnailfile: video.thumbnailfile,
71 previewfile: video.previewfile
df98563e 72 }
c24ac1c1 73
6de36768
C
74 const data = objectToFormData(body)
75
76 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
d8e689b8 77 .map(this.restExtractor.extractDataBool)
df98563e 78 .catch(this.restExtractor.handleError)
d8e689b8
C
79 }
80
db7af09b 81 uploadVideo (video: FormData) {
334ddfa4 82 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 83
fd45e8f4
C
84 return this.authHttp
85 .request(req)
86 .catch(this.restExtractor.handleError)
bfb3a98f
C
87 }
88
7b87d2d5 89 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
4635f59d 90 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 91
d592e0a9
C
92 let params = new HttpParams()
93 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 94
fd45e8f4
C
95 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
96 .map(this.extractVideos)
97 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
98 }
99
066e94c5
C
100 getVideos (
101 videoPagination: ComponentPagination,
7b87d2d5 102 sort: VideoSortField,
066e94c5
C
103 filter?: VideoFilter
104 ): Observable<{ videos: Video[], totalVideos: number}> {
4635f59d 105 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
106
107 let params = new HttpParams()
108 params = this.restService.addRestGetParams(params, pagination, sort)
109
066e94c5
C
110 if (filter) {
111 params = params.set('filter', filter)
112 }
113
fd45e8f4
C
114 return this.authHttp
115 .get(VideoService.BASE_VIDEO_URL, { params })
116 .map(this.extractVideos)
117 .catch((res) => this.restExtractor.handleError(res))
118 }
119
7b87d2d5 120 buildBaseFeedUrls (params: HttpParams) {
cc1561f9
C
121 const feeds = [
122 {
123 label: 'rss 2.0',
124 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
125 },
126 {
127 label: 'atom 1.0',
128 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
129 },
130 {
131 label: 'json 1.0',
132 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
133 }
134 ]
135
7b87d2d5
C
136 if (params && params.keys().length !== 0) {
137 for (const feed of feeds) {
138 feed.url += '?' + params.toString()
139 }
140 }
141
cc1561f9 142 return feeds
244e76a5
RK
143 }
144
7b87d2d5
C
145 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
146 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
244e76a5 147
cc1561f9
C
148 if (filter) params = params.set('filter', filter)
149
7b87d2d5 150 return this.buildBaseFeedUrls(params)
244e76a5
RK
151 }
152
cc1561f9 153 getAccountFeedUrls (accountId: number) {
244e76a5 154 let params = this.restService.addRestGetParams(new HttpParams())
244e76a5 155 params = params.set('accountId', accountId.toString())
cc1561f9 156
7b87d2d5 157 return this.buildBaseFeedUrls(params)
244e76a5
RK
158 }
159
4635f59d
C
160 searchVideos (
161 search: string,
162 videoPagination: ComponentPagination,
7b87d2d5 163 sort: VideoSortField
4635f59d 164 ): Observable<{ videos: Video[], totalVideos: number}> {
f3aaa9a9 165 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9 166
4635f59d 167 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
d592e0a9
C
168
169 let params = new HttpParams()
170 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 171 params = params.append('search', search)
cf20596c 172
fd45e8f4
C
173 return this.authHttp
174 .get<ResultList<VideoServerModel>>(url, { params })
175 .map(this.extractVideos)
176 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
177 }
178
179 removeVideo (id: number) {
fd45e8f4
C
180 return this.authHttp
181 .delete(VideoService.BASE_VIDEO_URL + id)
182 .map(this.restExtractor.extractDataBool)
183 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
184 }
185
2de96f4d
C
186 loadCompleteDescription (descriptionPath: string) {
187 return this.authHttp
63c4db6d 188 .get(environment.apiUrl + descriptionPath)
2de96f4d
C
189 .map(res => res['description'])
190 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
191 }
192
0a6658fd 193 setVideoLike (id: number) {
df98563e 194 return this.setVideoRate(id, 'like')
d38b8281
C
195 }
196
0a6658fd 197 setVideoDislike (id: number) {
df98563e 198 return this.setVideoRate(id, 'dislike')
d38b8281
C
199 }
200
57a49263
BB
201 unsetVideoLike (id: number) {
202 return this.setVideoRate(id, 'none')
203 }
204
0a6658fd 205 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 206 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 207
fd45e8f4
C
208 return this.authHttp
209 .get(url)
210 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
211 }
212
0a6658fd 213 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 214 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 215 const body: UserVideoRateUpdate = {
d38b8281 216 rating: rateType
df98563e 217 }
d38b8281 218
fd45e8f4
C
219 return this.authHttp
220 .put(url, body)
221 .map(this.restExtractor.extractDataBool)
222 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
223 }
224
d592e0a9 225 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
226 const videosJson = result.data
227 const totalVideos = result.total
228 const videos = []
d592e0a9 229
de59c48f 230 for (const videoJson of videosJson) {
df98563e 231 videos.push(new Video(videoJson))
501bc6c2
C
232 }
233
df98563e 234 return { videos, totalVideos }
501bc6c2 235 }
dc8bc31b 236}