mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
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
This commit is contained in:
@@ -9,6 +9,7 @@ var semver = require('semver');
|
||||
var user = require('../user');
|
||||
var meta = require('../meta');
|
||||
var plugins = require('../plugins');
|
||||
var utils = require('../../public/src/utils');
|
||||
var versions = require('../admin/versions');
|
||||
|
||||
var controllers = {
|
||||
@@ -37,39 +38,21 @@ module.exports = function (middleware) {
|
||||
], next);
|
||||
};
|
||||
|
||||
middleware.admin.renderHeader = function (req, res, data, next) {
|
||||
middleware.admin.renderHeader = async (req, res, data) => {
|
||||
var custom_header = {
|
||||
plugins: [],
|
||||
authentication: [],
|
||||
};
|
||||
res.locals.config = res.locals.config || {};
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
userData: function (next) {
|
||||
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], next);
|
||||
},
|
||||
scripts: function (next) {
|
||||
getAdminScripts(next);
|
||||
},
|
||||
custom_header: function (next) {
|
||||
plugins.fireHook('filter:admin.header.build', custom_header, next);
|
||||
},
|
||||
configs: function (next) {
|
||||
meta.configs.list(next);
|
||||
},
|
||||
latestVersion: function (next) {
|
||||
versions.getLatestVersion(function (err, result) {
|
||||
if (err) {
|
||||
winston.error('[acp] Failed to fetch latest version', err);
|
||||
}
|
||||
|
||||
next(null, err ? null : result);
|
||||
const results = await utils.promiseParallel({
|
||||
userData: user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed']),
|
||||
scripts: getAdminScripts(),
|
||||
custom_header: plugins.fireHook('filter:admin.header.build', custom_header),
|
||||
configs: meta.configs.list(),
|
||||
latestVersion: versions.getLatestVersion(),
|
||||
});
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
|
||||
var userData = results.userData;
|
||||
userData.uid = req.uid;
|
||||
userData['email:confirmed'] = userData['email:confirmed'] === 1;
|
||||
@@ -105,25 +88,17 @@ module.exports = function (middleware) {
|
||||
templateValues.template = { name: res.locals.template };
|
||||
templateValues.template[res.locals.template] = true;
|
||||
|
||||
req.app.render('admin/header', templateValues, next);
|
||||
},
|
||||
], next);
|
||||
return await req.app.renderAsync('admin/header', templateValues);
|
||||
};
|
||||
|
||||
function getAdminScripts(callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:admin.scripts.get', [], next);
|
||||
},
|
||||
function (scripts, next) {
|
||||
next(null, scripts.map(function (script) {
|
||||
async function getAdminScripts() {
|
||||
const scripts = await plugins.fireHook('filter:admin.scripts.get', []);
|
||||
return scripts.map(function (script) {
|
||||
return { src: script };
|
||||
}));
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
}
|
||||
|
||||
middleware.admin.renderFooter = function (req, res, data, next) {
|
||||
req.app.render('admin/footer', data, next);
|
||||
middleware.admin.renderFooter = async function (req, res, data) {
|
||||
return await req.app.renderAsync('admin/footer', data);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -52,7 +52,7 @@ module.exports = function (middleware) {
|
||||
], next);
|
||||
};
|
||||
|
||||
middleware.generateHeader = function generateHeader(req, res, data, callback) {
|
||||
async function generateHeader(req, res, data) {
|
||||
var registrationType = meta.config.registrationType || 'normal';
|
||||
res.locals.config = res.locals.config || {};
|
||||
var templateValues = {
|
||||
@@ -73,50 +73,24 @@ module.exports = function (middleware) {
|
||||
|
||||
templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
isAdmin: function (next) {
|
||||
user.isAdministrator(req.uid, next);
|
||||
},
|
||||
isGlobalMod: function (next) {
|
||||
user.isGlobalModerator(req.uid, next);
|
||||
},
|
||||
isModerator: function (next) {
|
||||
user.isModeratorOfAnyCategory(req.uid, next);
|
||||
},
|
||||
privileges: function (next) {
|
||||
privileges.global.get(req.uid, next);
|
||||
},
|
||||
user: function (next) {
|
||||
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),
|
||||
const results = await utils.promiseParallel({
|
||||
isAdmin: user.isAdministrator(req.uid),
|
||||
isGlobalMod: user.isGlobalModerator(req.uid),
|
||||
isModerator: user.isModeratorOfAnyCategory(req.uid),
|
||||
privileges: privileges.global.get(req.uid),
|
||||
user: user.getUserData(req.uid),
|
||||
isEmailConfirmSent: (!meta.config.requireEmailConfirmation || req.uid <= 0) ? false : await db.get('uid:' + req.uid + ':confirm:email:sent'),
|
||||
languageDirection: translator.translate('[[language:dir]]', res.locals.config.userLang),
|
||||
browserTitle: translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title))),
|
||||
navigation: navigation.get(req.uid),
|
||||
banned: user.bans.isBanned(req.uid),
|
||||
banReason: user.bans.getReason(req.uid),
|
||||
|
||||
unreadData: topics.getUnreadData({ uid: req.uid }),
|
||||
unreadChatCount: messaging.getUnreadCount(req.uid),
|
||||
unreadNotificationCount: user.notifications.getUnreadCount(req.uid),
|
||||
});
|
||||
|
||||
unreadData: async.apply(topics.getUnreadData, { uid: req.uid }),
|
||||
unreadChatCount: async.apply(messaging.getUnreadCount, req.uid),
|
||||
unreadNotificationCount: async.apply(user.notifications.getUnreadCount, req.uid),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (results.banned) {
|
||||
req.logout();
|
||||
return res.redirect('/');
|
||||
@@ -144,7 +118,7 @@ module.exports = function (middleware) {
|
||||
templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin'; // TODO remove in v1.12.0+
|
||||
|
||||
const unreadCounts = results.unreadData.counts;
|
||||
var unreadCount = {
|
||||
const unreadCount = {
|
||||
topic: unreadCounts[''] || 0,
|
||||
newTopic: unreadCounts.new || 0,
|
||||
watchedTopic: unreadCounts.watched || 0,
|
||||
@@ -207,60 +181,42 @@ module.exports = function (middleware) {
|
||||
modifyTitle(templateValues);
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:middleware.renderHeader', {
|
||||
const hookReturn = await plugins.fireHook('filter:middleware.renderHeader', {
|
||||
req: req,
|
||||
res: res,
|
||||
templateValues: templateValues,
|
||||
data: data,
|
||||
}, next);
|
||||
},
|
||||
], function (err, data) {
|
||||
callback(err, data.templateValues);
|
||||
});
|
||||
|
||||
return hookReturn.templateValues;
|
||||
}
|
||||
|
||||
middleware.renderHeader = async function renderHeader(req, res, data) {
|
||||
return await req.app.renderAsync('header', await generateHeader(req, res, data));
|
||||
};
|
||||
|
||||
middleware.renderHeader = function renderHeader(req, res, data, callback) {
|
||||
async.waterfall([
|
||||
async.apply(middleware.generateHeader, req, res, data),
|
||||
function (templateValues, next) {
|
||||
req.app.render('header', templateValues, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
middleware.renderFooter = function renderFooter(req, res, data, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:middleware.renderFooter', {
|
||||
middleware.renderFooter = async function renderFooter(req, res, templateValues) {
|
||||
const data = await plugins.fireHook('filter:middleware.renderFooter', {
|
||||
req: req,
|
||||
res: res,
|
||||
templateValues: data,
|
||||
}, next);
|
||||
},
|
||||
function (data, next) {
|
||||
async.parallel({
|
||||
scripts: async.apply(plugins.fireHook, 'filter:scripts.get', []),
|
||||
timeagoLocale: (next) => {
|
||||
async.waterfall([
|
||||
async.apply(languages.listCodes),
|
||||
(languageCodes, next) => {
|
||||
templateValues: templateValues,
|
||||
});
|
||||
|
||||
const results = await utils.promiseParallel({
|
||||
scripts: plugins.fireHook('filter:scripts.get', []),
|
||||
timeagoLocale: (async () => {
|
||||
const languageCodes = await languages.listCodes();
|
||||
const userLang = res.locals.config.userLang;
|
||||
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);
|
||||
return nconf.get('relative_path') + '/assets' + pathToLocaleFile;
|
||||
}
|
||||
},
|
||||
], next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
next(err, data, results);
|
||||
return false;
|
||||
})(),
|
||||
});
|
||||
},
|
||||
function (data, results, next) {
|
||||
|
||||
if (results.timeagoLocale) {
|
||||
results.scripts.push(results.timeagoLocale);
|
||||
}
|
||||
@@ -271,9 +227,8 @@ module.exports = function (middleware) {
|
||||
data.templateValues.useCustomJS = meta.config.useCustomJS;
|
||||
data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : '';
|
||||
data.templateValues.isSpider = req.uid === -1;
|
||||
req.app.render('footer', data.templateValues, next);
|
||||
},
|
||||
], callback);
|
||||
|
||||
return await req.app.renderAsync('footer', data.templateValues);
|
||||
};
|
||||
|
||||
function modifyTitle(obj) {
|
||||
|
||||
@@ -12,8 +12,6 @@ const widgets = require('../widgets');
|
||||
const utils = require('../utils');
|
||||
|
||||
module.exports = function (middleware) {
|
||||
const renderHeaderFooterAsync = util.promisify(renderHeaderFooter);
|
||||
|
||||
middleware.processRender = function processRender(req, res, next) {
|
||||
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
|
||||
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 results = await utils.promiseParallel({
|
||||
header: renderHeaderFooterAsync('renderHeader', req, res, options),
|
||||
header: renderHeaderFooter('renderHeader', req, res, options),
|
||||
content: renderAsync(templateToRender, options),
|
||||
footer: renderHeaderFooterAsync('renderFooter', req, res, options),
|
||||
footer: renderHeaderFooter('renderFooter', req, res, options),
|
||||
});
|
||||
|
||||
const str = results.header +
|
||||
@@ -89,14 +87,13 @@ module.exports = function (middleware) {
|
||||
next();
|
||||
};
|
||||
|
||||
function renderHeaderFooter(method, req, res, options, next) {
|
||||
async function renderHeaderFooter(method, req, res, options) {
|
||||
if (res.locals.renderHeader) {
|
||||
middleware[method](req, res, options, next);
|
||||
return await middleware[method](req, res, options);
|
||||
} else if (res.locals.renderAdminHeader) {
|
||||
middleware.admin[method](req, res, options, next);
|
||||
} else {
|
||||
next(null, '');
|
||||
return await middleware.admin[method](req, res, options);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
async function translate(str, req, res) {
|
||||
|
||||
Reference in New Issue
Block a user