mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 15:05:46 +01:00
fix: #7124
This commit is contained in:
@@ -96,44 +96,50 @@ 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);
|
||||
}
|
||||
|
||||
if (!unreadTopics[''][tid]) {
|
||||
increaseUnreadCount('');
|
||||
unreadTopics[''][tid] = true;
|
||||
}
|
||||
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 (isNewTopic && !unreadTopics.new[tid]) {
|
||||
increaseUnreadCount('new');
|
||||
unreadTopics.new[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 (isUnreplied && !unreadTopics.unreplied[tid]) {
|
||||
increaseUnreadCount('unreplied');
|
||||
unreadTopics.unreplied[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);
|
||||
if (post.topic.isFollowing && !unreadTopics.watched[tid]) {
|
||||
increaseUnreadCount('watched');
|
||||
unreadTopics.watched[tid] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
$('[data-tid="' + tid + '"]').addClass('unread');
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
@@ -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([
|
||||
|
||||
Reference in New Issue
Block a user