2
0

cardComments.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if (Meteor.isServer) {
  104. // Comments are often fetched within a card, so we create an index to make these
  105. // queries more efficient.
  106. Meteor.startup(() => {
  107. CardComments._collection._ensureIndex({ modifiedAt: -1 });
  108. CardComments._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  109. });
  110. CardComments.after.insert((userId, doc) => {
  111. commentCreation(userId, doc);
  112. });
  113. CardComments.after.update((userId, doc) => {
  114. const card = Cards.findOne(doc.cardId);
  115. Activities.insert({
  116. userId,
  117. activityType: 'editComment',
  118. boardId: doc.boardId,
  119. cardId: doc.cardId,
  120. commentId: doc._id,
  121. listId: card.listId,
  122. swimlaneId: card.swimlaneId,
  123. });
  124. });
  125. CardComments.before.remove((userId, doc) => {
  126. const card = Cards.findOne(doc.cardId);
  127. Activities.insert({
  128. userId,
  129. activityType: 'deleteComment',
  130. boardId: doc.boardId,
  131. cardId: doc.cardId,
  132. commentId: doc._id,
  133. listId: card.listId,
  134. swimlaneId: card.swimlaneId,
  135. });
  136. });
  137. CardComments.after.remove((userId, doc) => {
  138. const activity = Activities.findOne({ commentId: doc._id });
  139. if (activity) {
  140. Activities.remove(activity._id);
  141. }
  142. });
  143. }
  144. //CARD COMMENT REST API
  145. if (Meteor.isServer) {
  146. /**
  147. * @operation get_all_comments
  148. * @summary Get all comments attached to a card
  149. *
  150. * @param {string} boardId the board ID of the card
  151. * @param {string} cardId the ID of the card
  152. * @return_type [{_id: string,
  153. * comment: string,
  154. * authorId: string}]
  155. */
  156. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/comments', function(
  157. req,
  158. res,
  159. ) {
  160. try {
  161. Authentication.checkUserId(req.userId);
  162. const paramBoardId = req.params.boardId;
  163. const paramCardId = req.params.cardId;
  164. JsonRoutes.sendResult(res, {
  165. code: 200,
  166. data: CardComments.find({
  167. boardId: paramBoardId,
  168. cardId: paramCardId,
  169. }).map(function(doc) {
  170. return {
  171. _id: doc._id,
  172. comment: doc.text,
  173. authorId: doc.userId,
  174. };
  175. }),
  176. });
  177. } catch (error) {
  178. JsonRoutes.sendResult(res, {
  179. code: 200,
  180. data: error,
  181. });
  182. }
  183. });
  184. /**
  185. * @operation get_comment
  186. * @summary Get a comment on a card
  187. *
  188. * @param {string} boardId the board ID of the card
  189. * @param {string} cardId the ID of the card
  190. * @param {string} commentId the ID of the comment to retrieve
  191. * @return_type CardComments
  192. */
  193. JsonRoutes.add(
  194. 'GET',
  195. '/api/boards/:boardId/cards/:cardId/comments/:commentId',
  196. function(req, res) {
  197. try {
  198. Authentication.checkUserId(req.userId);
  199. const paramBoardId = req.params.boardId;
  200. const paramCommentId = req.params.commentId;
  201. const paramCardId = req.params.cardId;
  202. JsonRoutes.sendResult(res, {
  203. code: 200,
  204. data: CardComments.findOne({
  205. _id: paramCommentId,
  206. cardId: paramCardId,
  207. boardId: paramBoardId,
  208. }),
  209. });
  210. } catch (error) {
  211. JsonRoutes.sendResult(res, {
  212. code: 200,
  213. data: error,
  214. });
  215. }
  216. },
  217. );
  218. /**
  219. * @operation new_comment
  220. * @summary Add a comment on a card
  221. *
  222. * @param {string} boardId the board ID of the card
  223. * @param {string} cardId the ID of the card
  224. * @param {string} authorId the user who 'posted' the comment
  225. * @param {string} text the content of the comment
  226. * @return_type {_id: string}
  227. */
  228. JsonRoutes.add(
  229. 'POST',
  230. '/api/boards/:boardId/cards/:cardId/comments',
  231. function(req, res) {
  232. try {
  233. Authentication.checkUserId(req.userId);
  234. const paramBoardId = req.params.boardId;
  235. const paramCardId = req.params.cardId;
  236. const id = CardComments.direct.insert({
  237. userId: req.body.authorId,
  238. text: req.body.comment,
  239. cardId: paramCardId,
  240. boardId: paramBoardId,
  241. });
  242. JsonRoutes.sendResult(res, {
  243. code: 200,
  244. data: {
  245. _id: id,
  246. },
  247. });
  248. const cardComment = CardComments.findOne({
  249. _id: id,
  250. cardId: paramCardId,
  251. boardId: paramBoardId,
  252. });
  253. commentCreation(req.body.authorId, cardComment);
  254. } catch (error) {
  255. JsonRoutes.sendResult(res, {
  256. code: 200,
  257. data: error,
  258. });
  259. }
  260. },
  261. );
  262. /**
  263. * @operation delete_comment
  264. * @summary Delete a comment on a card
  265. *
  266. * @param {string} boardId the board ID of the card
  267. * @param {string} cardId the ID of the card
  268. * @param {string} commentId the ID of the comment to delete
  269. * @return_type {_id: string}
  270. */
  271. JsonRoutes.add(
  272. 'DELETE',
  273. '/api/boards/:boardId/cards/:cardId/comments/:commentId',
  274. function(req, res) {
  275. try {
  276. Authentication.checkUserId(req.userId);
  277. const paramBoardId = req.params.boardId;
  278. const paramCommentId = req.params.commentId;
  279. const paramCardId = req.params.cardId;
  280. CardComments.remove({
  281. _id: paramCommentId,
  282. cardId: paramCardId,
  283. boardId: paramBoardId,
  284. });
  285. JsonRoutes.sendResult(res, {
  286. code: 200,
  287. data: {
  288. _id: paramCardId,
  289. },
  290. });
  291. } catch (error) {
  292. JsonRoutes.sendResult(res, {
  293. code: 200,
  294. data: error,
  295. });
  296. }
  297. },
  298. );
  299. }
  300. export default CardComments;