]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index 56af2f30a979605248dfa3d8de08dce2cb328ae7..0150df4ce6303547abff5f44bc2decdce1af6a70 100644 (file)
@@ -21,6 +21,8 @@ import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared'
 import { User, UserRole } from '../../../shared/models/users'
 import {
   isUserAutoPlayVideoValid,
+  isUserBlockedReasonValid,
+  isUserBlockedValid,
   isUserNSFWPolicyValid,
   isUserPasswordValid,
   isUserRoleValid,
@@ -36,6 +38,10 @@ import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type'
 import { values } from 'lodash'
 import { NSFW_POLICY_TYPES } from '../../initializers'
 
+enum ScopeNames {
+  WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
+}
+
 @DefaultScope({
   include: [
     {
@@ -45,7 +51,7 @@ import { NSFW_POLICY_TYPES } from '../../initializers'
   ]
 })
 @Scopes({
-  withVideoChannel: {
+  [ScopeNames.WITH_VIDEO_CHANNEL]: {
     include: [
       {
         model: () => AccountModel,
@@ -96,6 +102,18 @@ export class UserModel extends Model<UserModel> {
   @Column
   autoPlayVideo: boolean
 
+  @AllowNull(false)
+  @Default(false)
+  @Is('UserBlocked', value => throwIfNotValid(value, isUserBlockedValid, 'blocked boolean'))
+  @Column
+  blocked: boolean
+
+  @AllowNull(true)
+  @Default(null)
+  @Is('UserBlockedReason', value => throwIfNotValid(value, isUserBlockedReasonValid, 'blocked reason'))
+  @Column
+  blockedReason: string
+
   @AllowNull(false)
   @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role'))
   @Column
@@ -143,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)
@@ -150,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
@@ -199,7 +239,7 @@ export class UserModel extends Model<UserModel> {
       }
     }
 
-    return UserModel.scope('withVideoChannel').findOne(query)
+    return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query)
   }
 
   static loadByEmail (email: string) {
@@ -231,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 },
@@ -263,6 +302,8 @@ export class UserModel extends Model<UserModel> {
   }
 
   toFormattedJSON (): User {
+    const videoQuotaUsed = this.get('videoQuotaUsed')
+
     const json = {
       id: this.id,
       username: this.username,
@@ -273,8 +314,11 @@ export class UserModel extends Model<UserModel> {
       roleLabel: USER_ROLE_LABELS[ this.role ],
       videoQuota: this.videoQuota,
       createdAt: this.createdAt,
+      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) {
@@ -291,7 +335,7 @@ export class UserModel extends Model<UserModel> {
     return json
   }
 
-  isAbleToUploadVideo (videoFile: Express.Multer.File) {
+  isAbleToUploadVideo (videoFile: { size: number }) {
     if (this.videoQuota === -1) return Promise.resolve(true)
 
     return UserModel.getOriginalVideoFileTotalFromUser(this)