Sync chat public key to users table and fix handle formatting

The chat public key is now also updated in the users table during key publishing for profile discovery. Additionally, sender handles in chat receive are now fully qualified with domain when necessary to ensure consistency in conversation and message storage.
This commit is contained in:
Christopher
2026-01-28 07:43:05 -08:00
parent 93a587f185
commit 28f561f689
2 changed files with 18 additions and 6 deletions
+7 -3
View File
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/db'; 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 { requireAuth } from '@/lib/auth';
import { eq, and, gt } from 'drizzle-orm'; 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'); console.log('[Chat Keys POST] Updated existing key');
} else { } else {
// Insert new bundle console.log('[Chat Keys POST] Inserted new key');
console.log('[Chat Keys POST] Inserting new bundle...');
await db.insert(chatDeviceBundles).values({ await db.insert(chatDeviceBundles).values({
userId: user.id, userId: user.id,
did: user.did, did: user.did,
@@ -219,6 +218,11 @@ export async function POST(request: NextRequest) {
console.log('[Chat Keys POST] Inserted new key'); 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); console.log('[Chat Keys POST] Key published successfully for', user.handle);
return NextResponse.json({ success: true }); return NextResponse.json({ success: true });
} catch (error: any) { } catch (error: any) {
+11 -3
View File
@@ -37,6 +37,14 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 }); 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 // 1. Find local recipient
const recipientUser = await db.query.users.findFirst({ const recipientUser = await db.query.users.findFirst({
where: eq(users.did, recipientDid) where: eq(users.did, recipientDid)
@@ -51,14 +59,14 @@ export async function POST(request: NextRequest) {
let conversation = await db.query.chatConversations.findFirst({ let conversation = await db.query.chatConversations.findFirst({
where: and( where: and(
eq(chatConversations.participant1Id, recipientUser.id), eq(chatConversations.participant1Id, recipientUser.id),
eq(chatConversations.participant2Handle, senderHandle) eq(chatConversations.participant2Handle, finalSenderHandle)
) )
}); });
if (!conversation) { if (!conversation) {
const [newConv] = await db.insert(chatConversations).values({ const [newConv] = await db.insert(chatConversations).values({
participant1Id: recipientUser.id, participant1Id: recipientUser.id,
participant2Handle: senderHandle, participant2Handle: finalSenderHandle,
lastMessageAt: new Date(), lastMessageAt: new Date(),
lastMessagePreview: '[Encrypted Message]' lastMessagePreview: '[Encrypted Message]'
}).returning(); }).returning();
@@ -68,7 +76,7 @@ export async function POST(request: NextRequest) {
// 3. Store Message // 3. Store Message
await db.insert(chatMessages).values({ await db.insert(chatMessages).values({
conversationId: conversation.id, conversationId: conversation.id,
senderHandle: senderHandle, senderHandle: finalSenderHandle,
senderDisplayName: senderDisplayName || senderHandle, senderDisplayName: senderDisplayName || senderHandle,
senderAvatarUrl: senderAvatarUrl, senderAvatarUrl: senderAvatarUrl,
senderNodeDomain: senderNodeDomain, senderNodeDomain: senderNodeDomain,