1 import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { I18n } from '@ngx-translate/i18n-polyfill'
3 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
4 import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
5 import { UserService } from '@app/shared/users'
6 import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
7 import { User, UserRight } from '../../../../../shared/models/users'
8 import { Account } from '@app/shared/account/account.model'
9 import { BlocklistService } from '@app/shared/blocklist'
12 selector: 'my-user-moderation-dropdown',
13 templateUrl: './user-moderation-dropdown.component.html'
15 export class UserModerationDropdownComponent implements OnChanges {
16 @ViewChild('userBanModal', { static: false }) userBanModal: UserBanModalComponent
19 @Input() account: Account
21 @Input() buttonSize: 'normal' | 'small' = 'normal'
22 @Input() placement = 'left'
24 @Output() userChanged = new EventEmitter()
25 @Output() userDeleted = new EventEmitter()
27 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
30 private authService: AuthService,
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private serverService: ServerService,
34 private userService: UserService,
35 private blocklistService: BlocklistService,
36 private auth: AuthService,
40 get requiresEmailVerification () {
41 return this.serverService.getConfig().signup.requiresEmailVerification
48 openBanUserModal (user: User) {
49 if (user.username === 'root') {
50 this.notifier.error(this.i18n('You cannot ban root.'))
54 this.userBanModal.openModal(user)
58 this.userChanged.emit()
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
66 this.userService.unbanUsers(user)
69 this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
71 this.userChanged.emit()
74 err => this.notifier.error(err.message)
78 async removeUser (user: User) {
79 if (user.username === 'root') {
80 this.notifier.error(this.i18n('You cannot delete root.'))
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
88 this.userService.removeUser(user).subscribe(
90 this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
91 this.userDeleted.emit()
94 err => this.notifier.error(err.message)
98 setEmailAsVerified (user: User) {
99 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
101 this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
103 this.userChanged.emit()
106 err => this.notifier.error(err.message)
110 blockAccountByUser (account: Account) {
111 this.blocklistService.blockAccountByUser(account)
114 this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
116 this.account.mutedByUser = true
117 this.userChanged.emit()
120 err => this.notifier.error(err.message)
124 unblockAccountByUser (account: Account) {
125 this.blocklistService.unblockAccountByUser(account)
128 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
130 this.account.mutedByUser = false
131 this.userChanged.emit()
134 err => this.notifier.error(err.message)
138 blockServerByUser (host: string) {
139 this.blocklistService.blockServerByUser(host)
142 this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
144 this.account.mutedServerByUser = true
145 this.userChanged.emit()
148 err => this.notifier.error(err.message)
152 unblockServerByUser (host: string) {
153 this.blocklistService.unblockServerByUser(host)
156 this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
158 this.account.mutedServerByUser = false
159 this.userChanged.emit()
162 err => this.notifier.error(err.message)
166 blockAccountByInstance (account: Account) {
167 this.blocklistService.blockAccountByInstance(account)
170 this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
172 this.account.mutedByInstance = true
173 this.userChanged.emit()
176 err => this.notifier.error(err.message)
180 unblockAccountByInstance (account: Account) {
181 this.blocklistService.unblockAccountByInstance(account)
184 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
186 this.account.mutedByInstance = false
187 this.userChanged.emit()
190 err => this.notifier.error(err.message)
194 blockServerByInstance (host: string) {
195 this.blocklistService.blockServerByInstance(host)
198 this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
200 this.account.mutedServerByInstance = true
201 this.userChanged.emit()
204 err => this.notifier.error(err.message)
208 unblockServerByInstance (host: string) {
209 this.blocklistService.unblockServerByInstance(host)
212 this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
214 this.account.mutedServerByInstance = false
215 this.userChanged.emit()
218 err => this.notifier.error(err.message)
222 getRouterUserEditLink (user: User) {
223 return [ '/admin', 'users', 'update', user.id ]
226 private buildActions () {
227 this.userActions = []
229 if (this.authService.isLoggedIn()) {
230 const authUser = this.authService.getUser()
232 if (this.user && authUser.id === this.user.id) return
234 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
235 this.userActions.push([
237 label: this.i18n('Edit'),
238 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
241 label: this.i18n('Delete'),
242 handler: ({ user }) => this.removeUser(user)
245 label: this.i18n('Ban'),
246 handler: ({ user }) => this.openBanUserModal(user),
247 isDisplayed: ({ user }) => !user.blocked
250 label: this.i18n('Unban'),
251 handler: ({ user }) => this.unbanUser(user),
252 isDisplayed: ({ user }) => user.blocked
255 label: this.i18n('Set Email as Verified'),
256 handler: ({ user }) => this.setEmailAsVerified(user),
257 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
262 // Actions on accounts/servers
265 this.userActions.push([
267 label: this.i18n('Mute this account'),
268 isDisplayed: ({ account }) => account.mutedByUser === false,
269 handler: ({ account }) => this.blockAccountByUser(account)
272 label: this.i18n('Unmute this account'),
273 isDisplayed: ({ account }) => account.mutedByUser === true,
274 handler: ({ account }) => this.unblockAccountByUser(account)
277 label: this.i18n('Mute the instance'),
278 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
279 handler: ({ account }) => this.blockServerByUser(account.host)
282 label: this.i18n('Unmute the instance'),
283 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
284 handler: ({ account }) => this.unblockServerByUser(account.host)
288 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
291 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
292 instanceActions = instanceActions.concat([
294 label: this.i18n('Mute this account by your instance'),
295 isDisplayed: ({ account }) => account.mutedByInstance === false,
296 handler: ({ account }) => this.blockAccountByInstance(account)
299 label: this.i18n('Unmute this account by your instance'),
300 isDisplayed: ({ account }) => account.mutedByInstance === true,
301 handler: ({ account }) => this.unblockAccountByInstance(account)
307 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
308 instanceActions = instanceActions.concat([
310 label: this.i18n('Mute the instance by your instance'),
311 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
312 handler: ({ account }) => this.blockServerByInstance(account.host)
315 label: this.i18n('Unmute the instance by your instance'),
316 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
317 handler: ({ account }) => this.unblockServerByInstance(account.host)
322 if (instanceActions.length !== 0) {
323 this.userActions.push(instanceActions)