From 1028a3285e88f6d5dd7280b5cc0a9f73148a9052 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 12:50:28 +0200 Subject: [PATCH] panel 2 courier api --- app/api/courrier/route.ts | 50 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index 802e8d56..510a2ede 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -21,8 +21,17 @@ interface Email { preview?: string | null; } -// Configure efficient caching with TTL -const emailCache = new LRUCache({ +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({ 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 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'),