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