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 commentReplyRedraftValue: string
31 commentThreadRedraftValue: string
32 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
33 threadLoading: { [ id: number ]: boolean } = {}
35 syndicationItems: Syndication[] = []
37 onDataSubject = new Subject<any[]>()
39 private sub: Subscription
42 private authService: AuthService,
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private videoCommentService: VideoCommentService,
46 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)
136 this.commentThreadRedraftValue = undefined
139 onWantedToReply (comment: VideoComment) {
140 this.inReplyToCommentId = comment.id
144 this.inReplyToCommentId = undefined
145 this.commentReplyRedraftValue = undefined
148 onThreadCreated (commentTree: VideoCommentThreadTree) {
149 this.viewReplies(commentTree.comment.id)
152 handleSortChange (sort: string) {
153 if (this.sort === sort) return
159 handleTimestampClicked (timestamp: number) {
160 this.timestampClicked.emit(timestamp)
163 async onWantedToDelete (
164 commentToDelete: VideoComment,
165 title = $localize`Delete`,
166 message = $localize`Do you really want to delete this comment?`
167 ): Promise<boolean> {
168 if (commentToDelete.isLocal || this.video.isLocal) {
169 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
171 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
174 const res = await this.confirmService.confirm(message, title)
175 if (res === false) return false
177 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
180 if (this.highlightedThread?.id === commentToDelete.id) {
181 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
183 this.highlightedThread = undefined
186 // Mark the comment as deleted
187 this.softDeleteComment(commentToDelete)
190 err => this.notifier.error(err.message)
196 async onWantedToRedraft (commentToRedraft: VideoComment) {
197 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
200 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
202 // Display <br /> tag as a break line for editing
203 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
205 if (commentToRedraft.threadId === commentToRedraft.id) {
206 this.commentThreadRedraftValue = commentToRedraftText
208 this.commentReplyRedraftValue = commentToRedraftText
215 return this.authService.isLoggedIn()
219 if (hasMoreItems(this.componentPagination)) {
220 this.componentPagination.currentPage++
221 this.loadMoreThreads()
225 private softDeleteComment (comment: VideoComment) {
226 comment.isDeleted = true
227 comment.deletedAt = new Date()
229 comment.account = null
232 private resetVideo () {
233 if (this.video.commentsEnabled === true) {
234 // Reset all our fields
235 this.highlightedThread = null
237 this.threadComments = {}
238 this.threadLoading = {}
239 this.inReplyToCommentId = undefined
240 this.componentPagination.currentPage = 1
241 this.componentPagination.totalItems = null
243 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
244 this.loadMoreThreads()
248 private processHighlightedThread (highlightedThreadId: number) {
249 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
251 const highlightThread = true
252 this.viewReplies(highlightedThreadId, highlightThread)