]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment.component.ts
9176de80f4a2e1e520001fbfb29c68388deeaa2f
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import * as sanitizeHtml from 'sanitize-html'
3 import { Account as AccountInterface } from '../../../../../../shared/models/actors'
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 { Account } from '../../../shared/account/account.model'
8 import { Video } from '../../../shared/video/video.model'
9 import { VideoComment } from './video-comment.model'
10
11 @Component({
12 selector: 'my-video-comment',
13 templateUrl: './video-comment.component.html',
14 styleUrls: ['./video-comment.component.scss']
15 })
16 export class VideoCommentComponent implements OnInit, OnChanges {
17 @Input() video: Video
18 @Input() comment: VideoComment
19 @Input() parentComments: VideoComment[] = []
20 @Input() commentTree: VideoCommentThreadTree
21 @Input() inReplyToCommentId: number
22 @Input() highlightedComment = false
23
24 @Output() wantedToDelete = new EventEmitter<VideoComment>()
25 @Output() wantedToReply = new EventEmitter<VideoComment>()
26 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
27 @Output() resetReply = new EventEmitter()
28
29 sanitizedCommentHTML = ''
30 newParentComments = []
31
32 constructor (private authService: AuthService) {}
33
34 get user () {
35 return this.authService.getUser()
36 }
37
38 ngOnInit () {
39 this.init()
40 }
41
42 ngOnChanges () {
43 this.init()
44 }
45
46 onCommentReplyCreated (createdComment: VideoComment) {
47 if (!this.commentTree) {
48 this.commentTree = {
49 comment: this.comment,
50 children: []
51 }
52
53 this.threadCreated.emit(this.commentTree)
54 }
55
56 this.commentTree.children.push({
57 comment: createdComment,
58 children: []
59 })
60 this.resetReply.emit()
61 }
62
63 onWantToReply (comment?: VideoComment) {
64 this.wantedToReply.emit(comment || this.comment)
65 }
66
67 onWantToDelete (comment?: VideoComment) {
68 this.wantedToDelete.emit(comment || this.comment)
69 }
70
71 isUserLoggedIn () {
72 return this.authService.isLoggedIn()
73 }
74
75 onResetReply () {
76 this.resetReply.emit()
77 }
78
79 getAvatarUrl (account: AccountInterface) {
80 return Account.GET_ACCOUNT_AVATAR_URL(account)
81 }
82
83 isRemovableByUser () {
84 return this.isUserLoggedIn() &&
85 (
86 this.user.account.id === this.comment.account.id ||
87 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
88 )
89 }
90
91 private init () {
92 this.sanitizedCommentHTML = sanitizeHtml(this.comment.text, {
93 allowedTags: [ 'p', 'span', 'br' ]
94 })
95
96 this.newParentComments = this.parentComments.concat([ this.comment ])
97 }
98 }