From a507072eb8ea1829b8cc3f9977f62d89c59576be Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Mon, 17 Apr 2023 16:14:43 +0100 Subject: [PATCH] Updated PostRevisions to accept html string - We also fix the name of the feature flag - We also correctly await the result of revision generation - We pass the HTML string so we can potentially do an easier word count diff --- ghost/core/core/server/models/post.js | 20 ++++++----- ghost/post-revisions/lib/post-revisions.js | 3 +- ghost/post-revisions/test/hello.test.js | 40 ++++++++++++++-------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index f3afa68306..a30ae50711 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -864,8 +864,8 @@ Post = ghostBookshelf.Model.extend({ }); } - if (model.hasChanged('lexical') && !model.get('mobiledoc') && !options.importing && !options.migrating) { - if (!labs.isSet('postsHistory')) { + if (!labs.isSet('postHistory')) { + if (model.hasChanged('lexical') && !model.get('mobiledoc') && !options.importing && !options.migrating) { ops.push(function updateRevisions() { return ghostBookshelf.model('PostRevision') .findAll(Object.assign({ @@ -895,7 +895,9 @@ Post = ghostBookshelf.Model.extend({ } }); }); - } else { + } + } else { + if (!model.get('mobiledoc') && !options.importing && !options.migrating) { const postRevisions = new PostRevisions({ config: { max_revisions: POST_REVISIONS_COUNT @@ -911,17 +913,17 @@ Post = ghostBookshelf.Model.extend({ const revisions = revisionModels.toJSON(); const previous = { id: model.id, - lexical: model.previous('lexical') + lexical: model.previous('lexical'), + html: model.previous('html') }; const current = { id: model.id, - lexical: model.get('lexical') + lexical: model.get('lexical'), + html: model.get('html') }; - model.set( - 'post_revisions', - postRevisions.getRevisions(previous, current, revisions) - ); + const newRevisions = await postRevisions.getRevisions(previous, current, revisions); + model.set('post_revisions', newRevisions); }); } } diff --git a/ghost/post-revisions/lib/post-revisions.js b/ghost/post-revisions/lib/post-revisions.js index 467db74dd8..5b971645f3 100644 --- a/ghost/post-revisions/lib/post-revisions.js +++ b/ghost/post-revisions/lib/post-revisions.js @@ -2,6 +2,7 @@ * @typedef {object} PostLike * @property {string} id * @property {string} lexical + * @property {string} html */ /** @@ -34,7 +35,7 @@ class PostRevisions { if (revisions.length === 0) { return true; } - return previous.lexical !== current.lexical; + return previous.html !== current.html; } /** diff --git a/ghost/post-revisions/test/hello.test.js b/ghost/post-revisions/test/hello.test.js index c2701014ff..4f3d3a8fb1 100644 --- a/ghost/post-revisions/test/hello.test.js +++ b/ghost/post-revisions/test/hello.test.js @@ -25,14 +25,16 @@ describe('PostRevisions', function () { assert.equal(actual, expected); }); - it('should return false if the current and previous lexical values are the same', function () { + it('should return false if the current and previous html values are the same', function () { const postRevisions = new PostRevisions({config}); const expected = false; const actual = postRevisions.shouldGenerateRevision({ - lexical: 'blah' + lexical: 'previous', + html: 'blah' }, { - lexical: 'blah' + lexical: 'current', + html: 'blah' }, [{ lexical: 'blah' }]); @@ -40,14 +42,16 @@ describe('PostRevisions', function () { assert.equal(actual, expected); }); - it('should return true if the current and previous lexical values are different', function () { + it('should return true if the current and previous html values are different', function () { const postRevisions = new PostRevisions({config}); const expected = true; const actual = postRevisions.shouldGenerateRevision({ - lexical: 'blah' + lexical: 'blah', + html: 'blah' }, { - lexical: 'blah2' + lexical: 'blah', + html: 'blah2' }, [{ lexical: 'blah' }]); @@ -77,9 +81,11 @@ describe('PostRevisions', function () { lexical: 'revision' }]; const actual = await postRevisions.getRevisions({ - lexical: 'blah' + lexical: 'blah', + html: 'blah' }, { - lexical: 'blah' + lexical: 'blah', + html: 'blah' }, [{ lexical: 'revision' }]); @@ -92,10 +98,12 @@ describe('PostRevisions', function () { const actual = await postRevisions.getRevisions({ id: '1', - lexical: 'previous' + lexical: 'previous', + html: 'previous' }, { id: '1', - lexical: 'current' + lexical: 'current', + html: 'current' }, []); assert.equal(actual.length, 2); @@ -112,18 +120,22 @@ describe('PostRevisions', function () { const revisions = await postRevisions.getRevisions({ id: '1', - lexical: 'previous' + lexical: 'previous', + html: 'previous' }, { id: '1', - lexical: 'current' + lexical: 'current', + html: 'current' }, []); const actual = await postRevisions.getRevisions({ id: '1', - lexical: 'old' + lexical: 'old', + html: 'old' }, { id: '1', - lexical: 'new' + lexical: 'new', + html: 'new' }, revisions); assert.equal(actual.length, 2);