Skip to content

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

Faster comments listing for users

Chocobozzz641c5fed192c
diff --git a/server/core/models/shared/abstract-list-query.ts b/server/core/models/shared/abstract-list-query.ts
index 2de91065b4..e4138c29ac 100644
--- a/server/core/models/shared/abstract-list-query.ts
+++ b/server/core/models/shared/abstract-list-query.ts
@@ -19,6 +19,7 @@ export abstract class AbstractListQuery extends AbstractRunQuery {
private subQuery: string
+ protected subQueryCTE: string[] = []
protected subQueryAttributes: string[] = []
protected subQueryLateralJoin = ''
protected subQueryJoin = ''
@@ -96,7 +97,8 @@ export abstract class AbstractListQuery extends AbstractRunQuery {
this.buildSubQueryLateralJoin()
this.buildSubQueryAttributes()
- this.subQuery = `${this.buildSelect(this.subQueryAttributes)} ` +
+ this.subQuery = this.buildCTE(this.subQueryCTE) +
+ this.buildSelect(this.subQueryAttributes) +
`FROM "${this.modelInfo.tableName}" AS "${this.modelInfo.modelName}" ` +
`${this.subQueryJoin} ` +
`${this.subQueryLateralJoin} ` +
@@ -116,7 +118,8 @@ export abstract class AbstractListQuery extends AbstractRunQuery {
private buildCountQuery () {
this.buildSubQueryWhere()
- this.query = `SELECT COUNT(*) AS "total" ` +
+ this.query = this.buildCTE(this.subQueryCTE) +
+ `SELECT COUNT(*) AS "total" ` +
`FROM "${this.modelInfo.tableName}" AS "${this.modelInfo.modelName}" ` +
`${this.subQueryJoin} ` +
`${this.subQueryWhere}`
diff --git a/server/core/models/shared/abstract-run-query.ts b/server/core/models/shared/abstract-run-query.ts
index 68f5ee8ee7..12187f851c 100644
--- a/server/core/models/shared/abstract-run-query.ts
+++ b/server/core/models/shared/abstract-run-query.ts
@@ -31,6 +31,12 @@ export class AbstractRunQuery {
return this.sequelize.query<any>(this.query, queryOptions)
}
+ protected buildCTE (cte: string[]) {
+ if (!cte.length) return ''
+
+ return `WITH ${cte.join(', ')} `
+ }
+
protected buildSelect (attributes: string[]) {
return `SELECT ${attributes.join(', ')} `
}
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 1d43dc40b4..6f249a8654 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
@@ -47,7 +47,6 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
private builtVideoJoin = false
private builtVideoChannelJoin = false
private builtVideoChannelActorJoin = false
- private builtVideoChannelCollaboratorsJoin = false
private builtAccountAvatarJoin = false
private builtChannelAvatarJoin = false
private builtAutomaticTagsJoin = false
@@ -149,21 +148,40 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
}
if (this.options.videoAccountOwnerId) {
- this.buildVideoChannelJoin()
-
this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
if (this.options.videoAccountOwnerIncludeCollaborations !== true) {
+ this.buildVideoChannelJoin()
+
where.push(`"Video->VideoChannel"."accountId" = :videoAccountOwnerId`)
} else {
- this.buildVideoChannelCollaboratorsJoin()
-
- where.push(
- `(` +
- `"Video->VideoChannel"."accountId" = :videoAccountOwnerId OR ` +
- `"Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId` +
- `)`
+ const base = 'SELECT "VideoCommentModel"."id" FROM "videoComment" AS "VideoCommentModel" ' +
+ this.getVideoJoin() +
+ getChannelJoin({
+ base: 'Video->',
+ on: '"Video"."channelId"',
+ includeAccount: false,
+ includeAvatars: false,
+ includeActors: false,
+ required: true
+ })
+
+ this.subQueryCTE.push(
+ '"candidates" AS (' +
+ `${base} WHERE "Video->VideoChannel"."accountId" = :videoAccountOwnerId ` +
+ `UNION ` +
+ `${base} ` +
+ 'INNER JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
+ 'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
+ 'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
+ 'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId ' +
+ ')'
)
+
+ this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
+ this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
+
+ this.subQueryJoin += ' INNER JOIN "candidates" ON "candidates"."id" = "VideoCommentModel"."id" '
}
}
@@ -251,11 +269,15 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
private buildVideoJoin () {
if (this.builtVideoJoin) return
- this.subQueryJoin += ' INNER JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
+ this.subQueryJoin += this.getVideoJoin()
this.builtVideoJoin = true
}
+ private getVideoJoin () {
+ return ' INNER JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
+ }
+
private buildVideoChannelJoin () {
if (this.builtVideoChannelJoin) return
@@ -287,23 +309,6 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
this.builtVideoChannelActorJoin = true
}
- private buildVideoChannelCollaboratorsJoin () {
- if (this.builtVideoChannelCollaboratorsJoin) return
-
- this.buildVideoChannelJoin()
-
- this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
- 'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
- 'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
- // Ensure we join with max 1 collaborator to not duplicate rows
- 'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId '
-
- this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
- this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
-
- this.builtVideoChannelCollaboratorsJoin = true
- }
-
private buildAutomaticTagsJoin () {
if (this.builtAutomaticTagsJoin) return
diff --git a/server/core/models/video/sql/video/videos-id-list-query-builder.ts b/server/core/models/video/sql/video/videos-id-list-query-builder.ts
index 04e08fa696..a7416b1f42 100644
--- a/server/core/models/video/sql/video/videos-id-list-query-builder.ts
+++ b/server/core/models/video/sql/video/videos-id-list-query-builder.ts
@@ -19,6 +19,7 @@ import { createSafeIn, parseRowCountResult } from '../../../shared/index.js'
/**
* Build videos list SQL query to fetch rows
+ * We don't list classic SQL builder classes used by other models because for performance reasons
*/
export type DisplayOnlyForFollowerOptions = {