]>
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 | changeEmail (password: string, newEmail: string) { | |
42 | const url = UserService.BASE_USERS_URL + 'me' | |
43 | const body: UserUpdateMe = { | |
44 | currentPassword: password, | |
45 | email: newEmail | |
46 | } | |
47 | ||
48 | return this.authHttp.put(url, body) | |
49 | .pipe( | |
50 | map(this.restExtractor.extractDataBool), | |
51 | catchError(err => this.restExtractor.handleError(err)) | |
52 | ) | |
53 | } | |
54 | ||
55 | updateMyProfile (profile: UserUpdateMe) { | |
56 | const url = UserService.BASE_USERS_URL + 'me' | |
57 | ||
58 | return this.authHttp.put(url, profile) | |
59 | .pipe( | |
60 | map(this.restExtractor.extractDataBool), | |
61 | catchError(err => this.restExtractor.handleError(err)) | |
62 | ) | |
63 | } | |
64 | ||
65 | deleteMe () { | |
66 | const url = UserService.BASE_USERS_URL + 'me' | |
67 | ||
68 | return this.authHttp.delete(url) | |
69 | .pipe( | |
70 | map(this.restExtractor.extractDataBool), | |
71 | catchError(err => this.restExtractor.handleError(err)) | |
72 | ) | |
73 | } | |
74 | ||
75 | changeAvatar (avatarForm: FormData) { | |
76 | const url = UserService.BASE_USERS_URL + 'me/avatar/pick' | |
77 | ||
78 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
79 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
80 | } | |
81 | ||
82 | signup (userCreate: UserRegister) { | |
83 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) | |
84 | .pipe( | |
85 | map(this.restExtractor.extractDataBool), | |
86 | catchError(err => this.restExtractor.handleError(err)) | |
87 | ) | |
88 | } | |
89 | ||
90 | getMyVideoQuotaUsed () { | |
91 | const url = UserService.BASE_USERS_URL + 'me/video-quota-used' | |
92 | ||
93 | return this.authHttp.get<UserVideoQuota>(url) | |
94 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
95 | } | |
96 | ||
97 | askResetPassword (email: string) { | |
98 | const url = UserService.BASE_USERS_URL + '/ask-reset-password' | |
99 | ||
100 | return this.authHttp.post(url, { email }) | |
101 | .pipe( | |
102 | map(this.restExtractor.extractDataBool), | |
103 | catchError(err => this.restExtractor.handleError(err)) | |
104 | ) | |
105 | } | |
106 | ||
107 | resetPassword (userId: number, verificationString: string, password: string) { | |
108 | const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password` | |
109 | const body = { | |
110 | verificationString, | |
111 | password | |
112 | } | |
113 | ||
114 | return this.authHttp.post(url, body) | |
115 | .pipe( | |
116 | map(this.restExtractor.extractDataBool), | |
117 | catchError(res => this.restExtractor.handleError(res)) | |
118 | ) | |
119 | } | |
120 | ||
121 | verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) { | |
122 | const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email` | |
123 | const body = { | |
124 | verificationString, | |
125 | isPendingEmail | |
126 | } | |
127 | ||
128 | return this.authHttp.post(url, body) | |
129 | .pipe( | |
130 | map(this.restExtractor.extractDataBool), | |
131 | catchError(res => this.restExtractor.handleError(res)) | |
132 | ) | |
133 | } | |
134 | ||
135 | askSendVerifyEmail (email: string) { | |
136 | const url = UserService.BASE_USERS_URL + '/ask-send-verify-email' | |
137 | ||
138 | return this.authHttp.post(url, { email }) | |
139 | .pipe( | |
140 | map(this.restExtractor.extractDataBool), | |
141 | catchError(err => this.restExtractor.handleError(err)) | |
142 | ) | |
143 | } | |
144 | ||
145 | autocomplete (search: string): Observable<string[]> { | |
146 | const url = UserService.BASE_USERS_URL + 'autocomplete' | |
147 | const params = new HttpParams().append('search', search) | |
148 | ||
149 | return this.authHttp | |
150 | .get<string[]>(url, { params }) | |
151 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
152 | } | |
153 | ||
154 | getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) { | |
155 | // Don't update display name, the user seems to have changed it | |
156 | if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername | |
157 | ||
158 | return this.displayNameToUsername(newDisplayName) | |
159 | } | |
160 | ||
161 | displayNameToUsername (displayName: string) { | |
162 | if (!displayName) return '' | |
163 | ||
164 | return displayName | |
165 | .toLowerCase() | |
166 | .replace(/\s/g, '_') | |
167 | .replace(/[^a-z0-9_.]/g, '') | |
168 | } | |
169 | ||
170 | /* ###### Admin methods ###### */ | |
171 | ||
172 | addUser (userCreate: UserCreate) { | |
173 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
174 | .pipe( | |
175 | map(this.restExtractor.extractDataBool), | |
176 | catchError(err => this.restExtractor.handleError(err)) | |
177 | ) | |
178 | } | |
179 | ||
180 | updateUser (userId: number, userUpdate: UserUpdate) { | |
181 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
182 | .pipe( | |
183 | map(this.restExtractor.extractDataBool), | |
184 | catchError(err => this.restExtractor.handleError(err)) | |
185 | ) | |
186 | } | |
187 | ||
188 | updateUsers (users: User[], userUpdate: UserUpdate) { | |
189 | return from(users) | |
190 | .pipe( | |
191 | concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), | |
192 | toArray(), | |
193 | catchError(err => this.restExtractor.handleError(err)) | |
194 | ) | |
195 | } | |
196 | ||
197 | getUser (userId: number) { | |
198 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | |
199 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
200 | } | |
201 | ||
202 | getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> { | |
203 | let params = new HttpParams() | |
204 | params = this.restService.addRestGetParams(params, pagination, sort) | |
205 | ||
206 | if (search) params = params.append('search', search) | |
207 | ||
208 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | |
209 | .pipe( | |
210 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
211 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | |
212 | catchError(err => this.restExtractor.handleError(err)) | |
213 | ) | |
214 | } | |
215 | ||
216 | removeUser (usersArg: User | User[]) { | |
217 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
218 | ||
219 | return from(users) | |
220 | .pipe( | |
221 | concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)), | |
222 | toArray(), | |
223 | catchError(err => this.restExtractor.handleError(err)) | |
224 | ) | |
225 | } | |
226 | ||
227 | banUsers (usersArg: User | User[], reason?: string) { | |
228 | const body = reason ? { reason } : {} | |
229 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
230 | ||
231 | return from(users) | |
232 | .pipe( | |
233 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)), | |
234 | toArray(), | |
235 | catchError(err => this.restExtractor.handleError(err)) | |
236 | ) | |
237 | } | |
238 | ||
239 | unbanUsers (usersArg: User | User[]) { | |
240 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
241 | ||
242 | return from(users) | |
243 | .pipe( | |
244 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})), | |
245 | toArray(), | |
246 | catchError(err => this.restExtractor.handleError(err)) | |
247 | ) | |
248 | } | |
249 | ||
250 | private formatUser (user: User) { | |
251 | let videoQuota | |
252 | if (user.videoQuota === -1) { | |
253 | videoQuota = this.i18n('Unlimited') | |
254 | } else { | |
255 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) | |
256 | } | |
257 | ||
258 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) | |
259 | ||
260 | const roleLabels: { [ id in UserRole ]: string } = { | |
261 | [UserRole.USER]: this.i18n('User'), | |
262 | [UserRole.ADMINISTRATOR]: this.i18n('Administrator'), | |
263 | [UserRole.MODERATOR]: this.i18n('Moderator') | |
264 | } | |
265 | ||
266 | return Object.assign(user, { | |
267 | roleLabel: roleLabels[user.role], | |
268 | videoQuota, | |
269 | videoQuotaUsed | |
270 | }) | |
271 | } | |
272 | } |