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}
`;
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 = 'https://flear.org/', 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);
}
}
Likes and bookmarks
${data.filter(item => item['wm-property'] === 'like-of' || item['wm-property'] === 'bookmark-of').map(item => html``)}Reposts
${data.filter(item => item['wm-property'] === 'repost-of').map(item => html``)}Comments and Replies