fediblock-instance/lib/api.js

54 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2024-09-15 17:44:53 +00:00
module.exports = (app, apex) => {
const constant = require('./constant'),
{ readFile, writeFile } = require('fs')
app.use('/api/scan', (req, res) => {
res.header('Content-Type', 'text/event-stream')
res.header('Connection', 'keep-alive')
res.header('Cache-Control', 'no-cache')
res.header('X-Accel-Buffering', 'no')
res.write(Buffer.from('data: ...\n\n'))
req.app.locals.scan.on('data', data => {
res.write(Buffer.from(data))
})
res.setTimeout(20000, () => {
res.write(Buffer.from('data: ...\n\n'))
})
})
app.use('/api/served', (req, res, next) => {
readFile(__dirname + '/../served.txt', async (err, data) => {
if (err) {
next(err)
} else {
const num = parseInt(data.toString()) + 1
writeFile(__dirname + '/../served.txt', num.toString(), 'utf8', (err) => {
if (err) {
next(err)
} else {
res.json({ served: num, lastscan: req.app.locals.scantotal, server: req.app.locals.server, instances: req.app.locals.instances, peers: req.app.locals.peers, created: req.app.locals.created, updated: req.app.locals.updated })
}
})
}
})
})
app.use('/api/outbox', async (req, res) => {
const outbox = await apex.getOutbox(await apex.store.getObject(apex.utils.usernameToIRI(constant.nick), true), 'true'),
notes = outbox.orderedItems.filter(e => e.object
&& e.object.length > 0
&& e.object[0].type === 'Note'
&& e.object[0].content
&& e.object[0].content[0].startsWith('<p>You')).slice(0, 20).map(e => ({
content: e.object[0].content[0],
published: e.published[0]
}))
res.json(notes)
})
app.use('/api/inbox', async (req, res) => {
const actor = await apex.store.getObject(apex.utils.usernameToIRI(constant.nick), true)
actor._local = {}
actor._local.blockList = []
const inbox = await apex.getInbox(actor, 'true'),
notes = inbox.orderedItems.filter(e => e.object && e.object.length > 0 && e.object[0].type === 'Note').map(e => e.object[0].content[0])
res.json(notes)
})
}