Skip to content

Commit ae3664f

Browse files
committed
Merge branch 'master' of github.com:TelescopeJS/Telescope
2 parents 46106d4 + 1cced6d commit ae3664f

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

lib/vote.js

+66-61
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,38 @@
4949
cancelDownvote(collection, item, user);
5050

5151
// Votes & Score
52-
var result = collection.update({_id: item && item._id},{
52+
var result = collection.update({_id: item && item._id, upvoters: {$not: {$in: [user._id]}}},{
5353
$addToSet: {upvoters: user._id},
5454
$inc: {upvotes: 1, baseScore: votePower},
5555
$set: {inactive: false}
5656
});
5757

58-
// Add item to list of upvoted items
59-
var vote = {
60-
itemId: item._id,
61-
votedAt: new Date(),
62-
power: votePower
63-
};
64-
addVote(user._id, vote, collectionName, 'up');
65-
66-
// extend item with baseScore to help calculate newScore
67-
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
68-
updateScore({collection: collection, item: item, forceUpdate: true});
69-
70-
// if the item is being upvoted by its own author, don't give karma
71-
if (item.userId != user._id){
72-
modifyKarma(item.userId, votePower);
58+
if (result > 0) {
59+
// Add item to list of upvoted items
60+
var vote = {
61+
itemId: item._id,
62+
votedAt: new Date(),
63+
power: votePower
64+
};
65+
addVote(user._id, vote, collectionName, 'up');
66+
67+
// extend item with baseScore to help calculate newScore
68+
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
69+
updateScore({collection: collection, item: item, forceUpdate: true});
70+
71+
// if the item is being upvoted by its own author, don't give karma
72+
if (item.userId != user._id){
73+
modifyKarma(item.userId, votePower);
7374

74-
// if karma redistribution is enabled, give karma to all previous upvoters of the post
75-
// (but not to the person doing the upvoting)
76-
if(getSetting('redistributeKarma', false)){
77-
_.each(item.upvoters, function(upvoterId){
78-
// share the karma equally among all upvoters, but cap the value at 0.1
79-
var karmaIncrease = Math.min(0.1, votePower/item.upvoters.length);
80-
modifyKarma(upvoterId, 0.1);
81-
});
75+
// if karma redistribution is enabled, give karma to all previous upvoters of the post
76+
// (but not to the person doing the upvoting)
77+
if(getSetting('redistributeKarma', false)){
78+
_.each(item.upvoters, function(upvoterId){
79+
// share the karma equally among all upvoters, but cap the value at 0.1
80+
var karmaIncrease = Math.min(0.1, votePower/item.upvoters.length);
81+
modifyKarma(upvoterId, 0.1);
82+
});
83+
}
8284
}
8385
}
8486
// console.log(collection.findOne(item._id));
@@ -98,28 +100,29 @@
98100
cancelUpvote(collection, item, user);
99101

100102
// Votes & Score
101-
collection.update({_id: item && item._id},{
103+
var result = collection.update({_id: item && item._id, downvoters: {$not: {$in: [user._id]}}},{
102104
$addToSet: {downvoters: user._id},
103105
$inc: {downvotes: 1, baseScore: -votePower},
104106
$set: {inactive: false}
105107
});
106108

107-
// Add item to list of downvoted items
108-
var vote = {
109-
itemId: item._id,
110-
votedAt: new Date(),
111-
power: votePower
112-
};
113-
addVote(user._id, vote, collectionName, 'down');
114-
115-
// extend item with baseScore to help calculate newScore
116-
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
117-
updateScore({collection: collection, item: item, forceUpdate: true});
118-
119-
// if the item is being upvoted by its own author, don't give karma
120-
if (item.userId != user._id)
121-
modifyKarma(item.userId, votePower);
122-
109+
if (result > 0) {
110+
// Add item to list of downvoted items
111+
var vote = {
112+
itemId: item._id,
113+
votedAt: new Date(),
114+
power: votePower
115+
};
116+
addVote(user._id, vote, collectionName, 'down');
117+
118+
// extend item with baseScore to help calculate newScore
119+
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
120+
updateScore({collection: collection, item: item, forceUpdate: true});
121+
122+
// if the item is being upvoted by its own author, don't give karma
123+
if (item.userId != user._id)
124+
modifyKarma(item.userId, votePower);
125+
}
123126
// console.log(collection.findOne(item._id));
124127
return true;
125128
};
@@ -134,23 +137,24 @@
134137
return false;
135138

136139
// Votes & Score
137-
collection.update({_id: item && item._id},{
140+
var result = collection.update({_id: item && item._id, upvoters: { $in: [user._id]}},{
138141
$pull: {upvoters: user._id},
139142
$inc: {upvotes: -1, baseScore: -votePower},
140143
$set: {inactive: false}
141144
});
142145

143-
// Remove item from list of upvoted items
144-
removeVote(user._id, item._id, collectionName, 'up');
146+
if (result > 0) {
147+
// Remove item from list of upvoted items
148+
removeVote(user._id, item._id, collectionName, 'up');
145149

146-
// extend item with baseScore to help calculate newScore
147-
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
148-
updateScore({collection: collection, item: item, forceUpdate: true});
150+
// extend item with baseScore to help calculate newScore
151+
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
152+
updateScore({collection: collection, item: item, forceUpdate: true});
149153

150-
// if the item is being upvoted by its own author, don't give karma
151-
if (item.userId != user._id)
152-
modifyKarma(item.userId, votePower);
153-
154+
// if the item is being upvoted by its own author, don't give karma
155+
if (item.userId != user._id)
156+
modifyKarma(item.userId, votePower);
157+
}
154158
// console.log(collection.findOne(item._id));
155159
return true;
156160
};
@@ -165,23 +169,24 @@
165169
return false;
166170

167171
// Votes & Score
168-
collection.update({_id: item && item._id},{
172+
var result = collection.update({_id: item && item._id, downvoters: {$in: [user._id]}},{
169173
$pull: {downvoters: user._id},
170174
$inc: {downvotes: 1, baseScore: votePower},
171175
$set: {inactive: false}
172176
});
173177

174-
// Remove item from list of downvoted items
175-
removeVote(user._id, item._id, collectionName, 'down');
178+
if (result > 0) {
179+
// Remove item from list of downvoted items
180+
removeVote(user._id, item._id, collectionName, 'down');
176181

177-
// extend item with baseScore to help calculate newScore
178-
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
179-
updateScore({collection: collection, item: item, forceUpdate: true});
182+
// extend item with baseScore to help calculate newScore
183+
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
184+
updateScore({collection: collection, item: item, forceUpdate: true});
180185

181-
// if the item is being upvoted by its own author, don't give karma
182-
if (item.userId != user._id)
183-
modifyKarma(item.userId, votePower);
184-
186+
// if the item is being upvoted by its own author, don't give karma
187+
if (item.userId != user._id)
188+
modifyKarma(item.userId, votePower);
189+
}
185190
// console.log(collection.findOne(item._id));
186191
return true;
187192
};

0 commit comments

Comments
 (0)