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.id,
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 err => this.notifier.error(err.message)
113 videoId: this.video.id,
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)
133 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
136 err => this.notifier.error(err.message)
140 onCommentThreadCreated (comment: VideoComment) {
141 this.comments.unshift(comment)
142 this.commentThreadRedraftValue = undefined
145 onWantedToReply (comment: VideoComment) {
146 this.inReplyToCommentId = comment.id
150 this.inReplyToCommentId = undefined
151 this.commentReplyRedraftValue = undefined
154 onThreadCreated (commentTree: VideoCommentThreadTree) {
155 this.viewReplies(commentTree.comment.id)
158 handleSortChange (sort: string) {
159 if (this.sort === sort) return
165 handleTimestampClicked (timestamp: number) {
166 this.timestampClicked.emit(timestamp)
169 async onWantedToDelete (
170 commentToDelete: VideoComment,
171 title = $localize`Delete`,
172 message = $localize`Do you really want to delete this comment?`
173 ): Promise<boolean> {
174 if (commentToDelete.isLocal || this.video.isLocal) {
175 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
177 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
180 const res = await this.confirmService.confirm(message, title)
181 if (res === false) return false
183 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
186 if (this.highlightedThread?.id === commentToDelete.id) {
187 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
189 this.highlightedThread = undefined
192 // Mark the comment as deleted
193 this.softDeleteComment(commentToDelete)
196 err => this.notifier.error(err.message)
202 async onWantedToRedraft (commentToRedraft: VideoComment) {
203 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
206 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
208 // Restore line feed for editing
209 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
211 if (commentToRedraft.threadId === commentToRedraft.id) {
212 this.commentThreadRedraftValue = commentToRedraftText
214 this.commentReplyRedraftValue = commentToRedraftText
221 return this.authService.isLoggedIn()
225 if (hasMoreItems(this.componentPagination)) {
226 this.componentPagination.currentPage++
227 this.loadMoreThreads()
231 private softDeleteComment (comment: VideoComment) {
232 comment.isDeleted = true
233 comment.deletedAt = new Date()
235 comment.account = null
238 private resetVideo () {
239 if (this.video.commentsEnabled === true) {
240 // Reset all our fields
241 this.highlightedThread = null
243 this.threadComments = {}
244 this.threadLoading = {}
245 this.inReplyToCommentId = undefined
246 this.componentPagination.currentPage = 1
247 this.componentPagination.totalItems = null
248 this.totalNotDeletedComments = null
250 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
251 this.loadMoreThreads()
255 private processHighlightedThread (highlightedThreadId: number) {
256 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
258 const highlightThread = true
259 this.viewReplies(highlightedThreadId, highlightThread)