X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=inline;f=server%2Fmodels%2Fuser%2Fuser.ts;h=0dc52d3cfdaa3d19c2e0397b098111719e205a3c;hb=14d3270f363245d2c83fcc2ac109e39743b5627e;hp=7a21dbefaffb80947d52d4df4606cd9ebd137c41;hpb=40298b02546e8225dd21bf6048fe7f224aefc32a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 7a21dbefa..0dc52d3cf 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -12,7 +12,6 @@ import { isUserDisplayNSFWValid, isUserVideoQuotaValid } from '../../helpers' -import { VideoResolution } from '../../../shared' import { addMethodsToModel } from '../utils' import { @@ -243,33 +242,21 @@ loadByUsernameOrEmail = function (username: string, email: string) { // --------------------------------------------------------------------------- function getOriginalVideoFileTotalFromUser (user: UserInstance) { - // attributes = [] because we don't want other fields than the sum - const query = { - where: { - resolution: VideoResolution.ORIGINAL - }, - include: [ - { - attributes: [], - model: User['sequelize'].models.Video, - include: [ - { - attributes: [], - model: User['sequelize'].models.Author, - include: [ - { - attributes: [], - model: User['sequelize'].models.User, - where: { - id: user.id - } - } - ] - } - ] - } - ] + // Don't use sequelize because we need to use a subquery + const query = 'SELECT SUM("size") AS "total" FROM ' + + '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + + 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + + 'INNER JOIN "Authors" ON "Videos"."authorId" = "Authors"."id" ' + + 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' + + 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' + + const options = { + bind: { userId: user.id }, + type: Sequelize.QueryTypes.SELECT } + return User['sequelize'].query(query, options).then(([ { total } ]) => { + if (total === null) return 0 - return User['sequelize'].models.VideoFile.sum('size', query) + return parseInt(total, 10) + }) }