refactor out linkdiscoverer to also auto-discover pingbacks

This commit is contained in:
Wouter Groeneveld 2021-03-24 14:03:54 +01:00
parent 7d4408080c
commit 02126b064c
8 changed files with 64 additions and 39 deletions

View File

@ -15,17 +15,25 @@ async function discover(target) {
const endpoint = await got(target)
if(endpoint.headers.link?.indexOf("webmention") >= 0) {
// e.g. Link: <http://aaronpk.example/webmention-endpoint>; 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

View File

@ -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 = {

View File

@ -67,10 +67,6 @@ function route(router) {
ctx.body = err(e)
}
});
router.put("pingback send endpoint", "/pingback/:domain/:token", async (ctx) => {
});
}
module.exports = {

9
src/pingback/send.js Normal file
View File

@ -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
}

View File

@ -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}`)
}
}

View File

@ -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))
<a[^>]+?' . $preg_target . '[^>]*>([^>]+?)</a>
})
})

View File

@ -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"
})
})
})
})

View File

@ -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")
})
})