revision.js 3.5 KB

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