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
26 componentPagination: ComponentPagination = {
31 totalNotDeletedComments: number
33 inReplyToCommentId: number
34 commentReplyRedraftValue: string
35 commentThreadRedraftValue: string
37 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
38 threadLoading: { [ id: number ]: boolean } = {}
40 syndicationItems: Syndication[] = []
42 onDataSubject = new Subject<any[]>()
44 private sub: Subscription
47 private authService: AuthService,
48 private notifier: Notifier,
49 private confirmService: ConfirmService,
50 private videoCommentService: VideoCommentService,
51 private activatedRoute: ActivatedRoute,
52 private hooks: HooksService
56 // Find highlighted comment in params
57 this.sub = this.activatedRoute.params.subscribe(
59 if (params['threadId']) {
60 const highlightedThreadId = +params['threadId']
61 this.processHighlightedThread(highlightedThreadId)
67 ngOnChanges (changes: SimpleChanges) {
68 if (changes['video']) {
74 if (this.sub) this.sub.unsubscribe()
77 viewReplies (commentId: number, highlightThread = false) {
78 this.threadLoading[commentId] = true
81 videoId: this.video.uuid,
85 const obs = this.hooks.wrapObsFun(
86 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
89 'filter:api.video-watch.video-thread-replies.list.params',
90 'filter:api.video-watch.video-thread-replies.list.result'
95 this.threadComments[commentId] = res
96 this.threadLoading[commentId] = false
97 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
99 if (highlightThread) {
100 this.highlightedThread = new VideoComment(res.comment)
102 // Scroll to the highlighted thread
103 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
107 error: err => this.notifier.error(err.message)
113 videoId: this.video.uuid,
114 componentPagination: this.componentPagination,
118 const obs = this.hooks.wrapObsFun(
119 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
122 'filter:api.video-watch.video-threads.list.params',
123 'filter:api.video-watch.video-threads.list.result'
128 this.comments = this.comments.concat(res.data)
129 this.componentPagination.totalItems = res.total
130 this.totalNotDeletedComments = res.totalNotDeletedComments
132 this.onDataSubject.next(res.data)
134 setTimeout(() => this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }))
137 error: err => this.notifier.error(err.message)
141 onCommentThreadCreated (comment: VideoComment) {
142 this.comments.unshift(comment)
143 this.commentThreadRedraftValue = undefined
146 onWantedToReply (comment: VideoComment) {
147 this.inReplyToCommentId = comment.id
151 this.inReplyToCommentId = undefined
152 this.commentReplyRedraftValue = undefined
155 onThreadCreated (commentTree: VideoCommentThreadTree) {
156 this.viewReplies(commentTree.comment.id)
159 handleSortChange (sort: string) {
160 if (this.sort === sort) return
166 handleTimestampClicked (timestamp: number) {
167 this.timestampClicked.emit(timestamp)
170 async onWantedToDelete (
171 commentToDelete: VideoComment,
172 title = $localize`Delete`,
173 message = $localize`Do you really want to delete this comment?`
174 ): Promise<boolean> {
175 if (commentToDelete.isLocal || this.video.isLocal) {
176 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
178 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
181 const res = await this.confirmService.confirm(message, title)
182 if (res === false) return false
184 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
187 if (this.highlightedThread?.id === commentToDelete.id) {
188 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
190 this.highlightedThread = undefined
193 // Mark the comment as deleted
194 this.softDeleteComment(commentToDelete)
197 error: err => this.notifier.error(err.message)
203 async onWantedToRedraft (commentToRedraft: VideoComment) {
204 const confirm = await this.onWantedToDelete(
206 $localize`Delete and re-draft`,
207 $localize`Do you really want to delete and re-draft this comment?`
211 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
213 // Restore line feed for editing
214 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
216 if (commentToRedraft.threadId === commentToRedraft.id) {
217 this.commentThreadRedraftValue = commentToRedraftText
219 this.commentReplyRedraftValue = commentToRedraftText
226 return this.authService.isLoggedIn()
230 if (hasMoreItems(this.componentPagination)) {
231 this.componentPagination.currentPage++
232 this.loadMoreThreads()
236 private softDeleteComment (comment: VideoComment) {
237 comment.isDeleted = true
238 comment.deletedAt = new Date()
240 comment.account = null
243 private resetVideo () {
244 if (this.video.commentsEnabled === true) {
245 // Reset all our fields
246 this.highlightedThread = null
248 this.threadComments = {}
249 this.threadLoading = {}
250 this.inReplyToCommentId = undefined
251 this.componentPagination.currentPage = 1
252 this.componentPagination.totalItems = null
253 this.totalNotDeletedComments = null
255 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
256 this.loadMoreThreads()
260 private processHighlightedThread (highlightedThreadId: number) {
261 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
263 const highlightThread = true
264 this.viewReplies(highlightedThreadId, highlightThread)