panel 2 courier api

This commit is contained in:
alma 2025-04-25 12:50:28 +02:00
parent f6c4c77993
commit 1028a3285e

View File

@ -21,8 +21,17 @@ interface Email {
preview?: string | null;
}
// Configure efficient caching with TTL
const emailCache = new LRUCache<string, Email>({
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
});
@ -53,6 +62,7 @@ async function getImapClient(userId: string, credentials: any): Promise<ImapFlow
return existing.client;
}
// Remove invalid options
const client = new ImapFlow({
host: credentials.host,
port: credentials.port,
@ -66,11 +76,7 @@ async function getImapClient(userId: string, credentials: any): Promise<ImapFlow
tls: {
rejectUnauthorized: false
},
// Add connection optimizations
disableAutoIdle: true,
idleTimeout: 30000,
connectTimeout: 10000,
greetingTimeout: 5000,
disableAutoIdle: true
});
await client.connect();
@ -140,7 +146,7 @@ export async function GET(request: Request) {
const cachedFolders = emailCache.get(foldersCacheKey);
if (cachedFolders) {
availableFolders = cachedFolders as unknown as string[];
availableFolders = cachedFolders;
} else {
const mailboxes = await client.list();
availableFolders = mailboxes.map(box => box.path);
@ -153,28 +159,30 @@ export async function GET(request: Request) {
const result: Email[] = [];
// 8. Fetch emails (if any exist)
// Define start and end variables HERE
let start = 1;
let end = 0;
if (mailbox.exists > 0) {
// Calculate range with boundaries
const start = Math.min((page - 1) * limit + 1, mailbox.exists);
const end = Math.min(start + limit - 1, mailbox.exists);
start = Math.min((page - 1) * limit + 1, mailbox.exists);
end = Math.min(start + limit - 1, mailbox.exists);
// Use sequence numbers in descending order for newest first
const range = mailbox.exists - end + 1 + ':' + (mailbox.exists - start + 1);
const range = `${mailbox.exists - end + 1}:${mailbox.exists - start + 1}`;
// Fetch messages with optimized options
const options = {
const options: any = {
envelope: true,
flags: true,
bodyStructure: true,
// Only fetch preview if requested
...(preview ? {
bodyParts: ['TEXT', 'HTML'],
bodyPartsOptions: {
maxLength: 1000 // Limit preview length
}
} : {})
bodyStructure: true
};
// Only fetch preview if requested
if (preview) {
options.bodyParts = ['TEXT', 'HTML'];
}
const messages = await client.fetch(range, options);
// Process messages
@ -193,7 +201,7 @@ export async function GET(request: Request) {
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(', ') || '',
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
subject: message.envelope.subject || '(No subject)',
date: message.envelope.date?.toISOString() || new Date().toISOString(),
read: message.flags.has('\\Seen'),