collections.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Websites.allow({
  2. insert: function (userId, doc) {
  3. // Ensure sane arguments.
  4. check(doc, {
  5. url: String,
  6. title: String,
  7. description: String,
  8. poster: Object,
  9. postDate: Date,
  10. words: String
  11. });
  12. // Reject anonymous inserts.
  13. if (!userId) {
  14. throw new Meteor.Error("logged-out", "User must be logged in to post a site.");
  15. // return false;
  16. }
  17. // Reject non-new inserts.
  18. const url = doc.url;
  19. if (Websites.findOne({ url })) {
  20. throw new Meteor.Error("duplicate", "User may only post new sites.");
  21. // return false;
  22. }
  23. // Reject wrong-looking URLs
  24. // TODO: find a validation package usable server-side.
  25. // The popular themeteorchef:jquery-validation appears to be client-only.
  26. // For now using a very limited check.
  27. const URL_BOGO_REGEX = /^https?:\/\/.+$/;
  28. if (!URL_BOGO_REGEX.test(doc.url)) {
  29. throw new Meteor.Error("bad-url", "Users may only post http(s) URLs.");
  30. // return false;
  31. }
  32. // Reject empty titles and descriptions.
  33. if (doc.title === "") {
  34. throw new Meteor.Error("empty-title", "Title may not be empty");
  35. // return false;
  36. }
  37. if (doc.description === "") {
  38. throw new Meteor.Error("empty-description", "Description may not be empty");
  39. // return false;
  40. }
  41. return true;
  42. },
  43. /**
  44. * Access check for update operations. NOT SAFE: needs deeper modifier checks.
  45. *
  46. * @param {String} userId
  47. * The user attempting the modification.
  48. * @param {Object} doc
  49. * The original document to modify.
  50. * @param {Array} fields
  51. * The list of affected fields.
  52. * @param {Object} modifier
  53. * The MongoDB update modifier.
  54. * @returns {boolean}
  55. * True to allow update.
  56. */
  57. update: function (userId, doc, fields, modifier) {
  58. if (!userId) {
  59. throw new Meteor.Error("logged-out", "User must be logged to vote on a site.");
  60. }
  61. const orderedFields = fields.sort();
  62. if (!_.isEqual(orderedFields, ["minus", "minusScore"]) &&
  63. !_.isEqual(orderedFields, ["plus", "plusScore"]) &&
  64. !_.isEqual(orderedFields, ["minus", "minusScore", "plus", "plusScore"]) &&
  65. !_.isEqual(orderedFields, ["comments"])) {
  66. throw new Meteor.Error("invalid-field", "May only update minus[Score] and plus[Score] or comments.");
  67. }
  68. // FIXME : check modifier.
  69. return true;
  70. }
  71. });