| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import loggerFactory from '~/utils/logger';
- // disable no-return-await for model functions
- /* eslint-disable no-return-await */
- module.exports = function(crowi) {
- // eslint-disable-next-line no-unused-vars
- const logger = loggerFactory('growi:models:revision');
- const mongoose = require('mongoose');
- const mongoosePaginate = require('mongoose-paginate-v2');
- const ObjectId = mongoose.Schema.Types.ObjectId;
- const revisionSchema = new mongoose.Schema({
- path: { type: String, required: true, index: true },
- body: {
- type: String,
- required: true,
- get: (data) => {
- // replace CR/CRLF to LF above v3.1.5
- // see https://github.com/weseek/growi/issues/463
- return data ? data.replace(/\r\n?/g, '\n') : '';
- },
- },
- format: { type: String, default: 'markdown' },
- author: { type: ObjectId, ref: 'User' },
- createdAt: { type: Date, default: Date.now },
- hasDiffToPrev: { type: Boolean },
- });
- revisionSchema.plugin(mongoosePaginate);
- revisionSchema.statics.findRevisionIdList = function(path) {
- return this.find({ path })
- .select('_id author createdAt hasDiffToPrev')
- .sort({ createdAt: -1 })
- .exec();
- };
- revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
- const Revision = this;
- return new Promise(((resolve, reject) => {
- Revision.update({ path }, { $set: updateData }, { multi: true }, (err, data) => {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- }));
- };
- revisionSchema.statics.prepareRevision = function(pageData, body, previousBody, user, options) {
- const Revision = this;
- if (!options) {
- // eslint-disable-next-line no-param-reassign
- options = {};
- }
- const format = options.format || 'markdown';
- if (!user._id) {
- throw new Error('Error: user should have _id');
- }
- const newRevision = new Revision();
- newRevision.path = pageData.path;
- newRevision.body = body;
- newRevision.format = format;
- newRevision.author = user._id;
- newRevision.createdAt = Date.now();
- if (pageData.revision != null) {
- newRevision.hasDiffToPrev = body !== previousBody;
- }
- return newRevision;
- };
- revisionSchema.statics.removeRevisionsByPath = function(path) {
- const Revision = this;
- return new Promise(((resolve, reject) => {
- Revision.remove({ path }, (err, data) => {
- if (err) {
- return reject(err);
- }
- return resolve(data);
- });
- }));
- };
- revisionSchema.statics.findAuthorsByPage = async function(page) {
- const result = await this.distinct('author', { path: page.path }).exec();
- return result;
- };
- return mongoose.model('Revision', revisionSchema);
- };
|