gancio-upstream/server/firstrun.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-06-06 23:54:32 +02:00
// check config.js existance
const fs = require('fs')
const consola = require('consola')
2019-06-06 23:54:32 +02:00
module.exports = {
check (config_path) {
return !fs.existsSync(config_path)
},
2019-06-06 23:54:32 +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-06-26 14:44:21 +02:00
const admin = { email: config.admin.email, password: config.admin.password }
delete config.admin
config.admin_email = admin.email
consola.info(`Save configuration into ${config_path}`)
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
2019-06-06 23:54:32 +02:00
// sync db (TODO, check if there's something in db and ask to backup)
const db = require('./api/models')
try {
consola.info(`Create tables..`)
await db.sequelize.sync({force: true})
} catch(e) {
consola.error('Error creating tables', e)
return -1
2019-06-07 17:02:33 +02:00
}
// create admin user
consola.info('Create admin user')
await db.user.create({
2019-06-26 14:44:21 +02:00
...admin,
is_admin: true,
is_active: true
})
2019-06-26 14:44:21 +02:00
2019-06-25 01:05:38 +02:00
// set default settings
consola.info('Set default settings')
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-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-06-26 14:44:21 +02:00
// send every event to admin
await db.notification.create({ type: 'admin_email' })
}
}