Vulcan/server/vote.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2012-09-11 15:32:25 +10:00
// TODO: should the baseScore be stored, and updated at vote time?
// This interface should change and become more OO, this'll do for now
var Scoring = {
// re-run the scoring algorithm on a single object
updateObject: function(object) {
// just count the number of votes for now
var baseScore = MyVotes.find({votedFor: object._id}).count();
// now multiply by 'age' exponentiated
// FIXME: timezones <-- set by server or is getTime() ok?
var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000);
object.score = baseScore * Math.pow(ageInHours + 2, -0.1375);
},
// rerun all the scoring
updateScores: function() {
Posts.find().forEach(function(post) {
Scoring.updateObject(post);
Posts.update(post._id, {$set: {score: post.score}});
});
}
}
2012-08-30 21:35:48 -04:00
Meteor.methods({
voteForPost: function(post){
var user = this.userId();
if(!user) return false;
2012-09-11 15:32:25 +10:00
var myvote = MyVotes.findOne({votedFor: post._id, user: user});
2012-08-30 21:35:48 -04:00
if(myvote) return false;
2012-09-11 15:32:25 +10:00
MyVotes.insert({votedFor: post._id, user: user, vote: 1});
Scoring.updateObject(post);
Posts.update(post._id, {$set: {score: post.score}});
2012-08-30 21:35:48 -04:00
return true;
}
});