refactoring vote.js

This commit is contained in:
Sacha Greif 2012-09-29 13:51:55 +09:00
parent a5d5be98ce
commit 75c8a86f4a
2 changed files with 57 additions and 55 deletions

View file

@ -13,115 +13,120 @@
modifyKarma(votedItem.user_id, karma);
};
var hasUpvotedItem= function(userId, collection, id){
// see http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
// 'is there an item with this id which does not contain the userId in its upvoters?'
// if such an item exists, it means we haven't voted yet.
// if it *doesn't* exist, it means we *have* voted
return collection.findOne({_id: id, upvoters: {$ne: userId}}) === undefined;
}
var hasDownvotedItem= function(userId, collection, id){
return collection.findOne({_id: id, downvoters: {$ne: userId}}) === undefined;
}
var upvote = function(collection, id) {
if (!this.userId())
if (!this.userId() || hasUpvotedItem(this.userId(), collection, id))
return false;
var votePower=getVotePower(this.userId());
// Is my userId already in up/downvoters?
var ack = (collection.findOne({_id: id, upvoters: {$ne: this.userId()}}) !== undefined);
// Votes & Score
// only modify the document if my userId isn't already in upvoters
collection.update({_id: id, upvoters: {$ne: this.userId()}},
{$push: {upvoters: this.userId()},
$inc: {votes: 1, baseScore: votePower}});
if (ack) {
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
}
if (!this.isSimulation)
updateScore(collection, id);
// Karma
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
return true;
};
var downvote = function(collection, id) {
if (!this.userId())
if (!this.userId() || hasDownvotedItem(this.userId(), collection, id))
return false;
var votePower=getVotePower(this.userId());
// Is my userId already in up/downvoters?
var ack = (collection.findOne({_id: id, downvoters: {$ne: this.userId()}}) !== undefined);
// Votes & Score
// only modify the document if my userId isn't already in downvoters
collection.update({_id: id, downvoters: {$ne: this.userId()}},
{$push: {downvoters: this.userId()},
$inc: {votes: -1, baseScore: -votePower}});
if (ack) {
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
}
if (!this.isSimulation)
updateScore(collection, id);
// Karma
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
return true;
};
var cancelUpvote = function(collection, id) {
if (!this.userId())
if (!this.userId() || !hasUpvotedItem(this.userId(), collection, id))
return false;
var votePower=getVotePower(this.userId());
// Is my userId already in up/downvoters?
var ack = (collection.findOne({_id: id, upvoters: {$ne: this.userId()}}) !== undefined);
// Votes & Score
// only modify the document if i am a recorded voter
collection.update({_id: id, upvoters: this.userId()},
{$pull: {upvoters: this.userId()},
$inc: {votes: -1, baseScore: -votePower}});
if (ack) {
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
}
if (!this.isSimulation)
updateScore(collection, id);
// Karma
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
return true;
};
var cancelDownvote = function(collection, id) {
if (!this.userId())
if (!this.userId() || !hasDownvotedItem(this.userId(), collection, id))
return false;
var votePower=getVotePower(this.userId());
// Is my userId already in up/downvoters?
var ack = (collection.findOne({_id: id, downvoters: {$ne: this.userId()}}) !== undefined);
// Votes & Score
// only modify the document if i am a recorded voter
collection.update({_id: id, downvoters: this.userId()},
{$pull: {downvoters: this.userId()},
$inc: {votes: 1, baseScore: votePower}});
if (ack) {
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
}
if (!this.isSimulation)
updateScore(collection, id);
// Karma
var votedItem = collection.findOne(id);
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
return true;
};

View file

@ -11,9 +11,6 @@ var updateScore = function (collection, id) {
// FIXME: timezones <-- set by server or is getTime() ok?
var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000);
// Bindle algorithm
// object.score = baseScore * Math.pow(ageInHours + 2, -0.1375);
// HN algorithm (same as Bindle)
var newScore = baseScore / Math.pow(ageInHours + 2, 1.3);