comment.js 3.6 KB

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