1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
4 import { getSort } from '../utils'
5 import { USER_ROLES } from '../../initializers'
11 isUserDisplayNSFWValid
12 } from '../../helpers'
14 import { addMethodsToModel } from '../utils'
21 } from './user-interface'
23 let User: Sequelize.Model<UserInstance, UserAttributes>
24 let isPasswordMatch: UserMethods.IsPasswordMatch
25 let toFormatedJSON: UserMethods.ToFormatedJSON
26 let isAdmin: UserMethods.IsAdmin
27 let countTotal: UserMethods.CountTotal
28 let getByUsername: UserMethods.GetByUsername
29 let list: UserMethods.List
30 let listForApi: UserMethods.ListForApi
31 let loadById: UserMethods.LoadById
32 let loadByUsername: UserMethods.LoadByUsername
33 let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
35 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
36 User = sequelize.define<UserInstance, UserAttributes>('User',
39 type: DataTypes.STRING,
42 passwordValid: function (value) {
43 const res = isUserPasswordValid(value)
44 if (res === false) throw new Error('Password not valid.')
49 type: DataTypes.STRING,
52 usernameValid: function (value) {
53 const res = isUserUsernameValid(value)
54 if (res === false) throw new Error('Username not valid.')
59 type: DataTypes.STRING(400),
66 type: DataTypes.BOOLEAN,
70 nsfwValid: function (value) {
71 const res = isUserDisplayNSFWValid(value)
72 if (res === false) throw new Error('Display NSFW is not valid.')
77 type: DataTypes.ENUM(values(USER_ROLES)),
84 fields: [ 'username' ],
93 beforeCreate: beforeCreateOrUpdate,
94 beforeUpdate: beforeCreateOrUpdate
99 const classMethods = [
108 loadByUsernameOrEmail
110 const instanceMethods = [
115 addMethodsToModel(User, classMethods, instanceMethods)
120 function beforeCreateOrUpdate (user: UserInstance) {
121 return new Promise(function (resolve, reject) {
122 cryptPassword(user.password, function (err, hash) {
123 if (err) return reject(err)
132 // ------------------------------ METHODS ------------------------------
134 isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) {
135 return comparePassword(password, this.password, callback)
138 toFormatedJSON = function (this: UserInstance) {
141 username: this.username,
143 displayNSFW: this.displayNSFW,
145 createdAt: this.createdAt
149 isAdmin = function (this: UserInstance) {
150 return this.role === USER_ROLES.ADMIN
153 // ------------------------------ STATICS ------------------------------
155 function associate (models) {
156 User.hasOne(models.Author, {
157 foreignKey: 'userId',
161 User.hasMany(models.OAuthToken, {
162 foreignKey: 'userId',
167 countTotal = function (callback: UserMethods.CountTotalCallback) {
168 return this.count().asCallback(callback)
171 getByUsername = function (username: string) {
178 return User.findOne(query)
181 list = function (callback: UserMethods.ListCallback) {
182 return User.find().asCallback(callback)
185 listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) {
189 order: [ getSort(sort) ]
192 return User.findAndCountAll(query).asCallback(function (err, result) {
193 if (err) return callback(err)
195 return callback(null, result.rows, result.count)
199 loadById = function (id: number, callback: UserMethods.LoadByIdCallback) {
200 return User.findById(id).asCallback(callback)
203 loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) {
210 return User.findOne(query).asCallback(callback)
213 loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) {
216 $or: [ { username }, { email } ]
220 return User.findOne(query).asCallback(callback)