1 import { forkJoin } from 'rxjs'
2 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { Notifier } from '@app/core'
4 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
5 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
6 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
7 import { User } from '@shared/models'
8 import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
9 import { Account } from '../shared-main'
10 import { UserAdminService } from '../shared-users'
11 import { BlocklistService } from './blocklist.service'
14 selector: 'my-user-ban-modal',
15 templateUrl: './user-ban-modal.component.html',
16 styleUrls: [ './user-ban-modal.component.scss' ]
18 export class UserBanModalComponent extends FormReactive implements OnInit {
19 @ViewChild('modal', { static: true }) modal: NgbModal
20 @Output() userBanned = new EventEmitter<User | User[]>()
22 private usersToBan: User | User[]
23 private openedModal: NgbModalRef
27 protected formValidatorService: FormValidatorService,
28 private modalService: NgbModal,
29 private notifier: Notifier,
30 private userAdminService: UserAdminService,
31 private blocklistService: BlocklistService
38 reason: USER_BAN_REASON_VALIDATOR,
43 openModal (user: User | User[]) {
44 this.usersToBan = user
45 this.openedModal = this.modalService.open(this.modal, { centered: true })
49 this.usersToBan = undefined
50 this.openedModal.close()
54 const reason = this.form.value['reason'] || undefined
55 const mute = this.form.value['mute']
58 this.userAdminService.banUsers(this.usersToBan, reason)
61 if (mute) observables.push(this.muteAccounts())
66 const message = Array.isArray(this.usersToBan)
67 ? $localize`${this.usersToBan.length} users banned.`
68 : $localize`User ${this.usersToBan.username} banned.`
70 this.notifier.success(message)
72 this.userBanned.emit(this.usersToBan)
77 error: err => this.notifier.error(err.message)
82 if (Array.isArray(this.usersToBan)) return $localize`Ban ${this.usersToBan.length} users`
84 return $localize`Ban "${this.usersToBan.username}"`
87 private muteAccounts () {
88 const accounts = Array.isArray(this.usersToBan)
89 ? this.usersToBan.map(u => new Account(u.account))
90 : new Account(this.usersToBan.account)
92 return this.blocklistService.blockAccountByInstance(accounts)