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'
10 selector: 'my-video-comments',
11 templateUrl: './video-comments.component.html',
12 styleUrls: ['./video-comments.component.scss']
14 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
15 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
16 @Input() video: VideoDetails
19 @Output() timestampClicked = new EventEmitter<number>()
21 comments: VideoComment[] = []
22 highlightedThread: VideoComment
24 componentPagination: ComponentPagination = {
29 inReplyToCommentId: number
30 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
31 threadLoading: { [ id: number ]: boolean } = {}
33 syndicationItems: Syndication[] = []
35 onDataSubject = new Subject<any[]>()
37 private sub: Subscription
40 private authService: AuthService,
41 private notifier: Notifier,
42 private confirmService: ConfirmService,
43 private videoCommentService: VideoCommentService,
44 private activatedRoute: ActivatedRoute,
45 private hooks: HooksService
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
74 videoId: this.video.id,
78 const obs = this.hooks.wrapObsFun(
79 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
82 'filter:api.video-watch.video-thread-replies.list.params',
83 'filter:api.video-watch.video-thread-replies.list.result'
88 this.threadComments[commentId] = res
89 this.threadLoading[commentId] = false
90 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
92 if (highlightThread) {
93 this.highlightedThread = new VideoComment(res.comment)
95 // Scroll to the highlighted thread
96 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
100 err => this.notifier.error(err.message)
106 videoId: this.video.id,
107 componentPagination: this.componentPagination,
111 const obs = this.hooks.wrapObsFun(
112 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
115 'filter:api.video-watch.video-threads.list.params',
116 'filter:api.video-watch.video-threads.list.result'
121 this.comments = this.comments.concat(res.data)
122 this.componentPagination.totalItems = res.total
124 this.onDataSubject.next(res.data)
125 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
128 err => this.notifier.error(err.message)
132 onCommentThreadCreated (comment: VideoComment) {
133 this.comments.unshift(comment)
136 onWantedToReply (comment: VideoComment) {
137 this.inReplyToCommentId = comment.id
141 this.inReplyToCommentId = undefined
144 onThreadCreated (commentTree: VideoCommentThreadTree) {
145 this.viewReplies(commentTree.comment.id)
148 handleSortChange (sort: string) {
149 if (this.sort === sort) return
155 handleTimestampClicked (timestamp: number) {
156 this.timestampClicked.emit(timestamp)
159 async onWantedToDelete (commentToDelete: VideoComment) {
160 let message = 'Do you really want to delete this comment?'
162 if (commentToDelete.isLocal || this.video.isLocal) {
163 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
165 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
168 const res = await this.confirmService.confirm(message, $localize`Delete`)
169 if (res === false) return
171 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
174 if (this.highlightedThread?.id === commentToDelete.id) {
175 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
177 this.highlightedThread = undefined
180 // Mark the comment as deleted
181 this.softDeleteComment(commentToDelete)
184 err => this.notifier.error(err.message)
189 return this.authService.isLoggedIn()
193 if (hasMoreItems(this.componentPagination)) {
194 this.componentPagination.currentPage++
195 this.loadMoreThreads()
199 private softDeleteComment (comment: VideoComment) {
200 comment.isDeleted = true
201 comment.deletedAt = new Date()
203 comment.account = null
206 private resetVideo () {
207 if (this.video.commentsEnabled === true) {
208 // Reset all our fields
209 this.highlightedThread = null
211 this.threadComments = {}
212 this.threadLoading = {}
213 this.inReplyToCommentId = undefined
214 this.componentPagination.currentPage = 1
215 this.componentPagination.totalItems = null
217 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
218 this.loadMoreThreads()
222 private processHighlightedThread (highlightedThreadId: number) {
223 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
225 const highlightThread = true
226 this.viewReplies(highlightedThreadId, highlightThread)