|
@@ -81,22 +81,23 @@ Meteor.methods({
|
|
|
_id: postId
|
|
|
};
|
|
|
},
|
|
|
+
|
|
|
upvote: function (postId) {
|
|
|
check(this.userId, Match.OneOf(null, String));
|
|
|
check(postId, String);
|
|
|
if (this.userId === null) {
|
|
|
throw new Meteor.Error('invalid', 'Not logged in');
|
|
|
}
|
|
|
- var post = Posts.findOne(postId);
|
|
|
- if (!post) {
|
|
|
- throw new Meteor.Error('invalid', 'Post not found');
|
|
|
- }
|
|
|
- if (_.include(post.upvoters, this.userId)) {
|
|
|
- throw new Meteor.Error('invalid', 'Already upvoted this post');
|
|
|
- }
|
|
|
- Posts.update(post._id, {
|
|
|
+
|
|
|
+ var affected = Posts.update({
|
|
|
+ _id: postId,
|
|
|
+ upvoters: { $ne: this.userId }
|
|
|
+ }, {
|
|
|
$addToSet: { upvoters: this.userId },
|
|
|
$inc: { votes: 1 }
|
|
|
});
|
|
|
+ if (!affected) {
|
|
|
+ throw new Meteor.Error('invalid', "You weren't able to upvote that post");
|
|
|
+ }
|
|
|
}
|
|
|
});
|