go-jamming/src/webmention/route.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-03-07 20:30:14 +01:00
2021-03-08 17:01:28 +01:00
const webmentionReceiver = require('./receive')
const webmentionLoader = require('./loader')
const webmentionSender = require('./send')
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")
}
console.log(` OK: looks like a valid webmention: \n\tsource ${ctx.request.body.source}\n\ttarget ${ctx.request.body.target}`)
// 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!"
2021-03-07 20:30:14 +01:00
ctx.status = 202
});
2021-03-08 17:01:28 +01:00
router.put("webmention send endpoint", "/webmention/:domain/:token", async (ctx) => {
if(!webmentionLoader.validate(ctx.params)) {
ctx.throw(403, "access denied")
}
console.log(` OK: someone wants to send mentions from domain ${ctx.params.domain}`)
// we do NOT await this on purpose.
webmentionSender.send(ctx.params.domain, ctx.request.query?.since)
ctx.body = "Thanks, bro. Will send these webmentions 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
}