]>
Commit | Line | Data |
---|---|---|
f77eb73b | 1 | import { SortMeta } from 'primeng/api' |
2e46eb97 | 2 | import { Component, OnInit, ViewChild } from '@angular/core' |
1fd61899 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
d92d070c | 4 | import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core' |
a282e4d8 | 5 | import { getAPIHost } from '@app/helpers' |
1fd61899 | 6 | import { AdvancedInputFilter } from '@app/shared/shared-forms' |
a282e4d8 C |
7 | import { Actor, DropdownAction } from '@app/shared/shared-main' |
8 | import { AccountMutedStatus, BlocklistService, UserBanModalComponent, UserModerationDisplayType } from '@app/shared/shared-moderation' | |
d92d070c | 9 | import { UserAdminService } from '@app/shared/shared-users' |
2989628b | 10 | import { User, UserRole } from '@shared/models' |
7da18e44 | 11 | |
4f5d0459 RK |
12 | type UserForList = User & { |
13 | rawVideoQuota: number | |
14 | rawVideoQuotaUsed: number | |
15 | rawVideoQuotaDaily: number | |
16 | rawVideoQuotaUsedDaily: number | |
17 | } | |
18 | ||
7da18e44 C |
19 | @Component({ |
20 | selector: 'my-user-list', | |
ec8d8440 C |
21 | templateUrl: './user-list.component.html', |
22 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 23 | }) |
2e46eb97 | 24 | export class UserListComponent extends RestTable implements OnInit { |
f36da21e | 25 | @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent |
791645e6 | 26 | |
a282e4d8 | 27 | users: (User & { accountMutedStatus: AccountMutedStatus })[] = [] |
1fd61899 | 28 | |
d592e0a9 | 29 | totalRecords = 0 |
ab998f7b | 30 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 31 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
1fd61899 | 32 | |
bc99dfe5 | 33 | highlightBannedUsers = false |
141b177d | 34 | |
791645e6 | 35 | selectedUsers: User[] = [] |
9b82d49d | 36 | bulkUserActions: DropdownAction<User[]>[][] = [] |
52c4976f | 37 | columns: { id: string, label: string }[] |
791645e6 | 38 | |
1fd61899 C |
39 | inputFilters: AdvancedInputFilter[] = [ |
40 | { | |
978c87e7 C |
41 | title: $localize`Advanced filters`, |
42 | children: [ | |
43 | { | |
dd6d2a7c | 44 | value: 'banned:true', |
978c87e7 C |
45 | label: $localize`Banned users` |
46 | } | |
47 | ] | |
1fd61899 C |
48 | } |
49 | ] | |
50 | ||
a282e4d8 C |
51 | userModerationDisplayOptions: UserModerationDisplayType = { |
52 | instanceAccount: true, | |
53 | instanceUser: true, | |
54 | myAccount: false | |
55 | } | |
56 | ||
2989628b C |
57 | requiresEmailVerification = false |
58 | ||
52c4976f | 59 | private _selectedColumns: string[] |
ba430d75 | 60 | |
df98563e | 61 | constructor ( |
5ed46c1b C |
62 | protected route: ActivatedRoute, |
63 | protected router: Router, | |
f8b2c1b4 | 64 | private notifier: Notifier, |
5769e1db | 65 | private confirmService: ConfirmService, |
fc2ec87a | 66 | private serverService: ServerService, |
a95a4cc8 | 67 | private auth: AuthService, |
a282e4d8 | 68 | private blocklist: BlocklistService, |
d92d070c | 69 | private userAdminService: UserAdminService |
5ed46c1b | 70 | ) { |
d592e0a9 | 71 | super() |
7da18e44 C |
72 | } |
73 | ||
a95a4cc8 C |
74 | get authUser () { |
75 | return this.auth.getUser() | |
76 | } | |
77 | ||
bc99dfe5 RK |
78 | get selectedColumns () { |
79 | return this._selectedColumns | |
80 | } | |
81 | ||
52c4976f | 82 | set selectedColumns (val: string[]) { |
bc99dfe5 RK |
83 | this._selectedColumns = val |
84 | } | |
85 | ||
ab998f7b | 86 | ngOnInit () { |
ba430d75 | 87 | this.serverService.getConfig() |
2989628b | 88 | .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification) |
ba430d75 | 89 | |
24b9417c | 90 | this.initialize() |
8491293b | 91 | |
791645e6 | 92 | this.bulkUserActions = [ |
9b82d49d RK |
93 | [ |
94 | { | |
66357162 C |
95 | label: $localize`Delete`, |
96 | description: $localize`Videos will be deleted, comments will be tombstoned.`, | |
9b82d49d RK |
97 | handler: users => this.removeUsers(users), |
98 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
99 | }, | |
100 | { | |
66357162 C |
101 | label: $localize`Ban`, |
102 | description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`, | |
9b82d49d RK |
103 | handler: users => this.openBanUserModal(users), |
104 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
105 | }, | |
106 | { | |
66357162 | 107 | label: $localize`Unban`, |
9b82d49d RK |
108 | handler: users => this.unbanUsers(users), |
109 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
a95a4cc8 | 110 | } |
9b82d49d RK |
111 | ], |
112 | [ | |
113 | { | |
66357162 | 114 | label: $localize`Set Email as Verified`, |
9b82d49d RK |
115 | handler: users => this.setEmailsAsVerified(users), |
116 | isDisplayed: users => { | |
117 | return this.requiresEmailVerification && | |
118 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
119 | } | |
120 | } | |
121 | ] | |
791645e6 | 122 | ] |
bc99dfe5 RK |
123 | |
124 | this.columns = [ | |
c3185413 | 125 | { id: 'username', label: $localize`Username` }, |
a282e4d8 | 126 | { id: 'role', label: $localize`Role` }, |
c3185413 C |
127 | { id: 'email', label: $localize`Email` }, |
128 | { id: 'quota', label: $localize`Video quota` }, | |
c3185413 | 129 | { id: 'createdAt', label: $localize`Created` } |
bc99dfe5 | 130 | ] |
52c4976f C |
131 | |
132 | this.selectedColumns = this.columns.map(c => c.id) | |
133 | ||
c3185413 C |
134 | this.columns.push({ id: 'quotaDaily', label: $localize`Daily quota` }) |
135 | this.columns.push({ id: 'pluginAuth', label: $localize`Auth plugin` }) | |
136 | this.columns.push({ id: 'lastLoginDate', label: $localize`Last login` }) | |
141b177d C |
137 | } |
138 | ||
8e11a1b3 C |
139 | getIdentifier () { |
140 | return 'UserListComponent' | |
141 | } | |
142 | ||
bc99dfe5 RK |
143 | getRoleClass (role: UserRole) { |
144 | switch (role) { | |
145 | case UserRole.ADMINISTRATOR: | |
146 | return 'badge-purple' | |
147 | case UserRole.MODERATOR: | |
148 | return 'badge-blue' | |
149 | default: | |
150 | return 'badge-yellow' | |
151 | } | |
152 | } | |
153 | ||
52c4976f C |
154 | isSelected (id: string) { |
155 | return this.selectedColumns.find(c => c === id) | |
156 | } | |
157 | ||
158 | getColumn (id: string) { | |
159 | return this.columns.find(c => c.id === id) | |
bc99dfe5 RK |
160 | } |
161 | ||
4f5d0459 | 162 | getUserVideoQuotaPercentage (user: UserForList) { |
bc99dfe5 RK |
163 | return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota |
164 | } | |
165 | ||
4f5d0459 | 166 | getUserVideoQuotaDailyPercentage (user: UserForList) { |
bc99dfe5 RK |
167 | return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily |
168 | } | |
169 | ||
791645e6 C |
170 | openBanUserModal (users: User[]) { |
171 | for (const user of users) { | |
172 | if (user.username === 'root') { | |
66357162 | 173 | this.notifier.error($localize`You cannot ban root.`) |
791645e6 C |
174 | return |
175 | } | |
176 | } | |
177 | ||
178 | this.userBanModal.openModal(users) | |
179 | } | |
180 | ||
2fbe7f19 | 181 | onUserChanged () { |
2e46eb97 | 182 | this.reloadData() |
791645e6 C |
183 | } |
184 | ||
185 | async unbanUsers (users: User[]) { | |
66357162 | 186 | const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`) |
791645e6 C |
187 | if (res === false) return |
188 | ||
d92d070c | 189 | this.userAdminService.unbanUsers(users) |
1378c0d3 C |
190 | .subscribe({ |
191 | next: () => { | |
66357162 | 192 | this.notifier.success($localize`${users.length} users unbanned.`) |
2e46eb97 | 193 | this.reloadData() |
791645e6 C |
194 | }, |
195 | ||
1378c0d3 C |
196 | error: err => this.notifier.error(err.message) |
197 | }) | |
791645e6 C |
198 | } |
199 | ||
200 | async removeUsers (users: User[]) { | |
201 | for (const user of users) { | |
202 | if (user.username === 'root') { | |
66357162 | 203 | this.notifier.error($localize`You cannot delete root.`) |
791645e6 C |
204 | return |
205 | } | |
206 | } | |
207 | ||
66357162 C |
208 | const message = $localize`If you remove these users, you will not be able to create others with the same username!` |
209 | const res = await this.confirmService.confirm(message, $localize`Delete`) | |
791645e6 C |
210 | if (res === false) return |
211 | ||
d92d070c | 212 | this.userAdminService.removeUser(users) |
1378c0d3 C |
213 | .subscribe({ |
214 | next: () => { | |
215 | this.notifier.success($localize`${users.length} users deleted.`) | |
216 | this.reloadData() | |
217 | }, | |
791645e6 | 218 | |
1378c0d3 C |
219 | error: err => this.notifier.error(err.message) |
220 | }) | |
791645e6 C |
221 | } |
222 | ||
98ab5dc8 | 223 | setEmailsAsVerified (users: User[]) { |
d92d070c | 224 | this.userAdminService.updateUsers(users, { emailVerified: true }) |
1378c0d3 C |
225 | .subscribe({ |
226 | next: () => { | |
227 | this.notifier.success($localize`${users.length} users email set as verified.`) | |
228 | this.reloadData() | |
229 | }, | |
fc2ec87a | 230 | |
1378c0d3 C |
231 | error: err => this.notifier.error(err.message) |
232 | }) | |
fc2ec87a JM |
233 | } |
234 | ||
791645e6 C |
235 | isInSelectionMode () { |
236 | return this.selectedUsers.length !== 0 | |
237 | } | |
dffd5d12 | 238 | |
2e46eb97 | 239 | protected reloadData () { |
dffd5d12 B |
240 | this.selectedUsers = [] |
241 | ||
d92d070c | 242 | this.userAdminService.getUsers({ |
8491293b RK |
243 | pagination: this.pagination, |
244 | sort: this.sort, | |
245 | search: this.search | |
1378c0d3 C |
246 | }).subscribe({ |
247 | next: resultList => { | |
a282e4d8 C |
248 | this.users = resultList.data.map(u => ({ |
249 | ...u, | |
250 | ||
251 | accountMutedStatus: { | |
252 | ...u.account, | |
253 | ||
254 | nameWithHost: Actor.CREATE_BY_STRING(u.account.name, u.account.host), | |
255 | ||
256 | mutedByInstance: false, | |
257 | mutedByUser: false, | |
258 | mutedServerByInstance: false, | |
259 | mutedServerByUser: false | |
260 | } | |
261 | })) | |
8491293b | 262 | this.totalRecords = resultList.total |
a282e4d8 C |
263 | |
264 | this.loadMutedStatus() | |
8491293b | 265 | }, |
f8b2c1b4 | 266 | |
1378c0d3 C |
267 | error: err => this.notifier.error(err.message) |
268 | }) | |
dffd5d12 | 269 | } |
a282e4d8 C |
270 | |
271 | private loadMutedStatus () { | |
272 | this.blocklist.getStatus({ accounts: this.users.map(u => u.username + '@' + getAPIHost()) }) | |
273 | .subscribe(blockStatus => { | |
274 | for (const user of this.users) { | |
275 | user.accountMutedStatus.mutedByInstance = blockStatus.accounts[user.username + '@' + getAPIHost()].blockedByServer | |
276 | } | |
277 | }) | |
278 | } | |
7da18e44 | 279 | } |