Skip to content

Node.js based forum software built for the modern web

fix: #10594, move counter code (#11529)

Barış Soner Uşaklı5607e5bccbbb
diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js
index c7ab8e2fa7..58f46313b3 100644
--- a/src/categories/recentreplies.js
+++ b/src/categories/recentreplies.js
@@ -165,7 +165,6 @@ module.exports = function (Categories) {
// terrible name, should be topics.moveTopicPosts
Categories.moveRecentReplies = async function (tid, oldCid, cid) {
- await updatePostCount(tid, oldCid, cid);
const [pids, topicDeleted] = await Promise.all([
topics.getPids(tid),
topics.getTopicField(tid, 'deleted'),
@@ -195,16 +194,4 @@ module.exports = function (Categories) {
]);
}, { batch: 500 });
};
-
- async function updatePostCount(tid, oldCid, newCid) {
- const postCount = await topics.getTopicField(tid, 'postcount');
- if (!postCount) {
- return;
- }
-
- await Promise.all([
- db.incrObjectFieldBy(`category:${oldCid}`, 'post_count', -postCount),
- db.incrObjectFieldBy(`category:${newCid}`, 'post_count', postCount),
- ]);
- }
};
diff --git a/src/categories/topics.js b/src/categories/topics.js
index 020713aabe..10090abaa3 100644
--- a/src/categories/topics.js
+++ b/src/categories/topics.js
@@ -188,6 +188,20 @@ module.exports = function (Categories) {
await Categories.updateRecentTidForCid(cid);
};
+ Categories.onTopicsMoved = async (cids) => {
+ await Promise.all(cids.map(async (cid) => {
+ await Promise.all([
+ Categories.setCategoryField(
+ cid, 'topic_count', await db.sortedSetCard(`cid:${cid}:tids:lastposttime`)
+ ),
+ Categories.setCategoryField(
+ cid, 'post_count', await db.sortedSetCard(`cid:${cid}:pids`)
+ ),
+ Categories.updateRecentTidForCid(cid),
+ ]);
+ }));
+ };
+
async function filterScheduledTids(tids) {
const scores = await db.sortedSetScores('topics:scheduled', tids);
const now = Date.now();
diff --git a/src/socket.io/topics/move.js b/src/socket.io/topics/move.js
index 5caeaad9f3..e7a164594c 100644
--- a/src/socket.io/topics/move.js
+++ b/src/socket.io/topics/move.js
@@ -20,13 +20,16 @@ module.exports = function (SocketTopics) {
}
const uids = await user.getUidsFromSet('users:online', 0, -1);
-
+ const cids = [parseInt(data.cid, 10)];
await async.eachLimit(data.tids, 10, async (tid) => {
const canMove = await privileges.topics.isAdminOrMod(tid, socket.uid);
if (!canMove) {
throw new Error('[[error:no-privileges]]');
}
const topicData = await topics.getTopicFields(tid, ['tid', 'cid', 'slug', 'deleted']);
+ if (!cids.includes(topicData.cid)) {
+ cids.push(topicData.cid);
+ }
data.uid = socket.uid;
await topics.tools.move(tid, data);
@@ -45,6 +48,8 @@ module.exports = function (SocketTopics) {
toCid: data.cid,
});
});
+
+ await categories.onTopicsMoved(cids);
};
@@ -62,6 +67,7 @@ module.exports = function (SocketTopics) {
await async.eachLimit(tids, 50, async (tid) => {
await topics.tools.move(tid, data);
});
+ await categories.onTopicsMoved([data.currentCid, data.cid]);
await events.log({
type: `topic-move-all`,
uid: socket.uid,
diff --git a/src/topics/tools.js b/src/topics/tools.js
index 887f166247..be38f97209 100644
--- a/src/topics/tools.js
+++ b/src/topics/tools.js
@@ -274,10 +274,6 @@ module.exports = function (Topics) {
await categories.moveRecentReplies(tid, oldCid, cid);
await Promise.all([
- categories.incrementCategoryFieldBy(oldCid, 'topic_count', -1),
- categories.incrementCategoryFieldBy(cid, 'topic_count', 1),
- categories.updateRecentTidForCid(cid),
- categories.updateRecentTidForCid(oldCid),
Topics.setTopicFields(tid, {
cid: cid,
oldCid: oldCid,