]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | @Injectable() | |
26 | export class VideoService { | |
27 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
28 | private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.' | |
29 | ||
30 | constructor ( | |
31 | private authHttp: HttpClient, | |
32 | private restExtractor: RestExtractor, | |
33 | private restService: RestService | |
34 | ) {} | |
35 | ||
36 | getVideoViewUrl (uuid: string) { | |
37 | return VideoService.BASE_VIDEO_URL + uuid + '/views' | |
38 | } | |
39 | ||
40 | getVideo (uuid: string): Observable<VideoDetails> { | |
41 | return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid) | |
42 | .map(videoHash => new VideoDetails(videoHash)) | |
43 | .catch((res) => this.restExtractor.handleError(res)) | |
44 | } | |
45 | ||
46 | viewVideo (uuid: string): Observable<VideoDetails> { | |
47 | return this.authHttp.post(this.getVideoViewUrl(uuid), {}) | |
48 | .map(this.restExtractor.extractDataBool) | |
49 | .catch(this.restExtractor.handleError) | |
50 | } | |
51 | ||
52 | updateVideo (video: VideoEdit) { | |
53 | const language = video.language || undefined | |
54 | const licence = video.licence || undefined | |
55 | const category = video.category || undefined | |
56 | const description = video.description || undefined | |
57 | const support = video.support || undefined | |
58 | ||
59 | const body: VideoUpdate = { | |
60 | name: video.name, | |
61 | category, | |
62 | licence, | |
63 | language, | |
64 | support, | |
65 | description, | |
66 | privacy: video.privacy, | |
67 | tags: video.tags, | |
68 | nsfw: video.nsfw, | |
69 | commentsEnabled: video.commentsEnabled, | |
70 | thumbnailfile: video.thumbnailfile, | |
71 | previewfile: video.previewfile | |
72 | } | |
73 | ||
74 | const data = objectToFormData(body) | |
75 | ||
76 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
77 | .map(this.restExtractor.extractDataBool) | |
78 | .catch(this.restExtractor.handleError) | |
79 | } | |
80 | ||
81 | uploadVideo (video: FormData) { | |
82 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) | |
83 | ||
84 | return this.authHttp | |
85 | .request(req) | |
86 | .catch(this.restExtractor.handleError) | |
87 | } | |
88 | ||
89 | getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> { | |
90 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
91 | ||
92 | let params = new HttpParams() | |
93 | params = this.restService.addRestGetParams(params, pagination, sort) | |
94 | ||
95 | return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params }) | |
96 | .map(this.extractVideos) | |
97 | .catch((res) => this.restExtractor.handleError(res)) | |
98 | } | |
99 | ||
100 | getVideos ( | |
101 | videoPagination: ComponentPagination, | |
102 | sort: VideoSortField, | |
103 | filter?: VideoFilter | |
104 | ): Observable<{ videos: Video[], totalVideos: number}> { | |
105 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
106 | ||
107 | let params = new HttpParams() | |
108 | params = this.restService.addRestGetParams(params, pagination, sort) | |
109 | ||
110 | if (filter) { | |
111 | params = params.set('filter', filter) | |
112 | } | |
113 | ||
114 | return this.authHttp | |
115 | .get(VideoService.BASE_VIDEO_URL, { params }) | |
116 | .map(this.extractVideos) | |
117 | .catch((res) => this.restExtractor.handleError(res)) | |
118 | } | |
119 | ||
120 | buildBaseFeedUrls (params: HttpParams) { | |
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 | ||
136 | if (params && params.keys().length !== 0) { | |
137 | for (const feed of feeds) { | |
138 | feed.url += '?' + params.toString() | |
139 | } | |
140 | } | |
141 | ||
142 | return feeds | |
143 | } | |
144 | ||
145 | getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) { | |
146 | let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort) | |
147 | ||
148 | if (filter) params = params.set('filter', filter) | |
149 | ||
150 | return this.buildBaseFeedUrls(params) | |
151 | } | |
152 | ||
153 | getAccountFeedUrls (accountId: number) { | |
154 | let params = this.restService.addRestGetParams(new HttpParams()) | |
155 | params = params.set('accountId', accountId.toString()) | |
156 | ||
157 | return this.buildBaseFeedUrls(params) | |
158 | } | |
159 | ||
160 | searchVideos ( | |
161 | search: string, | |
162 | videoPagination: ComponentPagination, | |
163 | sort: VideoSortField | |
164 | ): Observable<{ videos: Video[], totalVideos: number}> { | |
165 | const url = VideoService.BASE_VIDEO_URL + 'search' | |
166 | ||
167 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
168 | ||
169 | let params = new HttpParams() | |
170 | params = this.restService.addRestGetParams(params, pagination, sort) | |
171 | params = params.append('search', search) | |
172 | ||
173 | return this.authHttp | |
174 | .get<ResultList<VideoServerModel>>(url, { params }) | |
175 | .map(this.extractVideos) | |
176 | .catch((res) => this.restExtractor.handleError(res)) | |
177 | } | |
178 | ||
179 | removeVideo (id: number) { | |
180 | return this.authHttp | |
181 | .delete(VideoService.BASE_VIDEO_URL + id) | |
182 | .map(this.restExtractor.extractDataBool) | |
183 | .catch((res) => this.restExtractor.handleError(res)) | |
184 | } | |
185 | ||
186 | loadCompleteDescription (descriptionPath: string) { | |
187 | return this.authHttp | |
188 | .get(environment.apiUrl + descriptionPath) | |
189 | .map(res => res['description']) | |
190 | .catch((res) => this.restExtractor.handleError(res)) | |
191 | } | |
192 | ||
193 | setVideoLike (id: number) { | |
194 | return this.setVideoRate(id, 'like') | |
195 | } | |
196 | ||
197 | setVideoDislike (id: number) { | |
198 | return this.setVideoRate(id, 'dislike') | |
199 | } | |
200 | ||
201 | unsetVideoLike (id: number) { | |
202 | return this.setVideoRate(id, 'none') | |
203 | } | |
204 | ||
205 | getUserVideoRating (id: number): Observable<UserVideoRate> { | |
206 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' | |
207 | ||
208 | return this.authHttp | |
209 | .get(url) | |
210 | .catch(res => this.restExtractor.handleError(res)) | |
211 | } | |
212 | ||
213 | private setVideoRate (id: number, rateType: VideoRateType) { | |
214 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' | |
215 | const body: UserVideoRateUpdate = { | |
216 | rating: rateType | |
217 | } | |
218 | ||
219 | return this.authHttp | |
220 | .put(url, body) | |
221 | .map(this.restExtractor.extractDataBool) | |
222 | .catch(res => this.restExtractor.handleError(res)) | |
223 | } | |
224 | ||
225 | private extractVideos (result: ResultList<VideoServerModel>) { | |
226 | const videosJson = result.data | |
227 | const totalVideos = result.total | |
228 | const videos = [] | |
229 | ||
230 | for (const videoJson of videosJson) { | |
231 | videos.push(new Video(videoJson)) | |
232 | } | |
233 | ||
234 | return { videos, totalVideos } | |
235 | } | |
236 | } |