cardComments.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. copy(newCardId) {
  68. this.cardId = newCardId;
  69. delete this._id;
  70. CardComments.insert(this);
  71. },
  72. user() {
  73. return Users.findOne(this.userId);
  74. },
  75. });
  76. CardComments.hookOptions.after.update = { fetchPrevious: false };
  77. function commentCreation(userId, doc){
  78. Activities.insert({
  79. userId,
  80. activityType: 'addComment',
  81. boardId: doc.boardId,
  82. cardId: doc.cardId,
  83. commentId: doc._id,
  84. });
  85. }
  86. if (Meteor.isServer) {
  87. // Comments are often fetched within a card, so we create an index to make these
  88. // queries more efficient.
  89. Meteor.startup(() => {
  90. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  91. });
  92. CardComments.after.insert((userId, doc) => {
  93. commentCreation(userId, doc);
  94. });
  95. CardComments.after.remove((userId, doc) => {
  96. const activity = Activities.findOne({ commentId: doc._id });
  97. if (activity) {
  98. Activities.remove(activity._id);
  99. }
  100. });
  101. }
  102. //CARD COMMENT REST API
  103. if (Meteor.isServer) {
  104. /**
  105. * @operation get_all_comments
  106. * @summary Get all comments attached to a card
  107. *
  108. * @param {string} boardId the board ID of the card
  109. * @param {string} cardId the ID of the card
  110. * @return_type [{_id: string,
  111. * comment: string,
  112. * authorId: string}]
  113. */
  114. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function (req, res) {
  115. try {
  116. Authentication.checkUserId( req.userId);
  117. const paramBoardId = req.params.boardId;
  118. const paramCardId = req.params.cardId;
  119. JsonRoutes.sendResult(res, {
  120. code: 200,
  121. data: CardComments.find({ boardId: paramBoardId, cardId: paramCardId}).map(function (doc) {
  122. return {
  123. _id: doc._id,
  124. comment: doc.text,
  125. authorId: doc.userId,
  126. };
  127. }),
  128. });
  129. }
  130. catch (error) {
  131. JsonRoutes.sendResult(res, {
  132. code: 200,
  133. data: error,
  134. });
  135. }
  136. });
  137. /**
  138. * @operation get_comment
  139. * @summary Get a comment on a card
  140. *
  141. * @param {string} boardId the board ID of the card
  142. * @param {string} cardId the ID of the card
  143. * @param {string} commentId the ID of the comment to retrieve
  144. * @return_type CardComments
  145. */
  146. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  147. try {
  148. Authentication.checkUserId( req.userId);
  149. const paramBoardId = req.params.boardId;
  150. const paramCommentId = req.params.commentId;
  151. const paramCardId = req.params.cardId;
  152. JsonRoutes.sendResult(res, {
  153. code: 200,
  154. data: CardComments.findOne({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId }),
  155. });
  156. }
  157. catch (error) {
  158. JsonRoutes.sendResult(res, {
  159. code: 200,
  160. data: error,
  161. });
  162. }
  163. });
  164. /**
  165. * @operation new_comment
  166. * @summary Add a comment on a card
  167. *
  168. * @param {string} boardId the board ID of the card
  169. * @param {string} cardId the ID of the card
  170. * @param {string} authorId the user who 'posted' the comment
  171. * @param {string} text the content of the comment
  172. * @return_type {_id: string}
  173. */
  174. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/comments', function (req, res) {
  175. try {
  176. Authentication.checkUserId( req.userId);
  177. const paramBoardId = req.params.boardId;
  178. const paramCardId = req.params.cardId;
  179. const id = CardComments.direct.insert({
  180. userId: req.body.authorId,
  181. text: req.body.comment,
  182. cardId: paramCardId,
  183. boardId: paramBoardId,
  184. });
  185. JsonRoutes.sendResult(res, {
  186. code: 200,
  187. data: {
  188. _id: id,
  189. },
  190. });
  191. const cardComment = CardComments.findOne({_id: id, cardId:paramCardId, boardId: paramBoardId });
  192. commentCreation(req.body.authorId, cardComment);
  193. }
  194. catch (error) {
  195. JsonRoutes.sendResult(res, {
  196. code: 200,
  197. data: error,
  198. });
  199. }
  200. });
  201. /**
  202. * @operation delete_comment
  203. * @summary Delete a comment on a card
  204. *
  205. * @param {string} boardId the board ID of the card
  206. * @param {string} cardId the ID of the card
  207. * @param {string} commentId the ID of the comment to delete
  208. * @return_type {_id: string}
  209. */
  210. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/comments/:commentId', function (req, res) {
  211. try {
  212. Authentication.checkUserId( req.userId);
  213. const paramBoardId = req.params.boardId;
  214. const paramCommentId = req.params.commentId;
  215. const paramCardId = req.params.cardId;
  216. CardComments.remove({ _id: paramCommentId, cardId: paramCardId, boardId: paramBoardId });
  217. JsonRoutes.sendResult(res, {
  218. code: 200,
  219. data: {
  220. _id: paramCardId,
  221. },
  222. });
  223. }
  224. catch (error) {
  225. JsonRoutes.sendResult(res, {
  226. code: 200,
  227. data: error,
  228. });
  229. }
  230. });
  231. }