1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import { User, UserRight } from '../../../../../../shared/models/users'
3 import { AuthService } from '@app/core/auth'
4 import { AccountService } from '@app/shared/account/account.service'
5 import { Video } from '@app/shared/video/video.model'
6 import { VideoComment } from './video-comment.model'
7 import { MarkdownService } from '@app/shared/renderer'
8 import { Account } from '@app/shared/account/account.model'
9 import { Notifier } from '@app/core'
10 import { UserService } from '@app/shared'
11 import { Actor } from '@app/shared/actor/actor.model'
12 import { VideoCommentThreadTree } from '@app/videos/+video-watch/comment/video-comment-thread-tree.model'
15 selector: 'my-video-comment',
16 templateUrl: './video-comment.component.html',
17 styleUrls: ['./video-comment.component.scss']
19 export class VideoCommentComponent implements OnInit, OnChanges {
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 sanitizedCommentHTML = ''
35 newParentComments: VideoComment[] = []
37 commentAccount: Account
41 private markdownService: MarkdownService,
42 private authService: AuthService,
43 private accountService: AccountService,
44 private userService: UserService,
45 private notifier: Notifier
49 return this.authService.getUser()
60 onCommentReplyCreated (createdComment: VideoComment) {
61 if (!this.commentTree) {
63 comment: this.comment,
67 this.threadCreated.emit(this.commentTree)
70 this.commentTree.children.unshift({
71 comment: createdComment,
74 this.resetReply.emit()
77 onWantToReply (comment?: VideoComment) {
78 this.wantedToReply.emit(comment || this.comment)
81 onWantToDelete (comment?: VideoComment) {
82 this.wantedToDelete.emit(comment || this.comment)
86 return this.authService.isLoggedIn()
90 this.resetReply.emit()
93 handleTimestampClicked (timestamp: number) {
94 this.timestampClicked.emit(timestamp)
97 isRemovableByUser () {
98 return this.comment.account && this.isUserLoggedIn() &&
100 this.user.account.id === this.comment.account.id ||
101 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
105 switchToDefaultAvatar ($event: Event) {
106 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
109 private getUserIfNeeded (account: Account) {
110 if (!account.userId) return
111 if (!this.authService.isLoggedIn()) return
113 const user = this.authService.getUser()
114 if (user.hasRight(UserRight.MANAGE_USERS)) {
115 this.userService.getUserWithCache(account.userId)
117 user => this.commentUser = user,
119 err => this.notifier.error(err.message)
124 private async init () {
125 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
126 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
127 this.newParentComments = this.parentComments.concat([ this.comment ])
129 if (this.comment.account) {
130 this.commentAccount = new Account(this.comment.account)
131 this.getUserIfNeeded(this.commentAccount)
133 this.comment.account = null