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 { I18n } from '@ngx-translate/i18n-polyfill'
8 import { VideoCommentThreadTree } from './video-comment-thread-tree.model'
9 import { VideoComment } from './video-comment.model'
10 import { VideoCommentService } from './video-comment.service'
13 selector: 'my-video-comments',
14 templateUrl: './video-comments.component.html',
15 styleUrls: ['./video-comments.component.scss']
17 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
18 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
19 @Input() video: VideoDetails
22 @Output() timestampClicked = new EventEmitter<number>()
24 comments: VideoComment[] = []
25 highlightedThread: VideoComment
27 componentPagination: ComponentPagination = {
32 inReplyToCommentId: number
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,
49 private hooks: HooksService
53 // Find highlighted comment in params
54 this.sub = this.activatedRoute.params.subscribe(
56 if (params['threadId']) {
57 const highlightedThreadId = +params['threadId']
58 this.processHighlightedThread(highlightedThreadId)
64 ngOnChanges (changes: SimpleChanges) {
65 if (changes['video']) {
71 if (this.sub) this.sub.unsubscribe()
74 viewReplies (commentId: number, highlightThread = false) {
75 this.threadLoading[commentId] = true
78 videoId: this.video.id,
82 const obs = this.hooks.wrapObsFun(
83 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
86 'filter:api.video-watch.video-thread-replies.list.params',
87 'filter:api.video-watch.video-thread-replies.list.result'
92 this.threadComments[commentId] = res
93 this.threadLoading[commentId] = false
94 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
96 if (highlightThread) {
97 this.highlightedThread = new VideoComment(res.comment)
99 // Scroll to the highlighted thread
100 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
104 err => this.notifier.error(err.message)
110 videoId: this.video.id,
111 componentPagination: this.componentPagination,
115 const obs = this.hooks.wrapObsFun(
116 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
119 'filter:api.video-watch.video-threads.list.params',
120 'filter:api.video-watch.video-threads.list.result'
125 this.comments = this.comments.concat(res.data)
126 this.componentPagination.totalItems = res.total
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)
140 onWantedToReply (comment: VideoComment) {
141 this.inReplyToCommentId = comment.id
145 this.inReplyToCommentId = 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 (commentToDelete: VideoComment) {
164 let message = 'Do you really want to delete this comment?'
166 if (commentToDelete.isLocal || this.video.isLocal) {
167 message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.')
169 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
172 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
173 if (res === false) return
175 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
178 if (this.highlightedThread?.id === commentToDelete.id) {
179 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
181 this.highlightedThread = undefined
184 // Mark the comment as deleted
185 this.softDeleteComment(commentToDelete)
188 err => this.notifier.error(err.message)
193 return this.authService.isLoggedIn()
197 if (hasMoreItems(this.componentPagination)) {
198 this.componentPagination.currentPage++
199 this.loadMoreThreads()
203 private softDeleteComment (comment: VideoComment) {
204 comment.isDeleted = true
205 comment.deletedAt = new Date()
207 comment.account = null
210 private resetVideo () {
211 if (this.video.commentsEnabled === true) {
212 // Reset all our fields
213 this.highlightedThread = null
215 this.threadComments = {}
216 this.threadLoading = {}
217 this.inReplyToCommentId = undefined
218 this.componentPagination.currentPage = 1
219 this.componentPagination.totalItems = null
221 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
222 this.loadMoreThreads()
226 private processHighlightedThread (highlightedThreadId: number) {
227 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
229 const highlightThread = true
230 this.viewReplies(highlightedThreadId, highlightThread)