]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-users/user-admin.service.ts
Fix release script
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-users / user-admin.service.ts
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 { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate } from '@shared/models'
9
10 @Injectable()
11 export class UserAdminService {
12
13 constructor (
14 private authHttp: HttpClient,
15 private restExtractor: RestExtractor,
16 private restService: RestService
17 ) { }
18
19 addUser (userCreate: UserCreate) {
20 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
21 .pipe(catchError(err => this.restExtractor.handleError(err)))
22 }
23
24 updateUser (userId: number, userUpdate: UserUpdate) {
25 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
26 .pipe(catchError(err => this.restExtractor.handleError(err)))
27 }
28
29 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
30 return from(users)
31 .pipe(
32 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
33 toArray(),
34 catchError(err => this.restExtractor.handleError(err))
35 )
36 }
37
38 getUsers (parameters: {
39 pagination: RestPagination
40 sort: SortMeta
41 search?: string
42 }): Observable<ResultList<UserServerModel>> {
43 const { pagination, sort, search } = parameters
44
45 let params = new HttpParams()
46 params = this.restService.addRestGetParams(params, pagination, sort)
47
48 if (search) {
49 const filters = this.restService.parseQueryStringFilter(search, {
50 blocked: {
51 prefix: 'banned:',
52 isBoolean: true
53 }
54 })
55
56 params = this.restService.addObjectParams(params, filters)
57 }
58
59 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
60 .pipe(
61 map(res => this.restExtractor.convertResultListDateToHuman(res)),
62 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
63 catchError(err => this.restExtractor.handleError(err))
64 )
65 }
66
67 removeUser (usersArg: UserServerModel | UserServerModel[]) {
68 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
69
70 return from(users)
71 .pipe(
72 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
73 toArray(),
74 catchError(err => this.restExtractor.handleError(err))
75 )
76 }
77
78 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
79 const body = reason ? { reason } : {}
80 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
81
82 return from(users)
83 .pipe(
84 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
85 toArray(),
86 catchError(err => this.restExtractor.handleError(err))
87 )
88 }
89
90 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
91 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
92
93 return from(users)
94 .pipe(
95 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
96 toArray(),
97 catchError(err => this.restExtractor.handleError(err))
98 )
99 }
100
101 private formatUser (user: UserServerModel) {
102 let videoQuota
103 if (user.videoQuota === -1) {
104 videoQuota = '∞'
105 } else {
106 videoQuota = getBytes(user.videoQuota, 0)
107 }
108
109 const videoQuotaUsed = getBytes(user.videoQuotaUsed, 0)
110
111 let videoQuotaDaily: string
112 let videoQuotaUsedDaily: string
113 if (user.videoQuotaDaily === -1) {
114 videoQuotaDaily = '∞'
115 videoQuotaUsedDaily = getBytes(0, 0) + ''
116 } else {
117 videoQuotaDaily = getBytes(user.videoQuotaDaily, 0) + ''
118 videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
119 }
120
121 const roleLabels: { [ id in UserRole ]: string } = {
122 [UserRole.USER]: $localize`User`,
123 [UserRole.ADMINISTRATOR]: $localize`Administrator`,
124 [UserRole.MODERATOR]: $localize`Moderator`
125 }
126
127 return Object.assign(user, {
128 roleLabel: roleLabels[user.role],
129 videoQuota,
130 videoQuotaUsed,
131 rawVideoQuota: user.videoQuota,
132 rawVideoQuotaUsed: user.videoQuotaUsed,
133 videoQuotaDaily,
134 videoQuotaUsedDaily,
135 rawVideoQuotaDaily: user.videoQuotaDaily,
136 rawVideoQuotaUsedDaily: user.videoQuotaUsedDaily
137 })
138 }
139 }