From 14d3270f363245d2c83fcc2ac109e39743b5627e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 9 Oct 2017 11:06:13 +0200 Subject: Change how we handle resolution It was an enum before, now we just use video height --- server/models/user/user.ts | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) (limited to 'server/models/user/user.ts') 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) + }) } -- cgit v1.2.3