gancio-upstream/server/firstrun.js

80 lines
2.4 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-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
if (config.smtp_type === 'sendmail') {
config.smtp = {
sendmail: true,
newline: 'unix',
path: config.smtp.path
}
}
delete config.smtp_type
delete config.smtp_need_auth
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}`)
try {
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
2019-10-28 17:33:20 +01:00
} catch (e) {
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
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
}
// create admin user
2020-02-15 15:42:41 +01:00
consola.info(`Create admin with email: ${admin.email}`)
await db.user.create({
2019-07-29 22:40:27 +02:00
email: admin.email,
password: admin.password,
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
// await db.announcement.create({
// visible: true,
// title: 'Welcome to Gancio',
// announcement: 'TODO: HTML First presentation post'
// })
2020-02-20 18:37:10 +01:00
// send confirmed events to mastodon
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-02-20 18:37:10 +01:00
// send anon events to admin
await db.notification.create({ action: 'Create', type: 'admin_email', filters: { is_visible: false } })
2020-02-20 18:37:10 +01:00
// TODO email's notifications
// 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()
return true
}
2019-08-03 15:46:42 +02:00
}