mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 09:22:49 +03:00
d5cf0fc03e
refs https://github.com/TryGhost/Ghost/issues/12716 - The code in serializePostModel was broken and always defaulted to 'v3'! It refered to non-existent `model.get('api_version')` there's no such field in posts model! Changed the implementation so that the API version is passed in as a parameter to the method instead - The style of providing "defaults" everywhere creates a need for future maintenance when we bump the version e.g in Ghost v5. Maybe reworking these methods to require a passed version and throwing an error instead would be more maintainable long-term?
84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
const models = require('../../models');
|
|
const {i18n} = require('../../lib/common');
|
|
const errors = require('@tryghost/errors');
|
|
const mega = require('../../services/mega');
|
|
|
|
module.exports = {
|
|
docName: 'email_preview',
|
|
|
|
read: {
|
|
options: [
|
|
'fields'
|
|
],
|
|
validation: {
|
|
options: {
|
|
fields: ['html', 'plaintext', 'subject']
|
|
}
|
|
},
|
|
data: [
|
|
'id',
|
|
'status'
|
|
],
|
|
permissions: true,
|
|
query(frame) {
|
|
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
|
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 replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
|
|
|
replacements.forEach((replacement) => {
|
|
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
|
replacement.match,
|
|
replacement.fallback || ''
|
|
);
|
|
});
|
|
|
|
return emailContent;
|
|
});
|
|
});
|
|
}
|
|
},
|
|
sendTestEmail: {
|
|
statusCode: 200,
|
|
headers: {},
|
|
options: [
|
|
'id'
|
|
],
|
|
validation: {
|
|
options: {
|
|
id: {
|
|
required: true
|
|
}
|
|
}
|
|
},
|
|
permissions: true,
|
|
async query(frame) {
|
|
const options = Object.assign(frame.options, {status: 'all'});
|
|
let model = await models.Post.findOne(options, {withRelated: ['authors']});
|
|
if (!model) {
|
|
throw new errors.NotFoundError({
|
|
message: i18n.t('errors.api.posts.postNotFound')
|
|
});
|
|
}
|
|
const {emails = []} = frame.data;
|
|
const response = await mega.mega.sendTestEmail(model, emails, 'canary');
|
|
if (response && response[0] && response[0].error) {
|
|
throw new errors.EmailError({
|
|
statusCode: response[0].error.statusCode,
|
|
message: response[0].error.message,
|
|
context: response[0].error.originalMessage
|
|
});
|
|
}
|
|
return response;
|
|
}
|
|
}
|
|
};
|