12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Meteor } from 'meteor/meteor';
  2. import { Mongo } from 'meteor/mongo';
  3. import { check } from 'meteor/check';
  4. export const Tasks = new Mongo.Collection('tasks');
  5. if (Meteor.isServer) {
  6. // This code only runs on the server
  7. // Only publish tasks that are public or belong to the current user
  8. Meteor.publish('tasks', function tasksPublication() {
  9. return Tasks.find({
  10. $or: [
  11. { private: { $ne: true } },
  12. { owner: this.userId },
  13. ],
  14. });
  15. });
  16. }
  17. Meteor.methods({
  18. 'tasks.insert'(text) {
  19. check(text, String);
  20. // Make sure the user is logged in before inserting a task
  21. if (! Meteor.userId()) {
  22. throw new Meteor.Error('not-authorized');
  23. }
  24. Tasks.insert({
  25. text,
  26. createdAt: new Date(),
  27. owner: Meteor.userId(),
  28. username: Meteor.user().username,
  29. });
  30. },
  31. 'tasks.remove'(taskId) {
  32. check(taskId, String);
  33. const task = Tasks.findOne(taskId);
  34. if (task.public || !task.private && task.owner !== Meteor.userId()) {
  35. // If the task is private, make sure only the owner can delete it
  36. throw new Meteor.Error('not-authorized');
  37. }
  38. Tasks.remove(taskId);
  39. },
  40. 'tasks.setChecked'(taskId, setChecked) {
  41. check(taskId, String);
  42. check(setChecked, Boolean);
  43. const task = Tasks.findOne(taskId);
  44. if (task.private || !task.public && task.owner !== Meteor.userId()) {
  45. // If the task is private, make sure only the owner can check it off
  46. throw new Meteor.Error('not-authorized');
  47. }
  48. Tasks.update(taskId, { $set: { checked: setChecked } });
  49. },
  50. 'tasks.setPrivate'(taskId, setToPrivate) {
  51. check(taskId, String);
  52. check(setToPrivate, Boolean);
  53. const task = Tasks.findOne(taskId);
  54. // Make sure only the task owner can make a task private
  55. if (task.owner !== Meteor.userId()) {
  56. throw new Meteor.Error('not-authorized');
  57. }
  58. Tasks.update(taskId, { $set: { private: setToPrivate } });
  59. },
  60. });