]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Prevent commenting twice
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment-add.component.ts
1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { Observable } from 'rxjs'
4 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
5 import { FormReactive } from '../../../shared'
6 import { User } from '../../../shared/users'
7 import { Video } from '../../../shared/video/video.model'
8 import { VideoComment } from './video-comment.model'
9 import { VideoCommentService } from './video-comment.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service'
13
14 @Component({
15 selector: 'my-video-comment-add',
16 templateUrl: './video-comment-add.component.html',
17 styleUrls: ['./video-comment-add.component.scss']
18 })
19 export class VideoCommentAddComponent extends FormReactive implements OnInit {
20 @Input() user: User
21 @Input() video: Video
22 @Input() parentComment: VideoComment
23 @Input() parentComments: VideoComment[]
24 @Input() focusOnInit = false
25
26 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
27
28 @ViewChild('textarea') private textareaElement: ElementRef
29
30 private addingComment = false
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private videoCommentValidatorsService: VideoCommentValidatorsService,
35 private notificationsService: NotificationsService,
36 private videoCommentService: VideoCommentService,
37 private i18n: I18n
38 ) {
39 super()
40 }
41
42 ngOnInit () {
43 this.buildForm({
44 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
45 })
46
47 if (this.focusOnInit === true) {
48 this.textareaElement.nativeElement.focus()
49 }
50
51 if (this.parentComment) {
52 const mentions = this.parentComments
53 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
54 .map(c => '@' + c.by)
55
56 const mentionsSet = new Set(mentions)
57 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
58
59 this.form.patchValue({ text: mentionsText })
60 }
61 }
62
63 onValidKey () {
64 this.onValueChanged()
65 if (!this.form.valid) return
66
67 this.formValidated()
68 }
69
70 formValidated () {
71 // If we validate very quickly the comment form, we might comment twice
72 if (this.addingComment) return
73
74 this.addingComment = true
75
76 const commentCreate: VideoCommentCreate = this.form.value
77 let obs: Observable<any>
78
79 if (this.parentComment) {
80 obs = this.addCommentReply(commentCreate)
81 } else {
82 obs = this.addCommentThread(commentCreate)
83 }
84
85 obs.subscribe(
86 comment => {
87 this.addingComment = false
88 this.commentCreated.emit(comment)
89 this.form.reset()
90 },
91
92 err => {
93 this.addingComment = false
94
95 this.notificationsService.error(this.i18n('Error'), err.text)
96 }
97 )
98 }
99
100 isAddButtonDisplayed () {
101 return this.form.value['text']
102 }
103
104 private addCommentReply (commentCreate: VideoCommentCreate) {
105 return this.videoCommentService
106 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
107 }
108
109 private addCommentThread (commentCreate: VideoCommentCreate) {
110 return this.videoCommentService
111 .addCommentThread(this.video.id, commentCreate)
112 }
113 }