2019-06-06 23:54:32 +02:00
|
|
|
// check config.js existance
|
|
|
|
const fs = require('fs')
|
2019-06-21 23:52:18 +02:00
|
|
|
const consola = require('consola')
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-06-21 23:52:18 +02:00
|
|
|
module.exports = {
|
|
|
|
check (config_path) {
|
|
|
|
return !fs.existsSync(config_path)
|
|
|
|
},
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-06-21 23:52:18 +02:00
|
|
|
async setup (config, config_path) {
|
|
|
|
// generate a random salt
|
|
|
|
consola.info('Generate random salt')
|
|
|
|
config.secret = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-07-03 16:58:24 +02:00
|
|
|
// do not save admin's password in config file
|
2019-06-26 14:44:21 +02:00
|
|
|
const admin = { email: config.admin.email, password: config.admin.password }
|
|
|
|
delete config.admin
|
2019-07-03 16:58:24 +02:00
|
|
|
|
2019-11-13 11:14:48 +01:00
|
|
|
config.smtp.secure = true
|
2019-06-26 14:44:21 +02:00
|
|
|
config.admin_email = admin.email
|
2019-07-03 16:58:24 +02:00
|
|
|
config.db.logging = false
|
|
|
|
consola.info(`Save configuration to ${config_path}`)
|
2019-09-25 19:07:15 +02:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
|
2019-10-28 17:33:20 +01:00
|
|
|
} catch (e) {
|
2019-09-25 19:07:15 +02:00
|
|
|
consola.warn(` ⚠️ ${e}. You can specify configuration path using '--config'`)
|
|
|
|
}
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-06-26 16:15:25 +02:00
|
|
|
// sync db
|
2019-06-21 23:52:18 +02:00
|
|
|
const db = require('./api/models')
|
2019-10-25 18:43:49 +02:00
|
|
|
const users = await db.user.findAll()
|
|
|
|
if (users.length) {
|
2020-01-15 23:51:09 +01:00
|
|
|
consola.warn(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
|
2019-08-06 01:12:05 +02:00
|
|
|
return false
|
2019-10-25 18:43:49 +02:00
|
|
|
}
|
2019-06-21 23:52:18 +02:00
|
|
|
|
|
|
|
// create admin user
|
2019-07-03 16:58:24 +02:00
|
|
|
consola.info('Create admin user', admin)
|
2019-06-21 23:52:18 +02:00
|
|
|
await db.user.create({
|
2019-07-29 22:40:27 +02:00
|
|
|
email: admin.email,
|
|
|
|
password: admin.password,
|
2019-06-21 23:52:18 +02:00
|
|
|
is_admin: true,
|
|
|
|
is_active: true
|
|
|
|
})
|
|
|
|
|
2019-06-25 01:05:38 +02:00
|
|
|
// add default notification
|
|
|
|
consola.info('Add default notification')
|
2019-06-26 14:44:21 +02:00
|
|
|
|
2019-06-25 01:05:38 +02:00
|
|
|
// send confirmed event to mastodon
|
2019-10-02 20:57:21 +02:00
|
|
|
await db.notification.create({ action: 'Create', type: 'ap', filters: { is_visible: true } })
|
|
|
|
await db.notification.create({ action: 'Update', type: 'ap', filters: { is_visible: true } })
|
|
|
|
await db.notification.create({ action: 'Delete', type: 'ap', filters: { is_visible: true } })
|
2019-06-26 14:44:21 +02:00
|
|
|
|
2020-01-15 23:51:09 +01:00
|
|
|
// send anon event to administrator
|
2019-10-02 20:57:21 +02:00
|
|
|
await db.notification.create({ action: 'Create', type: 'admin_email', filters: { is_visible: false } })
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// await db.notification.create({ action: 'Create', type: 'email', filters: { is_visible: true } })
|
2019-07-03 16:58:24 +02:00
|
|
|
|
|
|
|
// close db connection
|
|
|
|
await db.sequelize.close()
|
2019-09-25 19:07:15 +02:00
|
|
|
|
|
|
|
return true
|
2019-06-21 23:52:18 +02:00
|
|
|
}
|
2019-08-03 15:46:42 +02:00
|
|
|
}
|