1 import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, 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 { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
6 import { AuthService } from '../../../core/auth'
7 import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model'
8 import { User } from '../../../shared/users'
9 import { VideoSortField } from '../../../shared/video/sort-field.type'
10 import { VideoDetails } from '../../../shared/video/video-details.model'
11 import { VideoComment } from './video-comment.model'
12 import { VideoCommentService } from './video-comment.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { Syndication } from '@app/shared/video/syndication.model'
15 import { HooksService } from '@app/core/plugins/hooks.service'
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', { static: false }) commentHighlightBlock: ElementRef
24 @Input() video: VideoDetails
27 comments: VideoComment[] = []
28 highlightedThread: VideoComment
29 sort: VideoSortField = '-createdAt'
30 componentPagination: ComponentPagination = {
35 inReplyToCommentId: number
36 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
37 threadLoading: { [ id: number ]: boolean } = {}
39 syndicationItems: Syndication[] = []
41 onDataSubject = new Subject<any[]>()
43 private sub: Subscription
46 private authService: AuthService,
47 private notifier: Notifier,
48 private confirmService: ConfirmService,
49 private videoCommentService: VideoCommentService,
50 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.id,
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
98 if (highlightThread) {
99 this.highlightedThread = new VideoComment(res.comment)
101 // Scroll to the highlighted thread
102 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
106 err => this.notifier.error(err.message)
112 videoId: this.video.id,
113 componentPagination: this.componentPagination,
117 const obs = this.hooks.wrapObsFun(
118 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
121 'filter:api.video-watch.video-threads.list.params',
122 'filter:api.video-watch.video-threads.list.result'
127 this.comments = this.comments.concat(res.data)
128 this.componentPagination.totalItems = res.total
130 this.onDataSubject.next(res.data)
133 err => this.notifier.error(err.message)
137 onCommentThreadCreated (comment: VideoComment) {
138 this.comments.unshift(comment)
141 onWantedToReply (comment: VideoComment) {
142 this.inReplyToCommentId = comment.id
146 this.inReplyToCommentId = undefined
149 onThreadCreated (commentTree: VideoCommentThreadTree) {
150 this.viewReplies(commentTree.comment.id)
153 async onWantedToDelete (commentToDelete: VideoComment) {
154 let message = 'Do you really want to delete this comment?'
156 if (commentToDelete.isLocal) {
157 message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.')
159 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
162 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
163 if (res === false) return
165 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
168 // Mark the comment as deleted
169 this.softDeleteComment(commentToDelete)
171 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
174 err => this.notifier.error(err.message)
179 return this.authService.isLoggedIn()
183 this.componentPagination.currentPage++
185 if (hasMoreItems(this.componentPagination)) {
186 this.loadMoreThreads()
190 private softDeleteComment (comment: VideoComment) {
191 comment.isDeleted = true
192 comment.deletedAt = new Date()
194 comment.account = null
197 private resetVideo () {
198 if (this.video.commentsEnabled === true) {
199 // Reset all our fields
200 this.highlightedThread = null
202 this.threadComments = {}
203 this.threadLoading = {}
204 this.inReplyToCommentId = undefined
205 this.componentPagination.currentPage = 1
206 this.componentPagination.totalItems = null
208 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
210 this.loadMoreThreads()
214 private processHighlightedThread (highlightedThreadId: number) {
215 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
217 const highlightThread = true
218 this.viewReplies(highlightedThreadId, highlightThread)