Skip to content

Node.js based forum software built for the modern web

posts:votes sorted set

psychobunny86a8b8ab9304
diff --git a/src/posts.js b/src/posts.js
index 2f9132a25b..83b8e39a1f 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -248,6 +248,9 @@ var plugins = require('./plugins');
},
], next);
},
+ function (next) {
+ db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next);
+ },
function (next) {
Posts.setPostFields(postData.pid, { upvotes: postData.upvotes, downvotes: postData.downvotes }, next);
},
diff --git a/src/upgrade.js b/src/upgrade.js
index 46075ff5e9..14b394efb1 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -12,7 +12,7 @@ var schemaDate;
var thisSchemaDate;
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
-var latestSchema = Date.UTC(2017, 1, 25);
+var latestSchema = Date.UTC(2017, 1, 27);
Upgrade.check = function (callback) {
db.get('schemaDate', function (err, value) {
@@ -479,6 +479,50 @@ Upgrade.upgrade = function (callback) {
next();
}
},
+ function (next) {
+ thisSchemaDate = Date.UTC(2017, 1, 27);
+ var schemaName = '[2017/2/27] New sorted set posts:votes';
+
+ if (schemaDate < thisSchemaDate) {
+ updatesMade = true;
+ winston.verbose(schemaName);
+
+ db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
+ if (err) {
+ return next(err);
+ }
+
+ async.eachSeries(cids, function (cid, next) {
+ db.getSortedSetRevRange('cid:' + cid + ':pids', 0, -1, function (err, pids) {
+ if (err || !pids) {
+ return next(err);
+ }
+
+ async.each(pids, function(pid, next) {
+ db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) {
+ if (err || !postData) {
+ return next(err);
+ }
+
+ var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10);
+ db.sortedSetAdd('posts:votes', votes, pid, next);
+ });
+ }, next);
+ });
+ }, function (err) {
+ if (err) {
+ return next(err);
+ }
+
+ winston.info(schemaName + ' - done');
+ Upgrade.update(thisSchemaDate, next);
+ });
+ });
+ } else {
+ winston.info(schemaName + ' - skipped!');
+ next();
+ }
+ },
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!
], function (err) {