cardComments.js 6.2 KB

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