cardComments.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. CardComments = new Mongo.Collection('card_comments');
  2. CardComments.attachSchema(new SimpleSchema({
  3. boardId: {
  4. type: String,
  5. },
  6. cardId: {
  7. type: String,
  8. },
  9. // XXX Rename in `content`? `text` is a bit vague...
  10. text: {
  11. type: String,
  12. },
  13. // XXX We probably don't need this information here, since we already have it
  14. // in the associated comment creation activity
  15. createdAt: {
  16. type: Date,
  17. denyUpdate: false,
  18. },
  19. // XXX Should probably be called `authorId`
  20. userId: {
  21. type: String,
  22. },
  23. }));
  24. CardComments.allow({
  25. insert(userId, doc) {
  26. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  27. },
  28. update(userId, doc) {
  29. return userId === doc.userId;
  30. },
  31. remove(userId, doc) {
  32. return userId === doc.userId;
  33. },
  34. fetch: ['userId', 'boardId'],
  35. });
  36. CardComments.helpers({
  37. user() {
  38. return Users.findOne(this.userId);
  39. },
  40. });
  41. CardComments.hookOptions.after.update = { fetchPrevious: false };
  42. CardComments.before.insert((userId, doc) => {
  43. doc.createdAt = new Date();
  44. doc.userId = userId;
  45. });
  46. if (Meteor.isServer) {
  47. CardComments.after.insert((userId, doc) => {
  48. Activities.insert({
  49. userId,
  50. activityType: 'addComment',
  51. boardId: doc.boardId,
  52. cardId: doc.cardId,
  53. commentId: doc._id,
  54. });
  55. });
  56. CardComments.after.remove((userId, doc) => {
  57. const activity = Activities.findOne({ commentId: doc._id });
  58. if (activity) {
  59. Activities.remove(activity._id);
  60. }
  61. });
  62. }