1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import { MarkdownService, Notifier, UserService } from '@app/core'
3 import { AuthService } from '@app/core/auth'
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'
7 import { VideoComment } from './video-comment.model'
10 selector: 'my-video-comment',
11 templateUrl: './video-comment.component.html',
12 styleUrls: ['./video-comment.component.scss']
14 export class VideoCommentComponent implements OnInit, OnChanges {
16 @Input() comment: VideoComment
17 @Input() parentComments: VideoComment[] = []
18 @Input() commentTree: VideoCommentThreadTree
19 @Input() inReplyToCommentId: number
20 @Input() highlightedComment = false
21 @Input() firstInThread = false
23 @Output() wantedToDelete = new EventEmitter<VideoComment>()
24 @Output() wantedToReply = new EventEmitter<VideoComment>()
25 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
26 @Output() resetReply = new EventEmitter()
27 @Output() timestampClicked = new EventEmitter<number>()
29 sanitizedCommentHTML = ''
30 newParentComments: VideoComment[] = []
32 commentAccount: Account
36 private markdownService: MarkdownService,
37 private authService: AuthService,
38 private userService: UserService,
39 private notifier: Notifier
43 return this.authService.getUser()
54 onCommentReplyCreated (createdComment: VideoComment) {
55 if (!this.commentTree) {
57 comment: this.comment,
61 this.threadCreated.emit(this.commentTree)
64 this.commentTree.children.unshift({
65 comment: createdComment,
68 this.resetReply.emit()
71 onWantToReply (comment?: VideoComment) {
72 this.wantedToReply.emit(comment || this.comment)
75 onWantToDelete (comment?: VideoComment) {
76 this.wantedToDelete.emit(comment || this.comment)
80 return this.authService.isLoggedIn()
84 this.resetReply.emit()
87 handleTimestampClicked (timestamp: number) {
88 this.timestampClicked.emit(timestamp)
91 isRemovableByUser () {
92 return this.comment.account && this.isUserLoggedIn() &&
94 this.user.account.id === this.comment.account.id ||
95 this.user.account.id === this.video.account.id ||
96 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
100 switchToDefaultAvatar ($event: Event) {
101 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
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.getUserWithCache(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 ])
124 if (this.comment.account) {
125 this.commentAccount = new Account(this.comment.account)
126 this.getUserIfNeeded(this.commentAccount)
128 this.comment.account = null