Neah/app/api/courrier/route.ts
2025-04-25 13:07:35 +02:00

227 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';
import { LRUCache } from 'lru-cache';
// Define types
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 | null;
}
interface CachedData {
emails: Email[];
folders: string[];
total: number;
hasMore: boolean;
page: number;
limit: number;
}
// Configure efficient caching with TTL - fix the type to allow any data
const emailCache = new LRUCache<string, any>({
max: 500, // Store up to 500 emails
ttl: 1000 * 60 * 5, // Cache for 5 minutes
});
// Simple in-memory cache for email content
const emailContentCache = new LRUCache<string, any>({
max: 100,
ttl: 1000 * 60 * 15, // 15 minutes
});
// Keep IMAP connections per user with timeouts
const connectionPool = new Map<string, {
client: ImapFlow;
lastUsed: number;
}>();
// Clean up idle connections periodically
setInterval(() => {
const now = Date.now();
connectionPool.forEach((connection, userId) => {
if (now - connection.lastUsed > 1000 * 60 * 2) { // 2 minutes idle
connection.client.logout().catch(console.error);
connectionPool.delete(userId);
}
});
}, 60000); // Check every minute
// Get or create IMAP client for user
async function getImapClient(userId: string, credentials: any): Promise<ImapFlow> {
const existing = connectionPool.get(userId);
if (existing) {
existing.lastUsed = Date.now();
return existing.client;
}
// Remove invalid options
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
},
disableAutoIdle: true
});
await client.connect();
connectionPool.set(userId, {
client,
lastUsed: Date.now()
});
return client;
}
// Generate cache key
function getCacheKey(userId: string, folder: string, page: number, limit: number): string {
return `${userId}:${folder}:${page}:${limit}`;
}
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
// 1. Properly await params to avoid Next.js error
const { id } = await Promise.resolve(params);
// 2. Authentication check
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
// 3. Check cache first
const cacheKey = `email:${session.user.id}:${id}`;
const cachedEmail = emailContentCache.get(cacheKey);
if (cachedEmail) {
return NextResponse.json(cachedEmail);
}
// 4. Get credentials from database
const credentials = await prisma.mailCredentials.findUnique({
where: {
userId: session.user.id
}
});
if (!credentials) {
return NextResponse.json(
{ error: 'No mail credentials found. Please configure your email account.' },
{ status: 401 }
);
}
// 5. Create IMAP client
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
},
disableAutoIdle: true
});
try {
await client.connect();
// 6. Open INBOX
await client.mailboxOpen('INBOX');
// 7. Fetch the email with UID search
const options = {
uid: true, // This is crucial - we must specify uid:true to fetch by UID
source: true,
envelope: true,
bodyStructure: true,
flags: true
};
// 7. Fetch by UID
const message = await client.fetchOne(id, options);
if (!message) {
return NextResponse.json(
{ error: 'Email not found' },
{ status: 404 }
);
}
// 8. Parse the email content
const emailContent = {
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: any) => addr.address).join(', ') || '',
subject: message.envelope.subject || '(No subject)',
date: message.envelope.date?.toISOString() || new Date().toISOString(),
content: message.source?.toString() || '',
read: message.flags.has('\\Seen'),
starred: message.flags.has('\\Flagged'),
flags: Array.from(message.flags),
hasAttachments: message.bodyStructure?.type === 'multipart'
};
// 9. Cache the email content
emailContentCache.set(cacheKey, emailContent);
// 10. Return the email content
return NextResponse.json(emailContent);
} finally {
// 11. Close the connection
try {
await client.logout();
} catch (e) {
console.error('Error during IMAP logout:', e);
}
}
} catch (error) {
console.error('Error fetching email:', error);
return NextResponse.json(
{ error: 'Failed to fetch email content' },
{ status: 500 }
);
}
}
// Helper method to release connection when app shutting down
export async function cleanup() {
for (const [userId, connection] of connectionPool.entries()) {
await connection.client.logout().catch(console.error);
connectionPool.delete(userId);
}
}