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