]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } from '@angular/core' | |
2 | import { AuthService, Notifier } from '@app/core' | |
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 { ServerConfig, 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', { static: true }) 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 | private serverConfig: ServerConfig | |
29 | ||
30 | constructor ( | |
31 | private notifier: Notifier, | |
32 | private confirmService: ConfirmService, | |
33 | private serverService: ServerService, | |
34 | private userService: UserService, | |
35 | private auth: AuthService, | |
36 | private i18n: I18n | |
37 | ) { | |
38 | super() | |
39 | } | |
40 | ||
41 | get authUser () { | |
42 | return this.auth.getUser() | |
43 | } | |
44 | ||
45 | get requiresEmailVerification () { | |
46 | return this.serverConfig.signup.requiresEmailVerification | |
47 | } | |
48 | ||
49 | ngOnInit () { | |
50 | this.serverConfig = this.serverService.getTmpConfig() | |
51 | this.serverService.getConfig() | |
52 | .subscribe(config => this.serverConfig = config) | |
53 | ||
54 | this.initialize() | |
55 | ||
56 | this.bulkUserActions = [ | |
57 | [ | |
58 | { | |
59 | label: this.i18n('Delete'), | |
60 | description: this.i18n('Videos will be deleted, comments will be tombstoned.'), | |
61 | handler: users => this.removeUsers(users), | |
62 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
63 | }, | |
64 | { | |
65 | label: this.i18n('Ban'), | |
66 | description: this.i18n('Videos will be kept as private, comments will be kept as is.'), | |
67 | handler: users => this.openBanUserModal(users), | |
68 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
69 | }, | |
70 | { | |
71 | label: this.i18n('Unban'), | |
72 | handler: users => this.unbanUsers(users), | |
73 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
74 | } | |
75 | ], | |
76 | [ | |
77 | { | |
78 | label: this.i18n('Set Email as Verified'), | |
79 | handler: users => this.setEmailsAsVerified(users), | |
80 | isDisplayed: users => { | |
81 | return this.requiresEmailVerification && | |
82 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
83 | } | |
84 | } | |
85 | ] | |
86 | ] | |
87 | } | |
88 | ||
89 | openBanUserModal (users: User[]) { | |
90 | for (const user of users) { | |
91 | if (user.username === 'root') { | |
92 | this.notifier.error(this.i18n('You cannot ban root.')) | |
93 | return | |
94 | } | |
95 | } | |
96 | ||
97 | this.userBanModal.openModal(users) | |
98 | } | |
99 | ||
100 | onUserChanged () { | |
101 | this.loadData() | |
102 | } | |
103 | ||
104 | async unbanUsers (users: User[]) { | |
105 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
106 | ||
107 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
108 | if (res === false) return | |
109 | ||
110 | this.userService.unbanUsers(users) | |
111 | .subscribe( | |
112 | () => { | |
113 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
114 | ||
115 | this.notifier.success(message) | |
116 | this.loadData() | |
117 | }, | |
118 | ||
119 | err => this.notifier.error(err.message) | |
120 | ) | |
121 | } | |
122 | ||
123 | async removeUsers (users: User[]) { | |
124 | for (const user of users) { | |
125 | if (user.username === 'root') { | |
126 | this.notifier.error(this.i18n('You cannot delete root.')) | |
127 | return | |
128 | } | |
129 | } | |
130 | ||
131 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
132 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
133 | if (res === false) return | |
134 | ||
135 | this.userService.removeUser(users).subscribe( | |
136 | () => { | |
137 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) | |
138 | this.loadData() | |
139 | }, | |
140 | ||
141 | err => this.notifier.error(err.message) | |
142 | ) | |
143 | } | |
144 | ||
145 | async setEmailsAsVerified (users: User[]) { | |
146 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
147 | () => { | |
148 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) | |
149 | this.loadData() | |
150 | }, | |
151 | ||
152 | err => this.notifier.error(err.message) | |
153 | ) | |
154 | } | |
155 | ||
156 | isInSelectionMode () { | |
157 | return this.selectedUsers.length !== 0 | |
158 | } | |
159 | ||
160 | protected loadData () { | |
161 | this.selectedUsers = [] | |
162 | ||
163 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
164 | .subscribe( | |
165 | resultList => { | |
166 | this.users = resultList.data | |
167 | this.totalRecords = resultList.total | |
168 | }, | |
169 | ||
170 | err => this.notifier.error(err.message) | |
171 | ) | |
172 | } | |
173 | } |