cardComments.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. CardComments = new Mongo.Collection('card_comments');
  2. /**
  3. * A comment on a card
  4. */
  5. CardComments.attachSchema(
  6. new SimpleSchema({
  7. boardId: {
  8. /**
  9. * the board ID
  10. */
  11. type: String,
  12. },
  13. cardId: {
  14. /**
  15. * the card ID
  16. */
  17. type: String,
  18. },
  19. // XXX Rename in `content`? `text` is a bit vague...
  20. text: {
  21. /**
  22. * the text of the comment
  23. */
  24. type: String,
  25. },
  26. createdAt: {
  27. /**
  28. * when was the comment created
  29. */
  30. type: Date,
  31. denyUpdate: false,
  32. // eslint-disable-next-line consistent-return
  33. autoValue() {
  34. if (this.isInsert) {
  35. return new Date();
  36. } else if (this.isUpsert) {
  37. return { $setOnInsert: new Date() };
  38. } else {
  39. this.unset();
  40. }
  41. },
  42. },
  43. modifiedAt: {
  44. type: Date,
  45. denyUpdate: false,
  46. // eslint-disable-next-line consistent-return
  47. autoValue() {
  48. if (this.isInsert || this.isUpsert || this.isUpdate) {
  49. return new Date();
  50. } else {
  51. this.unset();
  52. }
  53. },
  54. },
  55. // XXX Should probably be called `authorId`
  56. userId: {
  57. /**
  58. * the author ID of the comment
  59. */
  60. type: String,
  61. // eslint-disable-next-line consistent-return
  62. autoValue() {
  63. if (this.isInsert && !this.isSet) {
  64. return this.userId;
  65. }
  66. },
  67. },
  68. }),
  69. );
  70. CardComments.allow({
  71. insert(userId, doc) {
  72. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  73. },
  74. update(userId, doc) {
  75. return userId === doc.userId;
  76. },
  77. remove(userId, doc) {
  78. return userId === doc.userId;
  79. },
  80. fetch: ['userId', 'boardId'],
  81. });
  82. CardComments.helpers({
  83. copy(newCardId) {
  84. this.cardId = newCardId;
  85. delete this._id;
  86. CardComments.insert(this);
  87. },
  88. user() {
  89. return Users.findOne(this.userId);
  90. },
  91. });
  92. CardComments.hookOptions.after.update = { fetchPrevious: false };
  93. function commentCreation(userId, doc) {
  94. const card = Cards.findOne(doc.cardId);
  95. Activities.insert({
  96. userId,
  97. activityType: 'addComment',
  98. boardId: doc.boardId,
  99. cardId: doc.cardId,
  100. commentId: doc._id,
  101. listId: card.listId,
  102. swimlaneId: card.swimlaneId,
  103. });
  104. }
  105. if (Meteor.isServer) {
  106. // Comments are often fetched within a card, so we create an index to make these
  107. // queries more efficient.
  108. Meteor.startup(() => {
  109. CardComments._collection._ensureIndex({ modifiedAt: -1 });
  110. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  111. });
  112. CardComments.after.insert((userId, doc) => {
  113. commentCreation(userId, doc);
  114. });
  115. CardComments.after.update((userId, doc) => {
  116. const card = Cards.findOne(doc.cardId);
  117. Activities.insert({
  118. userId,
  119. activityType: 'editComment',
  120. boardId: doc.boardId,
  121. cardId: doc.cardId,
  122. commentId: doc._id,
  123. listId: card.listId,
  124. swimlaneId: card.swimlaneId,
  125. });
  126. });
  127. CardComments.before.remove((userId, doc) => {
  128. const card = Cards.findOne(doc.cardId);
  129. Activities.insert({
  130. userId,
  131. activityType: 'deleteComment',
  132. boardId: doc.boardId,
  133. cardId: doc.cardId,
  134. commentId: doc._id,
  135. listId: card.listId,
  136. swimlaneId: card.swimlaneId,
  137. });
  138. });
  139. CardComments.after.remove((userId, doc) => {
  140. const activity = Activities.findOne({ commentId: doc._id });
  141. if (activity) {
  142. Activities.remove(activity._id);
  143. }
  144. });
  145. }
  146. //CARD COMMENT REST API
  147. if (Meteor.isServer) {
  148. /**
  149. * @operation get_all_comments
  150. * @summary Get all comments attached to a card
  151. *
  152. * @param {string} boardId the board ID of the card
  153. * @param {string} cardId the ID of the card
  154. * @return_type [{_id: string,
  155. * comment: string,
  156. * authorId: string}]
  157. */
  158. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function(
  159. req,
  160. res,
  161. ) {
  162. try {
  163. Authentication.checkUserId(req.userId);
  164. const paramBoardId = req.params.boardId;
  165. const paramCardId = req.params.cardId;
  166. JsonRoutes.sendResult(res, {
  167. code: 200,
  168. data: CardComments.find({
  169. boardId: paramBoardId,
  170. cardId: paramCardId,
  171. }).map(function(doc) {
  172. return {
  173. _id: doc._id,
  174. comment: doc.text,
  175. authorId: doc.userId,
  176. };
  177. }),
  178. });
  179. } catch (error) {
  180. JsonRoutes.sendResult(res, {
  181. code: 200,
  182. data: error,
  183. });
  184. }
  185. });
  186. /**
  187. * @operation get_comment
  188. * @summary Get a comment on a card
  189. *
  190. * @param {string} boardId the board ID of the card
  191. * @param {string} cardId the ID of the card
  192. * @param {string} commentId the ID of the comment to retrieve
  193. * @return_type CardComments
  194. */
  195. JsonRoutes.add(
  196. 'GET',
  197. '/api/boards/:boardId/cards/:cardId/comments/:commentId',
  198. function(req, res) {
  199. try {
  200. Authentication.checkUserId(req.userId);
  201. const paramBoardId = req.params.boardId;
  202. const paramCommentId = req.params.commentId;
  203. const paramCardId = req.params.cardId;
  204. JsonRoutes.sendResult(res, {
  205. code: 200,
  206. data: CardComments.findOne({
  207. _id: paramCommentId,
  208. cardId: paramCardId,
  209. boardId: paramBoardId,
  210. }),
  211. });
  212. } catch (error) {
  213. JsonRoutes.sendResult(res, {
  214. code: 200,
  215. data: error,
  216. });
  217. }
  218. },
  219. );
  220. /**
  221. * @operation new_comment
  222. * @summary Add a comment on a card
  223. *
  224. * @param {string} boardId the board ID of the card
  225. * @param {string} cardId the ID of the card
  226. * @param {string} authorId the user who 'posted' the comment
  227. * @param {string} text the content of the comment
  228. * @return_type {_id: string}
  229. */
  230. JsonRoutes.add(
  231. 'POST',
  232. '/api/boards/:boardId/cards/:cardId/comments',
  233. function(req, res) {
  234. try {
  235. Authentication.checkUserId(req.userId);
  236. const paramBoardId = req.params.boardId;
  237. const paramCardId = req.params.cardId;
  238. const id = CardComments.direct.insert({
  239. userId: req.body.authorId,
  240. text: req.body.comment,
  241. cardId: paramCardId,
  242. boardId: paramBoardId,
  243. });
  244. JsonRoutes.sendResult(res, {
  245. code: 200,
  246. data: {
  247. _id: id,
  248. },
  249. });
  250. const cardComment = CardComments.findOne({
  251. _id: id,
  252. cardId: paramCardId,
  253. boardId: paramBoardId,
  254. });
  255. commentCreation(req.body.authorId, cardComment);
  256. } catch (error) {
  257. JsonRoutes.sendResult(res, {
  258. code: 200,
  259. data: error,
  260. });
  261. }
  262. },
  263. );
  264. /**
  265. * @operation delete_comment
  266. * @summary Delete a comment on a card
  267. *
  268. * @param {string} boardId the board ID of the card
  269. * @param {string} cardId the ID of the card
  270. * @param {string} commentId the ID of the comment to delete
  271. * @return_type {_id: string}
  272. */
  273. JsonRoutes.add(
  274. 'DELETE',
  275. '/api/boards/:boardId/cards/:cardId/comments/:commentId',
  276. function(req, res) {
  277. try {
  278. Authentication.checkUserId(req.userId);
  279. const paramBoardId = req.params.boardId;
  280. const paramCommentId = req.params.commentId;
  281. const paramCardId = req.params.cardId;
  282. CardComments.remove({
  283. _id: paramCommentId,
  284. cardId: paramCardId,
  285. boardId: paramBoardId,
  286. });
  287. JsonRoutes.sendResult(res, {
  288. code: 200,
  289. data: {
  290. _id: paramCardId,
  291. },
  292. });
  293. } catch (error) {
  294. JsonRoutes.sendResult(res, {
  295. code: 200,
  296. data: error,
  297. });
  298. }
  299. },
  300. );
  301. }
  302. export default CardComments;