]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
distinct style for rows of banned users in listing, saving space
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
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'
8
9 @Component({
10 selector: 'my-user-list',
11 templateUrl: './user-list.component.html',
12 styleUrls: [ './user-list.component.scss' ]
13 })
14 export class UserListComponent extends RestTable implements OnInit {
15 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
16
17 users: User[] = []
18 totalRecords = 0
19 sort: SortMeta = { field: 'createdAt', order: 1 }
20 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21
22 selectedUsers: User[] = []
23 bulkUserActions: DropdownAction<User[]>[][] = []
24
25 private serverConfig: ServerConfig
26
27 constructor (
28 private notifier: Notifier,
29 private confirmService: ConfirmService,
30 private serverService: ServerService,
31 private userService: UserService,
32 private auth: AuthService,
33 private i18n: I18n
34 ) {
35 super()
36 }
37
38 get authUser () {
39 return this.auth.getUser()
40 }
41
42 get requiresEmailVerification () {
43 return this.serverConfig.signup.requiresEmailVerification
44 }
45
46 ngOnInit () {
47 this.serverConfig = this.serverService.getTmpConfig()
48 this.serverService.getConfig()
49 .subscribe(config => this.serverConfig = config)
50
51 this.initialize()
52
53 this.bulkUserActions = [
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'),
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)
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)
71 }
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 ]
83 ]
84 }
85
86 getIdentifier () {
87 return 'UserListComponent'
88 }
89
90 getRowClasses (rowData: User) {
91 return {
92 banned: rowData.blocked
93 }
94 }
95
96 openBanUserModal (users: User[]) {
97 for (const user of users) {
98 if (user.username === 'root') {
99 this.notifier.error(this.i18n('You cannot ban root.'))
100 return
101 }
102 }
103
104 this.userBanModal.openModal(users)
105 }
106
107 onUserChanged () {
108 this.loadData()
109 }
110
111 switchToDefaultAvatar ($event: Event) {
112 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
113 }
114
115 async unbanUsers (users: User[]) {
116 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
117
118 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
119 if (res === false) return
120
121 this.userService.unbanUsers(users)
122 .subscribe(
123 () => {
124 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
125
126 this.notifier.success(message)
127 this.loadData()
128 },
129
130 err => this.notifier.error(err.message)
131 )
132 }
133
134 async removeUsers (users: User[]) {
135 for (const user of users) {
136 if (user.username === 'root') {
137 this.notifier.error(this.i18n('You cannot delete root.'))
138 return
139 }
140 }
141
142 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
143 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
144 if (res === false) return
145
146 this.userService.removeUser(users).subscribe(
147 () => {
148 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
149 this.loadData()
150 },
151
152 err => this.notifier.error(err.message)
153 )
154 }
155
156 async setEmailsAsVerified (users: User[]) {
157 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
158 () => {
159 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
160 this.loadData()
161 },
162
163 err => this.notifier.error(err.message)
164 )
165 }
166
167 isInSelectionMode () {
168 return this.selectedUsers.length !== 0
169 }
170
171 protected loadData () {
172 this.selectedUsers = []
173
174 this.userService.getUsers(this.pagination, this.sort, this.search)
175 .subscribe(
176 resultList => {
177 this.users = resultList.data
178 this.totalRecords = resultList.total
179 },
180
181 err => this.notifier.error(err.message)
182 )
183 }
184 }