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