revision.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. module.exports = function(crowi) {
  2. // eslint-disable-next-line no-unused-vars
  3. const logger = require('@alias/logger')('growi:models:revision');
  4. const mongoose = require('mongoose')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. ;
  7. const revisionSchema = new mongoose.Schema({
  8. path: { type: String, required: true },
  9. body: { type: String, required: true, get: (data) => {
  10. // replace CR/CRLF to LF above v3.1.5
  11. // see https://github.com/weseek/growi/issues/463
  12. return data ? data.replace(/\r\n?/g, '\n') : '';
  13. }},
  14. format: { type: String, default: 'markdown' },
  15. author: { type: ObjectId, ref: 'User' },
  16. createdAt: { type: Date, default: Date.now },
  17. hasDiffToPrev: { type: Boolean },
  18. });
  19. /*
  20. * preparation for https://github.com/weseek/growi/issues/216
  21. */
  22. // // create a XSS Filter instance
  23. // // TODO read options
  24. // this.xss = new Xss(true);
  25. // // prevent XSS when pre save
  26. // revisionSchema.pre('save', function(next) {
  27. // this.body = xss.process(this.body);
  28. // next();
  29. // });
  30. revisionSchema.statics.findRevisions = function(ids) {
  31. const Revision = this,
  32. User = crowi.model('User');
  33. if (!Array.isArray(ids)) {
  34. return Promise.reject('The argument was not Array.');
  35. }
  36. return new Promise(function(resolve, reject) {
  37. Revision
  38. .find({ _id: { $in: ids }})
  39. .sort({createdAt: -1})
  40. .populate('author', User.USER_PUBLIC_FIELDS)
  41. .exec(function(err, revisions) {
  42. if (err) {
  43. return reject(err);
  44. }
  45. return resolve(revisions);
  46. });
  47. });
  48. };
  49. revisionSchema.statics.findRevisionIdList = function(path) {
  50. return this.find({path: path})
  51. .select('_id author createdAt hasDiffToPrev')
  52. .sort({createdAt: -1})
  53. .exec();
  54. };
  55. revisionSchema.statics.findRevisionList = function(path, options) {
  56. const Revision = this,
  57. User = crowi.model('User');
  58. return new Promise(function(resolve, reject) {
  59. Revision.find({path: path})
  60. .sort({createdAt: -1})
  61. .populate('author', User.USER_PUBLIC_FIELDS)
  62. .exec(function(err, data) {
  63. if (err) {
  64. return reject(err);
  65. }
  66. return resolve(data);
  67. });
  68. });
  69. };
  70. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  71. const Revision = this;
  72. return new Promise(function(resolve, reject) {
  73. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  74. if (err) {
  75. return reject(err);
  76. }
  77. return resolve(data);
  78. });
  79. });
  80. };
  81. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  82. const Revision = this;
  83. if (!options) {
  84. options = {};
  85. }
  86. const format = options.format || 'markdown';
  87. if (!user._id) {
  88. throw new Error('Error: user should have _id');
  89. }
  90. const newRevision = new Revision();
  91. newRevision.path = pageData.path;
  92. newRevision.body = body;
  93. newRevision.format = format;
  94. newRevision.author = user._id;
  95. newRevision.createdAt = Date.now();
  96. if (pageData.revision != null) {
  97. newRevision.hasDiffToPrev = body !== pageData.revision.body;
  98. }
  99. return newRevision;
  100. };
  101. revisionSchema.statics.removeRevisionsByPath = function(path) {
  102. const Revision = this;
  103. return new Promise(function(resolve, reject) {
  104. Revision.remove({path: path}, function(err, data) {
  105. if (err) {
  106. return reject(err);
  107. }
  108. return resolve(data);
  109. });
  110. });
  111. };
  112. return mongoose.model('Revision', revisionSchema);
  113. };