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