cardComments.js 10 KB

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