comment.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. const debug = require('debug')('growi:routs:comment')
  4. , logger = require('@alias/logger')('growi:routes:comment')
  5. , Comment = crowi.model('Comment')
  6. , User = crowi.model('User')
  7. , Page = crowi.model('Page')
  8. , ApiResponse = require('../util/apiResponse')
  9. , globalNotificationService = crowi.getGlobalNotificationService()
  10. , actions = {}
  11. , api = {};
  12. actions.api = api;
  13. /**
  14. * @api {get} /comments.get Get comments of the page of the revision
  15. * @apiName GetComments
  16. * @apiGroup Comment
  17. *
  18. * @apiParam {String} page_id Page Id.
  19. * @apiParam {String} revision_id Revision Id.
  20. */
  21. api.get = function(req, res) {
  22. const pageId = req.query.page_id;
  23. const revisionId = req.query.revision_id;
  24. if (revisionId) {
  25. return Comment.getCommentsByRevisionId(revisionId)
  26. .then(function(comments) {
  27. res.json(ApiResponse.success({comments}));
  28. }).catch(function(err) {
  29. res.json(ApiResponse.error(err));
  30. });
  31. }
  32. return Comment.getCommentsByPageId(pageId)
  33. .then(function(comments) {
  34. res.json(ApiResponse.success({comments}));
  35. }).catch(function(err) {
  36. res.json(ApiResponse.error(err));
  37. });
  38. };
  39. /**
  40. * @api {post} /comments.add Post comment for the page
  41. * @apiName PostComment
  42. * @apiGroup Comment
  43. *
  44. * @apiParam {String} page_id Page Id.
  45. * @apiParam {String} revision_id Revision Id.
  46. * @apiParam {String} comment Comment body
  47. * @apiParam {Number} comment_position=-1 Line number of the comment
  48. */
  49. api.add = async function(req, res) {
  50. const commentForm = req.form.commentForm;
  51. const slackNotificationForm = req.form.slackNotificationForm;
  52. if (!req.form.isValid) {
  53. // return res.json(ApiResponse.error('Invalid comment.'));
  54. return res.json(ApiResponse.error('コメントを入力してください。'));
  55. }
  56. const pageId = commentForm.page_id;
  57. const revisionId = commentForm.revision_id;
  58. const comment = commentForm.comment;
  59. const position = commentForm.comment_position || -1;
  60. const isMarkdown = commentForm.is_markdown;
  61. const createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, isMarkdown)
  62. .catch(function(err) {
  63. return res.json(ApiResponse.error(err));
  64. });
  65. // update page
  66. const page = await Page.findOneAndUpdate({ _id: pageId }, {
  67. lastUpdateUser: req.user,
  68. updatedAt: new Date()
  69. });
  70. res.json(ApiResponse.success({comment: createdComment}));
  71. const path = page.path;
  72. // global notification
  73. globalNotificationService.notifyComment(createdComment, path);
  74. // slack notification
  75. if (slackNotificationForm.isSlackEnabled) {
  76. const user = await User.findUserByUsername(req.user.username);
  77. const channels = slackNotificationForm.slackChannels;
  78. if (channels) {
  79. page.updateSlackChannel(channels).catch(err => {
  80. logger.error('Error occured in updating slack channels: ', err);
  81. });
  82. const promises = channels.split(',').map(function(chan) {
  83. return crowi.slack.postComment(createdComment, user, chan, path);
  84. });
  85. Promise.all(promises)
  86. .catch(err => {
  87. logger.error('Error occured in sending slack notification: ', err);
  88. });
  89. }
  90. }
  91. };
  92. /**
  93. * @api {post} /comments.remove Remove specified comment
  94. * @apiName RemoveComment
  95. * @apiGroup Comment
  96. *
  97. * @apiParam {String} comment_id Comment Id.
  98. */
  99. api.remove = function(req, res) {
  100. const commentId = req.body.comment_id;
  101. if (!commentId) {
  102. return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
  103. }
  104. return Comment.findById(commentId).exec()
  105. .then(function(comment) {
  106. return comment.remove()
  107. .then(function() {
  108. return Page.updateCommentCount(comment.page);
  109. })
  110. .then(function() {
  111. return res.json(ApiResponse.success({}));
  112. });
  113. })
  114. .catch(function(err) {
  115. return res.json(ApiResponse.error(err));
  116. });
  117. };
  118. return actions;
  119. };