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