comment.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. module.exports = function(crowi, app) {
  2. const logger = require('@alias/logger')('growi:routes:comment');
  3. const Comment = crowi.model('Comment');
  4. const User = crowi.model('User');
  5. const Page = crowi.model('Page');
  6. const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
  7. const ApiResponse = require('../util/apiResponse');
  8. const globalNotificationService = crowi.getGlobalNotificationService();
  9. const { body } = require('express-validator/check');
  10. const mongoose = require('mongoose');
  11. const ObjectId = mongoose.Types.ObjectId;
  12. const actions = {};
  13. const api = {};
  14. actions.api = api;
  15. api.validators = {};
  16. /**
  17. * @api {get} /comments.get Get comments of the page of the revision
  18. * @apiName GetComments
  19. * @apiGroup Comment
  20. *
  21. * @apiParam {String} page_id Page Id.
  22. * @apiParam {String} revision_id Revision Id.
  23. */
  24. api.get = async function(req, res) {
  25. const pageId = req.query.page_id;
  26. const revisionId = req.query.revision_id;
  27. // check whether accessible
  28. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  29. if (!isAccessible) {
  30. return res.json(ApiResponse.error('Current user is not accessible to this page.'));
  31. }
  32. let fetcher = null;
  33. try {
  34. if (revisionId) {
  35. fetcher = Comment.getCommentsByRevisionId(revisionId);
  36. }
  37. else {
  38. fetcher = Comment.getCommentsByPageId(pageId);
  39. }
  40. }
  41. catch (err) {
  42. return res.json(ApiResponse.error(err));
  43. }
  44. const comments = await fetcher.populate(
  45. { path: 'creator', select: User.USER_PUBLIC_FIELDS, populate: User.IMAGE_POPULATION },
  46. );
  47. res.json(ApiResponse.success({ comments }));
  48. };
  49. api.validators.add = function() {
  50. const validator = [
  51. body('commentForm.page_id').exists(),
  52. body('commentForm.revision_id').exists(),
  53. body('commentForm.comment').exists(),
  54. body('commentForm.comment_position').isInt(),
  55. body('commentForm.is_markdown').isBoolean(),
  56. body('commentForm.replyTo').exists().custom((value) => {
  57. if (value === '') {
  58. return undefined;
  59. }
  60. return ObjectId(value);
  61. }),
  62. body('slackNotificationForm.isSlackEnabled').isBoolean().exists(),
  63. ];
  64. return validator;
  65. };
  66. /**
  67. * @api {post} /comments.add Post comment for the page
  68. * @apiName PostComment
  69. * @apiGroup Comment
  70. *
  71. * @apiParam {String} page_id Page Id.
  72. * @apiParam {String} revision_id Revision Id.
  73. * @apiParam {String} comment Comment body
  74. * @apiParam {Number} comment_position=-1 Line number of the comment
  75. */
  76. api.add = async function(req, res) {
  77. const { commentForm, slackNotificationForm } = req.body;
  78. const { validationResult } = require('express-validator/check');
  79. const errors = validationResult(req.body);
  80. if (!errors.isEmpty()) {
  81. // return res.json(ApiResponse.error('Invalid comment.'));
  82. // return res.status(422).json({ errors: errors.array() });
  83. return res.json(ApiResponse.error('コメントを入力してください。'));
  84. }
  85. const pageId = commentForm.page_id;
  86. const revisionId = commentForm.revision_id;
  87. const comment = commentForm.comment;
  88. const position = commentForm.comment_position || -1;
  89. const isMarkdown = commentForm.is_markdown;
  90. const replyTo = commentForm.replyTo;
  91. // check whether accessible
  92. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  93. if (!isAccessible) {
  94. return res.json(ApiResponse.error('Current user is not accessible to this page.'));
  95. }
  96. let createdComment;
  97. try {
  98. createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, isMarkdown, replyTo);
  99. await Comment.populate(createdComment, [
  100. { path: 'creator', model: 'User', select: User.USER_PUBLIC_FIELDS },
  101. ]);
  102. }
  103. catch (err) {
  104. logger.error(err);
  105. return res.json(ApiResponse.error(err));
  106. }
  107. // update page
  108. const page = await Page.findOneAndUpdate({ _id: pageId }, {
  109. lastUpdateUser: req.user,
  110. updatedAt: new Date(),
  111. });
  112. res.json(ApiResponse.success({ comment: createdComment }));
  113. const path = page.path;
  114. // global notification
  115. globalNotificationService.fire(GlobalNotificationSetting.EVENT.COMMENT, path, req.user, {
  116. comment: createdComment,
  117. });
  118. // slack notification
  119. if (slackNotificationForm.isSlackEnabled) {
  120. const user = await User.findUserByUsername(req.user.username);
  121. const channels = slackNotificationForm.slackChannels;
  122. if (channels) {
  123. page.updateSlackChannel(channels).catch((err) => {
  124. logger.error('Error occured in updating slack channels: ', err);
  125. });
  126. const promises = channels.split(',').map((chan) => {
  127. return crowi.slack.postComment(createdComment, user, chan, path);
  128. });
  129. Promise.all(promises)
  130. .catch((err) => {
  131. logger.error('Error occured in sending slack notification: ', err);
  132. });
  133. }
  134. }
  135. };
  136. /**
  137. * @api {post} /comments.remove Remove specified comment
  138. * @apiName RemoveComment
  139. * @apiGroup Comment
  140. *
  141. * @apiParam {String} comment_id Comment Id.
  142. */
  143. api.remove = async function(req, res) {
  144. const commentId = req.body.comment_id;
  145. if (!commentId) {
  146. return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
  147. }
  148. try {
  149. const comment = await Comment.findById(commentId).exec();
  150. if (comment == null) {
  151. throw new Error('This comment does not exist.');
  152. }
  153. // check whether accessible
  154. const pageId = comment.page;
  155. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  156. if (!isAccessible) {
  157. throw new Error('Current user is not accessible to this page.');
  158. }
  159. await comment.removeWithReplies();
  160. await Page.updateCommentCount(comment.page);
  161. }
  162. catch (err) {
  163. return res.json(ApiResponse.error(err));
  164. }
  165. return res.json(ApiResponse.success({}));
  166. };
  167. return actions;
  168. };