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 changeEmail (password: string, newEmail: string) {
42 const url = UserService.BASE_USERS_URL + 'me'
43 const body: UserUpdateMe = {
44 currentPassword: password,
48 return this.authHttp.put(url, body)
50 map(this.restExtractor.extractDataBool),
51 catchError(err => this.restExtractor.handleError(err))
55 updateMyProfile (profile: UserUpdateMe) {
56 const url = UserService.BASE_USERS_URL + 'me'
58 return this.authHttp.put(url, profile)
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
66 const url = UserService.BASE_USERS_URL + 'me'
68 return this.authHttp.delete(url)
70 map(this.restExtractor.extractDataBool),
71 catchError(err => this.restExtractor.handleError(err))
75 changeAvatar (avatarForm: FormData) {
76 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
78 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
79 .pipe(catchError(err => this.restExtractor.handleError(err)))
82 signup (userCreate: UserRegister) {
83 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
85 map(this.restExtractor.extractDataBool),
86 catchError(err => this.restExtractor.handleError(err))
90 getMyVideoQuotaUsed () {
91 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
93 return this.authHttp.get<UserVideoQuota>(url)
94 .pipe(catchError(err => this.restExtractor.handleError(err)))
97 askResetPassword (email: string) {
98 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
100 return this.authHttp.post(url, { email })
102 map(this.restExtractor.extractDataBool),
103 catchError(err => this.restExtractor.handleError(err))
107 resetPassword (userId: number, verificationString: string, password: string) {
108 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
114 return this.authHttp.post(url, body)
116 map(this.restExtractor.extractDataBool),
117 catchError(res => this.restExtractor.handleError(res))
121 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
122 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
128 return this.authHttp.post(url, body)
130 map(this.restExtractor.extractDataBool),
131 catchError(res => this.restExtractor.handleError(res))
135 askSendVerifyEmail (email: string) {
136 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
138 return this.authHttp.post(url, { email })
140 map(this.restExtractor.extractDataBool),
141 catchError(err => this.restExtractor.handleError(err))
145 autocomplete (search: string): Observable<string[]> {
146 const url = UserService.BASE_USERS_URL + 'autocomplete'
147 const params = new HttpParams().append('search', search)
150 .get<string[]>(url, { params })
151 .pipe(catchError(res => this.restExtractor.handleError(res)))
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
158 return this.displayNameToUsername(newDisplayName)
161 displayNameToUsername (displayName: string) {
162 if (!displayName) return ''
167 .replace(/[^a-z0-9_.]/g, '')
170 /* ###### Admin methods ###### */
172 addUser (userCreate: UserCreate) {
173 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
175 map(this.restExtractor.extractDataBool),
176 catchError(err => this.restExtractor.handleError(err))
180 updateUser (userId: number, userUpdate: UserUpdate) {
181 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
183 map(this.restExtractor.extractDataBool),
184 catchError(err => this.restExtractor.handleError(err))
188 updateUsers (users: User[], userUpdate: UserUpdate) {
191 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
193 catchError(err => this.restExtractor.handleError(err))
197 getUser (userId: number) {
198 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
199 .pipe(catchError(err => this.restExtractor.handleError(err)))
202 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> {
203 let params = new HttpParams()
204 params = this.restService.addRestGetParams(params, pagination, sort)
206 if (search) params = params.append('search', search)
208 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
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))
216 removeUser (usersArg: User | User[]) {
217 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
221 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
223 catchError(err => this.restExtractor.handleError(err))
227 banUsers (usersArg: User | User[], reason?: string) {
228 const body = reason ? { reason } : {}
229 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
233 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
235 catchError(err => this.restExtractor.handleError(err))
239 unbanUsers (usersArg: User | User[]) {
240 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
244 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
246 catchError(err => this.restExtractor.handleError(err))
250 private formatUser (user: User) {
252 if (user.videoQuota === -1) {
253 videoQuota = this.i18n('Unlimited')
255 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
258 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
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')
266 return Object.assign(user, {
267 roleLabel: roleLabels[user.role],