1 import { SortMeta } from 'primeng/api'
2 import { from, Observable } from 'rxjs'
3 import { catchError, concatMap, map, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService, UserService } from '@app/core'
7 import { getBytes } from '@root-helpers/bytes'
8 import { arrayify } from '@shared/core-utils'
9 import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate } from '@shared/models'
12 export class UserAdminService {
15 private authHttp: HttpClient,
16 private restExtractor: RestExtractor,
17 private restService: RestService
20 addUser (userCreate: UserCreate) {
21 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
22 .pipe(catchError(err => this.restExtractor.handleError(err)))
25 updateUser (userId: number, userUpdate: UserUpdate) {
26 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
27 .pipe(catchError(err => this.restExtractor.handleError(err)))
30 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
33 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
35 catchError(err => this.restExtractor.handleError(err))
39 getUsers (parameters: {
40 pagination: RestPagination
43 }): Observable<ResultList<UserServerModel>> {
44 const { pagination, sort, search } = parameters
46 let params = new HttpParams()
47 params = this.restService.addRestGetParams(params, pagination, sort)
50 const filters = this.restService.parseQueryStringFilter(search, {
57 params = this.restService.addObjectParams(params, filters)
60 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
62 map(res => this.restExtractor.convertResultListDateToHuman(res)),
63 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
64 catchError(err => this.restExtractor.handleError(err))
68 removeUser (usersArg: UserServerModel | UserServerModel[]) {
69 const users = arrayify(usersArg)
73 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
75 catchError(err => this.restExtractor.handleError(err))
79 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
80 const body = reason ? { reason } : {}
81 const users = arrayify(usersArg)
85 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
87 catchError(err => this.restExtractor.handleError(err))
91 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
92 const users = arrayify(usersArg)
96 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
98 catchError(err => this.restExtractor.handleError(err))
102 private formatUser (user: UserServerModel) {
104 if (user.videoQuota === -1) {
107 videoQuota = getBytes(user.videoQuota, 0)
110 const videoQuotaUsed = getBytes(user.videoQuotaUsed, 0)
112 let videoQuotaDaily: string
113 let videoQuotaUsedDaily: string
114 if (user.videoQuotaDaily === -1) {
115 videoQuotaDaily = '∞'
116 videoQuotaUsedDaily = getBytes(0, 0) + ''
118 videoQuotaDaily = getBytes(user.videoQuotaDaily, 0) + ''
119 videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
122 const roleLabels: { [ id in UserRole ]: string } = {
123 [UserRole.USER]: $localize`User`,
124 [UserRole.ADMINISTRATOR]: $localize`Administrator`,
125 [UserRole.MODERATOR]: $localize`Moderator`
128 return Object.assign(user, {
129 roleLabel: roleLabels[user.role],
132 rawVideoQuota: user.videoQuota,
133 rawVideoQuotaUsed: user.videoQuotaUsed,
136 rawVideoQuotaDaily: user.videoQuotaDaily,
137 rawVideoQuotaUsedDaily: user.videoQuotaUsedDaily