2017-12-08 19:58:12 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-09-23 14:56:20 -04:00
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
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),
|
2021-09-23 14:56:20 -04:00
|
|
|
method: async function () {
|
2021-02-06 14:10:15 -07:00
|
|
|
const { progress } = this;
|
2017-12-08 19:58:12 -05:00
|
|
|
|
2021-09-23 14:56:20 -04:00
|
|
|
batch.processSortedSet('topics:tid', async (tids) => {
|
|
|
|
|
await Promise.all(tids.map(async (tid) => {
|
2017-12-08 19:58:12 -05:00
|
|
|
progress.incr();
|
2021-09-23 14:56:20 -04:00
|
|
|
const topicData = await db.getObjectFields(`topic:${tid}`, ['mainPid', 'cid', 'pinned']);
|
|
|
|
|
if (topicData.mainPid && topicData.cid) {
|
|
|
|
|
const postData = await db.getObject(`post:${topicData.mainPid}`);
|
|
|
|
|
if (postData) {
|
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;
|
2021-09-23 14:56:20 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
db.setObject(`topic:${tid}`, data),
|
|
|
|
|
db.sortedSetAdd('topics:votes', votes, tid),
|
|
|
|
|
]);
|
|
|
|
|
if (parseInt(topicData.pinned, 10) !== 1) {
|
|
|
|
|
await db.sortedSetAdd(`cid:${topicData.cid}:tids:votes`, votes, tid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
2017-12-08 19:58:12 -05:00
|
|
|
}, {
|
|
|
|
|
progress: progress,
|
|
|
|
|
batch: 500,
|
2021-09-23 14:56:20 -04:00
|
|
|
});
|
2017-12-08 19:58:12 -05:00
|
|
|
},
|
|
|
|
|
};
|