From 560d1681690a050ab88d4f79387ed2986a89d075 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Wed, 18 Jan 2023 12:32:37 +0700 Subject: [PATCH] Added site title and author to Mention entity refs https://github.com/TryGhost/Team/issues/2435 We've made these fields optional, and we may need to extend this to other fields too as we discover more about the data we're able to get access to. --- ghost/webmentions/lib/Mention.js | 32 ++++++++++++++++++++++---- ghost/webmentions/lib/MentionsAPI.js | 2 ++ ghost/webmentions/test/Mention.test.js | 2 ++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ghost/webmentions/lib/Mention.js b/ghost/webmentions/lib/Mention.js index 55e530a406..894759221b 100644 --- a/ghost/webmentions/lib/Mention.js +++ b/ghost/webmentions/lib/Mention.js @@ -44,6 +44,18 @@ module.exports = class Mention { return this.#sourceTitle; } + /** @type {string} */ + #sourceSiteTitle; + get sourceSiteTitle() { + return this.#sourceSiteTitle; + } + + /** @type {string} */ + #sourceAuthor; + get sourceAuthor() { + return this.#sourceAuthor; + } + /** @type {string} */ #sourceExcerpt; get sourceExcerpt() { @@ -71,6 +83,8 @@ module.exports = class Mention { payload: this.payload, resourceId: this.resourceId, sourceTitle: this.sourceTitle, + sourceSiteTitle: this.sourceSiteTitle, + sourceAuthor: this.sourceAuthor, sourceExcerpt: this.sourceExcerpt, sourceFavicon: this.sourceFavicon, sourceFeaturedImage: this.sourceFeaturedImage @@ -86,6 +100,8 @@ module.exports = class Mention { this.#payload = data.payload; this.#resourceId = data.resourceId; this.#sourceTitle = data.sourceTitle; + this.#sourceSiteTitle = data.sourceSiteTitle; + this.#sourceAuthor = data.sourceAuthor; this.#sourceExcerpt = data.sourceExcerpt; this.#sourceFavicon = data.sourceFavicon; this.#sourceFeaturedImage = data.sourceFeaturedImage; @@ -151,6 +167,8 @@ module.exports = class Mention { const sourceTitle = validateString(data.sourceTitle, 191, 'sourceTitle'); const sourceExcerpt = validateString(data.sourceExcerpt, 1000, 'sourceExcerpt'); + const sourceSiteTitle = validateString(data.sourceSiteTitle, 191, 'sourceSiteTitle', false); + const sourceAuthor = validateString(data.sourceAuthor, 191, 'sourceAuthor', false); let sourceFavicon = null; if (data.sourceFavicon instanceof URL) { @@ -174,6 +192,8 @@ module.exports = class Mention { payload, resourceId, sourceTitle, + sourceSiteTitle, + sourceAuthor, sourceExcerpt, sourceFavicon, sourceFeaturedImage @@ -181,11 +201,15 @@ module.exports = class Mention { } }; -function validateString(value, maxlength, name) { +function validateString(value, maxlength, name, required = true) { if (!value) { - throw new ValidationError({ - message: `Missing ${name} for Mention` - }); + if (required) { + throw new ValidationError({ + message: `Missing ${name} for Mention` + }); + } else { + return null; + } } if (typeof value !== 'string') { diff --git a/ghost/webmentions/lib/MentionsAPI.js b/ghost/webmentions/lib/MentionsAPI.js index e2f098e42f..960f2af73b 100644 --- a/ghost/webmentions/lib/MentionsAPI.js +++ b/ghost/webmentions/lib/MentionsAPI.js @@ -98,6 +98,8 @@ module.exports = class MentionsAPI { payload: webmention.payload, resourceId: null, sourceTitle: 'Fake title', + sourceSiteTitle: 'Awesome Site', + sourceAuthor: 'James Bond', sourceExcerpt: 'Wow, what an awesome article, blah blah blah', sourceFavicon: null, sourceFeaturedImage: null diff --git a/ghost/webmentions/test/Mention.test.js b/ghost/webmentions/test/Mention.test.js index 698b9ef6a7..ac90ad5cd0 100644 --- a/ghost/webmentions/test/Mention.test.js +++ b/ghost/webmentions/test/Mention.test.js @@ -22,6 +22,8 @@ describe('Mention', function () { 'payload', 'resourceId', 'sourceTitle', + 'sourceSiteTitle', + 'sourceAuthor', 'sourceExcerpt', 'sourceFavicon', 'sourceFeaturedImage'