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

325 lines
9.4 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'
import { AuthService, ConfirmService, LocalStorageService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
2023-06-06 14:32:47 +02:00
import { formatICU, 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'
import { User, UserRole, UserRoleType } from '@peertube/peertube-models'
import { logger } from '@root-helpers/logger'
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
})
export class UserListComponent extends RestTable <User> implements OnInit {
private static readonly LOCAL_STORAGE_SELECTED_COLUMNS_KEY = 'admin-user-list-selected-columns'
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
bulkActions: 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
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,
private userAdminService: UserAdminService,
private peertubeLocalStorage: LocalStorageService
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
this.saveSelectedColumns()
}
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()
this.bulkActions = [
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` },
{ id: 'lastLoginDate', label: $localize`Last login` },
{ id: 'quotaDaily', label: $localize`Daily quota` },
{ id: 'pluginAuth', label: $localize`Auth plugin` }
]
2020-08-11 16:07:53 +02:00
this.loadSelectedColumns()
}
loadSelectedColumns () {
const result = this.peertubeLocalStorage.getItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY)
if (result) {
try {
this.selectedColumns = JSON.parse(result)
return
} catch (err) {
logger.error('Cannot load selected columns.', err)
}
}
// Default behaviour
this.selectedColumns = [ 'username', 'role', 'email', 'quota', 'createdAt', 'lastLoginDate' ]
return
}
2020-08-11 16:07:53 +02:00
saveSelectedColumns () {
this.peertubeLocalStorage.setItem(UserListComponent.LOCAL_STORAGE_SELECTED_COLUMNS_KEY, JSON.stringify(this.selectedColumns))
2018-08-09 17:51:25 +02:00
}
2020-04-08 10:49:26 +02:00
getIdentifier () {
return 'UserListComponent'
}
getRoleClass (role: UserRoleType) {
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[]) {
2022-05-24 16:29:01 +02:00
const res = await this.confirmService.confirm(
2023-06-06 14:32:47 +02:00
formatICU(
$localize`Do you really want to unban {count, plural, =1 {1 user} other {{count} users}}?`,
{ count: users.length }
2022-05-24 16:29:01 +02:00
),
$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: () => {
2022-05-24 16:29:01 +02:00
this.notifier.success(
2023-06-06 14:32:47 +02:00
formatICU(
$localize`{count, plural, =1 {1 user unbanned.} other {{count} users unbanned.}}`,
{ count: users.length }
2022-05-24 16:29:01 +02:00
)
)
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[]) {
2022-05-24 16:29:01 +02:00
if (users.some(u => u.username === 'root')) {
this.notifier.error($localize`You cannot delete root.`)
return
2018-10-08 15:15:11 +02:00
}
2022-05-24 16:29:01 +02:00
const message = $localize`<p>You can't create users or channels with a username that already used by a deleted user/channel.</p>` +
$localize`It means the following usernames will be permanently deleted and cannot be recovered:` +
'<ul>' + users.map(u => '<li>' + u.username + '</li>').join('') + '</ul>'
const res = await this.confirmService.confirm(message, $localize`Delete`)
2018-10-08 15:15:11 +02:00
if (res === false) return
this.userAdminService.removeUsers(users)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
2022-05-24 16:29:01 +02:00
this.notifier.success(
2023-06-06 14:32:47 +02:00
formatICU(
$localize`{count, plural, =1 {1 user deleted.} other {{count} users deleted.}}`,
{ count: users.length }
2022-05-24 16:29:01 +02:00
)
)
2021-08-17 11:27:47 +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
}
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: () => {
2022-05-24 16:29:01 +02:00
this.notifier.success(
2023-06-06 14:32:47 +02:00
formatICU(
$localize`{count, plural, =1 {1 user email set as verified.} other {{count} user emails set as verified.}}`,
{ count: users.length }
2022-05-24 16:29:01 +02:00
)
)
2021-08-17 11:27:47 +02:00
this.reloadData()
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
}
2023-01-20 15:06:08 +01:00
protected reloadDataInternal () {
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
}