Skip to content

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

Optimize comment SQL query

Chocobozzz0edf927e033a
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 41b2344e84..cf5f5ee4d7 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
@@ -6,7 +6,7 @@ import { createSafeIn } from '../../../shared/index.js'
import { VideoCommentTableAttributes } from './video-comment-table-attributes.js'
export interface ListVideoCommentsOptions extends AbstractListQueryOptions {
- selectType: 'api' | 'feed' | 'comment-only'
+ selectType: 'api-list' | 'api-video' | 'feed' | 'comment-only'
autoTagOfAccountId?: number
@@ -46,7 +46,8 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
private builtVideoJoin = false
private builtVideoChannelJoin = false
private builtVideoChannelCollaboratorsJoin = false
- private builtAvatarJoin = false
+ private builtAccountAvatarJoin = false
+ private builtChannelAvatarJoin = false
private builtAutomaticTagsJoin = false
constructor (
@@ -275,50 +276,73 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
// ---------------------------------------------------------------------------
- private buildAvatarsJoin () {
- if (this.builtAvatarJoin) return
+ private buildAccountAvatarsJoin () {
+ if (this.builtAccountAvatarJoin) return
this.join += getAvatarsJoin({
base: 'Account->Actor->',
on: '"VideoCommentModel"."Account.Actor.id"'
})
+ this.builtAccountAvatarJoin = true
+ }
+
+ private buildChannelAvatarsJoin () {
+ if (this.builtChannelAvatarJoin) return
+
this.join += getAvatarsJoin({
base: 'Video->VideoChannel->Actor->',
on: '"VideoCommentModel"."Video.VideoChannel.Actor.id"'
})
- this.builtAvatarJoin = true
+ this.builtChannelAvatarJoin = true
}
// ---------------------------------------------------------------------------
protected buildQueryJoin () {
- if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
- this.buildAvatarsJoin()
+ const selectType = this.options.selectType
+
+ if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
+ this.buildAccountAvatarsJoin()
+ }
+
+ if (selectType === 'api-list') {
+ this.buildChannelAvatarsJoin()
}
}
protected buildQueryAttributes () {
- if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
+ const selectType = this.options.selectType
+
+ if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
this.attributes.push(this.tableAttributes.getAccountAvatarAttributes())
+ }
+
+ if (selectType === 'api-list') {
this.attributes.push(this.tableAttributes.getChannelAvatarAttributes())
}
}
protected buildSubQueryJoin () {
- if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
+ const selectType = this.options.selectType
+
+ if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
this.buildAccountJoin()
+ }
+
+ if (selectType === 'api-list') {
this.buildVideoJoin()
+ this.buildVideoChannelJoin()
}
- if (this.options.autoTagOfAccountId && this.options.selectType === 'api') {
+ if (this.options.autoTagOfAccountId && selectType === 'api-list') {
this.buildAutomaticTagsJoin()
}
}
protected buildSubQueryAttributes () {
- this.buildVideoChannelJoin()
+ const selectType = this.options.selectType
this.subQueryAttributes = [
...this.subQueryAttributes,
@@ -326,23 +350,29 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
this.tableAttributes.getVideoCommentAttributes()
]
- if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
+ if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
this.subQueryAttributes = [
...this.subQueryAttributes,
this.tableAttributes.getVideoAttributes(),
- this.tableAttributes.getChannelAttributes(),
- this.tableAttributes.getChannelActorAttributes(),
- this.tableAttributes.getChannelServerAttributes(),
-
this.tableAttributes.getAccountAttributes(),
this.tableAttributes.getAccountActorAttributes(),
this.tableAttributes.getAccountServerAttributes()
]
}
- if (this.options.autoTagOfAccountId && this.options.selectType === 'api') {
+ if (selectType === 'api-list') {
+ this.subQueryAttributes = [
+ ...this.subQueryAttributes,
+
+ this.tableAttributes.getChannelAttributes(),
+ this.tableAttributes.getChannelActorAttributes(),
+ this.tableAttributes.getChannelServerAttributes()
+ ]
+ }
+
+ if (this.options.autoTagOfAccountId && this.options.selectType === 'api-list') {
this.subQueryAttributes = [
...this.subQueryAttributes,
diff --git a/server/core/models/video/video-comment.ts b/server/core/models/video/video-comment.ts
index c8dd73d1b9..b1c47af5dc 100644
--- a/server/core/models/video/video-comment.ts
+++ b/server/core/models/video/video-comment.ts
@@ -385,7 +385,7 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
'heldForReview'
]),
- selectType: 'api',
+ selectType: 'api-list',
notDeleted: true
}
@@ -409,7 +409,7 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
const { blockerAccountIds, canSeeHeldForReview } = await VideoCommentModel.buildBlockerAccountIdsAndCanSeeHeldForReview({ user, video })
const commonOptions: ListVideoCommentsOptions = {
- selectType: 'api',
+ selectType: 'api-video',
videoId: video.id,
blockerAccountIds,
@@ -461,7 +461,7 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
threadId,
videoId: video.id,
- selectType: 'api',
+ selectType: 'api-video',
sort: 'createdAt',
blockerAccountIds,