comment.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { getModelSafely } from '~/server/util/mongoose-utils';
  2. module.exports = function(crowi) {
  3. const debug = require('debug')('growi:models:comment');
  4. const mongoose = require('mongoose');
  5. const ObjectId = mongoose.Schema.Types.ObjectId;
  6. const commentEvent = crowi.event('comment');
  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 = async function(comment, isMarkdown, commentId) {
  57. const Comment = this;
  58. const commentData = await Comment.findOneAndUpdate(
  59. { _id: commentId },
  60. { $set: { comment, isMarkdown } },
  61. );
  62. await commentEvent.emit('update', commentData.creator);
  63. return commentData;
  64. };
  65. /**
  66. * post remove hook
  67. */
  68. commentSchema.post('reomove', async(savedComment) => {
  69. await commentEvent.emit('remove', savedComment);
  70. });
  71. commentSchema.methods.removeWithReplies = async function(comment) {
  72. const Comment = crowi.model('Comment');
  73. await Comment.remove({
  74. $or: (
  75. [{ replyTo: this._id }, { _id: this._id }]),
  76. });
  77. await commentEvent.emit('remove', comment);
  78. return;
  79. };
  80. commentSchema.statics.findCreatorsByPage = async function(page) {
  81. return this.distinct('creator', { page }).exec();
  82. };
  83. /**
  84. * post save hook
  85. */
  86. commentSchema.post('save', async(savedComment) => {
  87. await commentEvent.emit('create', savedComment);
  88. });
  89. return mongoose.model('Comment', commentSchema);
  90. };