trilium/routes/api/settings.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const express = require('express');
const router = express.Router();
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
2017-11-03 08:48:02 +08:00
const options = require('../../services/options');
2017-10-16 07:47:05 +08:00
const audit_category = require('../../services/audit_category');
const auth = require('../../services/auth');
const utils = require('../../services/utils');
2017-11-14 12:35:23 +08:00
const build = require('../../services/build');
const packageJson = require('../../package');
const migration = require('../../services/migration');
// options allowed to be updated directly in settings dialog
const ALLOWED_OPTIONS = ['protected_session_timeout', 'history_snapshot_time_interval'];
function addExtraSettings(settings) {
Object.assign(settings, build);
settings['app_version'] = packageJson.version;
settings['db_version'] = migration.APP_DB_VERSION;
}
router.get('/all', auth.checkApiAuth, async (req, res, next) => {
const settings = await sql.getMap("SELECT opt_name, opt_value FROM options");
addExtraSettings(settings);
2017-11-14 12:35:23 +08:00
res.send(settings);
});
router.get('/', auth.checkApiAuth, async (req, res, next) => {
const settings = await sql.getMap("SELECT opt_name, opt_value FROM options WHERE opt_name IN ("
+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);
addExtraSettings(settings);
2017-11-14 12:35:23 +08:00
res.send(settings);
});
router.post('/', async (req, res, next) => {
2017-10-25 10:58:59 +08:00
const body = req.body;
if (ALLOWED_OPTIONS.includes(body['name'])) {
2017-11-03 08:48:02 +08:00
const optionName = await options.getOption(body['name']);
await sql.doInTransaction(async () => {
await sql.addAudit(audit_category.SETTINGS, utils.browserId(req), null, optionName, body['value'], body['name']);
2017-11-03 08:48:02 +08:00
await options.setOption(body['name'], body['value']);
});
res.send({});
}
else {
res.send("not allowed option to set");
}
});
module.exports = router;