collections.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. });
  9. // Reject anonymous inserts.
  10. if (!userId) {
  11. throw new Meteor.Error("logged-out", "User must be logged in to post a site.");
  12. // return false;
  13. }
  14. // Reject non-new inserts.
  15. const url = doc.url;
  16. if (Websites.findOne({ url })) {
  17. throw new Meteor.Error("duplicate", "User may only post new sites.");
  18. // return false;
  19. }
  20. // Reject wrong-looking URLs
  21. // TODO: find a validation package usable server-side.
  22. // The popular themeteorchef:jquery-validation appears to be client-only.
  23. // For now using a very limited check.
  24. const URL_BOGO_REGEX = /^https?:\/\/.+$/;
  25. if (!URL_BOGO_REGEX.test(doc.url)) {
  26. throw new Meteor.Error("bad-url", "Users may only post http(s) URLs.");
  27. // return false;
  28. }
  29. // Reject empty titles and descriptions.
  30. if (doc.title === "") {
  31. throw new Meteor.Error("empty-title", "Title may not be empty");
  32. // return false;
  33. }
  34. if (doc.description === "") {
  35. throw new Meteor.Error("empty-description", "Description may not be empty");
  36. // return false;
  37. }
  38. return true;
  39. }
  40. });