X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fuser%2Fuser.ts;h=85720abfb59348a2b1547456cb7a383e96f09b8d;hb=9a82ce2455874a80167f5ae30bb19ea12eb0551e;hp=4ad76e5bc4377ac1fe3e115b4d25eaea8f6d27e4;hpb=3318147300b4f998adf728eb0a5a14a4c1829c51;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 4ad76e5bc..85720abfb 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -48,14 +48,14 @@ import { isUserEmailVerifiedValid, isUserNoModal, isUserNSFWPolicyValid, + isUserP2PEnabledValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, isUserVideoLanguages, isUserVideoQuotaDailyValid, isUserVideoQuotaValid, - isUserVideosHistoryEnabledValid, - isUserP2PEnabledValid + isUserVideosHistoryEnabledValid } from '../../helpers/custom-validators/users' import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' @@ -72,10 +72,13 @@ import { VideoImportModel } from '../video/video-import' import { VideoLiveModel } from '../video/video-live' import { VideoPlaylistModel } from '../video/video-playlist' import { UserNotificationSettingModel } from './user-notification-setting' +import { LiveQuotaStore } from '@server/lib/live' +import { logger } from '@server/helpers/logger' enum ScopeNames { FOR_ME_API = 'FOR_ME_API', WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', + WITH_QUOTA = 'WITH_QUOTA', WITH_STATS = 'WITH_STATS' } @@ -106,7 +109,7 @@ enum ScopeNames { include: [ { model: ActorImageModel, - as: 'Banner', + as: 'Banners', required: false } ] @@ -153,7 +156,7 @@ enum ScopeNames { } ] }, - [ScopeNames.WITH_STATS]: { + [ScopeNames.WITH_QUOTA]: { attributes: { include: [ [ @@ -161,12 +164,31 @@ enum ScopeNames { '(' + UserModel.generateUserQuotaBaseSQL({ withSelect: false, - whereUserId: '"UserModel"."id"' + whereUserId: '"UserModel"."id"', + daily: false }) + ')' ), 'videoQuotaUsed' ], + [ + literal( + '(' + + UserModel.generateUserQuotaBaseSQL({ + withSelect: false, + whereUserId: '"UserModel"."id"', + daily: true + }) + + ')' + ), + 'videoQuotaUsedDaily' + ] + ] + } + }, + [ScopeNames.WITH_STATS]: { + attributes: { + include: [ [ literal( '(' + @@ -474,34 +496,16 @@ export class UserModel extends Model>> { } const query: FindOptions = { - attributes: { - include: [ - [ - literal( - '(' + - UserModel.generateUserQuotaBaseSQL({ - withSelect: false, - whereUserId: '"UserModel"."id"' - }) + - ')' - ), - 'videoQuotaUsed' - ] - ] - }, offset: start, limit: count, order: getSort(sort), where } - return UserModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + UserModel.unscoped().count(query), + UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA ]).findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } static listWithRight (right: UserRight): Promise { @@ -582,7 +586,10 @@ export class UserModel extends Model>> { ScopeNames.WITH_VIDEOCHANNELS ] - if (withStats) scopes.push(ScopeNames.WITH_STATS) + if (withStats) { + scopes.push(ScopeNames.WITH_QUOTA) + scopes.push(ScopeNames.WITH_STATS) + } return UserModel.scope(scopes).findByPk(id) } @@ -623,7 +630,7 @@ export class UserModel extends Model>> { const query = { where: { [Op.or]: [ - where(fn('lower', col('username')), fn('lower', username)), + where(fn('lower', col('username')), fn('lower', username) as any), { email } ] @@ -763,10 +770,10 @@ export class UserModel extends Model>> { static generateUserQuotaBaseSQL (options: { whereUserId: '$userId' | '"UserModel"."id"' withSelect: boolean - where?: string + daily: boolean }) { - const andWhere = options.where - ? 'AND ' + options.where + const andWhere = options.daily === true + ? 'AND "video"."createdAt" > now() - interval \'24 hours\'' : '' const videoChannelJoin = 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + @@ -907,12 +914,15 @@ export class UserModel extends Model>> { videoQuota: this.videoQuota, videoQuotaDaily: this.videoQuotaDaily, + videoQuotaUsed: videoQuotaUsed !== undefined - ? parseInt(videoQuotaUsed + '', 10) + ? parseInt(videoQuotaUsed + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined - ? parseInt(videoQuotaUsedDaily + '', 10) + ? parseInt(videoQuotaUsedDaily + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id) : undefined, + videosCount: videosCount !== undefined ? parseInt(videosCount + '', 10) : undefined,