comment.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. createdAt: { type: Date, default: Date.now },
  14. isMarkdown: { type: Boolean, default: false },
  15. replyTo: { type: ObjectId },
  16. });
  17. commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position, isMarkdown, replyTo) {
  18. const Comment = this;
  19. return new Promise(((resolve, reject) => {
  20. const newComment = new Comment();
  21. newComment.page = pageId;
  22. newComment.creator = creatorId;
  23. newComment.revision = revisionId;
  24. newComment.comment = comment;
  25. newComment.commentPosition = position;
  26. newComment.isMarkdown = isMarkdown || false;
  27. newComment.replyTo = replyTo;
  28. newComment.save((err, data) => {
  29. if (err) {
  30. debug('Error on saving comment.', err);
  31. return reject(err);
  32. }
  33. debug('Comment saved.', data);
  34. return resolve(data);
  35. });
  36. }));
  37. };
  38. commentSchema.statics.getCommentsByPageId = function(id) {
  39. return this.find({ page: id }).sort({ createdAt: -1 });
  40. };
  41. commentSchema.statics.getCommentsByRevisionId = function(id) {
  42. return this.find({ revision: id }).sort({ createdAt: -1 });
  43. };
  44. commentSchema.statics.countCommentByPageId = function(page) {
  45. const self = this;
  46. return new Promise(((resolve, reject) => {
  47. self.count({ page }, (err, data) => {
  48. if (err) {
  49. return reject(err);
  50. }
  51. return resolve(data);
  52. });
  53. }));
  54. };
  55. commentSchema.statics.updateCommentsByPageId = function(comment, isMarkdown, commentId) {
  56. const Comment = this;
  57. return Comment.findOneAndUpdate(
  58. { _id: commentId },
  59. { $set: { comment, isMarkdown } },
  60. );
  61. };
  62. commentSchema.statics.removeCommentsByPageId = function(pageId) {
  63. const Comment = this;
  64. return new Promise(((resolve, reject) => {
  65. Comment.remove({ page: pageId }, (err, done) => {
  66. if (err) {
  67. return reject(err);
  68. }
  69. resolve(done);
  70. });
  71. }));
  72. };
  73. commentSchema.methods.removeWithReplies = async function() {
  74. const Comment = crowi.model('Comment');
  75. return Comment.remove({
  76. $or: (
  77. [{ replyTo: this._id }, { _id: this._id }]),
  78. });
  79. };
  80. /**
  81. * post save hook
  82. */
  83. commentSchema.post('save', (savedComment) => {
  84. const Page = crowi.model('Page');
  85. Page.updateCommentCount(savedComment.page)
  86. .then((page) => {
  87. debug('CommentCount Updated', page);
  88. })
  89. .catch(() => {
  90. });
  91. });
  92. return mongoose.model('Comment', commentSchema);
  93. };