forked from wgroeneveld/go-jamming
refactor out linkdiscoverer to also auto-discover pingbacks
This commit is contained in:
parent
7d4408080c
commit
02126b064c
|
@ -15,17 +15,25 @@ async function discover(target) {
|
||||||
const endpoint = await got(target)
|
const endpoint = await got(target)
|
||||||
if(endpoint.headers.link?.indexOf("webmention") >= 0) {
|
if(endpoint.headers.link?.indexOf("webmention") >= 0) {
|
||||||
// e.g. Link: <http://aaronpk.example/webmention-endpoint>; rel="webmention"
|
// e.g. Link: <http://aaronpk.example/webmention-endpoint>; rel="webmention"
|
||||||
return endpoint.headers.link
|
const link = endpoint.headers.link
|
||||||
.split(";")[0]
|
.split(";")[0]
|
||||||
.replace("<" ,"")
|
.replace("<" ,"")
|
||||||
.replace(">", "")
|
.replace(">", "")
|
||||||
|
return {
|
||||||
|
link,
|
||||||
|
type: "webmention"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const format = mf2(endpoint.body, {
|
const format = mf2(endpoint.body, {
|
||||||
// this also complies with w3.org regulations: relative endpoint could be possible
|
// this also complies with w3.org regulations: relative endpoint could be possible
|
||||||
baseUrl: baseUrlOf(target)
|
baseUrl: baseUrlOf(target)
|
||||||
})
|
})
|
||||||
return format.rels?.webmention?.[0]
|
const link = format.rels?.webmention?.[0]
|
||||||
|
return {
|
||||||
|
link,
|
||||||
|
type: "webmention"
|
||||||
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.warn(` -- whoops, failed to discover ${target}, why: ${err}`)
|
console.warn(` -- whoops, failed to discover ${target}, why: ${err}`)
|
||||||
return undefined
|
return undefined
|
|
@ -40,10 +40,13 @@ function validate(body) {
|
||||||
// Wordpress pingback processing source: https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/pingback_ping/
|
// Wordpress pingback processing source: https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/pingback_ping/
|
||||||
async function receive(body) {
|
async function receive(body) {
|
||||||
const xml = parser.parse(body)
|
const xml = parser.parse(body)
|
||||||
await webmentionReceiver.receive({
|
const webmentionBody = {
|
||||||
source: xml.methodCall.params.param[0].value.string,
|
source: xml.methodCall.params.param[0].value.string,
|
||||||
target: xml.methodCall.params.param[1].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 = {
|
module.exports = {
|
||||||
|
|
|
@ -67,10 +67,6 @@ function route(router) {
|
||||||
ctx.body = err(e)
|
ctx.body = err(e)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put("pingback send endpoint", "/pingback/:domain/:token", async (ctx) => {
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -1,13 +1,13 @@
|
||||||
|
|
||||||
const got = require('got')
|
const got = require('got')
|
||||||
const { collect } = require('./rsslinkcollector')
|
const { collect } = require('./rsslinkcollector')
|
||||||
const { discover } = require('./linkdiscoverer')
|
const { discover } = require('./../linkdiscoverer')
|
||||||
|
|
||||||
async function mention(opts) {
|
async function mention(opts) {
|
||||||
const { source, target } = opts
|
const { source, target } = opts
|
||||||
const endpoint = await discover(target)
|
const endpoint = await discover(target)
|
||||||
if(endpoint) {
|
if(endpoint) {
|
||||||
await got.post(endpoint, {
|
await got.post(endpoint.link, {
|
||||||
contentType: "x-www-form-urlencoded",
|
contentType: "x-www-form-urlencoded",
|
||||||
form: {
|
form: {
|
||||||
source,
|
source,
|
||||||
|
@ -18,7 +18,7 @@ async function mention(opts) {
|
||||||
methods: ["POST"]
|
methods: ["POST"]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(` OK: webmention@${endpoint}, sent: source ${source}, target ${target}`)
|
console.log(` OK: ${endpoint.type}@${endpoint.link}, sent: source ${source}, target ${target}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,6 @@ describe("e2e tests", () => {
|
||||||
const occ = html.indexOf(url)
|
const occ = html.indexOf(url)
|
||||||
const len = 100
|
const len = 100
|
||||||
console.log(html.substring(occ - len, occ + url.length + len))
|
console.log(html.substring(occ - len, occ + url.length + len))
|
||||||
|
|
||||||
<a[^>]+?' . $preg_target . '[^>]*>([^>]+?)</a>
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -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"
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
|
@ -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")
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
Loading…
Reference in New Issue