From 86a8b8ab9304e89da6c69e1dfec73d95cebf7d0c Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 27 Feb 2017 15:27:07 -0500 Subject: [PATCH] posts:votes sorted set --- src/posts.js | 3 +++ src/upgrade.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) 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) {