}
}
},
+ email: {
+ type: DataTypes.STRING(400),
+ allowNull: false,
+ validate: {
+ isEmail: true
+ }
+ },
+ displayNSFW: {
+ type: DataTypes.BOOLEAN,
+ allowNull: false,
+ defaultValue: false,
+ validate: {
+ nsfwValid: function (value) {
+ const res = customUsersValidators.isUserDisplayNSFWValid(value)
+ if (res === false) throw new Error('Display NSFW is not valid.')
+ }
+ }
+ },
role: {
type: DataTypes.ENUM(values(constants.USER_ROLES)),
allowNull: false
}
},
{
+ indexes: [
+ {
+ fields: [ 'username' ],
+ unique: true
+ },
+ {
+ fields: [ 'email' ],
+ unique: true
+ }
+ ],
classMethods: {
associate,
list,
listForApi,
loadById,
- loadByUsername
+ loadByUsername,
+ loadByUsernameOrEmail
},
instanceMethods: {
isPasswordMatch,
return {
id: this.id,
username: this.username,
+ email: this.email,
+ displayNSFW: this.displayNSFW,
role: this.role,
createdAt: this.createdAt
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
+ this.hasOne(models.Author, {
+ foreignKey: 'userId',
+ onDelete: 'cascade'
+ })
+
this.hasMany(models.OAuthToken, {
foreignKey: 'userId',
onDelete: 'cascade'
return this.findOne(query).asCallback(callback)
}
+
+function loadByUsernameOrEmail (username, email, callback) {
+ const query = {
+ where: {
+ $or: [ { username }, { email } ]
+ }
+ }
+
+ return this.findOne(query).asCallback(callback)
+}