revision.js 3.9 KB

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