0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Used new PostRevisions service for revision generation

This is behind the postHistory flag so that we leave alone the very import
revision generation code. The new package attempts to replicate the existing
strategy but gives us room to easily change it in future
This commit is contained in:
Fabien "egg" O'Carroll 2023-04-17 14:15:32 +01:00
parent a62fe42933
commit 7d7e19449a

View file

@ -19,6 +19,8 @@ const urlUtils = require('../../shared/url-utils');
const {Tag} = require('./tag');
const {Newsletter} = require('./newsletter');
const {BadRequestError} = require('@tryghost/errors');
const PostRevisions = require('@tryghost/post-revisions');
const labs = require('../../shared/labs');
const messages = {
isAlreadyPublished: 'Your post is already published, please reload your page.',
@ -862,37 +864,66 @@ Post = ghostBookshelf.Model.extend({
});
}
// CASE: Handle post backups/revisions. This is a pure database feature.
if (model.hasChanged('lexical') && !model.get('mobiledoc') && !options.importing && !options.migrating) {
ops.push(function updateRevisions() {
return ghostBookshelf.model('PostRevision')
.findAll(Object.assign({
filter: `post_id:${model.id}`,
columns: ['id']
}, _.pick(options, 'transacting')))
.then((revisions) => {
// Store previous + latest lexical content
if (!revisions.length && options.method !== 'insert') {
model.set('post_revisions', [{
post_id: model.id,
lexical: model.previous('lexical'),
created_at_ts: Date.now() - 1
}, {
post_id: model.id,
lexical: model.get('lexical'),
created_at_ts: Date.now()
}]);
} else {
const revisionsJSON = revisions.toJSON().slice(0, POST_REVISIONS_COUNT - 1);
if (!labs.isSet('postsHistory')) {
ops.push(function updateRevisions() {
return ghostBookshelf.model('PostRevision')
.findAll(Object.assign({
filter: `post_id:${model.id}`,
columns: ['id']
}, _.pick(options, 'transacting')))
.then((revisions) => {
// Store previous + latest lexical content
if (!revisions.length && options.method !== 'insert') {
model.set('post_revisions', [{
post_id: model.id,
lexical: model.previous('lexical'),
created_at_ts: Date.now() - 1
}, {
post_id: model.id,
lexical: model.get('lexical'),
created_at_ts: Date.now()
}]);
} else {
const revisionsJSON = revisions.toJSON().slice(0, POST_REVISIONS_COUNT - 1);
model.set('post_revisions', revisionsJSON.concat([{
post_id: model.id,
lexical: model.get('lexical'),
created_at_ts: Date.now()
}]));
}
});
});
model.set('post_revisions', revisionsJSON.concat([{
post_id: model.id,
lexical: model.get('lexical'),
created_at_ts: Date.now()
}]));
}
});
});
} else {
const postRevisions = new PostRevisions({
config: {
max_revisions: POST_REVISIONS_COUNT
}
});
ops.push(async function updateRevisions() {
const revisionModels = await ghostBookshelf.model('PostRevision')
.findAll(Object.assign({
filter: `post_id:${model.id}`,
columns: ['id']
}, _.pick(options, 'transacting')));
const revisions = revisionModels.toJSON();
const previous = {
id: model.id,
lexical: model.previous('lexical')
};
const current = {
id: model.id,
lexical: model.get('lexical')
};
model.set(
'post_revisions',
postRevisions.getRevisions(previous, current, revisions)
);
});
}
}
if (this.get('tiers')) {