revision.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:revision')
  3. , mongoose = require('mongoose')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , revisionSchema;
  6. revisionSchema = new mongoose.Schema({
  7. path: { type: String, required: true },
  8. body: { type: String, required: true },
  9. format: { type: String, default: 'markdown' },
  10. author: { type: ObjectId, ref: 'User' },
  11. createdAt: { type: Date, default: Date.now }
  12. });
  13. revisionSchema.statics.findLatestRevision = function(path, cb) {
  14. this.find({path: path})
  15. .sort({createdAt: -1})
  16. .limit(1)
  17. .exec(function(err, data) {
  18. cb(err, data.shift());
  19. });
  20. };
  21. revisionSchema.statics.findRevision = function(id) {
  22. var Revision = this;
  23. return new Promise(function(resolve, reject) {
  24. Revision.findById(id)
  25. .populate('author')
  26. .exec(function(err, data) {
  27. if (err) {
  28. return reject(err);
  29. }
  30. return resolve(data);
  31. });
  32. });
  33. };
  34. revisionSchema.statics.findRevisionList = function(path, options) {
  35. var Revision = this;
  36. return new Promise(function(resolve, reject) {
  37. Revision.find({path: path})
  38. .sort({createdAt: -1})
  39. .populate('author')
  40. .exec(function(err, data) {
  41. if (err) {
  42. return reject(err);
  43. }
  44. return resolve(data);
  45. });
  46. });
  47. };
  48. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  49. var Revision = this;
  50. return new Promise(function(resolve, reject) {
  51. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  52. if (err) {
  53. return reject(err);
  54. }
  55. return resolve(data);
  56. });
  57. });
  58. };
  59. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  60. var Revision = this;
  61. if (!options) {
  62. options = {};
  63. }
  64. var format = options.format || 'markdown';
  65. if (!user._id) {
  66. throw new Error('Error: user should have _id');
  67. }
  68. var newRevision = new Revision();
  69. newRevision.path = pageData.path;
  70. newRevision.body = body;
  71. newRevision.format = format;
  72. newRevision.author = user._id;
  73. newRevision.createdAt = Date.now();
  74. return newRevision;
  75. };
  76. revisionSchema.statics.updatePath = function(pathName) {
  77. };
  78. return mongoose.model('Revision', revisionSchema);
  79. };