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 })
113 this.userChanged.emit()
116 err => this.notificationsService.error(this.i18n('Error'), err.message)
120 blockAccountByUser (account: Account) {
121 this.blocklistService.blockAccountByUser(account)
124 this.notificationsService.success(
125 this.i18n('Success'),
126 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
129 this.account.mutedByUser = true
130 this.userChanged.emit()
133 err => this.notificationsService.error(this.i18n('Error'), err.message)
137 unblockAccountByUser (account: Account) {
138 this.blocklistService.unblockAccountByUser(account)
141 this.notificationsService.success(
142 this.i18n('Success'),
143 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
146 this.account.mutedByUser = false
147 this.userChanged.emit()
150 err => this.notificationsService.error(this.i18n('Error'), err.message)
154 blockServerByUser (host: string) {
155 this.blocklistService.blockServerByUser(host)
158 this.notificationsService.success(
159 this.i18n('Success'),
160 this.i18n('Instance {{host}} muted.', { host })
163 this.account.mutedServerByUser = true
164 this.userChanged.emit()
167 err => this.notificationsService.error(this.i18n('Error'), err.message)
171 unblockServerByUser (host: string) {
172 this.blocklistService.unblockServerByUser(host)
175 this.notificationsService.success(
176 this.i18n('Success'),
177 this.i18n('Instance {{host}} unmuted.', { host })
180 this.account.mutedServerByUser = false
181 this.userChanged.emit()
184 err => this.notificationsService.error(this.i18n('Error'), err.message)
188 blockAccountByInstance (account: Account) {
189 this.blocklistService.blockAccountByInstance(account)
192 this.notificationsService.success(
193 this.i18n('Success'),
194 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
197 this.account.mutedByInstance = true
198 this.userChanged.emit()
201 err => this.notificationsService.error(this.i18n('Error'), err.message)
205 unblockAccountByInstance (account: Account) {
206 this.blocklistService.unblockAccountByInstance(account)
209 this.notificationsService.success(
210 this.i18n('Success'),
211 this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })
214 this.account.mutedByInstance = false
215 this.userChanged.emit()
218 err => this.notificationsService.error(this.i18n('Error'), err.message)
222 blockServerByInstance (host: string) {
223 this.blocklistService.blockServerByInstance(host)
226 this.notificationsService.success(
227 this.i18n('Success'),
228 this.i18n('Instance {{host}} muted by the instance.', { host })
231 this.account.mutedServerByInstance = true
232 this.userChanged.emit()
235 err => this.notificationsService.error(this.i18n('Error'), err.message)
239 unblockServerByInstance (host: string) {
240 this.blocklistService.unblockServerByInstance(host)
243 this.notificationsService.success(
244 this.i18n('Success'),
245 this.i18n('Instance {{host}} unmuted by the instance.', { host })
248 this.account.mutedServerByInstance = false
249 this.userChanged.emit()
252 err => this.notificationsService.error(this.i18n('Error'), err.message)
256 getRouterUserEditLink (user: User) {
257 return [ '/admin', 'users', 'update', user.id ]
260 private buildActions () {
261 this.userActions = []
263 if (this.authService.isLoggedIn()) {
264 const authUser = this.authService.getUser()
266 if (this.user && authUser.id === this.user.id) return
268 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
269 this.userActions.push([
271 label: this.i18n('Edit'),
272 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
275 label: this.i18n('Delete'),
276 handler: ({ user }) => this.removeUser(user)
279 label: this.i18n('Ban'),
280 handler: ({ user }) => this.openBanUserModal(user),
281 isDisplayed: ({ user }) => !user.blocked
284 label: this.i18n('Unban'),
285 handler: ({ user }) => this.unbanUser(user),
286 isDisplayed: ({ user }) => user.blocked
289 label: this.i18n('Set Email as Verified'),
290 handler: ({ user }) => this.setEmailAsVerified(user),
291 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
296 // Actions on accounts/servers
299 this.userActions.push([
301 label: this.i18n('Mute this account'),
302 isDisplayed: ({ account }) => account.mutedByUser === false,
303 handler: ({ account }) => this.blockAccountByUser(account)
306 label: this.i18n('Unmute this account'),
307 isDisplayed: ({ account }) => account.mutedByUser === true,
308 handler: ({ account }) => this.unblockAccountByUser(account)
311 label: this.i18n('Mute the instance'),
312 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
313 handler: ({ account }) => this.blockServerByUser(account.host)
316 label: this.i18n('Unmute the instance'),
317 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
318 handler: ({ account }) => this.unblockServerByUser(account.host)
322 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
325 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
326 instanceActions = instanceActions.concat([
328 label: this.i18n('Mute this account by your instance'),
329 isDisplayed: ({ account }) => account.mutedByInstance === false,
330 handler: ({ account }) => this.blockAccountByInstance(account)
333 label: this.i18n('Unmute this account by your instance'),
334 isDisplayed: ({ account }) => account.mutedByInstance === true,
335 handler: ({ account }) => this.unblockAccountByInstance(account)
341 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
342 instanceActions = instanceActions.concat([
344 label: this.i18n('Mute the instance by your instance'),
345 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
346 handler: ({ account }) => this.blockServerByInstance(account.host)
349 label: this.i18n('Unmute the instance by your instance'),
350 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
351 handler: ({ account }) => this.unblockServerByInstance(account.host)
356 if (instanceActions.length !== 0) {
357 this.userActions.push(instanceActions)