2
0

cardComments.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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) {
  82. try {
  83. Authentication.checkUserId( req.userId);
  84. const paramBoardId = req.params.boardId;
  85. const paramCardId = req.params.cardId;
  86. JsonRoutes.sendResult(res, {
  87. code: 200,
  88. data: CardComments.find({ boardId: paramBoardId, cardId: paramCardId}).map(function (doc) {
  89. return {
  90. _id: doc._id,
  91. comment: doc.text,
  92. authorId: doc.userId,
  93. };
  94. }),
  95. });
  96. }
  97. catch (error) {
  98. JsonRoutes.sendResult(res, {
  99. code: 200,
  100. data: error,
  101. });
  102. }
  103. });
  104. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  105. try {
  106. Authentication.checkUserId( req.userId);
  107. const paramBoardId = req.params.boardId;
  108. const paramCommentId = req.params.commentId;
  109. const paramCardId = req.params.cardId;
  110. JsonRoutes.sendResult(res, {
  111. code: 200,
  112. data: CardComments.findOne({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId }),
  113. });
  114. }
  115. catch (error) {
  116. JsonRoutes.sendResult(res, {
  117. code: 200,
  118. data: error,
  119. });
  120. }
  121. });
  122. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/comments', function (req, res) {
  123. try {
  124. Authentication.checkUserId( req.userId);
  125. const paramBoardId = req.params.boardId;
  126. const paramCardId = req.params.cardId;
  127. const id = CardComments.direct.insert({
  128. userId: req.body.authorId,
  129. text: req.body.comment,
  130. cardId: paramCardId,
  131. boardId: paramBoardId,
  132. });
  133. JsonRoutes.sendResult(res, {
  134. code: 200,
  135. data: {
  136. _id: id,
  137. },
  138. });
  139. const cardComment = CardComments.findOne({_id: id, cardId:paramCardId, boardId: paramBoardId });
  140. commentCreation(req.body.authorId, cardComment);
  141. }
  142. catch (error) {
  143. JsonRoutes.sendResult(res, {
  144. code: 200,
  145. data: error,
  146. });
  147. }
  148. });
  149. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  150. try {
  151. Authentication.checkUserId( req.userId);
  152. const paramBoardId = req.params.boardId;
  153. const paramCommentId = req.params.commentId;
  154. const paramCardId = req.params.cardId;
  155. CardComments.remove({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId });
  156. JsonRoutes.sendResult(res, {
  157. code: 200,
  158. data: {
  159. _id: paramCardId,
  160. },
  161. });
  162. }
  163. catch (error) {
  164. JsonRoutes.sendResult(res, {
  165. code: 200,
  166. data: error,
  167. });
  168. }
  169. });
  170. }