]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Merge branch 'move-utils-to-shared' of https://github.com/buoyantair/PeerTube into...
[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, ServerService } from '../../../core'
5 import { RestPagination, RestTable, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { User } from '../../../../../../shared'
8 import { UserBanModalComponent } from '@app/shared/moderation'
9 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
10
11 @Component({
12 selector: 'my-user-list',
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
15 })
16 export class UserListComponent extends RestTable implements OnInit {
17 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
18
19 users: User[] = []
20 totalRecords = 0
21 rowsPerPage = 10
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User[]>[] = []
27
28 constructor (
29 private notificationsService: NotificationsService,
30 private confirmService: ConfirmService,
31 private serverService: ServerService,
32 private userService: UserService,
33 private i18n: I18n
34 ) {
35 super()
36 }
37
38 get requiresEmailVerification () {
39 return this.serverService.getConfig().signup.requiresEmailVerification
40 }
41
42 ngOnInit () {
43 this.initialize()
44
45 this.bulkUserActions = [
46 {
47 label: this.i18n('Delete'),
48 handler: users => this.removeUsers(users)
49 },
50 {
51 label: this.i18n('Ban'),
52 handler: users => this.openBanUserModal(users),
53 isDisplayed: users => users.every(u => u.blocked === false)
54 },
55 {
56 label: this.i18n('Unban'),
57 handler: users => this.unbanUsers(users),
58 isDisplayed: users => users.every(u => u.blocked === true)
59 },
60 {
61 label: this.i18n('Set Email as Verified'),
62 handler: users => this.setEmailsAsVerified(users),
63 isDisplayed: users => this.requiresEmailVerification && users.every(u => !u.blocked && u.emailVerified === false)
64 }
65 ]
66 }
67
68 openBanUserModal (users: User[]) {
69 for (const user of users) {
70 if (user.username === 'root') {
71 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
72 return
73 }
74 }
75
76 this.userBanModal.openModal(users)
77 }
78
79 onUserChanged () {
80 this.loadData()
81 }
82
83 async unbanUsers (users: User[]) {
84 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
85
86 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
87 if (res === false) return
88
89 this.userService.unbanUsers(users)
90 .subscribe(
91 () => {
92 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
93
94 this.notificationsService.success(this.i18n('Success'), message)
95 this.loadData()
96 },
97
98 err => this.notificationsService.error(this.i18n('Error'), err.message)
99 )
100 }
101
102 async removeUsers (users: User[]) {
103 for (const user of users) {
104 if (user.username === 'root') {
105 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
106 return
107 }
108 }
109
110 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
111 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
112 if (res === false) return
113
114 this.userService.removeUser(users).subscribe(
115 () => {
116 this.notificationsService.success(
117 this.i18n('Success'),
118 this.i18n('{{num}} users deleted.', { num: users.length })
119 )
120 this.loadData()
121 },
122
123 err => this.notificationsService.error(this.i18n('Error'), err.message)
124 )
125 }
126
127 async setEmailsAsVerified (users: User[]) {
128 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
129 () => {
130 this.notificationsService.success(
131 this.i18n('Success'),
132 this.i18n('{{num}} users email set as verified.', { num: users.length })
133 )
134 this.loadData()
135 },
136
137 err => this.notificationsService.error(this.i18n('Error'), err.message)
138 )
139 }
140
141 isInSelectionMode () {
142 return this.selectedUsers.length !== 0
143 }
144
145 protected loadData () {
146 this.selectedUsers = []
147
148 this.userService.getUsers(this.pagination, this.sort, this.search)
149 .subscribe(
150 resultList => {
151 this.users = resultList.data
152 this.totalRecords = resultList.total
153 },
154
155 err => this.notificationsService.error(this.i18n('Error'), err.message)
156 )
157 }
158 }