comments.js 956 B

12345678910111213141516171819202122232425262728293031323334353637
  1. Comments = new Mongo.Collection('comments');
  2. Meteor.methods({
  3. commentInsert: function (commentAttributes) {
  4. check(this.userId, String);
  5. check(commentAttributes, {
  6. postId: String,
  7. body: String
  8. });
  9. var user = Meteor.user();
  10. var post = Posts.findOne(commentAttributes.postId);
  11. if (!post) {
  12. throw new Meteor.Error('invalid-comment', 'You must comment on a post');
  13. }
  14. var comment = _.extend(commentAttributes, {
  15. userId: user._id,
  16. author: user.username,
  17. submitted: new Date()
  18. });
  19. // Update the post with the number of comments.
  20. Posts.update(comment.postId, {
  21. $inc: { commentsCount: 1 }
  22. });
  23. // Create the comment, save its id.
  24. comment._id = Comments.insert(comment);
  25. // Now create a notification, informing the post author that there's been a
  26. // comment created on their post.
  27. createCommentNotification(comment);
  28. return comment._id;
  29. }
  30. });