ActivityPub-federated video streaming platform using P2P directly in your web browser
Reduce number of rows returned by user SQL query
Chocobozzz639f014f0921
diff --git a/server/core/models/shared/sql/actor-helpers.ts b/server/core/models/shared/sql/actor-helpers.tsindex 53e825cf1d..431b824161 100644--- a/server/core/models/shared/sql/actor-helpers.ts+++ b/server/core/models/shared/sql/actor-helpers.ts@@ -121,3 +121,25 @@ export function getAvatarsJSONJoin (options: {export function getAvatarsJSONAttributes (base: string) {return `"${base}AvatarsJSON"."Avatars" AS "${base.replace(/->/g, '.')}Avatars"`}++/**+ * Build JSON directly in PostgreSQL to reduce amount of rows transferred and processed in JS+ */+export function getBannersJSONJoin (options: {+ attributes: string+ base: string+ on: string+}) {+ const { attributes, base, on } = options++ return `LEFT JOIN LATERAL (` ++ `SELECT json_agg(` ++ ` jsonb_build_object(${attributes})` ++ `) AS "Banners"` ++ ` FROM "actorImage" WHERE "actorId" = ${on} AND "type" = ${ActorImageType.BANNER}` ++ `) AS "${base}BannersJSON" ON TRUE `+}++export function getBannersJSONAttributes (base: string) {+ return `"${base}BannersJSON"."Banners" AS "${base.replace(/->/g, '.')}Banners"`+}diff --git a/server/core/models/user/sql/user/user-list-query-builder.ts b/server/core/models/user/sql/user/user-list-query-builder.tsindex 4788bd3eb2..15cfa63a64 100644--- a/server/core/models/user/sql/user/user-list-query-builder.ts+++ b/server/core/models/user/sql/user/user-list-query-builder.ts@@ -1,5 +1,6 @@-import { ActorImageType, VideoChannelCollaboratorState, VideoPlaylistType } from '@peertube/peertube-models'+import { VideoChannelCollaboratorState, VideoPlaylistType } from '@peertube/peertube-models'import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'+import { getAvatarsJSONJoin, getBannersJSONJoin } from '@server/models/shared/sql/actor-helpers.js'import { Sequelize } from 'sequelize'import { UserTableAttributes } from './user-table-attributes.js'@@ -43,8 +44,11 @@ export class UserListQueryBuilder extends AbstractListQuery {this.join += ' INNER JOIN "account" "Account" ON "Account"."userId" = "UserModel"."id" ' +'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" ' +- 'LEFT JOIN "actorImage" "Account->Actor->Avatars" ON "Account->Actor->Avatars"."actorId" = "Account->Actor"."id" ' +- ` AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `+ getAvatarsJSONJoin({+ attributes: this.tableAttributes.getAvatarAttributesJSON(),+ base: 'Account->Actor->',+ on: '"Account->Actor"."id"'+ })this.builtAccountJoin = true}@@ -57,12 +61,16 @@ export class UserListQueryBuilder extends AbstractListQuery {' ON "Account->VideoChannels->Actor"."videoChannelId" = "Account->VideoChannels"."id" ' +'LEFT JOIN "server" "Account->VideoChannels->Actor->Server" ' +' ON "Account->VideoChannels->Actor->Server"."id" = "Account->VideoChannels->Actor"."serverId" ' +- 'LEFT JOIN "actorImage" "Account->VideoChannels->Actor->Avatars" ' +- ' ON "Account->VideoChannels->Actor->Avatars"."actorId" = "Account->VideoChannels->Actor"."id" ' +- ` AND "Account->VideoChannels->Actor->Avatars"."type" = ${ActorImageType.AVATAR} ` +- 'LEFT JOIN "actorImage" "Account->VideoChannels->Actor->Banners" ' +- ' ON "Account->VideoChannels->Actor->Banners"."actorId" = "Account->VideoChannels->Actor"."id" ' +- ` AND "Account->VideoChannels->Actor->Banners"."type" = ${ActorImageType.BANNER} `+ getAvatarsJSONJoin({+ attributes: this.tableAttributes.getAvatarAttributesJSON(),+ base: 'Account->VideoChannels->Actor->',+ on: '"Account->VideoChannels->Actor"."id"'+ }) ++ getBannersJSONJoin({+ attributes: this.tableAttributes.getBannerAttributesJSON(),+ base: 'Account->VideoChannels->Actor->',+ on: '"Account->VideoChannels->Actor"."id"'+ })this.builtChannelsJoin = true}@@ -77,13 +85,17 @@ export class UserListQueryBuilder extends AbstractListQuery {' INNER JOIN "actor" AS "Account->Collabs->Channel->Actor" ' +' ON "Account->Collabs->Channel"."id" = "Account->Collabs->Channel->Actor"."videoChannelId" ' +' LEFT JOIN "server" AS "Account->Collabs->Channel->Actor->Server" ' +- ' ON "Account->Collabs->Channel->Actor"."serverId" = "Account->Collabs->Channel->Actor->Server"."id"' +- ' LEFT JOIN "actorImage" AS "Account->Collabs->Channel->Actor->Avatars" ' +- ' ON "Account->Collabs->Channel->Actor"."id" = "Account->Collabs->Channel->Actor->Avatars"."actorId"' +- ` AND "Account->Collabs->Channel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}` +- ' LEFT JOIN "actorImage" AS "Account->Collabs->Channel->Actor->Banners" ' +- ' ON "Account->Collabs->Channel->Actor"."id" = "Account->Collabs->Channel->Actor->Banners"."actorId" ' +- ` AND "Account->Collabs->Channel->Actor->Banners"."type" = ${ActorImageType.BANNER} ` ++ ' ON "Account->Collabs->Channel->Actor"."serverId" = "Account->Collabs->Channel->Actor->Server"."id" ' ++ getAvatarsJSONJoin({+ attributes: this.tableAttributes.getAvatarAttributesJSON(),+ base: 'Account->Collabs->Channel->Actor->',+ on: '"Account->Collabs->Channel->Actor"."id"'+ }) ++ getBannersJSONJoin({+ attributes: this.tableAttributes.getBannerAttributesJSON(),+ base: 'Account->Collabs->Channel->Actor->',+ on: '"Account->Collabs->Channel->Actor"."id"'+ }) +` INNER JOIN "account" "Account->Collabs->Channel->Account" ` +` ON "Account->Collabs->Channel"."accountId" = "Account->Collabs->Channel->Account"."id" ` +` INNER JOIN "actor" "Account->Collabs->Channel->Account->Actor" ` +diff --git a/server/core/models/user/sql/user/user-table-attributes.ts b/server/core/models/user/sql/user/user-table-attributes.tsindex ab65964b8d..4721b1a9c6 100644--- a/server/core/models/user/sql/user/user-table-attributes.ts+++ b/server/core/models/user/sql/user/user-table-attributes.ts@@ -3,6 +3,7 @@ import { AccountModel } from '@server/models/account/account.js'import { ActorImageModel } from '@server/models/actor/actor-image.js'import { ActorModel } from '@server/models/actor/actor.js'import { ServerModel } from '@server/models/server/server.js'+import { getAvatarsJSONAttributes, getBannersJSONAttributes } from '@server/models/shared/sql/actor-helpers.js'import { buildSQLAttributes } from '@server/models/shared/table.js'import { VideoChannelCollaboratorModel } from '@server/models/video/video-channel-collaborator.js'import { VideoChannelModel } from '@server/models/video/video-channel.js'@@ -32,9 +33,14 @@ export class UserTableAttributes {return ServerModel.getSQLAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')}+ @Memoize()+ getAvatarAttributesJSON () {+ return ActorImageModel.getSQLAttributesJSON().join(', ')+ }+@Memoize()getAccountAvatarAttributes () {- return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')+ return getAvatarsJSONAttributes('Account->Actor->')}// ---------------------------------------------------------------------------@@ -56,12 +62,17 @@ export class UserTableAttributes {@Memoize()getChannelAvatarAttributes () {- return ActorImageModel.getSQLAttributes('Account->VideoChannels->Actor->Avatars', 'Account.VideoChannels.Actor.Avatars.').join(', ')+ return getAvatarsJSONAttributes('Account->VideoChannels->Actor->')+ }++ @Memoize()+ getBannerAttributesJSON () {+ return ActorImageModel.getSQLAttributesJSON().join(', ')}@Memoize()getChannelBannerAttributes () {- return ActorImageModel.getSQLAttributes('Account->VideoChannels->Actor->Banners', 'Account.VideoChannels.Actor.Banners.').join(', ')+ return getBannersJSONAttributes('Account->VideoChannels->Actor->')}// ---------------------------------------------------------------------------@@ -105,19 +116,14 @@ export class UserTableAttributes {@Memoize()getCollabChannelActorAvatarAttributes () {- return ActorImageModel.getSQLAttributes(- 'Account->Collabs->Channel->Actor->Avatars',- 'Account.Collabs.Channel.Actor.Avatars.'- ).join(', ')+ return getAvatarsJSONAttributes('Account->Collabs->Channel->Actor->')}@Memoize()getCollabChannelActorBannerAttributes () {- return ActorImageModel.getSQLAttributes(- 'Account->Collabs->Channel->Actor->Banners',- 'Account.Collabs.Channel.Actor.Banners.'- ).join(', ')+ return getBannersJSONAttributes('Account->Collabs->Channel->Actor->')}+@Memoize()getCollabChannelAccountAttributes () {return buildSQLAttributes({