|
49 | 49 | cancelDownvote(collection, item, user);
|
50 | 50 |
|
51 | 51 | // 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]}}},{ |
53 | 53 | $addToSet: {upvoters: user._id},
|
54 | 54 | $inc: {upvotes: 1, baseScore: votePower},
|
55 | 55 | $set: {inactive: false}
|
56 | 56 | });
|
57 | 57 |
|
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); |
73 | 74 |
|
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 | + } |
82 | 84 | }
|
83 | 85 | }
|
84 | 86 | // console.log(collection.findOne(item._id));
|
|
98 | 100 | cancelUpvote(collection, item, user);
|
99 | 101 |
|
100 | 102 | // Votes & Score
|
101 |
| - collection.update({_id: item && item._id},{ |
| 103 | + var result = collection.update({_id: item && item._id, downvoters: {$not: {$in: [user._id]}}},{ |
102 | 104 | $addToSet: {downvoters: user._id},
|
103 | 105 | $inc: {downvotes: 1, baseScore: -votePower},
|
104 | 106 | $set: {inactive: false}
|
105 | 107 | });
|
106 | 108 |
|
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 | + } |
123 | 126 | // console.log(collection.findOne(item._id));
|
124 | 127 | return true;
|
125 | 128 | };
|
|
134 | 137 | return false;
|
135 | 138 |
|
136 | 139 | // Votes & Score
|
137 |
| - collection.update({_id: item && item._id},{ |
| 140 | + var result = collection.update({_id: item && item._id, upvoters: { $in: [user._id]}},{ |
138 | 141 | $pull: {upvoters: user._id},
|
139 | 142 | $inc: {upvotes: -1, baseScore: -votePower},
|
140 | 143 | $set: {inactive: false}
|
141 | 144 | });
|
142 | 145 |
|
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'); |
145 | 149 |
|
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}); |
149 | 153 |
|
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 | + } |
154 | 158 | // console.log(collection.findOne(item._id));
|
155 | 159 | return true;
|
156 | 160 | };
|
|
165 | 169 | return false;
|
166 | 170 |
|
167 | 171 | // Votes & Score
|
168 |
| - collection.update({_id: item && item._id},{ |
| 172 | + var result = collection.update({_id: item && item._id, downvoters: {$in: [user._id]}},{ |
169 | 173 | $pull: {downvoters: user._id},
|
170 | 174 | $inc: {downvotes: 1, baseScore: votePower},
|
171 | 175 | $set: {inactive: false}
|
172 | 176 | });
|
173 | 177 |
|
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'); |
176 | 181 |
|
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}); |
180 | 185 |
|
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 | + } |
185 | 190 | // console.log(collection.findOne(item._id));
|
186 | 191 | return true;
|
187 | 192 | };
|
|
0 commit comments