posts.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. Posts.allow({
  13. update: function (userId, post) {
  14. return ownsDocument(userId, post);
  15. },
  16. remove: function (userId, post) {
  17. return ownsDocument(userId, post);
  18. }
  19. });
  20. // This is in lib/ instead of server/ for latency compensation.
  21. Meteor.methods({
  22. postInsert: function(postAttributes) {
  23. "use strict";
  24. check(Meteor.userId(), String);
  25. check(postAttributes, {
  26. title: String,
  27. url: String
  28. });
  29. var postWithSameLink = Posts.findOne({ url: postAttributes.url });
  30. if (postWithSameLink) {
  31. // Return to skip the insert.
  32. return {
  33. postExists: true,
  34. _id: postWithSameLink._id
  35. };
  36. }
  37. var user = Meteor.user();
  38. var post = _.extend(postAttributes, {
  39. userId: user._id,
  40. author: user.username,
  41. submitted: new Date()
  42. });
  43. var postId = Posts.insert(post);
  44. return {
  45. _id: postId
  46. };
  47. }
  48. });