social-butterfly
Version:
Incorporate federated social network protocols easily. Used with Hello, world federated blog.
61 lines (52 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _url_factory = require("./util/url_factory");
var _default = options => async (req, res, next) => {
const {
constants,
getLocalUser,
getLocalContent
} = options;
const contentOwner = await getLocalUser(req.query.resource, req);
const content = await getLocalContent(req.query.resource, req);
if (!contentOwner || !content) {
return res.sendStatus(404);
}
const thumb = (0, _url_factory.buildUrl)({
req,
pathname: content?.thumb || contentOwner.logo || contentOwner.favicon
}); // If thumb is present, present that as the embed, otherwise it's just the title sadly for now.
let htmlContent = thumb ? `<img src="${thumb}" alt="thumbnail" title="${content.title}" />` : content.title; // Escape the less than/greater than signs.
htmlContent = htmlContent.replace(/</g, '\u003c').replace(/>/g, '\u003e'); // If your server has a hookup to listen to /api/stats you can get info on amount of times your content has been read.
const statsUrl = (0, _url_factory.buildUrl)({
req,
pathname: '/api/stats',
searchParams: {
resource: content.url
}
});
const statsImg = `<img src="${statsUrl}" alt="stats" />`;
res.type('application/json+oembed');
res.json({
type: 'rich',
version: '1.0',
provider_url: (0, _url_factory.buildUrl)({
req,
pathname: '/'
}),
title: content.title,
author_name: content.username,
author_url: contentOwner.url,
provider_name: contentOwner.title ? contentOwner.title : undefined,
width: thumb ? constants.thumbWidth : undefined,
height: thumb ? constants.thumbHeight : undefined,
thumbnail_width: thumb ? constants.thumbWidth : undefined,
thumbnail_height: thumb ? constants.thumbHeight : undefined,
thumbnail_url: thumb || undefined,
html: `<a href="${content.url}">${htmlContent}</a>${statsImg}`
});
};
exports.default = _default;