]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-abuse-list/abuse-message-modal.component.ts
Add migrations for abuse messages
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-abuse-list / abuse-message-modal.component.ts
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'
9
10 @Component({
11 selector: 'my-abuse-message-modal',
12 templateUrl: './abuse-message-modal.component.html',
13 styleUrls: [ './abuse-message-modal.component.scss' ]
14 })
15 export class AbuseMessageModalComponent extends FormReactive implements OnInit {
16 @ViewChild('modal', { static: true }) modal: NgbModal
17
18 @Input() isAdminView: boolean
19
20 @Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>()
21
22 abuseMessages: (AbuseMessage & { messageHtml: string })[] = []
23 textareaMessage: string
24 sendingMessage = false
25 noResults = false
26
27 private openedModal: NgbModalRef
28 private abuse: UserAbuse
29
30 constructor (
31 protected formValidatorService: FormValidatorService,
32 private abuseValidatorsService: AbuseValidatorsService,
33 private modalService: NgbModal,
34 private i18n: I18n,
35 private htmlRenderer: HtmlRendererService,
36 private auth: AuthService,
37 private notifier: Notifier,
38 private abuseService: AbuseService
39 ) {
40 super()
41 }
42
43 ngOnInit () {
44 this.buildForm({
45 message: this.abuseValidatorsService.ABUSE_MESSAGE
46 })
47 }
48
49 openModal (abuse: UserAbuse) {
50 this.abuse = abuse
51
52 this.openedModal = this.modalService.open(this.modal, { centered: true })
53
54 this.loadMessages()
55 }
56
57 hide () {
58 this.abuseMessages = []
59 this.openedModal.close()
60 }
61
62 addMessage () {
63 this.sendingMessage = true
64
65 this.abuseService.addAbuseMessage(this.abuse, this.form.value['message'])
66 .subscribe(
67 () => {
68 this.form.reset()
69 this.sendingMessage = false
70 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length + 1 })
71
72 this.loadMessages()
73 },
74
75 err => {
76 this.sendingMessage = false
77 console.error(err)
78 this.notifier.error('Sorry but you cannot send this message. Please retry later')
79 }
80 )
81 }
82
83 deleteMessage (abuseMessage: AbuseMessage) {
84 this.abuseService.deleteAbuseMessage(this.abuse, abuseMessage)
85 .subscribe(
86 () => {
87 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length - 1 })
88
89 this.abuseMessages = this.abuseMessages.filter(m => m.id !== abuseMessage.id)
90 },
91
92 err => this.notifier.error(err.message)
93 )
94 }
95
96 isMessageByMe (abuseMessage: AbuseMessage) {
97 return this.auth.getUser().account.id === abuseMessage.account.id
98 }
99
100 getPlaceholderMessage () {
101 if (this.isAdminView) {
102 return this.i18n('Add a message to communicate with the reporter')
103 }
104
105 return this.i18n('Add a message to communicate with the moderation team')
106 }
107
108 private loadMessages () {
109 this.abuseService.listAbuseMessages(this.abuse)
110 .subscribe(
111 async res => {
112 this.abuseMessages = []
113
114 for (const m of res.data) {
115 this.abuseMessages.push(Object.assign(m, {
116 messageHtml: await this.htmlRenderer.convertToBr(m.message)
117 }))
118 }
119
120 this.noResults = this.abuseMessages.length === 0
121
122 setTimeout(() => {
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)
126 })
127 },
128
129 err => this.notifier.error(err.message)
130 )
131 }
132
133 }