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