comment.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. import { Activity } from './activity';
  4. module.exports = function(crowi) {
  5. const debug = require('debug')('growi:models:comment');
  6. const mongoose = require('mongoose');
  7. const ObjectId = mongoose.Schema.Types.ObjectId;
  8. const commentSchema = new mongoose.Schema({
  9. page: { type: ObjectId, ref: 'Page', index: true },
  10. creator: { type: ObjectId, ref: 'User', index: true },
  11. revision: { type: ObjectId, ref: 'Revision', index: true },
  12. comment: { type: String, required: true },
  13. commentPosition: { type: Number, default: -1 },
  14. isMarkdown: { type: Boolean, default: false },
  15. replyTo: { type: ObjectId },
  16. }, {
  17. timestamps: true,
  18. });
  19. commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position, isMarkdown, replyTo) {
  20. const Comment = this;
  21. return new Promise(((resolve, reject) => {
  22. const newComment = new Comment();
  23. newComment.page = pageId;
  24. newComment.creator = creatorId;
  25. newComment.revision = revisionId;
  26. newComment.comment = comment;
  27. newComment.commentPosition = position;
  28. newComment.isMarkdown = isMarkdown || false;
  29. newComment.replyTo = replyTo;
  30. newComment.save((err, data) => {
  31. if (err) {
  32. debug('Error on saving comment.', err);
  33. return reject(err);
  34. }
  35. debug('Comment saved.', data);
  36. return resolve(data);
  37. });
  38. }));
  39. };
  40. commentSchema.statics.getCommentsByPageId = function(id) {
  41. return this.find({ page: id }).sort({ createdAt: -1 });
  42. };
  43. commentSchema.statics.getCommentsByRevisionId = function(id) {
  44. return this.find({ revision: id }).sort({ createdAt: -1 });
  45. };
  46. commentSchema.statics.countCommentByPageId = function(page) {
  47. const self = this;
  48. return new Promise(((resolve, reject) => {
  49. self.count({ page }, (err, data) => {
  50. if (err) {
  51. return reject(err);
  52. }
  53. return resolve(data);
  54. });
  55. }));
  56. };
  57. commentSchema.statics.updateCommentsByPageId = async function(comment, isMarkdown, commentId) {
  58. const Comment = this;
  59. const commentEvent = crowi.event('comment');
  60. const commentData = await Comment.findOneAndUpdate(
  61. { _id: commentId },
  62. { $set: { comment, isMarkdown } },
  63. );
  64. await commentEvent.emit('update', commentData.creator);
  65. return commentData;
  66. };
  67. commentSchema.statics.removeCommentsByPageId = function(pageId) {
  68. const Comment = this;
  69. return new Promise(((resolve, reject) => {
  70. Comment.remove({ page: pageId }, (err, done) => {
  71. if (err) {
  72. return reject(err);
  73. }
  74. resolve(done);
  75. });
  76. }));
  77. };
  78. commentSchema.methods.removeWithReplies = async function() {
  79. const Comment = crowi.model('Comment');
  80. return Comment.remove({
  81. $or: (
  82. [{ replyTo: this._id }, { _id: this._id }]),
  83. });
  84. };
  85. /**
  86. * post save hook
  87. */
  88. commentSchema.post('save', async(savedComment) => {
  89. const Page = crowi.model('Page');
  90. const commentEvent = crowi.event('comment');
  91. try {
  92. const page = await Page.updateCommentCount(savedComment.page);
  93. debug('CommentCount Updated', page);
  94. }
  95. catch (err) {
  96. throw err;
  97. }
  98. await commentEvent.emit('create', savedComment.creator);
  99. /**
  100. * TODO: move Activity operation from this model scope by GW-7506
  101. */
  102. try {
  103. const activityLog = await Activity.createByPageComment(savedComment);
  104. debug('Activity created', activityLog);
  105. }
  106. catch (err) {
  107. throw err;
  108. }
  109. });
  110. return mongoose.model('Comment', commentSchema);
  111. };