From 02126b064cfdbb42b8e8df22675bc4d56920a1e2 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Wed, 24 Mar 2021 14:03:54 +0100 Subject: [PATCH] refactor out linkdiscoverer to also auto-discover pingbacks --- src/{webmention => }/linkdiscoverer.js | 12 +++++++-- src/pingback/receive.js | 7 +++-- src/pingback/route.js | 4 --- src/pingback/send.js | 9 +++++++ src/webmention/send.js | 6 ++--- test/e2e.test.js | 2 -- test/linkdiscoverer.test.js | 37 ++++++++++++++++++++++++++ test/webmention/linkdiscoverer.test.js | 26 ------------------ 8 files changed, 64 insertions(+), 39 deletions(-) rename src/{webmention => }/linkdiscoverer.js (83%) create mode 100644 src/pingback/send.js create mode 100644 test/linkdiscoverer.test.js delete mode 100644 test/webmention/linkdiscoverer.test.js diff --git a/src/webmention/linkdiscoverer.js b/src/linkdiscoverer.js similarity index 83% rename from src/webmention/linkdiscoverer.js rename to src/linkdiscoverer.js index d8c36e3..18ed621 100644 --- a/src/webmention/linkdiscoverer.js +++ b/src/linkdiscoverer.js @@ -15,17 +15,25 @@ async function discover(target) { const endpoint = await got(target) if(endpoint.headers.link?.indexOf("webmention") >= 0) { // e.g. Link: ; rel="webmention" - return endpoint.headers.link + const link = endpoint.headers.link .split(";")[0] .replace("<" ,"") .replace(">", "") + return { + link, + type: "webmention" + } } const format = mf2(endpoint.body, { // this also complies with w3.org regulations: relative endpoint could be possible baseUrl: baseUrlOf(target) }) - return format.rels?.webmention?.[0] + const link = format.rels?.webmention?.[0] + return { + link, + type: "webmention" + } } catch(err) { console.warn(` -- whoops, failed to discover ${target}, why: ${err}`) return undefined diff --git a/src/pingback/receive.js b/src/pingback/receive.js index cc29fd6..71216f3 100644 --- a/src/pingback/receive.js +++ b/src/pingback/receive.js @@ -40,10 +40,13 @@ function validate(body) { // Wordpress pingback processing source: https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/pingback_ping/ async function receive(body) { const xml = parser.parse(body) - await webmentionReceiver.receive({ + const webmentionBody = { source: xml.methodCall.params.param[0].value.string, target: xml.methodCall.params.param[1].value.string - }) + } + + console.log(` OK: looks like a valid pingback: \n\tsource ${webmentionBody.source}\n\ttarget ${webmentionBody.target}`) + await webmentionReceiver.receive(webmentionBody) } module.exports = { diff --git a/src/pingback/route.js b/src/pingback/route.js index ed7935b..0a6fc56 100644 --- a/src/pingback/route.js +++ b/src/pingback/route.js @@ -67,10 +67,6 @@ function route(router) { ctx.body = err(e) } }); - - router.put("pingback send endpoint", "/pingback/:domain/:token", async (ctx) => { - - }); } module.exports = { diff --git a/src/pingback/send.js b/src/pingback/send.js new file mode 100644 index 0000000..ba664b4 --- /dev/null +++ b/src/pingback/send.js @@ -0,0 +1,9 @@ + +async function send(domain, since) { + const feed = await got(`https://${domain}/index.xml`) + await parseRssFeed(feed.body, since) +} + +module.exports = { + send +} diff --git a/src/webmention/send.js b/src/webmention/send.js index b058ec1..3c69e02 100644 --- a/src/webmention/send.js +++ b/src/webmention/send.js @@ -1,13 +1,13 @@ const got = require('got') const { collect } = require('./rsslinkcollector') -const { discover } = require('./linkdiscoverer') +const { discover } = require('./../linkdiscoverer') async function mention(opts) { const { source, target } = opts const endpoint = await discover(target) if(endpoint) { - await got.post(endpoint, { + await got.post(endpoint.link, { contentType: "x-www-form-urlencoded", form: { source, @@ -18,7 +18,7 @@ async function mention(opts) { methods: ["POST"] } }) - console.log(` OK: webmention@${endpoint}, sent: source ${source}, target ${target}`) + console.log(` OK: ${endpoint.type}@${endpoint.link}, sent: source ${source}, target ${target}`) } } diff --git a/test/e2e.test.js b/test/e2e.test.js index 1f28d80..fe684c5 100644 --- a/test/e2e.test.js +++ b/test/e2e.test.js @@ -19,8 +19,6 @@ describe("e2e tests", () => { const occ = html.indexOf(url) const len = 100 console.log(html.substring(occ - len, occ + url.length + len)) - - ]+?' . $preg_target . '[^>]*>([^>]+?) }) }) diff --git a/test/linkdiscoverer.test.js b/test/linkdiscoverer.test.js new file mode 100644 index 0000000..9eb47f5 --- /dev/null +++ b/test/linkdiscoverer.test.js @@ -0,0 +1,37 @@ + +const { discover } = require('../src/linkdiscoverer') + +describe("link discoverer", () => { + + describe("discovers webmention links", () => { + test("discover link if present in header", async () => { + const result = await discover("https://brainbaking.com/link-discover-test.html") + expect(result).toEqual({ + link: "http://aaronpk.example/webmention-endpoint", + type: "webmention" + }) + }) + + test("discover nothing if no webmention link is present", async() => { + const result = await discover("https://brainbaking.com/link-discover-test-none.html") + expect(result).toBeUndefined() + }) + + test("discover link if sole entry somewhere in html", async () => { + const result = await discover("https://brainbaking.com/link-discover-test-single.html") + expect(result).toEqual({ + link: "http://aaronpk.example/webmention-endpoint-body", + type: "webmention" + }) + }) + + test("use link in header if multiple present in html", async () => { + const result = await discover("https://brainbaking.com/link-discover-test-multiple.html") + expect(result).toEqual({ + link: "http://aaronpk.example/webmention-endpoint-header", + type: "webmention" + }) + }) + }) + +}) diff --git a/test/webmention/linkdiscoverer.test.js b/test/webmention/linkdiscoverer.test.js deleted file mode 100644 index 5470c1e..0000000 --- a/test/webmention/linkdiscoverer.test.js +++ /dev/null @@ -1,26 +0,0 @@ - -const { discover } = require('../../src/webmention/linkdiscoverer') - -describe("link discoverer", () => { - - test("discover link if present in header", async () => { - const result = await discover("https://brainbaking.com/link-discover-test.html") - expect(result).toBe("http://aaronpk.example/webmention-endpoint") - }) - - test("discover nothing if no webmention link is present", async() => { - const result = await discover("https://brainbaking.com/link-discover-test-none.html") - expect(result).toBeUndefined() - }) - - test("discover link if sole entry somewhere in html", async () => { - const result = await discover("https://brainbaking.com/link-discover-test-single.html") - expect(result).toBe("http://aaronpk.example/webmention-endpoint-body") - }) - - test("use link in header if multiple present in html", async () => { - const result = await discover("https://brainbaking.com/link-discover-test-multiple.html") - expect(result).toBe("http://aaronpk.example/webmention-endpoint-header") - }) - -}) \ No newline at end of file