197 lines
5.8 KiB
TypeScript
197 lines
5.8 KiB
TypeScript
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]: any;
|
|
}
|
|
|
|
// Credentials cache to reduce database queries
|
|
interface CredentialsCache {
|
|
[userId: string]: {
|
|
credentials: any;
|
|
timestamp: number;
|
|
}
|
|
}
|
|
|
|
// In-memory caches with expiration
|
|
const emailListCache: EmailCache = {};
|
|
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;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// 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';
|
|
|
|
// 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) {
|
|
return NextResponse.json(data);
|
|
}
|
|
}
|
|
|
|
// Get credentials from cache or database
|
|
const credentials = await getCredentialsWithCache(session.user.id);
|
|
|
|
if (!credentials) {
|
|
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;
|
|
|
|
// Connect to IMAP server
|
|
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();
|
|
|
|
// Get list of all mailboxes first
|
|
const mailboxes = await client.list();
|
|
const availableFolders = mailboxes.map(box => box.path);
|
|
|
|
// Open the requested mailbox
|
|
const mailbox = await client.mailboxOpen(folder);
|
|
|
|
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);
|
|
|
|
// 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'];
|
|
}
|
|
|
|
const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, fetchOptions);
|
|
|
|
for await (const message of messages) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
const responseData = {
|
|
emails: result,
|
|
folders: availableFolders,
|
|
total: mailbox.exists,
|
|
hasMore: end < mailbox.exists
|
|
};
|
|
|
|
// Cache the result
|
|
emailListCache[cacheKey] = {
|
|
data: responseData,
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
return NextResponse.json(responseData);
|
|
} finally {
|
|
try {
|
|
await client.logout();
|
|
} 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 }
|
|
);
|
|
}
|
|
}
|