]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/user/user.ts
Upgrade express validator to v4
[github/Chocobozzz/PeerTube.git] / server / models / user / user.ts
index bda95d2f62ef622f560eb83d46dee03390766322..79a595528db53babdd2371a7d880ff803b2196ff 100644 (file)
@@ -1,5 +1,6 @@
 import { values } from 'lodash'
 import * as Sequelize from 'sequelize'
+import * as Promise from 'bluebird'
 
 import { getSort } from '../utils'
 import { USER_ROLES } from '../../initializers'
@@ -8,7 +9,8 @@ import {
   comparePassword,
   isUserPasswordValid,
   isUserUsernameValid,
-  isUserDisplayNSFWValid
+  isUserDisplayNSFWValid,
+  isUserVideoQuotaValid
 } from '../../helpers'
 
 import { addMethodsToModel } from '../utils'
@@ -21,7 +23,7 @@ import {
 
 let User: Sequelize.Model<UserInstance, UserAttributes>
 let isPasswordMatch: UserMethods.IsPasswordMatch
-let toFormatedJSON: UserMethods.ToFormatedJSON
+let toFormattedJSON: UserMethods.ToFormattedJSON
 let isAdmin: UserMethods.IsAdmin
 let countTotal: UserMethods.CountTotal
 let getByUsername: UserMethods.GetByUsername
@@ -30,6 +32,7 @@ let listForApi: UserMethods.ListForApi
 let loadById: UserMethods.LoadById
 let loadByUsername: UserMethods.LoadByUsername
 let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
+let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo
 
 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
   User = sequelize.define<UserInstance, UserAttributes>('User',
@@ -75,6 +78,16 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
       role: {
         type: DataTypes.ENUM(values(USER_ROLES)),
         allowNull: false
+      },
+      videoQuota: {
+        type: DataTypes.BIGINT,
+        allowNull: false,
+        validate: {
+          videoQuotaValid: value => {
+            const res = isUserVideoQuotaValid(value)
+            if (res === false) throw new Error('Video quota is not valid.')
+          }
+        }
       }
     },
     {
@@ -108,8 +121,9 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
   ]
   const instanceMethods = [
     isPasswordMatch,
-    toFormatedJSON,
-    isAdmin
+    toFormattedJSON,
+    isAdmin,
+    isAbleToUploadVideo
   ]
   addMethodsToModel(User, classMethods, instanceMethods)
 
@@ -129,13 +143,14 @@ isPasswordMatch = function (this: UserInstance, password: string) {
   return comparePassword(password, this.password)
 }
 
-toFormatedJSON = function (this: UserInstance) {
+toFormattedJSON = function (this: UserInstance) {
   return {
     id: this.id,
     username: this.username,
     email: this.email,
     displayNSFW: this.displayNSFW,
     role: this.role,
+    videoQuota: this.videoQuota,
     createdAt: this.createdAt
   }
 }
@@ -144,6 +159,14 @@ isAdmin = function (this: UserInstance) {
   return this.role === USER_ROLES.ADMIN
 }
 
+isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) {
+  if (this.videoQuota === -1) return Promise.resolve(true)
+
+  return getOriginalVideoFileTotalFromUser(this).then(totalBytes => {
+    return (videoFile.size + totalBytes) < this.videoQuota
+  })
+}
+
 // ------------------------------ STATICS ------------------------------
 
 function associate (models) {
@@ -198,7 +221,7 @@ loadById = function (id: number) {
 loadByUsername = function (username: string) {
   const query = {
     where: {
-      username: username
+      username
     }
   }
 
@@ -212,5 +235,40 @@ loadByUsernameOrEmail = function (username: string, email: string) {
     }
   }
 
-  return User.findOne(query)
+  // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387
+  return (User as any).findOne(query)
+}
+
+// ---------------------------------------------------------------------------
+
+function getOriginalVideoFileTotalFromUser (user: UserInstance) {
+  // attributes = [] because we don't want other fields than the sum
+  const query = {
+    where: {
+      resolution: 0 // Original, TODO: improve readability
+    },
+    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
+                }
+              }
+            ]
+          }
+        ]
+      }
+    ]
+  }
+
+  return User['sequelize'].models.VideoFile.sum('size', query)
 }