refactor(federation-worker): extract buildContextMapForPeer helper

Pure refactor — will be reused by the needs_attention transition handler.
No behavior change.
This commit is contained in:
Jannis Braun
2026-04-21 20:35:51 +02:00
parent 617d71ab4b
commit 695ea0849d
+29 -16
View File
@@ -396,22 +396,7 @@ async function resolvePendingPeers(): Promise<void> {
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<string, string>();
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<void> {
}
}
/**
* 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<typeof getDb>,
peerId: string,
): Map<string, string> {
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<string, string>();
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.