]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
8f01c7d51497c586e2848bc843f98900c0966300
[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 openBanUserModal (users: User[]) {
91 for (const user of users) {
92 if (user.username === 'root') {
93 this.notifier.error(this.i18n('You cannot ban root.'))
94 return
95 }
96 }
97
98 this.userBanModal.openModal(users)
99 }
100
101 onUserChanged () {
102 this.loadData()
103 }
104
105 switchToDefaultAvatar ($event: Event) {
106 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
107 }
108
109 async unbanUsers (users: User[]) {
110 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
111
112 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
113 if (res === false) return
114
115 this.userService.unbanUsers(users)
116 .subscribe(
117 () => {
118 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
119
120 this.notifier.success(message)
121 this.loadData()
122 },
123
124 err => this.notifier.error(err.message)
125 )
126 }
127
128 async removeUsers (users: User[]) {
129 for (const user of users) {
130 if (user.username === 'root') {
131 this.notifier.error(this.i18n('You cannot delete root.'))
132 return
133 }
134 }
135
136 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
137 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
138 if (res === false) return
139
140 this.userService.removeUser(users).subscribe(
141 () => {
142 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
143 this.loadData()
144 },
145
146 err => this.notifier.error(err.message)
147 )
148 }
149
150 async setEmailsAsVerified (users: User[]) {
151 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
152 () => {
153 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
154 this.loadData()
155 },
156
157 err => this.notifier.error(err.message)
158 )
159 }
160
161 isInSelectionMode () {
162 return this.selectedUsers.length !== 0
163 }
164
165 protected loadData () {
166 this.selectedUsers = []
167
168 this.userService.getUsers(this.pagination, this.sort, this.search)
169 .subscribe(
170 resultList => {
171 this.users = resultList.data
172 this.totalRecords = resultList.total
173 },
174
175 err => this.notifier.error(err.message)
176 )
177 }
178 }