2020-08-12 08:40:04 +00:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
|
|
|
|
import { AuthService, HtmlRendererService, Notifier } from '@app/core'
|
2020-08-17 09:47:04 +00:00
|
|
|
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
|
2020-07-24 15:21:25 +00:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|
|
|
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
|
|
|
|
import { AbuseMessage, UserAbuse } from '@shared/models'
|
2020-08-17 09:47:04 +00:00
|
|
|
import { ABUSE_MESSAGE_VALIDATOR } from '../form-validators/abuse-validators'
|
2020-07-27 09:40:30 +00:00
|
|
|
import { AbuseService } from '../shared-moderation'
|
2020-07-24 15:21:25 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-abuse-message-modal',
|
|
|
|
templateUrl: './abuse-message-modal.component.html',
|
|
|
|
styleUrls: [ './abuse-message-modal.component.scss' ]
|
|
|
|
})
|
|
|
|
export class AbuseMessageModalComponent extends FormReactive implements OnInit {
|
|
|
|
@ViewChild('modal', { static: true }) modal: NgbModal
|
|
|
|
|
2020-07-27 09:40:30 +00:00
|
|
|
@Input() isAdminView: boolean
|
|
|
|
|
2020-07-24 15:21:25 +00:00
|
|
|
@Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>()
|
|
|
|
|
2020-07-28 07:57:16 +00:00
|
|
|
abuseMessages: (AbuseMessage & { messageHtml: string })[] = []
|
2020-07-24 15:21:25 +00:00
|
|
|
textareaMessage: string
|
|
|
|
sendingMessage = false
|
2020-07-27 09:40:30 +00:00
|
|
|
noResults = false
|
2020-07-24 15:21:25 +00:00
|
|
|
|
|
|
|
private openedModal: NgbModalRef
|
|
|
|
private abuse: UserAbuse
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
protected formValidatorService: FormValidatorService,
|
|
|
|
private modalService: NgbModal,
|
2020-07-28 07:57:16 +00:00
|
|
|
private htmlRenderer: HtmlRendererService,
|
2020-07-24 15:21:25 +00:00
|
|
|
private auth: AuthService,
|
|
|
|
private notifier: Notifier,
|
|
|
|
private abuseService: AbuseService
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm({
|
2020-08-17 09:47:04 +00:00
|
|
|
message: ABUSE_MESSAGE_VALIDATOR
|
2020-07-24 15:21:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
openModal (abuse: UserAbuse) {
|
|
|
|
this.abuse = abuse
|
|
|
|
|
|
|
|
this.openedModal = this.modalService.open(this.modal, { centered: true })
|
|
|
|
|
|
|
|
this.loadMessages()
|
|
|
|
}
|
|
|
|
|
|
|
|
hide () {
|
|
|
|
this.abuseMessages = []
|
|
|
|
this.openedModal.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessage () {
|
|
|
|
this.sendingMessage = true
|
|
|
|
|
|
|
|
this.abuseService.addAbuseMessage(this.abuse, this.form.value['message'])
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.form.reset()
|
|
|
|
this.sendingMessage = false
|
|
|
|
this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length + 1 })
|
|
|
|
|
|
|
|
this.loadMessages()
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
|
|
|
this.sendingMessage = false
|
|
|
|
console.error(err)
|
|
|
|
this.notifier.error('Sorry but you cannot send this message. Please retry later')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteMessage (abuseMessage: AbuseMessage) {
|
|
|
|
this.abuseService.deleteAbuseMessage(this.abuse, abuseMessage)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length - 1 })
|
|
|
|
|
|
|
|
this.abuseMessages = this.abuseMessages.filter(m => m.id !== abuseMessage.id)
|
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
isMessageByMe (abuseMessage: AbuseMessage) {
|
|
|
|
return this.auth.getUser().account.id === abuseMessage.account.id
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:40:30 +00:00
|
|
|
getPlaceholderMessage () {
|
|
|
|
if (this.isAdminView) {
|
2020-08-12 08:40:04 +00:00
|
|
|
return $localize`Add a message to communicate with the reporter`
|
2020-07-27 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:40:04 +00:00
|
|
|
return $localize`Add a message to communicate with the moderation team`
|
2020-07-27 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:21:25 +00:00
|
|
|
private loadMessages () {
|
|
|
|
this.abuseService.listAbuseMessages(this.abuse)
|
|
|
|
.subscribe(
|
2020-07-28 07:57:16 +00:00
|
|
|
async res => {
|
|
|
|
this.abuseMessages = []
|
|
|
|
|
|
|
|
for (const m of res.data) {
|
|
|
|
this.abuseMessages.push(Object.assign(m, {
|
|
|
|
messageHtml: await this.htmlRenderer.convertToBr(m.message)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:40:30 +00:00
|
|
|
this.noResults = this.abuseMessages.length === 0
|
2020-07-24 15:21:25 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2020-07-28 07:57:16 +00:00
|
|
|
// Don't use ViewChild: it is not supported inside a ng-template
|
|
|
|
const messagesBlock = document.querySelector('.messages')
|
|
|
|
messagesBlock.scroll(0, messagesBlock.scrollHeight)
|
2020-07-24 15:21:25 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|