cardComments.js 9.8 KB

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