Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Limit the number of replies when fetching a thread

Chocobozzz1b99282aedd4
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.html b/client/src/app/+video-watch/shared/comment/video-comment.component.html
index b0ca900d82..c6dfe08a3c 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.html
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.html
@@ -91,10 +91,12 @@
[inReplyToCommentId]="inReplyToCommentId()"
[commentTree]="commentChild"
[parentComments]="newParentComments"
+ [depth]="depth() + 1"
(wantedToReply)="onWantToReply($event)"
(wantedToDelete)="onWantToDelete($event)"
(wantedToRedraft)="onWantToRedraft($event)"
(wantedToApprove)="onWantToApprove($event)"
+ (wantedToViewRepliesInModal)="wantedToViewRepliesInModal.emit($event)"
(resetReply)="onResetReply()"
(timestampClicked)="handleTimestampClicked($event)"
[redraftValue]="redraftValue()"
@@ -102,6 +104,28 @@
</div>
}
</div>
+
+ @if (getNotFetchedRepliesCount() > 0) {
+ <button (click)="viewMoreReplies()" class="view-replies mb-2 button-unstyle">
+ @if (mustViewRepliesInModal()) {
+ <span class="chevron-right"></span>
+ } @else {
+ <span class="chevron-down"></span>
+ }
+
+ @if (commentTree().children.length === 0) {
+ <ng-container i18n>
+ View {getNotFetchedRepliesCount(), plural, =1 {1 reply} other {{{ getNotFetchedRepliesCount() }} replies}}
+ </ng-container>
+ } @else {
+ <ng-container i18n>
+ View {getNotFetchedRepliesCount(), plural, =1 {1 more reply} other {{{ getNotFetchedRepliesCount() }} more replies}}
+ </ng-container>
+ }
+
+ <my-loader size="sm" class="ms-1" [loading]="loadingReplies"></my-loader>
+ </button>
+ }
}
<ng-content></ng-content>
</div>
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.scss b/client/src/app/+video-watch/shared/comment/video-comment.component.scss
index ef08004063..17ca376cd7 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.scss
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.scss
@@ -142,7 +142,24 @@ my-video-comment-add {
}
}
-@media screen and (max-width: 600px) {
+.view-replies {
+ font-weight: $font-semibold;
+ cursor: pointer;
+ position: relative;
+
+ .chevron-down,
+ .chevron-right {
+ @include margin-left(0);
+ }
+}
+
+@media screen and (max-width: $mobile-view) {
+ .view-replies {
+ @include font-size(14px);
+ }
+}
+
+@media screen and (max-width: $small-view) {
.is-child .left {
@include margin-right(0.25rem);
}
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.ts b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
index d6a9945b92..0be010c84e 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.ts
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
@@ -4,29 +4,36 @@ import {
ChangeDetectorRef,
Component,
OnChanges,
+ OnDestroy,
+ SimpleChanges,
TemplateRef,
effect,
inject,
input,
model,
output,
+ signal,
viewChild
} from '@angular/core'
import { RouterLink } from '@angular/router'
import { MarkdownService, Notifier, ScreenService, UserService } from '@app/core'
import { AuthService } from '@app/core/auth'
+import { HooksService } from '@app/core/plugins/hooks.service'
import { ActorAvatarComponent } from '@app/shared/shared-actor-image/actor-avatar.component'
import { GlobalIconComponent } from '@app/shared/shared-icons/global-icon.component'
import { Account } from '@app/shared/shared-main/account/account.model'
import { DropdownAction } from '@app/shared/shared-main/buttons/action-dropdown.component'
+import { LoaderComponent } from '@app/shared/shared-main/common/loader.component'
import { FromNowPipe } from '@app/shared/shared-main/date/from-now.pipe'
import { Video } from '@app/shared/shared-main/video/video.model'
import { CommentReportComponent } from '@app/shared/shared-moderation/report-modals/comment-report.component'
import { UserModerationDropdownComponent } from '@app/shared/shared-moderation/user-moderation-dropdown.component'
import { VideoCommentThreadTree } from '@app/shared/shared-video-comment/video-comment-thread-tree.model'
import { VideoComment } from '@app/shared/shared-video-comment/video-comment.model'
+import { VideoCommentService } from '@app/shared/shared-video-comment/video-comment.service'
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
import { User, UserRight } from '@peertube/peertube-models'
+import { Subscription } from 'rxjs'
import { TimestampRouteTransformerDirective } from '../timestamp-route-transformer.directive'
import { VideoCommentAddComponent } from './video-comment-add.component'
@@ -44,10 +51,11 @@ import { VideoCommentAddComponent } from './video-comment-add.component'
VideoCommentAddComponent,
CommentReportComponent,
GlobalIconComponent,
- FromNowPipe
+ FromNowPipe,
+ LoaderComponent
]
})
-export class VideoCommentComponent implements OnChanges {
+export class VideoCommentComponent implements OnChanges, OnDestroy {
private cd = inject(ChangeDetectorRef)
private markdownService = inject(MarkdownService)
private authService = inject(AuthService)
@@ -55,6 +63,8 @@ export class VideoCommentComponent implements OnChanges {
private notifier = inject(Notifier)
private screenService = inject(ScreenService)
private modalService = inject(NgbModal)
+ private videoCommentService = inject(VideoCommentService)
+ private hooks = inject(HooksService)
readonly commentReportModal = viewChild<CommentReportComponent>('commentReportModal')
readonly commentReplyModal = viewChild<TemplateRef<any>>('commentReplyModal')
@@ -69,6 +79,11 @@ export class VideoCommentComponent implements OnChanges {
readonly commentTree = model<VideoCommentThreadTree>(undefined)
readonly redraftValue = model<string>(undefined)
+ // Nesting level of this comment, reset to 0 when the thread is continued in a modal
+ readonly depth = input(0)
+ // Immediately fetch the replies instead of displaying a "View replies" button
+ readonly autoLoadReplies = input(false)
+
readonly wantedToReply = output<VideoComment>()
readonly wantedToDelete = output<VideoComment>()
readonly wantedToApprove = output<VideoComment>()
@@ -76,6 +91,8 @@ export class VideoCommentComponent implements OnChanges {
readonly threadCreated = output<VideoCommentThreadTree>()
readonly resetReply = output()
readonly timestampClicked = output<number>()
+ // `parentComments` is forwarded so replies written in the modal keep mentioning the whole ancestor chain
+ readonly wantedToViewRepliesInModal = output<{ comment: VideoComment, parentComments: VideoComment[] }>()
prependModerationActions: DropdownAction<any>[]
@@ -87,22 +104,38 @@ export class VideoCommentComponent implements OnChanges {
private replyModalRef: NgbModalRef
showCommentReportModal = false
+ private reportModalRequested = signal(0)
+
+ loadingReplies = false
+ private repliesSub: Subscription
get user () {
return this.authService.getUser()
}
constructor () {
- // Open the report modal as soon as it has been loaded (it may be deferred)
+ // Open the report modal every time it's requested, waiting for it to be loaded first if it's deferred
effect(() => {
- const modal = this.commentReportModal()
- if (modal) modal.show()
+ if (this.reportModalRequested() === 0) return
+
+ this.commentReportModal()?.show()
})
}
- ngOnChanges () {
+ ngOnChanges (changes: SimpleChanges) {
+ // The "view replies" modal reuses this component for every comment of its navigation stack:
+ // a pending request targets the previous comment tree, so discard it
+ if (changes['comment'] && !changes['comment'].firstChange) {
+ this.cancelRepliesRequest()
+ }
+
this.reinit()
this.toggleMobileReplyModalIfNeeded()
+ this.autoLoadRepliesIfNeeded()
+ }
+
+ ngOnDestroy () {
+ this.cancelRepliesRequest()
}
onCommentReplyCreated (createdComment: VideoComment) {
@@ -110,17 +143,29 @@ export class VideoCommentComponent implements OnChanges {
this.commentTree.set({
comment: this.comment(),
hasDisplayedChildren: false,
- children: []
+ children: [],
+ totalChildren: 0,
+ fetchedChildren: 0
})
this.threadCreated.emit(this.commentTree())
}
- this.commentTree().children.unshift({
+ const tree = this.commentTree()
+
+ // Inserted locally, not fetched from the server: don't touch `fetchedChildren`, used as the pagination offset
+ // A later page may contain this comment again, `viewMoreReplies` deduplicates it
+ tree.children.unshift({
comment: createdComment,
hasDisplayedChildren: false,
- children: []
+ children: [],
+ totalChildren: 0,
+ fetchedChildren: 0
})
+ tree.totalChildren++
+
+ // Mutated in place above: bump the signal's identity so consumers relying on it are notified
+ this.commentTree.set(tree)
this.resetReply.emit()
@@ -129,6 +174,81 @@ export class VideoCommentComponent implements OnChanges {
this.closeReplyModal()
}
+ // ---------------------------------------------------------------------------
+ // Replies the server did not include in the thread tree, because it was too deep or too wide
+ // ---------------------------------------------------------------------------
+
+ getNotFetchedRepliesCount () {
+ const tree = this.commentTree()
+ if (!tree) return 0
+
+ return Math.max(tree.totalChildren - tree.children.length, 0)
+ }
+
+ // Do not overflow
+ mustViewRepliesInModal () {
+ return this.depth() >= this.getMaxInlineDepth()
+ }
+
+ viewMoreReplies () {
+ if (this.loadingReplies) return
+
+ if (this.mustViewRepliesInModal()) {
+ this.wantedToViewRepliesInModal.emit({ comment: this.comment(), parentComments: this.parentComments() })
+ return
+ }
+
+ this.loadingReplies = true
+
+ const tree = this.commentTree()
+ const params = {
+ videoId: this.video().uuid,
+ commentId: this.comment().id,
+ videoPassword: this.videoPassword(),
+ // Relative to this comment: don't fetch nesting levels deeper than what can still be rendered inline
+ maxDepth: this.getMaxInlineDepth() - this.depth(),
+ pagination: {
+ start: tree.fetchedChildren,
+ count: VideoCommentService.REPLIES_PER_PAGE
+ }
+ }
+
+ const obs = this.hooks.wrapObsFun(
+ this.videoCommentService.getMissingReplies.bind(this.videoCommentService),
+ params,
+ 'video-watch',
+ 'filter:api.video-watch.video-comment-replies.list.params',
+ 'filter:api.video-watch.video-comment-replies.list.result'
+ )
+
+ this.repliesSub = obs.subscribe({
+ next: ({ total, data }) => {
+ this.repliesSub = undefined
+ this.loadingReplies = false
+
+ // Replies created locally are already displayed but are still part of the server pagination
+ const displayedIds = new Set(tree.children.map(c => c.comment.id))
+
+ tree.children = tree.children.concat(data.filter(c => !displayedIds.has(c.comment.id)))
+ tree.totalChildren = total
+ tree.fetchedChildren += data.length
+
+ // Mutated in place above: bump the signal's identity so consumers relying on it are notified
+ this.commentTree.set(tree)
+
+ this.cd.markForCheck()
+ },
+
+ error: err => {
+ this.repliesSub = undefined
+ this.loadingReplies = false
+ this.cd.markForCheck()
+
+ this.notifier.handleError(err)
+ }
+ })
+ }
+
onWantToReply (comment?: VideoComment) {
this.wantedToReply.emit(comment || this.comment())
}
@@ -209,7 +329,28 @@ export class VideoCommentComponent implements OnChanges {
}
isChild () {
- return this.parentComments().length !== 0
+ return this.depth() !== 0
+ }
+
+ private getMaxInlineDepth () {
+ return this.videoCommentService.getMaxInlineCommentDepth()
+ }
+
+ private cancelRepliesRequest () {
+ if (!this.repliesSub) return
+
+ this.repliesSub.unsubscribe()
+ this.repliesSub = undefined
+ this.loadingReplies = false
+ }
+
+ private autoLoadRepliesIfNeeded () {
+ if (!this.autoLoadReplies() || this.loadingReplies) return
+
+ if (this.commentTree()?.children.length !== 0) return
+ if (this.getNotFetchedRepliesCount() === 0) return
+
+ this.viewMoreReplies()
}
private getUserIfNeeded (account: Account) {
@@ -291,6 +432,9 @@ export class VideoCommentComponent implements OnChanges {
private showReportModal () {
this.showCommentReportModal = true
+ this.reportModalRequested.update(v => v + 1)
+
+ this.cd.markForCheck()
}
private closeReplyModal () {
diff --git a/client/src/app/+video-watch/shared/comment/video-comments.component.html b/client/src/app/+video-watch/shared/comment/video-comments.component.html
index 0864c4bef4..e39490b4d9 100644
--- a/client/src/app/+video-watch/shared/comment/video-comments.component.html
+++ b/client/src/app/+video-watch/shared/comment/video-comments.component.html
@@ -1,7 +1,7 @@
<div>
<div class="title-block">
<h2>
- {totalNotDeletedComments, plural, =0 {Comments} =1 {1 Comment} other {{{ totalNotDeletedComments }} Comments}}
+ {totalNotDeletedComments, plural, =0 {Comments} =1 {1 Comment} other {{{ totalNotDeletedComments | myNumberFormatter }} Comments}}
</h2>
<my-feed class="me-1" [syndicationItems]="syndicationItems"></my-feed>
@@ -48,6 +48,7 @@
(wantedToDelete)="onWantedToDelete($event)"
(wantedToRedraft)="onWantedToRedraft($event)"
(wantedToApprove)="onWantToApprove($event)"
+ (wantedToViewRepliesInModal)="onWantedToViewRepliesInModal($event)"
(threadCreated)="onThreadCreated($event)"
(resetReply)="onResetReply()"
(timestampClicked)="handleTimestampClicked($event)"
@@ -70,6 +71,7 @@
(wantedToDelete)="onWantedToDelete($event)"
(wantedToRedraft)="onWantedToRedraft($event)"
(wantedToApprove)="onWantToApprove($event)"
+ (wantedToViewRepliesInModal)="onWantedToViewRepliesInModal($event)"
(threadCreated)="onThreadCreated($event)"
(resetReply)="onResetReply()"
(timestampClicked)="handleTimestampClicked($event)"
@@ -107,3 +109,42 @@
<div i18n>Comments are disabled.</div>
}
</div>
+
+<ng-template #repliesModal>
+ <div class="modal-header">
+ @if (repliesModalStack.length > 1) {
+ <button class="border-0 p-0 me-2" title="Go back to the previous comment" i18n-title (click)="onRepliesModalBack()">
+ <my-global-icon iconName="chevron-left" ariaHidden="true"></my-global-icon>
+ </button>
+ }
+
+ <h4 class="modal-title" i18n>Replies to {{ getRepliesModalRoot()?.comment?.account?.displayName || 'Unknown' }}</h4>
+
+ <button class="border-0 p-0" title="Close this modal" i18n-title (click)="closeRepliesModal()">
+ <my-global-icon iconName="cross" ariaHidden="true"></my-global-icon>
+ </button>
+ </div>
+
+ <div class="modal-body replies-modal-body">
+ <!-- The stack is emptied when the modal is closed, but the template can still be rendered while it fades out -->
+ @if (getRepliesModalRoot(); as repliesModalRoot) {
+ <my-video-comment
+ [comment]="repliesModalRoot.comment"
+ [video]="video()"
+ [videoPassword]="videoPassword()"
+ [inReplyToCommentId]="inReplyToCommentId"
+ [commentTree]="repliesModalTree"
+ [parentComments]="repliesModalRoot.parentComments"
+ [autoLoadReplies]="true"
+ (wantedToReply)="onWantedToReply($event)"
+ (wantedToDelete)="onWantedToDelete($event)"
+ (wantedToRedraft)="onWantedToRedraft($event)"
+ (wantedToApprove)="onWantToApprove($event)"
+ (wantedToViewRepliesInModal)="onWantedToViewRepliesInModal($event)"
+ (resetReply)="onResetReply()"
+ (timestampClicked)="handleTimestampClicked($event)"
+ [redraftValue]="commentReplyRedraftValue"
+ ></my-video-comment>
+ }
+ </div>
+</ng-template>
diff --git a/client/src/app/+video-watch/shared/comment/video-comments.component.scss b/client/src/app/+video-watch/shared/comment/video-comments.component.scss
index 1b40efc023..555e7c6d91 100644
--- a/client/src/app/+video-watch/shared/comment/video-comments.component.scss
+++ b/client/src/app/+video-watch/shared/comment/video-comments.component.scss
@@ -34,6 +34,11 @@
}
}
+.replies-modal-body {
+ max-height: 70vh;
+ overflow-y: auto;
+}
+
#dropdown-sort-comments {
font-weight: $font-semibold;
text-transform: uppercase;
diff --git a/client/src/app/+video-watch/shared/comment/video-comments.component.ts b/client/src/app/+video-watch/shared/comment/video-comments.component.ts
index 13a7e1ade3..b1db57679e 100644
--- a/client/src/app/+video-watch/shared/comment/video-comments.component.ts
+++ b/client/src/app/+video-watch/shared/comment/video-comments.component.ts
@@ -9,11 +9,13 @@ import {
OnInit,
output,
SimpleChanges,
+ TemplateRef,
viewChild
} from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, PluginService, User } from '@app/core'
import { HooksService } from '@app/core/plugins/hooks.service'
+import { GlobalIconComponent } from '@app/shared/shared-icons/global-icon.component'
import { InfiniteScrollerDirective } from '@app/shared/shared-main/common/infinite-scroller.directive'
import { LoaderComponent } from '@app/shared/shared-main/common/loader.component'
import { FeedComponent } from '@app/shared/shared-main/feeds/feed.component'
@@ -22,9 +24,18 @@ import { VideoDetails } from '@app/shared/shared-main/video/video-details.model'
import { VideoCommentThreadTree } from '@app/shared/shared-video-comment/video-comment-thread-tree.model'
import { VideoComment } from '@app/shared/shared-video-comment/video-comment.model'
import { VideoCommentService } from '@app/shared/shared-video-comment/video-comment.service'
-import { NgbDropdown, NgbDropdownButtonItem, NgbDropdownItem, NgbDropdownMenu, NgbDropdownToggle } from '@ng-bootstrap/ng-bootstrap'
+import {
+ NgbDropdown,
+ NgbDropdownButtonItem,
+ NgbDropdownItem,
+ NgbDropdownMenu,
+ NgbDropdownToggle,
+ NgbModal,
+ NgbModalRef
+} from '@ng-bootstrap/ng-bootstrap'
import { PeerTubeProblemDocument, ServerErrorCode, VideoCommentPolicy } from '@peertube/peertube-models'
import { lastValueFrom, Subject, Subscription } from 'rxjs'
+import { NumberFormatterPipe } from '../../../shared/shared-main/common/number-formatter.pipe'
import { VideoCommentAddComponent } from './video-comment-add.component'
import { VideoCommentComponent } from './video-comment.component'
@@ -43,7 +54,9 @@ import { VideoCommentComponent } from './video-comment.component'
VideoCommentAddComponent,
InfiniteScrollerDirective,
VideoCommentComponent,
- LoaderComponent
+ LoaderComponent,
+ NumberFormatterPipe,
+ GlobalIconComponent
]
})
export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
@@ -54,8 +67,11 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
private activatedRoute = inject(ActivatedRoute)
private hooks = inject(HooksService)
private pluginService = inject(PluginService)
+ private modalService = inject(NgbModal)
readonly commentHighlightBlock = viewChild<ElementRef>('commentHighlightBlock')
+ readonly repliesModal = viewChild<TemplateRef<any>>('repliesModal')
+
readonly video = input<VideoDetails>(undefined)
readonly videoPassword = input<string>(undefined)
readonly user = input<User>(undefined)
@@ -83,11 +99,17 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
threadComments: { [id: number]: VideoCommentThreadTree } = {}
threadLoading: { [id: number]: boolean } = {}
+ // Comments too deeply nested to be displayed inline continue in a modal, where they become a root comment again.
+ // We keep the visited comments so the user can navigate back to where they come from
+ repliesModalStack: { comment: VideoComment, parentComments: VideoComment[] }[] = []
+ repliesModalTree: VideoCommentThreadTree
+
syndicationItems: Syndication[] = []
onDataSubject = new Subject<any[]>()
private sub: Subscription
+ private repliesModalRef: NgbModalRef
ngOnInit () {
this.pluginService.addAction('video-watch-comment-list:load-data', () => this.loadMoreThreads(true))
@@ -113,6 +135,7 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
this.pluginService.removeAction('video-watch-comment-list:load-data')
if (this.sub) this.sub.unsubscribe()
+ if (this.repliesModalRef) this.repliesModalRef.close()
}
viewReplies (commentId: number, highlightThread = false) {
@@ -121,11 +144,12 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
const params = {
videoId: this.video().uuid,
threadId: commentId,
+ maxDepth: this.videoCommentService.getMaxInlineCommentDepth(),
videoPassword: this.videoPassword()
}
const obs = this.hooks.wrapObsFun(
- this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
+ this.videoCommentService.getThread.bind(this.videoCommentService),
params,
'video-watch',
'filter:api.video-watch.video-thread-replies.list.params',
@@ -170,7 +194,7 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
}
const obs = this.hooks.wrapObsFun(
- this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
+ this.videoCommentService.listThreads.bind(this.videoCommentService),
params,
'video-watch',
'filter:api.video-watch.video-threads.list.params',
@@ -211,6 +235,61 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
this.viewReplies(commentTree.comment.id)
}
+ // ---------------------------------------------------------------------------
+ // Replies that are too deeply nested to be displayed inline
+ // ---------------------------------------------------------------------------
+
+ onWantedToViewRepliesInModal (root: {
+ comment: VideoComment
+ parentComments: VideoComment[]
+ }): void {
+ this.repliesModalStack.push(root)
+ this.setRepliesModalRoot(root.comment)
+
+ if (this.repliesModalRef) return
+
+ this.repliesModalRef = this.modalService.open(this.repliesModal(), { centered: true, size: 'lg' })
+
+ // The result promise rejects when the modal is dismissed (ESC/backdrop click): nothing to do
+ this.repliesModalRef.result
+ .catch((): undefined => undefined)
+ .finally(() => {
+ this.repliesModalRef = undefined
+ this.repliesModalStack = []
+ this.repliesModalTree = undefined
+ })
+ }
+
+ onRepliesModalBack () {
+ this.repliesModalStack.pop()
+
+ const previousRoot = this.getRepliesModalRoot()
+ if (!previousRoot) return this.closeRepliesModal()
+
+ this.setRepliesModalRoot(previousRoot.comment)
+ }
+
+ closeRepliesModal () {
+ if (!this.repliesModalRef) return
+
+ this.repliesModalRef.close()
+ }
+
+ getRepliesModalRoot () {
+ return this.repliesModalStack[this.repliesModalStack.length - 1]
+ }
+
+ // The comment becomes a root comment again: my-video-comment fetches its replies itself
+ private setRepliesModalRoot (comment: VideoComment) {
+ this.repliesModalTree = {
+ comment,
+ children: [],
+ totalChildren: comment.totalReplies,
+ fetchedChildren: 0,
+ hasDisplayedChildren: true
+ }
+ }
+
handleSortChange (sort: string) {
if (this.sort === sort) return
@@ -311,6 +390,8 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
const video = this.video()
if (video.commentsPolicy.id === VideoCommentPolicy.DISABLED) return
+ this.closeRepliesModal()
+
// Reset all our fields
this.highlightedThread = null
this.comments = []
diff --git a/client/src/app/shared/shared-video-comment/video-comment-list-admin-owner.component.ts b/client/src/app/shared/shared-video-comment/video-comment-list-admin-owner.component.ts
index ea9c647dfe..e5c003e1da 100644
--- a/client/src/app/shared/shared-video-comment/video-comment-list-admin-owner.component.ts
+++ b/client/src/app/shared/shared-video-comment/video-comment-list-admin-owner.component.ts
@@ -1,4 +1,4 @@
-import { Component, OnDestroy, OnInit, inject, input, viewChild, ChangeDetectionStrategy } from '@angular/core'
+import { ChangeDetectionStrategy, Component, OnDestroy, OnInit, inject, input, viewChild } from '@angular/core'
import { RouterLink } from '@angular/router'
import { AuthService, ConfirmService, HooksService, MarkdownService, Notifier, PluginService } from '@app/core'
import { formatICU } from '@app/helpers'
@@ -309,12 +309,12 @@ export class VideoCommentListAdminOwnerComponent implements OnInit, OnDestroy {
private _dataLoader (
options:
& DataLoaderOptionsBase
- & Parameters<VideoCommentService['listAdminVideoComments']>[0]
- & Parameters<VideoCommentService['listVideoCommentsOfMyVideos']>[0]
+ & Parameters<VideoCommentService['listAdminComments']>[0]
+ & Parameters<VideoCommentService['listCommentsOfMyVideos']>[0]
) {
const method = this.mode() === 'admin'
- ? this.videoCommentService.listAdminVideoComments.bind(this.videoCommentService)
- : this.videoCommentService.listVideoCommentsOfMyVideos.bind(this.videoCommentService)
+ ? this.videoCommentService.listAdminComments.bind(this.videoCommentService)
+ : this.videoCommentService.listCommentsOfMyVideos.bind(this.videoCommentService)
return method(options)
.pipe(
diff --git a/client/src/app/shared/shared-video-comment/video-comment-thread-tree.model.ts b/client/src/app/shared/shared-video-comment/video-comment-thread-tree.model.ts
index 62683f57fa..c67b59c4a2 100644
--- a/client/src/app/shared/shared-video-comment/video-comment-thread-tree.model.ts
+++ b/client/src/app/shared/shared-video-comment/video-comment-thread-tree.model.ts
@@ -4,5 +4,11 @@ import { VideoComment } from './video-comment.model'
export class VideoCommentThreadTree implements VideoCommentThreadTreeServerModel {
comment: VideoComment
hasDisplayedChildren: boolean
+
+ // The server truncates the tree in depth and in width, so `children` may contain less elements than `totalChildren`
children: VideoCommentThreadTree[]
+ totalChildren: number
+
+ // Number of children actually fetched from the server (unlike `children.length`, doesn't count replies inserted locally)
+ fetchedChildren: number
}
diff --git a/client/src/app/shared/shared-video-comment/video-comment.service.ts b/client/src/app/shared/shared-video-comment/video-comment.service.ts
index 0d3f36148e..7cad34e208 100644
--- a/client/src/app/shared/shared-video-comment/video-comment.service.ts
+++ b/client/src/app/shared/shared-video-comment/video-comment.service.ts
@@ -1,6 +1,6 @@
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
-import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core'
+import { ComponentPaginationLight, RestExtractor, RestPagination, RestService, ScreenService } from '@app/core'
import { objectLineFeedToHtml } from '@app/helpers'
import {
FeedFormat,
@@ -25,9 +25,12 @@ export class VideoCommentService {
private authHttp = inject(HttpClient)
private restExtractor = inject(RestExtractor)
private restService = inject(RestService)
+ private screenService = inject(ScreenService)
static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.'
+ static REPLIES_PER_PAGE = 10
+
private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
private static BASE_ME_URL = environment.apiUrl + '/api/v1/users/me/'
@@ -76,8 +79,8 @@ export class VideoCommentService {
// ---------------------------------------------------------------------------
- listVideoCommentsOfMyVideos (
- options: Parameters<VideoCommentService['buildAdminVideoCommentsParams']>[0] & {
+ listCommentsOfMyVideos (
+ options: Parameters<VideoCommentService['buildAdminCommentsParams']>[0] & {
isHeldForReview?: boolean
}
): Observable<ResultList<VideoCommentForAdminOrUser>> {
@@ -85,7 +88,7 @@ export class VideoCommentService {
const url = VideoCommentService.BASE_ME_URL + 'videos/comments'
- let params = this.buildAdminVideoCommentsParams(options)
+ let params = this.buildAdminCommentsParams(options)
params = params.set('includeCollaborations', 'true')
if (isHeldForReview !== undefined) {
@@ -98,8 +101,8 @@ export class VideoCommentService {
)
}
- listAdminVideoComments (
- options: Parameters<VideoCommentService['buildAdminVideoCommentsParams']>[0] & {
+ listAdminComments (
+ options: Parameters<VideoCommentService['buildAdminCommentsParams']>[0] & {
isLocal?: boolean
onLocalVideo?: boolean
excludeMuted?: boolean
@@ -109,7 +112,7 @@ export class VideoCommentService {
const url = VideoCommentService.BASE_VIDEO_URL + 'comments'
- let params = this.buildAdminVideoCommentsParams(options)
+ let params = this.buildAdminCommentsParams(options)
if (isLocal !== undefined) {
params = params.set('isLocal', '' + isLocal)
@@ -132,7 +135,7 @@ export class VideoCommentService {
)
}
- private buildAdminVideoCommentsParams (options: {
+ private buildAdminCommentsParams (options: {
pagination: RestPagination
sort: SortMeta
search?: string
@@ -151,7 +154,7 @@ export class VideoCommentService {
// ---------------------------------------------------------------------------
- getVideoCommentThreads (parameters: {
+ listThreads (parameters: {
videoId: string
videoPassword: string
componentPagination: ComponentPaginationLight
@@ -174,23 +177,64 @@ export class VideoCommentService {
)
}
- getVideoThreadComments (parameters: {
+ getThread (parameters: {
videoId: string
threadId: number
+ maxDepth?: number
videoPassword?: string
}): Observable<VideoCommentThreadTree> {
- const { videoId, threadId, videoPassword } = parameters
+ const { videoId, threadId, maxDepth, videoPassword } = parameters
const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
const headers = VideoPasswordService.buildVideoPasswordHeader(videoPassword)
+ let params = new HttpParams().set('repliesPerLevel', VideoCommentService.REPLIES_PER_PAGE)
+ if (maxDepth) params = params.set('maxDepth', maxDepth)
+
return this.authHttp
- .get<VideoCommentThreadTreeServerModel>(url, { headers })
+ .get<VideoCommentThreadTreeServerModel>(url, { params, headers })
.pipe(
map(tree => this.extractVideoCommentTree(tree)),
catchError(err => this.restExtractor.handleError(err))
)
}
+ // Fetch the replies of a comment that the truncated thread tree did not include
+ getMissingReplies (parameters: {
+ videoId: string
+ commentId: number
+ pagination: RestPagination
+ maxDepth?: number
+ videoPassword?: string
+ }): Observable<ResultList<VideoCommentThreadTree>> {
+ const { videoId, commentId, pagination, maxDepth, videoPassword } = parameters
+
+ const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}/replies`
+ const headers = VideoPasswordService.buildVideoPasswordHeader(videoPassword)
+
+ let params = new HttpParams()
+ params = this.restService.addRestGetParams(params, pagination, 'createdAt')
+ params = params.set('repliesPerLevel', VideoCommentService.REPLIES_PER_PAGE)
+ if (maxDepth) params = params.set('maxDepth', maxDepth)
+
+ return this.authHttp
+ .get<ResultList<VideoCommentThreadTreeServerModel>>(url, { params, headers })
+ .pipe(
+ map(({ total, data }) => ({ total, data: data.map(tree => this.extractVideoCommentTree(tree)) })),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ // ---------------------------------------------------------------------------
+
+ // Nesting depth of comments the video-watch page can render inline before it has to continue them in a modal
+ getMaxInlineCommentDepth () {
+ if (this.screenService.isInMobileView()) return 2
+ if (this.screenService.isInSmallView()) return 3
+ if (this.screenService.isInMenuOverlayView()) return 4
+
+ return 5
+ }
+
// ---------------------------------------------------------------------------
deleteComments (comments: { videoId: number | string, commentId: number }[]) {
@@ -255,14 +299,21 @@ export class VideoCommentService {
private extractVideoCommentTree (serverTree: VideoCommentThreadTreeServerModel): VideoCommentThreadTree {
if (!serverTree) return null
+ const children = serverTree.children.map(c => this.extractVideoCommentTree(c))
+
const tree = {
comment: new VideoComment(serverTree.comment),
- children: serverTree.children.map(c => this.extractVideoCommentTree(c))
+ children,
+ totalChildren: serverTree.totalChildren,
+ fetchedChildren: children.length
}
+ // Replies we did not fetch yet also count: the user can still unfold them
+ const hasNotFetchedChildren = tree.totalChildren > tree.children.length
+
const hasDisplayedChildren = tree.children.length === 0
- ? !tree.comment.isDeleted
- : tree.children.some(c => c.hasDisplayedChildren)
+ ? !tree.comment.isDeleted || hasNotFetchedChildren
+ : tree.children.some(c => c.hasDisplayedChildren) || hasNotFetchedChildren
return Object.assign(tree, { hasDisplayedChildren })
}
diff --git a/packages/models/src/plugins/client/client-hook.model.ts b/packages/models/src/plugins/client/client-hook.model.ts
index 6e9c40aba2..769f15e52c 100644
--- a/packages/models/src/plugins/client/client-hook.model.ts
+++ b/packages/models/src/plugins/client/client-hook.model.ts
@@ -42,6 +42,11 @@ export const clientFilterHookObject = {
'filter:api.video-watch.video-thread-replies.list.params': true,
'filter:api.video-watch.video-thread-replies.list.result': true,
+ // Filter params/result of the function that fetch the replies of a comment nested in a thread of the video-watch page
+ // PeerTube >= 8.3
+ 'filter:api.video-watch.video-comment-replies.list.params': true,
+ 'filter:api.video-watch.video-comment-replies.list.result': true,
+
// Filter params/result of the function that fetch videos according to the user search
'filter:api.search.videos.list.params': true,
'filter:api.search.videos.list.result': true,
diff --git a/packages/models/src/plugins/server/server-hook.model.ts b/packages/models/src/plugins/server/server-hook.model.ts
index 24db010007..2852ae4107 100644
--- a/packages/models/src/plugins/server/server-hook.model.ts
+++ b/packages/models/src/plugins/server/server-hook.model.ts
@@ -90,6 +90,11 @@ export const serverFilterHookObject = {
'filter:api.video-thread-comments.list.params': true,
'filter:api.video-thread-comments.list.result': true,
+ // Filter params/result used to list replies of a specific comment
+ // PeerTube >= 8.3
+ 'filter:api.video-comment-replies.list.params': true,
+ 'filter:api.video-comment-replies.list.result': true,
+
// Filter get stats result
'filter:api.server.stats.get.result': true,
diff --git a/packages/models/src/videos/comment/video-comment.model.ts b/packages/models/src/videos/comment/video-comment.model.ts
index f944119112..e31c6afcab 100644
--- a/packages/models/src/videos/comment/video-comment.model.ts
+++ b/packages/models/src/videos/comment/video-comment.model.ts
@@ -54,5 +54,12 @@ export type VideoCommentThreads = ResultList<VideoComment> & { totalNotDeletedCo
export interface VideoCommentThreadTree {
comment: VideoComment
+
+ // May contain less elements than `totalChildren`: the tree is truncated in depth and in width
children: VideoCommentThreadTree[]
+
+ // Total direct replies of `comment` the current user can see
+ totalChildren: number
}
+
+export type VideoCommentReplies = ResultList<VideoCommentThreadTree>
diff --git a/packages/server-commands/src/videos/comments-command.ts b/packages/server-commands/src/videos/comments-command.ts
index 87ca60b66d..f784c67945 100644
--- a/packages/server-commands/src/videos/comments-command.ts
+++ b/packages/server-commands/src/videos/comments-command.ts
@@ -4,6 +4,7 @@ import {
ResultList,
VideoComment,
VideoCommentForAdminOrUser,
+ VideoCommentReplies,
VideoCommentThreads,
VideoCommentThreadTree
} from '@peertube/peertube-models'
@@ -106,6 +107,8 @@ export class CommentsCommand extends AbstractCommand {
options: OverrideCommandOptions & {
videoId: number | string
threadId: number
+ maxDepth?: number
+ repliesPerLevel?: number
}
) {
const { videoId, threadId } = options
@@ -115,6 +118,33 @@ export class CommentsCommand extends AbstractCommand {
...options,
path,
+ query: pick(options, [ 'maxDepth', 'repliesPerLevel' ]),
+ implicitToken: false,
+ defaultExpectedStatus: HttpStatusCode.OK_200
+ })
+ }
+
+ listReplies (
+ options: OverrideCommandOptions & {
+ videoId: number | string
+ commentId: number
+ start?: number
+ count?: number
+ sort?: string
+ maxDepth?: number
+ repliesPerLevel?: number
+ videoPassword?: string
+ }
+ ) {
+ const { videoId, commentId, videoPassword } = options
+ const path = '/api/v1/videos/' + videoId + '/comments/' + commentId + '/replies'
+
+ return this.getRequestBody<VideoCommentReplies>({
+ ...options,
+
+ path,
+ query: pick(options, [ 'start', 'count', 'sort', 'maxDepth', 'repliesPerLevel' ]),
+ headers: this.buildVideoPasswordHeader(videoPassword),
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
})
diff --git a/packages/tests/src/api/check-params/video-comments.ts b/packages/tests/src/api/check-params/video-comments.ts
index fb371ccdb4..e181110e3a 100644
--- a/packages/tests/src/api/check-params/video-comments.ts
+++ b/packages/tests/src/api/check-params/video-comments.ts
@@ -159,6 +159,39 @@ describe('Test video comments API validator', function () {
})
})
+ it('Should fail with an incorrect maxDepth', async function () {
+ for (const maxDepth of [ 'toto', -1, 0, 11 ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
+ query: { maxDepth },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should fail with an incorrect repliesPerLevel', async function () {
+ for (const repliesPerLevel of [ 'toto', -1, 0, 31 ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
+ query: { repliesPerLevel },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should fail with a maxDepth/repliesPerLevel combination that searches too many comments', async function () {
+ for (const query of [ { maxDepth: 10, repliesPerLevel: 30 }, { maxDepth: 7 }, { repliesPerLevel: 30, maxDepth: 5 } ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
+ query,
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
it('Should succeed with the correct params', async function () {
for (const token of [ server.accessToken, editorToken ]) {
await makeGetRequest({
@@ -169,9 +202,156 @@ describe('Test video comments API validator', function () {
})
}
+ for (const query of [ { maxDepth: 10, repliesPerLevel: 2 }, { maxDepth: 3, repliesPerLevel: 30 }, {} ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
+ query,
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ }
+ })
+ })
+
+ describe('When listing replies of a comment', function () {
+ let pathReplies: string
+
+ before(function () {
+ pathReplies = '/api/v1/videos/' + video.shortUUID + '/comments/' + commentId + '/replies'
+ })
+
+ it('Should fail with a bad start pagination', async function () {
+ await checkBadStartPagination(server.url, pathReplies, server.accessToken)
+ })
+
+ it('Should fail with a bad count pagination', async function () {
+ await checkBadCountPagination(server.url, pathReplies, server.accessToken)
+ })
+
+ it('Should fail with an incorrect sort', async function () {
+ await checkBadSort(server.url, pathReplies, server.accessToken)
+
+ // Only threads can be sorted by total replies
+ for (const sort of [ 'totalReplies', '-totalReplies' ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query: { sort },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should succeed with a correct sort', async function () {
+ for (const sort of [ 'createdAt', '-createdAt' ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query: { sort },
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ }
+ })
+
+ it('Should fail with an incorrect video', async function () {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId + '/replies',
+ expectedStatus: HttpStatusCode.NOT_FOUND_404
+ })
+ })
+
+ it('Should fail with an incorrect comment id', async function () {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + video.shortUUID + '/comments/156/replies',
+ expectedStatus: HttpStatusCode.NOT_FOUND_404
+ })
+ })
+
+ it('Should fail with a comment of another video', async function () {
+ await makeGetRequest({
+ url: server.url,
+ token: server.accessToken,
+ path: '/api/v1/videos/' + video.shortUUID + '/comments/' + privateCommentId + '/replies',
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ })
+
+ it('Should fail with an incorrect maxDepth', async function () {
+ for (const maxDepth of [ 'toto', -1, 0, 11 ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query: { maxDepth },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should fail with an incorrect repliesPerLevel', async function () {
+ for (const repliesPerLevel of [ 'toto', -1, 0, 31 ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query: { repliesPerLevel },
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should fail with a maxDepth/repliesPerLevel combination that searches too many comments', async function () {
+ // A high count multiplies every level, so it can break an otherwise valid maxDepth/repliesPerLevel pair
+ for (const query of [ { maxDepth: 10, repliesPerLevel: 30 }, { count: 100, maxDepth: 6 }, { repliesPerLevel: 30, maxDepth: 5 } ]) {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query,
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+ }
+ })
+
+ it('Should succeed with a high count and the default depth', async function () {
+ await makeGetRequest({
+ url: server.url,
+ path: pathReplies,
+ query: { count: 100 },
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ })
+
+ it('Should fail with a private video without token', async function () {
+ await makeGetRequest({
+ url: server.url,
+ path: '/api/v1/videos/' + privateVideo.shortUUID + '/comments/' + privateCommentId + '/replies',
+ expectedStatus: HttpStatusCode.UNAUTHORIZED_401
+ })
+ })
+
+ it('Should fail with another user token', async function () {
+ await makeGetRequest({
+ url: server.url,
+ token: userAccessToken,
+ path: '/api/v1/videos/' + privateVideo.shortUUID + '/comments/' + privateCommentId + '/replies',
+ expectedStatus: HttpStatusCode.FORBIDDEN_403
+ })
+ })
+
+ it('Should succeed with the correct params', async function () {
+ for (const token of [ server.accessToken, editorToken ]) {
+ await makeGetRequest({
+ url: server.url,
+ token,
+ path: '/api/v1/videos/' + privateVideo.shortUUID + '/comments/' + privateCommentId + '/replies',
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ }
+
await makeGetRequest({
url: server.url,
- path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
+ path: pathReplies,
+ query: { start: 0, count: 5, sort: 'createdAt', maxDepth: 10, repliesPerLevel: 2 },
expectedStatus: HttpStatusCode.OK_200
})
})
diff --git a/packages/tests/src/api/videos/video-comments.ts b/packages/tests/src/api/videos/video-comments.ts
index 4b0b0f5fad..4e24223662 100644
--- a/packages/tests/src/api/videos/video-comments.ts
+++ b/packages/tests/src/api/videos/video-comments.ts
@@ -472,6 +472,278 @@ describe('Test video comments', function () {
})
})
+ describe('Comment tree truncation', function () {
+ let treeVideoId: number
+ let treeVideoUUID: string
+ let deepThreadId: number
+ let wideThreadId: number
+
+ before(async function () {
+ this.timeout(120000)
+
+ const { id, uuid } = await server.videos.upload()
+ treeVideoId = id
+ treeVideoUUID = uuid
+ })
+
+ it('Should truncate the thread tree in depth', async function () {
+ const thread = await command.createThread({ videoId: treeVideoUUID, text: 'depth 0' })
+ deepThreadId = thread.id
+
+ let parentId = thread.id
+ for (let i = 1; i <= 8; i++) {
+ const reply = await command.addReply({ videoId: treeVideoId, toCommentId: parentId, text: 'depth ' + i })
+ parentId = reply.id
+ }
+
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: deepThreadId })
+
+ let node = tree
+ for (let i = 1; i <= 5; i++) {
+ expect(node.children, 'children of depth ' + (i - 1)).to.have.lengthOf(1)
+ expect(node.totalChildren).to.equal(1)
+
+ node = node.children[0]
+ expect(node.comment.text).to.equal('depth ' + i)
+ }
+
+ // Deeper replies have to be re-fetched
+ expect(node.children).to.have.lengthOf(0)
+ expect(node.totalChildren).to.equal(1)
+ })
+
+ it('Should respect the maxDepth parameter', async function () {
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: deepThreadId, maxDepth: 1 })
+
+ expect(tree.children).to.have.lengthOf(1)
+ expect(tree.children[0].comment.text).to.equal('depth 1')
+ expect(tree.children[0].children).to.have.lengthOf(0)
+ expect(tree.children[0].totalChildren).to.equal(1)
+ })
+
+ it('Should fetch the replies of a nested comment', async function () {
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: deepThreadId, maxDepth: 5 })
+
+ let node = tree
+ for (let i = 1; i <= 5; i++) node = node.children[0]
+
+ const { total, data } = await command.listReplies({ videoId: treeVideoUUID, commentId: node.comment.id })
+ expect(total).to.equal(1)
+ expect(data).to.have.lengthOf(1)
+
+ expect(data[0].comment.text).to.equal('depth 6')
+ expect(data[0].children).to.have.lengthOf(1)
+ expect(data[0].children[0].comment.text).to.equal('depth 7')
+ })
+
+ it('Should truncate the thread tree in width', async function () {
+ this.timeout(60000)
+
+ const thread = await command.createThread({ videoId: treeVideoUUID, text: 'wide thread' })
+
+ for (let i = 0; i < 13; i++) {
+ await command.addReply({ videoId: treeVideoId, toCommentId: thread.id, text: 'wide reply ' + i })
+ }
+
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: thread.id })
+ expect(tree.children).to.have.lengthOf(10)
+ expect(tree.totalChildren).to.equal(13)
+ expect(tree.children[0].comment.text).to.equal('wide reply 0')
+
+ const { total, data } = await command.listReplies({
+ videoId: treeVideoUUID,
+ commentId: thread.id,
+ start: 10,
+ count: 10,
+ sort: 'createdAt'
+ })
+ expect(total).to.equal(13)
+ expect(data).to.have.lengthOf(3)
+ expect(data[0].comment.text).to.equal('wide reply 10')
+ expect(data[2].comment.text).to.equal('wide reply 12')
+
+ wideThreadId = thread.id
+ })
+
+ it('Should respect the repliesPerLevel parameter', async function () {
+ {
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: wideThreadId, repliesPerLevel: 3 })
+
+ expect(tree.children).to.have.lengthOf(3)
+ expect(tree.totalChildren).to.equal(13)
+ expect(tree.children[0].comment.text).to.equal('wide reply 0')
+ }
+
+ {
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: wideThreadId, repliesPerLevel: 13 })
+
+ expect(tree.children).to.have.lengthOf(13)
+ expect(tree.totalChildren).to.equal(13)
+ }
+ })
+
+ it('Should limit the replies of every parent of a nested level', async function () {
+ this.timeout(120000)
+
+ const thread = await command.createThread({ videoId: treeVideoUUID, text: 'nested wide thread' })
+
+ for (const parentText of [ 'parent 0', 'parent 1' ]) {
+ const parent = await command.addReply({ videoId: treeVideoId, toCommentId: thread.id, text: parentText })
+
+ for (let i = 0; i < 12; i++) {
+ await command.addReply({ videoId: treeVideoId, toCommentId: parent.id, text: `${parentText} reply ${i}` })
+ }
+ }
+
+ const tree = await command.getThread({ videoId: treeVideoUUID, threadId: thread.id })
+ expect(tree.children).to.have.lengthOf(2)
+
+ for (const child of tree.children) {
+ expect(child.children).to.have.lengthOf(10)
+ expect(child.totalChildren).to.equal(12)
+ expect(child.children[0].comment.text).to.equal(`${child.comment.text} reply 0`)
+ }
+ })
+ })
+
+ describe('Comment replies sort', function () {
+ let treeVideoId: number
+ let treeVideoUUID: string
+ let wideThreadId: number
+ let nestedWideThreadId: number
+
+ // Text of the replies of the wide thread, in the order they were created
+ const wideReplyTexts: string[] = []
+
+ before(async function () {
+ this.timeout(240000)
+
+ const { id, uuid } = await server.videos.upload()
+ treeVideoId = id
+ treeVideoUUID = uuid
+
+ {
+ const thread = await command.createThread({ videoId: treeVideoUUID, text: 'wide thread' })
+ wideThreadId = thread.id
+
+ for (let i = 0; i < 13; i++) {
+ const text = 'wide reply ' + i
+ await command.addReply({ videoId: treeVideoId, toCommentId: thread.id, text })
+ wideReplyTexts.push(text)
+ }
+ }
+
+ {
+ const thread = await command.createThread({ videoId: treeVideoUUID, text: 'nested wide thread' })
+ nestedWideThreadId = thread.id
+
+ for (const parentText of [ 'parent 0', 'parent 1' ]) {
+ const parent = await command.addReply({ videoId: treeVideoId, toCommentId: thread.id, text: parentText })
+
+ for (let i = 0; i < 3; i++) {
+ await command.addReply({ videoId: treeVideoId, toCommentId: parent.id, text: `${parentText} reply ${i}` })
+ }
+ }
+ }
+ })
+
+ it('Should sort the replies chronologically by default, like the thread tree', async function () {
+ const { total, data } = await command.listReplies({ videoId: treeVideoUUID, commentId: wideThreadId })
+
+ expect(total).to.equal(13)
+ expect(data.map(d => d.comment.text)).to.deep.equal(wideReplyTexts)
+ })
+
+ it('Should sort the replies in reverse chronological order', async function () {
+ const { total, data } = await command.listReplies({ videoId: treeVideoUUID, commentId: wideThreadId, sort: '-createdAt' })
+
+ expect(total).to.equal(13)
+ expect(data.map(d => d.comment.text)).to.deep.equal([ ...wideReplyTexts ].reverse())
+ })
+
+ it('Should paginate the replies consistently with the sort', async function () {
+ for (const sort of [ 'createdAt', '-createdAt' ]) {
+ const expectedTexts = sort === 'createdAt'
+ ? wideReplyTexts
+ : [ ...wideReplyTexts ].reverse()
+
+ const fetchedTexts: string[] = []
+
+ for (let start = 0; start < 13; start += 5) {
+ const { total, data } = await command.listReplies({
+ videoId: treeVideoUUID,
+ commentId: wideThreadId,
+ start,
+ count: 5,
+ sort
+ })
+
+ expect(total, 'total of ' + sort).to.equal(13)
+ fetchedTexts.push(...data.map(d => d.comment.text))
+ }
+
+ // No reply is duplicated or skipped when unfolding page after page
+ expect(fetchedTexts, 'replies of ' + sort).to.deep.equal(expectedTexts)
+ }
+ })
+
+ it('Should apply the sort to the nested levels too', async function () {
+ {
+ const { data } = await command.listReplies({ videoId: treeVideoUUID, commentId: nestedWideThreadId, sort: 'createdAt' })
+
+ expect(data.map(d => d.comment.text)).to.deep.equal([ 'parent 0', 'parent 1' ])
+
+ for (const parent of data) {
+ expect(parent.children.map(c => c.comment.text))
+ .to.deep.equal([ 0, 1, 2 ].map(i => `${parent.comment.text} reply ${i}`))
+ }
+ }
+
+ {
+ const { data } = await command.listReplies({ videoId: treeVideoUUID, commentId: nestedWideThreadId, sort: '-createdAt' })
+
+ expect(data.map(d => d.comment.text)).to.deep.equal([ 'parent 1', 'parent 0' ])
+
+ for (const parent of data) {
+ expect(parent.children.map(c => c.comment.text))
+ .to.deep.equal([ 2, 1, 0 ].map(i => `${parent.comment.text} reply ${i}`))
+ }
+ }
+ })
+
+ it('Should truncate the widest replies of the sort', async function () {
+ {
+ const { data } = await command.listReplies({
+ videoId: treeVideoUUID,
+ commentId: nestedWideThreadId,
+ sort: '-createdAt',
+ repliesPerLevel: 2
+ })
+
+ for (const parent of data) {
+ expect(parent.children.map(c => c.comment.text))
+ .to.deep.equal([ 2, 1 ].map(i => `${parent.comment.text} reply ${i}`))
+ expect(parent.totalChildren).to.equal(3)
+ }
+ }
+
+ {
+ const { data } = await command.listReplies({
+ videoId: treeVideoUUID,
+ commentId: nestedWideThreadId,
+ sort: 'createdAt',
+ repliesPerLevel: 2
+ })
+
+ for (const parent of data) {
+ expect(parent.children.map(c => c.comment.text))
+ .to.deep.equal([ 0, 1 ].map(i => `${parent.comment.text} reply ${i}`))
+ expect(parent.totalChildren).to.equal(3)
+ }
+ }
+ })
+ })
+
describe('Disabling remote comments', function () {
let server2: PeerTubeServer
let server3: PeerTubeServer
diff --git a/server/core/controllers/api/videos/comment.ts b/server/core/controllers/api/videos/comment.ts
index 18a49b8cd4..e0d1ee1c7a 100644
--- a/server/core/controllers/api/videos/comment.ts
+++ b/server/core/controllers/api/videos/comment.ts
@@ -1,14 +1,22 @@
import { pick } from '@peertube/peertube-core-utils'
import { HttpStatusCode, ResultList, ThreadsResultList, UserRight, VideoCommentCreate, VideoCommentPolicy } from '@peertube/peertube-models'
+import { getAuthUser } from '@server/helpers/express-utils.js'
import { getServerActor } from '@server/models/application/application.js'
import { MCommentFormattable } from '@server/types/models/index.js'
import express from 'express'
import { CommentAuditView, auditLoggerFactory, getAuditIdFromRes } from '../../../helpers/audit-logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { CONFIG } from '../../../initializers/config.js'
+import { VIDEO_COMMENTS_TREE } from '../../../initializers/constants.js'
import { Notifier } from '../../../lib/notifier/index.js'
import { Hooks } from '../../../lib/plugins/hooks.js'
-import { approveComment, buildFormattedCommentTree, createLocalVideoComment, removeComment } from '../../../lib/video-comment.js'
+import {
+ approveComment,
+ buildFormattedCommentTree,
+ buildFormattedCommentTrees,
+ createLocalVideoComment,
+ removeComment
+} from '../../../lib/video-comment.js'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
@@ -17,6 +25,7 @@ import {
ensureUserHasRight,
optionalAuthenticate,
paginationValidator,
+ setDefaultCommentRepliesSort,
setDefaultPagination,
setDefaultSort
} from '../../../middlewares/index.js'
@@ -25,6 +34,7 @@ import {
addVideoCommentThreadValidator,
approveVideoCommentValidator,
listAllVideoCommentsForAdminValidator,
+ listVideoCommentRepliesValidator,
listVideoCommentThreadsValidator,
listVideoThreadCommentsValidator,
removeVideoCommentValidator,
@@ -60,6 +70,16 @@ videoCommentRouter.get(
optionalAuthenticate,
asyncMiddleware(listVideoThreadComments)
)
+videoCommentRouter.get(
+ '/:videoId/comments/:commentId/replies',
+ paginationValidator,
+ videoCommentsValidator,
+ setDefaultCommentRepliesSort,
+ setDefaultPagination,
+ optionalAuthenticate,
+ asyncMiddleware(listVideoCommentRepliesValidator),
+ asyncMiddleware(listVideoCommentReplies)
+)
videoCommentRouter.post(
'/:videoId/comment-threads',
@@ -180,7 +200,7 @@ async function listVideoThreads (req: express.Request, res: express.Response) {
async function listVideoThreadComments (req: express.Request, res: express.Response) {
const video = res.locals.videoWithBlacklist
- const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
+ const user = getAuthUser(res)
let resultList: ResultList<MCommentFormattable>
@@ -188,14 +208,22 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
const apiOptions = await Hooks.wrapObject({
video,
threadId: res.locals.videoCommentThread.id,
+ maxDepth: req.query.maxDepth ?? VIDEO_COMMENTS_TREE.DEPTH.DEFAULT,
+ repliesPerLevel: req.query.repliesPerLevel ?? VIDEO_COMMENTS_TREE.REPLIES_PER_LEVEL.DEFAULT,
user
}, 'filter:api.video-thread-comments.list.params')
- resultList = await Hooks.wrapPromiseFun(
- VideoCommentModel.listThreadCommentsForApi.bind(VideoCommentModel),
- apiOptions,
- 'filter:api.video-thread-comments.list.result'
- )
+ const result = await VideoCommentModel.listThreadCommentsForApi(apiOptions)
+
+ // This hook predates the reply tree feature: it historically received `{ total, data }` with the
+ // thread root as `data[0]`, so keep that contract instead of exposing the new `{ comment, total, data }` shape
+ resultList = result.comment
+ ? await Hooks.wrapObject(
+ { total: result.total, data: [ result.comment, ...result.data ] },
+ 'filter:api.video-thread-comments.list.result',
+ apiOptions
+ )
+ : { total: 0, data: [] }
} else {
resultList = {
total: 0,
@@ -210,7 +238,50 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
})
}
- return res.json(buildFormattedCommentTree(resultList))
+ const [ comment, ...replies ] = resultList.data
+
+ return res.json(buildFormattedCommentTree({
+ comment,
+ totalChildren: resultList.total,
+ replies
+ }))
+}
+
+async function listVideoCommentReplies (req: express.Request, res: express.Response) {
+ const video = res.locals.videoWithBlacklist
+ const comment = res.locals.videoCommentFull
+ const user = getAuthUser(res)
+
+ let resultList: ResultList<MCommentFormattable>
+
+ if (video.commentsPolicy !== VideoCommentPolicy.DISABLED) {
+ const apiOptions = await Hooks.wrapObject({
+ video,
+ parentCommentId: comment.id,
+ start: req.query.start,
+ count: req.query.count,
+ sort: req.query.sort,
+ maxDepth: req.query.maxDepth ?? VIDEO_COMMENTS_TREE.DEPTH.DEFAULT,
+ repliesPerLevel: req.query.repliesPerLevel ?? VIDEO_COMMENTS_TREE.REPLIES_PER_LEVEL.DEFAULT,
+ user
+ }, 'filter:api.video-comment-replies.list.params')
+
+ resultList = await Hooks.wrapPromiseFun(
+ VideoCommentModel.listRepliesForApi.bind(VideoCommentModel),
+ apiOptions,
+ 'filter:api.video-comment-replies.list.result'
+ )
+ } else {
+ resultList = {
+ total: 0,
+ data: []
+ }
+ }
+
+ return res.json({
+ total: resultList.total,
+ data: buildFormattedCommentTrees({ parentCommentId: comment.id, replies: resultList.data })
+ })
}
async function addVideoCommentThread (req: express.Request, res: express.Response) {
diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.ts
index 56fe0d4879..febb40f15b 100644
--- a/server/core/initializers/constants.ts
+++ b/server/core/initializers/constants.ts
@@ -62,7 +62,7 @@ import { CONFIG, registerConfigChangedHandler } from './config.js'
// ---------------------------------------------------------------------------
-export const LAST_MIGRATION_VERSION = 1120
+export const LAST_MIGRATION_VERSION = 1125
// ---------------------------------------------------------------------------
@@ -83,6 +83,22 @@ export const PAGINATION = {
}
}
+// Comment trees are truncated so we don't have to build/send/render a whole thread at once
+export const VIDEO_COMMENTS_TREE = {
+ DEPTH: {
+ DEFAULT: 5,
+ MAX: 10
+ },
+ REPLIES_PER_LEVEL: {
+ DEFAULT: 10,
+ MAX: 30
+ },
+ MAX_COMMENTS_PER_REQUEST: 300,
+ // A tree can make the database walk through `count * (repliesPerLevel ^ maxDepth - 1) / (repliesPerLevel - 1)`
+ // comments before we truncate it to MAX_COMMENTS_PER_REQUEST
+ MAX_SEARCHED_COMMENTS: 2_000_000
+}
+
export const WEBSERVER = {
URL: '',
HOST: '',
diff --git a/server/core/initializers/migrations/1125-video-comment-replies-index.ts b/server/core/initializers/migrations/1125-video-comment-replies-index.ts
new file mode 100644
index 0000000000..9f40c9bd26
--- /dev/null
+++ b/server/core/initializers/migrations/1125-video-comment-replies-index.ts
@@ -0,0 +1,24 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+ transaction: Sequelize.Transaction
+ queryInterface: Sequelize.QueryInterface
+ sequelize: Sequelize.Sequelize
+}): Promise<void> {
+ const { transaction } = utils
+
+ // Redundant with a new index introduced in the model (auto created at startup)
+ await utils.sequelize.query(
+ `DROP INDEX IF EXISTS "video_comment_in_reply_to_comment_id"`,
+ { transaction }
+ )
+}
+
+function down () {
+ throw new Error('Not implemented.')
+}
+
+export {
+ down,
+ up
+}
diff --git a/server/core/lib/video-comment.ts b/server/core/lib/video-comment.ts
index 71b92661b2..e85dfa7fcd 100644
--- a/server/core/lib/video-comment.ts
+++ b/server/core/lib/video-comment.ts
@@ -1,4 +1,5 @@
-import { AutomaticTagPolicy, ResultList, UserRight, VideoCommentPolicy, VideoCommentThreadTree } from '@peertube/peertube-models'
+import { AutomaticTagPolicy, UserRight, VideoCommentPolicy, VideoCommentThreadTree } from '@peertube/peertube-models'
+import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { createLogger } from '@server/helpers/logger.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { AccountModel } from '@server/models/account/account.js'
@@ -22,7 +23,6 @@ import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'
import { setAndSaveCommentAutomaticTags } from './automatic-tags/automatic-tags.js'
import { Notifier } from './notifier/notifier.js'
import { Hooks } from './plugins/hooks.js'
-import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
const logger = createLogger()
@@ -130,36 +130,56 @@ export async function createLocalVideoComment (options: {
})
}
-export function buildFormattedCommentTree (resultList: ResultList<MCommentFormattable>): VideoCommentThreadTree {
- // Comments are sorted by id ASC
- const comments = resultList.data
+// `replies` is a flat and truncated view of the descendants of `parentCommentId
+export function buildFormattedCommentTrees (options: {
+ parentCommentId: number
+ replies: MCommentFormattable[]
+}): VideoCommentThreadTree[] {
+ const { parentCommentId, replies } = options
- const comment = comments.shift()
- const thread: VideoCommentThreadTree = {
- comment: comment.toFormattedJSON(),
- children: []
- }
- const idx = {
- [comment.id]: thread
- }
+ const roots: VideoCommentThreadTree[] = []
+ const idx: { [id: number]: VideoCommentThreadTree } = {}
+
+ for (const reply of replies) {
+ const formattedComment = reply.toFormattedJSON()
+
+ idx[reply.id] = {
+ comment: formattedComment,
+ children: [],
- while (comments.length !== 0) {
- const childComment = comments.shift()
+ totalChildren: formattedComment.totalReplies
+ }
+ }
- const childCommentThread: VideoCommentThreadTree = {
- comment: childComment.toFormattedJSON(),
- children: []
+ // The flat list is not sorted by depth, so we can only attach children once every node exists
+ for (const reply of replies) {
+ if (reply.inReplyToCommentId === parentCommentId) {
+ roots.push(idx[reply.id])
+ continue
}
- const parentCommentThread = idx[childComment.inReplyToCommentId]
- // Maybe the parent comment was blocked by the admin/user
- if (!parentCommentThread) continue
+ // Maybe the parent comment was blocked by the admin/user, or truncated from the tree
+ const parentNode = idx[reply.inReplyToCommentId]
+ if (!parentNode) continue
- parentCommentThread.children.push(childCommentThread)
- idx[childComment.id] = childCommentThread
+ parentNode.children.push(idx[reply.id])
}
- return thread
+ return roots
+}
+
+export function buildFormattedCommentTree (options: {
+ comment: MCommentFormattable
+ totalChildren: number
+ replies: MCommentFormattable[]
+}): VideoCommentThreadTree {
+ const { comment, totalChildren, replies } = options
+
+ return {
+ comment: comment.toFormattedJSON(),
+ children: buildFormattedCommentTrees({ parentCommentId: comment.id, replies }),
+ totalChildren
+ }
}
export async function shouldCommentBeHeldForReview (options: {
diff --git a/server/core/middlewares/sort.ts b/server/core/middlewares/sort.ts
index 408a8ebc68..3f8a0c513b 100644
--- a/server/core/middlewares/sort.ts
+++ b/server/core/middlewares/sort.ts
@@ -8,6 +8,9 @@ export const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')
export const setDefaultSearchSort = setDefaultSortFactory('-match')
export const setBlacklistSort = setDefaultSortFactory('-createdAt')
+// Replies are unfolded on top of a thread tree, which is always built in chronological order
+export const setDefaultCommentRepliesSort = setDefaultSortFactory('createdAt')
+
export const setLiveSessionsSort = setDefaultSortFactory('startDate')
// ---------------------------------------------------------------------------
diff --git a/server/core/middlewares/validators/videos/video-comments.ts b/server/core/middlewares/validators/videos/video-comments.ts
index c8f65a8f3c..896fe339fa 100644
--- a/server/core/middlewares/validators/videos/video-comments.ts
+++ b/server/core/middlewares/validators/videos/video-comments.ts
@@ -17,6 +17,7 @@ import {
} from '../../../helpers/custom-validators/misc.js'
import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments.js'
import { createLogger } from '../../../helpers/logger.js'
+import { VIDEO_COMMENTS_TREE } from '../../../initializers/constants.js'
import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation.js'
import { Hooks } from '../../../lib/plugins/hooks.js'
import { MCommentOwnerVideoReply, MVideo, MVideoAccountLight } from '../../../types/models/video/index.js'
@@ -123,12 +124,15 @@ export const listVideoCommentThreadsValidator = [
export const listVideoThreadCommentsValidator = [
isValidVideoIdParam('videoId'),
- param('threadId')
- .custom(isIdValid),
+ param('threadId').custom(isIdValid),
+
+ ...getCommentTreeValidators(),
+
isValidVideoPasswordHeader(),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
+ if (!checkCommentTreeSearchedComments(req, res, { useCountParam: false })) return
if (!await doesVideoExist(req.params.videoId, res, 'with-blacklist')) return
if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.videoWithBlacklist, res)) return
@@ -138,6 +142,70 @@ export const listVideoThreadCommentsValidator = [
}
]
+export const listVideoCommentRepliesValidator = [
+ isValidVideoIdParam('videoId'),
+
+ param('commentId').custom(isIdValid),
+
+ ...getCommentTreeValidators(),
+
+ isValidVideoPasswordHeader(),
+
+ async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ if (areValidationErrors(req, res)) return
+ if (!checkCommentTreeSearchedComments(req, res, { useCountParam: true })) return
+ if (!await doesVideoExist(req.params.videoId, res, 'with-blacklist')) return
+ if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoWithBlacklist, res)) return
+
+ if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.videoWithBlacklist })) return
+
+ return next()
+ }
+]
+
+function getCommentTreeValidators () {
+ return [
+ query('maxDepth')
+ .optional()
+ .customSanitizer(toIntOrNull)
+ .isInt({ min: 1, max: VIDEO_COMMENTS_TREE.DEPTH.MAX }),
+
+ query('repliesPerLevel')
+ .optional()
+ .customSanitizer(toIntOrNull)
+ .isInt({ min: 1, max: VIDEO_COMMENTS_TREE.REPLIES_PER_LEVEL.MAX })
+ ]
+}
+
+// maxDepth and repliesPerLevel are valid on their own but multiply each other, so we also have to check
+// how many comments the database may have to walk through to build the tree
+function checkCommentTreeSearchedComments (req: express.Request, res: express.Response, options: { useCountParam: boolean }) {
+ const { useCountParam } = options
+
+ const maxDepth = req.query.maxDepth ?? VIDEO_COMMENTS_TREE.DEPTH.DEFAULT
+ const repliesPerLevel = req.query.repliesPerLevel ?? VIDEO_COMMENTS_TREE.REPLIES_PER_LEVEL.DEFAULT
+ // The thread endpoint is not paginated (it always fetches `repliesPerLevel` comments on its first level)
+ // and does not have a pagination validator, so `count` is only meaningful (and validated) on the replies endpoint
+ const count = useCountParam
+ ? req.query.count
+ : repliesPerLevel
+
+ const searchedComments = repliesPerLevel === 1
+ ? count * maxDepth
+ : count * (Math.pow(repliesPerLevel, maxDepth) - 1) / (repliesPerLevel - 1)
+
+ if (searchedComments <= VIDEO_COMMENTS_TREE.MAX_SEARCHED_COMMENTS) return true
+
+ res.fail({
+ message: `This combination of count, repliesPerLevel and maxDepth could search up to ${Math.round(searchedComments)} comments, ` +
+ `and the maximum is ${VIDEO_COMMENTS_TREE.MAX_SEARCHED_COMMENTS}. Decrease maxDepth or repliesPerLevel`
+ })
+
+ return false
+}
+
+// ---------------------------------------------------------------------------
+
export const addVideoCommentThreadValidator = [
isValidVideoIdParam('videoId'),
diff --git a/server/core/models/video/sql/comment/video-comment-list-query-builder.ts b/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
index 90608c62ed..d6e4cf09c1 100644
--- a/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
+++ b/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
@@ -1,5 +1,6 @@
import { VideoChannelCollaboratorState, VideoPrivacy } from '@peertube/peertube-models'
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
+import { buildSortDirectionAndField } from '@server/models/shared/sort.js'
import { getAccountJoin, getActorJoin, getAvatarsJSONJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
import { Sequelize } from 'sequelize'
import { createSafeIn } from '../../../shared/index.js'
@@ -14,6 +15,23 @@ export interface ListVideoCommentsOptions extends AbstractListQueryOptions {
threadId?: number
accountId?: number
+ // Filer on these comments
+ commentIds?: number[]
+ // Filter on comments that are in reply to these comments
+ inReplyToCommentIds?: number[]
+
+ // Select a truncated tree of the replies of a comment
+ // First level is paginated using `start`/`count`
+ // Deeper level only keeps the first `repliesPerLevel` replies of each of its parents
+ replyTree?: {
+ parentCommentId: number
+ start: number
+ count: number
+ maxDepth: number
+ repliesPerLevel: number
+ maxComments: number
+ }
+
blockerAccountIds?: number[]
isThread?: boolean
@@ -37,6 +55,10 @@ export interface ListVideoCommentsOptions extends AbstractListQueryOptions {
searchVideo?: string
includeReplyCounters?: boolean
+
+ // The reply tree keeps deleted comments as tombstones so their own children stay attached:
+ // set this to also count them in `totalReplies`, or the count won't match what `replyTree`/`replies` actually return
+ totalRepliesIncludeDeleted?: boolean
}
export class VideoCommentListQueryBuilder extends AbstractListQuery {
@@ -60,6 +82,18 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
if (this.options.includeReplyCounters && !this.options.videoId) {
throw new Error('Cannot include reply counters without videoId')
}
+
+ if (this.options.replyTree && (this.options.start !== undefined || this.options.count !== undefined)) {
+ throw new Error('Cannot use start/count with replyTree, it is paginated using replyTree.start/replyTree.count')
+ }
+ }
+
+ // The reply tree needs a recursive CTE
+ // PostgreSQL accepts non recursive CTE in a `WITH RECURSIVE`, so we can always use it as soon as one of our CTE is recursive
+ protected buildCTE (cte: string[]) {
+ if (cte.length === 0 || !this.options.replyTree) return super.buildCTE(cte)
+
+ return `WITH RECURSIVE ${cte.join(', ')} `
}
// ---------------------------------------------------------------------------
@@ -78,6 +112,20 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
where.push('("VideoCommentModel"."id" = :threadId OR "VideoCommentModel"."originCommentId" = :threadId)')
}
+ if (this.options.commentIds) {
+ where.push(`"VideoCommentModel"."id" IN (${createSafeIn(this.sequelize, this.options.commentIds)})`)
+ }
+
+ if (this.options.inReplyToCommentIds) {
+ const idsString = createSafeIn(this.sequelize, this.options.inReplyToCommentIds)
+
+ where.push(`"VideoCommentModel"."inReplyToCommentId" IN (${idsString})`)
+ }
+
+ if (this.options.replyTree) {
+ this.buildReplyTreeCTE()
+ }
+
if (this.options.accountId) {
this.replacements.accountId = this.options.accountId
@@ -98,19 +146,7 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
where.push('"VideoCommentModel"."deletedAt" IS NULL')
}
- if (this.options.heldForReview === true) {
- where.push('"VideoCommentModel"."heldForReview" IS TRUE')
- } else if (this.options.heldForReview === false) {
- const base = '"VideoCommentModel"."heldForReview" IS FALSE'
-
- if (this.options.heldForReviewAccountIdException) {
- this.replacements.heldForReviewAccountIdException = this.options.heldForReviewAccountIdException
-
- where.push(`(${base} OR "VideoCommentModel"."accountId" = :heldForReviewAccountIdException)`)
- } else {
- where.push(base)
- }
- }
+ where = where.concat(this.getHeldForReviewWhere('VideoCommentModel'))
if (this.options.autoTagOneOf) {
const tags = this.options.autoTagOneOf.map(t => t.toLowerCase())
@@ -460,12 +496,16 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
// Help the planner by providing videoId that should filter out many comments
this.replacements.videoId = this.options.videoId
+ const deletedWhere = this.options.totalRepliesIncludeDeleted === true
+ ? ''
+ : 'AND "deletedAt" IS NULL '
+
this.subQueryLateralJoin += `LEFT JOIN LATERAL (` +
`SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
`INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
`LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` +
`WHERE ("replies"."inReplyToCommentId" = "VideoCommentModel"."id" OR "replies"."originCommentId" = "VideoCommentModel"."id") ` +
- `AND "deletedAt" IS NULL ` +
+ `${deletedWhere}` +
`AND ${blockWhereString} ` +
`) "totalReplies" ON TRUE `
}
@@ -485,6 +525,94 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
// ---------------------------------------------------------------------------
+ private getHeldForReviewWhere (commentTableName: string) {
+ if (this.options.heldForReview === true) {
+ return [ `"${commentTableName}"."heldForReview" IS TRUE` ]
+ }
+
+ if (this.options.heldForReview === false) {
+ const base = `"${commentTableName}"."heldForReview" IS FALSE`
+
+ if (this.options.heldForReviewAccountIdException) {
+ this.replacements.heldForReviewAccountIdException = this.options.heldForReviewAccountIdException
+
+ return [ `(${base} OR "${commentTableName}"."accountId" = :heldForReviewAccountIdException)` ]
+ }
+
+ return [ base ]
+ }
+
+ return []
+ }
+
+ // Walk down the replies of a comment, keeping only `repliesPerLevel` of each parent
+ // The LATERAL is applied on every iteration, so cutting a comment also cuts its own descendants
+ private buildReplyTreeCTE () {
+ const { parentCommentId, start, count, maxDepth, repliesPerLevel, maxComments } = this.options.replyTree
+
+ Object.assign(this.replacements, {
+ treeParentCommentId: parentCommentId,
+ treeStart: start,
+ treeCount: count,
+ treeMaxDepth: maxDepth,
+ treeRepliesPerLevel: repliesPerLevel,
+ treeMaxComments: maxComments
+ })
+
+ const buildWhere = (on: string) => {
+ const where = [ `"replyTree"."inReplyToCommentId" = ${on}`, ...this.getHeldForReviewWhere('replyTree') ]
+
+ if (this.options.videoId) where.push('"replyTree"."videoId" = :videoId')
+
+ return where.join(' AND ')
+ }
+
+ // The first level is the one the client paginates with start/count, so it must match the
+ // sort order the outer query uses, and blocklists must be applied before LIMIT/OFFSET
+ // or pagination would duplicate/skip replies
+ const direction = this.options.sort
+ ? buildSortDirectionAndField(this.options.sort).direction
+ : 'ASC'
+
+ const order = `ORDER BY "replyTree"."createdAt" ${direction}, "replyTree"."id" ASC`
+
+ const firstLevelJoin = this.options.blockerAccountIds
+ ? 'INNER JOIN "video" AS "replyTreeVideo" ON "replyTreeVideo"."id" = "replyTree"."videoId" ' +
+ 'INNER JOIN "videoChannel" AS "replyTreeChannel" ON "replyTreeChannel"."id" = "replyTreeVideo"."channelId" '
+ : ''
+
+ const firstLevelWhere = this.options.blockerAccountIds
+ ? [ buildWhere(':treeParentCommentId'), ...this.getBlockWhere('replyTree', 'replyTreeChannel') ].join(' AND ')
+ : buildWhere(':treeParentCommentId')
+
+ this.subQueryCTE.push(
+ '"replyTreeAll" AS (' +
+ // The first level is the one the client paginates through
+ `SELECT * FROM (` +
+ ` SELECT "replyTree"."id", 0 AS "treeDepth" FROM "videoComment" AS "replyTree" ` +
+ ` ${firstLevelJoin}` +
+ ` WHERE ${firstLevelWhere} ${order} LIMIT :treeCount OFFSET :treeStart` +
+ `) AS "replyTreeFirstLevel" ` +
+ `UNION ALL ` +
+ // Recursive query
+ // Blocklists are not taken into account here to keep the query cheap
+ // We accept a blocked comment may consume a slot and slightly reduce the number of replies of a parent
+ `SELECT "replyTreeChild"."id", "replyTreeAll"."treeDepth" + 1 ` +
+ `FROM "replyTreeAll", LATERAL (` +
+ ` SELECT "replyTree"."id" FROM "videoComment" AS "replyTree" ` +
+ ` WHERE ${buildWhere('"replyTreeAll"."id"')} ${order} LIMIT :treeRepliesPerLevel` +
+ `) AS "replyTreeChild" ` +
+ `WHERE "replyTreeAll"."treeDepth" + 1 < :treeMaxDepth` +
+ ')',
+ // Never send back more comments than this
+ // Truncating the deepest levels first keeps the tree readable
+ // The client can still unfold what we dropped with other HTTP requests
+ '"replyTree" AS (SELECT "id" FROM "replyTreeAll" LIMIT :treeMaxComments)'
+ )
+
+ this.subQueryJoin += ' INNER JOIN "replyTree" ON "replyTree"."id" = "VideoCommentModel"."id" '
+ }
+
private getBlockWhere (commentTableName: string, channelTableName: string) {
const where: string[] = []
diff --git a/server/core/models/video/video-comment.ts b/server/core/models/video/video-comment.ts
index b998e33734..a7912d82e5 100644
--- a/server/core/models/video/video-comment.ts
+++ b/server/core/models/video/video-comment.ts
@@ -32,7 +32,7 @@ import {
UpdatedAt
} from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
-import { CONSTRAINTS_FIELDS, USER_EXPORT_MAX_ITEMS } from '../../initializers/constants.js'
+import { CONSTRAINTS_FIELDS, USER_EXPORT_MAX_ITEMS, VIDEO_COMMENTS_TREE } from '../../initializers/constants.js'
import {
MComment,
MCommentAdminOrUserFormattable,
@@ -137,7 +137,8 @@ export enum ScopeNames {
]
},
{
- fields: [ 'inReplyToCommentId' ]
+ // createdAt/id are the sort of the comment tree queries, so they can be served by an index only scan
+ fields: [ 'inReplyToCommentId', 'createdAt', 'id' ]
}
]
})
@@ -447,37 +448,115 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
})
}
+ // Return the thread root comment and a truncated view of its replies
+ // Replies the client does not get must be fetched with `listRepliesForApi`
static async listThreadCommentsForApi (parameters: {
video: MVideo
threadId: number
+ maxDepth: number
+ repliesPerLevel: number
user?: MUserAccountId
}) {
- const { user, video, threadId } = parameters
+ const { user, video, threadId, maxDepth, repliesPerLevel } = parameters
- const { blockerAccountIds, canSeeHeldForReview } = await VideoCommentModel.buildBlockerAccountIdsAndCanSeeHeldForReview({ user, video })
+ const commonOptions = await VideoCommentModel.buildCommentTreeCommonOptions({ user, video })
- const queryOptions: ListVideoCommentsOptions = {
- threadId,
+ const [ comment, { total, data } ] = await Promise.all([
+ new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, {
+ ...commonOptions,
+
+ commentIds: [ threadId ],
+ sort: 'createdAt'
+ }).get<MCommentAdminOrUserFormattable>(),
+
+ VideoCommentModel.listRepliesForApi({
+ video,
+ user,
+ maxDepth,
+ repliesPerLevel,
+
+ parentCommentId: threadId,
+ start: 0,
+ count: repliesPerLevel,
+ sort: 'createdAt',
+
+ commonOptions
+ })
+ ])
+
+ if (!comment) return { comment: null, total: 0, data: [] }
+
+ return { comment, total, data }
+ }
+
+ // Return the direct replies of `parentCommentId` (paginated) and, for each of them, a truncated view of their own replies
+ static async listRepliesForApi (parameters: {
+ video: MVideo
+ parentCommentId: number
+ start: number
+ count: number
+ sort: string
+ maxDepth: number
+ repliesPerLevel: number
+ user?: MUserAccountId
+
+ // Avoid rebuilding it (extra blocklist query + channel load) when the caller already has one
+ commonOptions?: ListVideoCommentsOptions
+ }) {
+ const { user, video, parentCommentId, start, count, sort, maxDepth, repliesPerLevel } = parameters
+
+ const commonOptions = parameters.commonOptions ?? await VideoCommentModel.buildCommentTreeCommonOptions({ user, video })
+
+ const listOptions: ListVideoCommentsOptions = {
+ ...commonOptions,
+
+ sort,
+ replyTree: {
+ parentCommentId,
+ start,
+ count,
+ maxDepth,
+ repliesPerLevel,
+ maxComments: VIDEO_COMMENTS_TREE.MAX_COMMENTS_PER_REQUEST
+ }
+ }
+ const countOptions: ListVideoCommentsOptions = {
+ ...commonOptions,
+
+ inReplyToCommentIds: [ parentCommentId ],
+ includeReplyCounters: false
+ }
+
+ return Promise.all([
+ new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, listOptions).list<MCommentAdminOrUserFormattable>(),
+ new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, countOptions).count()
+ ]).then(([ data, total ]) => {
+ return { total, data }
+ })
+ }
+
+ private static async buildCommentTreeCommonOptions (options: {
+ video: MVideo
+ user?: MUserAccountId
+ }): Promise<ListVideoCommentsOptions> {
+ const { video, user } = options
+
+ const { blockerAccountIds, canSeeHeldForReview } = await VideoCommentModel.buildBlockerAccountIdsAndCanSeeHeldForReview({ user, video })
+
+ return {
videoId: video.id,
selectType: 'api-video',
- sort: 'createdAt',
blockerAccountIds,
includeReplyCounters: true,
+ totalRepliesIncludeDeleted: true,
heldForReview: canSeeHeldForReview
? undefined // Display all comments for video owner or moderator
: false,
heldForReviewAccountIdException: user?.Account?.id
}
-
- return Promise.all([
- new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).list<MCommentAdminOrUserFormattable>(),
- new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).count()
- ]).then(([ rows, count ]) => {
- return { total: count, data: rows }
- })
}
static listThreadParentComments (options: {
diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yaml
index 16b775b439..bef85cd947 100644
--- a/support/doc/api/openapi.yaml
+++ b/support/doc/api/openapi.yaml
@@ -6315,11 +6315,17 @@ paths:
'/api/v1/videos/{id}/comment-threads/{threadId}':
get:
summary: Get a thread
+ description: >
+ Since PeerTube 8.3 the returned tree is truncated: only the first replies of each comment are included, and only
+ down to `maxDepth` nesting levels. Use `GET /api/v1/videos/{id}/comments/{commentId}/replies` to fetch the
+ replies that are missing, comparing `children` and `totalChildren` of each node of the tree.
tags:
- Video Comments
parameters:
- $ref: '#/components/parameters/idOrUUID'
- $ref: '#/components/parameters/threadId'
+ - $ref: '#/components/parameters/commentMaxDepth'
+ - $ref: '#/components/parameters/commentRepliesPerLevel'
- $ref: '#/components/parameters/videoPasswordHeader'
responses:
'200':
@@ -6329,6 +6335,40 @@ paths:
schema:
$ref: '#/components/schemas/VideoCommentThreadTree'
+ '/api/v1/videos/{id}/comments/{commentId}/replies':
+ get:
+ summary: List replies of a comment
+ description: >
+ **PeerTube >= 8.3** List the direct replies of a comment, each of them nested up to `maxDepth` levels. Used to unfold the parts of a
+ thread that `GET /api/v1/videos/{id}/comment-threads/{threadId}` truncated.
+ tags:
+ - Video Comments
+ parameters:
+ - $ref: '#/components/parameters/idOrUUID'
+ - $ref: '#/components/parameters/commentId'
+ - $ref: '#/components/parameters/start'
+ - $ref: '#/components/parameters/count'
+ - $ref: '#/components/parameters/commentRepliesSort'
+ - $ref: '#/components/parameters/commentMaxDepth'
+ - $ref: '#/components/parameters/commentRepliesPerLevel'
+ - $ref: '#/components/parameters/videoPasswordHeader'
+ responses:
+ '200':
+ description: successful operation
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ total:
+ type: integer
+ example: 13
+ description: Total number of direct replies of the comment
+ data:
+ type: array
+ items:
+ $ref: '#/components/schemas/VideoCommentThreadTree'
+
'/api/v1/videos/comments':
get:
summary: List instance comments
@@ -9437,6 +9477,50 @@ components:
description: The comment id
schema:
$ref: '#/components/schemas/VideoComment/properties/id'
+ commentMaxDepth:
+ name: maxDepth
+ in: query
+ required: false
+ description: >
+ Number of nesting levels to include below the requested comment.
+
+ Bounded by `repliesPerLevel` and `count`, see the note on `repliesPerLevel`
+ schema:
+ type: integer
+ minimum: 1
+ maximum: 10
+ default: 5
+ commentRepliesPerLevel:
+ name: repliesPerLevel
+ in: query
+ required: false
+ description: >
+ Number of direct replies to include for each comment of the tree. Does not apply to the first level of
+ `GET /api/v1/videos/{id}/comments/{commentId}/replies`, which is paginated using `start` and `count`.
+
+ A tree can make the server walk through
+ `count * (repliesPerLevel ^ maxDepth - 1) / (repliesPerLevel - 1)` comments, which grows exponentially.
+ Combinations above 2000000 are rejected
+ A deep tree has to be narrow and a wide tree has to be shallow.
+ schema:
+ type: integer
+ minimum: 1
+ maximum: 30
+ default: 10
+ commentRepliesSort:
+ name: sort
+ in: query
+ required: false
+ description: >
+ Sort the direct replies of the comment. Deeper levels of the tree are always sorted the same way.
+
+ Defaults to `createdAt` so the replies are appended to the thread tree in the order it was built
+ schema:
+ type: string
+ default: createdAt
+ enum:
+ - createdAt
+ - -createdAt
isLive:
name: isLive
in: query
@@ -11236,8 +11320,16 @@ components:
$ref: '#/components/schemas/VideoComment'
children:
type: array
+ description: >
+ Direct replies of `comment`. The server truncates the tree in depth (see the `maxDepth` parameter) and in
+ width, so this array may contain less elements than `totalChildren`. Missing replies can be fetched using
+ `GET /api/v1/videos/{id}/comments/{commentId}/replies`
items:
$ref: '#/components/schemas/VideoCommentThreadTree'
+ totalChildren:
+ type: integer
+ minimum: 0
+ description: Total number of direct replies of `comment` the current user can see
VideoCommentForOwnerOrAdmin:
properties:
id:
diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md
index e5b932ef69..2141cead39 100644
--- a/support/doc/plugins/guide.md
+++ b/support/doc/plugins/guide.md
@@ -1296,7 +1296,8 @@ If you want to create an antispam/moderation plugin, you could use the following
* `filter:api.video-thread.create.accept.result`: to accept or not local thread
* `filter:api.video-comment-reply.create.accept.result`: to accept or not local replies
* `filter:api.video-threads.list.result`: to change/hide the text of threads
- * `filter:api.video-thread-comments.list.result`: to change/hide the text of replies
+ * `filter:api.video-thread-comments.list.result`: to change/hide the text of replies. Since PeerTube 8.3 this hook only receives a truncated view of the thread, and its `total` is the number of direct replies of the root comment
+ * `filter:api.video-comment-replies.list.result`: to change/hide the text of the replies loaded after the thread (PeerTube >= 8.3)
* `filter:video.auto-blacklist.result`: to automatically blacklist local or remote videos
* `filter:admin-users-list.bulk-actions.create.result`: to add bulk actions in the admin users list
* `filter:admin-video-comments-list.actions.create.result`: to add actions in the admin video comments list