comment.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. module.exports = function(crowi) {
  2. const debug = require('debug')('growi:models:comment');
  3. const mongoose = require('mongoose');
  4. const ObjectId = mongoose.Schema.Types.ObjectId;
  5. const commentEvent = crowi.event('comment');
  6. const commentSchema = new mongoose.Schema({
  7. page: { type: ObjectId, ref: 'Page', index: true },
  8. creator: { type: ObjectId, ref: 'User', index: true },
  9. revision: { type: ObjectId, ref: 'Revision', index: true },
  10. comment: { type: String, required: true },
  11. commentPosition: { type: Number, default: -1 },
  12. isMarkdown: { type: Boolean, default: false },
  13. replyTo: { type: ObjectId },
  14. }, {
  15. timestamps: true,
  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 = async function(comment, isMarkdown, commentId) {
  56. const Comment = this;
  57. const commentData = await Comment.findOneAndUpdate(
  58. { _id: commentId },
  59. { $set: { comment, isMarkdown } },
  60. );
  61. await commentEvent.emit('update', commentData);
  62. return commentData;
  63. };
  64. /**
  65. * post remove hook
  66. */
  67. commentSchema.post('reomove', async(savedComment) => {
  68. await commentEvent.emit('remove', savedComment);
  69. });
  70. commentSchema.methods.removeWithReplies = async function(comment) {
  71. const Comment = crowi.model('Comment');
  72. await Comment.remove({
  73. $or: (
  74. [{ replyTo: this._id }, { _id: this._id }]),
  75. });
  76. await commentEvent.emit('remove', comment);
  77. return;
  78. };
  79. commentSchema.statics.findCreatorsByPage = async function(page) {
  80. return this.distinct('creator', { page }).exec();
  81. };
  82. /**
  83. * post save hook
  84. */
  85. commentSchema.post('save', async(savedComment) => {
  86. await commentEvent.emit('create', savedComment);
  87. });
  88. return mongoose.model('Comment', commentSchema);
  89. };