cardComments.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. autoValue() { // eslint-disable-line consistent-return
  19. if (this.isInsert) {
  20. return new Date();
  21. } else {
  22. this.unset();
  23. }
  24. },
  25. },
  26. // XXX Should probably be called `authorId`
  27. userId: {
  28. type: String,
  29. autoValue() { // eslint-disable-line consistent-return
  30. if (this.isInsert && !this.isSet) {
  31. return this.userId;
  32. }
  33. },
  34. },
  35. }));
  36. CardComments.allow({
  37. insert(userId, doc) {
  38. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  39. },
  40. update(userId, doc) {
  41. return userId === doc.userId;
  42. },
  43. remove(userId, doc) {
  44. return userId === doc.userId;
  45. },
  46. fetch: ['userId', 'boardId'],
  47. });
  48. CardComments.helpers({
  49. user() {
  50. return Users.findOne(this.userId);
  51. },
  52. });
  53. CardComments.hookOptions.after.update = { fetchPrevious: false };
  54. if (Meteor.isServer) {
  55. // Comments are often fetched within a card, so we create an index to make these
  56. // queries more efficient.
  57. Meteor.startup(() => {
  58. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  59. });
  60. CardComments.after.insert((userId, doc) => {
  61. Activities.insert({
  62. userId,
  63. activityType: 'addComment',
  64. boardId: doc.boardId,
  65. cardId: doc.cardId,
  66. commentId: doc._id,
  67. });
  68. });
  69. CardComments.after.remove((userId, doc) => {
  70. const activity = Activities.findOne({ commentId: doc._id });
  71. if (activity) {
  72. Activities.remove(activity._id);
  73. }
  74. });
  75. }