From a76138ff56ad3ad2df926f59578a06b22fb1f93c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Aug 2018 17:56:51 +0200 Subject: Add quota used in users list --- server/models/account/user.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 81b0651fd..0150df4ce 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -161,6 +161,25 @@ export class UserModel extends Model { static listForApi (start: number, count: number, sort: string) { const query = { + attributes: { + include: [ + [ + Sequelize.literal( + '(' + + 'SELECT COALESCE(SUM("size"), 0) FROM ' + + '(' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + + ') t' + + ')' + ), + 'videoQuotaUsed' + ] as any // FIXME: typings + ] + }, offset: start, limit: count, order: getSort(sort) @@ -168,6 +187,9 @@ export class UserModel extends Model { return UserModel.findAndCountAll(query) .then(({ rows, count }) => { + console.log(rows[0]) + console.log(rows[0]['videoQuotaUsed']) + console.log(rows[0].get('videoQuotaUsed')) return { data: rows, total: count @@ -249,8 +271,7 @@ export class UserModel extends Model { 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' + - 'WHERE "user"."id" = $userId GROUP BY "video"."id") t' + 'WHERE "account"."userId" = $userId GROUP BY "video"."id") t' const options = { bind: { userId: user.id }, @@ -281,6 +302,8 @@ export class UserModel extends Model { } toFormattedJSON (): User { + const videoQuotaUsed = this.get('videoQuotaUsed') + const json = { id: this.id, username: this.username, @@ -294,7 +317,8 @@ export class UserModel extends Model { blocked: this.blocked, blockedReason: this.blockedReason, account: this.Account.toFormattedJSON(), - videoChannels: [] + videoChannels: [], + videoQuotaUsed: videoQuotaUsed !== undefined ? parseInt(videoQuotaUsed, 10) : undefined } if (Array.isArray(this.Account.VideoChannels) === true) { -- cgit v1.2.3