From 6189040fc45972d127ffde6d42b1c2d9cda85e1a Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Wed, 3 May 2023 09:20:34 +0200 Subject: [PATCH] Added `unpublished` reason to post revisions (#16726) closes https://github.com/TryGhost/Team/issues/3137 We have now included the ability to display an "Unpublished" tag for revisions that have been unpublished. The tag is only displayed when the revision's reason property is set to "unpublished". A new revision is triggered when a post is unpublished, regardless whether there's a change in the content. --- .../app/components/modal-post-history.hbs | 2 +- .../app/components/modal-post-history.js | 3 +-- ghost/core/core/server/models/post.js | 4 +++- ghost/post-revisions/lib/post-revisions.js | 6 ++++++ ghost/post-revisions/test/hello.test.js | 21 +++++++++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ghost/admin/app/components/modal-post-history.hbs b/ghost/admin/app/components/modal-post-history.hbs index e9cfe22e69..ae1a4a6dc1 100644 --- a/ghost/admin/app/components/modal-post-history.hbs +++ b/ghost/admin/app/components/modal-post-history.hbs @@ -66,7 +66,7 @@ {{#if revision.new_publish}} Published {{/if}} - {{#if revision.new_unpublish}} + {{#if (eq revision.reason "unpublished")}} Unpublished {{/if}} diff --git a/ghost/admin/app/components/modal-post-history.js b/ghost/admin/app/components/modal-post-history.js index a70c38a740..07c3967de2 100644 --- a/ghost/admin/app/components/modal-post-history.js +++ b/ghost/admin/app/components/modal-post-history.js @@ -71,8 +71,7 @@ export default class ModalPostHistory extends Component { }, postStatus: revision.get('postStatus'), reason: revision.get('reason'), - new_publish: revision.get('postStatus') === 'published' && revisions[index + 1]?.get('postStatus') === 'draft', - new_unpublish: revision.get('postStatus') === 'draft' && revisions[index + 1]?.get('postStatus') === 'published' + new_publish: revision.get('postStatus') === 'published' && revisions[index + 1]?.get('postStatus') === 'draft' }; }); } diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index d6129ef731..93902ef0b3 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -935,7 +935,9 @@ Post = ghostBookshelf.Model.extend({ // This can be refactored once we have the status stored in each revision const revisionOptions = { forceRevision: options.save_revision, - isPublished: newStatus === 'published' + isPublished: newStatus === 'published', + newStatus, + olderStatus }; const newRevisions = await postRevisions.getRevisions(current, revisions, revisionOptions); model.set('post_revisions', newRevisions); diff --git a/ghost/post-revisions/lib/post-revisions.js b/ghost/post-revisions/lib/post-revisions.js index b0ed841108..011211e368 100644 --- a/ghost/post-revisions/lib/post-revisions.js +++ b/ghost/post-revisions/lib/post-revisions.js @@ -52,6 +52,12 @@ class PostRevisions { if (revisions.length === 0) { return {value: true, reason: 'initial_revision'}; } + // check if the post has been unpublished + const isUnpublished = options && options.newStatus === 'draft' && options.olderStatus === 'published'; + if (isUnpublished) { + return {value: true, reason: 'unpublished'}; + } + // check if the post has been published const isPublished = options && options.isPublished; if (isPublished) { return {value: true, reason: 'published'}; diff --git a/ghost/post-revisions/test/hello.test.js b/ghost/post-revisions/test/hello.test.js index 7d32847572..9df3b27ccf 100644 --- a/ghost/post-revisions/test/hello.test.js +++ b/ghost/post-revisions/test/hello.test.js @@ -89,6 +89,27 @@ describe('PostRevisions', function () { assert.deepEqual(actual, expected); }); + it('should return true if post is unpublished and forceRevision is true', function () { + const postRevisions = new PostRevisions({config}); + + const expected = {value: true, reason: 'unpublished'}; + + const actual = postRevisions.shouldGenerateRevision({ + lexical: 'blah', + html: 'blah', + title: 'blah2' + }, [{ + lexical: 'blah' + }], { + isPublished: false, + forceRevision: true, + newStatus: 'draft', + olderStatus: 'published' + }); + + assert.deepEqual(actual, expected); + }); + it('should always return true if isPublished is true', function () { const postRevisions = new PostRevisions({config});