posts.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. Posts.deny({
  21. update: function (userId, post, fieldNames) {
  22. // _.without() is like PHP array_diff($source, ...$keys).
  23. return (_.without(fieldNames, "url", "title").length > 0);
  24. }
  25. });
  26. // This is in lib/ instead of server/ for latency compensation.
  27. Meteor.methods({
  28. postInsert: function(postAttributes) {
  29. "use strict";
  30. check(Meteor.userId(), String);
  31. check(postAttributes, {
  32. title: String,
  33. url: String
  34. });
  35. var postWithSameLink = Posts.findOne({ url: postAttributes.url });
  36. if (postWithSameLink) {
  37. // Return to skip the insert.
  38. return {
  39. postExists: true,
  40. _id: postWithSameLink._id
  41. };
  42. }
  43. var user = Meteor.user();
  44. var post = _.extend(postAttributes, {
  45. userId: user._id,
  46. author: user.username,
  47. submitted: new Date()
  48. });
  49. var postId = Posts.insert(post);
  50. return {
  51. _id: postId
  52. };
  53. }
  54. });