]>
Commit | Line | Data |
---|---|---|
af5767ff | 1 | import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core' |
e724fa93 C |
2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
3 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
e724fa93 | 4 | import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component' |
79bd2632 | 5 | import { UserService } from '@app/shared/users' |
f8b2c1b4 | 6 | import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core' |
79bd2632 | 7 | import { User, UserRight } from '../../../../../shared/models/users' |
af5767ff C |
8 | import { Account } from '@app/shared/account/account.model' |
9 | import { BlocklistService } from '@app/shared/blocklist' | |
e724fa93 C |
10 | |
11 | @Component({ | |
12 | selector: 'my-user-moderation-dropdown', | |
f421fa06 | 13 | templateUrl: './user-moderation-dropdown.component.html' |
e724fa93 | 14 | }) |
af5767ff | 15 | export class UserModerationDropdownComponent implements OnChanges { |
f36da21e | 16 | @ViewChild('userBanModal', { static: false }) userBanModal: UserBanModalComponent |
e724fa93 C |
17 | |
18 | @Input() user: User | |
af5767ff C |
19 | @Input() account: Account |
20 | ||
79bd2632 | 21 | @Input() buttonSize: 'normal' | 'small' = 'normal' |
24b9417c | 22 | @Input() placement = 'left' |
79bd2632 | 23 | |
e724fa93 | 24 | @Output() userChanged = new EventEmitter() |
79bd2632 | 25 | @Output() userDeleted = new EventEmitter() |
e724fa93 | 26 | |
f97c91f7 | 27 | userActions: DropdownAction<{ user: User, account: Account }>[][] = [] |
e724fa93 | 28 | |
e724fa93 C |
29 | constructor ( |
30 | private authService: AuthService, | |
f8b2c1b4 | 31 | private notifier: Notifier, |
e724fa93 | 32 | private confirmService: ConfirmService, |
fc2ec87a | 33 | private serverService: ServerService, |
e724fa93 | 34 | private userService: UserService, |
af5767ff | 35 | private blocklistService: BlocklistService, |
a95a4cc8 | 36 | private auth: AuthService, |
e724fa93 C |
37 | private i18n: I18n |
38 | ) { } | |
39 | ||
fc2ec87a JM |
40 | get requiresEmailVerification () { |
41 | return this.serverService.getConfig().signup.requiresEmailVerification | |
42 | } | |
43 | ||
af5767ff | 44 | ngOnChanges () { |
79bd2632 | 45 | this.buildActions() |
e724fa93 C |
46 | } |
47 | ||
e724fa93 C |
48 | openBanUserModal (user: User) { |
49 | if (user.username === 'root') { | |
f8b2c1b4 | 50 | this.notifier.error(this.i18n('You cannot ban root.')) |
e724fa93 C |
51 | return |
52 | } | |
53 | ||
54 | this.userBanModal.openModal(user) | |
55 | } | |
56 | ||
57 | onUserBanned () { | |
58 | this.userChanged.emit() | |
59 | } | |
60 | ||
61 | async unbanUser (user: User) { | |
62 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
63 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
64 | if (res === false) return | |
65 | ||
791645e6 | 66 | this.userService.unbanUsers(user) |
e724fa93 C |
67 | .subscribe( |
68 | () => { | |
f8b2c1b4 | 69 | this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username })) |
e724fa93 C |
70 | |
71 | this.userChanged.emit() | |
72 | }, | |
73 | ||
f8b2c1b4 | 74 | err => this.notifier.error(err.message) |
e724fa93 C |
75 | ) |
76 | } | |
77 | ||
78 | async removeUser (user: User) { | |
79 | if (user.username === 'root') { | |
f8b2c1b4 | 80 | this.notifier.error(this.i18n('You cannot delete root.')) |
e724fa93 C |
81 | return |
82 | } | |
83 | ||
84 | const message = this.i18n('If you remove this user, you will not be able to create another with the same username!') | |
85 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
86 | if (res === false) return | |
87 | ||
88 | this.userService.removeUser(user).subscribe( | |
89 | () => { | |
f8b2c1b4 | 90 | this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username })) |
79bd2632 | 91 | this.userDeleted.emit() |
e724fa93 C |
92 | }, |
93 | ||
f8b2c1b4 | 94 | err => this.notifier.error(err.message) |
e724fa93 C |
95 | ) |
96 | } | |
97 | ||
fc2ec87a JM |
98 | setEmailAsVerified (user: User) { |
99 | this.userService.updateUser(user.id, { emailVerified: true }).subscribe( | |
100 | () => { | |
f8b2c1b4 | 101 | this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username })) |
a99e2d94 C |
102 | |
103 | this.userChanged.emit() | |
fc2ec87a JM |
104 | }, |
105 | ||
f8b2c1b4 | 106 | err => this.notifier.error(err.message) |
fc2ec87a JM |
107 | ) |
108 | } | |
109 | ||
af5767ff C |
110 | blockAccountByUser (account: Account) { |
111 | this.blocklistService.blockAccountByUser(account) | |
112 | .subscribe( | |
113 | () => { | |
f8b2c1b4 | 114 | this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })) |
af5767ff | 115 | |
65b21c96 | 116 | this.account.mutedByUser = true |
af5767ff C |
117 | this.userChanged.emit() |
118 | }, | |
119 | ||
f8b2c1b4 | 120 | err => this.notifier.error(err.message) |
af5767ff C |
121 | ) |
122 | } | |
123 | ||
124 | unblockAccountByUser (account: Account) { | |
125 | this.blocklistService.unblockAccountByUser(account) | |
126 | .subscribe( | |
127 | () => { | |
f8b2c1b4 | 128 | this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })) |
af5767ff | 129 | |
65b21c96 | 130 | this.account.mutedByUser = false |
af5767ff C |
131 | this.userChanged.emit() |
132 | }, | |
133 | ||
f8b2c1b4 | 134 | err => this.notifier.error(err.message) |
af5767ff C |
135 | ) |
136 | } | |
137 | ||
138 | blockServerByUser (host: string) { | |
139 | this.blocklistService.blockServerByUser(host) | |
140 | .subscribe( | |
141 | () => { | |
f8b2c1b4 | 142 | this.notifier.success(this.i18n('Instance {{host}} muted.', { host })) |
af5767ff | 143 | |
65b21c96 | 144 | this.account.mutedServerByUser = true |
af5767ff C |
145 | this.userChanged.emit() |
146 | }, | |
147 | ||
f8b2c1b4 | 148 | err => this.notifier.error(err.message) |
af5767ff C |
149 | ) |
150 | } | |
151 | ||
152 | unblockServerByUser (host: string) { | |
153 | this.blocklistService.unblockServerByUser(host) | |
154 | .subscribe( | |
155 | () => { | |
f8b2c1b4 | 156 | this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host })) |
af5767ff | 157 | |
65b21c96 C |
158 | this.account.mutedServerByUser = false |
159 | this.userChanged.emit() | |
160 | }, | |
161 | ||
f8b2c1b4 | 162 | err => this.notifier.error(err.message) |
65b21c96 C |
163 | ) |
164 | } | |
165 | ||
166 | blockAccountByInstance (account: Account) { | |
167 | this.blocklistService.blockAccountByInstance(account) | |
168 | .subscribe( | |
169 | () => { | |
f8b2c1b4 | 170 | this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })) |
65b21c96 C |
171 | |
172 | this.account.mutedByInstance = true | |
173 | this.userChanged.emit() | |
174 | }, | |
175 | ||
f8b2c1b4 | 176 | err => this.notifier.error(err.message) |
65b21c96 C |
177 | ) |
178 | } | |
179 | ||
180 | unblockAccountByInstance (account: Account) { | |
181 | this.blocklistService.unblockAccountByInstance(account) | |
182 | .subscribe( | |
183 | () => { | |
f8b2c1b4 | 184 | this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })) |
65b21c96 C |
185 | |
186 | this.account.mutedByInstance = false | |
187 | this.userChanged.emit() | |
188 | }, | |
189 | ||
f8b2c1b4 | 190 | err => this.notifier.error(err.message) |
65b21c96 C |
191 | ) |
192 | } | |
193 | ||
194 | blockServerByInstance (host: string) { | |
195 | this.blocklistService.blockServerByInstance(host) | |
196 | .subscribe( | |
197 | () => { | |
f8b2c1b4 | 198 | this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host })) |
65b21c96 C |
199 | |
200 | this.account.mutedServerByInstance = true | |
201 | this.userChanged.emit() | |
202 | }, | |
203 | ||
f8b2c1b4 | 204 | err => this.notifier.error(err.message) |
65b21c96 C |
205 | ) |
206 | } | |
207 | ||
208 | unblockServerByInstance (host: string) { | |
209 | this.blocklistService.unblockServerByInstance(host) | |
210 | .subscribe( | |
211 | () => { | |
f8b2c1b4 | 212 | this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host })) |
65b21c96 C |
213 | |
214 | this.account.mutedServerByInstance = false | |
af5767ff C |
215 | this.userChanged.emit() |
216 | }, | |
217 | ||
f8b2c1b4 | 218 | err => this.notifier.error(err.message) |
af5767ff C |
219 | ) |
220 | } | |
221 | ||
e724fa93 C |
222 | getRouterUserEditLink (user: User) { |
223 | return [ '/admin', 'users', 'update', user.id ] | |
224 | } | |
79bd2632 C |
225 | |
226 | private buildActions () { | |
227 | this.userActions = [] | |
228 | ||
229 | if (this.authService.isLoggedIn()) { | |
230 | const authUser = this.authService.getUser() | |
231 | ||
af5767ff C |
232 | if (this.user && authUser.id === this.user.id) return |
233 | ||
a95a4cc8 | 234 | if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) { |
f97c91f7 | 235 | this.userActions.push([ |
79bd2632 C |
236 | { |
237 | label: this.i18n('Edit'), | |
af5767ff | 238 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) |
79bd2632 C |
239 | }, |
240 | { | |
241 | label: this.i18n('Delete'), | |
af5767ff | 242 | handler: ({ user }) => this.removeUser(user) |
79bd2632 C |
243 | }, |
244 | { | |
245 | label: this.i18n('Ban'), | |
4e74e803 C |
246 | handler: ({ user }) => this.openBanUserModal(user), |
247 | isDisplayed: ({ user }) => !user.blocked | |
79bd2632 C |
248 | }, |
249 | { | |
250 | label: this.i18n('Unban'), | |
4e74e803 C |
251 | handler: ({ user }) => this.unbanUser(user), |
252 | isDisplayed: ({ user }) => user.blocked | |
fc2ec87a JM |
253 | }, |
254 | { | |
255 | label: this.i18n('Set Email as Verified'), | |
4e74e803 C |
256 | handler: ({ user }) => this.setEmailAsVerified(user), |
257 | isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false | |
af5767ff C |
258 | } |
259 | ]) | |
260 | } | |
261 | ||
65b21c96 | 262 | // Actions on accounts/servers |
af5767ff | 263 | if (this.account) { |
65b21c96 | 264 | // User actions |
f97c91f7 | 265 | this.userActions.push([ |
af5767ff C |
266 | { |
267 | label: this.i18n('Mute this account'), | |
4e74e803 C |
268 | isDisplayed: ({ account }) => account.mutedByUser === false, |
269 | handler: ({ account }) => this.blockAccountByUser(account) | |
af5767ff C |
270 | }, |
271 | { | |
272 | label: this.i18n('Unmute this account'), | |
4e74e803 C |
273 | isDisplayed: ({ account }) => account.mutedByUser === true, |
274 | handler: ({ account }) => this.unblockAccountByUser(account) | |
af5767ff C |
275 | }, |
276 | { | |
277 | label: this.i18n('Mute the instance'), | |
4e74e803 C |
278 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false, |
279 | handler: ({ account }) => this.blockServerByUser(account.host) | |
af5767ff C |
280 | }, |
281 | { | |
282 | label: this.i18n('Unmute the instance'), | |
4e74e803 C |
283 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true, |
284 | handler: ({ account }) => this.unblockServerByUser(account.host) | |
79bd2632 C |
285 | } |
286 | ]) | |
65b21c96 | 287 | |
f97c91f7 C |
288 | let instanceActions: DropdownAction<{ user: User, account: Account }>[] = [] |
289 | ||
65b21c96 C |
290 | // Instance actions |
291 | if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) { | |
f97c91f7 | 292 | instanceActions = instanceActions.concat([ |
65b21c96 C |
293 | { |
294 | label: this.i18n('Mute this account by your instance'), | |
4e74e803 C |
295 | isDisplayed: ({ account }) => account.mutedByInstance === false, |
296 | handler: ({ account }) => this.blockAccountByInstance(account) | |
65b21c96 C |
297 | }, |
298 | { | |
299 | label: this.i18n('Unmute this account by your instance'), | |
4e74e803 C |
300 | isDisplayed: ({ account }) => account.mutedByInstance === true, |
301 | handler: ({ account }) => this.unblockAccountByInstance(account) | |
65b21c96 C |
302 | } |
303 | ]) | |
304 | } | |
305 | ||
306 | // Instance actions | |
307 | if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) { | |
f97c91f7 | 308 | instanceActions = instanceActions.concat([ |
65b21c96 C |
309 | { |
310 | label: this.i18n('Mute the instance by your instance'), | |
4e74e803 C |
311 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false, |
312 | handler: ({ account }) => this.blockServerByInstance(account.host) | |
65b21c96 C |
313 | }, |
314 | { | |
315 | label: this.i18n('Unmute the instance by your instance'), | |
4e74e803 C |
316 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true, |
317 | handler: ({ account }) => this.unblockServerByInstance(account.host) | |
65b21c96 C |
318 | } |
319 | ]) | |
320 | } | |
f97c91f7 C |
321 | |
322 | if (instanceActions.length !== 0) { | |
323 | this.userActions.push(instanceActions) | |
324 | } | |
79bd2632 C |
325 | } |
326 | } | |
327 | } | |
e724fa93 | 328 | } |