Add chat page, improve chat API, and remove ChatWidget
Introduces a new chat page with end-to-end encryption, conversation list, and message thread UI. Adds new API endpoints for unread message count and debugging chat keys/messages. Improves chat key validation and reciprocal conversation/message creation for local recipients. Removes the old ChatWidget component. Updates login and chat API logic for better Turnstile and key handling.
This commit is contained in:
@@ -53,9 +53,26 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: 'chatPublicKey required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Validate it looks like a base64 SPKI key
|
||||
if (chatPublicKey.length < 50 || chatPublicKey.length > 500) {
|
||||
return NextResponse.json({ error: 'Invalid public key format' }, { status: 400 });
|
||||
// Validate it looks like a base64 SPKI key (should be ~120 chars for P-256)
|
||||
if (chatPublicKey.length < 100 || chatPublicKey.length > 200) {
|
||||
console.error('[Chat Keys API] Invalid public key length:', chatPublicKey.length);
|
||||
return NextResponse.json({
|
||||
error: `Invalid public key format: expected ~120 characters, got ${chatPublicKey.length}`
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// Additional validation: try to decode as base64
|
||||
try {
|
||||
const decoded = Buffer.from(chatPublicKey, 'base64');
|
||||
if (decoded.length < 80 || decoded.length > 100) {
|
||||
console.error('[Chat Keys API] Invalid decoded key size:', decoded.length);
|
||||
return NextResponse.json({
|
||||
error: `Invalid public key: decoded size ${decoded.length} bytes (expected ~91 bytes for P-256)`
|
||||
}, { status: 400 });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Chat Keys API] Failed to decode public key as base64:', e);
|
||||
return NextResponse.json({ error: 'Public key is not valid base64' }, { status: 400 });
|
||||
}
|
||||
|
||||
// chatPrivateKeyEncrypted should be a JSON string with encrypted data
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db, chatConversations, chatMessages } from '@/db';
|
||||
import { eq, and, isNull, inArray } from 'drizzle-orm';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get user's conversations
|
||||
const conversations = await db.query.chatConversations.findMany({
|
||||
where: eq(chatConversations.participant1Id, session.user.id),
|
||||
});
|
||||
|
||||
if (conversations.length === 0) {
|
||||
return NextResponse.json({ unreadCount: 0 });
|
||||
}
|
||||
|
||||
// Count unread messages across all conversations
|
||||
const conversationIds = conversations.map(c => c.id);
|
||||
|
||||
const unreadMessages = await db.query.chatMessages.findMany({
|
||||
where: and(
|
||||
inArray(chatMessages.conversationId, conversationIds),
|
||||
isNull(chatMessages.readAt)
|
||||
),
|
||||
});
|
||||
|
||||
// Filter out messages sent by the current user
|
||||
const unreadCount = unreadMessages.filter(
|
||||
msg => msg.senderHandle !== session.user.handle
|
||||
).length;
|
||||
|
||||
return NextResponse.json({ unreadCount });
|
||||
} catch (error) {
|
||||
console.error('Get unread chat count error:', error);
|
||||
return NextResponse.json({ error: 'Failed to get unread count' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user