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'
15 import { HooksService } from '@app/core/plugins/hooks.service'
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 notifier: Notifier,
46 private confirmService: ConfirmService,
47 private videoCommentService: VideoCommentService,
48 private activatedRoute: ActivatedRoute,
50 private hooks: HooksService
54 // Find highlighted comment in params
55 this.sub = this.activatedRoute.params.subscribe(
57 if (params['threadId']) {
58 const highlightedThreadId = +params['threadId']
59 this.processHighlightedThread(highlightedThreadId)
65 ngOnChanges (changes: SimpleChanges) {
66 if (changes['video']) {
72 if (this.sub) this.sub.unsubscribe()
75 viewReplies (commentId: number, highlightThread = false) {
76 this.threadLoading[commentId] = true
79 videoId: this.video.id,
83 const obs = this.hooks.wrapObsFun(
84 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
87 'filter:api.video-watch.video-thread-replies.list.params',
88 'filter:api.video-watch.video-thread-replies.list.result'
93 this.threadComments[commentId] = res
94 this.threadLoading[commentId] = false
96 if (highlightThread) {
97 this.highlightedThread = new VideoComment(res.comment)
99 // Scroll to the highlighted thread
100 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
104 err => this.notifier.error(err.message)
110 videoId: this.video.id,
111 componentPagination: this.componentPagination,
115 const obs = this.hooks.wrapObsFun(
116 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
119 'filter:api.video-watch.video-threads.list.params',
120 'filter:api.video-watch.video-threads.list.result'
125 this.comments = this.comments.concat(res.data)
126 this.componentPagination.totalItems = res.total
129 err => this.notifier.error(err.message)
133 onCommentThreadCreated (comment: VideoComment) {
134 this.comments.unshift(comment)
137 onWantedToReply (comment: VideoComment) {
138 this.inReplyToCommentId = comment.id
142 this.inReplyToCommentId = undefined
145 onThreadCreated (commentTree: VideoCommentThreadTree) {
146 this.viewReplies(commentTree.comment.id)
149 async onWantedToDelete (commentToDelete: VideoComment) {
150 let message = 'Do you really want to delete this comment?'
152 if (commentToDelete.totalReplies !== 0) {
153 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
156 if (commentToDelete.isLocal) {
157 message += this.i18n(' The deletion will be sent to remote instances so they remove the comment too.')
159 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
162 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
163 if (res === false) return
165 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
168 // Delete the comment in the tree
169 if (commentToDelete.inReplyToCommentId) {
170 const thread = this.threadComments[commentToDelete.threadId]
172 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
176 this.deleteLocalCommentThread(thread, commentToDelete)
181 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
182 this.componentPagination.totalItems--
184 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
187 err => this.notifier.error(err.message)
192 return this.authService.isLoggedIn()
196 this.componentPagination.currentPage++
198 if (hasMoreItems(this.componentPagination)) {
199 this.loadMoreThreads()
203 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
204 for (const commentChild of parentComment.children) {
205 if (commentChild.comment.id === commentToDelete.id) {
206 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
210 this.deleteLocalCommentThread(commentChild, commentToDelete)
214 private resetVideo () {
215 if (this.video.commentsEnabled === true) {
216 // Reset all our fields
217 this.highlightedThread = null
219 this.threadComments = {}
220 this.threadLoading = {}
221 this.inReplyToCommentId = undefined
222 this.componentPagination.currentPage = 1
223 this.componentPagination.totalItems = null
225 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
227 this.loadMoreThreads()
231 private processHighlightedThread (highlightedThreadId: number) {
232 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
234 const highlightThread = true
235 this.viewReplies(highlightedThreadId, highlightThread)