1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6 import { User } from '@shared/models'
7 import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
8 import { Account } from '../shared-main'
9 import { UserAdminService } from '../shared-users'
10 import { BlocklistService } from './blocklist.service'
13 selector: 'my-user-ban-modal',
14 templateUrl: './user-ban-modal.component.html',
15 styleUrls: [ './user-ban-modal.component.scss' ]
17 export class UserBanModalComponent extends FormReactive implements OnInit {
18 @ViewChild('modal', { static: true }) modal: NgbModal
19 @Output() userBanned = new EventEmitter<User | User[]>()
21 private usersToBan: User | User[]
22 private openedModal: NgbModalRef
26 protected formValidatorService: FormValidatorService,
27 private modalService: NgbModal,
28 private notifier: Notifier,
29 private userAdminService: UserAdminService,
30 private blocklistService: BlocklistService
37 reason: USER_BAN_REASON_VALIDATOR,
42 openModal (user: User | User[]) {
43 this.usersToBan = user
44 this.openedModal = this.modalService.open(this.modal, { centered: true })
46 const isSingleUser = !(Array.isArray(this.usersToBan) && this.usersToBan.length > 1)
47 this.modalMessage = isSingleUser ? $localize`Ban this user` : $localize`Ban these users`
51 this.usersToBan = undefined
52 this.openedModal.close()
56 const reason = this.form.value['reason'] || undefined
57 const mute = this.form.value['mute']
59 this.userAdminService.banUsers(this.usersToBan, reason)
62 const message = Array.isArray(this.usersToBan)
63 ? $localize`${this.usersToBan.length} users banned.`
64 : $localize`User ${this.usersToBan.username} banned.`
66 this.notifier.success(message)
68 this.userBanned.emit(this.usersToBan)
71 const users = Array.isArray(this.usersToBan) ? this.usersToBan : [ this.usersToBan ]
72 users.forEach(user => {
73 const account = new Account(user.account)
74 this.blocklistService.blockAccountByInstance(account)
77 this.notifier.success($localize`Account ${user.username} muted by the instance.`)
78 account.mutedByInstance = true
81 error: err => this.notifier.error(err.message)
89 error: err => this.notifier.error(err.message)