1 import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { ConfirmService, Notifier } from '@app/core'
4 import { Subscription } from 'rxjs'
5 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
6 import { AuthService } from '../../../core/auth'
7 import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model'
8 import { User } from '../../../shared/users'
9 import { VideoSortField } from '../../../shared/video/sort-field.type'
10 import { VideoDetails } from '../../../shared/video/video-details.model'
11 import { VideoComment } from './video-comment.model'
12 import { VideoCommentService } from './video-comment.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { Syndication } from '@app/shared/video/syndication.model'
17 selector: 'my-video-comments',
18 templateUrl: './video-comments.component.html',
19 styleUrls: ['./video-comments.component.scss']
21 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
22 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
23 @Input() video: VideoDetails
26 comments: VideoComment[] = []
27 highlightedThread: VideoComment
28 sort: VideoSortField = '-createdAt'
29 componentPagination: ComponentPagination = {
34 inReplyToCommentId: number
35 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
36 threadLoading: { [ id: number ]: boolean } = {}
38 syndicationItems: Syndication[] = []
40 private sub: Subscription
43 private authService: AuthService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private videoCommentService: VideoCommentService,
47 private activatedRoute: ActivatedRoute,
52 // Find highlighted comment in params
53 this.sub = this.activatedRoute.params.subscribe(
55 if (params['threadId']) {
56 const highlightedThreadId = +params['threadId']
57 this.processHighlightedThread(highlightedThreadId)
63 ngOnChanges (changes: SimpleChanges) {
64 if (changes['video']) {
70 if (this.sub) this.sub.unsubscribe()
73 viewReplies (commentId: number, highlightThread = false) {
74 this.threadLoading[commentId] = true
76 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
79 this.threadComments[commentId] = res
80 this.threadLoading[commentId] = false
82 if (highlightThread) {
83 this.highlightedThread = new VideoComment(res.comment)
85 // Scroll to the highlighted thread
86 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
90 err => this.notifier.error(err.message)
95 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
98 this.comments = this.comments.concat(res.comments)
99 this.componentPagination.totalItems = res.totalComments
102 err => this.notifier.error(err.message)
106 onCommentThreadCreated (comment: VideoComment) {
107 this.comments.unshift(comment)
110 onWantedToReply (comment: VideoComment) {
111 this.inReplyToCommentId = comment.id
115 this.inReplyToCommentId = undefined
118 onThreadCreated (commentTree: VideoCommentThreadTree) {
119 this.viewReplies(commentTree.comment.id)
122 async onWantedToDelete (commentToDelete: VideoComment) {
123 let message = 'Do you really want to delete this comment?'
124 if (commentToDelete.totalReplies !== 0) {
125 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
128 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
129 if (res === false) return
131 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
134 // Delete the comment in the tree
135 if (commentToDelete.inReplyToCommentId) {
136 const thread = this.threadComments[commentToDelete.threadId]
138 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
142 this.deleteLocalCommentThread(thread, commentToDelete)
147 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
148 this.componentPagination.totalItems--
150 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
153 err => this.notifier.error(err.message)
158 return this.authService.isLoggedIn()
162 this.componentPagination.currentPage++
164 if (hasMoreItems(this.componentPagination)) {
165 this.loadMoreComments()
169 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
170 for (const commentChild of parentComment.children) {
171 if (commentChild.comment.id === commentToDelete.id) {
172 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
176 this.deleteLocalCommentThread(commentChild, commentToDelete)
180 private resetVideo () {
181 if (this.video.commentsEnabled === true) {
182 // Reset all our fields
183 this.highlightedThread = null
185 this.threadComments = {}
186 this.threadLoading = {}
187 this.inReplyToCommentId = undefined
188 this.componentPagination.currentPage = 1
189 this.componentPagination.totalItems = null
191 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
193 this.loadMoreComments()
197 private processHighlightedThread (highlightedThreadId: number) {
198 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
200 const highlightThread = true
201 this.viewReplies(highlightedThreadId, highlightThread)