1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { AuthService, Notifier, HtmlRendererService } from '@app/core'
3 import { AbuseValidatorsService, FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { AbuseMessage, UserAbuse } from '@shared/models'
8 import { AbuseService } from '../shared-moderation'
11 selector: 'my-abuse-message-modal',
12 templateUrl: './abuse-message-modal.component.html',
13 styleUrls: [ './abuse-message-modal.component.scss' ]
15 export class AbuseMessageModalComponent extends FormReactive implements OnInit {
16 @ViewChild('modal', { static: true }) modal: NgbModal
18 @Input() isAdminView: boolean
20 @Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>()
22 abuseMessages: (AbuseMessage & { messageHtml: string })[] = []
23 textareaMessage: string
24 sendingMessage = false
27 private openedModal: NgbModalRef
28 private abuse: UserAbuse
31 protected formValidatorService: FormValidatorService,
32 private abuseValidatorsService: AbuseValidatorsService,
33 private modalService: NgbModal,
35 private htmlRenderer: HtmlRendererService,
36 private auth: AuthService,
37 private notifier: Notifier,
38 private abuseService: AbuseService
45 message: this.abuseValidatorsService.ABUSE_MESSAGE
49 openModal (abuse: UserAbuse) {
52 this.openedModal = this.modalService.open(this.modal, { centered: true })
58 this.abuseMessages = []
59 this.openedModal.close()
63 this.sendingMessage = true
65 this.abuseService.addAbuseMessage(this.abuse, this.form.value['message'])
69 this.sendingMessage = false
70 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length + 1 })
76 this.sendingMessage = false
78 this.notifier.error('Sorry but you cannot send this message. Please retry later')
83 deleteMessage (abuseMessage: AbuseMessage) {
84 this.abuseService.deleteAbuseMessage(this.abuse, abuseMessage)
87 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length - 1 })
89 this.abuseMessages = this.abuseMessages.filter(m => m.id !== abuseMessage.id)
92 err => this.notifier.error(err.message)
96 isMessageByMe (abuseMessage: AbuseMessage) {
97 return this.auth.getUser().account.id === abuseMessage.account.id
100 getPlaceholderMessage () {
101 if (this.isAdminView) {
102 return this.i18n('Add a message to communicate with the reporter')
105 return this.i18n('Add a message to communicate with the moderation team')
108 private loadMessages () {
109 this.abuseService.listAbuseMessages(this.abuse)
112 this.abuseMessages = []
114 for (const m of res.data) {
115 this.abuseMessages.push(Object.assign(m, {
116 messageHtml: await this.htmlRenderer.convertToBr(m.message)
120 this.noResults = this.abuseMessages.length === 0
123 // Don't use ViewChild: it is not supported inside a ng-template
124 const messagesBlock = document.querySelector('.messages')
125 messagesBlock.scroll(0, messagesBlock.scrollHeight)
129 err => this.notifier.error(err.message)