]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-users/user-admin.service.ts
Bumped to version v5.2.1
[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, switchMap, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core'
7 import { getBytes } from '@root-helpers/bytes'
8 import { arrayify, peertubeTranslate } from '@shared/core-utils'
9 import { ResultList, User as UserServerModel, UserCreate, 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 private serverService: ServerService
19 ) { }
20
21 addUser (userCreate: UserCreate) {
22 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
23 .pipe(catchError(err => this.restExtractor.handleError(err)))
24 }
25
26 updateUser (userId: number, userUpdate: UserUpdate) {
27 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
28 .pipe(catchError(err => this.restExtractor.handleError(err)))
29 }
30
31 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
32 return from(users)
33 .pipe(
34 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
35 toArray(),
36 catchError(err => this.restExtractor.handleError(err))
37 )
38 }
39
40 getUsers (parameters: {
41 pagination: RestPagination
42 sort: SortMeta
43 search?: string
44 }): Observable<ResultList<UserServerModel>> {
45 const { pagination, sort, search } = parameters
46
47 let params = new HttpParams()
48 params = this.restService.addRestGetParams(params, pagination, sort)
49
50 if (search) {
51 const filters = this.restService.parseQueryStringFilter(search, {
52 blocked: {
53 prefix: 'banned:',
54 isBoolean: true
55 }
56 })
57
58 params = this.restService.addObjectParams(params, filters)
59 }
60
61 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
62 .pipe(
63 switchMap(data => {
64 return this.serverService.getServerLocale()
65 .pipe(map(translations => ({ data, translations })))
66 }),
67 map(({ data, translations }) => {
68 return this.restExtractor.applyToResultListData(data, this.formatUser.bind(this), [ translations ])
69 }),
70 catchError(err => this.restExtractor.handleError(err))
71 )
72 }
73
74 removeUsers (usersArg: UserServerModel | UserServerModel[]) {
75 const users = arrayify(usersArg)
76
77 return from(users)
78 .pipe(
79 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
80 toArray(),
81 catchError(err => this.restExtractor.handleError(err))
82 )
83 }
84
85 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
86 const body = reason ? { reason } : {}
87 const users = arrayify(usersArg)
88
89 return from(users)
90 .pipe(
91 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
92 toArray(),
93 catchError(err => this.restExtractor.handleError(err))
94 )
95 }
96
97 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
98 const users = arrayify(usersArg)
99
100 return from(users)
101 .pipe(
102 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
103 toArray(),
104 catchError(err => this.restExtractor.handleError(err))
105 )
106 }
107
108 private formatUser (user: UserServerModel, translations: { [ id: string ]: string } = {}) {
109 let videoQuota
110 if (user.videoQuota === -1) {
111 videoQuota = '∞'
112 } else {
113 videoQuota = getBytes(user.videoQuota, 0)
114 }
115
116 const videoQuotaUsed = getBytes(user.videoQuotaUsed, 0)
117
118 let videoQuotaDaily: string
119 let videoQuotaUsedDaily: string
120 if (user.videoQuotaDaily === -1) {
121 videoQuotaDaily = '∞'
122 videoQuotaUsedDaily = getBytes(0, 0) + ''
123 } else {
124 videoQuotaDaily = getBytes(user.videoQuotaDaily, 0) + ''
125 videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
126 }
127
128 return Object.assign(user, {
129 role: {
130 id: user.role.id,
131 label: peertubeTranslate(user.role.label, translations)
132 },
133 videoQuota,
134 videoQuotaUsed,
135 rawVideoQuota: user.videoQuota,
136 rawVideoQuotaUsed: user.videoQuotaUsed,
137 videoQuotaDaily,
138 videoQuotaUsedDaily,
139 rawVideoQuotaDaily: user.videoQuotaDaily,
140 rawVideoQuotaUsedDaily: user.videoQuotaUsedDaily
141 })
142 }
143 }