1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core'
4 import { Actor, DropdownAction } from '@app/shared/shared-main'
5 import { UserBanModalComponent } from '@app/shared/shared-moderation'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { ServerConfig, User } from '@shared/models'
10 selector: 'my-user-list',
11 templateUrl: './user-list.component.html',
12 styleUrls: [ './user-list.component.scss' ]
14 export class UserListComponent extends RestTable implements OnInit {
15 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
19 sort: SortMeta = { field: 'createdAt', order: 1 }
20 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
22 selectedUsers: User[] = []
23 bulkUserActions: DropdownAction<User[]>[][] = []
25 private serverConfig: ServerConfig
28 private notifier: Notifier,
29 private confirmService: ConfirmService,
30 private serverService: ServerService,
31 private userService: UserService,
32 private auth: AuthService,
39 return this.auth.getUser()
42 get requiresEmailVerification () {
43 return this.serverConfig.signup.requiresEmailVerification
47 this.serverConfig = this.serverService.getTmpConfig()
48 this.serverService.getConfig()
49 .subscribe(config => this.serverConfig = config)
53 this.bulkUserActions = [
56 label: this.i18n('Delete'),
57 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
58 handler: users => this.removeUsers(users),
59 isDisplayed: users => users.every(u => this.authUser.canManage(u))
62 label: this.i18n('Ban'),
63 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
64 handler: users => this.openBanUserModal(users),
65 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
68 label: this.i18n('Unban'),
69 handler: users => this.unbanUsers(users),
70 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
75 label: this.i18n('Set Email as Verified'),
76 handler: users => this.setEmailsAsVerified(users),
77 isDisplayed: users => {
78 return this.requiresEmailVerification &&
79 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
87 return 'UserListComponent'
90 openBanUserModal (users: User[]) {
91 for (const user of users) {
92 if (user.username === 'root') {
93 this.notifier.error(this.i18n('You cannot ban root.'))
98 this.userBanModal.openModal(users)
105 switchToDefaultAvatar ($event: Event) {
106 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
109 async unbanUsers (users: User[]) {
110 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
112 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
113 if (res === false) return
115 this.userService.unbanUsers(users)
118 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
120 this.notifier.success(message)
124 err => this.notifier.error(err.message)
128 async removeUsers (users: User[]) {
129 for (const user of users) {
130 if (user.username === 'root') {
131 this.notifier.error(this.i18n('You cannot delete root.'))
136 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
137 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
138 if (res === false) return
140 this.userService.removeUser(users).subscribe(
142 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
146 err => this.notifier.error(err.message)
150 async setEmailsAsVerified (users: User[]) {
151 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
153 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
157 err => this.notifier.error(err.message)
161 isInSelectionMode () {
162 return this.selectedUsers.length !== 0
165 protected loadData () {
166 this.selectedUsers = []
168 this.userService.getUsers(this.pagination, this.sort, this.search)
171 this.users = resultList.data
172 this.totalRecords = resultList.total
175 err => this.notifier.error(err.message)