1 import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
5 import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
6 import { UserService } from '@app/shared/users'
7 import { AuthService, ConfirmService } from '@app/core'
8 import { User, UserRight } from '../../../../../shared/models/users'
9 import { Account } from '@app/shared/account/account.model'
10 import { BlocklistService } from '@app/shared/blocklist'
13 selector: 'my-user-moderation-dropdown',
14 templateUrl: './user-moderation-dropdown.component.html',
15 styleUrls: [ './user-moderation-dropdown.component.scss' ]
17 export class UserModerationDropdownComponent implements OnChanges {
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
21 @Input() account: Account
23 @Input() buttonSize: 'normal' | 'small' = 'normal'
24 @Input() placement = 'left'
26 @Output() userChanged = new EventEmitter()
27 @Output() userDeleted = new EventEmitter()
29 userActions: DropdownAction<{ user: User, account: Account }>[] = []
32 private authService: AuthService,
33 private notificationsService: NotificationsService,
34 private confirmService: ConfirmService,
35 private userService: UserService,
36 private blocklistService: BlocklistService,
44 openBanUserModal (user: User) {
45 if (user.username === 'root') {
46 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
50 this.userBanModal.openModal(user)
54 this.userChanged.emit()
57 async unbanUser (user: User) {
58 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
59 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
60 if (res === false) return
62 this.userService.unbanUsers(user)
65 this.notificationsService.success(
67 this.i18n('User {{username}} unbanned.', { username: user.username })
70 this.userChanged.emit()
73 err => this.notificationsService.error(this.i18n('Error'), err.message)
77 async removeUser (user: User) {
78 if (user.username === 'root') {
79 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
83 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
84 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
85 if (res === false) return
87 this.userService.removeUser(user).subscribe(
89 this.notificationsService.success(
91 this.i18n('User {{username}} deleted.', { username: user.username })
93 this.userDeleted.emit()
96 err => this.notificationsService.error(this.i18n('Error'), err.message)
100 blockAccountByUser (account: Account) {
101 this.blocklistService.blockAccountByUser(account)
104 this.notificationsService.success(
105 this.i18n('Success'),
106 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
109 this.account.mutedByUser = true
110 this.userChanged.emit()
113 err => this.notificationsService.error(this.i18n('Error'), err.message)
117 unblockAccountByUser (account: Account) {
118 this.blocklistService.unblockAccountByUser(account)
121 this.notificationsService.success(
122 this.i18n('Success'),
123 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
126 this.account.mutedByUser = false
127 this.userChanged.emit()
130 err => this.notificationsService.error(this.i18n('Error'), err.message)
134 blockServerByUser (host: string) {
135 this.blocklistService.blockServerByUser(host)
138 this.notificationsService.success(
139 this.i18n('Success'),
140 this.i18n('Instance {{host}} muted.', { host })
143 this.account.mutedServerByUser = true
144 this.userChanged.emit()
147 err => this.notificationsService.error(this.i18n('Error'), err.message)
151 unblockServerByUser (host: string) {
152 this.blocklistService.unblockServerByUser(host)
155 this.notificationsService.success(
156 this.i18n('Success'),
157 this.i18n('Instance {{host}} unmuted.', { host })
160 this.account.mutedServerByUser = false
161 this.userChanged.emit()
164 err => this.notificationsService.error(this.i18n('Error'), err.message)
168 blockAccountByInstance (account: Account) {
169 this.blocklistService.blockAccountByInstance(account)
172 this.notificationsService.success(
173 this.i18n('Success'),
174 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
177 this.account.mutedByInstance = true
178 this.userChanged.emit()
181 err => this.notificationsService.error(this.i18n('Error'), err.message)
185 unblockAccountByInstance (account: Account) {
186 this.blocklistService.unblockAccountByInstance(account)
189 this.notificationsService.success(
190 this.i18n('Success'),
191 this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })
194 this.account.mutedByInstance = false
195 this.userChanged.emit()
198 err => this.notificationsService.error(this.i18n('Error'), err.message)
202 blockServerByInstance (host: string) {
203 this.blocklistService.blockServerByInstance(host)
206 this.notificationsService.success(
207 this.i18n('Success'),
208 this.i18n('Instance {{host}} muted by the instance.', { host })
211 this.account.mutedServerByInstance = true
212 this.userChanged.emit()
215 err => this.notificationsService.error(this.i18n('Error'), err.message)
219 unblockServerByInstance (host: string) {
220 this.blocklistService.unblockServerByInstance(host)
223 this.notificationsService.success(
224 this.i18n('Success'),
225 this.i18n('Instance {{host}} unmuted by the instance.', { host })
228 this.account.mutedServerByInstance = false
229 this.userChanged.emit()
232 err => this.notificationsService.error(this.i18n('Error'), err.message)
236 getRouterUserEditLink (user: User) {
237 return [ '/admin', 'users', 'update', user.id ]
240 private buildActions () {
241 this.userActions = []
243 if (this.authService.isLoggedIn()) {
244 const authUser = this.authService.getUser()
246 if (this.user && authUser.id === this.user.id) return
248 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
249 this.userActions = this.userActions.concat([
251 label: this.i18n('Edit'),
252 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
255 label: this.i18n('Delete'),
256 handler: ({ user }) => this.removeUser(user)
259 label: this.i18n('Ban'),
260 handler: ({ user }: { user: User }) => this.openBanUserModal(user),
261 isDisplayed: ({ user }: { user: User }) => !user.blocked
264 label: this.i18n('Unban'),
265 handler: ({ user }: { user: User }) => this.unbanUser(user),
266 isDisplayed: ({ user }: { user: User }) => user.blocked
271 // Actions on accounts/servers
274 this.userActions = this.userActions.concat([
276 label: this.i18n('Mute this account'),
277 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === false,
278 handler: ({ account }: { account: Account }) => this.blockAccountByUser(account)
281 label: this.i18n('Unmute this account'),
282 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === true,
283 handler: ({ account }: { account: Account }) => this.unblockAccountByUser(account)
286 label: this.i18n('Mute the instance'),
287 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
288 handler: ({ account }: { account: Account }) => this.blockServerByUser(account.host)
291 label: this.i18n('Unmute the instance'),
292 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
293 handler: ({ account }: { account: Account }) => this.unblockServerByUser(account.host)
298 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
299 this.userActions = this.userActions.concat([
301 label: this.i18n('Mute this account by your instance'),
302 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === false,
303 handler: ({ account }: { account: Account }) => this.blockAccountByInstance(account)
306 label: this.i18n('Unmute this account by your instance'),
307 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === true,
308 handler: ({ account }: { account: Account }) => this.unblockAccountByInstance(account)
314 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
315 this.userActions = this.userActions.concat([
317 label: this.i18n('Mute the instance by your instance'),
318 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
319 handler: ({ account }: { account: Account }) => this.blockServerByInstance(account.host)
322 label: this.i18n('Unmute the instance by your instance'),
323 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
324 handler: ({ account }: { account: Account }) => this.unblockServerByInstance(account.host)