2017-05-15 22:22:03 +02:00
|
|
|
import { values } from 'lodash'
|
2017-05-22 20:58:25 +02:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-09-04 20:07:54 +02:00
|
|
|
import * as Promise from 'bluebird'
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-06-16 09:45:46 +02:00
|
|
|
import { getSort } from '../utils'
|
|
|
|
import { USER_ROLES } from '../../initializers'
|
2017-05-15 22:22:03 +02:00
|
|
|
import {
|
|
|
|
cryptPassword,
|
|
|
|
comparePassword,
|
|
|
|
isUserPasswordValid,
|
|
|
|
isUserUsernameValid,
|
2017-09-04 20:07:54 +02:00
|
|
|
isUserDisplayNSFWValid,
|
|
|
|
isUserVideoQuotaValid
|
2017-06-16 09:45:46 +02:00
|
|
|
} from '../../helpers'
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-06-16 09:45:46 +02:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 20:58:25 +02:00
|
|
|
import {
|
|
|
|
UserInstance,
|
|
|
|
UserAttributes,
|
|
|
|
|
|
|
|
UserMethods
|
|
|
|
} from './user-interface'
|
|
|
|
|
|
|
|
let User: Sequelize.Model<UserInstance, UserAttributes>
|
|
|
|
let isPasswordMatch: UserMethods.IsPasswordMatch
|
2017-08-25 11:45:31 +02:00
|
|
|
let toFormattedJSON: UserMethods.ToFormattedJSON
|
2017-05-22 20:58:25 +02:00
|
|
|
let isAdmin: UserMethods.IsAdmin
|
|
|
|
let countTotal: UserMethods.CountTotal
|
|
|
|
let getByUsername: UserMethods.GetByUsername
|
|
|
|
let list: UserMethods.List
|
|
|
|
let listForApi: UserMethods.ListForApi
|
|
|
|
let loadById: UserMethods.LoadById
|
|
|
|
let loadByUsername: UserMethods.LoadByUsername
|
|
|
|
let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
|
2017-09-04 20:07:54 +02:00
|
|
|
let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo
|
2017-05-22 20:58:25 +02:00
|
|
|
|
2017-06-11 17:35:32 +02:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
User = sequelize.define<UserInstance, UserAttributes>('User',
|
2016-12-11 21:50:51 +01:00
|
|
|
{
|
|
|
|
password: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
2017-07-11 17:04:57 +02:00
|
|
|
passwordValid: value => {
|
2017-05-15 22:22:03 +02:00
|
|
|
const res = isUserPasswordValid(value)
|
2016-12-28 15:49:23 +01:00
|
|
|
if (res === false) throw new Error('Password not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:50:51 +01:00
|
|
|
},
|
|
|
|
username: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
2017-07-11 17:04:57 +02:00
|
|
|
usernameValid: value => {
|
2017-05-15 22:22:03 +02:00
|
|
|
const res = isUserUsernameValid(value)
|
2016-12-28 15:49:23 +01:00
|
|
|
if (res === false) throw new Error('Username not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:50:51 +01:00
|
|
|
},
|
2017-02-18 09:29:59 +01:00
|
|
|
email: {
|
2017-02-18 11:56:28 +01:00
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 09:29:59 +01:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
|
|
|
},
|
2017-04-03 21:24:36 +02:00
|
|
|
displayNSFW: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: false,
|
|
|
|
validate: {
|
2017-07-11 17:04:57 +02:00
|
|
|
nsfwValid: value => {
|
2017-05-15 22:22:03 +02:00
|
|
|
const res = isUserDisplayNSFWValid(value)
|
2017-04-03 21:24:36 +02:00
|
|
|
if (res === false) throw new Error('Display NSFW is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-12-11 21:50:51 +01:00
|
|
|
role: {
|
2017-05-15 22:22:03 +02:00
|
|
|
type: DataTypes.ENUM(values(USER_ROLES)),
|
2016-12-28 15:49:23 +01:00
|
|
|
allowNull: false
|
2017-09-04 20:07:54 +02:00
|
|
|
},
|
|
|
|
videoQuota: {
|
|
|
|
type: DataTypes.BIGINT,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
videoQuotaValid: value => {
|
|
|
|
const res = isUserVideoQuotaValid(value)
|
|
|
|
if (res === false) throw new Error('Video quota is not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 09:33:28 +01:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 19:24:34 +01:00
|
|
|
fields: [ 'username' ],
|
|
|
|
unique: true
|
2017-02-18 09:29:59 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'email' ],
|
|
|
|
unique: true
|
2016-12-29 09:33:28 +01:00
|
|
|
}
|
|
|
|
],
|
2016-12-11 21:50:51 +01:00
|
|
|
hooks: {
|
|
|
|
beforeCreate: beforeCreateOrUpdate,
|
|
|
|
beforeUpdate: beforeCreateOrUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
getByUsername,
|
|
|
|
list,
|
|
|
|
listForApi,
|
|
|
|
loadById,
|
|
|
|
loadByUsername,
|
|
|
|
loadByUsernameOrEmail
|
|
|
|
]
|
|
|
|
const instanceMethods = [
|
|
|
|
isPasswordMatch,
|
2017-08-25 11:45:31 +02:00
|
|
|
toFormattedJSON,
|
2017-09-04 20:07:54 +02:00
|
|
|
isAdmin,
|
|
|
|
isAbleToUploadVideo
|
2017-05-22 20:58:25 +02:00
|
|
|
]
|
|
|
|
addMethodsToModel(User, classMethods, instanceMethods)
|
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
return User
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|
2016-07-01 16:03:53 +02:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function beforeCreateOrUpdate (user: UserInstance) {
|
2017-07-05 13:26:25 +02:00
|
|
|
return cryptPassword(user.password).then(hash => {
|
|
|
|
user.password = hash
|
|
|
|
return undefined
|
2016-08-25 17:57:37 +02:00
|
|
|
})
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
2016-07-01 16:03:53 +02:00
|
|
|
|
2016-08-25 17:57:37 +02:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
isPasswordMatch = function (this: UserInstance, password: string) {
|
|
|
|
return comparePassword(password, this.password)
|
2016-08-25 17:57:37 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 11:45:31 +02:00
|
|
|
toFormattedJSON = function (this: UserInstance) {
|
2016-08-25 17:57:37 +02:00
|
|
|
return {
|
2016-12-11 21:50:51 +01:00
|
|
|
id: this.id,
|
2016-08-25 17:57:37 +02:00
|
|
|
username: this.username,
|
2017-02-18 09:29:59 +01:00
|
|
|
email: this.email,
|
2017-04-03 21:24:36 +02:00
|
|
|
displayNSFW: this.displayNSFW,
|
2016-09-23 17:19:57 +02:00
|
|
|
role: this.role,
|
2017-09-04 20:07:54 +02:00
|
|
|
videoQuota: this.videoQuota,
|
2016-12-11 21:50:51 +01:00
|
|
|
createdAt: this.createdAt
|
2016-08-25 17:57:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2017-06-16 09:54:59 +02:00
|
|
|
isAdmin = function (this: UserInstance) {
|
2017-05-15 22:22:03 +02:00
|
|
|
return this.role === USER_ROLES.ADMIN
|
2017-04-26 21:22:10 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 20:07:54 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:57:37 +02:00
|
|
|
// ------------------------------ STATICS ------------------------------
|
2016-07-01 16:03:53 +02:00
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
function associate (models) {
|
2017-05-22 20:58:25 +02:00
|
|
|
User.hasOne(models.Author, {
|
2016-12-29 10:33:36 +01:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
User.hasMany(models.OAuthToken, {
|
2016-12-11 21:50:51 +01:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
countTotal = function () {
|
|
|
|
return this.count()
|
2016-08-16 21:51:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
getByUsername = function (username: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username: username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
return User.findOne(query)
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
list = function () {
|
|
|
|
return User.findAll()
|
2016-09-26 22:36:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
listForApi = function (start: number, count: number, sort: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-05-15 22:22:03 +02:00
|
|
|
order: [ getSort(sort) ]
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return User.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
2016-12-11 21:50:51 +01:00
|
|
|
})
|
2016-07-01 16:03:53 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
loadById = function (id: number) {
|
|
|
|
return User.findById(id)
|
2016-08-09 21:44:45 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
loadByUsername = function (username: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
2017-08-25 18:36:49 +02:00
|
|
|
username
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return User.findOne(query)
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|
2017-02-18 09:29:59 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
loadByUsernameOrEmail = function (username: string, email: string) {
|
2017-02-18 09:29:59 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
$or: [ { username }, { email } ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:36:49 +02:00
|
|
|
// FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387
|
|
|
|
return (User as any).findOne(query)
|
2017-02-18 09:29:59 +01:00
|
|
|
}
|
2017-09-04 20:07:54 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function getOriginalVideoFileTotalFromUser (user: UserInstance) {
|
2017-09-06 16:35:40 +02:00
|
|
|
// attributes = [] because we don't want other fields than the sum
|
2017-09-04 20:07:54 +02:00
|
|
|
const query = {
|
|
|
|
where: {
|
2017-09-06 16:35:40 +02:00
|
|
|
resolution: 0 // Original, TODO: improve readability
|
2017-09-04 20:07:54 +02:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
2017-09-06 16:35:40 +02:00
|
|
|
attributes: [],
|
|
|
|
model: User['sequelize'].models.Video,
|
2017-09-04 20:07:54 +02:00
|
|
|
include: [
|
|
|
|
{
|
2017-09-06 16:35:40 +02:00
|
|
|
attributes: [],
|
|
|
|
model: User['sequelize'].models.Author,
|
2017-09-04 20:07:54 +02:00
|
|
|
include: [
|
|
|
|
{
|
2017-09-06 16:35:40 +02:00
|
|
|
attributes: [],
|
|
|
|
model: User['sequelize'].models.User,
|
|
|
|
where: {
|
|
|
|
id: user.id
|
|
|
|
}
|
2017-09-04 20:07:54 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-06 16:35:40 +02:00
|
|
|
return User['sequelize'].models.VideoFile.sum('size', query)
|
2017-09-04 20:07:54 +02:00
|
|
|
}
|