PeerTube/client/src/app/+admin/overview/users/user-list/user-list.component.ts

280 lines
7.9 KiB
TypeScript
Raw Normal View History

2020-02-07 10:27:16 +01:00
import { SortMeta } from 'primeng/api'
2021-05-03 14:33:34 +02:00
import { Component, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
2022-01-21 11:03:25 +01:00
import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
2022-02-28 10:41:23 +01:00
import { getAPIHost } from '@app/helpers'
import { AdvancedInputFilter } from '@app/shared/shared-forms'
2022-02-28 10:41:23 +01:00
import { Actor, DropdownAction } from '@app/shared/shared-main'
import { AccountMutedStatus, BlocklistService, UserBanModalComponent, UserModerationDisplayType } from '@app/shared/shared-moderation'
2022-01-21 11:03:25 +01:00
import { UserAdminService } from '@app/shared/shared-users'
2021-06-04 13:31:41 +02:00
import { User, UserRole } from '@shared/models'
2016-08-09 21:45:21 +02:00
2020-07-23 21:30:04 +02:00
type UserForList = User & {
rawVideoQuota: number
rawVideoQuotaUsed: number
rawVideoQuotaDaily: number
rawVideoQuotaUsedDaily: number
}
2016-08-09 21:45:21 +02:00
@Component({
selector: 'my-user-list',
templateUrl: './user-list.component.html',
styleUrls: [ './user-list.component.scss' ]
2016-08-09 21:45:21 +02:00
})
2021-05-03 14:33:34 +02:00
export class UserListComponent extends RestTable implements OnInit {
2019-07-24 16:05:59 +02:00
@ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
2018-10-08 15:15:11 +02:00
2022-02-28 10:41:23 +01:00
users: (User & { accountMutedStatus: AccountMutedStatus })[] = []
totalRecords = 0
2018-02-23 14:36:16 +01:00
sort: SortMeta = { field: 'createdAt', order: 1 }
pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
highlightBannedUsers = false
2018-08-09 17:51:25 +02:00
2018-10-08 15:15:11 +02:00
selectedUsers: User[] = []
2020-01-15 19:25:51 +01:00
bulkUserActions: DropdownAction<User[]>[][] = []
2020-08-11 16:07:53 +02:00
columns: { id: string, label: string }[]
2018-10-08 15:15:11 +02:00
inputFilters: AdvancedInputFilter[] = [
{
title: $localize`Advanced filters`,
children: [
{
2021-11-03 14:23:55 +01:00
value: 'banned:true',
label: $localize`Banned users`
}
]
}
]
2022-02-28 10:41:23 +01:00
userModerationDisplayOptions: UserModerationDisplayType = {
instanceAccount: true,
instanceUser: true,
myAccount: false
}
2021-06-04 13:31:41 +02:00
requiresEmailVerification = false
2020-08-11 16:07:53 +02:00
private _selectedColumns: string[]
2019-12-18 15:31:54 +01:00
constructor (
2020-11-16 14:47:05 +01:00
protected route: ActivatedRoute,
protected router: Router,
private notifier: Notifier,
private confirmService: ConfirmService,
private serverService: ServerService,
2019-07-30 09:59:19 +02:00
private auth: AuthService,
2022-02-28 10:41:23 +01:00
private blocklist: BlocklistService,
2022-01-21 11:03:25 +01:00
private userAdminService: UserAdminService
2020-11-16 14:47:05 +01:00
) {
super()
2016-08-09 21:45:21 +02:00
}
2019-07-30 09:59:19 +02:00
get authUser () {
return this.auth.getUser()
}
get selectedColumns () {
return this._selectedColumns
}
2020-08-11 16:07:53 +02:00
set selectedColumns (val: string[]) {
this._selectedColumns = val
}
2018-02-23 14:36:16 +01:00
ngOnInit () {
2019-12-18 15:31:54 +01:00
this.serverService.getConfig()
2021-06-04 13:31:41 +02:00
.subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
2019-12-18 15:31:54 +01:00
2018-10-08 15:51:38 +02:00
this.initialize()
2018-10-08 15:15:11 +02:00
this.bulkUserActions = [
2020-01-15 19:25:51 +01:00
[
{
label: $localize`Delete`,
description: $localize`Videos will be deleted, comments will be tombstoned.`,
2020-01-15 19:25:51 +01:00
handler: users => this.removeUsers(users),
isDisplayed: users => users.every(u => this.authUser.canManage(u))
},
{
label: $localize`Ban`,
description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
2020-01-15 19:25:51 +01:00
handler: users => this.openBanUserModal(users),
isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
},
{
label: $localize`Unban`,
2020-01-15 19:25:51 +01:00
handler: users => this.unbanUsers(users),
isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
2019-07-30 09:59:19 +02:00
}
2020-01-15 19:25:51 +01:00
],
[
{
label: $localize`Set Email as Verified`,
2020-01-15 19:25:51 +01:00
handler: users => this.setEmailsAsVerified(users),
isDisplayed: users => {
return this.requiresEmailVerification &&
users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
}
}
]
2018-10-08 15:15:11 +02:00
]
this.columns = [
2021-07-21 16:22:18 +02:00
{ id: 'username', label: $localize`Username` },
2022-02-28 10:41:23 +01:00
{ id: 'role', label: $localize`Role` },
2021-07-21 16:22:18 +02:00
{ id: 'email', label: $localize`Email` },
{ id: 'quota', label: $localize`Video quota` },
{ id: 'createdAt', label: $localize`Created` }
]
2020-08-11 16:07:53 +02:00
this.selectedColumns = this.columns.map(c => c.id)
2021-07-21 16:22:18 +02:00
this.columns.push({ id: 'quotaDaily', label: $localize`Daily quota` })
this.columns.push({ id: 'pluginAuth', label: $localize`Auth plugin` })
this.columns.push({ id: 'lastLoginDate', label: $localize`Last login` })
2018-08-09 17:51:25 +02:00
}
2020-04-08 10:49:26 +02:00
getIdentifier () {
return 'UserListComponent'
}
getRoleClass (role: UserRole) {
switch (role) {
case UserRole.ADMINISTRATOR:
return 'badge-purple'
case UserRole.MODERATOR:
return 'badge-blue'
default:
return 'badge-yellow'
}
}
2020-08-11 16:07:53 +02:00
isSelected (id: string) {
return this.selectedColumns.find(c => c === id)
}
getColumn (id: string) {
return this.columns.find(c => c.id === id)
}
2020-07-23 21:30:04 +02:00
getUserVideoQuotaPercentage (user: UserForList) {
return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
}
2020-07-23 21:30:04 +02:00
getUserVideoQuotaDailyPercentage (user: UserForList) {
return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
}
2018-10-08 15:15:11 +02:00
openBanUserModal (users: User[]) {
for (const user of users) {
if (user.username === 'root') {
this.notifier.error($localize`You cannot ban root.`)
2018-10-08 15:15:11 +02:00
return
}
}
this.userBanModal.openModal(users)
}
2018-11-15 09:24:56 +01:00
onUserChanged () {
2021-05-03 14:33:34 +02:00
this.reloadData()
2018-10-08 15:15:11 +02:00
}
async unbanUsers (users: User[]) {
const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
2018-10-08 15:15:11 +02:00
if (res === false) return
2022-01-21 11:03:25 +01:00
this.userAdminService.unbanUsers(users)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`${users.length} users unbanned.`)
2021-05-03 14:33:34 +02:00
this.reloadData()
2018-10-08 15:15:11 +02:00
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
2018-10-08 15:15:11 +02:00
}
async removeUsers (users: User[]) {
for (const user of users) {
if (user.username === 'root') {
this.notifier.error($localize`You cannot delete root.`)
2018-10-08 15:15:11 +02:00
return
}
}
const message = $localize`If you remove these users, you will not be able to create others with the same username!`
const res = await this.confirmService.confirm(message, $localize`Delete`)
2018-10-08 15:15:11 +02:00
if (res === false) return
2022-01-21 11:03:25 +01:00
this.userAdminService.removeUser(users)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`${users.length} users deleted.`)
this.reloadData()
},
2018-10-08 15:15:11 +02:00
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
2018-10-08 15:15:11 +02:00
}
2021-08-25 16:14:11 +02:00
setEmailsAsVerified (users: User[]) {
2022-01-21 11:03:25 +01:00
this.userAdminService.updateUsers(users, { emailVerified: true })
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`${users.length} users email set as verified.`)
this.reloadData()
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
}
2018-10-08 15:15:11 +02:00
isInSelectionMode () {
return this.selectedUsers.length !== 0
}
2021-05-03 14:33:34 +02:00
protected reloadData () {
this.selectedUsers = []
2022-01-21 11:03:25 +01:00
this.userAdminService.getUsers({
pagination: this.pagination,
sort: this.sort,
search: this.search
2021-08-17 11:27:47 +02:00
}).subscribe({
next: resultList => {
2022-02-28 10:41:23 +01:00
this.users = resultList.data.map(u => ({
...u,
accountMutedStatus: {
...u.account,
nameWithHost: Actor.CREATE_BY_STRING(u.account.name, u.account.host),
mutedByInstance: false,
mutedByUser: false,
mutedServerByInstance: false,
mutedServerByUser: false
}
}))
this.totalRecords = resultList.total
2022-02-28 10:41:23 +01:00
this.loadMutedStatus()
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
}
2022-02-28 10:41:23 +01:00
private loadMutedStatus () {
this.blocklist.getStatus({ accounts: this.users.map(u => u.username + '@' + getAPIHost()) })
.subscribe(blockStatus => {
for (const user of this.users) {
user.accountMutedStatus.mutedByInstance = blockStatus.accounts[user.username + '@' + getAPIHost()].blockedByServer
}
})
}
2016-08-09 21:45:21 +02:00
}