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-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-06-21 23:52:18 +02:00
|
|
|
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
|
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-07-03 16:58:24 +02:00
|
|
|
|
2019-06-21 23:52:18 +02:00
|
|
|
try {
|
2019-06-26 16:15:25 +02:00
|
|
|
await db.user.findAll()
|
2019-08-06 01:12:05 +02:00
|
|
|
consola.warn(`⚠️ Non empty db! Please move your current db elsewhere than retry.`)
|
|
|
|
return false
|
2019-09-11 19:12:24 +02:00
|
|
|
} catch (e) { }
|
2019-06-26 16:15:25 +02:00
|
|
|
|
2019-07-03 16:58:24 +02:00
|
|
|
consola.info(`Create tables schema`)
|
2019-06-26 18:37:02 +02:00
|
|
|
await db.sequelize.sync().catch(e => {
|
2019-06-21 23:52:18 +02:00
|
|
|
consola.error('Error creating tables', e)
|
2019-08-06 01:12:05 +02:00
|
|
|
return false
|
2019-06-26 16:15:25 +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,
|
|
|
|
username: admin.email,
|
|
|
|
display_name: config.title,
|
2019-06-21 23:52:18 +02:00
|
|
|
is_admin: true,
|
|
|
|
is_active: true
|
|
|
|
})
|
|
|
|
|
2019-06-25 01:05:38 +02:00
|
|
|
// set default settings
|
|
|
|
consola.info('Set default settings')
|
2019-06-21 23:52:18 +02:00
|
|
|
const settings = require('./api/controller/settings')
|
2019-06-25 01:05:38 +02:00
|
|
|
await settings.set('allow_registration', true)
|
|
|
|
await settings.set('allow_anon_event', true)
|
2019-07-23 16:26:54 +02:00
|
|
|
await settings.set('allow_recurrent_event', true)
|
|
|
|
await settings.set('recurrent_event_visible', true)
|
2019-08-03 15:48:03 +02:00
|
|
|
await settings.set('enable_federation', false)
|
2019-06-21 23:52:18 +02:00
|
|
|
|
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
|
|
|
|
await db.notification.create({ type: 'mastodon', filters: { is_visible: true } })
|
2019-07-05 23:58:47 +02:00
|
|
|
await db.notification.create({ type: 'email', filters: { is_visible: true } })
|
2019-06-26 14:44:21 +02:00
|
|
|
|
|
|
|
// send every event to admin
|
|
|
|
await db.notification.create({ type: 'admin_email' })
|
2019-07-03 16:58:24 +02:00
|
|
|
|
|
|
|
// close db connection
|
|
|
|
await db.sequelize.close()
|
2019-06-21 23:52:18 +02:00
|
|
|
}
|
2019-08-03 15:46:42 +02:00
|
|
|
}
|