From 76640d9b7e0c93290be7dc61228d9320bda22c99 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Wed, 22 Feb 2023 21:02:15 +0800 Subject: [PATCH] Added page resources to emails (#16315) refs https://github.com/TryGhost/Team/issues/2572 - Passes post resource data to emails. - Updated email templates accordingly. - Added snapshot e2e tests. --- ghost/admin/app/models/mention.js | 1 - .../__snapshots__/webmentions.test.js.snap | 457 ++++++++++++++++++ .../e2e-api/webmentions/webmentions.test.js | 58 +++ .../email-templates/new-mention-received.hbs | 2 +- ghost/staff-service/lib/emails.js | 4 +- 5 files changed, 519 insertions(+), 3 deletions(-) create mode 100644 ghost/core/test/e2e-api/webmentions/__snapshots__/webmentions.test.js.snap diff --git a/ghost/admin/app/models/mention.js b/ghost/admin/app/models/mention.js index c1347bafe5..ebda8c14b8 100644 --- a/ghost/admin/app/models/mention.js +++ b/ghost/admin/app/models/mention.js @@ -1,6 +1,5 @@ import Model, {attr} from '@ember-data/model'; -// @todo this is a temporary model until we have the api / db model structure figured out export default Model.extend({ source: attr('string'), target: attr('string'), diff --git a/ghost/core/test/e2e-api/webmentions/__snapshots__/webmentions.test.js.snap b/ghost/core/test/e2e-api/webmentions/__snapshots__/webmentions.test.js.snap new file mode 100644 index 0000000000..524601630c --- /dev/null +++ b/ghost/core/test/e2e-api/webmentions/__snapshots__/webmentions.test.js.snap @@ -0,0 +1,457 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Webmentions (receiving) can display page title in notification email 1: [html 1] 1`] = ` +" + + + + + 💌 New mention from:http://testpage.com/external-article-12345-email-test/ + + + + + + + + + + +
  +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + +
+

Hey there,

+

Your page About this site was mentioned in:

+ + +
+ +
+
Test Page
+
Test description
+
+ + +  • John Doe +
+
+
+
+ + + + + + + + + + +
+ + + + + + +
View mentions
+
+
+

You can also copy & paste this URL into your browser:

+

http://127.0.0.1:2369/ghost/#/mentions

+
+

This message was sent from 127.0.0.1 to jbloggs@example.com

+
+

Don’t want to receive these emails? Manage your preferences here.

+
+
+ + + +
+
 
+ + +" +`; + +exports[`Webmentions (receiving) can display post title in notification email 1: [html 1] 1`] = ` +" + + + + + 💌 New mention from:http://testpage.com/external-article-1234-email-test/ + + + + + + + + + + +
  +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + +
+

Hey there,

+

Your post Setting up apps and custom integrations was mentioned in:

+ + +
+ +
+
Test Page
+
Test description
+
+ + +  • John Doe +
+
+
+
+ + + + + + + + + + +
+ + + + + + +
View mentions
+
+
+

You can also copy & paste this URL into your browser:

+

http://127.0.0.1:2369/ghost/#/mentions

+
+

This message was sent from 127.0.0.1 to jbloggs@example.com

+
+

Don’t want to receive these emails? Manage your preferences here.

+
+
+ + + +
+
 
+ + +" +`; diff --git a/ghost/core/test/e2e-api/webmentions/webmentions.test.js b/ghost/core/test/e2e-api/webmentions/webmentions.test.js index 989505d47c..e562c8b881 100644 --- a/ghost/core/test/e2e-api/webmentions/webmentions.test.js +++ b/ghost/core/test/e2e-api/webmentions/webmentions.test.js @@ -280,6 +280,64 @@ describe('Webmentions (receiving)', function () { emailMockReceiver.sentEmailCount(users.length); }); + it('can display post title in notification email', async function () { + const processWebmentionJob = jobsService.awaitCompletion('processWebmention'); + const targetUrl = new URL('integrations/', urlUtils.getSiteUrl()); + const sourceUrl = new URL('http://testpage.com/external-article-1234-email-test/'); + const html = ` + Test Page + `; + + nock(targetUrl.origin) + .head(targetUrl.pathname) + .reply(200); + + nock(sourceUrl.origin) + .get(sourceUrl.pathname) + .reply(200, html, {'Content-Type': 'text/html'}); + + await agent.post('/receive/') + .body({ + source: sourceUrl.href, + target: targetUrl.href + }) + .expectStatus(202); + + await processWebmentionJob; + await DomainEvents.allSettled(); + + emailMockReceiver.matchHTMLSnapshot(); + }); + + it('can display page title in notification email', async function () { + const processWebmentionJob = jobsService.awaitCompletion('processWebmention'); + const targetUrl = new URL('about/', urlUtils.getSiteUrl()); + const sourceUrl = new URL('http://testpage.com/external-article-12345-email-test/'); + const html = ` + Test Page + `; + + nock(targetUrl.origin) + .head(targetUrl.pathname) + .reply(200); + + nock(sourceUrl.origin) + .get(sourceUrl.pathname) + .reply(200, html, {'Content-Type': 'text/html'}); + + await agent.post('/receive/') + .body({ + source: sourceUrl.href, + target: targetUrl.href + }) + .expectStatus(202); + + await processWebmentionJob; + await DomainEvents.allSettled(); + + emailMockReceiver.matchHTMLSnapshot(); + }); + it('does not send notification with flag disabled', async function () { mockManager.mockLabsDisabled('webmentions'); const processWebmentionJob = jobsService.awaitCompletion('processWebmention'); diff --git a/ghost/staff-service/lib/email-templates/new-mention-received.hbs b/ghost/staff-service/lib/email-templates/new-mention-received.hbs index 8ff48eb027..a68cc28571 100644 --- a/ghost/staff-service/lib/email-templates/new-mention-received.hbs +++ b/ghost/staff-service/lib/email-templates/new-mention-received.hbs @@ -24,7 +24,7 @@

Hey there,

-

Your {{#if resource.type}} post {{else}} page {{/if}} {{#if resource.name}}{{resource.name}}{{else}}{{targetUrl}}{{/if}} was mentioned in:

+

Your {{#if resource.type}} {{resource.type}} {{else}} page {{/if}} {{#if resource.name}}{{resource.name}}{{else}}{{targetUrl}}{{/if}} was mentioned in:

diff --git a/ghost/staff-service/lib/emails.js b/ghost/staff-service/lib/emails.js index ecb665591f..114eb5ee5a 100644 --- a/ghost/staff-service/lib/emails.js +++ b/ghost/staff-service/lib/emails.js @@ -148,11 +148,12 @@ class StaffServiceEmails { if (mention.resourceId) { try { const postModel = await this.models.Post.findOne({id: mention.resourceId.toString()}); + // console.log(postModel.toJSON()); if (postModel) { resource = { id: postModel.id, name: postModel.get('title'), - type: 'post' + type: postModel.get('type') || 'post' }; } } catch (err) { @@ -181,6 +182,7 @@ class StaffServiceEmails { toEmail: to, staffUrl: this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`) }; + const {html, text} = await this.renderEmailTemplate('new-mention-received', templateData); await this.sendMail({