]>
Commit | Line | Data |
---|---|---|
1 | import { mapValues, pickBy } from 'lodash-es' | |
2 | import { Component, OnInit, ViewChild } from '@angular/core' | |
3 | import { Notifier } from '@app/core' | |
4 | import { ABUSE_REASON_VALIDATOR } from '@app/shared/form-validators/abuse-validators' | |
5 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' | |
6 | import { Account } from '@app/shared/shared-main' | |
7 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
8 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
9 | import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse' | |
10 | import { AbusePredefinedReasonsString } from '@shared/models' | |
11 | import { AbuseService } from '../abuse.service' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-account-report', | |
15 | templateUrl: './report.component.html', | |
16 | styleUrls: [ './report.component.scss' ] | |
17 | }) | |
18 | export class AccountReportComponent extends FormReactive implements OnInit { | |
19 | @ViewChild('modal', { static: true }) modal: NgbModal | |
20 | ||
21 | error: string = null | |
22 | predefinedReasons: { id: AbusePredefinedReasonsString, label: string, description?: string, help?: string }[] = [] | |
23 | modalTitle: string | |
24 | account: Account = null | |
25 | ||
26 | private openedModal: NgbModalRef | |
27 | ||
28 | constructor ( | |
29 | protected formReactiveService: FormReactiveService, | |
30 | private modalService: NgbModal, | |
31 | private abuseService: AbuseService, | |
32 | private notifier: Notifier | |
33 | ) { | |
34 | super() | |
35 | } | |
36 | ||
37 | get currentHost () { | |
38 | return window.location.host | |
39 | } | |
40 | ||
41 | get originHost () { | |
42 | if (this.isRemote()) { | |
43 | return this.account.host | |
44 | } | |
45 | ||
46 | return '' | |
47 | } | |
48 | ||
49 | ngOnInit () { | |
50 | this.buildForm({ | |
51 | reason: ABUSE_REASON_VALIDATOR, | |
52 | predefinedReasons: mapValues(abusePredefinedReasonsMap, r => null) | |
53 | }) | |
54 | ||
55 | this.predefinedReasons = this.abuseService.getPrefefinedReasons('account') | |
56 | } | |
57 | ||
58 | show (account: Account) { | |
59 | this.account = account | |
60 | ||
61 | this.modalTitle = $localize`Report ${this.account.displayName}` | |
62 | ||
63 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false, size: 'lg' }) | |
64 | } | |
65 | ||
66 | hide () { | |
67 | this.openedModal.close() | |
68 | this.openedModal = null | |
69 | } | |
70 | ||
71 | report () { | |
72 | const reason = this.form.get('reason').value | |
73 | const predefinedReasons = Object.keys(pickBy(this.form.get('predefinedReasons').value)) as AbusePredefinedReasonsString[] | |
74 | ||
75 | this.abuseService.reportVideo({ | |
76 | reason, | |
77 | predefinedReasons, | |
78 | account: { | |
79 | id: this.account.id | |
80 | } | |
81 | }).subscribe({ | |
82 | next: () => { | |
83 | this.notifier.success($localize`Account reported.`) | |
84 | this.hide() | |
85 | }, | |
86 | ||
87 | error: err => this.notifier.error(err.message) | |
88 | }) | |
89 | } | |
90 | ||
91 | isRemote () { | |
92 | return !this.account.isLocal | |
93 | } | |
94 | } |