2021-03-17 18:10:40 +01:00
|
|
|
|
|
|
|
const got = require('got')
|
|
|
|
const { collect } = require('./rsslinkcollector')
|
2021-03-24 14:03:54 +01:00
|
|
|
const { discover } = require('./../linkdiscoverer')
|
2021-03-24 15:34:08 +01:00
|
|
|
const { sendPingbackToEndpoint } = require('./../pingback/send')
|
|
|
|
|
2021-04-05 17:44:27 +02:00
|
|
|
const log = require('pino')()
|
|
|
|
|
2021-03-24 15:34:08 +01:00
|
|
|
async function sendWebmentionToEndpoint(endpoint, source, target) {
|
|
|
|
await got.post(endpoint, {
|
|
|
|
contentType: "x-www-form-urlencoded",
|
|
|
|
form: {
|
|
|
|
source,
|
|
|
|
target
|
|
|
|
},
|
|
|
|
retry: {
|
|
|
|
limit: 5,
|
|
|
|
methods: ["POST"]
|
|
|
|
}
|
|
|
|
})
|
2021-04-05 17:44:27 +02:00
|
|
|
log.info(` OK: webmention@${endpoint}, sent: source ${source}, target ${target}`)
|
2021-03-24 15:34:08 +01:00
|
|
|
}
|
2021-03-17 18:10:40 +01:00
|
|
|
|
|
|
|
async function mention(opts) {
|
|
|
|
const { source, target } = opts
|
|
|
|
const endpoint = await discover(target)
|
2021-03-24 15:34:08 +01:00
|
|
|
const sendMention = {
|
|
|
|
"webmention": sendWebmentionToEndpoint,
|
2021-03-24 15:52:30 +01:00
|
|
|
"pingback": sendPingbackToEndpoint,
|
|
|
|
"unknown": async function() {}
|
2021-03-17 18:10:40 +01:00
|
|
|
}
|
2021-03-24 15:34:08 +01:00
|
|
|
await sendMention[endpoint.type](endpoint.link, source, target)
|
2021-03-17 18:10:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function parseRssFeed(xml, since) {
|
|
|
|
const linksToMention = collect(xml, since)
|
|
|
|
.map(el => el.hrefs
|
|
|
|
// this strips relative URLs; could be a feature to also send these to own domain?
|
|
|
|
.filter(href => href.startsWith('http'))
|
|
|
|
.map(href => {
|
|
|
|
return {
|
|
|
|
// SOURCE is own domain this time, TARGET = outbound
|
|
|
|
target: href,
|
|
|
|
source: el.link
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.flat()
|
|
|
|
|
|
|
|
await Promise.all(linksToMention.map(mention))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function send(domain, since) {
|
|
|
|
const feed = await got(`https://${domain}/index.xml`)
|
|
|
|
await parseRssFeed(feed.body, since)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
send
|
|
|
|
}
|