]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/user-ban-modal.component.ts
Refactor row selection reset
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-ban-modal.component.ts
CommitLineData
a282e4d8 1import { forkJoin } from 'rxjs'
141b177d 2import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
d92d070c 3import { Notifier } from '@app/core'
eaa52952 4import { prepareIcu } from '@app/helpers'
5c5bcea2 5import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
141b177d
C
6import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
7import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
67ed6552 8import { User } from '@shared/models'
7ed1edbb 9import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
5a8de57d 10import { Account } from '../shared-main'
d92d070c 11import { UserAdminService } from '../shared-users'
5a8de57d 12import { BlocklistService } from './blocklist.service'
141b177d
C
13
14@Component({
15 selector: 'my-user-ban-modal',
16 templateUrl: './user-ban-modal.component.html',
17 styleUrls: [ './user-ban-modal.component.scss' ]
18})
19export class UserBanModalComponent extends FormReactive implements OnInit {
f36da21e 20 @ViewChild('modal', { static: true }) modal: NgbModal
791645e6 21 @Output() userBanned = new EventEmitter<User | User[]>()
141b177d 22
791645e6 23 private usersToBan: User | User[]
141b177d 24 private openedModal: NgbModalRef
884b1777 25 modalMessage = ''
141b177d
C
26
27 constructor (
5c5bcea2 28 protected formReactiveService: FormReactiveService,
141b177d 29 private modalService: NgbModal,
f8b2c1b4 30 private notifier: Notifier,
5a8de57d
P
31 private userAdminService: UserAdminService,
32 private blocklistService: BlocklistService
141b177d
C
33 ) {
34 super()
35 }
36
37 ngOnInit () {
38 this.buildForm({
5a8de57d
P
39 reason: USER_BAN_REASON_VALIDATOR,
40 mute: null
141b177d
C
41 })
42 }
43
791645e6
C
44 openModal (user: User | User[]) {
45 this.usersToBan = user
24e7916c 46 this.openedModal = this.modalService.open(this.modal, { centered: true })
141b177d
C
47 }
48
457bb213 49 hide () {
791645e6 50 this.usersToBan = undefined
141b177d
C
51 this.openedModal.close()
52 }
53
98ab5dc8 54 banUser () {
141b177d 55 const reason = this.form.value['reason'] || undefined
5a8de57d 56 const mute = this.form.value['mute']
141b177d 57
a282e4d8
C
58 const observables = [
59 this.userAdminService.banUsers(this.usersToBan, reason)
60 ]
61
62 if (mute) observables.push(this.muteAccounts())
63
64 forkJoin(observables)
1378c0d3
C
65 .subscribe({
66 next: () => {
eaa52952
C
67 let message: string
68
69 if (Array.isArray(this.usersToBan)) {
baf99fcc 70 message = prepareIcu($localize`{count, plural, =1 {1 user banned.} other {{count} users banned.}}`)(
eaa52952
C
71 { count: this.usersToBan.length },
72 $localize`${this.usersToBan.length} users banned.`
73 )
74 } else {
75 message = $localize`User ${this.usersToBan.username} banned.`
76 }
141b177d 77
f8b2c1b4 78 this.notifier.success(message)
791645e6
C
79
80 this.userBanned.emit(this.usersToBan)
5a8de57d 81
457bb213 82 this.hide()
141b177d
C
83 },
84
1378c0d3
C
85 error: err => this.notifier.error(err.message)
86 })
141b177d
C
87 }
88
a282e4d8 89 getModalTitle () {
eaa52952
C
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`
94 )
95 }
a282e4d8
C
96
97 return $localize`Ban "${this.usersToBan.username}"`
98 }
99
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)
104
105 return this.blocklistService.blockAccountByInstance(accounts)
106 }
141b177d 107}