cardComments.js 7.0 KB

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