]>
Commit | Line | Data |
---|---|---|
1263fc4e | 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' |
4cb6d457 | 2 | import { UserRight } from '../../../../../../shared/models/users' |
4635f59d C |
3 | import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' |
4 | import { AuthService } from '../../../core/auth' | |
4635f59d C |
5 | import { Video } from '../../../shared/video/video.model' |
6 | import { VideoComment } from './video-comment.model' | |
1506307f | 7 | import { HtmlRendererService } from '@app/shared/renderer' |
4635f59d C |
8 | |
9 | @Component({ | |
10 | selector: 'my-video-comment', | |
11 | templateUrl: './video-comment.component.html', | |
12 | styleUrls: ['./video-comment.component.scss'] | |
13 | }) | |
1263fc4e | 14 | export class VideoCommentComponent implements OnInit, OnChanges { |
4635f59d C |
15 | @Input() video: Video |
16 | @Input() comment: VideoComment | |
d7e70384 | 17 | @Input() parentComments: VideoComment[] = [] |
4635f59d C |
18 | @Input() commentTree: VideoCommentThreadTree |
19 | @Input() inReplyToCommentId: number | |
1263fc4e | 20 | @Input() highlightedComment = false |
4635f59d | 21 | |
4cb6d457 | 22 | @Output() wantedToDelete = new EventEmitter<VideoComment>() |
4635f59d | 23 | @Output() wantedToReply = new EventEmitter<VideoComment>() |
4cb6d457 | 24 | @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>() |
4635f59d C |
25 | @Output() resetReply = new EventEmitter() |
26 | ||
2890b615 | 27 | sanitizedCommentHTML = '' |
c199c427 | 28 | newParentComments: VideoComment[] = [] |
2890b615 | 29 | |
3d9eaae3 | 30 | constructor ( |
1506307f | 31 | private htmlRenderer: HtmlRendererService, |
e8cb4409 | 32 | private authService: AuthService |
3d9eaae3 | 33 | ) {} |
cf117aaa C |
34 | |
35 | get user () { | |
36 | return this.authService.getUser() | |
4635f59d C |
37 | } |
38 | ||
2890b615 | 39 | ngOnInit () { |
1263fc4e C |
40 | this.init() |
41 | } | |
d7e70384 | 42 | |
1263fc4e C |
43 | ngOnChanges () { |
44 | this.init() | |
2890b615 C |
45 | } |
46 | ||
ae45f988 C |
47 | onCommentReplyCreated (createdComment: VideoComment) { |
48 | if (!this.commentTree) { | |
49 | this.commentTree = { | |
50 | comment: this.comment, | |
51 | children: [] | |
52 | } | |
4cb6d457 C |
53 | |
54 | this.threadCreated.emit(this.commentTree) | |
ae45f988 C |
55 | } |
56 | ||
86ec3e53 | 57 | this.commentTree.children.unshift({ |
ae45f988 C |
58 | comment: createdComment, |
59 | children: [] | |
60 | }) | |
61 | this.resetReply.emit() | |
4635f59d C |
62 | } |
63 | ||
4cb6d457 C |
64 | onWantToReply (comment?: VideoComment) { |
65 | this.wantedToReply.emit(comment || this.comment) | |
4635f59d C |
66 | } |
67 | ||
4cb6d457 C |
68 | onWantToDelete (comment?: VideoComment) { |
69 | this.wantedToDelete.emit(comment || this.comment) | |
4635f59d C |
70 | } |
71 | ||
4cb6d457 C |
72 | isUserLoggedIn () { |
73 | return this.authService.isLoggedIn() | |
4635f59d C |
74 | } |
75 | ||
76 | onResetReply () { | |
77 | this.resetReply.emit() | |
78 | } | |
cf117aaa | 79 | |
4cb6d457 | 80 | isRemovableByUser () { |
69222afa | 81 | return this.comment.account && this.isUserLoggedIn() && |
4cb6d457 C |
82 | ( |
83 | this.user.account.id === this.comment.account.id || | |
84 | this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) | |
85 | ) | |
86 | } | |
1263fc4e | 87 | |
41d71344 C |
88 | private async init () { |
89 | this.sanitizedCommentHTML = await this.htmlRenderer.toSafeHtml(this.comment.text) | |
1263fc4e C |
90 | |
91 | this.newParentComments = this.parentComments.concat([ this.comment ]) | |
92 | } | |
4635f59d | 93 | } |