Bumped MEGA related serialization code to use v4 API

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?
This commit is contained in:
Naz 2021-03-02 14:31:01 +13:00
parent 72f25a7099
commit d5cf0fc03e
6 changed files with 12 additions and 13 deletions

View File

@ -31,7 +31,7 @@ module.exports = {
});
}
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true}).then((emailContent) => {
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'canary'}).then((emailContent) => {
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
replacements.forEach((replacement) => {
@ -69,7 +69,7 @@ module.exports = {
});
}
const {emails = []} = frame.data;
const response = await mega.mega.sendTestEmail(model, emails);
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,

View File

@ -195,7 +195,7 @@ module.exports = {
let postEmail = model.relations.email;
if (!postEmail) {
const email = await mega.addEmail(model, frame.options);
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);

View File

@ -31,7 +31,7 @@ module.exports = {
});
}
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true}).then((emailContent) => {
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'v3'}).then((emailContent) => {
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
replacements.forEach((replacement) => {
@ -69,7 +69,7 @@ module.exports = {
});
}
const {emails = []} = frame.data;
const response = await mega.mega.sendTestEmail(model, emails);
const response = await mega.mega.sendTestEmail(model, emails, 'v3');
if (response && response[0] && response[0].error) {
throw new errors.EmailError({
statusCode: response[0].error.statusCode,

View File

@ -195,7 +195,7 @@ module.exports = {
let postEmail = model.relations.email;
if (!postEmail) {
const email = await mega.addEmail(model, frame.options);
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);

View File

@ -49,8 +49,8 @@ const getEmailData = async (postModel, options) => {
};
};
const sendTestEmail = async (postModel, toEmails) => {
const emailData = await getEmailData(postModel);
const sendTestEmail = async (postModel, toEmails, apiVersion) => {
const emailData = await getEmailData(postModel, {apiVersion});
emailData.subject = `[Test] ${emailData.subject}`;
// fetch any matching members so that replacements use expected values
@ -128,7 +128,7 @@ const addEmail = async (postModel, options) => {
if (!existing) {
// get email contents and perform replacements using no member data so
// we have a decent snapshot of email content for later display
const emailData = await getEmailData(postModel);
const emailData = await getEmailData(postModel, options);
return models.Email.add({
post_id: postId,

View File

@ -43,10 +43,9 @@ const createUnsubscribeUrl = (uuid) => {
// NOTE: serialization is needed to make sure we are using current API and do post transformations
// such as image URL transformation from relative to absolute
const serializePostModel = async (model) => {
const serializePostModel = async (model, apiVersion = 'v4') => {
// fetch mobiledoc rather than html and plaintext so we can render email-specific contents
const frame = {options: {context: {user: true}, formats: 'mobiledoc'}};
const apiVersion = model.get('api_version') || 'v3';
const docName = 'posts';
await api.shared
@ -120,8 +119,8 @@ const parseReplacements = (email) => {
return replacements;
};
const serialize = async (postModel, options = {isBrowserPreview: false}) => {
const post = await serializePostModel(postModel);
const serialize = async (postModel, options = {isBrowserPreview: false, apiVersion: 'v4'}) => {
const post = await serializePostModel(postModel, options.apiVersion);
const timezone = settingsCache.get('timezone');
const momentDate = post.published_at ? moment(post.published_at) : moment();