2017-12-12 00:47:46 +03:00
|
|
|
var _ = require('lodash'),
|
2017-04-04 19:07:35 +03:00
|
|
|
hbs = require('./engine'),
|
2017-12-14 05:01:23 +03:00
|
|
|
urlService = require('../url'),
|
|
|
|
config = require('../../config'),
|
|
|
|
common = require('../../lib/common'),
|
2017-03-21 12:03:09 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
|
|
|
activeTheme = require('./active'),
|
|
|
|
themeMiddleware = {};
|
|
|
|
|
|
|
|
// ### Ensure Active Theme
|
|
|
|
// Ensure there's a properly set & mounted active theme before attempting to serve a blog request
|
|
|
|
// If there is no active theme, throw an error
|
|
|
|
// Else, ensure the active theme is mounted
|
|
|
|
themeMiddleware.ensureActiveTheme = function ensureActiveTheme(req, res, next) {
|
2017-05-31 19:42:42 +03:00
|
|
|
// CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme
|
2017-03-21 12:03:09 +03:00
|
|
|
if (!activeTheme.get()) {
|
|
|
|
// This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.InternalServerError({
|
2017-03-21 12:03:09 +03:00
|
|
|
// We use the settingsCache here, because the setting will be set,
|
|
|
|
// even if the theme itself is not usable because it is invalid or missing.
|
2017-12-12 00:47:46 +03:00
|
|
|
message: common.i18n.t('errors.middleware.themehandler.missingTheme', {theme: settingsCache.get('active_theme')})
|
2017-03-21 12:03:09 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-05-31 19:42:42 +03:00
|
|
|
// CASE: bootstrap theme validation failed, we would like to show the errors on the blog [only production]
|
|
|
|
if (activeTheme.get().error && config.get('env') === 'production') {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.InternalServerError({
|
2017-05-31 19:42:42 +03:00
|
|
|
// We use the settingsCache here, because the setting will be set,
|
|
|
|
// even if the theme itself is not usable because it is invalid or missing.
|
2017-12-12 00:47:46 +03:00
|
|
|
message: common.i18n.t('errors.middleware.themehandler.invalidTheme', {theme: settingsCache.get('active_theme')}),
|
2017-05-31 19:42:42 +03:00
|
|
|
errorDetails: activeTheme.get().error.errorDetails
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-03-21 12:03:09 +03:00
|
|
|
// If the active theme has not yet been mounted, mount it into express
|
|
|
|
if (!activeTheme.get().mounted) {
|
|
|
|
activeTheme.get().mount(req.app);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
// ### Update Template Data
|
|
|
|
// Updates handlebars with the contextual data for the current request
|
|
|
|
themeMiddleware.updateTemplateData = function updateTemplateData(req, res, next) {
|
|
|
|
// Static information, same for every request unless the settings change
|
|
|
|
// @TODO: bind this once and then update based on events?
|
|
|
|
var blogData = {
|
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
|
|
|
facebook: settingsCache.get('facebook'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
2017-04-24 20:41:00 +03:00
|
|
|
timezone: settingsCache.get('active_timezone'),
|
2017-03-21 12:03:09 +03:00
|
|
|
navigation: settingsCache.get('navigation'),
|
|
|
|
icon: settingsCache.get('icon'),
|
2017-04-24 20:21:47 +03:00
|
|
|
cover_image: settingsCache.get('cover_image'),
|
2017-03-21 12:03:09 +03:00
|
|
|
logo: settingsCache.get('logo'),
|
|
|
|
amp: settingsCache.get('amp')
|
|
|
|
},
|
|
|
|
labsData = _.cloneDeep(settingsCache.get('labs')),
|
|
|
|
themeData = {};
|
|
|
|
|
2018-11-09 14:42:21 +03:00
|
|
|
/**
|
|
|
|
* TODO: Uses hard-check for members prototype, removed here when added to settings
|
|
|
|
*/
|
|
|
|
if (config.get('enableDeveloperExperiments')) {
|
|
|
|
Object.assign(labsData, {
|
|
|
|
members: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-21 12:03:09 +03:00
|
|
|
if (activeTheme.get()) {
|
|
|
|
themeData.posts_per_page = activeTheme.get().config('posts_per_page');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request-specific information
|
|
|
|
// These things are super dependent on the request, so they need to be in middleware
|
2017-06-19 17:11:53 +03:00
|
|
|
// Serve the blog url without trailing slash
|
2017-12-11 21:14:05 +03:00
|
|
|
blogData.url = urlService.utils.urlFor('home', {secure: req.secure, trailingSlash: false}, true);
|
2017-03-21 12:03:09 +03:00
|
|
|
|
|
|
|
// Pass 'secure' flag to the view engine
|
|
|
|
// so that templates can choose to render https or http 'url', see url utility
|
|
|
|
res.locals.secure = req.secure;
|
|
|
|
|
|
|
|
// @TODO: only do this if something changed?
|
|
|
|
hbs.updateTemplateOptions({
|
|
|
|
data: {
|
|
|
|
blog: blogData,
|
|
|
|
labs: labsData,
|
|
|
|
config: themeData
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = [
|
|
|
|
themeMiddleware.ensureActiveTheme,
|
|
|
|
themeMiddleware.updateTemplateData
|
|
|
|
];
|