From 90b7a3f4d095a8aee185460ce6473dc1a9fb33bb Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Mon, 13 Feb 2023 19:18:17 +0700 Subject: [PATCH] Added verified member and verify method to Mention entity closes https://github.com/TryGhost/Team/issues/2548 Rather than use a setter here we've used a verify method which takes the HTML string and naively validates that the target URL is present. This is so that the logic of verification is encapsulated in the Mention, and should mean that erroneous verification doesn't happen. We could consider down the line that the verify method fetches the content itself, but if we're to do that we should pass in `got` as a param, so that it's possible to stub in tests. One thing to think about when it comes time to making this as performant as possible is doing a single fetch of the source document and using that for verification and metadata extraction. At that point we should probably consolidate both of those operations, either moving the metadata extraction into the Mention entity (passing in any necessary deps) OR we move the verification out to the same layer as metadata extraction. --- ghost/webmentions/lib/Mention.js | 17 +++++++++++++++++ ghost/webmentions/test/Mention.test.js | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/ghost/webmentions/lib/Mention.js b/ghost/webmentions/lib/Mention.js index 15b1990ac9..7b52cbce82 100644 --- a/ghost/webmentions/lib/Mention.js +++ b/ghost/webmentions/lib/Mention.js @@ -12,6 +12,23 @@ module.exports = class Mention { return this.#id; } + /** @type {boolean} */ + #verified = false; + get verified() { + return this.#verified; + } + + /** + * @param {string} html + */ + verify(html) { + if (html.includes(this.target.href)) { + this.#verified = true; + } else { + this.#verified = false; + } + } + /** @type {URL} */ #source; get source() { diff --git a/ghost/webmentions/test/Mention.test.js b/ghost/webmentions/test/Mention.test.js index 6a71158a43..64b3e423aa 100644 --- a/ghost/webmentions/test/Mention.test.js +++ b/ghost/webmentions/test/Mention.test.js @@ -32,6 +32,19 @@ describe('Mention', function () { }); }); + describe('verify', function () { + it('Does basic check for the target URL and updates verified property', async function () { + const mention = await Mention.create(validInput); + assert(!mention.verified); + + mention.verify(''); + assert(mention.verified); + + mention.verify(''); + assert(!mention.verified); + }); + }); + describe('create', function () { it('Will error with invalid inputs', async function () { const invalidInputs = [