Skip to content

Commit

Permalink
testing complexity for loadClientHeaderFooterData function
Browse files Browse the repository at this point in the history
  • Loading branch information
zanzoonh committed Sep 3, 2024
1 parent 9610627 commit 2bae49a
Showing 1 changed file with 160 additions and 38 deletions.
198 changes: 160 additions & 38 deletions src/middleware/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,151 @@ module.exports = function (middleware) {
return null;
}

function initializeTemplateValues(res, options, registrationType) {
// function initializeTemplateValues(res, options, registrationType) {
// return {

Check failure on line 156 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// title: meta.config.title || '',

Check failure on line 157 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'title:url': meta.config['title:url'] || '',

Check failure on line 158 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// description: meta.config.description || '',

Check failure on line 159 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'cache-buster': meta.config['cache-buster'] || '',

Check failure on line 160 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'brand:logo': meta.config['brand:logo'] || '',

Check failure on line 161 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'brand:logo:url': meta.config['brand:logo:url'] || '',

Check failure on line 162 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'brand:logo:alt': meta.config['brand:logo:alt'] || '',

Check failure on line 163 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// 'brand:logo:display': meta.config['brand:logo'] ? '' : 'hide',

Check failure on line 164 in src/middleware/render.js

View workflow job for this annotation

GitHub Actions / test

Unexpected tab character
// allowRegistration: registrationType === 'normal',
// searchEnabled: plugins.hooks.hasListeners('filter:search.query'),
// postQueueEnabled: !!meta.config.postQueue,
// registrationQueueEnabled: meta.config.registrationApprovalType !== 'normal'
// || (meta.config.registrationType === 'invite-only' || meta.config.registrationType === 'admin-invite-only'),
// config: res.locals.config,
// relative_path,
// bodyClass: options.bodyClass,
// widgets: options.widgets,
// configJSON: jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }),
// useCustomCSS: meta.config.useCustomCSS && meta.config.customCSS,
// customCSS: meta.config.useCustomCSS ? (meta.config.renderedCustomCSS || '') : '',
// useCustomHTML: meta.config.useCustomHTML,
// customHTML: meta.config.useCustomHTML ? meta.config.customHTML : '',
// maintenanceHeader: meta.config.maintenanceMode && !results.isAdmin,
// defaultLang: meta.config.defaultLang || 'en-GB',
// userLang: res.locals.config.userLang,
// };
// }

// async function loadClientHeaderFooterData(req, res, options) {
// const registrationType = meta.config.registrationType || 'normal';
// res.locals.config = res.locals.config || {};


// const templateValues = initializeTemplateValues(res, options, registrationType);

// templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });

// const title = translator.unescape(utils.stripHTMLTags(options.title || ''));
// 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),
// blocks: user.blocks.list(req.uid),
// user: user.getUserData(req.uid),
// isEmailConfirmSent: req.uid <= 0 ? false : await user.email.isValidationPending(req.uid),
// languageDirection: translator.translate('[[language:dir]]', res.locals.config.userLang),
// timeagoCode: languages.userTimeagoCode(res.locals.config.userLang),
// browserTitle: translator.translate(controllersHelpers.buildTitle(title)),
// navigation: navigation.get(req.uid),
// roomIds: req.uid > 0 ? db.getSortedSetRevRange(`uid:${req.uid}:chat:rooms`, 0, 0) : [],
// });

// const unreadData = {
// '': {},
// new: {},
// watched: {},
// unreplied: {},
// };

// results.user.unreadData = unreadData;
// results.user.isAdmin = results.isAdmin;
// results.user.isGlobalMod = results.isGlobalMod;
// results.user.isMod = !!results.isModerator;
// results.user.privileges = results.privileges;
// results.user.blocks = results.blocks;
// results.user.timeagoCode = results.timeagoCode;
// results.user[results.user.status] = true;
// results.user.lastRoomId = results.roomIds.length ? results.roomIds[0] : null;

// results.user.email = String(results.user.email);
// results.user['email:confirmed'] = results.user['email:confirmed'] === 1;
// results.user.isEmailConfirmSent = !!results.isEmailConfirmSent;

// templateValues.bootswatchSkin = res.locals.config.bootswatchSkin || '';
// templateValues.browserTitle = results.browserTitle;
// ({
// navigation: templateValues.navigation,
// unreadCount: templateValues.unreadCount,
// } = await appendUnreadCounts({
// uid: req.uid,
// query: req.query,
// navigation: results.navigation,
// unreadData,
// }));

// templateValues.isAdmin = results.user.isAdmin;
// templateValues.isGlobalMod = results.user.isGlobalMod;
// templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
// templateValues.canChat = (results.privileges.chat || results.privileges['chat:privileged']) && 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;
// if (req.query.noScriptMessage) {
// templateValues.noScriptMessage = validator.escape(String(req.query.noScriptMessage));
// }

// templateValues.template = { name: res.locals.template };
// templateValues.template[res.locals.template] = true;

// if (options.hasOwnProperty('_header')) {
// templateValues.metaTags = options._header.tags.meta;
// templateValues.linkTags = options._header.tags.link;
// }

// if (req.route && req.route.path === '/') {
// modifyTitle(templateValues);
// }
// return templateValues;
// }

async function loadClientHeaderFooterData(req, res, options) {
const registrationType = meta.config.registrationType || 'normal';
res.locals.config = res.locals.config || {};
const templateValues = initializeTemplateValues(req, res, options, registrationType);

const title = translator.unescape(utils.stripHTMLTags(options.title || ''));
const results = await gatherUserData(req, title);

updateUserData(results);
updateTemplateValues(templateValues, results, req, res, options);

if (req.query.noScriptMessage) {
templateValues.noScriptMessage = validator.escape(String(req.query.noScriptMessage));
}
templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;

if (req.route && req.route.path === '/') {
modifyTitle(templateValues);
}

return templateValues;
}

function initializeTemplateValues(req, res, options, registrationType) {
return {
title: meta.config.title || '',
'title:url': meta.config['title:url'] || '',
Expand Down Expand Up @@ -181,18 +325,9 @@ module.exports = function (middleware) {
userLang: res.locals.config.userLang,
};
}

async function loadClientHeaderFooterData(req, res, options) {
const registrationType = meta.config.registrationType || 'normal';
res.locals.config = res.locals.config || {};


const templateValues = initializeTemplateValues(res, options, registrationType);

templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });

const title = translator.unescape(utils.stripHTMLTags(options.title || ''));
const results = await utils.promiseParallel({

async function gatherUserData(req, title) {
return await utils.promiseParallel({
isAdmin: user.isAdministrator(req.uid),
isGlobalMod: user.isGlobalModerator(req.uid),
isModerator: user.isModeratorOfAnyCategory(req.uid),
Expand All @@ -206,14 +341,15 @@ module.exports = function (middleware) {
navigation: navigation.get(req.uid),
roomIds: req.uid > 0 ? db.getSortedSetRevRange(`uid:${req.uid}:chat:rooms`, 0, 0) : [],
});

}

function updateUserData(results) {
const unreadData = {
'': {},
new: {},
watched: {},
unreplied: {},
};

results.user.unreadData = unreadData;
results.user.isAdmin = results.isAdmin;
results.user.isGlobalMod = results.isGlobalMod;
Expand All @@ -223,55 +359,41 @@ module.exports = function (middleware) {
results.user.timeagoCode = results.timeagoCode;
results.user[results.user.status] = true;
results.user.lastRoomId = results.roomIds.length ? results.roomIds[0] : null;

results.user.email = String(results.user.email);
results.user['email:confirmed'] = results.user['email:confirmed'] === 1;
results.user.isEmailConfirmSent = !!results.isEmailConfirmSent;

templateValues.bootswatchSkin = res.locals.config.bootswatchSkin || '';
}

async function updateTemplateValues(templateValues, results, req, res, options) {
templateValues.browserTitle = results.browserTitle;

({
navigation: templateValues.navigation,
unreadCount: templateValues.unreadCount,
} = await appendUnreadCounts({
uid: req.uid,
query: req.query,
navigation: results.navigation,
unreadData,
unreadData: results.user.unreadData,
}));

templateValues.isAdmin = results.user.isAdmin;
templateValues.isGlobalMod = results.user.isGlobalMod;
templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
templateValues.canChat = (results.privileges.chat || results.privileges['chat:privileged']) && meta.config.disableChat !== 1;
templateValues.canChat = (results.privileges.chat || results.privileges['chat:privileged'])
&& 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;
if (req.query.noScriptMessage) {
templateValues.noScriptMessage = validator.escape(String(req.query.noScriptMessage));
}

templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;


if (options.hasOwnProperty('_header')) {
templateValues.metaTags = options._header.tags.meta;
templateValues.linkTags = options._header.tags.link;
}

if (req.route && req.route.path === '/') {
modifyTitle(templateValues);
}
return templateValues;
}

async function loadAdminHeaderFooterData(req, res, options) {
const custom_header = {
plugins: [],
Expand Down

0 comments on commit 2bae49a

Please sign in to comment.