posts.js 943 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 30/08/15
  7. * Time: 10:48
  8. */
  9. // Not a "var", to make it global.
  10. Posts = new Mongo.Collection('posts');
  11. // Removed Posts.allow : we no longer trigger inserts from client.
  12. // This is in lib/ instead of server/ for latency compensation (?).
  13. Meteor.methods({
  14. postInsert: function(postAttributes) {
  15. "use strict";
  16. check(Meteor.userId(), String);
  17. check(postAttributes, {
  18. title: String,
  19. url: String
  20. });
  21. var postWithSameLink = Posts.findOne({ url: postAttributes.url });
  22. if (postWithSameLink) {
  23. // Return to skip the insert.
  24. return {
  25. postExists: true,
  26. _id: postWithSameLink._id
  27. };
  28. }
  29. var user = Meteor.user();
  30. var post = _.extend(postAttributes, {
  31. userId: user._id,
  32. author: user.username,
  33. submitted: new Date()
  34. });
  35. var postId = Posts.insert(post);
  36. return {
  37. _id: postId
  38. };
  39. }
  40. });