]>
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' |
202f6b6c C |
11 | import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type' |
12 | import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model' | |
63c4db6d | 13 | import { environment } from '../../../environments/environment' |
4635f59d | 14 | import { ComponentPagination } from '../rest/component-pagination.model' |
202f6b6c C |
15 | import { RestExtractor } from '../rest/rest-extractor.service' |
16 | import { RestService } from '../rest/rest.service' | |
202f6b6c | 17 | import { UserService } from '../users/user.service' |
df98563e | 18 | import { SortField } from './sort-field.type' |
404b54e1 C |
19 | import { VideoDetails } from './video-details.model' |
20 | import { VideoEdit } from './video-edit.model' | |
202f6b6c | 21 | import { Video } from './video.model' |
6de36768 | 22 | import { objectToFormData } from '@app/shared/misc/utils' |
dc8bc31b C |
23 | |
24 | @Injectable() | |
41a2aee3 | 25 | export class VideoService { |
63c4db6d | 26 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' |
dc8bc31b | 27 | |
df98563e | 28 | constructor ( |
d592e0a9 | 29 | private authHttp: HttpClient, |
de59c48f C |
30 | private restExtractor: RestExtractor, |
31 | private restService: RestService | |
4fd8aa32 C |
32 | ) {} |
33 | ||
8cac1b64 C |
34 | getVideoViewUrl (uuid: string) { |
35 | return VideoService.BASE_VIDEO_URL + uuid + '/views' | |
36 | } | |
37 | ||
9d9597df | 38 | getVideo (uuid: string): Observable<VideoDetails> { |
404b54e1 C |
39 | return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid) |
40 | .map(videoHash => new VideoDetails(videoHash)) | |
d592e0a9 | 41 | .catch((res) => this.restExtractor.handleError(res)) |
4fd8aa32 | 42 | } |
dc8bc31b | 43 | |
1f3e9fec | 44 | viewVideo (uuid: string): Observable<VideoDetails> { |
8cac1b64 | 45 | return this.authHttp.post(this.getVideoViewUrl(uuid), {}) |
1f3e9fec C |
46 | .map(this.restExtractor.extractDataBool) |
47 | .catch(this.restExtractor.handleError) | |
48 | } | |
49 | ||
404b54e1 | 50 | updateVideo (video: VideoEdit) { |
6de36768 C |
51 | const language = video.language || undefined |
52 | const licence = video.licence || undefined | |
53 | const category = video.category || undefined | |
54 | const description = video.description || undefined | |
3bf1ec2e | 55 | const support = video.support || undefined |
c24ac1c1 | 56 | |
4771e000 | 57 | const body: VideoUpdate = { |
d8e689b8 | 58 | name: video.name, |
cadb46d8 C |
59 | category, |
60 | licence, | |
c24ac1c1 | 61 | language, |
3bf1ec2e | 62 | support, |
cadb46d8 | 63 | description, |
fd45e8f4 | 64 | privacy: video.privacy, |
4771e000 | 65 | tags: video.tags, |
47564bbe | 66 | nsfw: video.nsfw, |
6de36768 C |
67 | commentsEnabled: video.commentsEnabled, |
68 | thumbnailfile: video.thumbnailfile, | |
69 | previewfile: video.previewfile | |
df98563e | 70 | } |
c24ac1c1 | 71 | |
6de36768 C |
72 | const data = objectToFormData(body) |
73 | ||
74 | return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data) | |
d8e689b8 | 75 | .map(this.restExtractor.extractDataBool) |
df98563e | 76 | .catch(this.restExtractor.handleError) |
d8e689b8 C |
77 | } |
78 | ||
db7af09b | 79 | uploadVideo (video: FormData) { |
334ddfa4 | 80 | const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true }) |
bfb3a98f | 81 | |
fd45e8f4 C |
82 | return this.authHttp |
83 | .request(req) | |
84 | .catch(this.restExtractor.handleError) | |
bfb3a98f C |
85 | } |
86 | ||
4635f59d C |
87 | getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> { |
88 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) | |
cf20596c | 89 | |
d592e0a9 C |
90 | let params = new HttpParams() |
91 | params = this.restService.addRestGetParams(params, pagination, sort) | |
dc8bc31b | 92 | |
fd45e8f4 C |
93 | return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params }) |
94 | .map(this.extractVideos) | |
95 | .catch((res) => this.restExtractor.handleError(res)) | |
dc8bc31b C |
96 | } |
97 | ||
066e94c5 C |
98 | getVideos ( |
99 | videoPagination: ComponentPagination, | |
100 | sort: SortField, | |
101 | filter?: VideoFilter | |
102 | ): Observable<{ videos: Video[], totalVideos: number}> { | |
4635f59d | 103 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
fd45e8f4 C |
104 | |
105 | let params = new HttpParams() | |
106 | params = this.restService.addRestGetParams(params, pagination, sort) | |
107 | ||
066e94c5 C |
108 | if (filter) { |
109 | params = params.set('filter', filter) | |
110 | } | |
111 | ||
fd45e8f4 C |
112 | return this.authHttp |
113 | .get(VideoService.BASE_VIDEO_URL, { params }) | |
114 | .map(this.extractVideos) | |
115 | .catch((res) => this.restExtractor.handleError(res)) | |
116 | } | |
117 | ||
4635f59d C |
118 | searchVideos ( |
119 | search: string, | |
120 | videoPagination: ComponentPagination, | |
121 | sort: SortField | |
122 | ): Observable<{ videos: Video[], totalVideos: number}> { | |
f3aaa9a9 | 123 | const url = VideoService.BASE_VIDEO_URL + 'search' |
d592e0a9 | 124 | |
4635f59d | 125 | const pagination = this.restService.componentPaginationToRestPagination(videoPagination) |
d592e0a9 C |
126 | |
127 | let params = new HttpParams() | |
128 | params = this.restService.addRestGetParams(params, pagination, sort) | |
f3aaa9a9 | 129 | params = params.append('search', search) |
cf20596c | 130 | |
fd45e8f4 C |
131 | return this.authHttp |
132 | .get<ResultList<VideoServerModel>>(url, { params }) | |
133 | .map(this.extractVideos) | |
134 | .catch((res) => this.restExtractor.handleError(res)) | |
d592e0a9 C |
135 | } |
136 | ||
137 | removeVideo (id: number) { | |
fd45e8f4 C |
138 | return this.authHttp |
139 | .delete(VideoService.BASE_VIDEO_URL + id) | |
140 | .map(this.restExtractor.extractDataBool) | |
141 | .catch((res) => this.restExtractor.handleError(res)) | |
4fd8aa32 C |
142 | } |
143 | ||
2de96f4d C |
144 | loadCompleteDescription (descriptionPath: string) { |
145 | return this.authHttp | |
63c4db6d | 146 | .get(environment.apiUrl + descriptionPath) |
2de96f4d C |
147 | .map(res => res['description']) |
148 | .catch((res) => this.restExtractor.handleError(res)) | |
d38b8281 C |
149 | } |
150 | ||
0a6658fd | 151 | setVideoLike (id: number) { |
df98563e | 152 | return this.setVideoRate(id, 'like') |
d38b8281 C |
153 | } |
154 | ||
0a6658fd | 155 | setVideoDislike (id: number) { |
df98563e | 156 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
157 | } |
158 | ||
57a49263 BB |
159 | unsetVideoLike (id: number) { |
160 | return this.setVideoRate(id, 'none') | |
161 | } | |
162 | ||
0a6658fd | 163 | getUserVideoRating (id: number): Observable<UserVideoRate> { |
334ddfa4 | 164 | const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' |
d38b8281 | 165 | |
fd45e8f4 C |
166 | return this.authHttp |
167 | .get(url) | |
168 | .catch(res => this.restExtractor.handleError(res)) | |
d38b8281 C |
169 | } |
170 | ||
0a6658fd | 171 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 172 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 173 | const body: UserVideoRateUpdate = { |
d38b8281 | 174 | rating: rateType |
df98563e | 175 | } |
d38b8281 | 176 | |
fd45e8f4 C |
177 | return this.authHttp |
178 | .put(url, body) | |
179 | .map(this.restExtractor.extractDataBool) | |
180 | .catch(res => this.restExtractor.handleError(res)) | |
4f8c0eb0 C |
181 | } |
182 | ||
d592e0a9 | 183 | private extractVideos (result: ResultList<VideoServerModel>) { |
df98563e C |
184 | const videosJson = result.data |
185 | const totalVideos = result.total | |
186 | const videos = [] | |
d592e0a9 | 187 | |
de59c48f | 188 | for (const videoJson of videosJson) { |
df98563e | 189 | videos.push(new Video(videoJson)) |
501bc6c2 C |
190 | } |
191 | ||
df98563e | 192 | return { videos, totalVideos } |
501bc6c2 | 193 | } |
dc8bc31b | 194 | } |