1 import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } 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 @Input() video: VideoDetails
25 comments: VideoComment[] = []
26 highlightedThread: VideoComment
27 sort: VideoSortField = '-createdAt'
28 componentPagination: ComponentPagination = {
33 inReplyToCommentId: number
34 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
35 threadLoading: { [ id: number ]: boolean } = {}
37 private sub: Subscription
40 private authService: AuthService,
41 private notificationsService: NotificationsService,
42 private confirmService: ConfirmService,
43 private videoCommentService: VideoCommentService,
44 private activatedRoute: ActivatedRoute,
49 // Find highlighted comment in params
50 this.sub = this.activatedRoute.params.subscribe(
52 if (params['threadId']) {
53 const highlightedThreadId = +params['threadId']
54 this.processHighlightedThread(highlightedThreadId)
60 ngOnChanges (changes: SimpleChanges) {
61 if (changes['video']) {
67 if (this.sub) this.sub.unsubscribe()
70 viewReplies (commentId: number, highlightThread = false) {
71 this.threadLoading[commentId] = true
73 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
76 this.threadComments[commentId] = res
77 this.threadLoading[commentId] = false
79 if (highlightThread) this.highlightedThread = new VideoComment(res.comment)
82 err => this.notificationsService.error(this.i18n('Error'), err.message)
87 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
90 this.comments = this.comments.concat(res.comments)
91 this.componentPagination.totalItems = res.totalComments
94 err => this.notificationsService.error(this.i18n('Error'), err.message)
98 onCommentThreadCreated (comment: VideoComment) {
99 this.comments.unshift(comment)
102 onWantedToReply (comment: VideoComment) {
103 this.inReplyToCommentId = comment.id
107 this.inReplyToCommentId = undefined
110 onThreadCreated (commentTree: VideoCommentThreadTree) {
111 this.viewReplies(commentTree.comment.id)
114 async onWantedToDelete (commentToDelete: VideoComment) {
115 let message = 'Do you really want to delete this comment?'
116 if (commentToDelete.totalReplies !== 0) {
117 message += this.i18n(' {{ totalReplies }} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
120 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
121 if (res === false) return
123 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
126 // Delete the comment in the tree
127 if (commentToDelete.inReplyToCommentId) {
128 const thread = this.threadComments[commentToDelete.threadId]
130 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
134 this.deleteLocalCommentThread(thread, commentToDelete)
139 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
140 this.componentPagination.totalItems--
143 err => this.notificationsService.error(this.i18n('Error'), err.message)
148 return this.authService.isLoggedIn()
152 this.componentPagination.currentPage++
154 if (this.hasMoreComments()) {
155 this.loadMoreComments()
159 private hasMoreComments () {
161 if (this.componentPagination.totalItems === 0) return false
164 if (!this.componentPagination.totalItems) return true
166 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
167 return maxPage > this.componentPagination.currentPage
170 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
171 for (const commentChild of parentComment.children) {
172 if (commentChild.comment.id === commentToDelete.id) {
173 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
177 this.deleteLocalCommentThread(commentChild, commentToDelete)
181 private resetVideo () {
182 if (this.video.commentsEnabled === true) {
183 // Reset all our fields
184 this.highlightedThread = null
186 this.threadComments = {}
187 this.threadLoading = {}
188 this.inReplyToCommentId = undefined
189 this.componentPagination.currentPage = 1
190 this.componentPagination.totalItems = null
192 this.loadMoreComments()
196 private processHighlightedThread (highlightedThreadId: number) {
197 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
199 const highlightThread = true
200 this.viewReplies(highlightedThreadId, highlightThread)