]>
Commit | Line | Data |
---|---|---|
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' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-user-ban-modal', | |
15 | templateUrl: './user-ban-modal.component.html', | |
16 | styleUrls: [ './user-ban-modal.component.scss' ] | |
17 | }) | |
18 | export class UserBanModalComponent extends FormReactive implements OnInit { | |
19 | @ViewChild('modal', { static: true }) modal: NgbModal | |
20 | @Output() userBanned = new EventEmitter<User | User[]>() | |
21 | ||
22 | private usersToBan: User | User[] | |
23 | private openedModal: NgbModalRef | |
24 | modalMessage = '' | |
25 | ||
26 | constructor ( | |
27 | protected formValidatorService: FormValidatorService, | |
28 | private modalService: NgbModal, | |
29 | private notifier: Notifier, | |
30 | private userAdminService: UserAdminService, | |
31 | private blocklistService: BlocklistService | |
32 | ) { | |
33 | super() | |
34 | } | |
35 | ||
36 | ngOnInit () { | |
37 | this.buildForm({ | |
38 | reason: USER_BAN_REASON_VALIDATOR, | |
39 | mute: null | |
40 | }) | |
41 | } | |
42 | ||
43 | openModal (user: User | User[]) { | |
44 | this.usersToBan = user | |
45 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
46 | } | |
47 | ||
48 | hide () { | |
49 | this.usersToBan = undefined | |
50 | this.openedModal.close() | |
51 | } | |
52 | ||
53 | banUser () { | |
54 | const reason = this.form.value['reason'] || undefined | |
55 | const mute = this.form.value['mute'] | |
56 | ||
57 | const observables = [ | |
58 | this.userAdminService.banUsers(this.usersToBan, reason) | |
59 | ] | |
60 | ||
61 | if (mute) observables.push(this.muteAccounts()) | |
62 | ||
63 | forkJoin(observables) | |
64 | .subscribe({ | |
65 | next: () => { | |
66 | const message = Array.isArray(this.usersToBan) | |
67 | ? $localize`${this.usersToBan.length} users banned.` | |
68 | : $localize`User ${this.usersToBan.username} banned.` | |
69 | ||
70 | this.notifier.success(message) | |
71 | ||
72 | this.userBanned.emit(this.usersToBan) | |
73 | ||
74 | this.hide() | |
75 | }, | |
76 | ||
77 | error: err => this.notifier.error(err.message) | |
78 | }) | |
79 | } | |
80 | ||
81 | getModalTitle () { | |
82 | if (Array.isArray(this.usersToBan)) return $localize`Ban ${this.usersToBan.length} users` | |
83 | ||
84 | return $localize`Ban "${this.usersToBan.username}"` | |
85 | } | |
86 | ||
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) | |
91 | ||
92 | return this.blocklistService.blockAccountByInstance(accounts) | |
93 | } | |
94 | } |