revision.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, cb) {
  36. this.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  37. cb(err, data);
  38. });
  39. };
  40. revisionSchema.statics.findRevision = function(id, cb) {
  41. this.findById(id)
  42. .populate('author')
  43. .exec(function(err, data) {
  44. cb(err, data);
  45. });
  46. };
  47. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  48. var Revision = this;
  49. if (!options) {
  50. options = {};
  51. }
  52. var format = options.format || 'markdown';
  53. if (!user._id) {
  54. throw new Error('Error: user should have _id');
  55. }
  56. var newRevision = new Revision();
  57. newRevision.path = pageData.path;
  58. newRevision.body = body;
  59. newRevision.format = format;
  60. newRevision.author = user._id;
  61. newRevision.createdAt = Date.now();
  62. return newRevision;
  63. };
  64. revisionSchema.statics.updatePath = function(pathName) {
  65. };
  66. return mongoose.model('Revision', revisionSchema);
  67. };