From f758421748a446e984f4ad04172cf036ef887b82 Mon Sep 17 00:00:00 2001 From: Christomatt Date: Tue, 27 Jan 2026 06:28:42 +0100 Subject: [PATCH] feat(chat): Add remote user public key fetching for cross-node messaging - Implement remote user detection based on @domain format in recipient handle - Add HTTP/HTTPS protocol selection logic for localhost vs production domains - Fetch remote user public keys from their node's /api/users endpoint - Maintain local user key lookup for non-remote recipients - Add error handling and debug logging for remote key fetch failures - Enable end-to-end encryption support for federated chat across nodes --- src/app/api/swarm/chat/messages/route.ts | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/app/api/swarm/chat/messages/route.ts b/src/app/api/swarm/chat/messages/route.ts index c2bdeb4..b0202cf 100644 --- a/src/app/api/swarm/chat/messages/route.ts +++ b/src/app/api/swarm/chat/messages/route.ts @@ -71,11 +71,30 @@ export async function GET(request: NextRequest) { console.log('[Messages API] Fetching recipient key for:', recipientHandle); - // Fetch recipient's chat public key - const recipientUser = await db.query.users.findFirst({ - where: eq(users.handle, recipientHandle), - }); - recipientPublicKey = recipientUser?.chatPublicKey || null; + // Check if this is a remote user (has @domain) + const isRemote = recipientHandle.includes('@'); + + if (isRemote) { + // Remote user - fetch from their node + const [handle, domain] = recipientHandle.split('@'); + try { + const protocol = domain.includes('localhost') ? 'http' : 'https'; + const response = await fetch(`${protocol}://${domain}/api/users/${handle}`); + if (response.ok) { + const data = await response.json(); + recipientPublicKey = data.user?.chatPublicKey || null; + console.log('[Messages API] Fetched remote recipient key:', !!recipientPublicKey); + } + } catch (error) { + console.error('[Messages API] Failed to fetch remote recipient key:', error); + } + } else { + // Local user + const recipientUser = await db.query.users.findFirst({ + where: eq(users.handle, recipientHandle), + }); + recipientPublicKey = recipientUser?.chatPublicKey || null; + } console.log('[Messages API] Recipient public key found:', !!recipientPublicKey);