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 { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
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
27 componentPagination: ComponentPagination = {
32 totalNotDeletedComments: number
34 inReplyToCommentId: number
35 commentReplyRedraftValue: string
36 commentThreadRedraftValue: string
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,
53 private hooks: HooksService
57 // Find highlighted comment in params
58 this.sub = this.activatedRoute.params.subscribe(
60 if (params['threadId']) {
61 const highlightedThreadId = +params['threadId']
62 this.processHighlightedThread(highlightedThreadId)
68 ngOnChanges (changes: SimpleChanges) {
69 if (changes['video']) {
75 if (this.sub) this.sub.unsubscribe()
78 viewReplies (commentId: number, highlightThread = false) {
79 this.threadLoading[commentId] = true
82 videoId: this.video.uuid,
86 const obs = this.hooks.wrapObsFun(
87 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
90 'filter:api.video-watch.video-thread-replies.list.params',
91 'filter:api.video-watch.video-thread-replies.list.result'
96 this.threadComments[commentId] = res
97 this.threadLoading[commentId] = false
98 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
100 if (highlightThread) {
101 this.highlightedThread = new VideoComment(res.comment)
103 // Scroll to the highlighted thread
104 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
109 // We may try to fetch highlighted thread of another video, skip the error if it is the case
110 // We'll retry the request on video Input() change
111 const errorBody = err.body as PeerTubeProblemDocument
112 if (highlightThread && errorBody?.code === ServerErrorCode.COMMENT_NOT_ASSOCIATED_TO_VIDEO) return
114 this.notifier.error(err.message)
121 videoId: this.video.uuid,
122 componentPagination: this.componentPagination,
126 const obs = this.hooks.wrapObsFun(
127 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
130 'filter:api.video-watch.video-threads.list.params',
131 'filter:api.video-watch.video-threads.list.result'
136 this.comments = this.comments.concat(res.data)
137 this.componentPagination.totalItems = res.total
138 this.totalNotDeletedComments = res.totalNotDeletedComments
140 this.onDataSubject.next(res.data)
142 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
145 error: err => this.notifier.error(err.message)
149 onCommentThreadCreated (comment: VideoComment) {
150 this.comments.unshift(comment)
151 this.commentThreadRedraftValue = undefined
154 onWantedToReply (comment: VideoComment) {
155 this.inReplyToCommentId = comment.id
159 this.inReplyToCommentId = undefined
160 this.commentReplyRedraftValue = undefined
163 onThreadCreated (commentTree: VideoCommentThreadTree) {
164 this.viewReplies(commentTree.comment.id)
167 handleSortChange (sort: string) {
168 if (this.sort === sort) return
174 handleTimestampClicked (timestamp: number) {
175 this.timestampClicked.emit(timestamp)
178 async onWantedToDelete (
179 commentToDelete: VideoComment,
180 title = $localize`Delete`,
181 message = $localize`Do you really want to delete this comment?`
182 ): Promise<boolean> {
183 if (commentToDelete.isLocal || this.video.isLocal) {
184 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
186 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
189 const res = await this.confirmService.confirm(message, title)
190 if (res === false) return false
192 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
195 if (this.highlightedThread?.id === commentToDelete.id) {
196 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
198 this.highlightedThread = undefined
201 // Mark the comment as deleted
202 this.softDeleteComment(commentToDelete)
205 error: err => this.notifier.error(err.message)
211 async onWantedToRedraft (commentToRedraft: VideoComment) {
212 const confirm = await this.onWantedToDelete(
214 $localize`Delete and re-draft`,
215 $localize`Do you really want to delete and re-draft this comment?`
219 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
221 // Restore line feed for editing
222 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
224 if (commentToRedraft.threadId === commentToRedraft.id) {
225 this.commentThreadRedraftValue = commentToRedraftText
227 this.commentReplyRedraftValue = commentToRedraftText
234 return this.authService.isLoggedIn()
238 if (hasMoreItems(this.componentPagination)) {
239 this.componentPagination.currentPage++
240 this.loadMoreThreads()
244 private softDeleteComment (comment: VideoComment) {
245 comment.isDeleted = true
246 comment.deletedAt = new Date()
248 comment.account = null
251 private resetVideo () {
252 if (this.video.commentsEnabled === true) {
253 // Reset all our fields
254 this.highlightedThread = null
256 this.threadComments = {}
257 this.threadLoading = {}
258 this.inReplyToCommentId = undefined
259 this.componentPagination.currentPage = 1
260 this.componentPagination.totalItems = null
261 this.totalNotDeletedComments = null
263 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
264 this.loadMoreThreads()
266 if (this.activatedRoute.params['threadId']) {
267 this.processHighlightedThread(+this.activatedRoute.params['threadId'])
272 private processHighlightedThread (highlightedThreadId: number) {
273 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
275 const highlightThread = true
276 this.viewReplies(highlightedThreadId, highlightThread)