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'
20 } from './user-interface'
22 let User: Sequelize.Model<UserInstance, UserAttributes>
23 let isPasswordMatch: UserMethods.IsPasswordMatch
24 let toFormatedJSON: UserMethods.ToFormatedJSON
25 let isAdmin: UserMethods.IsAdmin
26 let countTotal: UserMethods.CountTotal
27 let getByUsername: UserMethods.GetByUsername
28 let list: UserMethods.List
29 let listForApi: UserMethods.ListForApi
30 let loadById: UserMethods.LoadById
31 let loadByUsername: UserMethods.LoadByUsername
32 let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
34 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
35 User = sequelize.define<UserInstance, UserAttributes>('User',
38 type: DataTypes.STRING,
41 passwordValid: function (value) {
42 const res = isUserPasswordValid(value)
43 if (res === false) throw new Error('Password not valid.')
48 type: DataTypes.STRING,
51 usernameValid: function (value) {
52 const res = isUserUsernameValid(value)
53 if (res === false) throw new Error('Username not valid.')
58 type: DataTypes.STRING(400),
65 type: DataTypes.BOOLEAN,
69 nsfwValid: function (value) {
70 const res = isUserDisplayNSFWValid(value)
71 if (res === false) throw new Error('Display NSFW is not valid.')
76 type: DataTypes.ENUM(values(USER_ROLES)),
83 fields: [ 'username' ],
92 beforeCreate: beforeCreateOrUpdate,
93 beforeUpdate: beforeCreateOrUpdate
98 const classMethods = [
107 loadByUsernameOrEmail
109 const instanceMethods = [
114 addMethodsToModel(User, classMethods, instanceMethods)
119 function beforeCreateOrUpdate (user: UserInstance) {
120 return cryptPassword(user.password).then(hash => {
126 // ------------------------------ METHODS ------------------------------
128 isPasswordMatch = function (this: UserInstance, password: string) {
129 return comparePassword(password, this.password)
132 toFormatedJSON = function (this: UserInstance) {
135 username: this.username,
137 displayNSFW: this.displayNSFW,
139 createdAt: this.createdAt
143 isAdmin = function (this: UserInstance) {
144 return this.role === USER_ROLES.ADMIN
147 // ------------------------------ STATICS ------------------------------
149 function associate (models) {
150 User.hasOne(models.Author, {
151 foreignKey: 'userId',
155 User.hasMany(models.OAuthToken, {
156 foreignKey: 'userId',
161 countTotal = function () {
165 getByUsername = function (username: string) {
172 return User.findOne(query)
176 return User.findAll()
179 listForApi = function (start: number, count: number, sort: string) {
183 order: [ getSort(sort) ]
186 return User.findAndCountAll(query).then(({ rows, count }) => {
194 loadById = function (id: number) {
195 return User.findById(id)
198 loadByUsername = function (username: string) {
205 return User.findOne(query)
208 loadByUsernameOrEmail = function (username: string, email: string) {
211 $or: [ { username }, { email } ]
215 return User.findOne(query)