revision.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.findRevisionList = function(path, options) {
  22. var Revision = this;
  23. return new Promise(function(resolve, reject) {
  24. Revision.find({path: path})
  25. .sort({createdAt: -1})
  26. .populate('author')
  27. .exec(function(err, data) {
  28. if (err) {
  29. return reject(err);
  30. }
  31. return resolve(data);
  32. });
  33. });
  34. };
  35. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  36. var Revision = this;
  37. return new Promise(function(resolve, reject) {
  38. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  39. if (err) {
  40. return reject(err);
  41. }
  42. return resolve(data);
  43. });
  44. });
  45. };
  46. revisionSchema.statics.findRevision = function(id, cb) {
  47. this.findById(id)
  48. .populate('author')
  49. .exec(function(err, data) {
  50. cb(err, data);
  51. });
  52. };
  53. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  54. var Revision = this;
  55. if (!options) {
  56. options = {};
  57. }
  58. var format = options.format || 'markdown';
  59. if (!user._id) {
  60. throw new Error('Error: user should have _id');
  61. }
  62. var newRevision = new Revision();
  63. newRevision.path = pageData.path;
  64. newRevision.body = body;
  65. newRevision.format = format;
  66. newRevision.author = user._id;
  67. newRevision.createdAt = Date.now();
  68. return newRevision;
  69. };
  70. revisionSchema.statics.updatePath = function(pathName) {
  71. };
  72. return mongoose.model('Revision', revisionSchema);
  73. };