revision.js 1.9 KB

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