feat(federation): Port to Versia 0.6

This commit is contained in:
Jesse Wierzbinski 2026-02-25 02:34:27 +01:00
parent de69f27877
commit fca30b4dad
No known key found for this signature in database
62 changed files with 1614 additions and 2008 deletions

View file

@ -2,7 +2,6 @@ import { config } from "@versia-server/config";
import { Worker } from "bullmq";
import { ApiError } from "../../api-error.ts";
import { Instance } from "../../db/instance.ts";
import { User } from "../../db/user.ts";
import { InboxProcessor } from "../../inbox-processor.ts";
import { connection } from "../../redis.ts";
import { type InboxJobData, InboxJobType, inboxQueue } from "./queue.ts";
@ -72,51 +71,29 @@ export const getInboxWorker = (): Worker<InboxJobData, void, InboxJobType> =>
"versia-signed-by": string;
};
const sender = await User.resolve(new URL(signedBy));
const sender = await Instance.resolve(signedBy);
if (!(sender || signedBy.startsWith("instance "))) {
if (!sender) {
await job.log(
`Could not resolve sender URI [${signedBy}]`,
`Could not resolve sender domain [${signedBy}]`,
);
return;
}
if (sender?.local) {
throw new Error(
"Cannot process federation requests from local users",
);
}
const remoteInstance = sender
? await Instance.fromUser(sender)
: await Instance.resolveFromHost(
signedBy.split(" ")[1],
);
if (!remoteInstance) {
await job.log("Could not resolve the remote instance.");
return;
}
await job.log(
`Entity [${data.id}] is from remote instance [${remoteInstance.data.baseUrl}]`,
`Entity [${data.id}] is from remote instance [${sender.data.baseUrl}]`,
);
if (!remoteInstance.data.publicKey?.key) {
if (!sender.data.publicKey?.key) {
throw new Error(
`Instance ${remoteInstance.data.baseUrl} has no public key stored in database`,
`Instance ${sender.data.baseUrl} has no public key stored in database`,
);
}
const key = await crypto.subtle.importKey(
"spki",
Buffer.from(
sender?.data.publicKey ??
remoteInstance.data.publicKey.key,
"base64",
),
Buffer.from(sender.data.publicKey.key, "base64"),
"Ed25519",
false,
["verify"],
@ -127,7 +104,7 @@ export const getInboxWorker = (): Worker<InboxJobData, void, InboxJobType> =>
req,
data,
{
instance: remoteInstance,
instance: sender,
key,
},
undefined,
@ -147,10 +124,10 @@ export const getInboxWorker = (): Worker<InboxJobData, void, InboxJobType> =>
);
await job.log(
`Sending error message to instance [${remoteInstance.data.baseUrl}]`,
`Sending error message to instance [${sender.data.baseUrl}]`,
);
await remoteInstance.sendMessage(
await sender.sendMessage(
`Failed processing entity [${
data.uri
}] delivered to inbox. Returned error:\n\n${JSON.stringify(