1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import { LinkifierService } from '@app/videos/+video-watch/comment/linkifier.service'
3 import * as sanitizeHtml from 'sanitize-html'
4 import { UserRight } from '../../../../../../shared/models/users'
5 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
6 import { AuthService } from '../../../core/auth'
7 import { Video } from '../../../shared/video/video.model'
8 import { VideoComment } from './video-comment.model'
11 selector: 'my-video-comment',
12 templateUrl: './video-comment.component.html',
13 styleUrls: ['./video-comment.component.scss']
15 export class VideoCommentComponent implements OnInit, OnChanges {
17 @Input() comment: VideoComment
18 @Input() parentComments: VideoComment[] = []
19 @Input() commentTree: VideoCommentThreadTree
20 @Input() inReplyToCommentId: number
21 @Input() highlightedComment = false
23 @Output() wantedToDelete = new EventEmitter<VideoComment>()
24 @Output() wantedToReply = new EventEmitter<VideoComment>()
25 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
26 @Output() resetReply = new EventEmitter()
28 sanitizedCommentHTML = ''
29 newParentComments: VideoComment[] = []
32 private linkifierService: LinkifierService,
33 private authService: AuthService
37 return this.authService.getUser()
48 onCommentReplyCreated (createdComment: VideoComment) {
49 if (!this.commentTree) {
51 comment: this.comment,
55 this.threadCreated.emit(this.commentTree)
58 this.commentTree.children.unshift({
59 comment: createdComment,
62 this.resetReply.emit()
65 onWantToReply (comment?: VideoComment) {
66 this.wantedToReply.emit(comment || this.comment)
69 onWantToDelete (comment?: VideoComment) {
70 this.wantedToDelete.emit(comment || this.comment)
74 return this.authService.isLoggedIn()
78 this.resetReply.emit()
81 isRemovableByUser () {
82 return this.isUserLoggedIn() &&
84 this.user.account.id === this.comment.account.id ||
85 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
90 // Convert possible markdown to html
91 const html = this.linkifierService.linkify(this.comment.text)
93 this.sanitizedCommentHTML = sanitizeHtml(html, {
94 allowedTags: [ 'a', 'p', 'span', 'br' ],
95 allowedSchemes: [ 'http', 'https' ],
97 'a': [ 'href', 'class', 'target' ]
100 a: (tagName, attribs) => {
103 attribs: Object.assign(attribs, {
105 rel: 'noopener noreferrer'
112 this.newParentComments = this.parentComments.concat([ this.comment ])