gancio-upstream/server/api/controller/settings.js

173 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-07-24 21:26:56 +02:00
const path = require('path')
2021-09-30 11:13:44 +02:00
const URL = require('url')
2019-07-24 21:26:56 +02:00
const fs = require('fs')
const crypto = require('crypto')
2021-09-30 11:13:44 +02:00
const { promisify } = require('util')
2020-07-07 21:58:52 +02:00
const sharp = require('sharp')
2021-09-30 11:13:44 +02:00
const config = require('../../config')
const pkg = require('../../../package.json')
const generateKeyPair = promisify(crypto.generateKeyPair)
2021-03-05 14:17:10 +01:00
const log = require('../../log')
2021-09-30 11:13:44 +02:00
let defaultHostname
try {
defaultHostname = new URL.URL(config.baseurl).hostname
} catch (e) {}
const defaultSettings = {
2021-10-21 12:17:41 +02:00
title: config.title || 'Gancio',
description: config.description || 'A shared agenda for local communities',
2021-09-30 11:13:44 +02:00
baseurl: config.baseurl || '',
hostname: defaultHostname,
instance_timezone: 'Europe/Rome',
2020-02-20 18:37:10 +01:00
instance_locale: 'en',
2021-09-30 11:13:44 +02:00
instance_name: 'gancio',
2020-03-18 10:11:24 +01:00
instance_place: '',
allow_registration: true,
allow_anon_event: true,
allow_recurrent_event: false,
recurrent_event_visible: false,
enable_federation: true,
enable_resources: false,
2020-03-14 18:45:20 +01:00
hide_boosts: true,
2020-03-18 10:11:24 +01:00
enable_trusted_instances: true,
trusted_instances: [],
2021-10-21 16:18:41 +02:00
'theme.is_dark': true,
'theme.primary': '#FF4500',
footerLinks: [
2021-05-18 20:37:16 +02:00
{ href: '/', label: 'home' },
{ href: '/about', label: 'about' }
2021-09-30 11:13:44 +02:00
],
2021-10-18 15:46:38 +02:00
admin_email: config.admin_email || '',
smtp: config.smtp || false
}
2019-10-11 18:34:14 +02:00
/**
* Settings controller: store instance settings
*/
2019-04-03 00:25:12 +02:00
const settingsController = {
2019-06-25 01:05:38 +02:00
settings: { initialized: false },
2019-07-26 23:51:32 +02:00
user_locale: {},
2019-06-25 01:05:38 +02:00
secretSettings: {},
2019-06-06 23:54:32 +02:00
async load () {
2021-09-27 10:42:17 +02:00
if (config.firstrun) {
settingsController.settings = defaultSettings
return
}
const Setting = require('../models/setting')
2019-06-25 01:05:38 +02:00
if (!settingsController.settings.initialized) {
2019-07-26 23:51:32 +02:00
// initialize instance settings from db
// note that this is done only once when the server starts
// and not for each request (it's a kind of cache)!
const settings = await Setting.findAll()
2019-06-25 01:05:38 +02:00
settingsController.settings.initialized = true
settingsController.settings = defaultSettings
2019-11-06 11:31:56 +01:00
settings.forEach(s => {
2019-06-25 01:05:38 +02:00
if (s.is_secret) {
settingsController.secretSettings[s.key] = s.value
} else {
settingsController.settings[s.key] = s.value
}
})
2019-07-26 23:51:32 +02:00
// add pub/priv instance key if needed
if (!settingsController.settings.publicKey) {
2021-04-28 12:44:26 +02:00
log.info('Instance priv/pub key not found, generating....')
const { publicKey, privateKey } = await generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
})
await settingsController.set('publicKey', publicKey)
await settingsController.set('privateKey', privateKey, true)
2019-09-26 22:46:04 +02:00
}
// initialize user_locale
2019-07-26 23:51:32 +02:00
if (config.user_locale && fs.existsSync(path.resolve(config.user_locale))) {
const user_locale = fs.readdirSync(path.resolve(config.user_locale))
2019-11-06 11:31:56 +01:00
user_locale.forEach(async f => {
2021-09-27 10:42:17 +02:00
log.info(`Loading user locale ${f}`)
2019-07-26 23:51:32 +02:00
const locale = path.basename(f, '.js')
settingsController.user_locale[locale] =
2019-09-19 16:23:46 +02:00
(await require(path.resolve(config.user_locale, f))).default
2019-07-26 23:51:32 +02:00
})
}
}
},
2019-11-06 11:31:56 +01:00
async set (key, value, is_secret = false) {
2021-09-27 10:42:17 +02:00
const Setting = require('../models/setting')
2021-05-20 11:19:26 +02:00
log.info(`SET ${key} ${is_secret ? '*****' : value}`)
try {
2020-07-05 23:50:10 +02:00
const [setting, created] = await Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
2019-04-03 00:25:12 +02:00
})
2020-07-06 00:52:32 +02:00
if (!created) { setting.update({ value, is_secret }) }
2019-11-06 11:31:56 +01:00
settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value
return true
2019-11-06 11:31:56 +01:00
} catch (e) {
2021-07-08 20:41:56 +02:00
log.error('[SETTING SET]', e)
return false
}
2019-04-03 00:25:12 +02:00
},
2019-11-06 11:31:56 +01:00
async setRequest (req, res) {
const { key, value, is_secret } = req.body
const ret = await settingsController.set(key, value, is_secret)
2019-11-06 11:31:56 +01:00
if (ret) { res.sendStatus(200) } else { res.sendStatus(400) }
},
2021-10-18 15:46:38 +02:00
async testSMTP (req, res) {
const smtp = req.body
await settingsController.set('smtp', smtp.smtp)
const mail = require('../mail')
try {
2021-10-19 16:38:10 +02:00
await mail._send(settingsController.settings.admin_email, 'test', null, 'en')
2021-10-18 15:46:38 +02:00
return res.sendStatus(200)
} catch (e) {
console.error(e)
return res.status(400).send(String(e))
}
},
2020-07-07 21:58:52 +02:00
setLogo (req, res) {
2020-02-01 22:39:31 +01:00
if (!req.file) {
2021-04-28 12:44:26 +02:00
settingsController.set('logo', false)
return res.status(200)
2020-02-01 22:39:31 +01:00
}
2020-07-05 23:53:37 +02:00
2020-07-28 12:24:39 +02:00
const uploadedPath = path.join(req.file.destination, req.file.filename)
const baseImgPath = path.resolve(config.upload_path, 'logo')
2020-07-07 21:58:52 +02:00
// convert and resize to png
return sharp(uploadedPath)
2020-07-07 21:58:52 +02:00
.resize(400)
.png({ quality: 90 })
.toFile(baseImgPath + '.png', (err, info) => {
2021-03-05 14:17:10 +01:00
if (err) {
log.error('[LOGO] ' + err)
2021-03-05 14:17:10 +01:00
}
2020-07-28 12:24:39 +02:00
settingsController.set('logo', baseImgPath)
2020-07-07 21:58:52 +02:00
res.sendStatus(200)
})
2020-01-15 23:51:09 +01:00
},
2019-11-06 11:31:56 +01:00
getAllRequest (req, res) {
2019-06-25 01:05:38 +02:00
// get public settings and public configuration
2021-09-27 10:42:17 +02:00
res.json({ ...settingsController.settings, version: pkg.version })
2019-11-06 11:31:56 +01:00
}
2019-04-03 00:25:12 +02:00
}
module.exports = settingsController