]>
Commit | Line | Data |
---|---|---|
1263fc4e | 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' |
67ed6552 | 2 | import { MarkdownService, Notifier, UserService } from '@app/core' |
edf1a4e5 | 3 | import { AuthService } from '@app/core/auth' |
67ed6552 C |
4 | import { Account, Actor, Video } from '@app/shared/shared-main' |
5 | import { User, UserRight } from '@shared/models' | |
6 | import { VideoCommentThreadTree } from './video-comment-thread-tree.model' | |
4635f59d | 7 | import { VideoComment } from './video-comment.model' |
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 |
c62a34d3 | 21 | @Input() firstInThread = false |
4635f59d | 22 | |
4cb6d457 | 23 | @Output() wantedToDelete = new EventEmitter<VideoComment>() |
4635f59d | 24 | @Output() wantedToReply = new EventEmitter<VideoComment>() |
4cb6d457 | 25 | @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>() |
4635f59d | 26 | @Output() resetReply = new EventEmitter() |
b29bf61d | 27 | @Output() timestampClicked = new EventEmitter<number>() |
4635f59d | 28 | |
2890b615 | 29 | sanitizedCommentHTML = '' |
c199c427 | 30 | newParentComments: VideoComment[] = [] |
2890b615 | 31 | |
edf1a4e5 RK |
32 | commentAccount: Account |
33 | commentUser: User | |
34 | ||
3d9eaae3 | 35 | constructor ( |
1aa75434 | 36 | private markdownService: MarkdownService, |
edf1a4e5 | 37 | private authService: AuthService, |
edf1a4e5 RK |
38 | private userService: UserService, |
39 | private notifier: Notifier | |
3d9eaae3 | 40 | ) {} |
cf117aaa C |
41 | |
42 | get user () { | |
43 | return this.authService.getUser() | |
4635f59d C |
44 | } |
45 | ||
2890b615 | 46 | ngOnInit () { |
1263fc4e C |
47 | this.init() |
48 | } | |
d7e70384 | 49 | |
1263fc4e C |
50 | ngOnChanges () { |
51 | this.init() | |
2890b615 C |
52 | } |
53 | ||
ae45f988 C |
54 | onCommentReplyCreated (createdComment: VideoComment) { |
55 | if (!this.commentTree) { | |
56 | this.commentTree = { | |
57 | comment: this.comment, | |
58 | children: [] | |
59 | } | |
4cb6d457 C |
60 | |
61 | this.threadCreated.emit(this.commentTree) | |
ae45f988 C |
62 | } |
63 | ||
86ec3e53 | 64 | this.commentTree.children.unshift({ |
ae45f988 C |
65 | comment: createdComment, |
66 | children: [] | |
67 | }) | |
68 | this.resetReply.emit() | |
4635f59d C |
69 | } |
70 | ||
4cb6d457 C |
71 | onWantToReply (comment?: VideoComment) { |
72 | this.wantedToReply.emit(comment || this.comment) | |
4635f59d C |
73 | } |
74 | ||
4cb6d457 C |
75 | onWantToDelete (comment?: VideoComment) { |
76 | this.wantedToDelete.emit(comment || this.comment) | |
4635f59d C |
77 | } |
78 | ||
4cb6d457 C |
79 | isUserLoggedIn () { |
80 | return this.authService.isLoggedIn() | |
4635f59d C |
81 | } |
82 | ||
83 | onResetReply () { | |
84 | this.resetReply.emit() | |
85 | } | |
cf117aaa | 86 | |
b29bf61d RK |
87 | handleTimestampClicked (timestamp: number) { |
88 | this.timestampClicked.emit(timestamp) | |
89 | } | |
90 | ||
4cb6d457 | 91 | isRemovableByUser () { |
b29bf61d | 92 | return this.comment.account && this.isUserLoggedIn() && |
4cb6d457 C |
93 | ( |
94 | this.user.account.id === this.comment.account.id || | |
fde37dc9 | 95 | this.user.account.id === this.video.account.id || |
4cb6d457 C |
96 | this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) |
97 | ) | |
98 | } | |
1263fc4e | 99 | |
c511c3f0 RK |
100 | switchToDefaultAvatar ($event: Event) { |
101 | ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL() | |
102 | } | |
103 | ||
edf1a4e5 RK |
104 | private getUserIfNeeded (account: Account) { |
105 | if (!account.userId) return | |
106 | if (!this.authService.isLoggedIn()) return | |
107 | ||
108 | const user = this.authService.getUser() | |
109 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
218b0874 | 110 | this.userService.getUserWithCache(account.userId) |
edf1a4e5 RK |
111 | .subscribe( |
112 | user => this.commentUser = user, | |
113 | ||
114 | err => this.notifier.error(err.message) | |
115 | ) | |
116 | } | |
117 | } | |
118 | ||
41d71344 | 119 | private async init () { |
b29bf61d RK |
120 | const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true) |
121 | this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html) | |
1263fc4e | 122 | this.newParentComments = this.parentComments.concat([ this.comment ]) |
6cb55644 C |
123 | |
124 | if (this.comment.account) { | |
125 | this.commentAccount = new Account(this.comment.account) | |
126 | this.getUserIfNeeded(this.commentAccount) | |
127 | } else { | |
128 | this.comment.account = null | |
129 | } | |
1263fc4e | 130 | } |
4635f59d | 131 | } |