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 { ThisReceiver } from '@angular/compiler'
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
27 componentPagination: ComponentPagination = {
32 totalNotDeletedComments: number
34 inReplyToCommentId: number
35 commentReplyRedraftValue: string
36 commentThreadRedraftValue: string
38 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
39 threadLoading: { [ id: number ]: boolean } = {}
41 syndicationItems: Syndication[] = []
43 onDataSubject = new Subject<any[]>()
45 private sub: Subscription
48 private authService: AuthService,
49 private notifier: Notifier,
50 private confirmService: ConfirmService,
51 private videoCommentService: VideoCommentService,
52 private activatedRoute: ActivatedRoute,
53 private hooks: HooksService
57 // Find highlighted comment in params
58 this.sub = this.activatedRoute.params.subscribe(
60 if (params['threadId']) {
61 const highlightedThreadId = +params['threadId']
62 this.processHighlightedThread(highlightedThreadId)
68 ngOnChanges (changes: SimpleChanges) {
69 if (changes['video']) {
75 if (this.sub) this.sub.unsubscribe()
78 viewReplies (commentId: number, highlightThread = false) {
79 this.threadLoading[commentId] = true
82 videoId: this.video.id,
86 const obs = this.hooks.wrapObsFun(
87 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
90 'filter:api.video-watch.video-thread-replies.list.params',
91 'filter:api.video-watch.video-thread-replies.list.result'
96 this.threadComments[commentId] = res
97 this.threadLoading[commentId] = false
98 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
100 if (highlightThread) {
101 this.highlightedThread = new VideoComment(res.comment)
103 // Scroll to the highlighted thread
104 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
108 err => this.notifier.error(err.message)
114 videoId: this.video.id,
115 componentPagination: this.componentPagination,
119 const obs = this.hooks.wrapObsFun(
120 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
123 'filter:api.video-watch.video-threads.list.params',
124 'filter:api.video-watch.video-threads.list.result'
129 this.comments = this.comments.concat(res.data)
130 this.componentPagination.totalItems = res.total
131 this.totalNotDeletedComments = res.totalNotDeletedComments
133 this.onDataSubject.next(res.data)
134 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
137 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 err => this.notifier.error(err.message)
203 async onWantedToRedraft (commentToRedraft: VideoComment) {
204 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
207 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
209 // Restore line feed for editing
210 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
212 if (commentToRedraft.threadId === commentToRedraft.id) {
213 this.commentThreadRedraftValue = commentToRedraftText
215 this.commentReplyRedraftValue = commentToRedraftText
222 return this.authService.isLoggedIn()
226 if (hasMoreItems(this.componentPagination)) {
227 this.componentPagination.currentPage++
228 this.loadMoreThreads()
232 private softDeleteComment (comment: VideoComment) {
233 comment.isDeleted = true
234 comment.deletedAt = new Date()
236 comment.account = null
239 private resetVideo () {
240 if (this.video.commentsEnabled === true) {
241 // Reset all our fields
242 this.highlightedThread = null
244 this.threadComments = {}
245 this.threadLoading = {}
246 this.inReplyToCommentId = undefined
247 this.componentPagination.currentPage = 1
248 this.componentPagination.totalItems = null
249 this.totalNotDeletedComments = null
251 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
252 this.loadMoreThreads()
256 private processHighlightedThread (highlightedThreadId: number) {
257 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
259 const highlightThread = true
260 this.viewReplies(highlightedThreadId, highlightThread)