Add V2 chat protocol support and improve error handling
Introduces support for V2 encrypted chat envelopes in the swarm inbox route, allowing storage and processing of new message formats. Enhances error handling and logging in both chat send and encryption hooks, including detection of remote nodes that do not support the V2 protocol and providing clearer error messages for users and developers.
This commit is contained in:
@@ -29,6 +29,49 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
console.log('[Swarm Inbox] Received body keys:', Object.keys(body), 'action:', body.action);
|
||||
|
||||
// Check if this is a V2 encrypted envelope (has 'action' and 'data' fields)
|
||||
// MUST check BEFORE Zod validation to avoid validation errors
|
||||
if (body.action === 'chat.deliver' && body.data) {
|
||||
// V2 E2EE Message - store in chatInbox
|
||||
const { recipientDid, recipientDeviceId, ciphertext } = body.data;
|
||||
|
||||
if (!recipientDid || !ciphertext) {
|
||||
return NextResponse.json({ error: 'Invalid V2 payload' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Find recipient by DID
|
||||
const recipient = await db.query.users.findFirst({
|
||||
where: eq(users.did, recipientDid)
|
||||
});
|
||||
|
||||
if (!recipient) {
|
||||
return NextResponse.json({ error: 'Recipient not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Import chatInbox schema
|
||||
const { chatInbox } = await import('@/db/schema');
|
||||
|
||||
// Store in V2 inbox
|
||||
await db.insert(chatInbox).values({
|
||||
senderDid: body.did, // From signed envelope
|
||||
recipientDid,
|
||||
recipientDeviceId: recipientDeviceId || null,
|
||||
envelope: JSON.stringify(body),
|
||||
expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
|
||||
});
|
||||
|
||||
console.log(`[Swarm Chat V2] Received encrypted message for ${recipientDid}`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'V2 message received',
|
||||
version: 2
|
||||
});
|
||||
}
|
||||
|
||||
// V1 Legacy Message Format - validate with Zod
|
||||
const data = chatMessageSchema.parse(body) as SwarmChatMessagePayload;
|
||||
|
||||
// Find the recipient (local user)
|
||||
|
||||
Reference in New Issue
Block a user