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)
133 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
136 error: 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 error: err => this.notifier.error(err.message)
202 async onWantedToRedraft (commentToRedraft: VideoComment) {
203 const confirm = await this.onWantedToDelete(
205 $localize`Delete and re-draft`,
206 $localize`Do you really want to delete and re-draft this comment?`
210 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
212 // Restore line feed for editing
213 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
215 if (commentToRedraft.threadId === commentToRedraft.id) {
216 this.commentThreadRedraftValue = commentToRedraftText
218 this.commentReplyRedraftValue = commentToRedraftText
225 return this.authService.isLoggedIn()
229 if (hasMoreItems(this.componentPagination)) {
230 this.componentPagination.currentPage++
231 this.loadMoreThreads()
235 private softDeleteComment (comment: VideoComment) {
236 comment.isDeleted = true
237 comment.deletedAt = new Date()
239 comment.account = null
242 private resetVideo () {
243 if (this.video.commentsEnabled === true) {
244 // Reset all our fields
245 this.highlightedThread = null
247 this.threadComments = {}
248 this.threadLoading = {}
249 this.inReplyToCommentId = undefined
250 this.componentPagination.currentPage = 1
251 this.componentPagination.totalItems = null
252 this.totalNotDeletedComments = null
254 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
255 this.loadMoreThreads()
259 private processHighlightedThread (highlightedThreadId: number) {
260 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
262 const highlightThread = true
263 this.viewReplies(highlightedThreadId, highlightThread)