]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/overview/users/user-list/user-list.component.ts
Increase global font size
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / users / user-list / user-list.component.ts
CommitLineData
f77eb73b 1import { SortMeta } from 'primeng/api'
2e46eb97 2import { Component, OnInit, ViewChild } from '@angular/core'
1fd61899 3import { ActivatedRoute, Router } from '@angular/router'
87a0cac6 4import { AuthService, ConfirmService, LocalStorageService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
eaa52952 5import { prepareIcu, getAPIHost } from '@app/helpers'
1fd61899 6import { AdvancedInputFilter } from '@app/shared/shared-forms'
a282e4d8
C
7import { Actor, DropdownAction } from '@app/shared/shared-main'
8import { AccountMutedStatus, BlocklistService, UserBanModalComponent, UserModerationDisplayType } from '@app/shared/shared-moderation'
d92d070c 9import { UserAdminService } from '@app/shared/shared-users'
2989628b 10import { User, UserRole } from '@shared/models'
7da18e44 11
4f5d0459
RK
12type UserForList = User & {
13 rawVideoQuota: number
14 rawVideoQuotaUsed: number
15 rawVideoQuotaDaily: number
16 rawVideoQuotaUsedDaily: number
17}
18
7da18e44
C
19@Component({
20 selector: 'my-user-list',
ec8d8440
C
21 templateUrl: './user-list.component.html',
22 styleUrls: [ './user-list.component.scss' ]
7da18e44 23})
2e46eb97 24export class UserListComponent extends RestTable implements OnInit {
87a0cac6
C
25 private static readonly LOCAL_STORAGE_SELECTED_COLUMNS_KEY = 'admin-user-list-selected-columns'
26
f36da21e 27 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
791645e6 28
a282e4d8 29 users: (User & { accountMutedStatus: AccountMutedStatus })[] = []
1fd61899 30
d592e0a9 31 totalRecords = 0
ab998f7b 32 sort: SortMeta = { field: 'createdAt', order: 1 }
d592e0a9 33 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
1fd61899 34
bc99dfe5 35 highlightBannedUsers = false
141b177d 36
791645e6 37 selectedUsers: User[] = []
9b82d49d 38 bulkUserActions: DropdownAction<User[]>[][] = []
52c4976f 39 columns: { id: string, label: string }[]
791645e6 40
1fd61899
C
41 inputFilters: AdvancedInputFilter[] = [
42 {
978c87e7
C
43 title: $localize`Advanced filters`,
44 children: [
45 {
dd6d2a7c 46 value: 'banned:true',
978c87e7
C
47 label: $localize`Banned users`
48 }
49 ]
1fd61899
C
50 }
51 ]
52
a282e4d8
C
53 userModerationDisplayOptions: UserModerationDisplayType = {
54 instanceAccount: true,
55 instanceUser: true,
56 myAccount: false
57 }
58
2989628b
C
59 requiresEmailVerification = false
60
87a0cac6 61 private _selectedColumns: string[] = []
ba430d75 62
df98563e 63 constructor (
5ed46c1b
C
64 protected route: ActivatedRoute,
65 protected router: Router,
f8b2c1b4 66 private notifier: Notifier,
5769e1db 67 private confirmService: ConfirmService,
fc2ec87a 68 private serverService: ServerService,
a95a4cc8 69 private auth: AuthService,
a282e4d8 70 private blocklist: BlocklistService,
87a0cac6
C
71 private userAdminService: UserAdminService,
72 private peertubeLocalStorage: LocalStorageService
5ed46c1b 73 ) {
d592e0a9 74 super()
7da18e44
C
75 }
76
a95a4cc8
C
77 get authUser () {
78 return this.auth.getUser()
79 }
80
bc99dfe5 81 get selectedColumns () {
87a0cac6 82 return this._selectedColumns || []
bc99dfe5
RK
83 }
84
52c4976f 85 set selectedColumns (val: string[]) {
bc99dfe5 86 this._selectedColumns = val
87a0cac6
C
87
88 this.saveSelectedColumns()
bc99dfe5
RK
89 }
90
ab998f7b 91 ngOnInit () {
ba430d75 92 this.serverService.getConfig()
2989628b 93 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
ba430d75 94
24b9417c 95 this.initialize()
8491293b 96
791645e6 97 this.bulkUserActions = [
9b82d49d
RK
98 [
99 {
66357162
C
100 label: $localize`Delete`,
101 description: $localize`Videos will be deleted, comments will be tombstoned.`,
9b82d49d
RK
102 handler: users => this.removeUsers(users),
103 isDisplayed: users => users.every(u => this.authUser.canManage(u))
104 },
105 {
66357162
C
106 label: $localize`Ban`,
107 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
9b82d49d
RK
108 handler: users => this.openBanUserModal(users),
109 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
110 },
111 {
66357162 112 label: $localize`Unban`,
9b82d49d
RK
113 handler: users => this.unbanUsers(users),
114 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
a95a4cc8 115 }
9b82d49d
RK
116 ],
117 [
118 {
66357162 119 label: $localize`Set Email as Verified`,
9b82d49d
RK
120 handler: users => this.setEmailsAsVerified(users),
121 isDisplayed: users => {
122 return this.requiresEmailVerification &&
123 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
124 }
125 }
126 ]
791645e6 127 ]
bc99dfe5
RK
128
129 this.columns = [
c3185413 130 { id: 'username', label: $localize`Username` },
a282e4d8 131 { id: 'role', label: $localize`Role` },
c3185413
C
132 { id: 'email', label: $localize`Email` },
133 { id: 'quota', label: $localize`Video quota` },
87a0cac6
C
134 { id: 'createdAt', label: $localize`Created` },
135 { id: 'lastLoginDate', label: $localize`Last login` },
136
137 { id: 'quotaDaily', label: $localize`Daily quota` },
138 { id: 'pluginAuth', label: $localize`Auth plugin` }
bc99dfe5 139 ]
52c4976f 140
87a0cac6
C
141 this.loadSelectedColumns()
142 }
143
144 loadSelectedColumns () {
145 const result = this.peertubeLocalStorage.getItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY)
146
147 if (result) {
148 try {
149 this.selectedColumns = JSON.parse(result)
150 return
151 } catch (err) {
152 console.error('Cannot load selected columns.', err)
153 }
154 }
155
156 // Default behaviour
157 this.selectedColumns = [ 'username', 'role', 'email', 'quota', 'createdAt', 'lastLoginDate' ]
158 return
159 }
52c4976f 160
87a0cac6
C
161 saveSelectedColumns () {
162 this.peertubeLocalStorage.setItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY, JSON.stringify(this.selectedColumns))
141b177d
C
163 }
164
8e11a1b3
C
165 getIdentifier () {
166 return 'UserListComponent'
167 }
168
bc99dfe5
RK
169 getRoleClass (role: UserRole) {
170 switch (role) {
171 case UserRole.ADMINISTRATOR:
172 return 'badge-purple'
173 case UserRole.MODERATOR:
174 return 'badge-blue'
175 default:
176 return 'badge-yellow'
177 }
178 }
179
52c4976f
C
180 isSelected (id: string) {
181 return this.selectedColumns.find(c => c === id)
182 }
183
184 getColumn (id: string) {
185 return this.columns.find(c => c.id === id)
bc99dfe5
RK
186 }
187
4f5d0459 188 getUserVideoQuotaPercentage (user: UserForList) {
bc99dfe5
RK
189 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
190 }
191
4f5d0459 192 getUserVideoQuotaDailyPercentage (user: UserForList) {
1fef6bcc 193 console.log(user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily)
bc99dfe5
RK
194 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
195 }
196
791645e6
C
197 openBanUserModal (users: User[]) {
198 for (const user of users) {
199 if (user.username === 'root') {
66357162 200 this.notifier.error($localize`You cannot ban root.`)
791645e6
C
201 return
202 }
203 }
204
205 this.userBanModal.openModal(users)
206 }
207
2fbe7f19 208 onUserChanged () {
2e46eb97 209 this.reloadData()
791645e6
C
210 }
211
212 async unbanUsers (users: User[]) {
eaa52952
C
213 const res = await this.confirmService.confirm(
214 prepareIcu($localize`Do you really want to unban {count, plural, =1 {1 user} other {{count} users}}?`)(
215 { count: users.length },
216 $localize`Do you really want to unban ${users.length} users?`
217 ),
218 $localize`Unban`
219 )
220
791645e6
C
221 if (res === false) return
222
d92d070c 223 this.userAdminService.unbanUsers(users)
1378c0d3
C
224 .subscribe({
225 next: () => {
eaa52952
C
226 this.notifier.success(
227 prepareIcu($localize`{count, plural, =1 {1 user} other {{count} users}} unbanned.`)(
228 { count: users.length },
229 $localize`${users.length} users unbanned.`
230 )
231 )
2e46eb97 232 this.reloadData()
791645e6
C
233 },
234
1378c0d3
C
235 error: err => this.notifier.error(err.message)
236 })
791645e6
C
237 }
238
239 async removeUsers (users: User[]) {
eaa52952
C
240 if (users.some(u => u.username === 'root')) {
241 this.notifier.error($localize`You cannot delete root.`)
242 return
791645e6
C
243 }
244
eaa52952
C
245 const message = $localize`<p>You can't create users or channels with a username that already used by a deleted user/channel.</p>` +
246 $localize`It means the following usernames will be permanently deleted and cannot be recovered:` +
247 '<ul>' + users.map(u => '<li>' + u.username + '</li>').join('') + '</ul>'
248
66357162 249 const res = await this.confirmService.confirm(message, $localize`Delete`)
791645e6
C
250 if (res === false) return
251
d92d070c 252 this.userAdminService.removeUser(users)
1378c0d3
C
253 .subscribe({
254 next: () => {
eaa52952
C
255 this.notifier.success(
256 prepareIcu($localize`{count, plural, =1 {1 user} other {{count} users}} deleted.`)(
257 { count: users.length },
258 $localize`${users.length} users deleted.`
259 )
260 )
261
1378c0d3
C
262 this.reloadData()
263 },
791645e6 264
1378c0d3
C
265 error: err => this.notifier.error(err.message)
266 })
791645e6
C
267 }
268
98ab5dc8 269 setEmailsAsVerified (users: User[]) {
d92d070c 270 this.userAdminService.updateUsers(users, { emailVerified: true })
1378c0d3
C
271 .subscribe({
272 next: () => {
eaa52952
C
273 this.notifier.success(
274 prepareIcu($localize`{count, plural, =1 {1 user} other {{count} users}} email set as verified.`)(
275 { count: users.length },
276 $localize`${users.length} users email set as verified.`
277 )
278 )
279
1378c0d3
C
280 this.reloadData()
281 },
fc2ec87a 282
1378c0d3
C
283 error: err => this.notifier.error(err.message)
284 })
fc2ec87a
JM
285 }
286
791645e6
C
287 isInSelectionMode () {
288 return this.selectedUsers.length !== 0
289 }
dffd5d12 290
2e46eb97 291 protected reloadData () {
dffd5d12
B
292 this.selectedUsers = []
293
d92d070c 294 this.userAdminService.getUsers({
8491293b
RK
295 pagination: this.pagination,
296 sort: this.sort,
297 search: this.search
1378c0d3
C
298 }).subscribe({
299 next: resultList => {
a282e4d8
C
300 this.users = resultList.data.map(u => ({
301 ...u,
302
303 accountMutedStatus: {
304 ...u.account,
305
306 nameWithHost: Actor.CREATE_BY_STRING(u.account.name, u.account.host),
307
308 mutedByInstance: false,
309 mutedByUser: false,
310 mutedServerByInstance: false,
311 mutedServerByUser: false
312 }
313 }))
8491293b 314 this.totalRecords = resultList.total
a282e4d8
C
315
316 this.loadMutedStatus()
8491293b 317 },
f8b2c1b4 318
1378c0d3
C
319 error: err => this.notifier.error(err.message)
320 })
dffd5d12 321 }
a282e4d8
C
322
323 private loadMutedStatus () {
324 this.blocklist.getStatus({ accounts: this.users.map(u => u.username + '@' + getAPIHost()) })
325 .subscribe(blockStatus => {
326 for (const user of this.users) {
327 user.accountMutedStatus.mutedByInstance = blockStatus.accounts[user.username + '@' + getAPIHost()].blockedByServer
328 }
329 })
330 }
7da18e44 331}