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