2021-03-07 20:30:14 +01:00
|
|
|
|
2021-03-08 17:01:28 +01:00
|
|
|
const webmentionReceiver = require('./receive')
|
|
|
|
const webmentionLoader = require('./loader')
|
2021-03-07 20:30:14 +01:00
|
|
|
|
|
|
|
function route(router) {
|
|
|
|
router.post("webmention receive endpoint", "/webmention", async (ctx) => {
|
2021-03-08 17:01:28 +01:00
|
|
|
if(!webmentionReceiver.validate(ctx.request)) {
|
2021-03-07 20:30:14 +01:00
|
|
|
ctx.throw(400, "malformed webmention request")
|
|
|
|
}
|
2021-03-08 13:59:20 +01:00
|
|
|
|
|
|
|
console.log(` OK: looks like a valid webmention: \n\tsource ${ctx.request.body.source}\n\ttarget ${ctx.request.body.target}`)
|
2021-03-08 10:25:02 +01:00
|
|
|
// we do NOT await this on purpose.
|
2021-03-08 17:01:28 +01:00
|
|
|
webmentionReceiver.receive(ctx.request.body)
|
2021-03-07 20:30:14 +01:00
|
|
|
|
|
|
|
ctx.body = "Thanks, bro. Will process this webmention soon, pinky swear!";
|
|
|
|
ctx.status = 202
|
|
|
|
});
|
2021-03-08 17:01:28 +01:00
|
|
|
|
|
|
|
router.get("webmention get endpoint", "/webmention/:domain/:token", async (ctx) => {
|
|
|
|
if(!webmentionLoader.validate(ctx.params)) {
|
|
|
|
ctx.throw(403, "access denied")
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(` OK: someone wants a list of mentions at domain ${ctx.params.domain}`)
|
|
|
|
const result = await webmentionLoader.load(ctx.params.domain)
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
status: 'success',
|
|
|
|
json: result
|
|
|
|
}
|
|
|
|
})
|
2021-03-07 20:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
route
|
|
|
|
}
|