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:
@@ -71,11 +71,30 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
console.log('[Messages API] Fetching recipient key for:', recipientHandle);
|
console.log('[Messages API] Fetching recipient key for:', recipientHandle);
|
||||||
|
|
||||||
// Fetch recipient's chat public key
|
// Check if this is a remote user (has @domain)
|
||||||
const recipientUser = await db.query.users.findFirst({
|
const isRemote = recipientHandle.includes('@');
|
||||||
where: eq(users.handle, recipientHandle),
|
|
||||||
});
|
if (isRemote) {
|
||||||
recipientPublicKey = recipientUser?.chatPublicKey || null;
|
// 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);
|
console.log('[Messages API] Recipient public key found:', !!recipientPublicKey);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user