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