From 2bae49a4a9dbb3482e25403ce509b96982bd4503 Mon Sep 17 00:00:00 2001 From: Zeina Halawa Date: Tue, 3 Sep 2024 12:24:29 +0300 Subject: [PATCH] testing complexity for loadClientHeaderFooterData function --- src/middleware/render.js | 198 +++++++++++++++++++++++++++++++-------- 1 file changed, 160 insertions(+), 38 deletions(-) diff --git a/src/middleware/render.js b/src/middleware/render.js index 24670836a6..49360b885e 100644 --- a/src/middleware/render.js +++ b/src/middleware/render.js @@ -152,7 +152,151 @@ module.exports = function (middleware) { return null; } - function initializeTemplateValues(res, options, registrationType) { + // function initializeTemplateValues(res, options, registrationType) { + // return { + // title: meta.config.title || '', + // 'title:url': meta.config['title:url'] || '', + // description: meta.config.description || '', + // 'cache-buster': meta.config['cache-buster'] || '', + // 'brand:logo': meta.config['brand:logo'] || '', + // 'brand:logo:url': meta.config['brand:logo:url'] || '', + // 'brand:logo:alt': meta.config['brand:logo:alt'] || '', + // 'brand:logo:display': meta.config['brand:logo'] ? '' : 'hide', + // 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'] || '', @@ -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), @@ -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; @@ -223,13 +359,14 @@ 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, @@ -237,41 +374,26 @@ module.exports = function (middleware) { 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: [],