revision.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import loggerFactory from '~/utils/logger';
  2. // disable no-return-await for model functions
  3. /* eslint-disable no-return-await */
  4. module.exports = function(crowi) {
  5. // eslint-disable-next-line no-unused-vars
  6. const logger = loggerFactory('growi:models:revision');
  7. const mongoose = require('mongoose');
  8. const mongoosePaginate = require('mongoose-paginate-v2');
  9. const ObjectId = mongoose.Schema.Types.ObjectId;
  10. const revisionSchema = new mongoose.Schema({
  11. path: { type: String, required: true, index: true },
  12. body: {
  13. type: String,
  14. required: true,
  15. get: (data) => {
  16. // replace CR/CRLF to LF above v3.1.5
  17. // see https://github.com/weseek/growi/issues/463
  18. return data ? data.replace(/\r\n?/g, '\n') : '';
  19. },
  20. },
  21. format: { type: String, default: 'markdown' },
  22. author: { type: ObjectId, ref: 'User' },
  23. createdAt: { type: Date, default: Date.now },
  24. hasDiffToPrev: { type: Boolean },
  25. });
  26. revisionSchema.plugin(mongoosePaginate);
  27. revisionSchema.statics.findRevisionIdList = function(path) {
  28. return this.find({ path })
  29. .select('_id author createdAt hasDiffToPrev')
  30. .sort({ createdAt: -1 })
  31. .exec();
  32. };
  33. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  34. const Revision = this;
  35. return new Promise(((resolve, reject) => {
  36. Revision.update({ path }, { $set: updateData }, { multi: true }, (err, data) => {
  37. if (err) {
  38. return reject(err);
  39. }
  40. return resolve(data);
  41. });
  42. }));
  43. };
  44. revisionSchema.statics.prepareRevision = function(pageData, body, previousBody, user, options) {
  45. const Revision = this;
  46. if (!options) {
  47. // eslint-disable-next-line no-param-reassign
  48. options = {};
  49. }
  50. const format = options.format || 'markdown';
  51. if (!user._id) {
  52. throw new Error('Error: user should have _id');
  53. }
  54. const newRevision = new Revision();
  55. newRevision.path = pageData.path;
  56. newRevision.body = body;
  57. newRevision.format = format;
  58. newRevision.author = user._id;
  59. newRevision.createdAt = Date.now();
  60. if (pageData.revision != null) {
  61. newRevision.hasDiffToPrev = body !== previousBody;
  62. }
  63. return newRevision;
  64. };
  65. revisionSchema.statics.removeRevisionsByPath = function(path) {
  66. const Revision = this;
  67. return new Promise(((resolve, reject) => {
  68. Revision.remove({ path }, (err, data) => {
  69. if (err) {
  70. return reject(err);
  71. }
  72. return resolve(data);
  73. });
  74. }));
  75. };
  76. revisionSchema.statics.findAuthorsByPage = async function(page) {
  77. const result = await this.distinct('author', { path: page.path }).exec();
  78. return result;
  79. };
  80. return mongoose.model('Revision', revisionSchema);
  81. };