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'
15 import { Syndication } from '@app/shared/video/syndication.model'
18 selector: 'my-video-comments',
19 templateUrl: './video-comments.component.html',
20 styleUrls: ['./video-comments.component.scss']
22 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
23 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
24 @Input() video: VideoDetails
27 comments: VideoComment[] = []
28 highlightedThread: VideoComment
29 sort: VideoSortField = '-createdAt'
30 componentPagination: ComponentPagination = {
35 inReplyToCommentId: number
36 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
37 threadLoading: { [ id: number ]: boolean } = {}
39 syndicationItems: Syndication[] = []
41 private sub: Subscription
44 private authService: AuthService,
45 private notificationsService: NotificationsService,
46 private confirmService: ConfirmService,
47 private videoCommentService: VideoCommentService,
48 private activatedRoute: ActivatedRoute,
53 // Find highlighted comment in params
54 this.sub = this.activatedRoute.params.subscribe(
56 if (params['threadId']) {
57 const highlightedThreadId = +params['threadId']
58 this.processHighlightedThread(highlightedThreadId)
64 ngOnChanges (changes: SimpleChanges) {
65 if (changes['video']) {
71 if (this.sub) this.sub.unsubscribe()
74 viewReplies (commentId: number, highlightThread = false) {
75 this.threadLoading[commentId] = true
77 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
80 this.threadComments[commentId] = res
81 this.threadLoading[commentId] = false
83 if (highlightThread) {
84 this.highlightedThread = new VideoComment(res.comment)
86 // Scroll to the highlighted thread
88 // -60 because of the fixed header
89 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
90 window.scroll(0, scrollY)
95 err => this.notificationsService.error(this.i18n('Error'), err.message)
100 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
103 this.comments = this.comments.concat(res.comments)
104 this.componentPagination.totalItems = res.totalComments
107 err => this.notificationsService.error(this.i18n('Error'), err.message)
111 onCommentThreadCreated (comment: VideoComment) {
112 this.comments.unshift(comment)
115 onWantedToReply (comment: VideoComment) {
116 this.inReplyToCommentId = comment.id
120 this.inReplyToCommentId = undefined
123 onThreadCreated (commentTree: VideoCommentThreadTree) {
124 this.viewReplies(commentTree.comment.id)
127 async onWantedToDelete (commentToDelete: VideoComment) {
128 let message = 'Do you really want to delete this comment?'
129 if (commentToDelete.totalReplies !== 0) {
130 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
133 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
134 if (res === false) return
136 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
139 // Delete the comment in the tree
140 if (commentToDelete.inReplyToCommentId) {
141 const thread = this.threadComments[commentToDelete.threadId]
143 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
147 this.deleteLocalCommentThread(thread, commentToDelete)
152 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
153 this.componentPagination.totalItems--
155 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
158 err => this.notificationsService.error(this.i18n('Error'), err.message)
163 return this.authService.isLoggedIn()
167 this.componentPagination.currentPage++
169 if (this.hasMoreComments()) {
170 this.loadMoreComments()
174 private hasMoreComments () {
176 if (this.componentPagination.totalItems === 0) return false
179 if (!this.componentPagination.totalItems) return true
181 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
182 return maxPage > this.componentPagination.currentPage
185 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
186 for (const commentChild of parentComment.children) {
187 if (commentChild.comment.id === commentToDelete.id) {
188 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
192 this.deleteLocalCommentThread(commentChild, commentToDelete)
196 private resetVideo () {
197 if (this.video.commentsEnabled === true) {
198 // Reset all our fields
199 this.highlightedThread = null
201 this.threadComments = {}
202 this.threadLoading = {}
203 this.inReplyToCommentId = undefined
204 this.componentPagination.currentPage = 1
205 this.componentPagination.totalItems = null
207 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
209 this.loadMoreComments()
213 private processHighlightedThread (highlightedThreadId: number) {
214 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
216 const highlightThread = true
217 this.viewReplies(highlightedThreadId, highlightThread)