import { NextResponse } from 'next/server'; import { ImapFlow } from 'imapflow'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; // Define the Email type interface Email { id: string; from: string; fromName: string; to: string; subject: string; date: string; read: boolean; starred: boolean; folder: string; hasAttachments: boolean; flags: string[]; preview?: string; } // Add caching for better performance const cache = new Map(); const getCachedEmail = (id: string) => cache.get(id); const setCachedEmail = (id: string, email: Email) => cache.set(id, email); // Add debouncing for folder changes const debouncedLoadEmails = debounce((folder: string) => { loadEmails(folder); }, 300); // Add infinite scroll const handleScroll = (e: React.UIEvent) => { const { scrollTop, scrollHeight, clientHeight } = e.currentTarget; if (scrollHeight - scrollTop === clientHeight) { // Load more emails } }; export async function GET(request: Request) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ); } const credentials = await prisma.mailCredentials.findUnique({ where: { userId: session.user.id } }); if (!credentials) { return NextResponse.json( { error: 'No mail credentials found' }, { status: 401 } ); } const url = new URL(request.url); const folder = url.searchParams.get('folder') || 'INBOX'; const page = parseInt(url.searchParams.get('page') || '1'); const limit = parseInt(url.searchParams.get('limit') || '20'); const preview = url.searchParams.get('preview') === 'true'; const client = new ImapFlow({ host: credentials.host, port: credentials.port, secure: true, auth: { user: credentials.email, pass: credentials.password, }, logger: false, emitLogs: false, tls: { rejectUnauthorized: false } }); try { await client.connect(); const mailboxes = await client.list(); const availableFolders = mailboxes.map(box => box.path); const mailbox = await client.mailboxOpen(folder); const start = (page - 1) * limit + 1; const end = start + limit - 1; const result: Email[] = []; if (mailbox.exists > 0) { const adjustedStart = Math.min(start, mailbox.exists); const adjustedEnd = Math.min(end, mailbox.exists); const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, { envelope: true, flags: true, bodyStructure: true, ...(preview ? { bodyParts: ['TEXT'], bodyPartsOptions: { TEXT: { maxLength: 1000 } } } : {}) }); for await (const message of messages) { const email: Email = { id: message.uid.toString(), from: message.envelope.from?.[0]?.address || '', fromName: message.envelope.from?.[0]?.name || message.envelope.from?.[0]?.address?.split('@')[0] || '', to: message.envelope.to?.map(addr => addr.address).join(', ') || '', subject: message.envelope.subject || '(No subject)', date: message.envelope.date?.toISOString() || new Date().toISOString(), read: message.flags.has('\\Seen'), starred: message.flags.has('\\Flagged'), folder: mailbox.path, hasAttachments: message.bodyStructure?.type === 'multipart', flags: Array.from(message.flags), preview: preview ? message.bodyParts?.get('TEXT')?.toString() : undefined }; result.push(email); } } return NextResponse.json({ emails: result, folders: availableFolders, total: mailbox.exists, hasMore: end < mailbox.exists }); } finally { await client.logout(); } } catch (error) { console.error('Error in courrier route:', error); return NextResponse.json( { error: 'An unexpected error occurred' }, { status: 500 } ); } }