mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 16:42:17 +03:00
Refactored email-preview ctrl to use async/await
refs https://github.com/TryGhost/Team/issues/694 - async/await has been a standard way to handle async code throughout the codebase. Refactoring it before moving code makes it way easier to reason about similarities between multiple controllers
This commit is contained in:
parent
a39dd7255d
commit
9a9866cf59
@ -22,34 +22,35 @@ module.exports = {
|
|||||||
'status'
|
'status'
|
||||||
],
|
],
|
||||||
permissions: true,
|
permissions: true,
|
||||||
query(frame) {
|
async query(frame) {
|
||||||
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
||||||
const data = Object.assign(frame.data, {status: 'all'});
|
const data = Object.assign(frame.data, {status: 'all'});
|
||||||
return models.Post.findOne(data, options)
|
|
||||||
.then((model) => {
|
|
||||||
if (!model) {
|
|
||||||
throw new errors.NotFoundError({
|
|
||||||
message: i18n.t('errors.api.posts.postNotFound')
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'canary'}).then((emailContent) => {
|
const model = await models.Post.findOne(data, options);
|
||||||
if (labs.isSet('emailCardSegments') && frame.options.memberSegment) {
|
|
||||||
emailContent = mega.postEmailSerializer.renderEmailForSegment(emailContent, frame.options.memberSegment);
|
|
||||||
}
|
|
||||||
|
|
||||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
if (!model) {
|
||||||
|
throw new errors.NotFoundError({
|
||||||
replacements.forEach((replacement) => {
|
message: i18n.t('errors.api.posts.postNotFound')
|
||||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
|
||||||
replacement.match,
|
|
||||||
replacement.fallback || ''
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
return emailContent;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let emailContent = await mega.postEmailSerializer.serialize(model, {
|
||||||
|
isBrowserPreview: true,
|
||||||
|
apiVersion: 'canary'
|
||||||
|
});
|
||||||
|
if (labs.isSet('emailCardSegments') && frame.options.memberSegment) {
|
||||||
|
emailContent = mega.postEmailSerializer.renderEmailForSegment(emailContent, frame.options.memberSegment);
|
||||||
|
}
|
||||||
|
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||||
|
|
||||||
|
replacements.forEach((replacement) => {
|
||||||
|
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||||
|
replacement.match,
|
||||||
|
replacement.fallback || ''
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return emailContent;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
sendTestEmail: {
|
sendTestEmail: {
|
||||||
|
@ -20,30 +20,32 @@ module.exports = {
|
|||||||
'status'
|
'status'
|
||||||
],
|
],
|
||||||
permissions: true,
|
permissions: true,
|
||||||
query(frame) {
|
async query(frame) {
|
||||||
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
||||||
const data = Object.assign(frame.data, {status: 'all'});
|
const data = Object.assign(frame.data, {status: 'all'});
|
||||||
return models.Post.findOne(data, options)
|
|
||||||
.then((model) => {
|
|
||||||
if (!model) {
|
|
||||||
throw new errors.NotFoundError({
|
|
||||||
message: i18n.t('errors.api.posts.postNotFound')
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'v3'}).then((emailContent) => {
|
const model = await models.Post.findOne(data, options);
|
||||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
|
||||||
|
|
||||||
replacements.forEach((replacement) => {
|
if (!model) {
|
||||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
throw new errors.NotFoundError({
|
||||||
replacement.match,
|
message: i18n.t('errors.api.posts.postNotFound')
|
||||||
replacement.fallback || ''
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
return emailContent;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let emailContent = await mega.postEmailSerializer.serialize(model, {
|
||||||
|
isBrowserPreview: true,
|
||||||
|
apiVersion: 'v3'
|
||||||
|
});
|
||||||
|
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||||
|
|
||||||
|
replacements.forEach((replacement) => {
|
||||||
|
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||||
|
replacement.match,
|
||||||
|
replacement.fallback || ''
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return emailContent;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
sendTestEmail: {
|
sendTestEmail: {
|
||||||
|
Loading…
Reference in New Issue
Block a user