1 import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild, ElementRef } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { ConfirmService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { Subscription } from 'rxjs'
6 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
7 import { AuthService } from '../../../core/auth'
8 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
9 import { User } from '../../../shared/users'
10 import { VideoSortField } from '../../../shared/video/sort-field.type'
11 import { VideoDetails } from '../../../shared/video/video-details.model'
12 import { VideoComment } from './video-comment.model'
13 import { VideoCommentService } from './video-comment.service'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
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 private sub: Subscription
41 private authService: AuthService,
42 private notificationsService: NotificationsService,
43 private confirmService: ConfirmService,
44 private videoCommentService: VideoCommentService,
45 private activatedRoute: ActivatedRoute,
50 // Find highlighted comment in params
51 this.sub = this.activatedRoute.params.subscribe(
53 if (params['threadId']) {
54 const highlightedThreadId = +params['threadId']
55 this.processHighlightedThread(highlightedThreadId)
61 ngOnChanges (changes: SimpleChanges) {
62 if (changes['video']) {
68 if (this.sub) this.sub.unsubscribe()
71 viewReplies (commentId: number, highlightThread = false) {
72 this.threadLoading[commentId] = true
74 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
77 this.threadComments[commentId] = res
78 this.threadLoading[commentId] = false
80 if (highlightThread) {
81 this.highlightedThread = new VideoComment(res.comment)
83 // Scroll to the highlighted thread
85 // -60 because of the fixed header
86 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
87 window.scroll(0, scrollY)
92 err => this.notificationsService.error(this.i18n('Error'), err.message)
97 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
100 this.comments = this.comments.concat(res.comments)
101 this.componentPagination.totalItems = res.totalComments
104 err => this.notificationsService.error(this.i18n('Error'), err.message)
108 onCommentThreadCreated (comment: VideoComment) {
109 this.comments.unshift(comment)
112 onWantedToReply (comment: VideoComment) {
113 this.inReplyToCommentId = comment.id
117 this.inReplyToCommentId = undefined
120 onThreadCreated (commentTree: VideoCommentThreadTree) {
121 this.viewReplies(commentTree.comment.id)
124 async onWantedToDelete (commentToDelete: VideoComment) {
125 let message = 'Do you really want to delete this comment?'
126 if (commentToDelete.totalReplies !== 0) {
127 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
130 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
131 if (res === false) return
133 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
136 // Delete the comment in the tree
137 if (commentToDelete.inReplyToCommentId) {
138 const thread = this.threadComments[commentToDelete.threadId]
140 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
144 this.deleteLocalCommentThread(thread, commentToDelete)
149 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
150 this.componentPagination.totalItems--
152 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
155 err => this.notificationsService.error(this.i18n('Error'), err.message)
160 return this.authService.isLoggedIn()
164 this.componentPagination.currentPage++
166 if (this.hasMoreComments()) {
167 this.loadMoreComments()
171 private hasMoreComments () {
173 if (this.componentPagination.totalItems === 0) return false
176 if (!this.componentPagination.totalItems) return true
178 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
179 return maxPage > this.componentPagination.currentPage
182 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
183 for (const commentChild of parentComment.children) {
184 if (commentChild.comment.id === commentToDelete.id) {
185 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
189 this.deleteLocalCommentThread(commentChild, commentToDelete)
193 private resetVideo () {
194 if (this.video.commentsEnabled === true) {
195 // Reset all our fields
196 this.highlightedThread = null
198 this.threadComments = {}
199 this.threadLoading = {}
200 this.inReplyToCommentId = undefined
201 this.componentPagination.currentPage = 1
202 this.componentPagination.totalItems = null
204 this.loadMoreComments()
208 private processHighlightedThread (highlightedThreadId: number) {
209 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
211 const highlightThread = true
212 this.viewReplies(highlightedThreadId, highlightThread)