]>
Commit | Line | Data |
---|---|---|
791645e6 C |
1 | import { from, Observable } from 'rxjs' |
2 | import { catchError, concatMap, map, toArray } from 'rxjs/operators' | |
74d63469 | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d | 4 | import { Injectable } from '@angular/core' |
e724fa93 | 5 | import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' |
63c4db6d | 6 | import { environment } from '../../../environments/environment' |
e724fa93 | 7 | import { RestExtractor, RestPagination, RestService } from '../rest' |
5fcbd898 | 8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
e724fa93 C |
9 | import { SortMeta } from 'primeng/api' |
10 | import { BytesPipe } from 'ngx-pipes' | |
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
1d5342ab | 12 | import { UserRegister } from '@shared/models/users/user-register.model' |
629d8d6f C |
13 | |
14 | @Injectable() | |
e2a2d6c8 | 15 | export class UserService { |
63c4db6d | 16 | static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' |
629d8d6f | 17 | |
e724fa93 C |
18 | private bytesPipe = new BytesPipe() |
19 | ||
df98563e | 20 | constructor ( |
d592e0a9 | 21 | private authHttp: HttpClient, |
e724fa93 C |
22 | private restExtractor: RestExtractor, |
23 | private restService: RestService, | |
24 | private i18n: I18n | |
25 | ) { } | |
629d8d6f | 26 | |
a890d1e0 | 27 | changePassword (currentPassword: string, newPassword: string) { |
8094a898 C |
28 | const url = UserService.BASE_USERS_URL + 'me' |
29 | const body: UserUpdateMe = { | |
a890d1e0 | 30 | currentPassword, |
629d8d6f | 31 | password: newPassword |
df98563e | 32 | } |
629d8d6f | 33 | |
de59c48f | 34 | return this.authHttp.put(url, body) |
db400f44 C |
35 | .pipe( |
36 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 37 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 38 | ) |
629d8d6f | 39 | } |
af5e743b | 40 | |
ed56ad11 | 41 | updateMyProfile (profile: UserUpdateMe) { |
8094a898 | 42 | const url = UserService.BASE_USERS_URL + 'me' |
af5e743b | 43 | |
ed56ad11 | 44 | return this.authHttp.put(url, profile) |
db400f44 C |
45 | .pipe( |
46 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 47 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 48 | ) |
af5e743b | 49 | } |
a184c71b | 50 | |
92b9d60c C |
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 | ||
c5911fd3 C |
61 | changeAvatar (avatarForm: FormData) { |
62 | const url = UserService.BASE_USERS_URL + 'me/avatar/pick' | |
63 | ||
5fcbd898 | 64 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) |
e4f0e92e | 65 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
c5911fd3 C |
66 | } |
67 | ||
1d5342ab | 68 | signup (userCreate: UserRegister) { |
d592e0a9 | 69 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) |
db400f44 C |
70 | .pipe( |
71 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 72 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 73 | ) |
a184c71b | 74 | } |
c5911fd3 | 75 | |
ce5496d6 C |
76 | getMyVideoQuotaUsed () { |
77 | const url = UserService.BASE_USERS_URL + '/me/video-quota-used' | |
c5911fd3 | 78 | |
5fcbd898 | 79 | return this.authHttp.get<UserVideoQuota>(url) |
e4f0e92e | 80 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
c5911fd3 | 81 | } |
ecb4e35f C |
82 | |
83 | askResetPassword (email: string) { | |
84 | const url = UserService.BASE_USERS_URL + '/ask-reset-password' | |
85 | ||
86 | return this.authHttp.post(url, { email }) | |
db400f44 C |
87 | .pipe( |
88 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 89 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 90 | ) |
ecb4e35f C |
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) | |
db400f44 C |
101 | .pipe( |
102 | map(this.restExtractor.extractDataBool), | |
103 | catchError(res => this.restExtractor.handleError(res)) | |
104 | ) | |
ecb4e35f | 105 | } |
d9eaee39 JM |
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 | } | |
74d63469 GR |
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 | } | |
e724fa93 | 138 | |
1f20622f C |
139 | getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) { |
140 | // Don't update display name, the user seems to have changed it | |
141 | if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername | |
142 | ||
143 | return this.displayNameToUsername(newDisplayName) | |
144 | } | |
145 | ||
146 | displayNameToUsername (displayName: string) { | |
147 | if (!displayName) return '' | |
148 | ||
149 | return displayName | |
150 | .toLowerCase() | |
151 | .replace(/\s/g, '_') | |
152 | .replace(/[^a-z0-9_.]/g, '') | |
153 | } | |
154 | ||
e724fa93 C |
155 | /* ###### Admin methods ###### */ |
156 | ||
157 | addUser (userCreate: UserCreate) { | |
158 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
159 | .pipe( | |
160 | map(this.restExtractor.extractDataBool), | |
161 | catchError(err => this.restExtractor.handleError(err)) | |
162 | ) | |
163 | } | |
164 | ||
165 | updateUser (userId: number, userUpdate: UserUpdate) { | |
166 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
167 | .pipe( | |
168 | map(this.restExtractor.extractDataBool), | |
169 | catchError(err => this.restExtractor.handleError(err)) | |
170 | ) | |
171 | } | |
172 | ||
fc2ec87a JM |
173 | updateUsers (users: User[], userUpdate: UserUpdate) { |
174 | return from(users) | |
175 | .pipe( | |
176 | concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), | |
177 | toArray(), | |
178 | catchError(err => this.restExtractor.handleError(err)) | |
179 | ) | |
180 | } | |
181 | ||
e724fa93 C |
182 | getUser (userId: number) { |
183 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | |
184 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
185 | } | |
186 | ||
24b9417c | 187 | getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> { |
e724fa93 C |
188 | let params = new HttpParams() |
189 | params = this.restService.addRestGetParams(params, pagination, sort) | |
190 | ||
24b9417c C |
191 | if (search) params = params.append('search', search) |
192 | ||
e724fa93 C |
193 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) |
194 | .pipe( | |
195 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
196 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | |
197 | catchError(err => this.restExtractor.handleError(err)) | |
198 | ) | |
199 | } | |
200 | ||
791645e6 C |
201 | removeUser (usersArg: User | User[]) { |
202 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
203 | ||
204 | return from(users) | |
205 | .pipe( | |
206 | concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)), | |
207 | toArray(), | |
208 | catchError(err => this.restExtractor.handleError(err)) | |
209 | ) | |
e724fa93 C |
210 | } |
211 | ||
791645e6 | 212 | banUsers (usersArg: User | User[], reason?: string) { |
e724fa93 | 213 | const body = reason ? { reason } : {} |
791645e6 | 214 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] |
e724fa93 | 215 | |
791645e6 C |
216 | return from(users) |
217 | .pipe( | |
218 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)), | |
219 | toArray(), | |
220 | catchError(err => this.restExtractor.handleError(err)) | |
221 | ) | |
e724fa93 C |
222 | } |
223 | ||
791645e6 C |
224 | unbanUsers (usersArg: User | User[]) { |
225 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | |
226 | ||
227 | return from(users) | |
228 | .pipe( | |
229 | concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})), | |
230 | toArray(), | |
231 | catchError(err => this.restExtractor.handleError(err)) | |
232 | ) | |
e724fa93 C |
233 | } |
234 | ||
235 | private formatUser (user: User) { | |
236 | let videoQuota | |
237 | if (user.videoQuota === -1) { | |
238 | videoQuota = this.i18n('Unlimited') | |
239 | } else { | |
240 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) | |
241 | } | |
242 | ||
243 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) | |
244 | ||
245 | const roleLabels: { [ id in UserRole ]: string } = { | |
246 | [UserRole.USER]: this.i18n('User'), | |
247 | [UserRole.ADMINISTRATOR]: this.i18n('Administrator'), | |
248 | [UserRole.MODERATOR]: this.i18n('Moderator') | |
249 | } | |
250 | ||
251 | return Object.assign(user, { | |
252 | roleLabel: roleLabels[user.role], | |
253 | videoQuota, | |
254 | videoQuotaUsed | |
255 | }) | |
256 | } | |
629d8d6f | 257 | } |