From 695ea0849d0e743953a4636537e9274f28d73366 Mon Sep 17 00:00:00 2001 From: Jannis Braun <151788261+TheZwiss@users.noreply.github.com> Date: Tue, 21 Apr 2026 20:35:51 +0200 Subject: [PATCH] refactor(federation-worker): extract buildContextMapForPeer helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pure refactor — will be reused by the needs_attention transition handler. No behavior change. --- packages/server/src/utils/federationWorker.ts | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/server/src/utils/federationWorker.ts b/packages/server/src/utils/federationWorker.ts index 17d7a433..b63bdf2d 100644 --- a/packages/server/src/utils/federationWorker.ts +++ b/packages/server/src/utils/federationWorker.ts @@ -396,22 +396,7 @@ async function resolvePendingPeers(): Promise { case 'rejected': { console.warn(`[federation-worker] Auto-peering rejected by ${peerOrigin}: ${result.error}`); - // Collect affected contexts before purging - const entries = db - .select({ - contextId: schema.federationOutbox.contextId, - contextType: schema.federationOutbox.contextType, - }) - .from(schema.federationOutbox) - .where(eq(schema.federationOutbox.peerId, peerId)) - .all(); - - const contextMap = new Map(); - for (const e of entries) { - if (!contextMap.has(e.contextId)) { - contextMap.set(e.contextId, e.contextType); - } - } + const contextMap = buildContextMapForPeer(db, peerId); // Purge outbox entries (NOT mutation log) db.delete(schema.federationOutbox) @@ -433,6 +418,34 @@ async function resolvePendingPeers(): Promise { } } +/** + * Build a map of contextId → contextType for all outbox entries targeting + * a specific peer. Used for surfacing "delivery impossible" via + * pushPeerRejectedEvent when a peer is rejected or transitioned to + * needs_attention. + */ +function buildContextMapForPeer( + db: ReturnType, + peerId: string, +): Map { + const entries = db + .select({ + contextId: schema.federationOutbox.contextId, + contextType: schema.federationOutbox.contextType, + }) + .from(schema.federationOutbox) + .where(eq(schema.federationOutbox.peerId, peerId)) + .all(); + + const contextMap = new Map(); + for (const e of entries) { + if (!contextMap.has(e.contextId)) { + contextMap.set(e.contextId, e.contextType); + } + } + return contextMap; +} + /** * Push a federation_peer_rejected WS event to all local users affected by * the rejection. Resolves contextLabel from the database for each context.