2
0

revision.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.findRevisionList = function(path, options) {
  54. var Revision = this,
  55. User = crowi.model('User');
  56. return new Promise(function(resolve, reject) {
  57. Revision.find({path: path})
  58. .sort({createdAt: -1})
  59. .populate('author', User.USER_PUBLIC_FIELDS)
  60. .exec(function(err, data) {
  61. if (err) {
  62. return reject(err);
  63. }
  64. return resolve(data);
  65. });
  66. });
  67. };
  68. revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
  69. var Revision = this;
  70. return new Promise(function(resolve, reject) {
  71. Revision.update({path: path}, {$set: updateData}, {multi: true}, function(err, data) {
  72. if (err) {
  73. return reject(err);
  74. }
  75. return resolve(data);
  76. });
  77. });
  78. };
  79. revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
  80. var Revision = this;
  81. if (!options) {
  82. options = {};
  83. }
  84. var format = options.format || 'markdown';
  85. if (!user._id) {
  86. throw new Error('Error: user should have _id');
  87. }
  88. var newRevision = new Revision();
  89. newRevision.path = pageData.path;
  90. newRevision.body = body;
  91. newRevision.format = format;
  92. newRevision.author = user._id;
  93. newRevision.createdAt = Date.now();
  94. return newRevision;
  95. };
  96. revisionSchema.statics.removeRevisionsByPath = function(path) {
  97. var Revision = this;
  98. return new Promise(function(resolve, reject) {
  99. Revision.remove({path: path}, function(err, data) {
  100. if (err) {
  101. return reject(err);
  102. }
  103. return resolve(data);
  104. });
  105. });
  106. };
  107. revisionSchema.statics.updatePath = function(pathName) {
  108. };
  109. return mongoose.model('Revision', revisionSchema);
  110. };