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 { Account as AccountInterface } from '../../../../../../shared/models/actors'
5 import { UserRight } from '../../../../../../shared/models/users'
6 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
7 import { AuthService } from '../../../core/auth'
8 import { Account } from '../../../shared/account/account.model'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
13 selector: 'my-video-comment',
14 templateUrl: './video-comment.component.html',
15 styleUrls: ['./video-comment.component.scss']
17 export class VideoCommentComponent implements OnInit, OnChanges {
19 @Input() comment: VideoComment
20 @Input() parentComments: VideoComment[] = []
21 @Input() commentTree: VideoCommentThreadTree
22 @Input() inReplyToCommentId: number
23 @Input() highlightedComment = false
25 @Output() wantedToDelete = new EventEmitter<VideoComment>()
26 @Output() wantedToReply = new EventEmitter<VideoComment>()
27 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
28 @Output() resetReply = new EventEmitter()
30 sanitizedCommentHTML = ''
31 newParentComments = []
34 private linkifierService: LinkifierService,
35 private authService: AuthService
39 return this.authService.getUser()
50 onCommentReplyCreated (createdComment: VideoComment) {
51 if (!this.commentTree) {
53 comment: this.comment,
57 this.threadCreated.emit(this.commentTree)
60 this.commentTree.children.unshift({
61 comment: createdComment,
64 this.resetReply.emit()
67 onWantToReply (comment?: VideoComment) {
68 this.wantedToReply.emit(comment || this.comment)
71 onWantToDelete (comment?: VideoComment) {
72 this.wantedToDelete.emit(comment || this.comment)
76 return this.authService.isLoggedIn()
80 this.resetReply.emit()
83 getAvatarUrl (account: AccountInterface) {
84 return Account.GET_ACCOUNT_AVATAR_URL(account)
87 isRemovableByUser () {
88 return this.isUserLoggedIn() &&
90 this.user.account.id === this.comment.account.id ||
91 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
96 // Convert possible markdown to html
97 const html = this.linkifierService.linkify(this.comment.text)
99 this.sanitizedCommentHTML = sanitizeHtml(html, {
100 allowedTags: [ 'a', 'p', 'span', 'br' ],
101 allowedSchemes: [ 'http', 'https' ],
103 'a': [ 'href', 'class', 'target' ]
106 a: (tagName, attribs) => {
109 attribs: Object.assign(attribs, {
117 this.newParentComments = this.parentComments.concat([ this.comment ])