cardComments.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. CardComments = new Mongo.Collection('card_comments');
  2. /**
  3. * A comment on a card
  4. */
  5. CardComments.attachSchema(new SimpleSchema({
  6. boardId: {
  7. /**
  8. * the board ID
  9. */
  10. type: String,
  11. },
  12. cardId: {
  13. /**
  14. * the card ID
  15. */
  16. type: String,
  17. },
  18. // XXX Rename in `content`? `text` is a bit vague...
  19. text: {
  20. /**
  21. * the text of the comment
  22. */
  23. type: String,
  24. },
  25. // XXX We probably don't need this information here, since we already have it
  26. // in the associated comment creation activity
  27. createdAt: {
  28. /**
  29. * when was the comment created
  30. */
  31. type: Date,
  32. denyUpdate: false,
  33. autoValue() { // eslint-disable-line consistent-return
  34. if (this.isInsert) {
  35. return new Date();
  36. } else {
  37. this.unset();
  38. }
  39. },
  40. },
  41. // XXX Should probably be called `authorId`
  42. userId: {
  43. /**
  44. * the author ID of the comment
  45. */
  46. type: String,
  47. autoValue() { // eslint-disable-line consistent-return
  48. if (this.isInsert && !this.isSet) {
  49. return this.userId;
  50. }
  51. },
  52. },
  53. }));
  54. CardComments.allow({
  55. insert(userId, doc) {
  56. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  57. },
  58. update(userId, doc) {
  59. return userId === doc.userId;
  60. },
  61. remove(userId, doc) {
  62. return userId === doc.userId;
  63. },
  64. fetch: ['userId', 'boardId'],
  65. });
  66. CardComments.helpers({
  67. user() {
  68. return Users.findOne(this.userId);
  69. },
  70. });
  71. CardComments.hookOptions.after.update = { fetchPrevious: false };
  72. function commentCreation(userId, doc){
  73. Activities.insert({
  74. userId,
  75. activityType: 'addComment',
  76. boardId: doc.boardId,
  77. cardId: doc.cardId,
  78. commentId: doc._id,
  79. });
  80. }
  81. if (Meteor.isServer) {
  82. // Comments are often fetched within a card, so we create an index to make these
  83. // queries more efficient.
  84. Meteor.startup(() => {
  85. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  86. });
  87. CardComments.after.insert((userId, doc) => {
  88. commentCreation(userId, doc);
  89. });
  90. CardComments.after.remove((userId, doc) => {
  91. const activity = Activities.findOne({ commentId: doc._id });
  92. if (activity) {
  93. Activities.remove(activity._id);
  94. }
  95. });
  96. }
  97. //CARD COMMENT REST API
  98. if (Meteor.isServer) {
  99. /**
  100. * @operation get_all_comments
  101. * @summary Get all comments attached to a card
  102. *
  103. * @param {string} boardId the board ID of the card
  104. * @param {string} cardId the ID of the card
  105. * @return_type [{_id: string,
  106. * comment: string,
  107. * authorId: string}]
  108. */
  109. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function (req, res) {
  110. try {
  111. Authentication.checkUserId( req.userId);
  112. const paramBoardId = req.params.boardId;
  113. const paramCardId = req.params.cardId;
  114. JsonRoutes.sendResult(res, {
  115. code: 200,
  116. data: CardComments.find({ boardId: paramBoardId, cardId: paramCardId}).map(function (doc) {
  117. return {
  118. _id: doc._id,
  119. comment: doc.text,
  120. authorId: doc.userId,
  121. };
  122. }),
  123. });
  124. }
  125. catch (error) {
  126. JsonRoutes.sendResult(res, {
  127. code: 200,
  128. data: error,
  129. });
  130. }
  131. });
  132. /**
  133. * @operation get_comment
  134. * @summary Get a comment on a card
  135. *
  136. * @param {string} boardId the board ID of the card
  137. * @param {string} cardId the ID of the card
  138. * @param {string} commentId the ID of the comment to retrieve
  139. * @return_type CardComments
  140. */
  141. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  142. try {
  143. Authentication.checkUserId( req.userId);
  144. const paramBoardId = req.params.boardId;
  145. const paramCommentId = req.params.commentId;
  146. const paramCardId = req.params.cardId;
  147. JsonRoutes.sendResult(res, {
  148. code: 200,
  149. data: CardComments.findOne({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId }),
  150. });
  151. }
  152. catch (error) {
  153. JsonRoutes.sendResult(res, {
  154. code: 200,
  155. data: error,
  156. });
  157. }
  158. });
  159. /**
  160. * @operation new_comment
  161. * @summary Add a comment on a card
  162. *
  163. * @param {string} boardId the board ID of the card
  164. * @param {string} cardId the ID of the card
  165. * @param {string} authorId the user who 'posted' the comment
  166. * @param {string} text the content of the comment
  167. * @return_type {_id: string}
  168. */
  169. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/comments', function (req, res) {
  170. try {
  171. Authentication.checkUserId( req.userId);
  172. const paramBoardId = req.params.boardId;
  173. const paramCardId = req.params.cardId;
  174. const id = CardComments.direct.insert({
  175. userId: req.body.authorId,
  176. text: req.body.comment,
  177. cardId: paramCardId,
  178. boardId: paramBoardId,
  179. });
  180. JsonRoutes.sendResult(res, {
  181. code: 200,
  182. data: {
  183. _id: id,
  184. },
  185. });
  186. const cardComment = CardComments.findOne({_id: id, cardId:paramCardId, boardId: paramBoardId });
  187. commentCreation(req.body.authorId, cardComment);
  188. }
  189. catch (error) {
  190. JsonRoutes.sendResult(res, {
  191. code: 200,
  192. data: error,
  193. });
  194. }
  195. });
  196. /**
  197. * @operation delete_comment
  198. * @summary Delete a comment on a card
  199. *
  200. * @param {string} boardId the board ID of the card
  201. * @param {string} cardId the ID of the card
  202. * @param {string} commentId the ID of the comment to delete
  203. * @return_type {_id: string}
  204. */
  205. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  206. try {
  207. Authentication.checkUserId( req.userId);
  208. const paramBoardId = req.params.boardId;
  209. const paramCommentId = req.params.commentId;
  210. const paramCardId = req.params.cardId;
  211. CardComments.remove({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId });
  212. JsonRoutes.sendResult(res, {
  213. code: 200,
  214. data: {
  215. _id: paramCardId,
  216. },
  217. });
  218. }
  219. catch (error) {
  220. JsonRoutes.sendResult(res, {
  221. code: 200,
  222. data: error,
  223. });
  224. }
  225. });
  226. }