cardComments.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  76. //CARD COMMENT REST API
  77. if (Meteor.isServer) {
  78. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function (req, res, next) {
  79. Authentication.checkUserId( req.userId);
  80. const paramBoardId = req.params.boardId;
  81. const paramCardId = req.params.cardId;
  82. JsonRoutes.sendResult(res, {
  83. code: 200,
  84. data: CardComments.find({ boardId: paramBoardId, cardId: paramCardId}).map(function (doc) {
  85. return {
  86. _id: doc._id,
  87. comment: doc.text,
  88. authorId: doc.userId,
  89. };
  90. }),
  91. });
  92. });
  93. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res, next) {
  94. Authentication.checkUserId( req.userId);
  95. const paramBoardId = req.params.boardId;
  96. const paramCommentId = req.params.commentId;
  97. const paramCardId = req.params.cardId;
  98. JsonRoutes.sendResult(res, {
  99. code: 200,
  100. data: CardComments.findOne({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId }),
  101. });
  102. });
  103. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/comments', function (req, res, next) {
  104. Authentication.checkUserId( req.userId);
  105. const paramBoardId = req.params.boardId;
  106. const paramCardId = req.params.cardId;
  107. const id = CardComments.insert({
  108. userId: req.body.authorId,
  109. text: req.body.comment,
  110. cardId: paramCardId,
  111. boardId: paramBoardId,
  112. });
  113. JsonRoutes.sendResult(res, {
  114. code: 200,
  115. data: {
  116. _id: id,
  117. },
  118. });
  119. });
  120. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res, next) {
  121. Authentication.checkUserId( req.userId);
  122. const paramBoardId = req.params.boardId;
  123. const paramCommentId = req.params.commentId;
  124. const paramCardId = req.params.cardId;
  125. CardComments.remove({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId });
  126. JsonRoutes.sendResult(res, {
  127. code: 200,
  128. data: {
  129. _id: paramCardId,
  130. },
  131. });
  132. });
  133. }