]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
add postgresql96-contrib to the FreeBSD dependencies (#958)
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index 81b0651fdf915fc3193f8b62752991ce5c559b9a..0150df4ce6303547abff5f44bc2decdce1af6a70 100644 (file)
@@ -161,6 +161,25 @@ export class UserModel extends Model<UserModel> {
 
   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<UserModel> {
 
     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<UserModel> {
       '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<UserModel> {
   }
 
   toFormattedJSON (): User {
+    const videoQuotaUsed = this.get('videoQuotaUsed')
+
     const json = {
       id: this.id,
       username: this.username,
@@ -294,7 +317,8 @@ export class UserModel extends Model<UserModel> {
       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) {