aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/sql')
-rw-r--r--server/models/video/sql/comment/video-comment-list-query-builder.ts45
1 files changed, 30 insertions, 15 deletions
diff --git a/server/models/video/sql/comment/video-comment-list-query-builder.ts b/server/models/video/sql/comment/video-comment-list-query-builder.ts
index 3960f6b13..6d752d4a4 100644
--- a/server/models/video/sql/comment/video-comment-list-query-builder.ts
+++ b/server/models/video/sql/comment/video-comment-list-query-builder.ts
@@ -44,6 +44,7 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
44 44
45 private innerSelect = '' 45 private innerSelect = ''
46 private innerJoins = '' 46 private innerJoins = ''
47 private innerLateralJoins = ''
47 private innerWhere = '' 48 private innerWhere = ''
48 49
49 private readonly built = { 50 private readonly built = {
@@ -59,6 +60,10 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
59 private readonly options: ListVideoCommentsOptions 60 private readonly options: ListVideoCommentsOptions
60 ) { 61 ) {
61 super(sequelize) 62 super(sequelize)
63
64 if (this.options.includeReplyCounters && !this.options.videoId) {
65 throw new Error('Cannot include reply counters without videoId')
66 }
62 } 67 }
63 68
64 async listComments <T extends Model> () { 69 async listComments <T extends Model> () {
@@ -97,6 +102,7 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
97 this.innerQuery = `${this.innerSelect} ` + 102 this.innerQuery = `${this.innerSelect} ` +
98 `FROM "videoComment" AS "VideoCommentModel" ` + 103 `FROM "videoComment" AS "VideoCommentModel" ` +
99 `${this.innerJoins} ` + 104 `${this.innerJoins} ` +
105 `${this.innerLateralJoins} ` +
100 `${this.innerWhere} ` + 106 `${this.innerWhere} ` +
101 `${this.getOrder()} ` + 107 `${this.getOrder()} ` +
102 `${this.getInnerLimit()}` 108 `${this.getInnerLimit()}`
@@ -284,11 +290,6 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
284 toSelect.push(this.tableAttributes.getAvatarAttributes()) 290 toSelect.push(this.tableAttributes.getAvatarAttributes())
285 } 291 }
286 292
287 if (this.options.includeReplyCounters === true) {
288 toSelect.push(this.getTotalRepliesSelect())
289 toSelect.push(this.getAuthorTotalRepliesSelect())
290 }
291
292 this.select = this.buildSelect(toSelect) 293 this.select = this.buildSelect(toSelect)
293 } 294 }
294 295
@@ -307,6 +308,14 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
307 ]) 308 ])
308 } 309 }
309 310
311 if (this.options.includeReplyCounters === true) {
312 this.buildTotalRepliesSelect()
313 this.buildAuthorTotalRepliesSelect()
314
315 toSelect.push('"totalRepliesFromVideoAuthor"."count" AS "totalRepliesFromVideoAuthor"')
316 toSelect.push('"totalReplies"."count" AS "totalReplies"')
317 }
318
310 this.innerSelect = this.buildSelect(toSelect) 319 this.innerSelect = this.buildSelect(toSelect)
311 } 320 }
312 321
@@ -344,26 +353,32 @@ export class VideoCommentListQueryBuilder extends AbstractRunQuery {
344 353
345 // --------------------------------------------------------------------------- 354 // ---------------------------------------------------------------------------
346 355
347 private getTotalRepliesSelect () { 356 private buildTotalRepliesSelect () {
348 const blockWhereString = this.getBlockWhere('replies', 'videoChannel').join(' AND ') 357 const blockWhereString = this.getBlockWhere('replies', 'videoChannel').join(' AND ')
349 358
350 return `(` + 359 // Help the planner by providing videoId that should filter out many comments
351 `SELECT COUNT("replies"."id") FROM "videoComment" AS "replies" ` + 360 this.replacements.videoId = this.options.videoId
352 `LEFT JOIN "video" ON "video"."id" = "replies"."videoId" ` + 361
362 this.innerLateralJoins += `LEFT JOIN LATERAL (` +
363 `SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
364 `INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
353 `LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` + 365 `LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` +
354 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ` + 366 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ` +
355 `AND "deletedAt" IS NULL ` + 367 `AND "deletedAt" IS NULL ` +
356 `AND ${blockWhereString} ` + 368 `AND ${blockWhereString} ` +
357 `) AS "totalReplies"` 369 `) "totalReplies" ON TRUE `
358 } 370 }
359 371
360 private getAuthorTotalRepliesSelect () { 372 private buildAuthorTotalRepliesSelect () {
361 return `(` + 373 // Help the planner by providing videoId that should filter out many comments
362 `SELECT COUNT("replies"."id") FROM "videoComment" AS "replies" ` + 374 this.replacements.videoId = this.options.videoId
363 `INNER JOIN "video" ON "video"."id" = "replies"."videoId" ` + 375
376 this.innerLateralJoins += `LEFT JOIN LATERAL (` +
377 `SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
378 `INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
364 `INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` + 379 `INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
365 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" AND "replies"."accountId" = "videoChannel"."accountId"` + 380 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" AND "replies"."accountId" = "videoChannel"."accountId"` +
366 `) AS "totalRepliesFromVideoAuthor"` 381 `) "totalRepliesFromVideoAuthor" ON TRUE `
367 } 382 }
368 383
369 private getOrder () { 384 private getOrder () {