[refactor] remove username field and let instance_name be the only AP Actor

This commit is contained in:
les
2019-12-04 00:50:15 +01:00
parent e84d7f3bd1
commit 3116e776a0
23 changed files with 159 additions and 201 deletions

View File

@@ -1,20 +1,8 @@
'use strict'
const bcrypt = require('bcryptjs')
const crypto = require('crypto')
const util = require('util')
const debug = require('debug')('model:user')
const generateKeyPair = util.promisify(crypto.generateKeyPair)
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define('user', {
username: {
type: DataTypes.STRING,
unique: { msg: 'error.nick_taken' },
index: true,
allowNull: false
},
display_name: DataTypes.STRING,
const User = sequelize.define('user', {
settings: {
type: DataTypes.JSON,
defaultValue: '{}'
@@ -29,53 +17,33 @@ module.exports = (sequelize, DataTypes) => {
password: DataTypes.STRING,
recover_code: DataTypes.STRING,
is_admin: DataTypes.BOOLEAN,
is_active: DataTypes.BOOLEAN,
rsa: DataTypes.JSON
is_active: DataTypes.BOOLEAN
}, {
scopes: {
withoutPassword: {
attributes: { exclude: ['password', 'recover_code', 'rsa'] }
attributes: { exclude: ['password', 'recover_code'] }
}
}
})
user.associate = function (models) {
// associations can be defined here
user.hasMany(models.event)
user.belongsToMany(models.fed_users, { through: 'user_followers', as: 'followers' })
User.associate = function (models) {
User.hasMany(models.event)
}
user.prototype.comparePassword = async function (pwd) {
User.prototype.comparePassword = async function (pwd) {
if (!this.password) { return false }
const ret = await bcrypt.compare(pwd, this.password)
return ret
}
user.beforeSave(async (user, options) => {
User.beforeSave(async (user, options) => {
if (user.changed('password')) {
debug('Password for %s modified', user.username)
debug('Password for %s modified', user.email)
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(user.password, salt)
user.password = hash
}
})
user.beforeCreate(async (user, options) => {
debug('Create a new user => %s', user.username)
// generate rsa keys
const rsa = await generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
})
user.rsa = rsa
})
return user
return User
}