]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-list/user-list.component.ts
Improving select displays, focus box-shadows for paginators, instructions for index url
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
CommitLineData
791645e6 1import { Component, OnInit, ViewChild } from '@angular/core'
a95a4cc8 2import { AuthService, Notifier } from '@app/core'
f77eb73b 3import { SortMeta } from 'primeng/api'
fc2ec87a 4import { ConfirmService, ServerService } from '../../../core'
e724fa93 5import { RestPagination, RestTable, UserService } from '../../../shared'
b1d40cff 6import { I18n } from '@ngx-translate/i18n-polyfill'
ba430d75 7import { ServerConfig, User } from '../../../../../../shared'
791645e6
C
8import { UserBanModalComponent } from '@app/shared/moderation'
9import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
7da18e44
C
10
11@Component({
12 selector: 'my-user-list',
ec8d8440
C
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
7da18e44 15})
ab998f7b 16export class UserListComponent extends RestTable implements OnInit {
f36da21e 17 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
791645e6 18
d592e0a9
C
19 users: User[] = []
20 totalRecords = 0
21 rowsPerPage = 10
ab998f7b 22 sort: SortMeta = { field: 'createdAt', order: 1 }
d592e0a9 23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
141b177d 24
791645e6 25 selectedUsers: User[] = []
9b82d49d 26 bulkUserActions: DropdownAction<User[]>[][] = []
791645e6 27
ba430d75
C
28 private serverConfig: ServerConfig
29
df98563e 30 constructor (
f8b2c1b4 31 private notifier: Notifier,
5769e1db 32 private confirmService: ConfirmService,
fc2ec87a 33 private serverService: ServerService,
b1d40cff 34 private userService: UserService,
a95a4cc8 35 private auth: AuthService,
b1d40cff 36 private i18n: I18n
28798b5d 37 ) {
d592e0a9 38 super()
7da18e44
C
39 }
40
a95a4cc8
C
41 get authUser () {
42 return this.auth.getUser()
43 }
44
fc2ec87a 45 get requiresEmailVerification () {
ba430d75 46 return this.serverConfig.signup.requiresEmailVerification
fc2ec87a
JM
47 }
48
ab998f7b 49 ngOnInit () {
ba430d75
C
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
53
24b9417c 54 this.initialize()
ab998f7b 55
791645e6 56 this.bulkUserActions = [
9b82d49d
RK
57 [
58 {
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))
63 },
64 {
65 label: this.i18n('Ban'),
f0ad4710 66 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
9b82d49d
RK
67 handler: users => this.openBanUserModal(users),
68 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
69 },
70 {
71 label: this.i18n('Unban'),
72 handler: users => this.unbanUsers(users),
73 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
a95a4cc8 74 }
9b82d49d
RK
75 ],
76 [
77 {
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)
83 }
84 }
85 ]
791645e6 86 ]
141b177d
C
87 }
88
8e11a1b3
C
89 getIdentifier () {
90 return 'UserListComponent'
91 }
92
791645e6
C
93 openBanUserModal (users: User[]) {
94 for (const user of users) {
95 if (user.username === 'root') {
f8b2c1b4 96 this.notifier.error(this.i18n('You cannot ban root.'))
791645e6
C
97 return
98 }
99 }
100
101 this.userBanModal.openModal(users)
102 }
103
2fbe7f19 104 onUserChanged () {
791645e6
C
105 this.loadData()
106 }
107
108 async unbanUsers (users: User[]) {
109 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
110
111 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
112 if (res === false) return
113
114 this.userService.unbanUsers(users)
115 .subscribe(
116 () => {
117 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
118
f8b2c1b4 119 this.notifier.success(message)
791645e6
C
120 this.loadData()
121 },
122
f8b2c1b4 123 err => this.notifier.error(err.message)
791645e6
C
124 )
125 }
126
127 async removeUsers (users: User[]) {
128 for (const user of users) {
129 if (user.username === 'root') {
f8b2c1b4 130 this.notifier.error(this.i18n('You cannot delete root.'))
791645e6
C
131 return
132 }
133 }
134
135 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
136 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
137 if (res === false) return
138
139 this.userService.removeUser(users).subscribe(
140 () => {
f8b2c1b4 141 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
791645e6
C
142 this.loadData()
143 },
144
f8b2c1b4 145 err => this.notifier.error(err.message)
791645e6
C
146 )
147 }
148
fc2ec87a
JM
149 async setEmailsAsVerified (users: User[]) {
150 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
151 () => {
f8b2c1b4 152 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
fc2ec87a
JM
153 this.loadData()
154 },
155
f8b2c1b4 156 err => this.notifier.error(err.message)
fc2ec87a
JM
157 )
158 }
159
791645e6
C
160 isInSelectionMode () {
161 return this.selectedUsers.length !== 0
162 }
dffd5d12
B
163
164 protected loadData () {
165 this.selectedUsers = []
166
167 this.userService.getUsers(this.pagination, this.sort, this.search)
f8b2c1b4
C
168 .subscribe(
169 resultList => {
170 this.users = resultList.data
171 this.totalRecords = resultList.total
172 },
173
174 err => this.notifier.error(err.message)
175 )
dffd5d12 176 }
7da18e44 177}