cardComments.js 10 KB

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