feat(chat): Enhance message decryption with improved key management and debug logging

- Add recipient public key fetching in messages API endpoint for sent message decryption
- Include `isE2E` flag in message response to indicate end-to-end encryption status
- Simplify encrypted content handling by always returning `encryptedContent` field
- Add comprehensive debug logging throughout message loading pipeline for troubleshooting
- Improve key resolution logic to fetch recipient keys when not available in conversation data
- Add detailed logging for each message processing step including key availability and decryption status
- Enhance error handling with more descriptive error messages in key fetching operations
- Better distinguish between sent and received message decryption requirements with clear comments
This commit is contained in:
Christomatt
2026-01-27 06:18:20 +01:00
parent 863cbc8dce
commit 7d1e22e457
3 changed files with 65 additions and 16 deletions
+23 -11
View File
@@ -65,26 +65,38 @@ export async function GET(request: NextRequest) {
limit,
});
// Decrypt messages
// Note: In production, you'd decrypt the private key first using a user password/session key
// For now, we'll return encrypted content and decrypt client-side
// Get recipient info for sent messages
const recipientHandle = conversation.participant2Handle;
let recipientPublicKey: string | null = null;
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;
console.log('[Messages API] Recipient public key found:', !!recipientPublicKey);
const messagesWithDecryption = messages.map((msg) => {
const isSentByMe = msg.senderHandle === session.user.handle;
// Return the appropriate encrypted content:
// - For sent messages: use senderEncryptedContent (encrypted with sender's key)
// - For received messages: use encryptedContent (encrypted with recipient's key)
const encryptedForViewer = isSentByMe
? (msg.senderEncryptedContent || msg.encryptedContent)
: msg.encryptedContent;
const senderPubKey = isSentByMe ? recipientPublicKey : msg.senderChatPublicKey;
console.log('[Messages API] Message:', msg.id, 'isSentByMe:', isSentByMe, 'senderPubKey:', !!senderPubKey, 'msgSenderChatPubKey:', !!msg.senderChatPublicKey);
return {
id: msg.id,
senderHandle: msg.senderHandle,
senderDisplayName: msg.senderDisplayName,
senderAvatarUrl: msg.senderAvatarUrl,
senderPublicKey: msg.senderChatPublicKey, // For E2E decryption
encryptedContent: encryptedForViewer,
// For decryption:
// - Sent messages: need recipient's public key
// - Received messages: need sender's public key
senderPublicKey: senderPubKey,
isE2E: !!msg.senderChatPublicKey || (isSentByMe && !!recipientPublicKey),
encryptedContent: msg.encryptedContent,
deliveredAt: msg.deliveredAt,
readAt: msg.readAt,
createdAt: msg.createdAt,