diff --git a/src/topics/tools.js b/src/topics/tools.js index a7c22b5989..e0d33b53b2 100644 --- a/src/topics/tools.js +++ b/src/topics/tools.js @@ -274,10 +274,6 @@ module.exports = function (Topics) { function (next) { db.sortedSetAdd('cid:' + cid + ':tids:lastposttime', topic.lastposttime, tid, next); }, - function (next) { - var votes = (parseInt(topic.upvotes, 10) || 0) - (parseInt(topic.downvotes, 10) || 0); - db.sortedSetAdd('cid:' + cid + ':tids:votes', votes, tid, next); - }, function (next) { if (parseInt(topic.pinned, 10)) { db.sortedSetAdd('cid:' + cid + ':tids:pinned', Date.now(), tid, next); @@ -290,6 +286,10 @@ module.exports = function (Topics) { topic.postcount = topic.postcount || 0; db.sortedSetAdd('cid:' + cid + ':tids:posts', topic.postcount, tid, next); }, + function (next) { + var votes = (parseInt(topic.upvotes, 10) || 0) - (parseInt(topic.downvotes, 10) || 0); + db.sortedSetAdd('cid:' + cid + ':tids:votes', votes, tid, next); + }, ], function (err) { next(err); }); diff --git a/src/upgrades/1.8.0/fix_moved_topics_byvotes.js b/src/upgrades/1.8.0/fix_moved_topics_byvotes.js new file mode 100644 index 0000000000..f59d6f154c --- /dev/null +++ b/src/upgrades/1.8.0/fix_moved_topics_byvotes.js @@ -0,0 +1,53 @@ +'use strict'; + +var async = require('async'); +var batch = require('../../batch'); +var db = require('../../database'); + +module.exports = { + name: 'Fix sort by votes for moved topics', + timestamp: Date.UTC(2018, 0, 8), + method: function (callback) { + var progress = this.progress; + + batch.processSortedSet('topics:tid', function (tids, next) { + async.eachLimit(tids, 500, function (tid, _next) { + progress.incr(); + var topicData; + async.waterfall([ + function (next) { + db.getObjectFields('topic:' + tid, ['cid', 'oldCid', 'upvotes', 'downvotes', 'pinned'], next); + }, + function (_topicData, next) { + topicData = _topicData; + if (!topicData.cid || !topicData.oldCid) { + return _next(); + } + + var upvotes = parseInt(topicData.upvotes, 10) || 0; + var downvotes = parseInt(topicData.downvotes, 10) || 0; + var votes = upvotes - downvotes; + + async.parallel([ + function (next) { + db.sortedSetRemove('cid:' + topicData.oldCid + ':tids:votes', tid, next); + }, + function (next) { + if (!parseInt(topicData.pinned, 10)) { + db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', votes, tid, next); + } else { + next(); + } + }, + ], function (err) { + next(err); + }); + }, + ], _next); + }, next); + }, { + progress: progress, + batch: 500, + }, callback); + }, +};