From 78bebc6279d2c61843f544f92ec14b4567e8fcf3 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Thu, 9 Feb 2023 17:44:14 +0700 Subject: [PATCH] Refactored the assignment of Mention metadata refs https://github.com/TryGhost/Team/issues/2535 Moving this into a separate method allows us to set the metadata externally from the Mention entity and keep all of the validation, without having duplicate code --- ghost/webmentions/lib/Mention.js | 84 ++++++++++++++++---------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/ghost/webmentions/lib/Mention.js b/ghost/webmentions/lib/Mention.js index 8a989c66d1..15b1990ac9 100644 --- a/ghost/webmentions/lib/Mention.js +++ b/ghost/webmentions/lib/Mention.js @@ -78,6 +78,46 @@ module.exports = class Mention { return this.#sourceFeaturedImage; } + /** + * @param {object} metadata + */ + setSourceMetadata(metadata) { + /** @type {string} */ + let sourceTitle = validateString(metadata.sourceTitle, 2000, 'sourceTitle'); + if (sourceTitle === null) { + sourceTitle = this.#source.host; + } + /** @type {string | null} */ + const sourceExcerpt = validateString(metadata.sourceExcerpt, 2000, 'sourceExcerpt'); + /** @type {string | null} */ + const sourceSiteTitle = validateString(metadata.sourceSiteTitle, 2000, 'sourceSiteTitle'); + /** @type {string | null} */ + const sourceAuthor = validateString(metadata.sourceAuthor, 2000, 'sourceAuthor'); + + /** @type {URL | null} */ + let sourceFavicon = null; + if (metadata.sourceFavicon instanceof URL) { + sourceFavicon = metadata.sourceFavicon; + } else if (metadata.sourceFavicon) { + sourceFavicon = new URL(metadata.sourceFavicon); + } + + /** @type {URL | null} */ + let sourceFeaturedImage = null; + if (metadata.sourceFeaturedImage instanceof URL) { + sourceFeaturedImage = metadata.sourceFeaturedImage; + } else if (metadata.sourceFeaturedImage) { + sourceFeaturedImage = new URL(metadata.sourceFeaturedImage); + } + + this.#sourceTitle = sourceTitle; + this.#sourceExcerpt = sourceExcerpt; + this.#sourceSiteTitle = sourceSiteTitle; + this.#sourceAuthor = sourceAuthor; + this.#sourceFavicon = sourceFavicon; + this.#sourceFeaturedImage = sourceFeaturedImage; + } + #deleted = false; delete() { this.#deleted = true; @@ -108,12 +148,6 @@ module.exports = class Mention { this.#timestamp = data.timestamp; 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; } /** @@ -181,49 +215,17 @@ module.exports = class Mention { } } - /** @type {string} */ - let sourceTitle = validateString(data.sourceTitle, 2000, 'sourceTitle'); - if (sourceTitle === null) { - sourceTitle = source.host; - } - /** @type {string | null} */ - const sourceExcerpt = validateString(data.sourceExcerpt, 2000, 'sourceExcerpt'); - /** @type {string | null} */ - const sourceSiteTitle = validateString(data.sourceSiteTitle, 2000, 'sourceSiteTitle'); - /** @type {string | null} */ - const sourceAuthor = validateString(data.sourceAuthor, 2000, 'sourceAuthor'); - - /** @type {URL | null} */ - let sourceFavicon = null; - if (data.sourceFavicon instanceof URL) { - sourceFavicon = data.sourceFavicon; - } else if (data.sourceFavicon) { - sourceFavicon = new URL(data.sourceFavicon); - } - - /** @type {URL | null} */ - let sourceFeaturedImage = null; - if (data.sourceFeaturedImage instanceof URL) { - sourceFeaturedImage = data.sourceFeaturedImage; - } else if (data.sourceFeaturedImage) { - sourceFeaturedImage = new URL(data.sourceFeaturedImage); - } - const mention = new Mention({ id, source, target, timestamp, payload, - resourceId, - sourceTitle, - sourceSiteTitle, - sourceAuthor, - sourceExcerpt, - sourceFavicon, - sourceFeaturedImage + resourceId }); + mention.setSourceMetadata(data); + if (isNew) { mention.events.push(MentionCreatedEvent.create({mention})); }