Skip to content

Node.js based forum software built for the modern web

fix: #7124

Barış Soner Uşaklı4650a76036f0
diff --git a/public/src/client/unread.js b/public/src/client/unread.js
index d5a8966576..504f72e613 100644
--- a/public/src/client/unread.js
+++ b/public/src/client/unread.js
@@ -96,43 +96,49 @@ define('forum/unread', ['topicSelect', 'components', 'topicList'], function (top
}
Unread.initUnreadTopics = function () {
- var unreadTopics = {};
+ var unreadTopics = app.user.unreadData;
function onNewPost(data) {
if (data && data.posts && data.posts.length) {
var post = data.posts[0];
- if (
- parseInt(post.uid, 10) !== parseInt(app.user.uid, 10) &&
- !unreadTopics[post.topic.tid] &&
- (post.topic.isFollowing || post.categoryWatchState === watchStates.watching)
+ if (parseInt(post.uid, 10) === parseInt(app.user.uid, 10) ||
+ (!post.topic.isFollowing && post.categoryWatchState !== watchStates.watching)
) {
- increaseUnreadCount(post);
- markTopicsUnread(post.topic.tid);
- unreadTopics[post.topic.tid] = true;
+ return;
}
- }
- }
- function increaseUnreadCount(post) {
- var unreadTopicCount = parseInt($('a[href="' + config.relative_path + '/unread"].navigation-link i').attr('data-content'), 10) + 1;
- updateUnreadTopicCount('/unread', unreadTopicCount);
+ var tid = post.topic.tid;
+ if (!unreadTopics[''][tid] || !unreadTopics.new[tid] ||
+ !unreadTopics.watched[tid] || !unreadTopics.unreplied[tid]) {
+ markTopicsUnread(tid);
+ }
- var isNewTopic = post.isMain && parseInt(post.uid, 10) !== parseInt(app.user.uid, 10);
- if (isNewTopic) {
- var unreadNewTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=new"].navigation-link i').attr('data-content'), 10) + 1;
- updateUnreadTopicCount('/unread?filter=new', unreadNewTopicCount);
- }
+ if (!unreadTopics[''][tid]) {
+ increaseUnreadCount('');
+ unreadTopics[''][tid] = true;
+ }
+ var isNewTopic = post.isMain && parseInt(post.uid, 10) !== parseInt(app.user.uid, 10);
+ if (isNewTopic && !unreadTopics.new[tid]) {
+ increaseUnreadCount('new');
+ unreadTopics.new[tid] = true;
+ }
+ var isUnreplied = parseInt(post.topic.postcount, 10) <= 1;
+ if (isUnreplied && !unreadTopics.unreplied[tid]) {
+ increaseUnreadCount('unreplied');
+ unreadTopics.unreplied[tid] = true;
+ }
- var isUnreplied = parseInt(post.topic.postcount, 10) <= 1;
- if (isUnreplied) {
- var unreadUnrepliedTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=unreplied"].navigation-link i').attr('data-content'), 10) + 1;
- updateUnreadTopicCount('/unread?filter=unreplied', unreadUnrepliedTopicCount);
+ if (post.topic.isFollowing && !unreadTopics.watched[tid]) {
+ increaseUnreadCount('watched');
+ unreadTopics.watched[tid] = true;
+ }
}
+ }
- if (post.topic.isFollowing) {
- var unreadWatchedTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=watched"].navigation-link i').attr('data-content'), 10) + 1;
- updateUnreadTopicCount('/unread?filter=watched', unreadWatchedTopicCount);
- }
+ function increaseUnreadCount(filter) {
+ var unreadUrl = '/unread' + (filter ? '?filter=' + filter : '');
+ var newCount = 1 + parseInt($('a[href="' + config.relative_path + unreadUrl + '"].navigation-link i').attr('data-content'), 10);
+ updateUnreadTopicCount(unreadUrl, newCount);
}
function markTopicsUnread(tid) {
@@ -141,7 +147,9 @@ define('forum/unread', ['topicSelect', 'components', 'topicList'], function (top
$(window).on('action:ajaxify.end', function () {
if (ajaxify.data.template.topic) {
- delete unreadTopics[ajaxify.data.tid];
+ ['', 'new', 'watched', 'unreplied'].forEach(function (filter) {
+ delete unreadTopics[filter][ajaxify.data.tid];
+ });
}
});
socket.removeListener('event:new_post', onNewPost);
diff --git a/public/src/modules/topicList.js b/public/src/modules/topicList.js
index ab4d0aa08c..f002665904 100644
--- a/public/src/modules/topicList.js
+++ b/public/src/modules/topicList.js
@@ -236,7 +236,7 @@ define('topicList', [
count: config.topicsPerPage,
cid: query.cid,
query: query,
- term: ajaxify.data.selectedTerm.term,
+ term: ajaxify.data.selectedTerm && ajaxify.data.selectedTerm.term,
filter: ajaxify.data.selectedFilter.filter,
set: topicListEl.attr('data-set') ? topicListEl.attr('data-set') : 'topics:recent',
}, callback);
diff --git a/src/middleware/header.js b/src/middleware/header.js
index e2d6633722..2ea64ea33e 100644
--- a/src/middleware/header.js
+++ b/src/middleware/header.js
@@ -3,6 +3,7 @@
var async = require('async');
var nconf = require('nconf');
var jsesc = require('jsesc');
+var _ = require('lodash');
var db = require('../database');
var user = require('../user');
@@ -109,7 +110,7 @@ module.exports = function (middleware) {
banned: async.apply(user.isBanned, req.uid),
banReason: async.apply(user.getBannedReason, req.uid),
- unreadCounts: async.apply(topics.getUnreadTids, { uid: req.uid, count: true }),
+ unreadData: async.apply(topics.getUnreadData, { uid: req.uid }),
unreadChatCount: async.apply(messaging.getUnreadCount, req.uid),
unreadNotificationCount: async.apply(user.notifications.getUnreadCount, req.uid),
}, next);
@@ -120,6 +121,14 @@ module.exports = function (middleware) {
return res.redirect('/');
}
+ const unreadData = {
+ '': {},
+ new: {},
+ watched: {},
+ unreplied: {},
+ };
+
+ results.user.unreadData = unreadData;
results.user.isAdmin = results.isAdmin;
results.user.isGlobalMod = results.isGlobalMod;
results.user.isMod = !!results.isModerator;
@@ -131,12 +140,12 @@ module.exports = function (middleware) {
results.user.isEmailConfirmSent = !!results.isEmailConfirmSent;
templateValues.bootswatchSkin = parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin || '' : '';
-
+ const unreadCounts = results.unreadData.counts;
var unreadCount = {
- topic: results.unreadCounts[''] || 0,
- newTopic: results.unreadCounts.new || 0,
- watchedTopic: results.unreadCounts.watched || 0,
- unrepliedTopic: results.unreadCounts.unreplied || 0,
+ topic: unreadCounts[''] || 0,
+ newTopic: unreadCounts.new || 0,
+ watchedTopic: unreadCounts.watched || 0,
+ unrepliedTopic: unreadCounts.unreplied || 0,
chat: results.unreadChatCount || 0,
notification: results.unreadNotificationCount || 0,
};
@@ -147,19 +156,21 @@ module.exports = function (middleware) {
}
});
+ const tidsByFilter = results.unreadData.tidsByFilter;
results.navigation = results.navigation.map(function (item) {
- function modifyNavItem(item, route, count, content) {
+ function modifyNavItem(item, route, filter, content) {
if (item && item.originalRoute === route) {
+ unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true));
item.content = content;
- if (count > 0) {
+ if (unreadCounts[filter] > 0) {
item.iconClass += ' unread-count';
}
}
}
- modifyNavItem(item, '/unread', results.unreadCounts[''], unreadCount.topic);
- modifyNavItem(item, '/unread?filter=new', results.unreadCounts.new, unreadCount.newTopic);
- modifyNavItem(item, '/unread?filter=watched', results.unreadCounts.watched, unreadCount.watchedTopic);
- modifyNavItem(item, '/unread?filter=unreplied', results.unreadCounts.unreplied, unreadCount.unrepliedTopic);
+ 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;
});
diff --git a/src/topics/unread.js b/src/topics/unread.js
index 5179d994ea..c02fc54640 100644
--- a/src/topics/unread.js
+++ b/src/topics/unread.js
@@ -64,15 +64,37 @@ module.exports = function (Topics) {
};
Topics.getUnreadTids = function (params, callback) {
- var uid = parseInt(params.uid, 10);
- var counts = {
+ async.waterfall([
+ function (next) {
+ Topics.getUnreadData(params, next);
+ },
+ function (results, next) {
+ next(null, params.count ? results.counts : results.tids);
+ },
+ ], callback);
+ };
+
+ Topics.getUnreadData = function (params, callback) {
+ const uid = parseInt(params.uid, 10);
+ const counts = {
'': 0,
new: 0,
watched: 0,
unreplied: 0,
};
+ const noUnreadData = {
+ tids: [],
+ counts: counts,
+ tidsByFilter: {
+ '': [],
+ new: [],
+ watched: [],
+ unreplied: [],
+ },
+ };
+
if (uid <= 0) {
- return callback(null, params.count ? counts : []);
+ return setImmediate(callback, null, noUnreadData);
}
params.filter = params.filter || '';
@@ -102,7 +124,7 @@ module.exports = function (Topics) {
},
function (results, next) {
if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
- return callback(null, params.count ? counts : []);
+ return callback(null, noUnreadData);
}
filterTopics(params, results, next);
@@ -117,9 +139,6 @@ module.exports = function (Topics) {
filter: params.filter,
}, next);
},
- function (results, next) {
- next(null, params.count ? results.counts : results.tids);
- },
], callback);
};
@@ -166,7 +185,7 @@ module.exports = function (Topics) {
tids = tids.slice(0, 200);
if (!tids.length) {
- return callback(null, { counts: counts, tids: tids });
+ return callback(null, { counts: counts, tids: tids, tidsByFilter: tidsByFilter });
}
async.waterfall([