posts.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. if (Meteor.isServer) {
  21. postAttributes.title += " (server)";
  22. Meteor._sleepForMs(5000);
  23. }
  24. else {
  25. postAttributes.title += " (client)";
  26. }
  27. var postWithSameLink = Posts.findOne({ url: postAttributes.url });
  28. if (postWithSameLink) {
  29. // Return to skip the insert.
  30. return {
  31. postExists: true,
  32. _id: postWithSameLink._id
  33. }
  34. }
  35. var user = Meteor.user();
  36. var post = _.extend(postAttributes, {
  37. userId: user._id,
  38. author: user.username,
  39. submitted: new Date()
  40. });
  41. var postId = Posts.insert(post);
  42. return {
  43. _id: postId
  44. };
  45. }
  46. });