summary refs log tree commit diff
path: root/src/activitypub/federation/utils.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-09-29 04:06:07 +0000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-09-29 04:06:07 +0000
commitb7a79505fd0661542c44f835eb1a678134bf7764 (patch)
tree202c468c0d5e6ed8db570309a54fa20a044066b1 /src/activitypub/federation/utils.ts
parentHttp signatures: fix missing quotes in sent header, add date check (diff)
downloadserver-b7a79505fd0661542c44f835eb1a678134bf7764.tar.xz
Debug logging and cache federated lookups
Diffstat (limited to 'src/activitypub/federation/utils.ts')
-rw-r--r--src/activitypub/federation/utils.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/activitypub/federation/utils.ts b/src/activitypub/federation/utils.ts
index 7f1d5e6a..520c292e 100644
--- a/src/activitypub/federation/utils.ts
+++ b/src/activitypub/federation/utils.ts
@@ -2,7 +2,9 @@ import { DEFAULT_FETCH_OPTIONS } from "@spacebar/api";
 import {
 	ActorType,
 	Config,
+	Debug,
 	FederationActivity,
+	FederationCache,
 	FederationKey,
 	OrmUtils,
 	Snowflake,
@@ -26,6 +28,10 @@ import TurndownService from "turndown";
 import { federationQueue } from "./queue";
 
 export const ACTIVITYSTREAMS_CONTEXT = "https://www.w3.org/ns/activitystreams";
+export const LOG_NAMES = {
+	webfinger: "Webfinger",
+	remote: "Remote",
+};
 
 export const fetchOpts = Object.freeze(
 	OrmUtils.mergeDeep(DEFAULT_FETCH_OPTIONS, {
@@ -52,6 +58,11 @@ export const resolveAPObject = async <T extends AnyAPObject>(
 	// we were already given an object
 	if (typeof data != "string") return data;
 
+	const cache = await FederationCache.findOne({ where: { id: data } });
+	if (cache) return cache.toJSON() as T;
+
+	Debug(LOG_NAMES.remote, `Fetching from remote ${data}`);
+
 	const agent = new ProxyAgent();
 	const ret = await fetch(data, {
 		...fetchOpts,
@@ -62,6 +73,10 @@ export const resolveAPObject = async <T extends AnyAPObject>(
 
 	if (!hasAPContext(json)) throw new APError("Object is not APObject");
 
+	setImmediate(async () => {
+		await FederationCache.create({ id: json.id, data: json }).save();
+	});
+
 	return json as T;
 };
 
@@ -95,6 +110,8 @@ export const resolveWebfinger = async (
 ): Promise<AnyAPObject> => {
 	const { domain } = splitQualifiedMention(lookup);
 
+	Debug(LOG_NAMES.webfinger, `Performing lookup ${lookup}`);
+
 	const agent = new ProxyAgent();
 	const wellknown = (await fetch(
 		`https://${domain}/.well-known/webfinger?resource=${lookup}`,