You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/upgrades/1.1.0/separate_upvote_downvote.js

51 lines
1.2 KiB
JavaScript

/* jslint node: true */
'use strict';
var db = require('../database');
var async = require('async');
var winston = require('winston');
module.exports = {
name: 'Store upvotes/downvotes separately',
timestamp: Date.UTC(2016, 5, 13),
method: function (callback) {
var batch = require('../batch');
var posts = require('../posts');
var count = 0;
batch.processSortedSet('posts:pid', function (pids, next) {
winston.verbose('upgraded ' + count + ' posts');
count += pids.length;
async.each(pids, function (pid, next) {
async.parallel({
upvotes: function (next) {
db.setCount('pid:' + pid + ':upvote', next);
},
downvotes: function (next) {
db.setCount('pid:' + pid + ':downvote', next);
},
}, function (err, results) {
if (err) {
return next(err);
}
var data = {};
if (parseInt(results.upvotes, 10) > 0) {
data.upvotes = results.upvotes;
}
if (parseInt(results.downvotes, 10) > 0) {
data.downvotes = results.downvotes;
}
if (Object.keys(data).length) {
posts.setPostFields(pid, data, next);
} else {
next();
}
}, next);
}, next);
}, {}, callback);
},
};