UNPKG

fraction.io

Version:

Your personalized community aggregator.

55 lines (45 loc) 1.05 kB
'use strict'; var countVotes = () => { // find votes for object var deltaVotesQuery = { delta: { $exists: true } }; var deltaVotes = Votes.find(deltaVotesQuery).fetch(); var voteTable = {}; // count how many votes each object has _.forEach(deltaVotes, function (vote) { var post = voteTable[vote.obj]; if (typeof post === 'undefined') { // first vote for this post voteTable[vote.obj] = vote.delta; } else { // not first vote for the post voteTable[vote.obj] += vote.delta; } Votes.update({ _id: vote._id }, { $unset : { delta: '' } }); }); var i = 0; _.forEach(voteTable, (value, key) => { Posts.update({_id: key}, { $inc: {votes: value}}); i++; }); if (i > 0) { var str = 'Applying ' + i + ' vote'; if (i > 1) { str += 's'; } console.log(str); } }; // run on startup Meteor.startup(countVotes); // run every 5 seconds Meteor.setInterval(countVotes, 5 * 1000);