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