Browse Source

Commit 7-7: Enforce post URL uniqueness.

Frederic G. MARAND 9 years ago
parent
commit
45bb8d2a61
2 changed files with 13 additions and 0 deletions
  1. 3 0
      client/templates/posts/post_submit.js
  2. 10 0
      lib/collections/posts.js

+ 3 - 0
client/templates/posts/post_submit.js

@@ -21,6 +21,9 @@ Template.postSubmit.events({
       if (error) {
         return alert(error);
       }
+      if (result.postExists) {
+        alert("This link has already been posted");
+      }
 
       Router.go('postPage', { _id: result._id });
     });

+ 10 - 0
lib/collections/posts.js

@@ -20,6 +20,16 @@ Meteor.methods({
       title: String,
       url: String
     });
+
+    var postWithSameLink = Posts.findOne({ url: postAttributes.url });
+    if (postWithSameLink) {
+      // Return to skip the insert.
+      return {
+        postExists: true,
+        _id: postWithSameLink._id
+      }
+    }
+
     var user = Meteor.user();
     var post = _.extend(postAttributes, {
       userId: user._id,