Files
NodeBB/src/upgrades/1.7.3/topic_votes.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-12-08 19:58:12 -05:00
'use strict';
2021-02-04 00:06:15 -07:00
const async = require('async');
const batch = require('../../batch');
const db = require('../../database');
2017-12-08 19:58:12 -05:00
module.exports = {
name: 'Add votes to topics',
timestamp: Date.UTC(2017, 11, 8),
method: function (callback) {
2021-02-06 14:10:15 -07:00
const { progress } = this;
2017-12-08 19:58:12 -05:00
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();
2021-02-04 00:06:15 -07:00
let topicData;
2017-12-08 19:58:12 -05:00
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();
}
2021-02-04 00:06:15 -07:00
const upvotes = parseInt(postData.upvotes, 10) || 0;
const downvotes = parseInt(postData.downvotes, 10) || 0;
const data = {
2017-12-08 19:58:12 -05:00
upvotes: upvotes,
downvotes: downvotes,
};
2021-02-04 00:06:15 -07:00
const votes = upvotes - downvotes;
2017-12-08 19:58:12 -05:00
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);
},
};