mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Extracted post edit logic to posts service
refs https://github.com/TryGhost/Team/issues/949 - The post model handling related to newsletter sending and email recipient filter logic were duplicating across v3/v4(canary) APIs and it made sense to extract it into a posts service. - This will allow for a central place to handle about to land logic for email_only newsletter handling.
This commit is contained in:
parent
15073bad29
commit
bd8a45d094
3 changed files with 65 additions and 114 deletions
|
@ -159,63 +159,7 @@ module.exports = {
|
|||
unsafeAttrs: unsafeAttrs
|
||||
},
|
||||
async query(frame) {
|
||||
let model;
|
||||
if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**Handle newsletter email */
|
||||
const emailRecipientFilter = model.get('email_recipient_filter');
|
||||
if (emailRecipientFilter !== 'none') {
|
||||
if (emailRecipientFilter !== 'all') {
|
||||
// check filter is valid
|
||||
try {
|
||||
await models.Member.findPage({filter: `subscribed:true+${emailRecipientFilter}`, limit: 1});
|
||||
} catch (err) {
|
||||
return Promise.reject(new BadRequestError({
|
||||
message: i18n.t('errors.api.posts.invalidEmailRecipientFilter'),
|
||||
context: err.message
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
||||
if (postPublished) {
|
||||
let postEmail = model.relations.email;
|
||||
|
||||
if (!postEmail) {
|
||||
const email = await mega.addEmail(model, Object.assign({}, frame.options, {apiVersion: 'canary'}));
|
||||
model.set('email', email);
|
||||
} else if (postEmail && postEmail.get('status') === 'failed') {
|
||||
const email = await mega.retryFailedEmail(postEmail);
|
||||
model.set('email', email);
|
||||
}
|
||||
}
|
||||
}
|
||||
let model = await postsService.editPost(frame);
|
||||
|
||||
this.headers.cacheInvalidate = postsService.handleCacheInvalidation(model);
|
||||
|
||||
|
|
|
@ -159,63 +159,7 @@ module.exports = {
|
|||
unsafeAttrs: unsafeAttrs
|
||||
},
|
||||
async query(frame) {
|
||||
let model;
|
||||
if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**Handle newsletter email */
|
||||
const emailRecipientFilter = model.get('email_recipient_filter');
|
||||
if (emailRecipientFilter !== 'none') {
|
||||
if (emailRecipientFilter !== 'all') {
|
||||
// check filter is valid
|
||||
try {
|
||||
await models.Member.findPage({filter: `subscribed:true+${emailRecipientFilter}`, limit: 1});
|
||||
} catch (err) {
|
||||
return Promise.reject(new BadRequestError({
|
||||
message: i18n.t('errors.api.posts.invalidEmailRecipientFilter'),
|
||||
context: err.message
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
||||
if (postPublished) {
|
||||
let postEmail = model.relations.email;
|
||||
|
||||
if (!postEmail) {
|
||||
const email = await mega.addEmail(model, Object.assign({}, frame.options, {apiVersion: 'v3'}));
|
||||
model.set('email', email);
|
||||
} else if (postEmail && postEmail.get('status') === 'failed') {
|
||||
const email = await mega.retryFailedEmail(postEmail);
|
||||
model.set('email', email);
|
||||
}
|
||||
}
|
||||
}
|
||||
let model = await postsService.editPost(frame);
|
||||
|
||||
this.headers.cacheInvalidate = postsService.handleCacheInvalidation(model);
|
||||
|
||||
|
|
|
@ -9,6 +9,69 @@ class PostsService {
|
|||
this.models = models;
|
||||
}
|
||||
|
||||
async editPost(frame) {
|
||||
let model;
|
||||
|
||||
if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) {
|
||||
await this.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 this.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 this.models.Post.edit(frame.data.posts[0], options);
|
||||
});
|
||||
} else {
|
||||
model = await this.models.Post.edit(frame.data.posts[0], frame.options);
|
||||
}
|
||||
|
||||
/**Handle newsletter email */
|
||||
const emailRecipientFilter = model.get('email_recipient_filter');
|
||||
if (emailRecipientFilter !== 'none') {
|
||||
if (emailRecipientFilter !== 'all') {
|
||||
// check filter is valid
|
||||
try {
|
||||
await this.models.Member.findPage({filter: `subscribed:true+${emailRecipientFilter}`, limit: 1});
|
||||
} catch (err) {
|
||||
return Promise.reject(new BadRequestError({
|
||||
message: this.i18n.t('errors.api.posts.invalidEmailRecipientFilter'),
|
||||
context: err.message
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
||||
if (postPublished) {
|
||||
let postEmail = model.relations.email;
|
||||
|
||||
if (!postEmail) {
|
||||
const email = await this.mega.addEmail(model, Object.assign({}, frame.options, {apiVersion: this.apiVersion}));
|
||||
model.set('email', email);
|
||||
} else if (postEmail && postEmail.get('status') === 'failed') {
|
||||
const email = await this.mega.retryFailedEmail(postEmail);
|
||||
model.set('email', email);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
handleCacheInvalidation(model) {
|
||||
let cacheInvalidate;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue