]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { AuthService, Notifier } from '@app/core'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import { ConfirmService, ServerService } from '../../../core'
5 import { RestPagination, RestTable, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { ServerConfig, User } from '../../../../../../shared'
8 import { UserBanModalComponent } from '@app/shared/moderation'
9 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
10
11 @Component({
12 selector: 'my-user-list',
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
15 })
16 export class UserListComponent extends RestTable implements OnInit {
17 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
18
19 users: User[] = []
20 totalRecords = 0
21 rowsPerPage = 10
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User[]>[] = []
27
28 private serverConfig: ServerConfig
29
30 constructor (
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private serverService: ServerService,
34 private userService: UserService,
35 private auth: AuthService,
36 private i18n: I18n
37 ) {
38 super()
39 }
40
41 get authUser () {
42 return this.auth.getUser()
43 }
44
45 get requiresEmailVerification () {
46 return this.serverConfig.signup.requiresEmailVerification
47 }
48
49 ngOnInit () {
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
53
54 this.initialize()
55
56 this.bulkUserActions = [
57 {
58 label: this.i18n('Delete'),
59 handler: users => this.removeUsers(users),
60 isDisplayed: users => users.every(u => this.authUser.canManage(u))
61 },
62 {
63 label: this.i18n('Ban'),
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 label: this.i18n('Set Email as Verified'),
74 handler: users => this.setEmailsAsVerified(users),
75 isDisplayed: users => {
76 return this.requiresEmailVerification &&
77 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
78 }
79 }
80 ]
81 }
82
83 openBanUserModal (users: User[]) {
84 for (const user of users) {
85 if (user.username === 'root') {
86 this.notifier.error(this.i18n('You cannot ban root.'))
87 return
88 }
89 }
90
91 this.userBanModal.openModal(users)
92 }
93
94 onUserChanged () {
95 this.loadData()
96 }
97
98 async unbanUsers (users: User[]) {
99 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
100
101 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
102 if (res === false) return
103
104 this.userService.unbanUsers(users)
105 .subscribe(
106 () => {
107 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
108
109 this.notifier.success(message)
110 this.loadData()
111 },
112
113 err => this.notifier.error(err.message)
114 )
115 }
116
117 async removeUsers (users: User[]) {
118 for (const user of users) {
119 if (user.username === 'root') {
120 this.notifier.error(this.i18n('You cannot delete root.'))
121 return
122 }
123 }
124
125 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
126 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
127 if (res === false) return
128
129 this.userService.removeUser(users).subscribe(
130 () => {
131 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
132 this.loadData()
133 },
134
135 err => this.notifier.error(err.message)
136 )
137 }
138
139 async setEmailsAsVerified (users: User[]) {
140 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
141 () => {
142 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
143 this.loadData()
144 },
145
146 err => this.notifier.error(err.message)
147 )
148 }
149
150 isInSelectionMode () {
151 return this.selectedUsers.length !== 0
152 }
153
154 protected loadData () {
155 this.selectedUsers = []
156
157 this.userService.getUsers(this.pagination, this.sort, this.search)
158 .subscribe(
159 resultList => {
160 this.users = resultList.data
161 this.totalRecords = resultList.total
162 },
163
164 err => this.notifier.error(err.message)
165 )
166 }
167 }