Browse Source

Commit 11-1: Added basic notifications collection.

Frederic G. MARAND 8 years ago
parent
commit
c4bc9aadd9
5 changed files with 50 additions and 2 deletions
  1. 2 0
      .jshintrc
  2. 8 1
      lib/collections/comments.js
  3. 32 0
      lib/collections/notifications.js
  4. 4 1
      lib/router.js
  5. 4 0
      server/publications.js

+ 2 - 0
.jshintrc

@@ -135,9 +135,11 @@
       "Comments": true,
       "Errors": true,
       "Mongo": true,
+      "Notifications": true,
       "Posts": true,
 
       // - Helpers
+      "createCommentNotification": true,
       "ownsDocument": true,
       "throwError": true,
       "validatePost": true,

+ 8 - 1
lib/collections/comments.js

@@ -30,6 +30,13 @@ Meteor.methods({
       $inc: { commentsCount: 1 }
     });
 
-    return Comments.insert(comment);
+    // Create the comment, save its id.
+    comment._id = Comment.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;
   }
 });

+ 32 - 0
lib/collections/notifications.js

@@ -0,0 +1,32 @@
+/**
+ * @file
+ *
+ *
+ * User: marand
+ * Date: 07/09/15
+ * Time: 18:48
+ */
+
+// Notifications are sent to a post author when someone comments on their post.
+Notifications = new Mongo.Collection('notifications');
+
+Notifications.allow({
+  'update': function (userId, doc, fieldNames) {
+    // Only allow post author to update, and only the "read" field.
+    return ownsDocument(userId, doc) &&
+      fieldNames.length === 1 && fieldNames[0] === 'read';
+  }
+});
+
+createCommentNotification = function (comment) {
+  var post = Posts.findOne(comment.postId);
+  if (comment.userId !== post.userId) {
+    Notification.insert({
+      userId: post.userId,
+      postId: post._id,
+      commentId: comment._id,
+      commenterName: comment.author,
+      read: false
+    });
+  }
+};

+ 4 - 1
lib/router.js

@@ -12,7 +12,10 @@ Router.configure({
   loadingTemplate: "loading",
   notFoundTemplate: "notFound",
   waitOn: function () {
-    return Meteor.subscribe('posts');
+    return [
+      Meteor.subscribe('posts'),
+      Meteor.subscribe('notifications')
+    ];
   }
 });
 

+ 4 - 0
server/publications.js

@@ -17,3 +17,7 @@ Meteor.publish('comments', function(postId) {
     postId: postId
   });
 });
+
+Meteor.publish('notifications', function () {
+  return Notifications.find();
+});