comment.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. module.exports = function(crowi) {
  4. const debug = require('debug')('growi:models:comment');
  5. const mongoose = require('mongoose');
  6. const ObjectId = mongoose.Schema.Types.ObjectId;
  7. const commentSchema = new mongoose.Schema({
  8. page: { type: ObjectId, ref: 'Page', index: true },
  9. creator: { type: ObjectId, ref: 'User', index: true },
  10. revision: { type: ObjectId, ref: 'Revision', index: true },
  11. comment: { type: String, required: true },
  12. commentPosition: { type: Number, default: -1 },
  13. isMarkdown: { type: Boolean, default: false },
  14. replyTo: { type: ObjectId },
  15. }, {
  16. timestamps: true,
  17. });
  18. commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position, isMarkdown, replyTo) {
  19. const Comment = this;
  20. return new Promise(((resolve, reject) => {
  21. const newComment = new Comment();
  22. newComment.page = pageId;
  23. newComment.creator = creatorId;
  24. newComment.revision = revisionId;
  25. newComment.comment = comment;
  26. newComment.commentPosition = position;
  27. newComment.isMarkdown = isMarkdown || false;
  28. newComment.replyTo = replyTo;
  29. newComment.save((err, data) => {
  30. if (err) {
  31. debug('Error on saving comment.', err);
  32. return reject(err);
  33. }
  34. debug('Comment saved.', data);
  35. return resolve(data);
  36. });
  37. }));
  38. };
  39. commentSchema.statics.getCommentsByPageId = function(id) {
  40. return this.find({ page: id }).sort({ createdAt: -1 });
  41. };
  42. commentSchema.statics.getCommentsByRevisionId = function(id) {
  43. return this.find({ revision: id }).sort({ createdAt: -1 });
  44. };
  45. commentSchema.statics.countCommentByPageId = function(page) {
  46. const self = this;
  47. return new Promise(((resolve, reject) => {
  48. self.count({ page }, (err, data) => {
  49. if (err) {
  50. return reject(err);
  51. }
  52. return resolve(data);
  53. });
  54. }));
  55. };
  56. commentSchema.statics.removeCommentsByPageId = function(pageId) {
  57. const Comment = this;
  58. return new Promise(((resolve, reject) => {
  59. Comment.remove({ page: pageId }, (err, done) => {
  60. if (err) {
  61. return reject(err);
  62. }
  63. resolve(done);
  64. });
  65. }));
  66. };
  67. commentSchema.methods.removeWithReplies = async function() {
  68. const Comment = crowi.model('Comment');
  69. return Comment.remove({
  70. $or: (
  71. [{ replyTo: this._id }, { _id: this._id }]),
  72. });
  73. };
  74. /**
  75. * post save hook
  76. */
  77. commentSchema.post('save', (savedComment) => {
  78. const Page = crowi.model('Page');
  79. Page.updateCommentCount(savedComment.page)
  80. .then((page) => {
  81. debug('CommentCount Updated', page);
  82. })
  83. .catch(() => {
  84. });
  85. });
  86. return mongoose.model('Comment', commentSchema);
  87. };