12345678910111213141516171819202122232425262728293031323334353637 |
- Comments = new Mongo.Collection('comments');
- Meteor.methods({
- commentInsert: function (commentAttributes) {
- check(this.userId, String);
- check(commentAttributes, {
- postId: String,
- body: String
- });
- var user = Meteor.user();
- var post = Posts.findOne(commentAttributes.postId);
- if (!post) {
- throw new Meteor.Error('invalid-comment', 'You must comment on a post');
- }
- var comment = _.extend(commentAttributes, {
- userId: user._id,
- author: user.username,
- submitted: new Date()
- });
- // Update the post with the number of comments.
- Posts.update(comment.postId, {
- $inc: { commentsCount: 1 }
- });
- // Create the comment, save its id.
- comment._id = Comments.insert(comment);
- // Now create a notification, informing the post author that there's been a
- // comment created on their post.
- createCommentNotification(comment);
- return comment._id;
- }
- });
|