]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Refractor notification service
[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'
660d11e9 2import { Router } from '@angular/router'
f8b2c1b4 3import { Notifier } from '@app/core'
db400f44 4import { Observable } from 'rxjs'
3d9eaae3 5import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
4635f59d 6import { FormReactive } from '../../../shared'
cf117aaa 7import { User } from '../../../shared/users'
4635f59d
C
8import { Video } from '../../../shared/video/video.model'
9import { VideoComment } from './video-comment.model'
10import { VideoCommentService } from './video-comment.service'
b1d40cff 11import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 12import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 13import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service'
660d11e9
RK
14import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
15import { AuthService } from '@app/core/auth'
4635f59d
C
16
17@Component({
18 selector: 'my-video-comment-add',
19 templateUrl: './video-comment-add.component.html',
20 styleUrls: ['./video-comment-add.component.scss']
21})
22export class VideoCommentAddComponent extends FormReactive implements OnInit {
cf117aaa 23 @Input() user: User
4635f59d
C
24 @Input() video: Video
25 @Input() parentComment: VideoComment
d7e70384 26 @Input() parentComments: VideoComment[]
eacf925e 27 @Input() focusOnInit = false
4635f59d
C
28
29 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
30
660d11e9 31 @ViewChild('visitorModal') visitorModal: NgbModal
f7454ca6 32 @ViewChild('textarea') textareaElement: ElementRef
eacf925e 33
2fbe7f19 34 addingComment = false
ff336427 35
4635f59d 36 constructor (
d18d6478 37 protected formValidatorService: FormValidatorService,
e309822b 38 private videoCommentValidatorsService: VideoCommentValidatorsService,
f8b2c1b4 39 private notifier: Notifier,
b1d40cff 40 private videoCommentService: VideoCommentService,
660d11e9
RK
41 private authService: AuthService,
42 private modalService: NgbModal,
43 private router: Router,
b1d40cff 44 private i18n: I18n
4635f59d
C
45 ) {
46 super()
47 }
48
4635f59d 49 ngOnInit () {
d18d6478 50 this.buildForm({
e309822b 51 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
d18d6478 52 })
eacf925e 53
660d11e9
RK
54 if (this.user) {
55 if (this.focusOnInit === true) {
56 this.textareaElement.nativeElement.focus()
57 }
58
59 if (this.parentComment) {
60 const mentions = this.parentComments
61 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
62 .map(c => '@' + c.by)
d7e70384 63
660d11e9
RK
64 const mentionsSet = new Set(mentions)
65 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
d7e70384 66
660d11e9
RK
67 this.form.patchValue({ text: mentionsText })
68 }
d7e70384 69 }
4635f59d
C
70 }
71
1263fc4e
C
72 onValidKey () {
73 this.onValueChanged()
74 if (!this.form.valid) return
75
76 this.formValidated()
77 }
78
244b4ae3 79 openVisitorModal (event: any) {
660d11e9
RK
80 if (this.user === null) { // we only open it for visitors
81 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
82 event.srcElement.blur()
83 event.preventDefault()
84
85 this.modalService.open(this.visitorModal)
86 }
87 }
88
89 hideVisitorModal () {
90 this.modalService.dismissAll()
91 }
92
4635f59d 93 formValidated () {
ff336427
C
94 // If we validate very quickly the comment form, we might comment twice
95 if (this.addingComment) return
96
97 this.addingComment = true
98
4635f59d
C
99 const commentCreate: VideoCommentCreate = this.form.value
100 let obs: Observable<any>
101
102 if (this.parentComment) {
103 obs = this.addCommentReply(commentCreate)
104 } else {
105 obs = this.addCommentThread(commentCreate)
106 }
107
108 obs.subscribe(
109 comment => {
ff336427 110 this.addingComment = false
4635f59d
C
111 this.commentCreated.emit(comment)
112 this.form.reset()
113 },
114
ff336427
C
115 err => {
116 this.addingComment = false
117
f8b2c1b4 118 this.notifier.error(err.text)
ff336427 119 }
4635f59d 120 )
d50acfab 121 }
4635f59d
C
122
123 isAddButtonDisplayed () {
124 return this.form.value['text']
125 }
126
6d5973fa
RK
127 getUrl () {
128 return window.location.href
129 }
130
660d11e9
RK
131 getAvatarUrl () {
132 if (this.user) return this.user.accountAvatarUrl
133 return window.location.origin + '/client/assets/images/default-avatar.png'
134 }
135
136 gotoLogin () {
137 this.hideVisitorModal()
660d11e9
RK
138 this.router.navigate([ '/login' ])
139 }
140
4635f59d
C
141 private addCommentReply (commentCreate: VideoCommentCreate) {
142 return this.videoCommentService
143 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
144 }
145
146 private addCommentThread (commentCreate: VideoCommentCreate) {
147 return this.videoCommentService
148 .addCommentThread(this.video.id, commentCreate)
149 }
150}