revision.js 3.9 KB

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