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
26 @Output() wantedToDelete = new EventEmitter<VideoComment>()
27 @Output() wantedToReply = new EventEmitter<VideoComment>()
28 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
29 @Output() resetReply = new EventEmitter()
30 @Output() timestampClicked = new EventEmitter<number>()
32 sanitizedCommentHTML = ''
33 newParentComments: VideoComment[] = []
35 commentAccount: Account
39 private markdownService: MarkdownService,
40 private authService: AuthService,
41 private accountService: AccountService,
42 private userService: UserService,
43 private notifier: Notifier
47 return this.authService.getUser()
58 onCommentReplyCreated (createdComment: VideoComment) {
59 if (!this.commentTree) {
61 comment: this.comment,
65 this.threadCreated.emit(this.commentTree)
68 this.commentTree.children.unshift({
69 comment: createdComment,
72 this.resetReply.emit()
75 onWantToReply (comment?: VideoComment) {
76 this.wantedToReply.emit(comment || this.comment)
79 onWantToDelete (comment?: VideoComment) {
80 this.wantedToDelete.emit(comment || this.comment)
84 return this.authService.isLoggedIn()
88 this.resetReply.emit()
91 handleTimestampClicked (timestamp: number) {
92 this.timestampClicked.emit(timestamp)
95 isRemovableByUser () {
96 return this.comment.account && this.isUserLoggedIn() &&
98 this.user.account.id === this.comment.account.id ||
99 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
103 private getUserIfNeeded (account: Account) {
104 if (!account.userId) return
105 if (!this.authService.isLoggedIn()) return
107 const user = this.authService.getUser()
108 if (user.hasRight(UserRight.MANAGE_USERS)) {
109 this.userService.getUser(account.userId)
111 user => this.commentUser = user,
113 err => this.notifier.error(err.message)
118 private async init () {
119 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
120 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
121 this.newParentComments = this.parentComments.concat([ this.comment ])
122 this.commentAccount = new Account(this.comment.account)
123 this.getUserIfNeeded(this.commentAccount)