PeerTube/client/src/app/+videos/+video-watch/shared/comment/video-comments.component.ts

282 lines
8.4 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { Subject, Subscription } from 'rxjs'
2020-02-10 14:25:38 +01:00
import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
2018-02-19 10:38:24 +01:00
import { ActivatedRoute } from '@angular/router'
2020-06-23 14:10:17 +02:00
import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, User } from '@app/core'
import { HooksService } from '@app/core/plugins/hooks.service'
import { Syndication, VideoDetails } from '@app/shared/shared-main'
2020-07-09 15:54:24 +02:00
import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
import { PeerTubeProblemDocument, ServerErrorCode } from '@peertube/peertube-models'
2017-12-27 16:11:53 +01:00
@Component({
selector: 'my-video-comments',
templateUrl: './video-comments.component.html',
2021-08-17 14:42:53 +02:00
styleUrls: [ './video-comments.component.scss' ]
2017-12-27 16:11:53 +01:00
})
2018-02-19 10:38:24 +01:00
export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
2020-02-07 10:00:34 +01:00
@ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
2018-01-03 10:12:36 +01:00
@Input() video: VideoDetails
@Input() videoPassword: string
2017-12-27 16:11:53 +01:00
@Input() user: User
@Output() timestampClicked = new EventEmitter<number>()
2017-12-27 16:11:53 +01:00
comments: VideoComment[] = []
2018-02-21 17:46:04 +01:00
highlightedThread: VideoComment
2020-06-23 14:10:17 +02:00
sort = '-createdAt'
2017-12-27 16:11:53 +01:00
componentPagination: ComponentPagination = {
currentPage: 1,
2017-12-29 09:29:32 +01:00
itemsPerPage: 10,
2017-12-27 16:11:53 +01:00
totalItems: null
}
totalNotDeletedComments: number
2017-12-27 16:11:53 +01:00
inReplyToCommentId: number
commentReplyRedraftValue: string
commentThreadRedraftValue: string
2017-12-27 16:11:53 +01:00
threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
threadLoading: { [ id: number ]: boolean } = {}
2018-02-19 10:38:24 +01:00
2018-10-18 14:35:31 +02:00
syndicationItems: Syndication[] = []
2019-08-02 14:49:25 +02:00
onDataSubject = new Subject<any[]>()
2018-02-19 10:38:24 +01:00
private sub: Subscription
2017-12-27 16:11:53 +01:00
constructor (
private authService: AuthService,
private notifier: Notifier,
2018-01-04 11:19:16 +01:00
private confirmService: ConfirmService,
private videoCommentService: VideoCommentService,
2018-06-04 16:21:17 +02:00
private activatedRoute: ActivatedRoute,
2019-07-22 15:40:13 +02:00
private hooks: HooksService
2017-12-27 16:11:53 +01:00
) {}
2018-02-19 10:38:24 +01:00
ngOnInit () {
// Find highlighted comment in params
this.sub = this.activatedRoute.params.subscribe(
params => {
2018-02-21 17:46:04 +01:00
if (params['threadId']) {
const highlightedThreadId = +params['threadId']
this.processHighlightedThread(highlightedThreadId)
2018-02-19 10:38:24 +01:00
}
}
)
}
2018-01-12 17:16:48 +01:00
ngOnChanges (changes: SimpleChanges) {
if (changes['video']) {
2018-02-19 10:38:24 +01:00
this.resetVideo()
2018-01-03 10:12:36 +01:00
}
2017-12-27 16:11:53 +01:00
}
2018-02-19 10:38:24 +01:00
ngOnDestroy () {
if (this.sub) this.sub.unsubscribe()
}
2018-02-21 17:46:04 +01:00
viewReplies (commentId: number, highlightThread = false) {
2018-02-19 10:38:24 +01:00
this.threadLoading[commentId] = true
2017-12-27 16:11:53 +01:00
2019-07-22 15:40:13 +02:00
const params = {
2022-06-24 14:47:32 +02:00
videoId: this.video.uuid,
threadId: commentId,
videoPassword: this.videoPassword
2019-07-22 15:40:13 +02:00
}
const obs = this.hooks.wrapObsFun(
this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
params,
'video-watch',
'filter:api.video-watch.video-thread-replies.list.params',
'filter:api.video-watch.video-thread-replies.list.result'
)
2021-08-17 11:27:47 +02:00
obs.subscribe({
next: res => {
this.threadComments[commentId] = res
this.threadLoading[commentId] = false
this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
2018-02-19 10:38:24 +01:00
2021-08-17 11:27:47 +02:00
if (highlightThread) {
this.highlightedThread = new VideoComment(res.comment)
2021-08-17 11:27:47 +02:00
// Scroll to the highlighted thread
setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
}
},
2017-12-27 16:11:53 +01:00
2022-07-29 10:32:56 +02:00
error: err => {
// We may try to fetch highlighted thread of another video, skip the error if it is the case
// We'll retry the request on video Input() change
const errorBody = err.body as PeerTubeProblemDocument
if (highlightThread && errorBody?.code === ServerErrorCode.COMMENT_NOT_ASSOCIATED_TO_VIDEO) return
this.notifier.error(err.message)
}
2021-08-17 11:27:47 +02:00
})
2017-12-27 16:11:53 +01:00
}
2019-07-22 15:40:13 +02:00
loadMoreThreads () {
const params = {
2022-06-24 14:47:32 +02:00
videoId: this.video.uuid,
videoPassword: this.videoPassword,
2019-07-22 15:40:13 +02:00
componentPagination: this.componentPagination,
sort: this.sort
}
2017-12-29 09:29:32 +01:00
2019-07-22 15:40:13 +02:00
const obs = this.hooks.wrapObsFun(
this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
params,
'video-watch',
'filter:api.video-watch.video-threads.list.params',
'filter:api.video-watch.video-threads.list.result'
)
2021-08-17 11:27:47 +02:00
obs.subscribe({
next: res => {
2019-07-22 16:04:44 +02:00
this.comments = this.comments.concat(res.data)
this.componentPagination.totalItems = res.total
this.totalNotDeletedComments = res.totalNotDeletedComments
2019-08-02 14:49:25 +02:00
this.onDataSubject.next(res.data)
2022-08-03 10:39:40 +02:00
this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
2019-07-22 15:40:13 +02:00
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
2017-12-29 09:29:32 +01:00
}
2017-12-27 16:11:53 +01:00
onCommentThreadCreated (comment: VideoComment) {
this.comments.unshift(comment)
2020-08-12 12:43:26 +02:00
this.commentThreadRedraftValue = undefined
2017-12-27 16:11:53 +01:00
}
onWantedToReply (comment: VideoComment) {
this.inReplyToCommentId = comment.id
}
onResetReply () {
this.inReplyToCommentId = undefined
2020-08-12 12:43:26 +02:00
this.commentReplyRedraftValue = undefined
2017-12-27 16:11:53 +01:00
}
2018-01-04 11:19:16 +01:00
onThreadCreated (commentTree: VideoCommentThreadTree) {
2018-02-19 10:38:24 +01:00
this.viewReplies(commentTree.comment.id)
2018-01-04 11:19:16 +01:00
}
2020-06-23 14:10:17 +02:00
handleSortChange (sort: string) {
2019-12-27 17:02:34 +01:00
if (this.sort === sort) return
this.sort = sort
this.resetVideo()
}
handleTimestampClicked (timestamp: number) {
this.timestampClicked.emit(timestamp)
}
2018-01-04 11:19:16 +01:00
2020-08-14 15:04:58 +02:00
async onWantedToDelete (
commentToDelete: VideoComment,
title = $localize`Delete`,
message = $localize`Do you really want to delete this comment?`
): Promise<boolean> {
2020-05-14 14:54:10 +02:00
if (commentToDelete.isLocal || this.video.isLocal) {
message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
2019-03-20 10:12:31 +01:00
} else {
message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
2019-03-20 10:12:31 +01:00
}
2020-08-07 23:24:22 +02:00
const res = await this.confirmService.confirm(message, title)
if (res === false) return false
this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
2020-05-14 14:54:10 +02:00
if (this.highlightedThread?.id === commentToDelete.id) {
commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
this.highlightedThread = undefined
}
// Mark the comment as deleted
this.softDeleteComment(commentToDelete)
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
return true
}
2020-08-07 23:24:22 +02:00
async onWantedToRedraft (commentToRedraft: VideoComment) {
2021-08-17 14:42:53 +02:00
const confirm = await this.onWantedToDelete(
commentToRedraft,
$localize`Delete and re-draft`,
$localize`Do you really want to delete and re-draft this comment?`
)
if (confirm) {
this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
// Restore line feed for editing
const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
if (commentToRedraft.threadId === commentToRedraft.id) {
this.commentThreadRedraftValue = commentToRedraftText
} else {
this.commentReplyRedraftValue = commentToRedraftText
}
}
2018-01-04 11:19:16 +01:00
}
2017-12-27 16:11:53 +01:00
isUserLoggedIn () {
return this.authService.isLoggedIn()
}
2017-12-29 09:29:32 +01:00
2020-03-06 13:10:34 +01:00
onNearOfBottom () {
2019-01-08 11:26:41 +01:00
if (hasMoreItems(this.componentPagination)) {
this.componentPagination.currentPage++
2019-07-22 15:40:13 +02:00
this.loadMoreThreads()
2017-12-29 09:29:32 +01:00
}
}
private softDeleteComment (comment: VideoComment) {
comment.isDeleted = true
comment.deletedAt = new Date()
comment.text = ''
comment.account = null
2018-01-04 11:19:16 +01:00
}
2018-01-12 17:16:48 +01:00
2018-02-19 10:38:24 +01:00
private resetVideo () {
2018-01-12 17:16:48 +01:00
if (this.video.commentsEnabled === true) {
// Reset all our fields
2018-02-21 17:46:04 +01:00
this.highlightedThread = null
2018-01-12 17:16:48 +01:00
this.comments = []
this.threadComments = {}
this.threadLoading = {}
this.inReplyToCommentId = undefined
2018-02-13 14:11:05 +01:00
this.componentPagination.currentPage = 1
this.componentPagination.totalItems = null
this.totalNotDeletedComments = null
2018-01-12 17:16:48 +01:00
this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
2019-07-22 15:40:13 +02:00
this.loadMoreThreads()
2022-07-29 10:32:56 +02:00
if (this.activatedRoute.snapshot.params['threadId']) {
this.processHighlightedThread(+this.activatedRoute.snapshot.params['threadId'])
2022-07-29 10:32:56 +02:00
}
2018-01-12 17:16:48 +01:00
}
}
2018-02-19 10:38:24 +01:00
2018-02-21 17:46:04 +01:00
private processHighlightedThread (highlightedThreadId: number) {
this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
2018-02-19 10:38:24 +01:00
2018-02-21 17:46:04 +01:00
const highlightThread = true
this.viewReplies(highlightedThreadId, highlightThread)
2018-02-19 10:38:24 +01:00
}
2017-12-27 16:11:53 +01:00
}