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