Skip to content

Commit

Permalink
removed previous changes and added one functioin
Browse files Browse the repository at this point in the history
  • Loading branch information
zanzoonh committed Sep 3, 2024
1 parent 2bae49a commit 0b689b7
Showing 1 changed file with 34 additions and 168 deletions.
202 changes: 34 additions & 168 deletions src/middleware/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){

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

View workflow job for this annotation

GitHub Actions / test

Unexpected space before function parentheses

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

View workflow job for this annotation

GitHub Actions / test

Missing space before opening brace
const registrationType = meta.config.registrationType || 'normal';
res.locals.config = res.locals.config || {};

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

View workflow job for this annotation

GitHub Actions / test

'res' is not defined

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

View workflow job for this annotation

GitHub Actions / test

'res' is not defined
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 All @@ -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,

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

View workflow job for this annotation

GitHub Actions / test

'res' is not defined
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,
};

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

View workflow job for this annotation

GitHub Actions / test

Block must not be padded by blank lines

}

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),
Expand All @@ -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;
Expand All @@ -359,41 +212,54 @@ 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,
} = await appendUnreadCounts({
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;
}

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

View workflow job for this annotation

GitHub Actions / test

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

0 comments on commit 0b689b7

Please sign in to comment.