cardComments.js 10 KB

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