1 import { forkJoin } from 'rxjs'
2 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { Notifier } from '@app/core'
4 import { prepareIcu } from '@app/helpers'
5 import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
6 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
7 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
8 import { User } from '@shared/models'
9 import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
10 import { Account } from '../shared-main'
11 import { UserAdminService } from '../shared-users'
12 import { BlocklistService } from './blocklist.service'
15 selector: 'my-user-ban-modal',
16 templateUrl: './user-ban-modal.component.html',
17 styleUrls: [ './user-ban-modal.component.scss' ]
19 export class UserBanModalComponent extends FormReactive implements OnInit {
20 @ViewChild('modal', { static: true }) modal: NgbModal
21 @Output() userBanned = new EventEmitter<User | User[]>()
23 private usersToBan: User | User[]
24 private openedModal: NgbModalRef
28 protected formReactiveService: FormReactiveService,
29 private modalService: NgbModal,
30 private notifier: Notifier,
31 private userAdminService: UserAdminService,
32 private blocklistService: BlocklistService
39 reason: USER_BAN_REASON_VALIDATOR,
44 openModal (user: User | User[]) {
45 this.usersToBan = user
46 this.openedModal = this.modalService.open(this.modal, { centered: true })
50 this.usersToBan = undefined
51 this.openedModal.close()
55 const reason = this.form.value['reason'] || undefined
56 const mute = this.form.value['mute']
59 this.userAdminService.banUsers(this.usersToBan, reason)
62 if (mute) observables.push(this.muteAccounts())
69 if (Array.isArray(this.usersToBan)) {
70 message = prepareIcu($localize`{count, plural, =1 {1 user banned.} other {{count} users banned.}}`)(
71 { count: this.usersToBan.length },
72 $localize`${this.usersToBan.length} users banned.`
75 message = $localize`User ${this.usersToBan.username} banned.`
78 this.notifier.success(message)
80 this.userBanned.emit(this.usersToBan)
85 error: err => this.notifier.error(err.message)
90 if (Array.isArray(this.usersToBan)) {
91 return prepareIcu($localize`Ban {count, plural, =1 {1 user} other {{count} users}}`)(
92 { count: this.usersToBan.length },
93 $localize`Ban ${this.usersToBan.length} users`
97 return $localize`Ban "${this.usersToBan.username}"`
100 private muteAccounts () {
101 const accounts = Array.isArray(this.usersToBan)
102 ? this.usersToBan.map(u => new Account(u.account))
103 : new Account(this.usersToBan.account)
105 return this.blocklistService.blockAccountByInstance(accounts)