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'
14 export class UserService {
15 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
17 private bytesPipe = new BytesPipe()
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
26 changePassword (currentPassword: string, newPassword: string) {
27 const url = UserService.BASE_USERS_URL + 'me'
28 const body: UserUpdateMe = {
33 return this.authHttp.put(url, body)
35 map(this.restExtractor.extractDataBool),
36 catchError(err => this.restExtractor.handleError(err))
40 updateMyProfile (profile: UserUpdateMe) {
41 const url = UserService.BASE_USERS_URL + 'me'
43 return this.authHttp.put(url, profile)
45 map(this.restExtractor.extractDataBool),
46 catchError(err => this.restExtractor.handleError(err))
51 const url = UserService.BASE_USERS_URL + 'me'
53 return this.authHttp.delete(url)
55 map(this.restExtractor.extractDataBool),
56 catchError(err => this.restExtractor.handleError(err))
60 changeAvatar (avatarForm: FormData) {
61 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
63 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
64 .pipe(catchError(err => this.restExtractor.handleError(err)))
67 signup (userCreate: UserCreate) {
68 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
70 map(this.restExtractor.extractDataBool),
71 catchError(err => this.restExtractor.handleError(err))
75 getMyVideoQuotaUsed () {
76 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
78 return this.authHttp.get<UserVideoQuota>(url)
79 .pipe(catchError(err => this.restExtractor.handleError(err)))
82 askResetPassword (email: string) {
83 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
85 return this.authHttp.post(url, { email })
87 map(this.restExtractor.extractDataBool),
88 catchError(err => this.restExtractor.handleError(err))
92 resetPassword (userId: number, verificationString: string, password: string) {
93 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
99 return this.authHttp.post(url, body)
101 map(this.restExtractor.extractDataBool),
102 catchError(res => this.restExtractor.handleError(res))
106 verifyEmail (userId: number, verificationString: string) {
107 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
112 return this.authHttp.post(url, body)
114 map(this.restExtractor.extractDataBool),
115 catchError(res => this.restExtractor.handleError(res))
119 askSendVerifyEmail (email: string) {
120 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
122 return this.authHttp.post(url, { email })
124 map(this.restExtractor.extractDataBool),
125 catchError(err => this.restExtractor.handleError(err))
129 autocomplete (search: string): Observable<string[]> {
130 const url = UserService.BASE_USERS_URL + 'autocomplete'
131 const params = new HttpParams().append('search', search)
134 .get<string[]>(url, { params })
135 .pipe(catchError(res => this.restExtractor.handleError(res)))
138 /* ###### Admin methods ###### */
140 addUser (userCreate: UserCreate) {
141 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
143 map(this.restExtractor.extractDataBool),
144 catchError(err => this.restExtractor.handleError(err))
148 updateUser (userId: number, userUpdate: UserUpdate) {
149 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
151 map(this.restExtractor.extractDataBool),
152 catchError(err => this.restExtractor.handleError(err))
156 getUser (userId: number) {
157 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
158 .pipe(catchError(err => this.restExtractor.handleError(err)))
161 getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> {
162 let params = new HttpParams()
163 params = this.restService.addRestGetParams(params, pagination, sort)
165 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
167 map(res => this.restExtractor.convertResultListDateToHuman(res)),
168 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
169 catchError(err => this.restExtractor.handleError(err))
173 removeUser (usersArg: User | User[]) {
174 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
178 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
180 catchError(err => this.restExtractor.handleError(err))
184 banUsers (usersArg: User | User[], reason?: string) {
185 const body = reason ? { reason } : {}
186 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
190 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
192 catchError(err => this.restExtractor.handleError(err))
196 unbanUsers (usersArg: User | User[]) {
197 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
201 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
203 catchError(err => this.restExtractor.handleError(err))
207 private formatUser (user: User) {
209 if (user.videoQuota === -1) {
210 videoQuota = this.i18n('Unlimited')
212 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
215 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
217 const roleLabels: { [ id in UserRole ]: string } = {
218 [UserRole.USER]: this.i18n('User'),
219 [UserRole.ADMINISTRATOR]: this.i18n('Administrator'),
220 [UserRole.MODERATOR]: this.i18n('Moderator')
223 return Object.assign(user, {
224 roleLabel: roleLabels[user.role],