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