]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
CommitLineData
1263fc4e 1import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
e8cb4409 2import { LinkifierService } from '@app/videos/+video-watch/comment/linkifier.service'
2890b615 3import * as sanitizeHtml from 'sanitize-html'
4cb6d457 4import { UserRight } from '../../../../../../shared/models/users'
4635f59d
C
5import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
6import { AuthService } from '../../../core/auth'
4635f59d
C
7import { Video } from '../../../shared/video/video.model'
8import { VideoComment } from './video-comment.model'
4635f59d
C
9
10@Component({
11 selector: 'my-video-comment',
12 templateUrl: './video-comment.component.html',
13 styleUrls: ['./video-comment.component.scss']
14})
1263fc4e 15export class VideoCommentComponent implements OnInit, OnChanges {
4635f59d
C
16 @Input() video: Video
17 @Input() comment: VideoComment
d7e70384 18 @Input() parentComments: VideoComment[] = []
4635f59d
C
19 @Input() commentTree: VideoCommentThreadTree
20 @Input() inReplyToCommentId: number
1263fc4e 21 @Input() highlightedComment = false
4635f59d 22
4cb6d457 23 @Output() wantedToDelete = new EventEmitter<VideoComment>()
4635f59d 24 @Output() wantedToReply = new EventEmitter<VideoComment>()
4cb6d457 25 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
4635f59d
C
26 @Output() resetReply = new EventEmitter()
27
2890b615 28 sanitizedCommentHTML = ''
c199c427 29 newParentComments: VideoComment[] = []
2890b615 30
3d9eaae3 31 constructor (
e8cb4409
C
32 private linkifierService: LinkifierService,
33 private authService: AuthService
3d9eaae3 34 ) {}
cf117aaa
C
35
36 get user () {
37 return this.authService.getUser()
4635f59d
C
38 }
39
2890b615 40 ngOnInit () {
1263fc4e
C
41 this.init()
42 }
d7e70384 43
1263fc4e
C
44 ngOnChanges () {
45 this.init()
2890b615
C
46 }
47
ae45f988
C
48 onCommentReplyCreated (createdComment: VideoComment) {
49 if (!this.commentTree) {
50 this.commentTree = {
51 comment: this.comment,
52 children: []
53 }
4cb6d457
C
54
55 this.threadCreated.emit(this.commentTree)
ae45f988
C
56 }
57
86ec3e53 58 this.commentTree.children.unshift({
ae45f988
C
59 comment: createdComment,
60 children: []
61 })
62 this.resetReply.emit()
4635f59d
C
63 }
64
4cb6d457
C
65 onWantToReply (comment?: VideoComment) {
66 this.wantedToReply.emit(comment || this.comment)
4635f59d
C
67 }
68
4cb6d457
C
69 onWantToDelete (comment?: VideoComment) {
70 this.wantedToDelete.emit(comment || this.comment)
4635f59d
C
71 }
72
4cb6d457
C
73 isUserLoggedIn () {
74 return this.authService.isLoggedIn()
4635f59d
C
75 }
76
77 onResetReply () {
78 this.resetReply.emit()
79 }
cf117aaa 80
4cb6d457
C
81 isRemovableByUser () {
82 return this.isUserLoggedIn() &&
83 (
84 this.user.account.id === this.comment.account.id ||
85 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
86 )
87 }
1263fc4e
C
88
89 private init () {
e8cb4409
C
90 // Convert possible markdown to html
91 const html = this.linkifierService.linkify(this.comment.text)
92
93 this.sanitizedCommentHTML = sanitizeHtml(html, {
3d9eaae3 94 allowedTags: [ 'a', 'p', 'span', 'br' ],
e8cb4409
C
95 allowedSchemes: [ 'http', 'https' ],
96 allowedAttributes: {
b10ba55a 97 'a': [ 'href', 'class', 'target' ]
e8cb4409
C
98 },
99 transformTags: {
100 a: (tagName, attribs) => {
101 return {
102 tagName,
103 attribs: Object.assign(attribs, {
632c5e36
C
104 target: '_blank',
105 rel: 'noopener noreferrer'
e8cb4409
C
106 })
107 }
108 }
109 }
1263fc4e
C
110 })
111
112 this.newParentComments = this.parentComments.concat([ this.comment ])
113 }
4635f59d 114}