aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-05-04 10:07:06 +0200
committerChocobozzz <me@florianbigard.com>2022-05-04 10:32:49 +0200
commit9a82ce2455874a80167f5ae30bb19ea12eb0551e (patch)
tree0b4fe8ff0bfd00d0f3b60e047ec9d7cfdd7c0c7a /server/models/user
parentb003d5751803e737b34dc71ce61e66a0d9d12eb4 (diff)
downloadPeerTube-9a82ce2455874a80167f5ae30bb19ea12eb0551e.tar.gz
PeerTube-9a82ce2455874a80167f5ae30bb19ea12eb0551e.tar.zst
PeerTube-9a82ce2455874a80167f5ae30bb19ea12eb0551e.zip
Fix quota inconstistencies with lives
Diffstat (limited to 'server/models/user')
-rw-r--r--server/models/user/user.ts61
1 files changed, 37 insertions, 24 deletions
diff --git a/server/models/user/user.ts b/server/models/user/user.ts
index bcf56dfa1..85720abfb 100644
--- a/server/models/user/user.ts
+++ b/server/models/user/user.ts
@@ -72,10 +72,13 @@ import { VideoImportModel } from '../video/video-import'
72import { VideoLiveModel } from '../video/video-live' 72import { VideoLiveModel } from '../video/video-live'
73import { VideoPlaylistModel } from '../video/video-playlist' 73import { VideoPlaylistModel } from '../video/video-playlist'
74import { UserNotificationSettingModel } from './user-notification-setting' 74import { UserNotificationSettingModel } from './user-notification-setting'
75import { LiveQuotaStore } from '@server/lib/live'
76import { logger } from '@server/helpers/logger'
75 77
76enum ScopeNames { 78enum ScopeNames {
77 FOR_ME_API = 'FOR_ME_API', 79 FOR_ME_API = 'FOR_ME_API',
78 WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS', 80 WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS',
81 WITH_QUOTA = 'WITH_QUOTA',
79 WITH_STATS = 'WITH_STATS' 82 WITH_STATS = 'WITH_STATS'
80} 83}
81 84
@@ -153,7 +156,7 @@ enum ScopeNames {
153 } 156 }
154 ] 157 ]
155 }, 158 },
156 [ScopeNames.WITH_STATS]: { 159 [ScopeNames.WITH_QUOTA]: {
157 attributes: { 160 attributes: {
158 include: [ 161 include: [
159 [ 162 [
@@ -161,7 +164,8 @@ enum ScopeNames {
161 '(' + 164 '(' +
162 UserModel.generateUserQuotaBaseSQL({ 165 UserModel.generateUserQuotaBaseSQL({
163 withSelect: false, 166 withSelect: false,
164 whereUserId: '"UserModel"."id"' 167 whereUserId: '"UserModel"."id"',
168 daily: false
165 }) + 169 }) +
166 ')' 170 ')'
167 ), 171 ),
@@ -170,6 +174,24 @@ enum ScopeNames {
170 [ 174 [
171 literal( 175 literal(
172 '(' + 176 '(' +
177 UserModel.generateUserQuotaBaseSQL({
178 withSelect: false,
179 whereUserId: '"UserModel"."id"',
180 daily: true
181 }) +
182 ')'
183 ),
184 'videoQuotaUsedDaily'
185 ]
186 ]
187 }
188 },
189 [ScopeNames.WITH_STATS]: {
190 attributes: {
191 include: [
192 [
193 literal(
194 '(' +
173 'SELECT COUNT("video"."id") ' + 195 'SELECT COUNT("video"."id") ' +
174 'FROM "video" ' + 196 'FROM "video" ' +
175 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 197 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
@@ -474,21 +496,6 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
474 } 496 }
475 497
476 const query: FindOptions = { 498 const query: FindOptions = {
477 attributes: {
478 include: [
479 [
480 literal(
481 '(' +
482 UserModel.generateUserQuotaBaseSQL({
483 withSelect: false,
484 whereUserId: '"UserModel"."id"'
485 }) +
486 ')'
487 ),
488 'videoQuotaUsed'
489 ]
490 ]
491 },
492 offset: start, 499 offset: start,
493 limit: count, 500 limit: count,
494 order: getSort(sort), 501 order: getSort(sort),
@@ -497,7 +504,7 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
497 504
498 return Promise.all([ 505 return Promise.all([
499 UserModel.unscoped().count(query), 506 UserModel.unscoped().count(query),
500 UserModel.findAll(query) 507 UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA ]).findAll(query)
501 ]).then(([ total, data ]) => ({ total, data })) 508 ]).then(([ total, data ]) => ({ total, data }))
502 } 509 }
503 510
@@ -579,7 +586,10 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
579 ScopeNames.WITH_VIDEOCHANNELS 586 ScopeNames.WITH_VIDEOCHANNELS
580 ] 587 ]
581 588
582 if (withStats) scopes.push(ScopeNames.WITH_STATS) 589 if (withStats) {
590 scopes.push(ScopeNames.WITH_QUOTA)
591 scopes.push(ScopeNames.WITH_STATS)
592 }
583 593
584 return UserModel.scope(scopes).findByPk(id) 594 return UserModel.scope(scopes).findByPk(id)
585 } 595 }
@@ -760,10 +770,10 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
760 static generateUserQuotaBaseSQL (options: { 770 static generateUserQuotaBaseSQL (options: {
761 whereUserId: '$userId' | '"UserModel"."id"' 771 whereUserId: '$userId' | '"UserModel"."id"'
762 withSelect: boolean 772 withSelect: boolean
763 where?: string 773 daily: boolean
764 }) { 774 }) {
765 const andWhere = options.where 775 const andWhere = options.daily === true
766 ? 'AND ' + options.where 776 ? 'AND "video"."createdAt" > now() - interval \'24 hours\''
767 : '' 777 : ''
768 778
769 const videoChannelJoin = 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 779 const videoChannelJoin = 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
@@ -904,12 +914,15 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
904 914
905 videoQuota: this.videoQuota, 915 videoQuota: this.videoQuota,
906 videoQuotaDaily: this.videoQuotaDaily, 916 videoQuotaDaily: this.videoQuotaDaily,
917
907 videoQuotaUsed: videoQuotaUsed !== undefined 918 videoQuotaUsed: videoQuotaUsed !== undefined
908 ? parseInt(videoQuotaUsed + '', 10) 919 ? parseInt(videoQuotaUsed + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id)
909 : undefined, 920 : undefined,
921
910 videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined 922 videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined
911 ? parseInt(videoQuotaUsedDaily + '', 10) 923 ? parseInt(videoQuotaUsedDaily + '', 10) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id)
912 : undefined, 924 : undefined,
925
913 videosCount: videosCount !== undefined 926 videosCount: videosCount !== undefined
914 ? parseInt(videosCount + '', 10) 927 ? parseInt(videosCount + '', 10)
915 : undefined, 928 : undefined,