]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment.component.ts
38e603d0dc8cdf3bf8b6d0a1702a9485ac53f34f
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
1 import { Component, EventEmitter, Input, 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 {
17 @Input() video: Video
18 @Input() comment: VideoComment
19 @Input() parentComments: VideoComment[] = []
20 @Input() commentTree: VideoCommentThreadTree
21 @Input() inReplyToCommentId: number
22
23 @Output() wantedToDelete = new EventEmitter<VideoComment>()
24 @Output() wantedToReply = new EventEmitter<VideoComment>()
25 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
26 @Output() resetReply = new EventEmitter()
27
28 sanitizedCommentHTML = ''
29 newParentComments = []
30
31 constructor (private authService: AuthService) {}
32
33 get user () {
34 return this.authService.getUser()
35 }
36
37 ngOnInit () {
38 this.sanitizedCommentHTML = sanitizeHtml(this.comment.text, {
39 allowedTags: [ 'p', 'span' ]
40 })
41
42 this.newParentComments = this.parentComments.concat([ this.comment ])
43 }
44
45 onCommentReplyCreated (createdComment: VideoComment) {
46 if (!this.commentTree) {
47 this.commentTree = {
48 comment: this.comment,
49 children: []
50 }
51
52 this.threadCreated.emit(this.commentTree)
53 }
54
55 this.commentTree.children.push({
56 comment: createdComment,
57 children: []
58 })
59 this.resetReply.emit()
60 }
61
62 onWantToReply (comment?: VideoComment) {
63 this.wantedToReply.emit(comment || this.comment)
64 }
65
66 onWantToDelete (comment?: VideoComment) {
67 this.wantedToDelete.emit(comment || this.comment)
68 }
69
70 isUserLoggedIn () {
71 return this.authService.isLoggedIn()
72 }
73
74 onResetReply () {
75 this.resetReply.emit()
76 }
77
78 getAvatarUrl (account: AccountInterface) {
79 return Account.GET_ACCOUNT_AVATAR_URL(account)
80 }
81
82 isRemovableByUser () {
83 return this.isUserLoggedIn() &&
84 (
85 this.user.account.id === this.comment.account.id ||
86 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
87 )
88 }
89 }