revision.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module.exports = function(crowi) {
  2. /* eslint-disable no-unused-vars */
  3. const logger = require('@alias/logger')('growi:models:revision');
  4. /* eslint-enable */
  5. const mongoose = require('mongoose')
  6. , ObjectId = mongoose.Schema.Types.ObjectId
  7. ;
  8. const revisionSchema = new mongoose.Schema({
  9. path: { type: String, required: true },
  10. body: { type: String, required: true, get: (data) => {
  11. // replace CR/CRLF to LF above v3.1.5
  12. // see https://github.com/weseek/growi/issues/463
  13. return data.replace(/\r\n?/g, '\n');
  14. }},
  15. format: { type: String, default: 'markdown' },
  16. author: { type: ObjectId, ref: 'User' },
  17. createdAt: { type: Date, default: Date.now }
  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.findLatestRevision = function(path, cb) {
  31. this.find({path: path})
  32. .sort({createdAt: -1})
  33. .limit(1)
  34. .exec(function(err, data) {
  35. cb(err, data.shift());
  36. });
  37. };
  38. revisionSchema.statics.findRevision = function(id) {
  39. const Revision = this;
  40. return new Promise(function(resolve, reject) {
  41. Revision.findById(id)
  42. .populate('author')
  43. .exec(function(err, data) {
  44. if (err) {
  45. return reject(err);
  46. }
  47. return resolve(data);
  48. });
  49. });
  50. };
  51. revisionSchema.statics.findRevisions = function(ids) {
  52. const Revision = this,
  53. User = crowi.model('User');
  54. if (!Array.isArray(ids)) {
  55. return Promise.reject('The argument was not Array.');
  56. }
  57. return new Promise(function(resolve, reject) {
  58. Revision
  59. .find({ _id: { $in: ids }})
  60. .sort({createdAt: -1})
  61. .populate('author', User.USER_PUBLIC_FIELDS)
  62. .exec(function(err, revisions) {
  63. if (err) {
  64. return reject(err);
  65. }
  66. return resolve(revisions);
  67. });
  68. });
  69. };
  70. revisionSchema.statics.findRevisionIdList = function(path) {
  71. return this.find({path: path})
  72. .select('_id author createdAt')
  73. .sort({createdAt: -1})
  74. .exec();
  75. };
  76. revisionSchema.statics.findRevisionList = function(path, options) {
  77. const Revision = this,
  78. User = crowi.model('User');
  79. return new Promise(function(resolve, reject) {
  80. Revision.find({path: path})
  81. .sort({createdAt: -1})
  82. .populate('author', User.USER_PUBLIC_FIELDS)
  83. .exec(function(err, data) {
  84. if (err) {
  85. return reject(err);
  86. }
  87. return resolve(data);
  88. });
  89. });
  90. };
  91. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  92. const Revision = this;
  93. return new Promise(function(resolve, reject) {
  94. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  95. if (err) {
  96. return reject(err);
  97. }
  98. return resolve(data);
  99. });
  100. });
  101. };
  102. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  103. const Revision = this;
  104. if (!options) {
  105. options = {};
  106. }
  107. const format = options.format || 'markdown';
  108. if (!user._id) {
  109. throw new Error('Error: user should have _id');
  110. }
  111. const newRevision = new Revision();
  112. newRevision.path = pageData.path;
  113. newRevision.body = body;
  114. newRevision.format = format;
  115. newRevision.author = user._id;
  116. newRevision.createdAt = Date.now();
  117. return newRevision;
  118. };
  119. revisionSchema.statics.removeRevisionsByPath = function(path) {
  120. const Revision = this;
  121. return new Promise(function(resolve, reject) {
  122. Revision.remove({path: path}, function(err, data) {
  123. if (err) {
  124. return reject(err);
  125. }
  126. return resolve(data);
  127. });
  128. });
  129. };
  130. revisionSchema.statics.updatePath = function(pathName) {
  131. };
  132. return mongoose.model('Revision', revisionSchema);
  133. };