]>
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' |
df98563e | 19 | import { SortField } 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' |
dc8bc31b C |
24 | |
25 | @Injectable() | |
41a2aee3 | 26 | export 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 | ||
4635f59d C |
89 | getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> { |
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, | |
102 | sort: SortField, | |
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 | ||
244e76a5 RK |
120 | baseFeed () { |
121 | const feed = {} | |
122 | ||
123 | for (let item in FeedFormat) { | |
124 | feed[FeedFormat[item]] = VideoService.BASE_FEEDS_URL + item.toLowerCase() | |
125 | } | |
126 | ||
127 | return feed | |
128 | } | |
129 | ||
130 | getFeed ( | |
131 | filter?: VideoFilter | |
132 | ) { | |
133 | let params = this.restService.addRestGetParams(new HttpParams()) | |
134 | const feed = this.baseFeed() | |
135 | ||
136 | if (filter) { | |
137 | params = params.set('filter', filter) | |
138 | } | |
139 | for (let item in feed) { | |
140 | feed[item] = feed[item] + ((params.toString().length === 0) ? '' : '?') + params.toString() | |
141 | } | |
142 | ||
143 | return feed | |
144 | } | |
145 | ||
146 | getAccountFeed ( | |
147 | accountId: number, | |
148 | host?: string | |
149 | ) { | |
150 | let params = this.restService.addRestGetParams(new HttpParams()) | |
151 | const feed = this.baseFeed() | |
152 | ||
153 | params = params.set('accountId', accountId.toString()) | |
154 | for (let item in feed) { | |
155 | feed[item] = feed[item] + ((params.toString().length === 0) ? '' : '?') + params.toString() | |
156 | } | |
157 | ||
158 | return feed | |
159 | } | |
160 | ||
4635f59d C |
161 | searchVideos ( |
162 | search: string, | |
163 | videoPagination: ComponentPagination, | |
164 | sort: SortField | |
165 | ): Observable<{ videos: Video[], totalVideos: number}> { | |
f3aaa9a9 | 166 | const url = VideoService.BASE_VIDEO_URL + 'search' |
d592e0a9 | 167 | |
4635f59d | 168 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
d592e0a9 C |
169 | |
170 | let params = new HttpParams() | |
171 | params = this.restService.addRestGetParams(params, pagination, sort) | |
f3aaa9a9 | 172 | params = params.append('search', search) |
cf20596c | 173 | |
fd45e8f4 C |
174 | return this.authHttp |
175 | .get<ResultList<VideoServerModel>>(url, { params }) | |
176 | .map(this.extractVideos) | |
177 | .catch((res) => this.restExtractor.handleError(res)) | |
d592e0a9 C |
178 | } |
179 | ||
180 | removeVideo (id: number) { | |
fd45e8f4 C |
181 | return this.authHttp |
182 | .delete(VideoService.BASE_VIDEO_URL + id) | |
183 | .map(this.restExtractor.extractDataBool) | |
184 | .catch((res) => this.restExtractor.handleError(res)) | |
4fd8aa32 C |
185 | } |
186 | ||
2de96f4d C |
187 | loadCompleteDescription (descriptionPath: string) { |
188 | return this.authHttp | |
63c4db6d | 189 | .get(environment.apiUrl + descriptionPath) |
2de96f4d C |
190 | .map(res => res['description']) |
191 | .catch((res) => this.restExtractor.handleError(res)) | |
d38b8281 C |
192 | } |
193 | ||
0a6658fd | 194 | setVideoLike (id: number) { |
df98563e | 195 | return this.setVideoRate(id, 'like') |
d38b8281 C |
196 | } |
197 | ||
0a6658fd | 198 | setVideoDislike (id: number) { |
df98563e | 199 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
200 | } |
201 | ||
57a49263 BB |
202 | unsetVideoLike (id: number) { |
203 | return this.setVideoRate(id, 'none') | |
204 | } | |
205 | ||
0a6658fd | 206 | getUserVideoRating (id: number): Observable<UserVideoRate> { |
334ddfa4 | 207 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 208 | |
fd45e8f4 C |
209 | return this.authHttp |
210 | .get(url) | |
211 | .catch(res => this.restExtractor.handleError(res)) | |
d38b8281 C |
212 | } |
213 | ||
0a6658fd | 214 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 215 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 216 | const body: UserVideoRateUpdate = { |
d38b8281 | 217 | rating: rateType |
df98563e | 218 | } |
d38b8281 | 219 | |
fd45e8f4 C |
220 | return this.authHttp |
221 | .put(url, body) | |
222 | .map(this.restExtractor.extractDataBool) | |
223 | .catch(res => this.restExtractor.handleError(res)) | |
4f8c0eb0 C |
224 | } |
225 | ||
d592e0a9 | 226 | private extractVideos (result: ResultList<VideoServerModel>) { |
df98563e C |
227 | const videosJson = result.data |
228 | const totalVideos = result.total | |
229 | const videos = [] | |
d592e0a9 | 230 | |
de59c48f | 231 | for (const videoJson of videosJson) { |
df98563e | 232 | videos.push(new Video(videoJson)) |
501bc6c2 C |
233 | } |
234 | ||
df98563e | 235 | return { videos, totalVideos } |
501bc6c2 | 236 | } |
dc8bc31b | 237 | } |