]>
Commit | Line | Data |
---|---|---|
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' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-video-comment', | |
12 | templateUrl: './video-comment.component.html', | |
13 | styleUrls: ['./video-comment.component.scss'] | |
14 | }) | |
15 | export class VideoCommentComponent implements OnInit, OnChanges { | |
16 | @Input() video: Video | |
17 | @Input() comment: VideoComment | |
18 | @Input() parentComments: VideoComment[] = [] | |
19 | @Input() commentTree: VideoCommentThreadTree | |
20 | @Input() inReplyToCommentId: number | |
21 | @Input() highlightedComment = false | |
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 ( | |
32 | private linkifierService: LinkifierService, | |
33 | private authService: AuthService | |
34 | ) {} | |
35 | ||
36 | get user () { | |
37 | return this.authService.getUser() | |
38 | } | |
39 | ||
40 | ngOnInit () { | |
41 | this.init() | |
42 | } | |
43 | ||
44 | ngOnChanges () { | |
45 | this.init() | |
46 | } | |
47 | ||
48 | onCommentReplyCreated (createdComment: VideoComment) { | |
49 | if (!this.commentTree) { | |
50 | this.commentTree = { | |
51 | comment: this.comment, | |
52 | children: [] | |
53 | } | |
54 | ||
55 | this.threadCreated.emit(this.commentTree) | |
56 | } | |
57 | ||
58 | this.commentTree.children.unshift({ | |
59 | comment: createdComment, | |
60 | children: [] | |
61 | }) | |
62 | this.resetReply.emit() | |
63 | } | |
64 | ||
65 | onWantToReply (comment?: VideoComment) { | |
66 | this.wantedToReply.emit(comment || this.comment) | |
67 | } | |
68 | ||
69 | onWantToDelete (comment?: VideoComment) { | |
70 | this.wantedToDelete.emit(comment || this.comment) | |
71 | } | |
72 | ||
73 | isUserLoggedIn () { | |
74 | return this.authService.isLoggedIn() | |
75 | } | |
76 | ||
77 | onResetReply () { | |
78 | this.resetReply.emit() | |
79 | } | |
80 | ||
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 | } | |
88 | ||
89 | private init () { | |
90 | // Convert possible markdown to html | |
91 | const html = this.linkifierService.linkify(this.comment.text) | |
92 | ||
93 | this.sanitizedCommentHTML = sanitizeHtml(html, { | |
94 | allowedTags: [ 'a', 'p', 'span', 'br' ], | |
95 | allowedSchemes: [ 'http', 'https' ], | |
96 | allowedAttributes: { | |
97 | 'a': [ 'href', 'class', 'target' ] | |
98 | }, | |
99 | transformTags: { | |
100 | a: (tagName, attribs) => { | |
101 | return { | |
102 | tagName, | |
103 | attribs: Object.assign(attribs, { | |
104 | target: '_blank', | |
105 | rel: 'noopener noreferrer' | |
106 | }) | |
107 | } | |
108 | } | |
109 | } | |
110 | }) | |
111 | ||
112 | this.newParentComments = this.parentComments.concat([ this.comment ]) | |
113 | } | |
114 | } |