Faster get a user

pull/6648/head
Chocobozzz 2024-09-24 13:27:46 +02:00
parent 1a34815872
commit 499d660723
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 116 additions and 107 deletions

View File

@ -24,11 +24,9 @@ my-select-custom-value {
@include responsive-width($form-base-input-width); @include responsive-width($form-base-input-width);
} }
.danger-zone { .danger-zone button {
button { @include danger-button;
@include danger-button; @include disable-outline;
@include disable-outline;
}
} }
.dashboard { .dashboard {

View File

@ -21,7 +21,7 @@ import {
MUserNotifSettingChannelDefault, MUserNotifSettingChannelDefault,
MUserWithNotificationSetting MUserWithNotificationSetting
} from '@server/types/models/index.js' } from '@server/types/models/index.js'
import { col, FindOptions, fn, literal, Op, QueryTypes, where, WhereOptions } from 'sequelize' import { col, FindOptions, fn, literal, Op, QueryTypes, ScopeOptions, where, WhereOptions } from 'sequelize'
import { import {
AfterDestroy, AfterDestroy,
AfterUpdate, AfterUpdate,
@ -85,6 +85,8 @@ enum ScopeNames {
WITH_STATS = 'WITH_STATS' WITH_STATS = 'WITH_STATS'
} }
type WhereUserIdScopeOptions = { whereUserId?: '$userId' | '"UserModel"."id"' }
@DefaultScope(() => ({ @DefaultScope(() => ({
include: [ include: [
{ {
@ -159,107 +161,113 @@ enum ScopeNames {
} }
] ]
}, },
[ScopeNames.WITH_QUOTA]: { [ScopeNames.WITH_QUOTA]: (options: WhereUserIdScopeOptions = {}) => {
attributes: { return {
include: [ attributes: {
[ include: [
literal( [
'(' + literal(
UserModel.generateUserQuotaBaseSQL({ '(' +
whereUserId: '"UserModel"."id"', UserModel.generateUserQuotaBaseSQL({
daily: false, whereUserId: options.whereUserId ?? '"UserModel"."id"',
onlyMaxResolution: true daily: false,
}) + onlyMaxResolution: true
')' }) +
), ')'
'videoQuotaUsed' ),
], 'videoQuotaUsed'
[ ],
literal( [
'(' + literal(
UserModel.generateUserQuotaBaseSQL({ '(' +
whereUserId: '"UserModel"."id"', UserModel.generateUserQuotaBaseSQL({
daily: true, whereUserId: options.whereUserId ?? '"UserModel"."id"',
onlyMaxResolution: true daily: true,
}) + onlyMaxResolution: true
')' }) +
), ')'
'videoQuotaUsedDaily' ),
'videoQuotaUsedDaily'
]
] ]
] }
} }
}, },
[ScopeNames.WITH_TOTAL_FILE_SIZES]: { [ScopeNames.WITH_TOTAL_FILE_SIZES]: (options: WhereUserIdScopeOptions = {}) => {
attributes: { return {
include: [ attributes: {
[ include: [
literal( [
'(' + literal(
UserModel.generateUserQuotaBaseSQL({ '(' +
whereUserId: '"UserModel"."id"', UserModel.generateUserQuotaBaseSQL({
daily: false, whereUserId: options.whereUserId ?? '"UserModel"."id"',
onlyMaxResolution: false daily: false,
}) + onlyMaxResolution: false
')' }) +
), ')'
'totalVideoFileSize' ),
'totalVideoFileSize'
]
] ]
] }
} }
}, },
[ScopeNames.WITH_STATS]: { [ScopeNames.WITH_STATS]: (options: WhereUserIdScopeOptions = {}) => {
attributes: { return {
include: [ attributes: {
[ include: [
literal( [
'(' + literal(
'SELECT COUNT("video"."id") ' + '(' +
'FROM "video" ' + 'SELECT COUNT("video"."id") ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 'FROM "video" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
')' `WHERE "account"."userId" = ${options.whereUserId}` +
), ')'
'videosCount' ),
], 'videosCount'
[ ],
literal( [
'(' + literal(
`SELECT concat_ws(':', "abuses", "acceptedAbuses") ` + '(' +
'FROM (' + `SELECT concat_ws(':', "abuses", "acceptedAbuses") ` +
'SELECT COUNT("abuse"."id") AS "abuses", ' + 'FROM (' +
`COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` + 'SELECT COUNT("abuse"."id") AS "abuses", ' +
`COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` +
'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' +
`WHERE "account"."userId" = ${options.whereUserId}` +
') t' +
')'
),
'abusesCount'
],
[
literal(
'(' +
'SELECT COUNT("abuse"."id") ' +
'FROM "abuse" ' + 'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' + 'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + `WHERE "account"."userId" = ${options.whereUserId}` +
') t' + ')'
')' ),
), 'abusesCreatedCount'
'abusesCount' ],
], [
[ literal(
literal( '(' +
'(' + 'SELECT COUNT("videoComment"."id") ' +
'SELECT COUNT("abuse"."id") ' + 'FROM "videoComment" ' +
'FROM "abuse" ' + 'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' + `WHERE "account"."userId" = ${options.whereUserId}` +
'WHERE "account"."userId" = "UserModel"."id"' + ')'
')' ),
), 'videoCommentsCount'
'abusesCreatedCount' ]
],
[
literal(
'(' +
'SELECT COUNT("videoComment"."id") ' +
'FROM "videoComment" ' +
'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' +
')'
),
'videoCommentsCount'
] ]
] }
} }
} }
})) }))
@ -619,17 +627,20 @@ export class UserModel extends SequelizeModel<UserModel> {
} }
static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> { static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> {
const scopes = [ const scopes: (string | ScopeOptions)[] = [ ScopeNames.WITH_VIDEOCHANNELS ]
ScopeNames.WITH_VIDEOCHANNELS
]
if (withStats) { if (withStats) {
scopes.push(ScopeNames.WITH_QUOTA) const scopeOptions: WhereUserIdScopeOptions = { whereUserId: '$userId' }
scopes.push(ScopeNames.WITH_STATS)
scopes.push(ScopeNames.WITH_TOTAL_FILE_SIZES) scopes.push({ method: [ ScopeNames.WITH_QUOTA, scopeOptions ] })
scopes.push({ method: [ ScopeNames.WITH_STATS, scopeOptions ] })
scopes.push({ method: [ ScopeNames.WITH_TOTAL_FILE_SIZES, scopeOptions ] })
} }
return UserModel.scope(scopes).findByPk(id) return UserModel.scope(scopes).findOne({
where: { id },
bind: { userId: id }
})
} }
static loadByUsername (username: string): Promise<MUserDefault> { static loadByUsername (username: string): Promise<MUserDefault> {

View File

@ -349,7 +349,7 @@ export class VideoFileModel extends SequelizeModel<VideoFileModel> {
} }
} }
return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, whereVideo ] }) return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, { whereVideo } ] })
.findOne(options) .findOne(options)
.then(file => { .then(file => {
if (!file) return null if (!file) return null