comment.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.updateCommentsByPageId = function(comment, isMarkdown, commentId) {
  57. const Comment = this;
  58. return Comment.findOneAndUpdate(
  59. { _id: commentId },
  60. { $set: { comment, isMarkdown } },
  61. );
  62. };
  63. commentSchema.statics.removeCommentsByPageId = function(pageId) {
  64. const Comment = this;
  65. return new Promise(((resolve, reject) => {
  66. Comment.remove({ page: pageId }, (err, done) => {
  67. if (err) {
  68. return reject(err);
  69. }
  70. resolve(done);
  71. });
  72. }));
  73. };
  74. commentSchema.methods.removeWithReplies = async function() {
  75. const Comment = crowi.model('Comment');
  76. return Comment.remove({
  77. $or: (
  78. [{ replyTo: this._id }, { _id: this._id }]),
  79. });
  80. };
  81. /**
  82. * post save hook
  83. */
  84. commentSchema.post('save', (savedComment) => {
  85. const Page = crowi.model('Page');
  86. Page.updateCommentCount(savedComment.page)
  87. .then((page) => {
  88. debug('CommentCount Updated', page);
  89. })
  90. .catch(() => {
  91. });
  92. });
  93. return mongoose.model('Comment', commentSchema);
  94. };