]>
Commit | Line | Data |
---|---|---|
d92d070c C |
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' | |
e3d6c643 | 8 | import { arrayify } from '@shared/core-utils' |
d92d070c C |
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( | |
d92d070c C |
62 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), |
63 | catchError(err => this.restExtractor.handleError(err)) | |
64 | ) | |
65 | } | |
66 | ||
cd940f40 | 67 | removeUsers (usersArg: UserServerModel | UserServerModel[]) { |
e3d6c643 | 68 | const users = arrayify(usersArg) |
d92d070c C |
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 } : {} | |
e3d6c643 | 80 | const users = arrayify(usersArg) |
d92d070c C |
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[]) { | |
e3d6c643 | 91 | const users = arrayify(usersArg) |
d92d070c C |
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, { | |
9e5cf66b C |
128 | role: { |
129 | id: user.role.id, | |
130 | label: roleLabels[user.role.id] | |
131 | }, | |
d92d070c C |
132 | videoQuota, |
133 | videoQuotaUsed, | |
134 | rawVideoQuota: user.videoQuota, | |
135 | rawVideoQuotaUsed: user.videoQuotaUsed, | |
136 | videoQuotaDaily, | |
137 | videoQuotaUsedDaily, | |
138 | rawVideoQuotaDaily: user.videoQuotaDaily, | |
139 | rawVideoQuotaUsedDaily: user.videoQuotaUsedDaily | |
140 | }) | |
141 | } | |
142 | } |