1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import { User, UserRight } from '../../../../../../shared/models/users'
3 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4 import { AuthService } from '@app/core/auth'
5 import { AccountService } from '@app/shared/account/account.service'
6 import { Video } from '@app/shared/video/video.model'
7 import { VideoComment } from './video-comment.model'
8 import { MarkdownService } from '@app/shared/renderer'
9 import { Account } from '@app/shared/account/account.model'
10 import { Notifier } from '@app/core'
11 import { UserService } from '@app/shared'
14 selector: 'my-video-comment',
15 templateUrl: './video-comment.component.html',
16 styleUrls: ['./video-comment.component.scss']
18 export class VideoCommentComponent implements OnInit, OnChanges {
20 @Input() comment: VideoComment
21 @Input() parentComments: VideoComment[] = []
22 @Input() commentTree: VideoCommentThreadTree
23 @Input() inReplyToCommentId: number
24 @Input() highlightedComment = false
25 @Input() firstInThread = false
27 @Output() wantedToDelete = new EventEmitter<VideoComment>()
28 @Output() wantedToReply = new EventEmitter<VideoComment>()
29 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
30 @Output() resetReply = new EventEmitter()
31 @Output() timestampClicked = new EventEmitter<number>()
33 sanitizedCommentHTML = ''
34 newParentComments: VideoComment[] = []
36 commentAccount: Account
40 private markdownService: MarkdownService,
41 private authService: AuthService,
42 private accountService: AccountService,
43 private userService: UserService,
44 private notifier: Notifier
48 return this.authService.getUser()
59 onCommentReplyCreated (createdComment: VideoComment) {
60 if (!this.commentTree) {
62 comment: this.comment,
66 this.threadCreated.emit(this.commentTree)
69 this.commentTree.children.unshift({
70 comment: createdComment,
73 this.resetReply.emit()
76 onWantToReply (comment?: VideoComment) {
77 this.wantedToReply.emit(comment || this.comment)
80 onWantToDelete (comment?: VideoComment) {
81 this.wantedToDelete.emit(comment || this.comment)
85 return this.authService.isLoggedIn()
89 this.resetReply.emit()
92 handleTimestampClicked (timestamp: number) {
93 this.timestampClicked.emit(timestamp)
96 isRemovableByUser () {
97 return this.comment.account && this.isUserLoggedIn() &&
99 this.user.account.id === this.comment.account.id ||
100 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
104 private getUserIfNeeded (account: Account) {
105 if (!account.userId) return
106 if (!this.authService.isLoggedIn()) return
108 const user = this.authService.getUser()
109 if (user.hasRight(UserRight.MANAGE_USERS)) {
110 this.userService.getUser(account.userId)
112 user => this.commentUser = user,
114 err => this.notifier.error(err.message)
119 private async init () {
120 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
121 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
122 this.newParentComments = this.parentComments.concat([ this.comment ])
123 this.commentAccount = new Account(this.comment.account)
124 this.getUserIfNeeded(this.commentAccount)