Files
NodeBB/src/activitypub/helpers.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const request = require('request-promise-native');
2023-06-16 11:26:25 -04:00
const ttl = require('../cache/ttl');
const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
const Helpers = module.exports;
Helpers.query = async (id) => {
const [username, hostname] = id.split('@');
if (!username || !hostname) {
return false;
}
2023-06-16 11:26:25 -04:00
if (webfingerCache.has(id)) {
return webfingerCache.get(id);
}
// Make a webfinger query to retrieve routing information
const response = await request(`https://${hostname}/.well-known/webfinger?resource=acct:${id}`, {
simple: false,
resolveWithFullResponse: true,
json: true,
});
if (response.statusCode !== 200 || !response.body.hasOwnProperty('links')) {
return false;
}
// Parse links to find actor endpoint
let actorUri = response.body.links.filter(link => link.type === 'application/activity+json' && link.rel === 'self');
if (actorUri.length) {
actorUri = actorUri.pop();
({ href: actorUri } = actorUri);
}
2023-06-16 11:26:25 -04:00
webfingerCache.set(id, { username, hostname, actorUri });
return { username, hostname, actorUri };
};