comment.js 3.0 KB

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