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
25 componentPagination: ComponentPagination = {
30 inReplyToCommentId: number
31 commentReplyRedraftValue: string
32 commentThreadRedraftValue: string
33 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
34 threadLoading: { [ id: number ]: boolean } = {}
36 syndicationItems: Syndication[] = []
38 onDataSubject = new Subject<any[]>()
40 private sub: Subscription
43 private authService: AuthService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private videoCommentService: VideoCommentService,
47 private activatedRoute: ActivatedRoute,
48 private hooks: HooksService
52 // Find highlighted comment in params
53 this.sub = this.activatedRoute.params.subscribe(
55 if (params['threadId']) {
56 const highlightedThreadId = +params['threadId']
57 this.processHighlightedThread(highlightedThreadId)
63 ngOnChanges (changes: SimpleChanges) {
64 if (changes['video']) {
70 if (this.sub) this.sub.unsubscribe()
73 viewReplies (commentId: number, highlightThread = false) {
74 this.threadLoading[commentId] = true
77 videoId: this.video.id,
81 const obs = this.hooks.wrapObsFun(
82 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
85 'filter:api.video-watch.video-thread-replies.list.params',
86 'filter:api.video-watch.video-thread-replies.list.result'
91 this.threadComments[commentId] = res
92 this.threadLoading[commentId] = false
93 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
95 if (highlightThread) {
96 this.highlightedThread = new VideoComment(res.comment)
98 // Scroll to the highlighted thread
99 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
103 err => this.notifier.error(err.message)
109 videoId: this.video.id,
110 componentPagination: this.componentPagination,
114 const obs = this.hooks.wrapObsFun(
115 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
118 'filter:api.video-watch.video-threads.list.params',
119 'filter:api.video-watch.video-threads.list.result'
124 this.comments = this.comments.concat(res.data)
125 // Client does not display removed comments
126 this.componentPagination.totalItems = res.total - this.comments.filter(c => c.isDeleted).length
128 this.onDataSubject.next(res.data)
129 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
132 err => this.notifier.error(err.message)
136 onCommentThreadCreated (comment: VideoComment) {
137 this.comments.unshift(comment)
138 this.commentThreadRedraftValue = undefined
141 onWantedToReply (comment: VideoComment) {
142 this.inReplyToCommentId = comment.id
146 this.inReplyToCommentId = undefined
147 this.commentReplyRedraftValue = undefined
150 onThreadCreated (commentTree: VideoCommentThreadTree) {
151 this.viewReplies(commentTree.comment.id)
154 handleSortChange (sort: string) {
155 if (this.sort === sort) return
161 handleTimestampClicked (timestamp: number) {
162 this.timestampClicked.emit(timestamp)
165 async onWantedToDelete (
166 commentToDelete: VideoComment,
167 title = $localize`Delete`,
168 message = $localize`Do you really want to delete this comment?`
169 ): Promise<boolean> {
170 if (commentToDelete.isLocal || this.video.isLocal) {
171 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
173 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
176 const res = await this.confirmService.confirm(message, title)
177 if (res === false) return false
179 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
182 if (this.highlightedThread?.id === commentToDelete.id) {
183 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
185 this.highlightedThread = undefined
188 // Mark the comment as deleted
189 this.softDeleteComment(commentToDelete)
192 err => this.notifier.error(err.message)
198 async onWantedToRedraft (commentToRedraft: VideoComment) {
199 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
202 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
204 // Restore line feed for editing
205 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
207 if (commentToRedraft.threadId === commentToRedraft.id) {
208 this.commentThreadRedraftValue = commentToRedraftText
210 this.commentReplyRedraftValue = commentToRedraftText
217 return this.authService.isLoggedIn()
221 if (hasMoreItems(this.componentPagination)) {
222 this.componentPagination.currentPage++
223 this.loadMoreThreads()
227 private softDeleteComment (comment: VideoComment) {
228 comment.isDeleted = true
229 comment.deletedAt = new Date()
231 comment.account = null
234 private resetVideo () {
235 if (this.video.commentsEnabled === true) {
236 // Reset all our fields
237 this.highlightedThread = null
239 this.threadComments = {}
240 this.threadLoading = {}
241 this.inReplyToCommentId = undefined
242 this.componentPagination.currentPage = 1
243 this.componentPagination.totalItems = null
245 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
246 this.loadMoreThreads()
250 private processHighlightedThread (highlightedThreadId: number) {
251 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
253 const highlightThread = true
254 this.viewReplies(highlightedThreadId, highlightThread)