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