import { values } from 'lodash'
import * as Sequelize from 'sequelize'
+import * as Promise from 'bluebird'
import { getSort } from '../utils'
import { USER_ROLES } from '../../initializers'
comparePassword,
isUserPasswordValid,
isUserUsernameValid,
- isUserDisplayNSFWValid
+ isUserDisplayNSFWValid,
+ isUserVideoQuotaValid
} from '../../helpers'
import { addMethodsToModel } from '../utils'
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
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',
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.')
+ }
+ }
}
},
{
]
const instanceMethods = [
isPasswordMatch,
- toFormatedJSON,
- isAdmin
+ toFormattedJSON,
+ isAdmin,
+ isAbleToUploadVideo
]
addMethodsToModel(User, classMethods, instanceMethods)
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
}
}
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) {
loadByUsername = function (username: string) {
const query = {
where: {
- username: username
+ username
}
}
}
}
- 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)
}