cardComments.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. function commentCreation(userId, doc){
  55. Activities.insert({
  56. userId,
  57. activityType: 'addComment',
  58. boardId: doc.boardId,
  59. cardId: doc.cardId,
  60. commentId: doc._id,
  61. });
  62. }
  63. if (Meteor.isServer) {
  64. // Comments are often fetched within a card, so we create an index to make these
  65. // queries more efficient.
  66. Meteor.startup(() => {
  67. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  68. });
  69. CardComments.after.insert((userId, doc) => {
  70. commentCreation(userId, doc);
  71. });
  72. CardComments.after.remove((userId, doc) => {
  73. const activity = Activities.findOne({ commentId: doc._id });
  74. if (activity) {
  75. Activities.remove(activity._id);
  76. }
  77. });
  78. }
  79. //CARD COMMENT REST API
  80. if (Meteor.isServer) {
  81. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function (req, res, next) {
  82. Authentication.checkUserId( req.userId);
  83. const paramBoardId = req.params.boardId;
  84. const paramCardId = req.params.cardId;
  85. JsonRoutes.sendResult(res, {
  86. code: 200,
  87. data: CardComments.find({ boardId: paramBoardId, cardId: paramCardId}).map(function (doc) {
  88. return {
  89. _id: doc._id,
  90. comment: doc.text,
  91. authorId: doc.userId,
  92. };
  93. }),
  94. });
  95. });
  96. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res, next) {
  97. Authentication.checkUserId( req.userId);
  98. const paramBoardId = req.params.boardId;
  99. const paramCommentId = req.params.commentId;
  100. const paramCardId = req.params.cardId;
  101. JsonRoutes.sendResult(res, {
  102. code: 200,
  103. data: CardComments.findOne({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId }),
  104. });
  105. });
  106. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/comments', function (req, res, next) {
  107. Authentication.checkUserId( req.userId);
  108. const paramBoardId = req.params.boardId;
  109. const paramCardId = req.params.cardId;
  110. const id = CardComments.direct.insert({
  111. userId: req.body.authorId,
  112. text: req.body.comment,
  113. cardId: paramCardId,
  114. boardId: paramBoardId,
  115. });
  116. JsonRoutes.sendResult(res, {
  117. code: 200,
  118. data: {
  119. _id: id,
  120. },
  121. });
  122. const cardComment = CardComments.findOne({_id: id, cardId:paramCardId, boardId: paramBoardId });
  123. commentCreation(req.body.authorId, cardComment);
  124. });
  125. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res, next) {
  126. Authentication.checkUserId( req.userId);
  127. const paramBoardId = req.params.boardId;
  128. const paramCommentId = req.params.commentId;
  129. const paramCardId = req.params.cardId;
  130. CardComments.remove({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId });
  131. JsonRoutes.sendResult(res, {
  132. code: 200,
  133. data: {
  134. _id: paramCardId,
  135. },
  136. });
  137. });
  138. }