]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-abuse-list/abuse-message-modal.component.ts
Add abuse messages/states notifications
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-abuse-list / abuse-message-modal.component.ts
CommitLineData
94148c90
C
1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { AuthService, Notifier } from '@app/core'
3import { AbuseValidatorsService, FormReactive, FormValidatorService } from '@app/shared/shared-forms'
441e453a
C
4import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { AbuseMessage, UserAbuse } from '@shared/models'
94148c90 8import { AbuseService } from '../shared-moderation'
441e453a
C
9
10@Component({
11 selector: 'my-abuse-message-modal',
12 templateUrl: './abuse-message-modal.component.html',
13 styleUrls: [ './abuse-message-modal.component.scss' ]
14})
15export class AbuseMessageModalComponent extends FormReactive implements OnInit {
16 @ViewChild('modal', { static: true }) modal: NgbModal
17 @ViewChild('messagesBlock', { static: false }) messagesBlock: ElementRef
18
94148c90
C
19 @Input() isAdminView: boolean
20
441e453a
C
21 @Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>()
22
23 abuseMessages: AbuseMessage[] = []
24 textareaMessage: string
25 sendingMessage = false
94148c90 26 noResults = false
441e453a
C
27
28 private openedModal: NgbModalRef
29 private abuse: UserAbuse
30
31 constructor (
32 protected formValidatorService: FormValidatorService,
33 private abuseValidatorsService: AbuseValidatorsService,
34 private modalService: NgbModal,
94148c90 35 private i18n: I18n,
441e453a
C
36 private auth: AuthService,
37 private notifier: Notifier,
441e453a
C
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
94148c90
C
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
441e453a
C
108 private loadMessages () {
109 this.abuseService.listAbuseMessages(this.abuse)
110 .subscribe(
111 res => {
112 this.abuseMessages = res.data
94148c90 113 this.noResults = this.abuseMessages.length === 0
441e453a
C
114
115 setTimeout(() => {
116 if (!this.messagesBlock) return
117
118 const element = this.messagesBlock.nativeElement as HTMLElement
119 element.scrollIntoView({ block: 'end', inline: 'nearest' })
120 })
121 },
122
123 err => this.notifier.error(err.message)
124 )
125 }
126
127}