]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/users/user.service.ts
Add ability to change email in client
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user.service.ts
CommitLineData
791645e6
C
1import { from, Observable } from 'rxjs'
2import { catchError, concatMap, map, toArray } from 'rxjs/operators'
74d63469 3import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 4import { Injectable } from '@angular/core'
e724fa93 5import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
63c4db6d 6import { environment } from '../../../environments/environment'
e724fa93 7import { RestExtractor, RestPagination, RestService } from '../rest'
5fcbd898 8import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
e724fa93
C
9import { SortMeta } from 'primeng/api'
10import { BytesPipe } from 'ngx-pipes'
11import { I18n } from '@ngx-translate/i18n-polyfill'
1d5342ab 12import { UserRegister } from '@shared/models/users/user-register.model'
629d8d6f
C
13
14@Injectable()
e2a2d6c8 15export 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
0ba5f5ba
C
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
ed56ad11 55 updateMyProfile (profile: UserUpdateMe) {
8094a898 56 const url = UserService.BASE_USERS_URL + 'me'
af5e743b 57
ed56ad11 58 return this.authHttp.put(url, profile)
db400f44
C
59 .pipe(
60 map(this.restExtractor.extractDataBool),
e4f0e92e 61 catchError(err => this.restExtractor.handleError(err))
db400f44 62 )
af5e743b 63 }
a184c71b 64
92b9d60c
C
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
c5911fd3
C
75 changeAvatar (avatarForm: FormData) {
76 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
77
5fcbd898 78 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
e4f0e92e 79 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3
C
80 }
81
1d5342ab 82 signup (userCreate: UserRegister) {
d592e0a9 83 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
db400f44
C
84 .pipe(
85 map(this.restExtractor.extractDataBool),
e4f0e92e 86 catchError(err => this.restExtractor.handleError(err))
db400f44 87 )
a184c71b 88 }
c5911fd3 89
ce5496d6
C
90 getMyVideoQuotaUsed () {
91 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
c5911fd3 92
5fcbd898 93 return this.authHttp.get<UserVideoQuota>(url)
e4f0e92e 94 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3 95 }
ecb4e35f
C
96
97 askResetPassword (email: string) {
98 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
99
100 return this.authHttp.post(url, { email })
db400f44
C
101 .pipe(
102 map(this.restExtractor.extractDataBool),
e4f0e92e 103 catchError(err => this.restExtractor.handleError(err))
db400f44 104 )
ecb4e35f
C
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)
db400f44
C
115 .pipe(
116 map(this.restExtractor.extractDataBool),
117 catchError(res => this.restExtractor.handleError(res))
118 )
ecb4e35f 119 }
d9eaee39 120
0ba5f5ba 121 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
d9eaee39
JM
122 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
123 const body = {
0ba5f5ba
C
124 verificationString,
125 isPendingEmail
d9eaee39
JM
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 }
74d63469
GR
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 }
e724fa93 153
1f20622f
C
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
e724fa93
C
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
fc2ec87a
JM
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
e724fa93
C
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
24b9417c 202 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> {
e724fa93
C
203 let params = new HttpParams()
204 params = this.restService.addRestGetParams(params, pagination, sort)
205
24b9417c
C
206 if (search) params = params.append('search', search)
207
e724fa93
C
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
791645e6
C
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 )
e724fa93
C
225 }
226
791645e6 227 banUsers (usersArg: User | User[], reason?: string) {
e724fa93 228 const body = reason ? { reason } : {}
791645e6 229 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
e724fa93 230
791645e6
C
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 )
e724fa93
C
237 }
238
791645e6
C
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 )
e724fa93
C
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 }
629d8d6f 272}