1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
5 import { getAPIHost } from '@app/helpers'
6 import { AdvancedInputFilter } from '@app/shared/shared-forms'
7 import { Actor, DropdownAction } from '@app/shared/shared-main'
8 import { AccountMutedStatus, BlocklistService, UserBanModalComponent, UserModerationDisplayType } from '@app/shared/shared-moderation'
9 import { UserAdminService } from '@app/shared/shared-users'
10 import { User, UserRole } from '@shared/models'
12 type UserForList = User & {
14 rawVideoQuotaUsed: number
15 rawVideoQuotaDaily: number
16 rawVideoQuotaUsedDaily: number
20 selector: 'my-user-list',
21 templateUrl: './user-list.component.html',
22 styleUrls: [ './user-list.component.scss' ]
24 export class UserListComponent extends RestTable implements OnInit {
25 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
27 users: (User & { accountMutedStatus: AccountMutedStatus })[] = []
30 sort: SortMeta = { field: 'createdAt', order: 1 }
31 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
33 highlightBannedUsers = false
35 selectedUsers: User[] = []
36 bulkUserActions: DropdownAction<User[]>[][] = []
37 columns: { id: string, label: string }[]
39 inputFilters: AdvancedInputFilter[] = [
41 title: $localize`Advanced filters`,
45 label: $localize`Banned users`
51 userModerationDisplayOptions: UserModerationDisplayType = {
52 instanceAccount: true,
57 requiresEmailVerification = false
59 private _selectedColumns: string[]
62 protected route: ActivatedRoute,
63 protected router: Router,
64 private notifier: Notifier,
65 private confirmService: ConfirmService,
66 private serverService: ServerService,
67 private auth: AuthService,
68 private blocklist: BlocklistService,
69 private userAdminService: UserAdminService
75 return this.auth.getUser()
78 get selectedColumns () {
79 return this._selectedColumns
82 set selectedColumns (val: string[]) {
83 this._selectedColumns = val
87 this.serverService.getConfig()
88 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
92 this.bulkUserActions = [
95 label: $localize`Delete`,
96 description: $localize`Videos will be deleted, comments will be tombstoned.`,
97 handler: users => this.removeUsers(users),
98 isDisplayed: users => users.every(u => this.authUser.canManage(u))
101 label: $localize`Ban`,
102 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
103 handler: users => this.openBanUserModal(users),
104 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
107 label: $localize`Unban`,
108 handler: users => this.unbanUsers(users),
109 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
114 label: $localize`Set Email as Verified`,
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)
125 { id: 'username', label: $localize`Username` },
126 { id: 'role', label: $localize`Role` },
127 { id: 'email', label: $localize`Email` },
128 { id: 'quota', label: $localize`Video quota` },
129 { id: 'createdAt', label: $localize`Created` }
132 this.selectedColumns = this.columns.map(c => c.id)
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` })
140 return 'UserListComponent'
143 getRoleClass (role: UserRole) {
145 case UserRole.ADMINISTRATOR:
146 return 'badge-purple'
147 case UserRole.MODERATOR:
150 return 'badge-yellow'
154 isSelected (id: string) {
155 return this.selectedColumns.find(c => c === id)
158 getColumn (id: string) {
159 return this.columns.find(c => c.id === id)
162 getUserVideoQuotaPercentage (user: UserForList) {
163 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
166 getUserVideoQuotaDailyPercentage (user: UserForList) {
167 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
170 openBanUserModal (users: User[]) {
171 for (const user of users) {
172 if (user.username === 'root') {
173 this.notifier.error($localize`You cannot ban root.`)
178 this.userBanModal.openModal(users)
185 async unbanUsers (users: User[]) {
186 const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
187 if (res === false) return
189 this.userAdminService.unbanUsers(users)
192 this.notifier.success($localize`${users.length} users unbanned.`)
196 error: err => this.notifier.error(err.message)
200 async removeUsers (users: User[]) {
201 for (const user of users) {
202 if (user.username === 'root') {
203 this.notifier.error($localize`You cannot delete root.`)
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`)
210 if (res === false) return
212 this.userAdminService.removeUser(users)
215 this.notifier.success($localize`${users.length} users deleted.`)
219 error: err => this.notifier.error(err.message)
223 setEmailsAsVerified (users: User[]) {
224 this.userAdminService.updateUsers(users, { emailVerified: true })
227 this.notifier.success($localize`${users.length} users email set as verified.`)
231 error: err => this.notifier.error(err.message)
235 isInSelectionMode () {
236 return this.selectedUsers.length !== 0
239 protected reloadData () {
240 this.selectedUsers = []
242 this.userAdminService.getUsers({
243 pagination: this.pagination,
247 next: resultList => {
248 this.users = resultList.data.map(u => ({
251 accountMutedStatus: {
254 nameWithHost: Actor.CREATE_BY_STRING(u.account.name, u.account.host),
256 mutedByInstance: false,
258 mutedServerByInstance: false,
259 mutedServerByUser: false
262 this.totalRecords = resultList.total
264 this.loadMutedStatus()
267 error: err => this.notifier.error(err.message)
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