comment.js 3.1 KB

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