]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-users/user-admin.service.ts
Update angular
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-users / user-admin.service.ts
CommitLineData
d92d070c
C
1import { SortMeta } from 'primeng/api'
2import { from, Observable } from 'rxjs'
208c97e1 3import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'
d92d070c
C
4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core'
208c97e1 6import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core'
d92d070c 7import { getBytes } from '@root-helpers/bytes'
208c97e1
C
8import { arrayify, peertubeTranslate } from '@shared/core-utils'
9import { ResultList, User as UserServerModel, UserCreate, UserUpdate } from '@shared/models'
d92d070c
C
10
11@Injectable()
12export class UserAdminService {
13
14 constructor (
15 private authHttp: HttpClient,
16 private restExtractor: RestExtractor,
208c97e1
C
17 private restService: RestService,
18 private serverService: ServerService
d92d070c
C
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 })
208c97e1
C
62 .pipe(
63 switchMap(data => {
64 return this.serverService.getServerLocale()
65 .pipe(map(translations => ({ data, translations })))
66 }),
67 map(({ data, translations }) => {
f8c62d34 68 return this.restExtractor.applyToResultListData(data, this.formatUser.bind(this), [ translations ])
208c97e1
C
69 }),
70 catchError(err => this.restExtractor.handleError(err))
71 )
d92d070c
C
72 }
73
cd940f40 74 removeUsers (usersArg: UserServerModel | UserServerModel[]) {
e3d6c643 75 const users = arrayify(usersArg)
d92d070c
C
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 } : {}
e3d6c643 87 const users = arrayify(usersArg)
d92d070c
C
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[]) {
e3d6c643 98 const users = arrayify(usersArg)
d92d070c
C
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
208c97e1 108 private formatUser (user: UserServerModel, translations: { [ id: string ]: string } = {}) {
d92d070c
C
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
d92d070c 128 return Object.assign(user, {
9e5cf66b
C
129 role: {
130 id: user.role.id,
208c97e1 131 label: peertubeTranslate(user.role.label, translations)
9e5cf66b 132 },
d92d070c
C
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}