From 0b689b7e793765a61814c2293a14aa954ebc197a Mon Sep 17 00:00:00 2001 From: Zeina Halawa Date: Tue, 3 Sep 2024 14:47:38 +0300 Subject: [PATCH] removed previous changes and added one functioin --- src/middleware/render.js | 202 +++++++-------------------------------- 1 file changed, 34 insertions(+), 168 deletions(-) diff --git a/src/middleware/render.js b/src/middleware/render.js index 49360b885e..da181a805f 100644 --- a/src/middleware/render.js +++ b/src/middleware/render.js @@ -152,151 +152,9 @@ module.exports = function (middleware) { return null; } - // 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) { + async function initializeTemplate (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'] || '', @@ -309,25 +167,21 @@ module.exports = function (middleware) { 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'), + 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 gatherUserData(req, title) { - return await utils.promiseParallel({ + async function loadClientHeaderFooterData(req, res, options) { + const templateValues = initializeTemplate(options); + + 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), @@ -341,15 +195,14 @@ 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; @@ -359,14 +212,13 @@ 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; - } - - async function updateTemplateValues(templateValues, results, req, res, options) { + + templateValues.bootswatchSkin = res.locals.config.bootswatchSkin || ''; templateValues.browserTitle = results.browserTitle; - ({ navigation: templateValues.navigation, unreadCount: templateValues.unreadCount, @@ -374,26 +226,40 @@ module.exports = function (middleware) { uid: req.uid, query: req.query, navigation: results.navigation, - unreadData: results.user.unreadData, + 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: [],