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
This commit is contained in:
Christomatt
2026-01-27 06:28:42 +01:00
parent 7d1e22e457
commit f758421748
+24 -5
View File
@@ -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);