const html = require('whatwg-flora-tmpl'); const { Readable } = require('stream'); const fetch = require('node-fetch'); const sanitize = (str) => { if (str == null || str == undefined) return ""; return str.replace('<', '<').replace('>', '>').replace('"', '"'); }; const render = (data) => html`Interactions with ${data.url}

Likes and bookmarks

${data.filter(item => item['wm-property'] === 'like-of' || item['wm-property'] === 'bookmark-of').map(item => html`${sanitize(item.author.name)}`)}

Reposts

${data.filter(item => item['wm-property'] === 'repost-of').map(item => html`${sanitize(item.author.name)}`)}

Comments and Replies

${data.filter(item => item['wm-property'] === 'in-reply-to' || item['wm-property'] === 'mention-of').map(item => html`
${sanitize(item.author.name)}${sanitize(item.author.name)}
${sanitize(item.content?.text)}
`)}
`; class FromWhatWGReadableStream extends Readable { constructor(options, whatwgStream) { super(options); const streamReader = whatwgStream.getReader(); const outStream = this; function pump() { return streamReader.read().then(({ value, done }) => { if (done) { outStream.push(null); return; } outStream.push(value); return pump(); }); } pump(); } } module.exports = async (req, res) => { const { url = `${process.env.ACTIVITYPUB_URL}following`, count = 200 } = req.query; const referer = req.headers.referer; const cacheAge = 12 * 60 * 60; const mentionsUrl = `https://webmention.io/api/mentions.jf2?per-page=${count}&target=${referer || url}`; try { const mentionsResponse = await fetch(mentionsUrl); const data = await mentionsResponse.json(); data.url = referer || url; // Add caching. res.statusCode = 200; res.setHeader('Content-Type', `text/html; charset=utf-8`); res.setHeader('Cache-Control', `public,s-maxage=${cacheAge}, max-age=${cacheAge}`); const output = await render(data.children); const stream = new FromWhatWGReadableStream({}, output); stream.pipe(res, { end: true }); } catch (ex) { console.error(ex); res.status(500).send(ex); } }