2 import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
3 import { MarkdownService, Notifier, UserService } from '@app/core'
4 import { AuthService } from '@app/core/auth'
5 import { Account, Actor, DropdownAction, Video } from '@app/shared/shared-main'
6 import { CommentReportComponent } from '@app/shared/shared-moderation/comment-report.component'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { User, UserRight } from '@shared/models'
9 import { VideoCommentThreadTree } from './video-comment-thread-tree.model'
10 import { VideoComment } from './video-comment.model'
13 selector: 'my-video-comment',
14 templateUrl: './video-comment.component.html',
15 styleUrls: ['./video-comment.component.scss']
17 export class VideoCommentComponent implements OnInit, OnChanges {
18 @ViewChild('commentReportModal') commentReportModal: CommentReportComponent
21 @Input() comment: VideoComment
22 @Input() parentComments: VideoComment[] = []
23 @Input() commentTree: VideoCommentThreadTree
24 @Input() inReplyToCommentId: number
25 @Input() highlightedComment = false
26 @Input() firstInThread = false
28 @Output() wantedToDelete = new EventEmitter<VideoComment>()
29 @Output() wantedToReply = new EventEmitter<VideoComment>()
30 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
31 @Output() resetReply = new EventEmitter()
32 @Output() timestampClicked = new EventEmitter<number>()
34 prependModerationActions: DropdownAction<any>[]
36 sanitizedCommentHTML = ''
37 newParentComments: VideoComment[] = []
39 commentAccount: Account
44 private markdownService: MarkdownService,
45 private authService: AuthService,
46 private userService: UserService,
47 private notifier: Notifier
51 return this.authService.getUser()
62 onCommentReplyCreated (createdComment: VideoComment) {
63 if (!this.commentTree) {
65 comment: this.comment,
69 this.threadCreated.emit(this.commentTree)
72 this.commentTree.children.unshift({
73 comment: createdComment,
76 this.resetReply.emit()
79 onWantToReply (comment?: VideoComment) {
80 this.wantedToReply.emit(comment || this.comment)
83 onWantToDelete (comment?: VideoComment) {
84 this.wantedToDelete.emit(comment || this.comment)
88 return this.authService.isLoggedIn()
92 this.resetReply.emit()
95 handleTimestampClicked (timestamp: number) {
96 this.timestampClicked.emit(timestamp)
99 isRemovableByUser () {
100 return this.comment.account && this.isUserLoggedIn() &&
102 this.user.account.id === this.comment.account.id ||
103 this.user.account.id === this.video.account.id ||
104 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
108 switchToDefaultAvatar ($event: Event) {
109 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
112 private getUserIfNeeded (account: Account) {
113 if (!account.userId) return
114 if (!this.authService.isLoggedIn()) return
116 const user = this.authService.getUser()
117 if (user.hasRight(UserRight.MANAGE_USERS)) {
118 this.userService.getUserWithCache(account.userId)
120 user => this.commentUser = user,
122 err => this.notifier.error(err.message)
127 private async init () {
128 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
129 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
130 this.newParentComments = this.parentComments.concat([ this.comment ])
132 if (this.comment.account) {
133 this.commentAccount = new Account(this.comment.account)
134 this.getUserIfNeeded(this.commentAccount)
136 this.comment.account = null
139 if (this.isUserLoggedIn()) {
140 this.prependModerationActions = [
142 label: this.i18n('Report comment'),
143 handler: () => this.showReportModal()
147 this.prependModerationActions = undefined
151 private showReportModal () {
152 this.commentReportModal.show()