2019-09-26 22:46:04 +02:00
|
|
|
const { setting: Setting, user: User } = require('../models')
|
2019-06-21 23:52:18 +02:00
|
|
|
const config = require('config')
|
2019-07-26 23:51:32 +02:00
|
|
|
const consola = require('consola')
|
2019-07-24 21:26:56 +02:00
|
|
|
const path = require('path')
|
|
|
|
|
const fs = require('fs')
|
2019-11-06 11:31:56 +01:00
|
|
|
const pkg = require('../../../package.json')
|
|
|
|
|
const debug = require('debug')('settings')
|
2019-10-11 18:34:14 +02:00
|
|
|
/**
|
|
|
|
|
* Settings controller: store instance settings
|
|
|
|
|
* Current supported settings:
|
2019-11-06 11:31:56 +01:00
|
|
|
*
|
2019-10-11 18:34:14 +02:00
|
|
|
* Usage:
|
|
|
|
|
* backend/fediverse/api:
|
2019-11-06 11:31:56 +01:00
|
|
|
*
|
2019-10-11 18:34:14 +02:00
|
|
|
* frontend:
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
2019-06-25 01:05:38 +02:00
|
|
|
async initialize () {
|
|
|
|
|
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)!
|
2019-06-21 23:52:18 +02:00
|
|
|
const settings = await Setting.findAll()
|
2019-06-25 01:05:38 +02:00
|
|
|
settingsController.settings.initialized = true
|
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
|
|
|
|
2019-09-26 22:46:04 +02:00
|
|
|
// set fediverse admin actor
|
2019-11-06 11:31:56 +01:00
|
|
|
const fedi_admin = await User.findOne({ where: { email: config.admin_email } })
|
2019-09-26 22:46:04 +02:00
|
|
|
if (fedi_admin) {
|
2019-11-06 11:31:56 +01:00
|
|
|
settingsController.settings.fedi_admin = fedi_admin.username
|
|
|
|
|
} else {
|
|
|
|
|
debug('Federation disabled! An admin with %s as email cannot be found', config.admin_email)
|
|
|
|
|
settingsController.settings.enable_federation = false
|
2019-09-26 22:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-19 16:23:46 +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 => {
|
2019-07-26 23:51:32 +02:00
|
|
|
consola.info(`Loading user locale ${f}`)
|
|
|
|
|
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-06-21 23:52:18 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-11-06 11:31:56 +01:00
|
|
|
async set (key, value, is_secret = false) {
|
2019-06-21 23:52:18 +02:00
|
|
|
try {
|
|
|
|
|
await Setting.findOrCreate({
|
|
|
|
|
where: { key },
|
|
|
|
|
defaults: { value, is_secret }
|
2019-07-13 01:02:11 +02:00
|
|
|
}).spread((setting, created) => {
|
2019-11-06 11:31:56 +01:00
|
|
|
if (!created) { return setting.update({ value, is_secret }) }
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
2019-11-06 11:31:56 +01:00
|
|
|
settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value
|
2019-06-21 23:52:18 +02:00
|
|
|
return true
|
2019-11-06 11:31:56 +01:00
|
|
|
} catch (e) {
|
2019-06-21 23:52:18 +02:00
|
|
|
console.error(e)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
|
2019-11-06 11:31:56 +01:00
|
|
|
getUserLocale (req, res) {
|
2019-07-24 11:49:02 +02:00
|
|
|
// load user locale specified in configuration
|
2019-07-26 23:51:32 +02:00
|
|
|
res.json(settingsController.user_locale)
|
2019-07-24 11:49:02 +02:00
|
|
|
},
|
|
|
|
|
|
2019-11-06 11:31:56 +01:00
|
|
|
async setRequest (req, res) {
|
2019-06-21 23:52:18 +02:00
|
|
|
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) }
|
2019-06-21 23:52:18 +02: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
|
|
|
|
|
const settings = {
|
|
|
|
|
...settingsController.settings,
|
2019-07-08 00:06:56 +02:00
|
|
|
baseurl: config.baseurl,
|
|
|
|
|
title: config.title,
|
2019-08-31 22:25:00 +02:00
|
|
|
description: config.description,
|
2019-11-06 11:31:56 +01:00
|
|
|
version: pkg.version
|
2019-06-25 01:05:38 +02:00
|
|
|
}
|
|
|
|
|
res.json(settings)
|
2019-11-06 11:31:56 +01:00
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:15:23 +02:00
|
|
|
settingsController.initialize()
|
2019-04-03 00:25:12 +02:00
|
|
|
module.exports = settingsController
|