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