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