1 import { from, Observable, of } from 'rxjs'
2 import { catchError, concatMap, map, share, shareReplay, tap, 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'
15 export class UserService {
16 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
18 private bytesPipe = new BytesPipe()
20 private userCache: { [ id: number ]: Observable<User> } = {}
23 private authHttp: HttpClient,
24 private restExtractor: RestExtractor,
25 private restService: RestService,
29 changePassword (currentPassword: string, newPassword: string) {
30 const url = UserService.BASE_USERS_URL + 'me'
31 const body: UserUpdateMe = {
36 return this.authHttp.put(url, body)
38 map(this.restExtractor.extractDataBool),
39 catchError(err => this.restExtractor.handleError(err))
43 changeEmail (password: string, newEmail: string) {
44 const url = UserService.BASE_USERS_URL + 'me'
45 const body: UserUpdateMe = {
46 currentPassword: password,
50 return this.authHttp.put(url, body)
52 map(this.restExtractor.extractDataBool),
53 catchError(err => this.restExtractor.handleError(err))
57 updateMyProfile (profile: UserUpdateMe) {
58 const url = UserService.BASE_USERS_URL + 'me'
60 return this.authHttp.put(url, profile)
62 map(this.restExtractor.extractDataBool),
63 catchError(err => this.restExtractor.handleError(err))
68 const url = UserService.BASE_USERS_URL + 'me'
70 return this.authHttp.delete(url)
72 map(this.restExtractor.extractDataBool),
73 catchError(err => this.restExtractor.handleError(err))
77 changeAvatar (avatarForm: FormData) {
78 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
80 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
81 .pipe(catchError(err => this.restExtractor.handleError(err)))
84 signup (userCreate: UserRegister) {
85 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
87 map(this.restExtractor.extractDataBool),
88 catchError(err => this.restExtractor.handleError(err))
92 getMyVideoQuotaUsed () {
93 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
95 return this.authHttp.get<UserVideoQuota>(url)
96 .pipe(catchError(err => this.restExtractor.handleError(err)))
99 askResetPassword (email: string) {
100 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
102 return this.authHttp.post(url, { email })
104 map(this.restExtractor.extractDataBool),
105 catchError(err => this.restExtractor.handleError(err))
109 resetPassword (userId: number, verificationString: string, password: string) {
110 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
116 return this.authHttp.post(url, body)
118 map(this.restExtractor.extractDataBool),
119 catchError(res => this.restExtractor.handleError(res))
123 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
124 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
130 return this.authHttp.post(url, body)
132 map(this.restExtractor.extractDataBool),
133 catchError(res => this.restExtractor.handleError(res))
137 askSendVerifyEmail (email: string) {
138 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
140 return this.authHttp.post(url, { email })
142 map(this.restExtractor.extractDataBool),
143 catchError(err => this.restExtractor.handleError(err))
147 autocomplete (search: string): Observable<string[]> {
148 const url = UserService.BASE_USERS_URL + 'autocomplete'
149 const params = new HttpParams().append('search', search)
152 .get<string[]>(url, { params })
153 .pipe(catchError(res => this.restExtractor.handleError(res)))
156 getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
157 // Don't update display name, the user seems to have changed it
158 if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername
160 return this.displayNameToUsername(newDisplayName)
163 displayNameToUsername (displayName: string) {
164 if (!displayName) return ''
169 .replace(/[^a-z0-9_.]/g, '')
172 /* ###### Admin methods ###### */
174 addUser (userCreate: UserCreate) {
175 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
177 map(this.restExtractor.extractDataBool),
178 catchError(err => this.restExtractor.handleError(err))
182 updateUser (userId: number, userUpdate: UserUpdate) {
183 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
185 map(this.restExtractor.extractDataBool),
186 catchError(err => this.restExtractor.handleError(err))
190 updateUsers (users: User[], userUpdate: UserUpdate) {
193 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
195 catchError(err => this.restExtractor.handleError(err))
199 getUserWithCache (userId: number) {
200 if (!this.userCache[userId]) {
201 this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
204 return this.userCache[userId]
207 getUser (userId: number) {
208 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
209 .pipe(catchError(err => this.restExtractor.handleError(err)))
212 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> {
213 let params = new HttpParams()
214 params = this.restService.addRestGetParams(params, pagination, sort)
216 if (search) params = params.append('search', search)
218 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
220 map(res => this.restExtractor.convertResultListDateToHuman(res)),
221 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
222 catchError(err => this.restExtractor.handleError(err))
226 removeUser (usersArg: User | User[]) {
227 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
231 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
233 catchError(err => this.restExtractor.handleError(err))
237 banUsers (usersArg: User | User[], reason?: string) {
238 const body = reason ? { reason } : {}
239 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
243 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
245 catchError(err => this.restExtractor.handleError(err))
249 unbanUsers (usersArg: User | User[]) {
250 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
254 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
256 catchError(err => this.restExtractor.handleError(err))
260 private formatUser (user: User) {
262 if (user.videoQuota === -1) {
263 videoQuota = this.i18n('Unlimited')
265 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
268 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
270 const roleLabels: { [ id in UserRole ]: string } = {
271 [UserRole.USER]: this.i18n('User'),
272 [UserRole.ADMINISTRATOR]: this.i18n('Administrator'),
273 [UserRole.MODERATOR]: this.i18n('Moderator')
276 return Object.assign(user, {
277 roleLabel: roleLabels[user.role],