Configuration split to lists, send configurations and server config.

This is before testing.
This commit is contained in:
Tomas Bures 2018-04-22 17:33:43 +02:00
parent 4fce4b6f81
commit c12efeb97f
40 changed files with 819 additions and 311 deletions

32
routes/index.js Normal file
View file

@ -0,0 +1,32 @@
'use strict';
const passport = require('../lib/passport');
const _ = require('../lib/translate')._;
const clientHelpers = require('../lib/client-helpers');
const routerFactory = require('../lib/router-async');
function getRouter(trusted) {
const router = routerFactory.create();
if (trusted) {
router.getAsync('/*', passport.csrfProtection, async (req, res) => {
const mailtrainConfig = await clientHelpers.getAnonymousConfig(req.context, trusted);
if (req.user) {
Object.assign(mailtrainConfig, await clientHelpers.getAuthenticatedConfig(req.context));
}
res.render('root', {
reactCsrfToken: req.csrfToken(),
mailtrainConfig: JSON.stringify(mailtrainConfig)
});
});
}
return router;
}
module.exports = {
getRouter
};

View file

@ -1,7 +1,7 @@
'use strict';
const config = require('config');
const router = require('../lib/router-async').create();
const routerFactory = require('../lib/router-async');
const passport = require('../lib/passport');
const clientHelpers = require('../lib/client-helpers');
const gm = require('gm').subClass({
@ -110,73 +110,84 @@ function sanitizeSize(val, min, max, defaultVal, allowNull) {
return val;
}
router.getAsync('/img/:type/:fileId', passport.loggedIn, async (req, res) => {
const method = req.query.method;
const params = req.query.params;
let [width, height] = params.split(',');
let image;
if (method === 'placeholder') {
width = sanitizeSize(width, 1, 2048, 600, false);
height = sanitizeSize(height, 1, 2048, 300, false);
image = await placeholderImage(width, height);
} else {
width = sanitizeSize(width, 1, 2048, 600, false);
height = sanitizeSize(height, 1, 2048, 300, true);
// TODO - validate that one has the rights to read this ???
image = await resizedImage(req.query.src, method, width, height);
function getRouter(trusted) {
const router = routerFactory.create();
if (!trusted) {
router.getAsync('/img/:type/:fileId', passport.loggedIn, async (req, res) => {
const method = req.query.method;
const params = req.query.params;
let [width, height] = params.split(',');
let image;
if (method === 'placeholder') {
width = sanitizeSize(width, 1, 2048, 600, false);
height = sanitizeSize(height, 1, 2048, 300, false);
image = await placeholderImage(width, height);
} else {
width = sanitizeSize(width, 1, 2048, 600, false);
height = sanitizeSize(height, 1, 2048, 300, true);
// TODO - validate that one has the rights to read this ???
image = await resizedImage(req.query.src, method, width, height);
}
res.set('Content-Type', 'image/' + image.format);
image.stream.pipe(res);
});
fileHelpers.installUploadHandler(router, '/upload/:type/:entityId', true);
router.getAsync('/upload/:type/:fileId', passport.loggedIn, async (req, res) => {
const entries = await files.list(req.context, req.params.type, req.params.fileId);
const filesOut = [];
for (const entry of entries) {
filesOut.push({
name: entry.originalname,
url: `/files/${req.params.type}/${req.params.fileId}/${entry.filename}`,
size: entry.size,
thumbnailUrl: `/files/${req.params.type}/${req.params.fileId}/${entry.filename}` // TODO - use smaller thumbnails
})
}
res.json({
files: filesOut
});
});
router.getAsync('/editor', passport.csrfProtection, async (req, res) => {
const resourceType = req.query.type;
const resourceId = req.query.id;
const mailtrainConfig = await clientHelpers.getAnonymousConfig(req.context, trusted);
let languageStrings = null;
if (config.language && config.language !== 'en') {
const lang = config.language.split('_')[0];
try {
const file = path.join(__dirname, '..', 'client', 'public', 'mosaico', 'lang', 'mosaico-' + lang + '.json');
languageStrings = await fsReadFile(file, 'utf8');
} catch (err) {
}
}
res.render('mosaico/root', {
layout: 'mosaico/layout',
editorConfig: config.mosaico,
languageStrings: languageStrings,
reactCsrfToken: req.csrfToken(),
mailtrainConfig: JSON.stringify(mailtrainConfig)
});
});
}
res.set('Content-Type', 'image/' + image.format);
image.stream.pipe(res);
});
return router;
}
fileHelpers.installUploadHandler(router, '/upload/:type/:entityId', true);
router.getAsync('/upload/:type/:fileId', passport.loggedIn, async (req, res) => {
const entries = await files.list(req.context, req.params.type, req.params.fileId);
const filesOut = [];
for (const entry of entries) {
filesOut.push({
name: entry.originalname,
url: `/files/${req.params.type}/${req.params.fileId}/${entry.filename}`,
size: entry.size,
thumbnailUrl: `/files/${req.params.type}/${req.params.fileId}/${entry.filename}` // TODO - use smaller thumbnails
})
}
res.json({
files: filesOut
});
});
router.getAsync('/editor', passport.csrfProtection, async (req, res) => {
const resourceType = req.query.type;
const resourceId = req.query.id;
const mailtrainConfig = await clientHelpers.getAnonymousConfig(req.context);
let languageStrings = null;
if (config.language && config.language !== 'en') {
const lang = config.language.split('_')[0];
try {
const file = path.join(__dirname, '..', 'client', 'public', 'mosaico', 'lang', 'mosaico-' + lang + '.json');
languageStrings = await fsReadFile(file, 'utf8');
} catch (err) {
}
}
res.render('mosaico/root', {
layout: 'mosaico/layout',
editorConfig: config.mosaico,
languageStrings: languageStrings,
reactCsrfToken: req.csrfToken(),
mailtrainConfig: JSON.stringify(mailtrainConfig)
});
});
module.exports = router;
module.exports = {
getRouter
};

22
routes/rest/settings.js Normal file
View file

@ -0,0 +1,22 @@
'use strict';
const passport = require('../../lib/passport');
const settings = require('../../models/settings');
const router = require('../../lib/router-async').create();
router.getAsync('/settings', passport.loggedIn, async (req, res) => {
const configItems = await settings.get(req.context);
configItems.hash = settings.hash(configItems);
return res.json(configItems);
});
router.putAsync('/settings', passport.loggedIn, passport.csrfProtection, async (req, res) => {
const configItems = req.body;
await settings.set(req.context, configItems);
return res.json();
});
module.exports = router;

View file

@ -1,22 +0,0 @@
'use strict';
const passport = require('../lib/passport');
const _ = require('../lib/translate')._;
const clientHelpers = require('../lib/client-helpers');
const router = require('../lib/router-async').create();
router.getAsync('/*', passport.csrfProtection, async (req, res) => {
const mailtrainConfig = await clientHelpers.getAnonymousConfig(req.context);
if (req.user) {
Object.assign(mailtrainConfig, await clientHelpers.getAuthenticatedConfig(req.context));
}
res.render('root', {
reactCsrfToken: req.csrfToken(),
mailtrainConfig: JSON.stringify(mailtrainConfig)
});
});
module.exports = router;

View file

@ -205,10 +205,9 @@ async function _renderSubscribe(req, res, list, subscription) {
data.customFields = await fields.forHbs(contextHelpers.getAdminContext(), list.id, subscription);
data.useEditor = true;
const configItems = await settings.get(['pgpPrivateKey', 'defaultAddress', 'defaultPostaddress']);
const configItems = await settings.get(['pgpPrivateKey', 'defaultAddress']);
data.hasPubkey = !!configItems.pgpPrivateKey;
data.defaultAddress = configItems.defaultAddress;
data.defaultPostaddress = configItems.defaultPostaddress;
data.template = {
template: 'subscription/web-subscribe.mjml.hbs',
@ -407,10 +406,9 @@ router.getAsync('/:lcid/manage/:ucid', passport.csrfProtection, async (req, res)
data.useEditor = true;
const configItems = await settings.get(['pgpPrivateKey', 'defaultAddress', 'defaultPostaddress']);
const configItems = await settings.get(['pgpPrivateKey', 'defaultAddress']);
data.hasPubkey = !!configItems.pgpPrivateKey;
data.defaultAddress = configItems.defaultAddress;
data.defaultPostaddress = configItems.defaultPostaddress;
data.template = {
template: 'subscription/web-manage.mjml.hbs',
@ -454,7 +452,7 @@ router.getAsync('/:lcid/manage-address/:ucid', passport.csrfProtection, async (r
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
}
const configItems = await settings.get(['defaultAddress', 'defaultPostaddress']);
const configItems = await settings.get(['defaultAddress']);
const data = {};
data.email = subscription.email;
@ -463,7 +461,6 @@ router.getAsync('/:lcid/manage-address/:ucid', passport.csrfProtection, async (r
data.title = list.name;
data.csrfToken = req.csrfToken();
data.defaultAddress = configItems.defaultAddress;
data.defaultPostaddress = configItems.defaultPostaddress;
data.template = {
template: 'subscription/web-manage-address.mjml.hbs',
@ -538,7 +535,7 @@ router.postAsync('/:lcid/manage-address', passport.parseForm, passport.csrfProte
router.getAsync('/:lcid/unsubscribe/:ucid', passport.csrfProtection, async (req, res) => {
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
const configItems = await settings.get(['defaultAddress', 'defaultPostaddress']);
const configItems = await settings.get(['defaultAddress']);
const autoUnsubscribe = req.query.auto === 'yes';
@ -563,7 +560,6 @@ router.getAsync('/:lcid/unsubscribe/:ucid', passport.csrfProtection, async (req,
data.csrfToken = req.csrfToken();
data.campaign = req.query.c;
data.defaultAddress = configItems.defaultAddress;
data.defaultPostaddress = configItems.defaultPostaddress;
data.template = {
template: 'subscription/web-unsubscribe.mjml.hbs',
@ -702,14 +698,13 @@ router.postAsync('/publickey', passport.parseForm, async (req, res) => {
async function webNotice(type, req, res) {
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
const configItems = await settings.get(['defaultHomepage', 'serviceUrl', 'defaultAddress', 'defaultPostaddress', 'adminEmail']);
const configItems = await settings.get(['defaultHomepage', 'serviceUrl', 'defaultAddress', 'adminEmail']);
const data = {
title: list.name,
homepage: configItems.defaultHomepage || configItems.serviceUrl,
defaultAddress: configItems.defaultAddress,
defaultPostaddress: configItems.defaultPostaddress,
contactAddress: configItems.defaultAddress,
template: {
template: 'subscription/web-' + type + '-notice.mjml.hbs',