|
@@ -83,6 +83,7 @@ enum ScopeNames {
|
|
|
FOR_ME_API = 'FOR_ME_API',
|
|
|
WITH_VIDEOCHANNELS = 'WITH_VIDEOCHANNELS',
|
|
|
WITH_QUOTA = 'WITH_QUOTA',
|
|
|
+ WITH_TOTAL_FILE_SIZES = 'WITH_TOTAL_FILE_SIZES',
|
|
|
WITH_STATS = 'WITH_STATS'
|
|
|
}
|
|
|
|
|
@@ -168,7 +169,8 @@ enum ScopeNames {
|
|
|
'(' +
|
|
|
UserModel.generateUserQuotaBaseSQL({
|
|
|
whereUserId: '"UserModel"."id"',
|
|
|
- daily: false
|
|
|
+ daily: false,
|
|
|
+ onlyMaxResolution: true
|
|
|
}) +
|
|
|
')'
|
|
|
),
|
|
@@ -179,7 +181,8 @@ enum ScopeNames {
|
|
|
'(' +
|
|
|
UserModel.generateUserQuotaBaseSQL({
|
|
|
whereUserId: '"UserModel"."id"',
|
|
|
- daily: true
|
|
|
+ daily: true,
|
|
|
+ onlyMaxResolution: true
|
|
|
}) +
|
|
|
')'
|
|
|
),
|
|
@@ -188,6 +191,24 @@ enum ScopeNames {
|
|
|
]
|
|
|
}
|
|
|
},
|
|
|
+ [ScopeNames.WITH_TOTAL_FILE_SIZES]: {
|
|
|
+ attributes: {
|
|
|
+ include: [
|
|
|
+ [
|
|
|
+ literal(
|
|
|
+ '(' +
|
|
|
+ UserModel.generateUserQuotaBaseSQL({
|
|
|
+ whereUserId: '"UserModel"."id"',
|
|
|
+ daily: false,
|
|
|
+ onlyMaxResolution: false
|
|
|
+ }) +
|
|
|
+ ')'
|
|
|
+ ),
|
|
|
+ 'totalVideoFileSize'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ },
|
|
|
[ScopeNames.WITH_STATS]: {
|
|
|
attributes: {
|
|
|
include: [
|
|
@@ -521,7 +542,7 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
|
|
|
return Promise.all([
|
|
|
UserModel.unscoped().count(query),
|
|
|
- UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA ]).findAll(query)
|
|
|
+ UserModel.scope([ 'defaultScope', ScopeNames.WITH_QUOTA, ScopeNames.WITH_TOTAL_FILE_SIZES ]).findAll(query)
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
|
|
}
|
|
|
|
|
@@ -607,6 +628,7 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
if (withStats) {
|
|
|
scopes.push(ScopeNames.WITH_QUOTA)
|
|
|
scopes.push(ScopeNames.WITH_STATS)
|
|
|
+ scopes.push(ScopeNames.WITH_TOTAL_FILE_SIZES)
|
|
|
}
|
|
|
|
|
|
return UserModel.scope(scopes).findByPk(id)
|
|
@@ -805,8 +827,9 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
static generateUserQuotaBaseSQL (options: {
|
|
|
daily: boolean
|
|
|
whereUserId: '$userId' | '"UserModel"."id"'
|
|
|
+ onlyMaxResolution: boolean
|
|
|
}) {
|
|
|
- const { daily, whereUserId } = options
|
|
|
+ const { daily, whereUserId, onlyMaxResolution } = options
|
|
|
|
|
|
const andWhere = daily === true
|
|
|
? 'AND "video"."createdAt" > now() - interval \'24 hours\''
|
|
@@ -825,9 +848,13 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
'INNER JOIN "video" ON "videoStreamingPlaylist"."videoId" = "video"."id" AND "video"."isLive" IS FALSE ' +
|
|
|
videoChannelJoin
|
|
|
|
|
|
+ const sizeSelect = onlyMaxResolution
|
|
|
+ ? 'MAX("t1"."size")'
|
|
|
+ : 'SUM("t1"."size")'
|
|
|
+
|
|
|
return 'SELECT COALESCE(SUM("size"), 0) AS "total" ' +
|
|
|
'FROM (' +
|
|
|
- `SELECT MAX("t1"."size") AS "size" FROM (${webVideoFiles} UNION ${hlsFiles}) t1 ` +
|
|
|
+ `SELECT ${sizeSelect} AS "size" FROM (${webVideoFiles} UNION ${hlsFiles}) t1 ` +
|
|
|
'GROUP BY "t1"."videoId"' +
|
|
|
') t2'
|
|
|
}
|
|
@@ -838,7 +865,7 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
}) {
|
|
|
const { daily, userId } = options
|
|
|
|
|
|
- const sql = this.generateUserQuotaBaseSQL({ daily, whereUserId: '$userId' })
|
|
|
+ const sql = this.generateUserQuotaBaseSQL({ daily, whereUserId: '$userId', onlyMaxResolution: true })
|
|
|
|
|
|
const queryOptions = {
|
|
|
bind: { userId },
|
|
@@ -914,6 +941,7 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
const [ abusesCount, abusesAcceptedCount ] = (this.get('abusesCount') as string || ':').split(':')
|
|
|
const abusesCreatedCount = this.get('abusesCreatedCount')
|
|
|
const videoCommentsCount = this.get('videoCommentsCount')
|
|
|
+ const totalVideoFileSize = this.get('totalVideoFileSize')
|
|
|
|
|
|
const json: User = {
|
|
|
id: this.id,
|
|
@@ -943,12 +971,16 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
|
|
|
videoQuota: this.videoQuota,
|
|
|
videoQuotaDaily: this.videoQuotaDaily,
|
|
|
|
|
|
+ totalVideoFileSize: totalVideoFileSize !== undefined
|
|
|
+ ? forceNumber(totalVideoFileSize)
|
|
|
+ : undefined,
|
|
|
+
|
|
|
videoQuotaUsed: videoQuotaUsed !== undefined
|
|
|
- ? forceNumber(videoQuotaUsed) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id)
|
|
|
+ ? forceNumber(videoQuotaUsed) + LiveQuotaStore.Instance.getLiveQuotaOfUser(this.id)
|
|
|
: undefined,
|
|
|
|
|
|
videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined
|
|
|
- ? forceNumber(videoQuotaUsedDaily) + LiveQuotaStore.Instance.getLiveQuotaOf(this.id)
|
|
|
+ ? forceNumber(videoQuotaUsedDaily) + LiveQuotaStore.Instance.getLiveQuotaOfUser(this.id)
|
|
|
: undefined,
|
|
|
|
|
|
videosCount: videosCount !== undefined
|