refactor: making rendering of header and footer async functions

* refactor: make middleware.admin.renderHeader async

* refactor: making rendering of header and footer async functions

* fix: use app.renderAsync instead of promifying it
v1.18.x
Julian Lam 5 years ago committed by GitHub
parent f6ad9605c6
commit 023942da7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ var semver = require('semver');
var user = require('../user'); var user = require('../user');
var meta = require('../meta'); var meta = require('../meta');
var plugins = require('../plugins'); var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var versions = require('../admin/versions'); var versions = require('../admin/versions');
var controllers = { var controllers = {
@ -37,93 +38,67 @@ module.exports = function (middleware) {
], next); ], next);
}; };
middleware.admin.renderHeader = function (req, res, data, next) { middleware.admin.renderHeader = async (req, res, data) => {
var custom_header = { var custom_header = {
plugins: [], plugins: [],
authentication: [], authentication: [],
}; };
res.locals.config = res.locals.config || {}; res.locals.config = res.locals.config || {};
async.waterfall([
function (next) { const results = await utils.promiseParallel({
async.parallel({ userData: user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed']),
userData: function (next) { scripts: getAdminScripts(),
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], next); custom_header: plugins.fireHook('filter:admin.header.build', custom_header),
}, configs: meta.configs.list(),
scripts: function (next) { latestVersion: versions.getLatestVersion(),
getAdminScripts(next); });
},
custom_header: function (next) { var userData = results.userData;
plugins.fireHook('filter:admin.header.build', custom_header, next); userData.uid = req.uid;
}, userData['email:confirmed'] = userData['email:confirmed'] === 1;
configs: function (next) {
meta.configs.list(next); var acpPath = req.path.slice(1).split('/');
}, acpPath.forEach(function (path, i) {
latestVersion: function (next) { acpPath[i] = path.charAt(0).toUpperCase() + path.slice(1);
versions.getLatestVersion(function (err, result) { });
if (err) { acpPath = acpPath.join(' > ');
winston.error('[acp] Failed to fetch latest version', err);
} var version = nconf.get('version');
next(null, err ? null : result); res.locals.config.userLang = res.locals.config.acpLang || res.locals.config.userLang;
}); var templateValues = {
}, config: res.locals.config,
}, next); configJSON: jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }),
}, relative_path: res.locals.config.relative_path,
function (results, next) { adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)),
var userData = results.userData; user: userData,
userData.uid = req.uid; userJSON: jsesc(JSON.stringify(userData), { isScriptContext: true }),
userData['email:confirmed'] = userData['email:confirmed'] === 1; plugins: results.custom_header.plugins,
authentication: results.custom_header.authentication,
var acpPath = req.path.slice(1).split('/'); scripts: results.scripts,
acpPath.forEach(function (path, i) { 'cache-buster': meta.config['cache-buster'] || '',
acpPath[i] = path.charAt(0).toUpperCase() + path.slice(1); env: !!process.env.NODE_ENV,
}); title: (acpPath || 'Dashboard') + ' | NodeBB Admin Control Panel',
acpPath = acpPath.join(' > '); bodyClass: data.bodyClass,
version: version,
var version = nconf.get('version'); latestVersion: results.latestVersion,
upgradeAvailable: results.latestVersion && semver.gt(results.latestVersion, version),
res.locals.config.userLang = res.locals.config.acpLang || res.locals.config.userLang; };
var templateValues = {
config: res.locals.config, templateValues.template = { name: res.locals.template };
configJSON: jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }), templateValues.template[res.locals.template] = true;
relative_path: res.locals.config.relative_path,
adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)), return await req.app.renderAsync('admin/header', templateValues);
user: userData,
userJSON: jsesc(JSON.stringify(userData), { isScriptContext: true }),
plugins: results.custom_header.plugins,
authentication: results.custom_header.authentication,
scripts: results.scripts,
'cache-buster': meta.config['cache-buster'] || '',
env: !!process.env.NODE_ENV,
title: (acpPath || 'Dashboard') + ' | NodeBB Admin Control Panel',
bodyClass: data.bodyClass,
version: version,
latestVersion: results.latestVersion,
upgradeAvailable: results.latestVersion && semver.gt(results.latestVersion, version),
};
templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;
req.app.render('admin/header', templateValues, next);
},
], next);
}; };
function getAdminScripts(callback) { async function getAdminScripts() {
async.waterfall([ const scripts = await plugins.fireHook('filter:admin.scripts.get', []);
function (next) { return scripts.map(function (script) {
plugins.fireHook('filter:admin.scripts.get', [], next); return { src: script };
}, });
function (scripts, next) {
next(null, scripts.map(function (script) {
return { src: script };
}));
},
], callback);
} }
middleware.admin.renderFooter = function (req, res, data, next) { middleware.admin.renderFooter = async function (req, res, data) {
req.app.render('admin/footer', data, next); return await req.app.renderAsync('admin/footer', data);
}; };
}; };

@ -52,7 +52,7 @@ module.exports = function (middleware) {
], next); ], next);
}; };
middleware.generateHeader = function generateHeader(req, res, data, callback) { async function generateHeader(req, res, data) {
var registrationType = meta.config.registrationType || 'normal'; var registrationType = meta.config.registrationType || 'normal';
res.locals.config = res.locals.config || {}; res.locals.config = res.locals.config || {};
var templateValues = { var templateValues = {
@ -73,207 +73,162 @@ module.exports = function (middleware) {
templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }); templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });
async.waterfall([ const results = await utils.promiseParallel({
function (next) { isAdmin: user.isAdministrator(req.uid),
async.parallel({ isGlobalMod: user.isGlobalModerator(req.uid),
isAdmin: function (next) { isModerator: user.isModeratorOfAnyCategory(req.uid),
user.isAdministrator(req.uid, next); privileges: privileges.global.get(req.uid),
}, user: user.getUserData(req.uid),
isGlobalMod: function (next) { isEmailConfirmSent: (!meta.config.requireEmailConfirmation || req.uid <= 0) ? false : await db.get('uid:' + req.uid + ':confirm:email:sent'),
user.isGlobalModerator(req.uid, next); languageDirection: translator.translate('[[language:dir]]', res.locals.config.userLang),
}, browserTitle: translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title))),
isModerator: function (next) { navigation: navigation.get(req.uid),
user.isModeratorOfAnyCategory(req.uid, next); banned: user.bans.isBanned(req.uid),
}, banReason: user.bans.getReason(req.uid),
privileges: function (next) {
privileges.global.get(req.uid, next); unreadData: topics.getUnreadData({ uid: req.uid }),
}, unreadChatCount: messaging.getUnreadCount(req.uid),
user: function (next) { unreadNotificationCount: user.notifications.getUnreadCount(req.uid),
user.getUserData(req.uid, next); });
},
isEmailConfirmSent: function (next) {
if (!meta.config.requireEmailConfirmation || req.uid <= 0) {
return next(null, false);
}
db.get('uid:' + req.uid + ':confirm:email:sent', next);
},
languageDirection: function (next) {
translator.translate('[[language:dir]]', res.locals.config.userLang, function (translated) {
next(null, translated);
});
},
browserTitle: function (next) {
translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title)), function (translated) {
next(null, translated);
});
},
navigation: async.apply(navigation.get, req.uid),
banned: async.apply(user.bans.isBanned, req.uid),
banReason: async.apply(user.bans.getReason, req.uid),
unreadData: async.apply(topics.getUnreadData, { uid: req.uid }), if (results.banned) {
unreadChatCount: async.apply(messaging.getUnreadCount, req.uid), req.logout();
unreadNotificationCount: async.apply(user.notifications.getUnreadCount, req.uid), return res.redirect('/');
}, next); }
},
function (results, next) {
if (results.banned) {
req.logout();
return res.redirect('/');
}
const unreadData = { const unreadData = {
'': {}, '': {},
new: {}, new: {},
watched: {}, watched: {},
unreplied: {}, unreplied: {},
}; };
results.user.unreadData = unreadData; results.user.unreadData = unreadData;
results.user.isAdmin = results.isAdmin; results.user.isAdmin = results.isAdmin;
results.user.isGlobalMod = results.isGlobalMod; results.user.isGlobalMod = results.isGlobalMod;
results.user.isMod = !!results.isModerator; results.user.isMod = !!results.isModerator;
results.user.privileges = results.privileges; results.user.privileges = results.privileges;
results.user[results.user.status] = true; results.user[results.user.status] = true;
results.user.email = String(results.user.email); results.user.email = String(results.user.email);
results.user['email:confirmed'] = results.user['email:confirmed'] === 1; results.user['email:confirmed'] = results.user['email:confirmed'] === 1;
results.user.isEmailConfirmSent = !!results.isEmailConfirmSent; results.user.isEmailConfirmSent = !!results.isEmailConfirmSent;
templateValues.bootswatchSkin = (parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin : '') || meta.config.bootswatchSkin || ''; templateValues.bootswatchSkin = (parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin : '') || meta.config.bootswatchSkin || '';
templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin'; // TODO remove in v1.12.0+ templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin'; // TODO remove in v1.12.0+
const unreadCounts = results.unreadData.counts; const unreadCounts = results.unreadData.counts;
var unreadCount = { const unreadCount = {
topic: unreadCounts[''] || 0, topic: unreadCounts[''] || 0,
newTopic: unreadCounts.new || 0, newTopic: unreadCounts.new || 0,
watchedTopic: unreadCounts.watched || 0, watchedTopic: unreadCounts.watched || 0,
unrepliedTopic: unreadCounts.unreplied || 0, unrepliedTopic: unreadCounts.unreplied || 0,
chat: results.unreadChatCount || 0, chat: results.unreadChatCount || 0,
notification: results.unreadNotificationCount || 0, notification: results.unreadNotificationCount || 0,
}; };
Object.keys(unreadCount).forEach(function (key) { Object.keys(unreadCount).forEach(function (key) {
if (unreadCount[key] > 99) { if (unreadCount[key] > 99) {
unreadCount[key] = '99+'; unreadCount[key] = '99+';
} }
}); });
const tidsByFilter = results.unreadData.tidsByFilter; const tidsByFilter = results.unreadData.tidsByFilter;
results.navigation = results.navigation.map(function (item) { results.navigation = results.navigation.map(function (item) {
function modifyNavItem(item, route, filter, content) { function modifyNavItem(item, route, filter, content) {
if (item && item.originalRoute === route) { if (item && item.originalRoute === route) {
unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true)); unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true));
item.content = content; item.content = content;
if (unreadCounts[filter] > 0) { if (unreadCounts[filter] > 0) {
item.iconClass += ' unread-count'; item.iconClass += ' unread-count';
}
}
} }
modifyNavItem(item, '/unread', '', unreadCount.topic);
modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic);
modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic);
modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic);
return item;
});
templateValues.browserTitle = results.browserTitle;
templateValues.navigation = results.navigation;
templateValues.unreadCount = unreadCount;
templateValues.isAdmin = results.user.isAdmin;
templateValues.isGlobalMod = results.user.isGlobalMod;
templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
templateValues.canChat = results.canChat && meta.config.disableChat !== 1;
templateValues.user = results.user;
templateValues.userJSON = jsesc(JSON.stringify(results.user), { isScriptContext: true });
templateValues.useCustomCSS = meta.config.useCustomCSS && meta.config.customCSS;
templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : '';
templateValues.useCustomHTML = meta.config.useCustomHTML;
templateValues.customHTML = templateValues.useCustomHTML ? meta.config.customHTML : '';
templateValues.maintenanceHeader = meta.config.maintenanceMode && !results.isAdmin;
templateValues.defaultLang = meta.config.defaultLang || 'en-GB';
templateValues.userLang = res.locals.config.userLang;
templateValues.languageDirection = results.languageDirection;
templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;
if (data.hasOwnProperty('_header')) {
templateValues.metaTags = data._header.tags.meta;
templateValues.linkTags = data._header.tags.link;
} }
}
modifyNavItem(item, '/unread', '', unreadCount.topic);
modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic);
modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic);
modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic);
return item;
});
if (req.route && req.route.path === '/') { templateValues.browserTitle = results.browserTitle;
modifyTitle(templateValues); templateValues.navigation = results.navigation;
} templateValues.unreadCount = unreadCount;
templateValues.isAdmin = results.user.isAdmin;
templateValues.isGlobalMod = results.user.isGlobalMod;
templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
templateValues.canChat = results.canChat && meta.config.disableChat !== 1;
templateValues.user = results.user;
templateValues.userJSON = jsesc(JSON.stringify(results.user), { isScriptContext: true });
templateValues.useCustomCSS = meta.config.useCustomCSS && meta.config.customCSS;
templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : '';
templateValues.useCustomHTML = meta.config.useCustomHTML;
templateValues.customHTML = templateValues.useCustomHTML ? meta.config.customHTML : '';
templateValues.maintenanceHeader = meta.config.maintenanceMode && !results.isAdmin;
templateValues.defaultLang = meta.config.defaultLang || 'en-GB';
templateValues.userLang = res.locals.config.userLang;
templateValues.languageDirection = results.languageDirection;
templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;
if (data.hasOwnProperty('_header')) {
templateValues.metaTags = data._header.tags.meta;
templateValues.linkTags = data._header.tags.link;
}
plugins.fireHook('filter:middleware.renderHeader', { if (req.route && req.route.path === '/') {
req: req, modifyTitle(templateValues);
res: res, }
templateValues: templateValues,
data: data, const hookReturn = await plugins.fireHook('filter:middleware.renderHeader', {
}, next); req: req,
}, res: res,
], function (err, data) { templateValues: templateValues,
callback(err, data.templateValues); data: data,
}); });
};
middleware.renderHeader = function renderHeader(req, res, data, callback) { return hookReturn.templateValues;
async.waterfall([ }
async.apply(middleware.generateHeader, req, res, data),
function (templateValues, next) { middleware.renderHeader = async function renderHeader(req, res, data) {
req.app.render('header', templateValues, next); return await req.app.renderAsync('header', await generateHeader(req, res, data));
},
], callback);
}; };
middleware.renderFooter = function renderFooter(req, res, data, callback) { middleware.renderFooter = async function renderFooter(req, res, templateValues) {
async.waterfall([ const data = await plugins.fireHook('filter:middleware.renderFooter', {
function (next) { req: req,
plugins.fireHook('filter:middleware.renderFooter', { res: res,
req: req, templateValues: templateValues,
res: res, });
templateValues: data,
}, next); const results = await utils.promiseParallel({
}, scripts: plugins.fireHook('filter:scripts.get', []),
function (data, next) { timeagoLocale: (async () => {
async.parallel({ const languageCodes = await languages.listCodes();
scripts: async.apply(plugins.fireHook, 'filter:scripts.get', []), const userLang = res.locals.config.userLang;
timeagoLocale: (next) => { const timeagoCode = utils.userLangToTimeagoCode(userLang);
async.waterfall([
async.apply(languages.listCodes), if (languageCodes.includes(userLang) && languages.timeagoCodes.includes(timeagoCode)) {
(languageCodes, next) => { const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + timeagoCode + '.js';
const userLang = res.locals.config.userLang; return nconf.get('relative_path') + '/assets' + pathToLocaleFile;
const timeagoCode = utils.userLangToTimeagoCode(userLang);
if (languageCodes.includes(userLang) && languages.timeagoCodes.includes(timeagoCode)) {
const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + timeagoCode + '.js';
next(null, (nconf.get('relative_path') + '/assets' + pathToLocaleFile));
} else {
next(null, false);
}
},
], next);
},
}, function (err, results) {
next(err, data, results);
});
},
function (data, results, next) {
if (results.timeagoLocale) {
results.scripts.push(results.timeagoLocale);
} }
data.templateValues.scripts = results.scripts.map(function (script) { return false;
return { src: script }; })(),
}); });
data.templateValues.useCustomJS = meta.config.useCustomJS; if (results.timeagoLocale) {
data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : ''; results.scripts.push(results.timeagoLocale);
data.templateValues.isSpider = req.uid === -1; }
req.app.render('footer', data.templateValues, next); data.templateValues.scripts = results.scripts.map(function (script) {
}, return { src: script };
], callback); });
data.templateValues.useCustomJS = meta.config.useCustomJS;
data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : '';
data.templateValues.isSpider = req.uid === -1;
return await req.app.renderAsync('footer', data.templateValues);
}; };
function modifyTitle(obj) { function modifyTitle(obj) {

@ -12,8 +12,6 @@ const widgets = require('../widgets');
const utils = require('../utils'); const utils = require('../utils');
module.exports = function (middleware) { module.exports = function (middleware) {
const renderHeaderFooterAsync = util.promisify(renderHeaderFooter);
middleware.processRender = function processRender(req, res, next) { middleware.processRender = function processRender(req, res, next) {
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687 // res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
const render = res.render; const render = res.render;
@ -63,9 +61,9 @@ module.exports = function (middleware) {
const renderAsync = util.promisify((templateToRender, options, next) => render.call(self, templateToRender, options, next)); const renderAsync = util.promisify((templateToRender, options, next) => render.call(self, templateToRender, options, next));
const results = await utils.promiseParallel({ const results = await utils.promiseParallel({
header: renderHeaderFooterAsync('renderHeader', req, res, options), header: renderHeaderFooter('renderHeader', req, res, options),
content: renderAsync(templateToRender, options), content: renderAsync(templateToRender, options),
footer: renderHeaderFooterAsync('renderFooter', req, res, options), footer: renderHeaderFooter('renderFooter', req, res, options),
}); });
const str = results.header + const str = results.header +
@ -89,14 +87,13 @@ module.exports = function (middleware) {
next(); next();
}; };
function renderHeaderFooter(method, req, res, options, next) { async function renderHeaderFooter(method, req, res, options) {
if (res.locals.renderHeader) { if (res.locals.renderHeader) {
middleware[method](req, res, options, next); return await middleware[method](req, res, options);
} else if (res.locals.renderAdminHeader) { } else if (res.locals.renderAdminHeader) {
middleware.admin[method](req, res, options, next); return await middleware.admin[method](req, res, options);
} else {
next(null, '');
} }
return '';
} }
async function translate(str, req, res) { async function translate(str, req, res) {

Loading…
Cancel
Save