From cbaf6e5a748ae30b8da97f9575c9c1f3be5f02ee Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Wed, 25 Nov 2020 13:58:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20backwards=20compatibilit?= =?UTF-8?q?y=20for=20newsletters=20(#12422)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes https://github.com/TryGhost/Ghost/issues/12416 This fixes compatibility for the `send_email_when_published` option for the Posts API. The model layer only allows setting the `email_recipient_filter` column when the `status` is being changed. Because of this we need to withhold the `status` change until after we have determined the `email_recipient_filter`. --- core/server/api/canary/posts.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/core/server/api/canary/posts.js b/core/server/api/canary/posts.js index 4f8dd5eaa6..7f31b56a89 100644 --- a/core/server/api/canary/posts.js +++ b/core/server/api/canary/posts.js @@ -159,10 +159,32 @@ module.exports = { await membersService.checkHostLimit(); } - let model = await models.Post.edit(frame.data.posts[0], frame.options); - + let model; if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) { - frame.options.email_recipient_filter = model.get('visibility') === 'paid' ? 'paid' : 'all'; + await models.Base.transaction(async (transacting) => { + const options = { + ...frame.options, + transacting + }; + + /** + * 1. We need to edit the post first in order to know what the visibility is. + * 2. We can only pass the email_recipient_filter when we change the status. + * + * So, we first edit the post as requested, with all information except the status, + * from there we can determine what the email_recipient_filter should be and then finish + * the edit, with the status and the email_recipient_filter option. + */ + const status = frame.data.posts[0].status; + delete frame.data.posts[0].status; + const interimModel = await models.Post.edit(frame.data.posts[0], options); + frame.data.posts[0].status = status; + + options.email_recipient_filter = interimModel.get('visibility') === 'paid' ? 'paid' : 'all'; + + model = await models.Post.edit(frame.data.posts[0], options); + }); + } else { model = await models.Post.edit(frame.data.posts[0], frame.options); }