]>
Commit | Line | Data |
---|---|---|
a282e4d8 | 1 | import { forkJoin } from 'rxjs' |
141b177d | 2 | import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
d92d070c | 3 | import { Notifier } from '@app/core' |
7ed1edbb | 4 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
141b177d C |
5 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
6 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
67ed6552 | 7 | import { User } from '@shared/models' |
7ed1edbb | 8 | import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators' |
5a8de57d | 9 | import { Account } from '../shared-main' |
d92d070c | 10 | import { UserAdminService } from '../shared-users' |
5a8de57d | 11 | import { BlocklistService } from './blocklist.service' |
141b177d C |
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 { | |
f36da21e | 19 | @ViewChild('modal', { static: true }) modal: NgbModal |
791645e6 | 20 | @Output() userBanned = new EventEmitter<User | User[]>() |
141b177d | 21 | |
791645e6 | 22 | private usersToBan: User | User[] |
141b177d | 23 | private openedModal: NgbModalRef |
884b1777 | 24 | modalMessage = '' |
141b177d C |
25 | |
26 | constructor ( | |
27 | protected formValidatorService: FormValidatorService, | |
28 | private modalService: NgbModal, | |
f8b2c1b4 | 29 | private notifier: Notifier, |
5a8de57d P |
30 | private userAdminService: UserAdminService, |
31 | private blocklistService: BlocklistService | |
141b177d C |
32 | ) { |
33 | super() | |
34 | } | |
35 | ||
36 | ngOnInit () { | |
37 | this.buildForm({ | |
5a8de57d P |
38 | reason: USER_BAN_REASON_VALIDATOR, |
39 | mute: null | |
141b177d C |
40 | }) |
41 | } | |
42 | ||
791645e6 C |
43 | openModal (user: User | User[]) { |
44 | this.usersToBan = user | |
24e7916c | 45 | this.openedModal = this.modalService.open(this.modal, { centered: true }) |
141b177d C |
46 | } |
47 | ||
457bb213 | 48 | hide () { |
791645e6 | 49 | this.usersToBan = undefined |
141b177d C |
50 | this.openedModal.close() |
51 | } | |
52 | ||
98ab5dc8 | 53 | banUser () { |
141b177d | 54 | const reason = this.form.value['reason'] || undefined |
5a8de57d | 55 | const mute = this.form.value['mute'] |
141b177d | 56 | |
a282e4d8 C |
57 | const observables = [ |
58 | this.userAdminService.banUsers(this.usersToBan, reason) | |
59 | ] | |
60 | ||
61 | if (mute) observables.push(this.muteAccounts()) | |
62 | ||
63 | forkJoin(observables) | |
1378c0d3 C |
64 | .subscribe({ |
65 | next: () => { | |
791645e6 | 66 | const message = Array.isArray(this.usersToBan) |
66357162 C |
67 | ? $localize`${this.usersToBan.length} users banned.` |
68 | : $localize`User ${this.usersToBan.username} banned.` | |
141b177d | 69 | |
f8b2c1b4 | 70 | this.notifier.success(message) |
791645e6 C |
71 | |
72 | this.userBanned.emit(this.usersToBan) | |
5a8de57d | 73 | |
457bb213 | 74 | this.hide() |
141b177d C |
75 | }, |
76 | ||
1378c0d3 C |
77 | error: err => this.notifier.error(err.message) |
78 | }) | |
141b177d C |
79 | } |
80 | ||
a282e4d8 C |
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 | } | |
141b177d | 94 | } |