comment.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. return Comment.create(pageId, req.user._id, revisionId, comment, position)
  58. .then(function(createdComment) {
  59. createdComment.creator = req.user;
  60. return res.json(ApiResponse.success({comment: createdComment}));
  61. }).catch(function(err) {
  62. return res.json(ApiResponse.error(err));
  63. });
  64. };
  65. /**
  66. * @api {post} /comments.remove Remove specified comment
  67. * @apiName RemoveComment
  68. * @apiGroup Comment
  69. *
  70. * @apiParam {String} comment_id Comment Id.
  71. */
  72. api.remove = function(req, res) {
  73. var commentId = req.body.comment_id;
  74. if (!commentId) {
  75. return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
  76. }
  77. return Comment.findById(commentId).exec()
  78. .then(function(comment) {
  79. return comment.remove()
  80. .then(function() {
  81. return Page.updateCommentCount(comment.page);
  82. })
  83. .then(function() {
  84. return res.json(ApiResponse.success({}));
  85. });
  86. })
  87. .catch(function(err) {
  88. return res.json(ApiResponse.error(err));
  89. });
  90. };
  91. return actions;
  92. };