diff --git a/src/app/api/chat/keys/route.ts b/src/app/api/chat/keys/route.ts index 317706e..fcab630 100644 --- a/src/app/api/chat/keys/route.ts +++ b/src/app/api/chat/keys/route.ts @@ -1,6 +1,6 @@ import { NextRequest, NextResponse } from 'next/server'; import { db } from '@/db'; -import { chatDeviceBundles, handleRegistry, remoteIdentityCache } from '@/db/schema'; +import { chatDeviceBundles, handleRegistry, remoteIdentityCache, users } from '@/db/schema'; import { requireAuth } from '@/lib/auth'; import { eq, and, gt } from 'drizzle-orm'; @@ -205,8 +205,7 @@ export async function POST(request: NextRequest) { ); console.log('[Chat Keys POST] Updated existing key'); } else { - // Insert new bundle - console.log('[Chat Keys POST] Inserting new bundle...'); + console.log('[Chat Keys POST] Inserted new key'); await db.insert(chatDeviceBundles).values({ userId: user.id, did: user.did, @@ -219,6 +218,11 @@ export async function POST(request: NextRequest) { console.log('[Chat Keys POST] Inserted new key'); } + // Sync to users table too for profile discovery + await db.update(users) + .set({ chatPublicKey: publicKey }) + .where(eq(users.id, user.id)); + console.log('[Chat Keys POST] Key published successfully for', user.handle); return NextResponse.json({ success: true }); } catch (error: any) { diff --git a/src/app/api/chat/receive/route.ts b/src/app/api/chat/receive/route.ts index 9edcf9d..2a18d68 100644 --- a/src/app/api/chat/receive/route.ts +++ b/src/app/api/chat/receive/route.ts @@ -37,6 +37,14 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: 'Missing required fields' }, { status: 400 }); } + console.log(`[Chat Receive] From: ${senderHandle} (${senderNodeDomain}), To: ${recipientDid} [${encryptedContent.substring(0, 20)}...]`); + + // Ensure remote handle is fully qualified + let finalSenderHandle = senderHandle; + if (!finalSenderHandle.includes('@') && senderNodeDomain) { + finalSenderHandle = `${senderHandle}@${senderNodeDomain}`; + } + // 1. Find local recipient const recipientUser = await db.query.users.findFirst({ where: eq(users.did, recipientDid) @@ -51,14 +59,14 @@ export async function POST(request: NextRequest) { let conversation = await db.query.chatConversations.findFirst({ where: and( eq(chatConversations.participant1Id, recipientUser.id), - eq(chatConversations.participant2Handle, senderHandle) + eq(chatConversations.participant2Handle, finalSenderHandle) ) }); if (!conversation) { const [newConv] = await db.insert(chatConversations).values({ participant1Id: recipientUser.id, - participant2Handle: senderHandle, + participant2Handle: finalSenderHandle, lastMessageAt: new Date(), lastMessagePreview: '[Encrypted Message]' }).returning(); @@ -68,7 +76,7 @@ export async function POST(request: NextRequest) { // 3. Store Message await db.insert(chatMessages).values({ conversationId: conversation.id, - senderHandle: senderHandle, + senderHandle: finalSenderHandle, senderDisplayName: senderDisplayName || senderHandle, senderAvatarUrl: senderAvatarUrl, senderNodeDomain: senderNodeDomain,