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 console.log(this.commentHighlightBlock.nativeElement.offsetTop)
87 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
88 window.scroll(0, scrollY)
93 err => this.notificationsService.error(this.i18n('Error'), err.message)
98 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
101 this.comments = this.comments.concat(res.comments)
102 this.componentPagination.totalItems = res.totalComments
105 err => this.notificationsService.error(this.i18n('Error'), err.message)
109 onCommentThreadCreated (comment: VideoComment) {
110 this.comments.unshift(comment)
113 onWantedToReply (comment: VideoComment) {
114 this.inReplyToCommentId = comment.id
118 this.inReplyToCommentId = undefined
121 onThreadCreated (commentTree: VideoCommentThreadTree) {
122 this.viewReplies(commentTree.comment.id)
125 async onWantedToDelete (commentToDelete: VideoComment) {
126 let message = 'Do you really want to delete this comment?'
127 if (commentToDelete.totalReplies !== 0) {
128 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
131 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
132 if (res === false) return
134 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
137 // Delete the comment in the tree
138 if (commentToDelete.inReplyToCommentId) {
139 const thread = this.threadComments[commentToDelete.threadId]
141 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
145 this.deleteLocalCommentThread(thread, commentToDelete)
150 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
151 this.componentPagination.totalItems--
154 err => this.notificationsService.error(this.i18n('Error'), err.message)
159 return this.authService.isLoggedIn()
163 this.componentPagination.currentPage++
165 if (this.hasMoreComments()) {
166 this.loadMoreComments()
170 private hasMoreComments () {
172 if (this.componentPagination.totalItems === 0) return false
175 if (!this.componentPagination.totalItems) return true
177 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
178 return maxPage > this.componentPagination.currentPage
181 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
182 for (const commentChild of parentComment.children) {
183 if (commentChild.comment.id === commentToDelete.id) {
184 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
188 this.deleteLocalCommentThread(commentChild, commentToDelete)
192 private resetVideo () {
193 if (this.video.commentsEnabled === true) {
194 // Reset all our fields
195 this.highlightedThread = null
197 this.threadComments = {}
198 this.threadLoading = {}
199 this.inReplyToCommentId = undefined
200 this.componentPagination.currentPage = 1
201 this.componentPagination.totalItems = null
203 this.loadMoreComments()
207 private processHighlightedThread (highlightedThreadId: number) {
208 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
210 const highlightThread = true
211 this.viewReplies(highlightedThreadId, highlightThread)