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
23 sort: SortMeta = { field: 'createdAt', order: 1 }
24 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
26 selectedUsers: User[] = []
27 bulkUserActions: DropdownAction<User[]>[][] = []
29 private serverConfig: ServerConfig
32 private notifier: Notifier,
33 private confirmService: ConfirmService,
34 private serverService: ServerService,
35 private userService: UserService,
36 private auth: AuthService,
43 return this.auth.getUser()
46 get requiresEmailVerification () {
47 return this.serverConfig.signup.requiresEmailVerification
51 this.serverConfig = this.serverService.getTmpConfig()
52 this.serverService.getConfig()
53 .subscribe(config => this.serverConfig = config)
57 this.bulkUserActions = [
60 label: this.i18n('Delete'),
61 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
62 handler: users => this.removeUsers(users),
63 isDisplayed: users => users.every(u => this.authUser.canManage(u))
66 label: this.i18n('Ban'),
67 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
68 handler: users => this.openBanUserModal(users),
69 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
72 label: this.i18n('Unban'),
73 handler: users => this.unbanUsers(users),
74 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
79 label: this.i18n('Set Email as Verified'),
80 handler: users => this.setEmailsAsVerified(users),
81 isDisplayed: users => {
82 return this.requiresEmailVerification &&
83 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
91 return 'UserListComponent'
94 openBanUserModal (users: User[]) {
95 for (const user of users) {
96 if (user.username === 'root') {
97 this.notifier.error(this.i18n('You cannot ban root.'))
102 this.userBanModal.openModal(users)
109 switchToDefaultAvatar ($event: Event) {
110 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
113 async unbanUsers (users: User[]) {
114 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
116 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
117 if (res === false) return
119 this.userService.unbanUsers(users)
122 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
124 this.notifier.success(message)
128 err => this.notifier.error(err.message)
132 async removeUsers (users: User[]) {
133 for (const user of users) {
134 if (user.username === 'root') {
135 this.notifier.error(this.i18n('You cannot delete root.'))
140 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
141 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
142 if (res === false) return
144 this.userService.removeUser(users).subscribe(
146 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
150 err => this.notifier.error(err.message)
154 async setEmailsAsVerified (users: User[]) {
155 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
157 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
161 err => this.notifier.error(err.message)
165 isInSelectionMode () {
166 return this.selectedUsers.length !== 0
169 protected loadData () {
170 this.selectedUsers = []
172 this.userService.getUsers(this.pagination, this.sort, this.search)
175 this.users = resultList.data
176 this.totalRecords = resultList.total
179 err => this.notifier.error(err.message)