cardComments.js 7.9 KB

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