From 4f5f025d5776ce74b78a5a9f5a4cb64d7b7f35b6 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 16 Jun 2023 11:26:25 -0400 Subject: [PATCH] feat: add webfinger ttl cache --- src/activitypub/helpers.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js index 7c77462839..d001cfb2ed 100644 --- a/src/activitypub/helpers.js +++ b/src/activitypub/helpers.js @@ -2,6 +2,10 @@ const request = require('request-promise-native'); +const ttl = require('../cache/ttl'); + +const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours + const Helpers = module.exports; Helpers.query = async (id) => { @@ -10,6 +14,10 @@ Helpers.query = async (id) => { return false; } + 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, @@ -28,5 +36,6 @@ Helpers.query = async (id) => { ({ href: actorUri } = actorUri); } + webfingerCache.set(id, { username, hostname, actorUri }); return { username, hostname, actorUri }; };