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,12 +24,10 @@ 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 {
display: flex; display: flex;

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,14 +161,15 @@ enum ScopeNames {
} }
] ]
}, },
[ScopeNames.WITH_QUOTA]: { [ScopeNames.WITH_QUOTA]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: { attributes: {
include: [ include: [
[ [
literal( literal(
'(' + '(' +
UserModel.generateUserQuotaBaseSQL({ UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"', whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: false, daily: false,
onlyMaxResolution: true onlyMaxResolution: true
}) + }) +
@ -178,7 +181,7 @@ enum ScopeNames {
literal( literal(
'(' + '(' +
UserModel.generateUserQuotaBaseSQL({ UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"', whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: true, daily: true,
onlyMaxResolution: true onlyMaxResolution: true
}) + }) +
@ -188,15 +191,17 @@ enum ScopeNames {
] ]
] ]
} }
}
}, },
[ScopeNames.WITH_TOTAL_FILE_SIZES]: { [ScopeNames.WITH_TOTAL_FILE_SIZES]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: { attributes: {
include: [ include: [
[ [
literal( literal(
'(' + '(' +
UserModel.generateUserQuotaBaseSQL({ UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"', whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: false, daily: false,
onlyMaxResolution: false onlyMaxResolution: false
}) + }) +
@ -206,8 +211,10 @@ enum ScopeNames {
] ]
] ]
} }
}
}, },
[ScopeNames.WITH_STATS]: { [ScopeNames.WITH_STATS]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: { attributes: {
include: [ include: [
[ [
@ -217,7 +224,7 @@ enum ScopeNames {
'FROM "video" ' + 'FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + `WHERE "account"."userId" = ${options.whereUserId}` +
')' ')'
), ),
'videosCount' 'videosCount'
@ -231,7 +238,7 @@ enum ScopeNames {
`COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` + `COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` +
'FROM "abuse" ' + 'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' + 'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + `WHERE "account"."userId" = ${options.whereUserId}` +
') t' + ') t' +
')' ')'
), ),
@ -243,7 +250,7 @@ enum ScopeNames {
'SELECT COUNT("abuse"."id") ' + 'SELECT COUNT("abuse"."id") ' +
'FROM "abuse" ' + 'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' + 'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + `WHERE "account"."userId" = ${options.whereUserId}` +
')' ')'
), ),
'abusesCreatedCount' 'abusesCreatedCount'
@ -254,7 +261,7 @@ enum ScopeNames {
'SELECT COUNT("videoComment"."id") ' + 'SELECT COUNT("videoComment"."id") ' +
'FROM "videoComment" ' + 'FROM "videoComment" ' +
'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' + 'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' + `WHERE "account"."userId" = ${options.whereUserId}` +
')' ')'
), ),
'videoCommentsCount' 'videoCommentsCount'
@ -262,6 +269,7 @@ enum ScopeNames {
] ]
} }
} }
}
})) }))
@Table({ @Table({
tableName: 'user', tableName: 'user',
@ -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