]>
Commit | Line | Data |
---|---|---|
1 | import { from, Observable } from 'rxjs' | |
2 | import { catchError, concatMap, map, toArray } from 'rxjs/operators' | |
3 | import { HttpClient, HttpParams } from '@angular/common/http' | |
4 | import { Injectable } from '@angular/core' | |
5 | import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' | |
6 | import { environment } from '../../../environments/environment' | |
7 | import { RestExtractor, RestPagination, RestService } from '../rest' | |
8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' | |
9 | import { SortMeta } from 'primeng/api' | |
10 | import { BytesPipe } from 'ngx-pipes' | |
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
12 | import { UserRegister } from '@shared/models/users/user-register.model' | |
13 | ||
14 | @Injectable() | |
15 | export class UserService { | |
16 | static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' | |
17 | ||
18 | private bytesPipe = new BytesPipe() | |
19 | ||
20 | constructor ( | |
21 | private authHttp: HttpClient, | |
22 | private restExtractor: RestExtractor, | |
23 | private restService: RestService, | |
24 | private i18n: I18n | |
25 | ) { } | |
26 | ||
27 | changePassword (currentPassword: string, newPassword: string) { | |
28 | const url = UserService.BASE_USERS_URL + 'me' | |
29 | const body: UserUpdateMe = { | |
30 | currentPassword, | |
31 | password: newPassword | |
32 | } | |
33 | ||
34 | return this.authHttp.put(url, body) | |
35 | .pipe( | |
36 | map(this.restExtractor.extractDataBool), | |
37 | catchError(err => this.restExtractor.handleError(err)) | |
38 | ) | |
39 | } | |
40 | ||
41 | updateMyProfile (profile: UserUpdateMe) { | |
42 | const url = UserService.BASE_USERS_URL + 'me' | |
43 | ||
44 | return this.authHttp.put(url, profile) | |
45 | .pipe( | |
46 | map(this.restExtractor.extractDataBool), | |
47 | catchError(err => this.restExtractor.handleError(err)) | |
48 | ) | |
49 | } | |
50 | ||
51 | deleteMe () { | |
52 | const url = UserService.BASE_USERS_URL + 'me' | |
53 | ||
54 | return this.authHttp.delete(url) | |
55 | .pipe( | |
56 | map(this.restExtractor.extractDataBool), | |
57 | catchError(err => this.restExtractor.handleError(err)) | |
58 | ) | |
59 | } | |
60 | ||
61 | changeAvatar (avatarForm: FormData) { | |
62 | const url = UserService.BASE_USERS_URL + 'me/avatar/pick' | |
63 | ||
64 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
65 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
66 | } | |
67 | ||
68 | signup (userCreate: UserRegister) { | |
69 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) | |
70 | .pipe( | |
71 | map(this.restExtractor.extractDataBool), | |
72 | catchError(err => this.restExtractor.handleError(err)) | |
73 | ) | |
74 | } | |
75 | ||
76 | getMyVideoQuotaUsed () { | |
77 | const url = UserService.BASE_USERS_URL + '/me/video-quota-used' | |
78 | ||
79 | return this.authHttp.get<UserVideoQuota>(url) | |
80 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
81 | } | |
82 | ||
83 | askResetPassword (email: string) { | |
84 | const url = UserService.BASE_USERS_URL + '/ask-reset-password' | |
85 | ||
86 | return this.authHttp.post(url, { email }) | |
87 | .pipe( | |
88 | map(this.restExtractor.extractDataBool), | |
89 | catchError(err => this.restExtractor.handleError(err)) | |
90 | ) | |
91 | } | |
92 | ||
93 | resetPassword (userId: number, verificationString: string, password: string) { | |
94 | const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password` | |
95 | const body = { | |
96 | verificationString, | |
97 | password | |
98 | } | |
99 | ||
100 | return this.authHttp.post(url, body) | |
101 | .pipe( | |
102 | map(this.restExtractor.extractDataBool), | |
103 | catchError(res => this.restExtractor.handleError(res)) | |
104 | ) | |
105 | } | |
106 | ||
107 | verifyEmail (userId: number, verificationString: string) { | |
108 | const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email` | |
109 | const body = { | |
110 | verificationString | |
111 | } | |
112 | ||
113 | return this.authHttp.post(url, body) | |
114 | .pipe( | |
115 | map(this.restExtractor.extractDataBool), | |
116 | catchError(res => this.restExtractor.handleError(res)) | |
117 | ) | |
118 | } | |
119 | ||
120 | askSendVerifyEmail (email: string) { | |
121 | const url = UserService.BASE_USERS_URL + '/ask-send-verify-email' | |
122 | ||
123 | return this.authHttp.post(url, { email }) | |
124 | .pipe( | |
125 | map(this.restExtractor.extractDataBool), | |
126 | catchError(err => this.restExtractor.handleError(err)) | |
127 | ) | |
128 | } | |
129 | ||
130 | autocomplete (search: string): Observable<string[]> { | |
131 | const url = UserService.BASE_USERS_URL + 'autocomplete' | |
132 | const params = new HttpParams().append('search', search) | |
133 | ||
134 | return this.authHttp | |
135 | .get<string[]>(url, { params }) | |
136 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
137 | } | |
138 | ||
139 | /* ###### Admin methods ###### */ | |
140 | ||
141 | addUser (userCreate: UserCreate) { | |
142 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
143 | .pipe( | |
144 | map(this.restExtractor.extractDataBool), | |
145 | catchError(err => this.restExtractor.handleError(err)) | |
146 | ) | |
147 | } | |
148 | ||
149 | updateUser (userId: number, userUpdate: UserUpdate) { | |
150 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
151 | .pipe( | |
152 | map(this.restExtractor.extractDataBool), | |
153 | catchError(err => this.restExtractor.handleError(err)) | |
154 | ) | |
155 | } | |
156 | ||
157 | updateUsers (users: User[], userUpdate: UserUpdate) { | |
158 | return from(users) | |
159 | .pipe( | |
160 | concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), | |
161 | toArray(), | |
162 | catchError(err => this.restExtractor.handleError(err)) | |
163 | ) | |
164 | } | |
165 | ||
166 | getUser (userId: number) { | |
167 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | |
168 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
169 | } | |
170 | ||
171 | getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> { | |
172 | let params = new HttpParams() | |
173 | params = this.restService.addRestGetParams(params, pagination, sort) | |
174 | ||
175 | if (search) params = params.append('search', search) | |
176 | ||
177 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | |
178 | .pipe( | |
179 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
180 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | |
181 | catchError(err => this.restExtractor.handleError(err)) | |
182 | ) | |
183 | } | |
184 | ||
185 | removeUser (usersArg: User | User[]) { | |
186 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
187 | ||
188 | return from(users) | |
189 | .pipe( | |
190 | concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)), | |
191 | toArray(), | |
192 | catchError(err => this.restExtractor.handleError(err)) | |
193 | ) | |
194 | } | |
195 | ||
196 | banUsers (usersArg: User | User[], reason?: string) { | |
197 | const body = reason ? { reason } : {} | |
198 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
199 | ||
200 | return from(users) | |
201 | .pipe( | |
202 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)), | |
203 | toArray(), | |
204 | catchError(err => this.restExtractor.handleError(err)) | |
205 | ) | |
206 | } | |
207 | ||
208 | unbanUsers (usersArg: User | User[]) { | |
209 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
210 | ||
211 | return from(users) | |
212 | .pipe( | |
213 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})), | |
214 | toArray(), | |
215 | catchError(err => this.restExtractor.handleError(err)) | |
216 | ) | |
217 | } | |
218 | ||
219 | private formatUser (user: User) { | |
220 | let videoQuota | |
221 | if (user.videoQuota === -1) { | |
222 | videoQuota = this.i18n('Unlimited') | |
223 | } else { | |
224 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) | |
225 | } | |
226 | ||
227 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) | |
228 | ||
229 | const roleLabels: { [ id in UserRole ]: string } = { | |
230 | [UserRole.USER]: this.i18n('User'), | |
231 | [UserRole.ADMINISTRATOR]: this.i18n('Administrator'), | |
232 | [UserRole.MODERATOR]: this.i18n('Moderator') | |
233 | } | |
234 | ||
235 | return Object.assign(user, { | |
236 | roleLabel: roleLabels[user.role], | |
237 | videoQuota, | |
238 | videoQuotaUsed | |
239 | }) | |
240 | } | |
241 | } |