]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
variable columns for users list, more columns possible, badge display for statuses
[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, UserRole } from '@shared/models'
8 import { Params, Router, ActivatedRoute } from '@angular/router'
9
10 @Component({
11 selector: 'my-user-list',
12 templateUrl: './user-list.component.html',
13 styleUrls: [ './user-list.component.scss' ]
14 })
15 export class UserListComponent extends RestTable implements OnInit {
16 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
17
18 users: User[] = []
19 totalRecords = 0
20 sort: SortMeta = { field: 'createdAt', order: 1 }
21 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
22 highlightBannedUsers = false
23
24 selectedUsers: User[] = []
25 bulkUserActions: DropdownAction<User[]>[][] = []
26 columns: { key: string, label: string }[]
27 _selectedColumns: { key: string, label: string }[]
28
29 private serverConfig: ServerConfig
30
31 constructor (
32 private notifier: Notifier,
33 private confirmService: ConfirmService,
34 private serverService: ServerService,
35 private userService: UserService,
36 private auth: AuthService,
37 private route: ActivatedRoute,
38 private router: Router,
39 private i18n: I18n
40 ) {
41 super()
42 }
43
44 get authUser () {
45 return this.auth.getUser()
46 }
47
48 get requiresEmailVerification () {
49 return this.serverConfig.signup.requiresEmailVerification
50 }
51
52 get selectedColumns () {
53 return this._selectedColumns
54 }
55
56 set selectedColumns (val) {
57 this._selectedColumns = val
58 }
59
60 ngOnInit () {
61 this.serverConfig = this.serverService.getTmpConfig()
62 this.serverService.getConfig()
63 .subscribe(config => this.serverConfig = config)
64
65 this.initialize()
66
67 this.route.queryParams
68 .subscribe(params => {
69 this.search = params.search || ''
70
71 this.setTableFilter(this.search)
72 this.loadData()
73 })
74
75 this.bulkUserActions = [
76 [
77 {
78 label: this.i18n('Delete'),
79 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
80 handler: users => this.removeUsers(users),
81 isDisplayed: users => users.every(u => this.authUser.canManage(u))
82 },
83 {
84 label: this.i18n('Ban'),
85 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
86 handler: users => this.openBanUserModal(users),
87 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
88 },
89 {
90 label: this.i18n('Unban'),
91 handler: users => this.unbanUsers(users),
92 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
93 }
94 ],
95 [
96 {
97 label: this.i18n('Set Email as Verified'),
98 handler: users => this.setEmailsAsVerified(users),
99 isDisplayed: users => {
100 return this.requiresEmailVerification &&
101 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
102 }
103 }
104 ]
105 ]
106
107 this.columns = [
108 { key: 'username', label: 'Username' },
109 { key: 'email', label: 'Email' },
110 { key: 'quota', label: 'Video quota' },
111 { key: 'role', label: 'Role' },
112 { key: 'createdAt', label: 'Created' }
113 ]
114 this.selectedColumns = [...this.columns]
115 this.columns.push({ key: 'quotaDaily', label: 'Daily quota' })
116 this.columns.push({ key: 'pluginAuth', label: 'Auth plugin' })
117 this.columns.push({ key: 'lastLoginDate', label: 'Last login' })
118 }
119
120 getIdentifier () {
121 return 'UserListComponent'
122 }
123
124 getRoleClass (role: UserRole) {
125 switch (role) {
126 case UserRole.ADMINISTRATOR:
127 return 'badge-purple'
128 case UserRole.MODERATOR:
129 return 'badge-blue'
130 default:
131 return 'badge-yellow'
132 }
133 }
134
135 getColumn (key: string) {
136 return this.selectedColumns.find((col: any) => col.key === key)
137 }
138
139 getUserVideoQuotaPercentage (user: User & { rawVideoQuota: number, rawVideoQuotaUsed: number}) {
140 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
141 }
142
143 getUserVideoQuotaDailyPercentage (user: User & { rawVideoQuotaDaily: number, rawVideoQuotaUsedDaily: number}) {
144 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
145 }
146
147 openBanUserModal (users: User[]) {
148 for (const user of users) {
149 if (user.username === 'root') {
150 this.notifier.error(this.i18n('You cannot ban root.'))
151 return
152 }
153 }
154
155 this.userBanModal.openModal(users)
156 }
157
158 onUserChanged () {
159 this.loadData()
160 }
161
162 /* Table filter functions */
163 onUserSearch (event: Event) {
164 this.onSearch(event)
165 this.setQueryParams((event.target as HTMLInputElement).value)
166 }
167
168 setQueryParams (search: string) {
169 const queryParams: Params = {}
170 if (search) Object.assign(queryParams, { search })
171
172 this.router.navigate([ '/admin/users/list' ], { queryParams })
173 }
174
175 resetTableFilter () {
176 this.setTableFilter('')
177 this.setQueryParams('')
178 this.resetSearch()
179 }
180 /* END Table filter functions */
181
182 switchToDefaultAvatar ($event: Event) {
183 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
184 }
185
186 async unbanUsers (users: User[]) {
187 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
188
189 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
190 if (res === false) return
191
192 this.userService.unbanUsers(users)
193 .subscribe(
194 () => {
195 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
196
197 this.notifier.success(message)
198 this.loadData()
199 },
200
201 err => this.notifier.error(err.message)
202 )
203 }
204
205 async removeUsers (users: User[]) {
206 for (const user of users) {
207 if (user.username === 'root') {
208 this.notifier.error(this.i18n('You cannot delete root.'))
209 return
210 }
211 }
212
213 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
214 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
215 if (res === false) return
216
217 this.userService.removeUser(users).subscribe(
218 () => {
219 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
220 this.loadData()
221 },
222
223 err => this.notifier.error(err.message)
224 )
225 }
226
227 async setEmailsAsVerified (users: User[]) {
228 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
229 () => {
230 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
231 this.loadData()
232 },
233
234 err => this.notifier.error(err.message)
235 )
236 }
237
238 isInSelectionMode () {
239 return this.selectedUsers.length !== 0
240 }
241
242 protected loadData () {
243 this.selectedUsers = []
244
245 this.userService.getUsers({
246 pagination: this.pagination,
247 sort: this.sort,
248 search: this.search
249 }).subscribe(
250 resultList => {
251 this.users = resultList.data
252 this.totalRecords = resultList.total
253 },
254
255 err => this.notifier.error(err.message)
256 )
257 }
258 }