From 30f8b0a4462eb6e9adc0a52df6c52c13e73e8b49 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 5 May 2022 12:16:20 +0800 Subject: [PATCH] Added support for html templates in version emails refs https://github.com/TryGhost/Toolbox/issues/292 - The html/text emails is a desired system that's used in Ghost core and should be reused with version mismatch notification emails too. - Currently there's only one template defined "generic-mismatch" and the original file for it can be found under /templates/generic-mismatch.html - If we need to distinguish user agents we can addd more templates to the `/templates/` folder --- .../lib/api-version-compatibility-service.js | 34 +++- .../lib/templates/generic-mismatch.html | 168 ++++++++++++++++++ .../package.json | 3 + .../api-version-compatibility-service.test.js | 111 ++++++++++-- 4 files changed, 289 insertions(+), 27 deletions(-) create mode 100644 ghost/api-version-compatibility-service/lib/templates/generic-mismatch.html diff --git a/ghost/api-version-compatibility-service/lib/api-version-compatibility-service.js b/ghost/api-version-compatibility-service/lib/api-version-compatibility-service.js index 757fedde2e..f16faf2875 100644 --- a/ghost/api-version-compatibility-service/lib/api-version-compatibility-service.js +++ b/ghost/api-version-compatibility-service/lib/api-version-compatibility-service.js @@ -1,3 +1,6 @@ +const path = require('path'); +const EmailContentGenerator = require('@tryghost/email-content-generator'); + class APIVersionCompatibilityService { /** * @@ -6,30 +9,43 @@ class APIVersionCompatibilityService { * @param {() => Promise} options.fetchEmailsToNotify - email address to receive notifications * @param {(acceptVersion: String) => Promise} options.fetchHandled - retrives already handled compatibility notifications * @param {(acceptVersion: String) => Promise} options.saveHandled - persists already handled compatibility notifications + * @param {Function} options.getSiteUrl + * @param {Function} options.getSiteTitle */ - constructor({sendEmail, fetchEmailsToNotify, fetchHandled, saveHandled}) { + constructor({sendEmail, fetchEmailsToNotify, fetchHandled, saveHandled, getSiteUrl, getSiteTitle}) { this.sendEmail = sendEmail; this.fetchEmailsToNotify = fetchEmailsToNotify; this.fetchHandled = fetchHandled; this.saveHandled = saveHandled; + + this.emailContentGenerator = new EmailContentGenerator({ + getSiteUrl, + getSiteTitle, + templatesDir: path.join(__dirname, 'templates') + }); } async handleMismatch({acceptVersion, contentVersion, userAgent = ''}) { if (!await this.fetchHandled(acceptVersion)) { const trimmedUseAgent = userAgent.split('/')[0]; - const htmlContent = ` - ${trimmedUseAgent} integration expected Ghost version: ${acceptVersion} - Current Ghost version: ${contentVersion} - `; - const textContent = htmlContent; - const emails = await this.fetchEmailsToNotify(); + for (const email of emails) { + const {html, text} = await this.emailContentGenerator.getContent({ + template: 'generic-mismatch', + data: { + acceptVersion, + contentVersion, + clientName: trimmedUseAgent, + recipientEmail: email + } + }); + await this.sendEmail({ subject: `Attention required: Your ${trimmedUseAgent} integration has failed`, to: email, - html: htmlContent, - text: textContent + html, + text }); } diff --git a/ghost/api-version-compatibility-service/lib/templates/generic-mismatch.html b/ghost/api-version-compatibility-service/lib/templates/generic-mismatch.html new file mode 100644 index 0000000000..dc92399c91 --- /dev/null +++ b/ghost/api-version-compatibility-service/lib/templates/generic-mismatch.html @@ -0,0 +1,168 @@ + + + + + +Integration error + + + + + + + + + + +
  + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + +
+

Uh-oh!

+
+

+ Ghost has noticed that your {{clientName}} is no longer working as expected. To resolve, this integration must be updated by its developer to work with the your version of Ghost. +

+ +

+ To help you get things fixed as quickly as possible, Ghost has automatically generated some helpful information about the error that you can share with the creator of the Cove integration below: +

+ +

+ {{clientName}} integration expected Ghost version:  {{acceptVersion}} +
+ Current Ghost version:  {{contentVersion}} +
+ Failed request URL:  /content/posts/1234/ +

+
+
+ +
+ + +
+
 
+ + diff --git a/ghost/api-version-compatibility-service/package.json b/ghost/api-version-compatibility-service/package.json index 04bb7a6c56..c909471416 100644 --- a/ghost/api-version-compatibility-service/package.json +++ b/ghost/api-version-compatibility-service/package.json @@ -24,5 +24,8 @@ "c8": "7.11.2", "mocha": "10.0.0", "sinon": "13.0.2" + }, + "dependencies": { + "@tryghost/email-content-generator": "^0.1.0" } } diff --git a/ghost/api-version-compatibility-service/test/api-version-compatibility-service.test.js b/ghost/api-version-compatibility-service/test/api-version-compatibility-service.test.js index 28be1888d8..a0c2a1ec7f 100644 --- a/ghost/api-version-compatibility-service/test/api-version-compatibility-service.test.js +++ b/ghost/api-version-compatibility-service/test/api-version-compatibility-service.test.js @@ -3,6 +3,9 @@ const sinon = require('sinon'); const APIVersionCompatibilityService = require('../index'); describe('APIVersionCompatibilityService', function () { + const getSiteUrl = () => 'https://amazeballsghostsite.com'; + const getSiteTitle = () => 'Tahini and chickpeas'; + afterEach(function () { sinon.reset(); }); @@ -16,7 +19,9 @@ describe('APIVersionCompatibilityService', function () { sendEmail, fetchEmailsToNotify: async () => ['test_env@example.com'], fetchHandled, - saveHandled + saveHandled, + getSiteUrl, + getSiteTitle }); await compatibilityService.handleMismatch({ @@ -28,8 +33,20 @@ describe('APIVersionCompatibilityService', function () { assert.equal(sendEmail.called, true); assert.equal(sendEmail.args[0][0].to, 'test_env@example.com'); assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`); - assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/); - assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/); + + assert.match(sendEmail.args[0][0].html, /Ghost has noticed that your Elaborate Fox<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version:<\/strong>  v4.5/); + assert.match(sendEmail.args[0][0].html, /Current Ghost version:<\/strong>  v5.1/); + + assert.match(sendEmail.args[0][0].html, /This email was sent from ['test_env@example.com'], fetchHandled, - saveHandled + saveHandled, + getSiteUrl, + getSiteTitle }); await compatibilityService.handleMismatch({ @@ -53,11 +72,23 @@ describe('APIVersionCompatibilityService', function () { userAgent: 'Elaborate Fox' }); - assert.equal(sendEmail.calledOnce, true); + assert.equal(sendEmail.called, true); assert.equal(sendEmail.args[0][0].to, 'test_env@example.com'); assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`); - assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/); - assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/); + + assert.match(sendEmail.args[0][0].html, /Ghost has noticed that your Elaborate Fox<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version:<\/strong>  v4.5/); + assert.match(sendEmail.args[0][0].html, /Current Ghost version:<\/strong>  v5.1/); + + assert.match(sendEmail.args[0][0].html, /This email was sent from ['test_env@example.com', 'test_env2@example.com'], fetchHandled, - saveHandled + saveHandled, + getSiteUrl, + getSiteTitle }); await compatibilityService.handleMismatch({ @@ -92,14 +125,38 @@ describe('APIVersionCompatibilityService', function () { assert.equal(sendEmail.calledTwice, true); assert.equal(sendEmail.args[0][0].to, 'test_env@example.com'); assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`); - assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/); - assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/); + + assert.match(sendEmail.args[0][0].html, /Ghost has noticed that your Elaborate Fox<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version:<\/strong>  v4.5/); + assert.match(sendEmail.args[0][0].html, /Current Ghost version:<\/strong>  v5.1/); + + assert.match(sendEmail.args[0][0].html, /This email was sent from Elaborate Fox<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[1][0].html, /Elaborate Fox integration expected Ghost version:<\/strong>  v4.5/); + assert.match(sendEmail.args[1][0].html, /Current Ghost version:<\/strong>  v5.1/); + + assert.match(sendEmail.args[1][0].html, /This email was sent from Elaborate Fox<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[2][0].html, /Elaborate Fox integration expected Ghost version:<\/strong>  v4.8/); + assert.match(sendEmail.args[2][0].html, /Current Ghost version:<\/strong>  v5.1/); + assert.match(sendEmail.args[2][0].text, /Ghost has noticed that your Elaborate Fox is no longer working as expected\./); + assert.match(sendEmail.args[2][0].text, /Elaborate Fox integration expected Ghost version:v4.8/); + assert.match(sendEmail.args[2][0].text, /Current Ghost version:v5.1/); }); it('Trims down the name of the integration when a lot of meta information is present in user-agent header', async function (){ @@ -123,7 +184,9 @@ describe('APIVersionCompatibilityService', function () { sendEmail, fetchEmailsToNotify: async () => ['test_env@example.com'], fetchHandled, - saveHandled + saveHandled, + getSiteUrl, + getSiteTitle }); await compatibilityService.handleMismatch({ @@ -135,7 +198,19 @@ describe('APIVersionCompatibilityService', function () { assert.equal(sendEmail.called, true); assert.equal(sendEmail.args[0][0].to, 'test_env@example.com'); assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Zapier integration has failed`); - assert.match(sendEmail.args[0][0].html, /Zapier integration expected Ghost version: v4.5/); - assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/); + + assert.match(sendEmail.args[0][0].html, /Ghost has noticed that your Zapier<\/strong> is no longer working as expected\./); + assert.match(sendEmail.args[0][0].html, /Zapier integration expected Ghost version:<\/strong>  v4.5/); + assert.match(sendEmail.args[0][0].html, /Current Ghost version:<\/strong>  v5.1/); + + assert.match(sendEmail.args[0][0].html, /This email was sent from