]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Add ability to ban/unban users
[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 { NotificationsService } from 'angular2-notifications'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import { ConfirmService } from '../../../core'
5 import { RestPagination, RestTable, User } from '../../../shared'
6 import { UserService } from '../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
9 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
10 import { UserBanModalComponent } from '@app/+admin/users/user-list/user-ban-modal.component'
11
12 @Component({
13 selector: 'my-user-list',
14 templateUrl: './user-list.component.html',
15 styleUrls: [ './user-list.component.scss' ]
16 })
17 export class UserListComponent extends RestTable implements OnInit {
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
20 users: User[] = []
21 totalRecords = 0
22 rowsPerPage = 10
23 sort: SortMeta = { field: 'createdAt', order: 1 }
24 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
25 userActions: DropdownAction<User>[] = []
26
27 private userToBan: User
28 private openedModal: NgbModalRef
29
30 constructor (
31 private notificationsService: NotificationsService,
32 private confirmService: ConfirmService,
33 private userService: UserService,
34 private i18n: I18n
35 ) {
36 super()
37
38 this.userActions = [
39 {
40 label: this.i18n('Edit'),
41 linkBuilder: this.getRouterUserEditLink
42 },
43 {
44 label: this.i18n('Delete'),
45 handler: user => this.removeUser(user)
46 },
47 {
48 label: this.i18n('Ban'),
49 handler: user => this.openBanUserModal(user),
50 isDisplayed: user => !user.blocked
51 },
52 {
53 label: this.i18n('Unban'),
54 handler: user => this.unbanUser(user),
55 isDisplayed: user => user.blocked
56 }
57 ]
58 }
59
60 ngOnInit () {
61 this.loadSort()
62 }
63
64 hideBanUserModal () {
65 this.userToBan = undefined
66 this.openedModal.close()
67 }
68
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
72 return
73 }
74
75 this.userBanModal.openModal(user)
76 }
77
78 onUserBanned () {
79 this.loadData()
80 }
81
82 async unbanUser (user: User) {
83 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
84 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
85 if (res === false) return
86
87 this.userService.unbanUser(user)
88 .subscribe(
89 () => {
90 this.notificationsService.success(
91 this.i18n('Success'),
92 this.i18n('User {{username}} unbanned.', { username: user.username })
93 )
94 this.loadData()
95 },
96
97 err => this.notificationsService.error(this.i18n('Error'), err.message)
98 )
99 }
100
101 async removeUser (user: User) {
102 if (user.username === 'root') {
103 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
104 return
105 }
106
107 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete'))
108 if (res === false) return
109
110 this.userService.removeUser(user).subscribe(
111 () => {
112 this.notificationsService.success(
113 this.i18n('Success'),
114 this.i18n('User {{username}} deleted.', { username: user.username })
115 )
116 this.loadData()
117 },
118
119 err => this.notificationsService.error(this.i18n('Error'), err.message)
120 )
121 }
122
123 getRouterUserEditLink (user: User) {
124 return [ '/admin', 'users', 'update', user.id ]
125 }
126
127 protected loadData () {
128 this.userService.getUsers(this.pagination, this.sort)
129 .subscribe(
130 resultList => {
131 this.users = resultList.data
132 this.totalRecords = resultList.total
133 },
134
135 err => this.notificationsService.error(this.i18n('Error'), err.message)
136 )
137 }
138 }