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 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()
21 private authHttp: HttpClient,
22 private restExtractor: RestExtractor,
23 private restService: RestService,
27 changePassword (currentPassword: string, newPassword: string) {
28 const url = UserService.BASE_USERS_URL + 'me'
29 const body: UserUpdateMe = {
34 return this.authHttp.put(url, body)
36 map(this.restExtractor.extractDataBool),
37 catchError(err => this.restExtractor.handleError(err))
41 updateMyProfile (profile: UserUpdateMe) {
42 const url = UserService.BASE_USERS_URL + 'me'
44 return this.authHttp.put(url, profile)
46 map(this.restExtractor.extractDataBool),
47 catchError(err => this.restExtractor.handleError(err))
52 const url = UserService.BASE_USERS_URL + 'me'
54 return this.authHttp.delete(url)
56 map(this.restExtractor.extractDataBool),
57 catchError(err => this.restExtractor.handleError(err))
61 changeAvatar (avatarForm: FormData) {
62 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
64 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
65 .pipe(catchError(err => this.restExtractor.handleError(err)))
68 signup (userCreate: UserRegister) {
69 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
71 map(this.restExtractor.extractDataBool),
72 catchError(err => this.restExtractor.handleError(err))
76 getMyVideoQuotaUsed () {
77 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
79 return this.authHttp.get<UserVideoQuota>(url)
80 .pipe(catchError(err => this.restExtractor.handleError(err)))
83 askResetPassword (email: string) {
84 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
86 return this.authHttp.post(url, { email })
88 map(this.restExtractor.extractDataBool),
89 catchError(err => this.restExtractor.handleError(err))
93 resetPassword (userId: number, verificationString: string, password: string) {
94 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
100 return this.authHttp.post(url, body)
102 map(this.restExtractor.extractDataBool),
103 catchError(res => this.restExtractor.handleError(res))
107 verifyEmail (userId: number, verificationString: string) {
108 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
113 return this.authHttp.post(url, body)
115 map(this.restExtractor.extractDataBool),
116 catchError(res => this.restExtractor.handleError(res))
120 askSendVerifyEmail (email: string) {
121 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
123 return this.authHttp.post(url, { email })
125 map(this.restExtractor.extractDataBool),
126 catchError(err => this.restExtractor.handleError(err))
130 autocomplete (search: string): Observable<string[]> {
131 const url = UserService.BASE_USERS_URL + 'autocomplete'
132 const params = new HttpParams().append('search', search)
135 .get<string[]>(url, { params })
136 .pipe(catchError(res => this.restExtractor.handleError(res)))
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
143 return this.displayNameToUsername(newDisplayName)
146 displayNameToUsername (displayName: string) {
147 if (!displayName) return ''
152 .replace(/[^a-z0-9_.]/g, '')
155 /* ###### Admin methods ###### */
157 addUser (userCreate: UserCreate) {
158 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
160 map(this.restExtractor.extractDataBool),
161 catchError(err => this.restExtractor.handleError(err))
165 updateUser (userId: number, userUpdate: UserUpdate) {
166 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
168 map(this.restExtractor.extractDataBool),
169 catchError(err => this.restExtractor.handleError(err))
173 updateUsers (users: User[], userUpdate: UserUpdate) {
176 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
178 catchError(err => this.restExtractor.handleError(err))
182 getUser (userId: number) {
183 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
184 .pipe(catchError(err => this.restExtractor.handleError(err)))
187 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> {
188 let params = new HttpParams()
189 params = this.restService.addRestGetParams(params, pagination, sort)
191 if (search) params = params.append('search', search)
193 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
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))
201 removeUser (usersArg: User | User[]) {
202 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
206 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
208 catchError(err => this.restExtractor.handleError(err))
212 banUsers (usersArg: User | User[], reason?: string) {
213 const body = reason ? { reason } : {}
214 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
218 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
220 catchError(err => this.restExtractor.handleError(err))
224 unbanUsers (usersArg: User | User[]) {
225 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
229 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
231 catchError(err => this.restExtractor.handleError(err))
235 private formatUser (user: User) {
237 if (user.videoQuota === -1) {
238 videoQuota = this.i18n('Unlimited')
240 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
243 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
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')
251 return Object.assign(user, {
252 roleLabel: roleLabels[user.role],