2017-12-08 19:58:12 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var batch = require('../../batch');
|
|
|
|
|
var db = require('../../database');
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
name: 'Add votes to topics',
|
|
|
|
|
timestamp: Date.UTC(2017, 11, 8),
|
|
|
|
|
method: function (callback) {
|
|
|
|
|
var progress = this.progress;
|
|
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
batch.processSortedSet('topics:tid', (tids, next) => {
|
|
|
|
|
async.eachLimit(tids, 500, (tid, _next) => {
|
2017-12-08 19:58:12 -05:00
|
|
|
progress.incr();
|
|
|
|
|
var topicData;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2021-02-03 23:59:08 -07:00
|
|
|
db.getObjectFields(`topic:${tid}`, ['mainPid', 'cid', 'pinned'], next);
|
2017-12-08 19:58:12 -05:00
|
|
|
},
|
|
|
|
|
function (_topicData, next) {
|
|
|
|
|
topicData = _topicData;
|
|
|
|
|
if (!topicData.mainPid || !topicData.cid) {
|
|
|
|
|
return _next();
|
|
|
|
|
}
|
2021-02-03 23:59:08 -07:00
|
|
|
db.getObject(`post:${topicData.mainPid}`, next);
|
2017-12-08 19:58:12 -05:00
|
|
|
},
|
|
|
|
|
function (postData, next) {
|
|
|
|
|
if (!postData) {
|
|
|
|
|
return _next();
|
|
|
|
|
}
|
|
|
|
|
var upvotes = parseInt(postData.upvotes, 10) || 0;
|
|
|
|
|
var downvotes = parseInt(postData.downvotes, 10) || 0;
|
|
|
|
|
var data = {
|
|
|
|
|
upvotes: upvotes,
|
|
|
|
|
downvotes: downvotes,
|
|
|
|
|
};
|
|
|
|
|
var votes = upvotes - downvotes;
|
|
|
|
|
async.parallel([
|
|
|
|
|
function (next) {
|
2021-02-03 23:59:08 -07:00
|
|
|
db.setObject(`topic:${tid}`, data, next);
|
2017-12-08 19:58:12 -05:00
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
db.sortedSetAdd('topics:votes', votes, tid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
2018-01-08 12:03:02 -05:00
|
|
|
if (parseInt(topicData.pinned, 10) !== 1) {
|
2021-02-03 23:59:08 -07:00
|
|
|
db.sortedSetAdd(`cid:${topicData.cid}:tids:votes`, votes, tid, next);
|
2018-01-08 12:03:02 -05:00
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2017-12-08 19:58:12 -05:00
|
|
|
},
|
2021-02-04 00:01:39 -07:00
|
|
|
], (err) => {
|
2017-12-08 19:58:12 -05:00
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], _next);
|
|
|
|
|
}, next);
|
|
|
|
|
}, {
|
|
|
|
|
progress: progress,
|
|
|
|
|
batch: 500,
|
|
|
|
|
}, callback);
|
|
|
|
|
},
|
|
|
|
|
};
|