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'; // Email cache structure interface EmailCache { [key: string]: { data: any; timestamp: number; }; } // Credentials cache to reduce database queries interface CredentialsCache { [userId: string]: { credentials: any; timestamp: number; } } // In-memory caches with expiration // Make emailListCache available globally for other routes if (!global.emailListCache) { global.emailListCache = {}; } const emailListCache: EmailCache = global.emailListCache; const credentialsCache: CredentialsCache = {}; // Cache TTL in milliseconds (5 minutes) const CACHE_TTL = 5 * 60 * 1000; // Helper function to get credentials with caching async function getCredentialsWithCache(userId: string) { // Check if we have fresh cached credentials const cachedCreds = credentialsCache[userId]; const now = Date.now(); if (cachedCreds && now - cachedCreds.timestamp < CACHE_TTL) { return cachedCreds.credentials; } // Otherwise fetch from database const credentials = await prisma.mailCredentials.findUnique({ where: { userId } }); // Cache the result if (credentials) { credentialsCache[userId] = { credentials, timestamp: now }; } return credentials; } // Retry logic for IMAP operations async function retryOperation(operation: () => Promise, maxAttempts = 3, delay = 1000): Promise { let lastError: Error; for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { return await operation(); } catch (error) { lastError = error as Error; console.warn(`Operation failed (attempt ${attempt}/${maxAttempts}):`, error); if (attempt < maxAttempts) { // Exponential backoff const backoffDelay = delay * Math.pow(2, attempt - 1); console.log(`Retrying in ${backoffDelay}ms...`); await new Promise(resolve => setTimeout(resolve, backoffDelay)); } } } throw lastError!; } export async function GET(request: Request) { try { console.log('Courrier API call received'); const session = await getServerSession(authOptions); if (!session?.user?.id) { console.log('No authenticated session found'); return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ); } console.log('User authenticated:', session.user.id); // Get URL parameters 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 skipCache = url.searchParams.get('skipCache') === 'true'; console.log('Request parameters:', { folder, page, limit, preview, skipCache }); // Generate cache key based on request parameters const cacheKey = `${session.user.id}:${folder}:${page}:${limit}:${preview}`; // Check cache first if not explicitly skipped if (!skipCache && emailListCache[cacheKey]) { const { data, timestamp } = emailListCache[cacheKey]; // Return cached data if it's fresh (less than 1 minute old) if (Date.now() - timestamp < 60000) { console.log('Returning cached email data'); return NextResponse.json(data); } } // Get credentials from cache or database const credentials = await getCredentialsWithCache(session.user.id); console.log('Credentials retrieved:', credentials ? 'yes' : 'no'); if (!credentials) { console.log('No mail credentials found for user'); return NextResponse.json( { error: 'No mail credentials found. Please configure your email account.' }, { status: 401 } ); } // Calculate start and end sequence numbers const start = (page - 1) * limit + 1; const end = start + limit - 1; console.log('Fetching emails from range:', { start, end }); // Connect to IMAP server console.log('Connecting to IMAP server:', credentials.host, credentials.port); 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(); console.log('Connected to IMAP server'); // Get list of all mailboxes first const mailboxes = await client.list(); const availableFolders = mailboxes.map(box => box.path); console.log('Available folders:', availableFolders); // Open the requested mailbox console.log('Opening mailbox:', folder); const mailbox = await client.mailboxOpen(folder); console.log('Mailbox stats:', { exists: mailbox.exists, name: mailbox.path, flags: mailbox.flags }); const result = []; // Only try to fetch if the mailbox has messages if (mailbox.exists > 0) { // Adjust start and end to be within bounds const adjustedStart = Math.min(start, mailbox.exists); const adjustedEnd = Math.min(end, mailbox.exists); console.log('Adjusted fetch range:', { adjustedStart, adjustedEnd }); // Fetch both metadata and preview content const fetchOptions: any = { envelope: true, flags: true, bodyStructure: true }; // Only fetch preview content if requested if (preview) { fetchOptions.bodyParts = ['TEXT']; } console.log('Fetching messages with options:', fetchOptions); const fetchPromises = []; for (let i = adjustedStart; i <= adjustedEnd; i++) { // Convert to string sequence number as required by ImapFlow fetchPromises.push(client.fetchOne(`${i}`, fetchOptions)); } try { const results = await Promise.all(fetchPromises); for (const message of results) { if (!message) continue; // Skip undefined messages console.log('Processing message ID:', message.uid); const emailData: any = { id: message.uid, 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) }; // Include preview content if available if (preview && message.bodyParts && message.bodyParts.has('TEXT')) { emailData.preview = message.bodyParts.get('TEXT')?.toString() || null; } result.push(emailData); } } catch (fetchError) { console.error('Error fetching emails:', fetchError); // Continue with any successfully fetched messages } } else { console.log('No messages in mailbox'); } const responseData = { emails: result, folders: availableFolders, total: mailbox.exists, hasMore: end < mailbox.exists }; console.log('Response summary:', { emailCount: result.length, folderCount: availableFolders.length, total: mailbox.exists, hasMore: end < mailbox.exists }); // Cache the result emailListCache[cacheKey] = { data: responseData, timestamp: Date.now() }; return NextResponse.json(responseData); } catch (error) { console.error('Error in IMAP operations:', error); let errorMessage = 'Failed to fetch emails'; let statusCode = 500; // Type guard for Error objects if (error instanceof Error) { errorMessage = error.message; // Handle specific error cases if (errorMessage.includes('authentication') || errorMessage.includes('login')) { statusCode = 401; errorMessage = 'Authentication failed. Please check your email credentials.'; } else if (errorMessage.includes('connect')) { errorMessage = 'Failed to connect to email server. Please check your settings.'; } else if (errorMessage.includes('timeout')) { errorMessage = 'Connection timed out. Please try again later.'; } } return NextResponse.json( { error: errorMessage }, { status: statusCode } ); } finally { try { await client.logout(); console.log('IMAP client logged out'); } catch (e) { console.error('Error during logout:', e); } } } catch (error) { console.error('Error in courrier route:', error); return NextResponse.json( { error: 'An unexpected error occurred' }, { status: 500 } ); } } export async function POST(request: Request) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { emailId, folderName, action } = await request.json(); if (!emailId) { return NextResponse.json({ error: 'Missing emailId parameter' }, { status: 400 }); } // Invalidate cache entries for this folder const userId = session.user.id; // If folder is specified, only invalidate that folder's cache if (folderName) { Object.keys(emailListCache).forEach(key => { if (key.includes(`${userId}:${folderName}`)) { delete emailListCache[key]; } }); } else { // Otherwise invalidate all cache entries for this user Object.keys(emailListCache).forEach(key => { if (key.startsWith(`${userId}:`)) { delete emailListCache[key]; } }); } return NextResponse.json({ success: true }); } catch (error) { console.error('Error in POST handler:', error); return NextResponse.json({ error: 'An unexpected error occurred' }, { status: 500 }); } }