mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
instead of updating counters one by one on each topic move, update them once after all topics are moved, use zcard instead of incr/decr
This commit is contained in:
committed by
GitHub
parent
01669fa54e
commit
5607e5bccb
@@ -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),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user