revision.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.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. });
  18. /*
  19. * preparation for https://github.com/weseek/growi/issues/216
  20. */
  21. // // create a XSS Filter instance
  22. // // TODO read options
  23. // this.xss = new Xss(true);
  24. // // prevent XSS when pre save
  25. // revisionSchema.pre('save', function(next) {
  26. // this.body = xss.process(this.body);
  27. // next();
  28. // });
  29. revisionSchema.statics.findLatestRevision = function(path, cb) {
  30. this.find({path: path})
  31. .sort({createdAt: -1})
  32. .limit(1)
  33. .exec(function(err, data) {
  34. cb(err, data.shift());
  35. });
  36. };
  37. revisionSchema.statics.findRevision = function(id) {
  38. const Revision = this;
  39. return new Promise(function(resolve, reject) {
  40. Revision.findById(id)
  41. .populate('author')
  42. .exec(function(err, data) {
  43. if (err) {
  44. return reject(err);
  45. }
  46. return resolve(data);
  47. });
  48. });
  49. };
  50. revisionSchema.statics.findRevisions = function(ids) {
  51. const Revision = this,
  52. User = crowi.model('User');
  53. if (!Array.isArray(ids)) {
  54. return Promise.reject('The argument was not Array.');
  55. }
  56. return new Promise(function(resolve, reject) {
  57. Revision
  58. .find({ _id: { $in: ids }})
  59. .sort({createdAt: -1})
  60. .populate('author', User.USER_PUBLIC_FIELDS)
  61. .exec(function(err, revisions) {
  62. if (err) {
  63. return reject(err);
  64. }
  65. return resolve(revisions);
  66. });
  67. });
  68. };
  69. revisionSchema.statics.findRevisionIdList = function(path) {
  70. return this.find({path: path})
  71. .select('_id author createdAt')
  72. .sort({createdAt: -1})
  73. .exec();
  74. };
  75. revisionSchema.statics.findRevisionList = function(path, options) {
  76. const Revision = this,
  77. User = crowi.model('User');
  78. return new Promise(function(resolve, reject) {
  79. Revision.find({path: path})
  80. .sort({createdAt: -1})
  81. .populate('author', User.USER_PUBLIC_FIELDS)
  82. .exec(function(err, data) {
  83. if (err) {
  84. return reject(err);
  85. }
  86. return resolve(data);
  87. });
  88. });
  89. };
  90. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  91. const Revision = this;
  92. return new Promise(function(resolve, reject) {
  93. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  94. if (err) {
  95. return reject(err);
  96. }
  97. return resolve(data);
  98. });
  99. });
  100. };
  101. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  102. const Revision = this;
  103. if (!options) {
  104. options = {};
  105. }
  106. const format = options.format || 'markdown';
  107. if (!user._id) {
  108. throw new Error('Error: user should have _id');
  109. }
  110. const newRevision = new Revision();
  111. newRevision.path = pageData.path;
  112. newRevision.body = body;
  113. newRevision.format = format;
  114. newRevision.author = user._id;
  115. newRevision.createdAt = Date.now();
  116. return newRevision;
  117. };
  118. revisionSchema.statics.removeRevisionsByPath = function(path) {
  119. const Revision = this;
  120. return new Promise(function(resolve, reject) {
  121. Revision.remove({path: path}, function(err, data) {
  122. if (err) {
  123. return reject(err);
  124. }
  125. return resolve(data);
  126. });
  127. });
  128. };
  129. revisionSchema.statics.updatePath = function(pathName) {
  130. };
  131. return mongoose.model('Revision', revisionSchema);
  132. };