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, LocalStorageService, 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 private static readonly LOCAL_STORAGE_SELECTED_COLUMNS_KEY = 'admin-user-list-selected-columns'
27 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
29 users: (User & { accountMutedStatus: AccountMutedStatus })[] = []
32 sort: SortMeta = { field: 'createdAt', order: 1 }
33 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
35 highlightBannedUsers = false
37 selectedUsers: User[] = []
38 bulkUserActions: DropdownAction<User[]>[][] = []
39 columns: { id: string, label: string }[]
41 inputFilters: AdvancedInputFilter[] = [
43 title: $localize`Advanced filters`,
47 label: $localize`Banned users`
53 userModerationDisplayOptions: UserModerationDisplayType = {
54 instanceAccount: true,
59 requiresEmailVerification = false
61 private _selectedColumns: string[] = []
64 protected route: ActivatedRoute,
65 protected router: Router,
66 private notifier: Notifier,
67 private confirmService: ConfirmService,
68 private serverService: ServerService,
69 private auth: AuthService,
70 private blocklist: BlocklistService,
71 private userAdminService: UserAdminService,
72 private peertubeLocalStorage: LocalStorageService
78 return this.auth.getUser()
81 get selectedColumns () {
82 return this._selectedColumns || []
85 set selectedColumns (val: string[]) {
86 this._selectedColumns = val
88 this.saveSelectedColumns()
92 this.serverService.getConfig()
93 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
97 this.bulkUserActions = [
100 label: $localize`Delete`,
101 description: $localize`Videos will be deleted, comments will be tombstoned.`,
102 handler: users => this.removeUsers(users),
103 isDisplayed: users => users.every(u => this.authUser.canManage(u))
106 label: $localize`Ban`,
107 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
108 handler: users => this.openBanUserModal(users),
109 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
112 label: $localize`Unban`,
113 handler: users => this.unbanUsers(users),
114 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
119 label: $localize`Set Email as Verified`,
120 handler: users => this.setEmailsAsVerified(users),
121 isDisplayed: users => {
122 return this.requiresEmailVerification &&
123 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
130 { id: 'username', label: $localize`Username` },
131 { id: 'role', label: $localize`Role` },
132 { id: 'email', label: $localize`Email` },
133 { id: 'quota', label: $localize`Video quota` },
134 { id: 'createdAt', label: $localize`Created` },
135 { id: 'lastLoginDate', label: $localize`Last login` },
137 { id: 'quotaDaily', label: $localize`Daily quota` },
138 { id: 'pluginAuth', label: $localize`Auth plugin` }
141 this.loadSelectedColumns()
144 loadSelectedColumns () {
145 const result = this.peertubeLocalStorage.getItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY)
149 this.selectedColumns = JSON.parse(result)
152 console.error('Cannot load selected columns.', err)
157 this.selectedColumns = [ 'username', 'role', 'email', 'quota', 'createdAt', 'lastLoginDate' ]
161 saveSelectedColumns () {
162 this.peertubeLocalStorage.setItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY, JSON.stringify(this.selectedColumns))
166 return 'UserListComponent'
169 getRoleClass (role: UserRole) {
171 case UserRole.ADMINISTRATOR:
172 return 'badge-purple'
173 case UserRole.MODERATOR:
176 return 'badge-yellow'
180 isSelected (id: string) {
181 return this.selectedColumns.find(c => c === id)
184 getColumn (id: string) {
185 return this.columns.find(c => c.id === id)
188 getUserVideoQuotaPercentage (user: UserForList) {
189 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
192 getUserVideoQuotaDailyPercentage (user: UserForList) {
193 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
196 openBanUserModal (users: User[]) {
197 for (const user of users) {
198 if (user.username === 'root') {
199 this.notifier.error($localize`You cannot ban root.`)
204 this.userBanModal.openModal(users)
211 async unbanUsers (users: User[]) {
212 const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
213 if (res === false) return
215 this.userAdminService.unbanUsers(users)
218 this.notifier.success($localize`${users.length} users unbanned.`)
222 error: err => this.notifier.error(err.message)
226 async removeUsers (users: User[]) {
227 for (const user of users) {
228 if (user.username === 'root') {
229 this.notifier.error($localize`You cannot delete root.`)
234 const message = $localize`If you remove these users, you will not be able to create others with the same username!`
235 const res = await this.confirmService.confirm(message, $localize`Delete`)
236 if (res === false) return
238 this.userAdminService.removeUser(users)
241 this.notifier.success($localize`${users.length} users deleted.`)
245 error: err => this.notifier.error(err.message)
249 setEmailsAsVerified (users: User[]) {
250 this.userAdminService.updateUsers(users, { emailVerified: true })
253 this.notifier.success($localize`${users.length} users email set as verified.`)
257 error: err => this.notifier.error(err.message)
261 isInSelectionMode () {
262 return this.selectedUsers.length !== 0
265 protected reloadData () {
266 this.selectedUsers = []
268 this.userAdminService.getUsers({
269 pagination: this.pagination,
273 next: resultList => {
274 this.users = resultList.data.map(u => ({
277 accountMutedStatus: {
280 nameWithHost: Actor.CREATE_BY_STRING(u.account.name, u.account.host),
282 mutedByInstance: false,
284 mutedServerByInstance: false,
285 mutedServerByUser: false
288 this.totalRecords = resultList.total
290 this.loadMutedStatus()
293 error: err => this.notifier.error(err.message)
297 private loadMutedStatus () {
298 this.blocklist.getStatus({ accounts: this.users.map(u => u.username + '@' + getAPIHost()) })
299 .subscribe(blockStatus => {
300 for (const user of this.users) {
301 user.accountMutedStatus.mutedByInstance = blockStatus.accounts[user.username + '@' + getAPIHost()].blockedByServer