1 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { ConfirmService, Notifier } from '@app/core'
4 import { Subject, Subscription } from 'rxjs'
5 import { AuthService } from '../../../core/auth'
6 import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model'
7 import { User } from '../../../shared/users'
8 import { CommentSortField } from '../../../shared/video/sort-field.type'
9 import { VideoDetails } from '../../../shared/video/video-details.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { Syndication } from '@app/shared/video/syndication.model'
14 import { HooksService } from '@app/core/plugins/hooks.service'
15 import { VideoCommentThreadTree } from '@app/videos/+video-watch/comment/video-comment-thread-tree.model'
18 selector: 'my-video-comments',
19 templateUrl: './video-comments.component.html',
20 styleUrls: ['./video-comments.component.scss']
22 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
23 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
24 @Input() video: VideoDetails
27 @Output() timestampClicked = new EventEmitter<number>()
29 comments: VideoComment[] = []
30 highlightedThread: VideoComment
31 sort: CommentSortField = '-createdAt'
32 componentPagination: ComponentPagination = {
37 inReplyToCommentId: number
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,
54 private hooks: HooksService
58 // Find highlighted comment in params
59 this.sub = this.activatedRoute.params.subscribe(
61 if (params['threadId']) {
62 const highlightedThreadId = +params['threadId']
63 this.processHighlightedThread(highlightedThreadId)
69 ngOnChanges (changes: SimpleChanges) {
70 if (changes['video']) {
76 if (this.sub) this.sub.unsubscribe()
79 viewReplies (commentId: number, highlightThread = false) {
80 this.threadLoading[commentId] = true
83 videoId: this.video.id,
87 const obs = this.hooks.wrapObsFun(
88 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
91 'filter:api.video-watch.video-thread-replies.list.params',
92 'filter:api.video-watch.video-thread-replies.list.result'
97 this.threadComments[commentId] = res
98 this.threadLoading[commentId] = false
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
132 this.onDataSubject.next(res.data)
133 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
136 err => this.notifier.error(err.message)
140 onCommentThreadCreated (comment: VideoComment) {
141 this.comments.unshift(comment)
144 onWantedToReply (comment: VideoComment) {
145 this.inReplyToCommentId = comment.id
149 this.inReplyToCommentId = undefined
152 onThreadCreated (commentTree: VideoCommentThreadTree) {
153 this.viewReplies(commentTree.comment.id)
156 handleSortChange (sort: CommentSortField) {
157 if (this.sort === sort) return
163 handleTimestampClicked (timestamp: number) {
164 this.timestampClicked.emit(timestamp)
167 async onWantedToDelete (commentToDelete: VideoComment) {
168 let message = 'Do you really want to delete this comment?'
170 if (commentToDelete.isLocal) {
171 message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.')
173 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
176 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
177 if (res === false) return
179 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
182 // Mark the comment as deleted
183 this.softDeleteComment(commentToDelete)
185 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
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)