posts.js 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. check(Meteor.userId(), String);
  16. check(postAttributes, {
  17. title: String,
  18. url: String
  19. });
  20. var postWithSameLink = Posts.findOne({ url: postAttributes.url });
  21. if (postWithSameLink) {
  22. // Return to skip the insert.
  23. return {
  24. postExists: true,
  25. _id: postWithSameLink._id
  26. }
  27. }
  28. var user = Meteor.user();
  29. var post = _.extend(postAttributes, {
  30. userId: user._id,
  31. author: user.username,
  32. submitted: new Date()
  33. });
  34. var postId = Posts.insert(post);
  35. return {
  36. _id: postId
  37. };
  38. }
  39. });