]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/user-ban-modal.component.ts
b3f03990f66a1095ea8f26f89360270cd685609a
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-ban-modal.component.ts
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'
11
12 @Component({
13 selector: 'my-user-ban-modal',
14 templateUrl: './user-ban-modal.component.html',
15 styleUrls: [ './user-ban-modal.component.scss' ]
16 })
17 export class UserBanModalComponent extends FormReactive implements OnInit {
18 @ViewChild('modal', { static: true }) modal: NgbModal
19 @Output() userBanned = new EventEmitter<User | User[]>()
20
21 private usersToBan: User | User[]
22 private openedModal: NgbModalRef
23 modalMessage = ''
24
25 constructor (
26 protected formValidatorService: FormValidatorService,
27 private modalService: NgbModal,
28 private notifier: Notifier,
29 private userAdminService: UserAdminService,
30 private blocklistService: BlocklistService
31 ) {
32 super()
33 }
34
35 ngOnInit () {
36 this.buildForm({
37 reason: USER_BAN_REASON_VALIDATOR,
38 mute: null
39 })
40 }
41
42 openModal (user: User | User[]) {
43 this.usersToBan = user
44 this.openedModal = this.modalService.open(this.modal, { centered: true })
45
46 const isSingleUser = !(Array.isArray(this.usersToBan) && this.usersToBan.length > 1)
47 this.modalMessage = isSingleUser ? $localize`Ban this user` : $localize`Ban these users`
48 }
49
50 hide () {
51 this.usersToBan = undefined
52 this.openedModal.close()
53 }
54
55 banUser () {
56 const reason = this.form.value['reason'] || undefined
57 const mute = this.form.value['mute']
58
59 this.userAdminService.banUsers(this.usersToBan, reason)
60 .subscribe({
61 next: () => {
62 const message = Array.isArray(this.usersToBan)
63 ? $localize`${this.usersToBan.length} users banned.`
64 : $localize`User ${this.usersToBan.username} banned.`
65
66 this.notifier.success(message)
67
68 this.userBanned.emit(this.usersToBan)
69
70 if (mute) {
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)
75 .subscribe({
76 next: () => {
77 this.notifier.success($localize`Account ${user.username} muted by the instance.`)
78 account.mutedByInstance = true
79 },
80
81 error: err => this.notifier.error(err.message)
82 })
83 })
84 }
85
86 this.hide()
87 },
88
89 error: err => this.notifier.error(err.message)
90 })
91 }
92
93 }