From 9911e6be78132678b163462cd9ca320061f9773c Mon Sep 17 00:00:00 2001 From: Michael Barrett <991592+mike182uk@users.noreply.github.com> Date: Tue, 18 Apr 2023 14:15:26 +0100 Subject: [PATCH] Persisted post revision author and title (#16653) no issue Persisted post revision author and title --- ghost/core/core/server/models/post.js | 9 ++++-- .../admin/__snapshots__/posts.test.js.snap | 12 +++---- ghost/post-revisions/lib/post-revisions.js | 12 +++++-- ghost/post-revisions/test/hello.test.js | 31 +++++++++++++++++-- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index 98f3ba5295..69a66ff991 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -908,6 +908,7 @@ Post = ghostBookshelf.Model.extend({ max_revisions: POST_REVISIONS_COUNT } }); + const authorId = this.contextUser(options); ops.push(async function updateRevisions() { const revisionModels = await ghostBookshelf.model('PostRevision') .findAll(Object.assign({ @@ -919,12 +920,16 @@ Post = ghostBookshelf.Model.extend({ const previous = { id: model.id, lexical: model.previous('lexical'), - html: model.previous('html') + html: model.previous('html'), + author_id: model.previous('updated_by'), + title: model.previous('title') }; const current = { id: model.id, lexical: model.get('lexical'), - html: model.get('html') + html: model.get('html'), + author_id: authorId, + title: model.get('title') }; const newRevisions = await postRevisions.getRevisions(previous, current, revisions); diff --git a/ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap b/ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap index 7d765f55da..cbd9127b5a 100644 --- a/ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap +++ b/ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap @@ -693,13 +693,13 @@ Object { "og_title": null, "post_revisions": Array [ Object { - "author_id": null, + "author_id": "1", "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "created_at_ts": Any, "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "lexical": "{\\"root\\":{\\"children\\":[{\\"children\\":[{\\"detail\\":0,\\"format\\":0,\\"mode\\":\\"normal\\",\\"style\\":\\"\\",\\"text\\":\\"Testing post creation with lexical\\",\\"type\\":\\"text\\",\\"version\\":1}],\\"direction\\":\\"ltr\\",\\"format\\":\\"\\",\\"indent\\":0,\\"type\\":\\"paragraph\\",\\"version\\":1}],\\"direction\\":\\"ltr\\",\\"format\\":\\"\\",\\"indent\\":0,\\"type\\":\\"root\\",\\"version\\":1}}", "post_id": StringMatching /\\[a-f0-9\\]\\{24\\}/, - "title": null, + "title": "Post Revisions Test", }, ], "published_at": null, @@ -722,7 +722,7 @@ exports[`Posts API Can read with post_revisions included 4: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "1480", + "content-length": "1496", "content-type": "application/json; charset=utf-8", "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, @@ -831,7 +831,7 @@ exports[`Posts API Create Can create a post with lexical 2: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "4565", + "content-length": "4574", "content-type": "application/json; charset=utf-8", "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, @@ -1261,7 +1261,7 @@ exports[`Posts API Update Can update a post with lexical 2: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "4495", + "content-length": "4511", "content-type": "application/json; charset=utf-8", "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, @@ -1371,7 +1371,7 @@ exports[`Posts API Update Can update a post with lexical 4: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "5012", + "content-length": "5044", "content-type": "application/json; charset=utf-8", "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, diff --git a/ghost/post-revisions/lib/post-revisions.js b/ghost/post-revisions/lib/post-revisions.js index 0b472c6598..b6515bc66e 100644 --- a/ghost/post-revisions/lib/post-revisions.js +++ b/ghost/post-revisions/lib/post-revisions.js @@ -3,6 +3,8 @@ * @property {string} id * @property {string} lexical * @property {string} html + * @property {string} author_id + * @property {string} title */ /** @@ -10,6 +12,8 @@ * @property {string} post_id * @property {string} lexical * @property {number} created_at_ts + * @property {string} author_id + * @property {string} title */ class PostRevisions { @@ -35,7 +39,7 @@ class PostRevisions { if (revisions.length === 0) { return true; } - return previous.html !== current.html; + return previous.html !== current.html || previous.title !== current.title; } /** @@ -57,7 +61,7 @@ class PostRevisions { ]; } - return [...revisions, currentRevision].slice(-this.config.max_revisions); + return [currentRevision, ...revisions].slice(0, this.config.max_revisions); } /** @@ -68,7 +72,9 @@ class PostRevisions { return { post_id: input.id, lexical: input.lexical, - created_at_ts: Date.now() - offset + created_at_ts: Date.now() - offset, + author_id: input.author_id, + title: input.title }; } } diff --git a/ghost/post-revisions/test/hello.test.js b/ghost/post-revisions/test/hello.test.js index 315cadc172..ec11bec9cb 100644 --- a/ghost/post-revisions/test/hello.test.js +++ b/ghost/post-revisions/test/hello.test.js @@ -58,6 +58,25 @@ describe('PostRevisions', function () { assert.equal(actual, expected); }); + + it('should return true if the current and previous title values are different', function () { + const postRevisions = new PostRevisions({config}); + + const expected = true; + const actual = postRevisions.shouldGenerateRevision({ + lexical: 'blah', + html: 'blah', + title: 'blah' + }, { + lexical: 'blah', + html: 'blah', + title: 'blah2' + }, [{ + lexical: 'blah' + }]); + + assert.equal(actual, expected); + }); }); describe('getRevisions', function () { @@ -93,21 +112,27 @@ describe('PostRevisions', function () { assert.deepEqual(actual, expected); }); - it('returns one revisions when there are no existing revisions', async function () { + it('returns one revision when there are no existing revisions', async function () { const postRevisions = new PostRevisions({config}); const actual = await postRevisions.getRevisions({ id: '1', lexical: 'previous', - html: 'previous' + html: 'previous', + author_id: '123', + title: 'foo bar baz' }, { id: '1', lexical: 'current', - html: 'current' + html: 'current', + author_id: '123', + title: 'foo bar baz' }, []); assert.equal(actual.length, 1); assert.equal(actual[0].lexical, 'current'); + assert.equal(actual[0].author_id, '123'); + assert.equal(actual[0].title, 'foo bar baz'); }); it('limits the number of revisions to the max_revisions count', async function () {