feat(chat): Implement end-to-end encrypted swarm messaging system

- Add Swarm Chat documentation with architecture, API endpoints, and security considerations
- Create database schema for chat conversations, messages, and typing indicators
- Implement chat API endpoints for sending, receiving, and managing messages
- Add client-side encryption utilities using RSA-OAEP with SHA-256
- Create chat page UI for viewing conversations and messages
- Add chat type definitions for TypeScript support
- Update database schema with new chat tables and relationships
- Update sidebar navigation to include chat link
- Enable true end-to-end encrypted messaging across swarm nodes without ActivityPub limitations
This commit is contained in:
Christomatt
2026-01-26 20:00:17 +01:00
parent c5a5985aa6
commit 6b8eeb6814
13 changed files with 5957 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
/**
* Swarm Chat Messages
*
* GET: Get messages for a conversation
* PATCH: Mark messages as read
*/
import { NextRequest, NextResponse } from 'next/server';
import { db, chatConversations, chatMessages, users } from '@/db';
import { eq, desc, and, lt } from 'drizzle-orm';
import { getSession } from '@/lib/auth';
import { decryptMessage } from '@/lib/swarm/chat-crypto';
export async function GET(request: NextRequest) {
try {
if (!db) {
return NextResponse.json({ messages: [] });
}
const session = await getSession();
if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const conversationId = searchParams.get('conversationId');
const cursor = searchParams.get('cursor'); // For pagination
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 100);
if (!conversationId) {
return NextResponse.json({ error: 'conversationId required' }, { status: 400 });
}
// Verify user has access to this conversation
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.id, conversationId),
eq(chatConversations.participant1Id, session.user.id)
),
});
if (!conversation) {
return NextResponse.json({ error: 'Conversation not found' }, { status: 404 });
}
// Get user's private key for decryption
const user = await db.query.users.findFirst({
where: eq(users.id, session.user.id),
});
if (!user?.privateKeyEncrypted) {
return NextResponse.json({ error: 'Cannot decrypt messages' }, { status: 500 });
}
// Build query with cursor-based pagination
let whereCondition = eq(chatMessages.conversationId, conversationId);
if (cursor) {
whereCondition = and(whereCondition, lt(chatMessages.createdAt, new Date(cursor)));
}
// Get messages
const messages = await db.query.chatMessages.findMany({
where: whereCondition,
orderBy: [desc(chatMessages.createdAt)],
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
const messagesWithDecryption = messages.map((msg) => {
const isSentByMe = msg.senderHandle === session.user.handle;
return {
...msg,
// Only include encrypted content - client will decrypt
content: undefined, // Remove plaintext
isSentByMe,
};
});
return NextResponse.json({
messages: messagesWithDecryption.reverse(), // Oldest first for display
nextCursor: messages.length === limit ? messages[messages.length - 1].createdAt.toISOString() : null,
});
} catch (error) {
console.error('Get messages error:', error);
return NextResponse.json({ error: 'Failed to get messages' }, { status: 500 });
}
}
export async function PATCH(request: NextRequest) {
try {
if (!db) {
return NextResponse.json({ error: 'Database not available' }, { status: 503 });
}
const session = await getSession();
if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { conversationId } = await request.json();
if (!conversationId) {
return NextResponse.json({ error: 'conversationId required' }, { status: 400 });
}
// Verify user has access to this conversation
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.id, conversationId),
eq(chatConversations.participant1Id, session.user.id)
),
});
if (!conversation) {
return NextResponse.json({ error: 'Conversation not found' }, { status: 404 });
}
// Mark all unread messages as read
await db.update(chatMessages)
.set({ readAt: new Date() })
.where(
and(
eq(chatMessages.conversationId, conversationId),
eq(chatMessages.readAt, null)
)
);
return NextResponse.json({ success: true });
} catch (error) {
console.error('Mark as read error:', error);
return NextResponse.json({ error: 'Failed to mark as read' }, { status: 500 });
}
}