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);
}
.danger-zone {
button {
.danger-zone button {
@include danger-button;
@include disable-outline;
}
}
.dashboard {
display: flex;

View File

@ -21,7 +21,7 @@ import {
MUserNotifSettingChannelDefault,
MUserWithNotificationSetting
} 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 {
AfterDestroy,
AfterUpdate,
@ -85,6 +85,8 @@ enum ScopeNames {
WITH_STATS = 'WITH_STATS'
}
type WhereUserIdScopeOptions = { whereUserId?: '$userId' | '"UserModel"."id"' }
@DefaultScope(() => ({
include: [
{
@ -159,14 +161,15 @@ enum ScopeNames {
}
]
},
[ScopeNames.WITH_QUOTA]: {
[ScopeNames.WITH_QUOTA]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: {
include: [
[
literal(
'(' +
UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"',
whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: false,
onlyMaxResolution: true
}) +
@ -178,7 +181,7 @@ enum ScopeNames {
literal(
'(' +
UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"',
whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: true,
onlyMaxResolution: true
}) +
@ -188,15 +191,17 @@ enum ScopeNames {
]
]
}
}
},
[ScopeNames.WITH_TOTAL_FILE_SIZES]: {
[ScopeNames.WITH_TOTAL_FILE_SIZES]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: {
include: [
[
literal(
'(' +
UserModel.generateUserQuotaBaseSQL({
whereUserId: '"UserModel"."id"',
whereUserId: options.whereUserId ?? '"UserModel"."id"',
daily: false,
onlyMaxResolution: false
}) +
@ -206,8 +211,10 @@ enum ScopeNames {
]
]
}
}
},
[ScopeNames.WITH_STATS]: {
[ScopeNames.WITH_STATS]: (options: WhereUserIdScopeOptions = {}) => {
return {
attributes: {
include: [
[
@ -217,7 +224,7 @@ enum ScopeNames {
'FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' +
`WHERE "account"."userId" = ${options.whereUserId}` +
')'
),
'videosCount'
@ -231,7 +238,7 @@ enum ScopeNames {
`COUNT("abuse"."id") FILTER (WHERE "abuse"."state" = ${AbuseState.ACCEPTED}) AS "acceptedAbuses" ` +
'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."flaggedAccountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' +
`WHERE "account"."userId" = ${options.whereUserId}` +
') t' +
')'
),
@ -243,7 +250,7 @@ enum ScopeNames {
'SELECT COUNT("abuse"."id") ' +
'FROM "abuse" ' +
'INNER JOIN "account" ON "account"."id" = "abuse"."reporterAccountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' +
`WHERE "account"."userId" = ${options.whereUserId}` +
')'
),
'abusesCreatedCount'
@ -254,7 +261,7 @@ enum ScopeNames {
'SELECT COUNT("videoComment"."id") ' +
'FROM "videoComment" ' +
'INNER JOIN "account" ON "account"."id" = "videoComment"."accountId" ' +
'WHERE "account"."userId" = "UserModel"."id"' +
`WHERE "account"."userId" = ${options.whereUserId}` +
')'
),
'videoCommentsCount'
@ -262,6 +269,7 @@ enum ScopeNames {
]
}
}
}
}))
@Table({
tableName: 'user',
@ -619,17 +627,20 @@ export class UserModel extends SequelizeModel<UserModel> {
}
static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> {
const scopes = [
ScopeNames.WITH_VIDEOCHANNELS
]
const scopes: (string | ScopeOptions)[] = [ ScopeNames.WITH_VIDEOCHANNELS ]
if (withStats) {
scopes.push(ScopeNames.WITH_QUOTA)
scopes.push(ScopeNames.WITH_STATS)
scopes.push(ScopeNames.WITH_TOTAL_FILE_SIZES)
const scopeOptions: WhereUserIdScopeOptions = { whereUserId: '$userId' }
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> {

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)
.then(file => {
if (!file) return null