gancio-upstream/server/firstrun.js

78 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// check config.js existance
const fs = require('fs')
const consola = require('consola')
module.exports = {
check (config_path) {
return !fs.existsSync(config_path)
},
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)
// do not save admin's password in config file
const admin = { email: config.admin.email, password: config.admin.password }
delete config.admin
config.admin_email = admin.email
config.db.logging = false
consola.info(`Save configuration to ${config_path}`)
try {
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
} catch(e) {
consola.warn(` ⚠️ ${e}. You can specify configuration path using '--config'`)
}
// sync db
const db = require('./api/models')
try {
await db.user.findAll()
consola.warn(` ⚠ Non empty db! Please move your current db elsewhere than retry.`)
return false
} catch (e) { }
consola.info(`Create tables schema`)
await db.sequelize.sync().catch(e => {
consola.error(' ⚠ Error creating tables', e)
return false
})
// create admin user
consola.info('Create admin user', admin)
await db.user.create({
email: admin.email,
password: admin.password,
username: admin.email,
display_name: config.title,
is_admin: true,
is_active: true
})
// set default settings
consola.info('Set default settings')
const settings = require('./api/controller/settings')
await settings.set('allow_registration', true)
await settings.set('allow_anon_event', true)
await settings.set('allow_recurrent_event', true)
await settings.set('recurrent_event_visible', true)
await settings.set('enable_federation', false)
// add default notification
consola.info('Add default notification')
// send confirmed event to mastodon
await db.notification.create({ type: 'mastodon', filters: { is_visible: true } })
await db.notification.create({ type: 'email', filters: { is_visible: true } })
// send every event to admin
await db.notification.create({ type: 'admin_email' })
// close db connection
await db.sequelize.close()
return true
}
}