go-jamming/src/webmention/receive.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-03-07 20:30:14 +01:00
const got = require('got')
const config = require('./../config')
2021-03-07 20:30:14 +01:00
function isValidUrl(url) {
2021-03-07 20:41:54 +01:00
return url !== undefined &&
2021-03-07 20:30:14 +01:00
(url.startsWith("http://") || url.startsWith("https://"))
}
function isValidDomain(url) {
return config.allowedWebmentionSources.some(domain => {
return url.indexOf(domain) !== -1
})
}
2021-03-07 20:30:14 +01:00
/**
Remember, TARGET is own domain, SOURCE is the article to process
2021-03-07 20:30:14 +01:00
https://www.w3.org/TR/webmention/#sender-notifies-receiver
example:
POST /webmention-endpoint HTTP/1.1
Host: aaronpk.example
Content-Type: application/x-www-form-urlencoded
source=https://waterpigs.example/post-by-barnaby&
target=https://aaronpk.example/post-by-aaron
HTTP/1.1 202 Accepted
*/
function validate(request) {
return request.type === "application/x-www-form-urlencoded" &&
2021-03-07 20:41:54 +01:00
request.body !== undefined &&
isValidUrl(request?.body?.source) &&
isValidUrl(request?.body?.target) &&
request?.body?.source !== request?.body?.target &&
isValidDomain(request?.body?.source)
}
function processSourceBody(body, target) {
if(body.indexOf(target) === -1) {
return
}
2021-03-07 20:30:14 +01:00
}
async function receive(body) {
try {
await got(body.target)
} catch(unknownTarget) {
return
}
try {
const src = await got(body.source)
processSourceBody(src.body, body.target)
} catch(unknownSource) {
return
}
2021-03-07 20:30:14 +01:00
}
module.exports = {
receive,
validate
}