go-jamming/jsfork/src/webmention/route.js

54 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
2021-04-05 17:44:27 +02:00
const log = require('pino')()
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-04-05 17:44:27 +02:00
log.info('%s %o', 'OK: looks like a valid webmention', ctx.request.body)
// 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")
}
2021-03-17 21:51:13 +01:00
const since = ctx.request.query?.since
2021-04-05 17:44:27 +02:00
log.info(` OK: someone wants to send mentions from domain ${ctx.params.domain} since ${since}`)
// we do NOT await this on purpose.
2021-03-17 21:51:13 +01:00
webmentionSender.send(ctx.params.domain, 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")
}
2021-04-05 17:44:27 +02:00
log.info(` OK: someone wants a list of mentions at domain ${ctx.params.domain}`)
2021-03-08 17:01:28 +01:00
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
}