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, ServerService } 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 serverService: ServerService,
36 private userService: UserService,
37 private blocklistService: BlocklistService,
41 get requiresEmailVerification () {
42 return this.serverService.getConfig().signup.requiresEmailVerification
49 openBanUserModal (user: User) {
50 if (user.username === 'root') {
51 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
55 this.userBanModal.openModal(user)
59 this.userChanged.emit()
62 async unbanUser (user: User) {
63 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
64 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
65 if (res === false) return
67 this.userService.unbanUsers(user)
70 this.notificationsService.success(
72 this.i18n('User {{username}} unbanned.', { username: user.username })
75 this.userChanged.emit()
78 err => this.notificationsService.error(this.i18n('Error'), err.message)
82 async removeUser (user: User) {
83 if (user.username === 'root') {
84 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
88 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
89 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
90 if (res === false) return
92 this.userService.removeUser(user).subscribe(
94 this.notificationsService.success(
96 this.i18n('User {{username}} deleted.', { username: user.username })
98 this.userDeleted.emit()
101 err => this.notificationsService.error(this.i18n('Error'), err.message)
105 setEmailAsVerified (user: User) {
106 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
108 this.notificationsService.success(
109 this.i18n('Success'),
110 this.i18n('User {{username}} email set as verified', { username: user.username })
114 err => this.notificationsService.error(this.i18n('Error'), err.message)
118 blockAccountByUser (account: Account) {
119 this.blocklistService.blockAccountByUser(account)
122 this.notificationsService.success(
123 this.i18n('Success'),
124 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
127 this.account.mutedByUser = true
128 this.userChanged.emit()
131 err => this.notificationsService.error(this.i18n('Error'), err.message)
135 unblockAccountByUser (account: Account) {
136 this.blocklistService.unblockAccountByUser(account)
139 this.notificationsService.success(
140 this.i18n('Success'),
141 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
144 this.account.mutedByUser = false
145 this.userChanged.emit()
148 err => this.notificationsService.error(this.i18n('Error'), err.message)
152 blockServerByUser (host: string) {
153 this.blocklistService.blockServerByUser(host)
156 this.notificationsService.success(
157 this.i18n('Success'),
158 this.i18n('Instance {{host}} muted.', { host })
161 this.account.mutedServerByUser = true
162 this.userChanged.emit()
165 err => this.notificationsService.error(this.i18n('Error'), err.message)
169 unblockServerByUser (host: string) {
170 this.blocklistService.unblockServerByUser(host)
173 this.notificationsService.success(
174 this.i18n('Success'),
175 this.i18n('Instance {{host}} unmuted.', { host })
178 this.account.mutedServerByUser = false
179 this.userChanged.emit()
182 err => this.notificationsService.error(this.i18n('Error'), err.message)
186 blockAccountByInstance (account: Account) {
187 this.blocklistService.blockAccountByInstance(account)
190 this.notificationsService.success(
191 this.i18n('Success'),
192 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
195 this.account.mutedByInstance = true
196 this.userChanged.emit()
199 err => this.notificationsService.error(this.i18n('Error'), err.message)
203 unblockAccountByInstance (account: Account) {
204 this.blocklistService.unblockAccountByInstance(account)
207 this.notificationsService.success(
208 this.i18n('Success'),
209 this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })
212 this.account.mutedByInstance = false
213 this.userChanged.emit()
216 err => this.notificationsService.error(this.i18n('Error'), err.message)
220 blockServerByInstance (host: string) {
221 this.blocklistService.blockServerByInstance(host)
224 this.notificationsService.success(
225 this.i18n('Success'),
226 this.i18n('Instance {{host}} muted by the instance.', { host })
229 this.account.mutedServerByInstance = true
230 this.userChanged.emit()
233 err => this.notificationsService.error(this.i18n('Error'), err.message)
237 unblockServerByInstance (host: string) {
238 this.blocklistService.unblockServerByInstance(host)
241 this.notificationsService.success(
242 this.i18n('Success'),
243 this.i18n('Instance {{host}} unmuted by the instance.', { host })
246 this.account.mutedServerByInstance = false
247 this.userChanged.emit()
250 err => this.notificationsService.error(this.i18n('Error'), err.message)
254 getRouterUserEditLink (user: User) {
255 return [ '/admin', 'users', 'update', user.id ]
258 private buildActions () {
259 this.userActions = []
261 if (this.authService.isLoggedIn()) {
262 const authUser = this.authService.getUser()
264 if (this.user && authUser.id === this.user.id) return
266 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
267 this.userActions = this.userActions.concat([
269 label: this.i18n('Edit'),
270 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
273 label: this.i18n('Delete'),
274 handler: ({ user }) => this.removeUser(user)
277 label: this.i18n('Ban'),
278 handler: ({ user }: { user: User }) => this.openBanUserModal(user),
279 isDisplayed: ({ user }: { user: User }) => !user.blocked
282 label: this.i18n('Unban'),
283 handler: ({ user }: { user: User }) => this.unbanUser(user),
284 isDisplayed: ({ user }: { user: User }) => user.blocked
287 label: this.i18n('Set Email as Verified'),
288 handler: ({ user }: { user: User }) => this.setEmailAsVerified(user),
289 isDisplayed: ({ user }: { user: User }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
294 // Actions on accounts/servers
297 this.userActions = this.userActions.concat([
299 label: this.i18n('Mute this account'),
300 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === false,
301 handler: ({ account }: { account: Account }) => this.blockAccountByUser(account)
304 label: this.i18n('Unmute this account'),
305 isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === true,
306 handler: ({ account }: { account: Account }) => this.unblockAccountByUser(account)
309 label: this.i18n('Mute the instance'),
310 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
311 handler: ({ account }: { account: Account }) => this.blockServerByUser(account.host)
314 label: this.i18n('Unmute the instance'),
315 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
316 handler: ({ account }: { account: Account }) => this.unblockServerByUser(account.host)
321 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
322 this.userActions = this.userActions.concat([
324 label: this.i18n('Mute this account by your instance'),
325 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === false,
326 handler: ({ account }: { account: Account }) => this.blockAccountByInstance(account)
329 label: this.i18n('Unmute this account by your instance'),
330 isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === true,
331 handler: ({ account }: { account: Account }) => this.unblockAccountByInstance(account)
337 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
338 this.userActions = this.userActions.concat([
340 label: this.i18n('Mute the instance by your instance'),
341 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false,
342 handler: ({ account }: { account: Account }) => this.blockServerByInstance(account.host)
345 label: this.i18n('Unmute the instance by your instance'),
346 isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true,
347 handler: ({ account }: { account: Account }) => this.unblockServerByInstance(account.host)