comment.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 form = req.form.commentForm;
  50. if (!req.form.isValid) {
  51. // return res.json(ApiResponse.error('Invalid comment.'));
  52. return res.json(ApiResponse.error('コメントを入力してください。'));
  53. }
  54. const pageId = form.page_id;
  55. const revisionId = form.revision_id;
  56. const comment = form.comment;
  57. const position = form.comment_position || -1;
  58. const isMarkdown = form.is_markdown;
  59. const createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, isMarkdown)
  60. .catch(function(err) {
  61. return res.json(ApiResponse.error(err));
  62. });
  63. // update page
  64. const page = await Page.findOneAndUpdate({ _id: pageId }, {
  65. lastUpdateUser: req.user,
  66. updatedAt: new Date()
  67. });
  68. //slack notification
  69. const slackNotify = new slack(crowi);
  70. const user = await User.findUserByUsername(req.user.username);
  71. await slackNotify.post(page, user, 'general', 'comment', null, createdComment);
  72. return res.json(ApiResponse.success({comment: createdComment}));
  73. };
  74. /**
  75. * @api {post} /comments.remove Remove specified comment
  76. * @apiName RemoveComment
  77. * @apiGroup Comment
  78. *
  79. * @apiParam {String} comment_id Comment Id.
  80. */
  81. api.remove = function(req, res) {
  82. const commentId = req.body.comment_id;
  83. if (!commentId) {
  84. return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
  85. }
  86. return Comment.findById(commentId).exec()
  87. .then(function(comment) {
  88. return comment.remove()
  89. .then(function() {
  90. return Page.updateCommentCount(comment.page);
  91. })
  92. .then(function() {
  93. return res.json(ApiResponse.success({}));
  94. });
  95. })
  96. .catch(function(err) {
  97. return res.json(ApiResponse.error(err));
  98. });
  99. };
  100. return actions;
  101. };