Permalink
Browse files

Added basic notifications collection.

And create a notification when a comment is created.

chapter11-1
  • Loading branch information...
tmeasday committed Apr 8, 2013
1 parent 767f0c5 commit a6a68b0e617031344d47465169ea5f53c6de179c
Showing with 34 additions and 3 deletions.
  1. +7 −1 lib/collections/comments.js
  2. +21 −0 lib/collections/notifications.js
  3. +1 −1 lib/router.js
  4. +5 −1 server/publications.js
@@ -23,6 +23,12 @@ Meteor.methods({
// update the post with the number of comments
Posts.update(comment.postId, {$inc: {commentsCount: 1}});
return Comments.insert(comment);
// create the comment, save the id
comment._id = Comments.insert(comment);
// now create a notification, informing the user that there's been a comment
createCommentNotification(comment);
return comment._id;
}
});
@@ -0,0 +1,21 @@
Notifications = new Mongo.Collection('notifications');
Notifications.allow({
update: function(userId, doc, fieldNames) {
return ownsDocument(userId, doc) &&
fieldNames.length === 1 && fieldNames[0] === 'read';
}
});
createCommentNotification = function(comment) {
var post = Posts.findOne(comment.postId);
if (comment.userId !== post.userId) {
Notifications.insert({
userId: post.userId,
postId: post._id,
commentId: comment._id,
commenterName: comment.author,
read: false
});
}
};
View
@@ -3,7 +3,7 @@ Router.configure({
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
waitOn: function() {
return Meteor.subscribe('posts');
return [Meteor.subscribe('posts'), Meteor.subscribe('notifications')]
}
});
View
@@ -5,4 +5,8 @@ Meteor.publish('posts', function() {
Meteor.publish('comments', function(postId) {
check(postId, String);
return Comments.find({postId: postId});
});
});
Meteor.publish('notifications', function() {
return Notifications.find();
})

0 comments on commit a6a68b0

Please sign in to comment.