1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { AuthService, Notifier } from '@app/core'
3 import { SortMeta } from 'primeng/api'
4 import { ConfirmService, ServerService } from '../../../core'
5 import { RestPagination, RestTable, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { ServerConfig, User } from '../../../../../../shared'
8 import { UserBanModalComponent } from '@app/shared/moderation'
9 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
10 import { Actor } from '@app/shared/actor/actor.model'
13 selector: 'my-user-list',
14 templateUrl: './user-list.component.html',
15 styleUrls: [ './user-list.component.scss' ]
17 export class UserListComponent extends RestTable implements OnInit {
18 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User[]>[][] = []
28 private serverConfig: ServerConfig
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private serverService: ServerService,
34 private userService: UserService,
35 private auth: AuthService,
42 return this.auth.getUser()
45 get requiresEmailVerification () {
46 return this.serverConfig.signup.requiresEmailVerification
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
56 this.bulkUserActions = [
59 label: this.i18n('Delete'),
60 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
61 handler: users => this.removeUsers(users),
62 isDisplayed: users => users.every(u => this.authUser.canManage(u))
65 label: this.i18n('Ban'),
66 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
67 handler: users => this.openBanUserModal(users),
68 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
71 label: this.i18n('Unban'),
72 handler: users => this.unbanUsers(users),
73 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
78 label: this.i18n('Set Email as Verified'),
79 handler: users => this.setEmailsAsVerified(users),
80 isDisplayed: users => {
81 return this.requiresEmailVerification &&
82 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
90 return 'UserListComponent'
93 openBanUserModal (users: User[]) {
94 for (const user of users) {
95 if (user.username === 'root') {
96 this.notifier.error(this.i18n('You cannot ban root.'))
101 this.userBanModal.openModal(users)
108 switchToDefaultAvatar ($event: Event) {
109 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
112 async unbanUsers (users: User[]) {
113 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
115 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
116 if (res === false) return
118 this.userService.unbanUsers(users)
121 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
123 this.notifier.success(message)
127 err => this.notifier.error(err.message)
131 async removeUsers (users: User[]) {
132 for (const user of users) {
133 if (user.username === 'root') {
134 this.notifier.error(this.i18n('You cannot delete root.'))
139 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
140 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
141 if (res === false) return
143 this.userService.removeUser(users).subscribe(
145 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
149 err => this.notifier.error(err.message)
153 async setEmailsAsVerified (users: User[]) {
154 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
156 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
160 err => this.notifier.error(err.message)
164 isInSelectionMode () {
165 return this.selectedUsers.length !== 0
168 protected loadData () {
169 this.selectedUsers = []
171 this.userService.getUsers(this.pagination, this.sort, this.search)
174 this.users = resultList.data
175 this.totalRecords = resultList.total
178 err => this.notifier.error(err.message)