revision.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.findRevisions = function(ids) {
  35. var Revision = this;
  36. if (!Array.isArray(ids)) {
  37. return Promise.reject('The argument was not Array.');
  38. }
  39. return new Promise(function(resolve, reject) {
  40. Revision
  41. .find({ _id: { $in: ids }})
  42. .sort({createdAt: -1})
  43. .populate('author')
  44. .exec(function(err, revisions) {
  45. if (err) {
  46. return reject(err);
  47. }
  48. return resolve(revisions);
  49. });
  50. });
  51. };
  52. revisionSchema.statics.findRevisionList = function(path, options) {
  53. var Revision = this;
  54. return new Promise(function(resolve, reject) {
  55. Revision.find({path: path})
  56. .sort({createdAt: -1})
  57. .populate('author')
  58. .exec(function(err, data) {
  59. if (err) {
  60. return reject(err);
  61. }
  62. return resolve(data);
  63. });
  64. });
  65. };
  66. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  67. var Revision = this;
  68. return new Promise(function(resolve, reject) {
  69. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  70. if (err) {
  71. return reject(err);
  72. }
  73. return resolve(data);
  74. });
  75. });
  76. };
  77. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  78. var Revision = this;
  79. if (!options) {
  80. options = {};
  81. }
  82. var format = options.format || 'markdown';
  83. if (!user._id) {
  84. throw new Error('Error: user should have _id');
  85. }
  86. var newRevision = new Revision();
  87. newRevision.path = pageData.path;
  88. newRevision.body = body;
  89. newRevision.format = format;
  90. newRevision.author = user._id;
  91. newRevision.createdAt = Date.now();
  92. return newRevision;
  93. };
  94. revisionSchema.statics.updatePath = function(pathName) {
  95. };
  96. return mongoose.model('Revision', revisionSchema);
  97. };