1 import { Subject, Subscription } from 'rxjs'
2 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, User } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { Syndication, VideoDetails } from '@app/shared/shared-main'
7 import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
11 selector: 'my-video-comments',
12 templateUrl: './video-comments.component.html',
13 styleUrls: ['./video-comments.component.scss']
15 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
16 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
17 @Input() video: VideoDetails
20 @Output() timestampClicked = new EventEmitter<number>()
22 comments: VideoComment[] = []
23 highlightedThread: VideoComment
25 componentPagination: ComponentPagination = {
30 inReplyToCommentId: number
31 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
32 threadLoading: { [ id: number ]: boolean } = {}
34 syndicationItems: Syndication[] = []
36 onDataSubject = new Subject<any[]>()
38 private sub: Subscription
41 private authService: AuthService,
42 private notifier: Notifier,
43 private confirmService: ConfirmService,
44 private videoCommentService: VideoCommentService,
45 private activatedRoute: ActivatedRoute,
47 private hooks: HooksService
51 // Find highlighted comment in params
52 this.sub = this.activatedRoute.params.subscribe(
54 if (params['threadId']) {
55 const highlightedThreadId = +params['threadId']
56 this.processHighlightedThread(highlightedThreadId)
62 ngOnChanges (changes: SimpleChanges) {
63 if (changes['video']) {
69 if (this.sub) this.sub.unsubscribe()
72 viewReplies (commentId: number, highlightThread = false) {
73 this.threadLoading[commentId] = true
76 videoId: this.video.id,
80 const obs = this.hooks.wrapObsFun(
81 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
84 'filter:api.video-watch.video-thread-replies.list.params',
85 'filter:api.video-watch.video-thread-replies.list.result'
90 this.threadComments[commentId] = res
91 this.threadLoading[commentId] = false
92 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
94 if (highlightThread) {
95 this.highlightedThread = new VideoComment(res.comment)
97 // Scroll to the highlighted thread
98 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
102 err => this.notifier.error(err.message)
108 videoId: this.video.id,
109 componentPagination: this.componentPagination,
113 const obs = this.hooks.wrapObsFun(
114 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
117 'filter:api.video-watch.video-threads.list.params',
118 'filter:api.video-watch.video-threads.list.result'
123 this.comments = this.comments.concat(res.data)
124 this.componentPagination.totalItems = res.total
126 this.onDataSubject.next(res.data)
127 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
130 err => this.notifier.error(err.message)
134 onCommentThreadCreated (comment: VideoComment) {
135 this.comments.unshift(comment)
138 onWantedToReply (comment: VideoComment) {
139 this.inReplyToCommentId = comment.id
143 this.inReplyToCommentId = undefined
146 onThreadCreated (commentTree: VideoCommentThreadTree) {
147 this.viewReplies(commentTree.comment.id)
150 handleSortChange (sort: string) {
151 if (this.sort === sort) return
157 handleTimestampClicked (timestamp: number) {
158 this.timestampClicked.emit(timestamp)
161 async onWantedToDelete (commentToDelete: VideoComment) {
162 let message = 'Do you really want to delete this comment?'
164 if (commentToDelete.isLocal || this.video.isLocal) {
165 message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.')
167 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
170 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
171 if (res === false) return
173 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
176 if (this.highlightedThread?.id === commentToDelete.id) {
177 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
179 this.highlightedThread = undefined
182 // Mark the comment as deleted
183 this.softDeleteComment(commentToDelete)
186 err => this.notifier.error(err.message)
191 return this.authService.isLoggedIn()
195 if (hasMoreItems(this.componentPagination)) {
196 this.componentPagination.currentPage++
197 this.loadMoreThreads()
201 private softDeleteComment (comment: VideoComment) {
202 comment.isDeleted = true
203 comment.deletedAt = new Date()
205 comment.account = null
208 private resetVideo () {
209 if (this.video.commentsEnabled === true) {
210 // Reset all our fields
211 this.highlightedThread = null
213 this.threadComments = {}
214 this.threadLoading = {}
215 this.inReplyToCommentId = undefined
216 this.componentPagination.currentPage = 1
217 this.componentPagination.totalItems = null
219 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
220 this.loadMoreThreads()
224 private processHighlightedThread (highlightedThreadId: number) {
225 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
227 const highlightThread = true
228 this.viewReplies(highlightedThreadId, highlightThread)