mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
refactor: split out logic dedicated to calculating unread counts, to a separate local method
This commit is contained in:
@@ -79,9 +79,6 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
|
|||||||
timeagoCode: languages.userTimeagoCode(res.locals.config.userLang),
|
timeagoCode: languages.userTimeagoCode(res.locals.config.userLang),
|
||||||
browserTitle: translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title))),
|
browserTitle: translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title))),
|
||||||
navigation: navigation.get(req.uid),
|
navigation: navigation.get(req.uid),
|
||||||
unreadData: topics.getUnreadData({ uid: req.uid }),
|
|
||||||
unreadChatCount: messaging.getUnreadCount(req.uid),
|
|
||||||
unreadNotificationCount: user.notifications.getUnreadCount(req.uid),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const unreadData = {
|
const unreadData = {
|
||||||
@@ -105,44 +102,15 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
|
|||||||
|
|
||||||
templateValues.bootswatchSkin = (parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin : '') || meta.config.bootswatchSkin || '';
|
templateValues.bootswatchSkin = (parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin : '') || meta.config.bootswatchSkin || '';
|
||||||
templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin'; // TODO remove in v1.12.0+
|
templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin'; // TODO remove in v1.12.0+
|
||||||
|
|
||||||
const unreadCounts = results.unreadData.counts;
|
|
||||||
const unreadCount = {
|
|
||||||
topic: unreadCounts[''] || 0,
|
|
||||||
newTopic: unreadCounts.new || 0,
|
|
||||||
watchedTopic: unreadCounts.watched || 0,
|
|
||||||
unrepliedTopic: unreadCounts.unreplied || 0,
|
|
||||||
chat: results.unreadChatCount || 0,
|
|
||||||
notification: results.unreadNotificationCount || 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
Object.keys(unreadCount).forEach(function (key) {
|
|
||||||
if (unreadCount[key] > 99) {
|
|
||||||
unreadCount[key] = '99+';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const tidsByFilter = results.unreadData.tidsByFilter;
|
|
||||||
results.navigation = results.navigation.map(function (item) {
|
|
||||||
function modifyNavItem(item, route, filter, content) {
|
|
||||||
if (item && validator.unescape(item.originalRoute) === route) {
|
|
||||||
unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true));
|
|
||||||
item.content = content;
|
|
||||||
if (unreadCounts[filter] > 0) {
|
|
||||||
item.iconClass += ' unread-count';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
modifyNavItem(item, '/unread', '', unreadCount.topic);
|
|
||||||
modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic);
|
|
||||||
modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic);
|
|
||||||
modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic);
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
|
|
||||||
templateValues.browserTitle = results.browserTitle;
|
templateValues.browserTitle = results.browserTitle;
|
||||||
templateValues.navigation = results.navigation;
|
({
|
||||||
templateValues.unreadCount = unreadCount;
|
navigation: templateValues.navigation,
|
||||||
|
unreadCount: templateValues.unreadCount,
|
||||||
|
} = await appendUnreadCounts({
|
||||||
|
uid: req.uid,
|
||||||
|
navigation: results.navigation,
|
||||||
|
unreadData,
|
||||||
|
}));
|
||||||
templateValues.isAdmin = results.user.isAdmin;
|
templateValues.isAdmin = results.user.isAdmin;
|
||||||
templateValues.isGlobalMod = results.user.isGlobalMod;
|
templateValues.isGlobalMod = results.user.isGlobalMod;
|
||||||
templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
|
templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
|
||||||
@@ -183,6 +151,50 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
|
|||||||
return await req.app.renderAsync('header', hookReturn.templateValues);
|
return await req.app.renderAsync('header', hookReturn.templateValues);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function appendUnreadCounts({ uid, navigation, unreadData }) {
|
||||||
|
const results = await utils.promiseParallel({
|
||||||
|
unreadData: topics.getUnreadData({ uid: uid }),
|
||||||
|
unreadChatCount: messaging.getUnreadCount(uid),
|
||||||
|
unreadNotificationCount: user.notifications.getUnreadCount(uid),
|
||||||
|
});
|
||||||
|
|
||||||
|
const unreadCounts = results.unreadData.counts;
|
||||||
|
const unreadCount = {
|
||||||
|
topic: unreadCounts[''] || 0,
|
||||||
|
newTopic: unreadCounts.new || 0,
|
||||||
|
watchedTopic: unreadCounts.watched || 0,
|
||||||
|
unrepliedTopic: unreadCounts.unreplied || 0,
|
||||||
|
chat: results.unreadChatCount || 0,
|
||||||
|
notification: results.unreadNotificationCount || 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.keys(unreadCount).forEach(function (key) {
|
||||||
|
if (unreadCount[key] > 99) {
|
||||||
|
unreadCount[key] = '99+';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const tidsByFilter = results.unreadData.tidsByFilter;
|
||||||
|
navigation = navigation.map(function (item) {
|
||||||
|
function modifyNavItem(item, route, filter, content) {
|
||||||
|
if (item && validator.unescape(item.originalRoute) === route) {
|
||||||
|
unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true));
|
||||||
|
item.content = content;
|
||||||
|
if (unreadCounts[filter] > 0) {
|
||||||
|
item.iconClass += ' unread-count';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
modifyNavItem(item, '/unread', '', unreadCount.topic);
|
||||||
|
modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic);
|
||||||
|
modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic);
|
||||||
|
modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic);
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
|
||||||
|
return { navigation, unreadCount };
|
||||||
|
}
|
||||||
|
|
||||||
middleware.renderFooter = async function renderFooter(req, res, templateValues) {
|
middleware.renderFooter = async function renderFooter(req, res, templateValues) {
|
||||||
const data = await plugins.hooks.fire('filter:middleware.renderFooter', {
|
const data = await plugins.hooks.fire('filter:middleware.renderFooter', {
|
||||||
req: req,
|
req: req,
|
||||||
|
|||||||
Reference in New Issue
Block a user