From 74f0a40139f7caff3fcdf737a474578c79a0f58b Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 17:16:01 +0200 Subject: [PATCH] panel 2 courier api restore --- app/api/courrier/login/route.ts | 2 +- app/api/courrier/route.ts | 60 ++++++++++++++------------------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/app/api/courrier/login/route.ts b/app/api/courrier/login/route.ts index d09853e4..6f153ef4 100644 --- a/app/api/courrier/login/route.ts +++ b/app/api/courrier/login/route.ts @@ -106,7 +106,7 @@ export async function GET( read: message.flags.has('\\Seen'), starred: message.flags.has('\\Flagged'), flags: Array.from(message.flags), - hasAttachments: message.bodyStructure?.type === 'multipart') + hasAttachments: message.bodyStructure?.type === 'multipart' }; // 9. Cache the email content diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index 5ae450fb..13412c7d 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -4,24 +4,13 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; -// Add caching for better performance -const cache = new Map(); +// Simple email cache structure +interface EmailCache { + [key: string]: any; +} -const getCachedEmail = (id: string) => cache.get(id); -const setCachedEmail = (id: string, email: Email) => cache.set(id, email); - -// Add debouncing for folder changes -const debouncedLoadEmails = debounce((folder: string) => { - loadEmails(folder); -}, 300); - -// Add infinite scroll -const handleScroll = (e: React.UIEvent) => { - const { scrollTop, scrollHeight, clientHeight } = e.currentTarget; - if (scrollHeight - scrollTop === clientHeight) { - // Load more emails - } -}; +// Simple in-memory cache +const cache: EmailCache = {}; export async function GET(request: Request) { try { @@ -93,23 +82,21 @@ export async function GET(request: Request) { const adjustedEnd = Math.min(end, mailbox.exists); // Fetch both metadata and preview content - const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, { + const fetchOptions: any = { envelope: true, flags: true, - bodyStructure: true, - // Only fetch preview content if requested - ...(preview ? { - bodyParts: ['TEXT'], - bodyPartsOptions: { - TEXT: { - maxLength: 1000 // Limit preview length - } - } - } : {}) - }); + 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) { - result.push({ + 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] || '', @@ -120,10 +107,15 @@ export async function GET(request: Request) { starred: message.flags.has('\\Flagged'), folder: mailbox.path, hasAttachments: message.bodyStructure?.type === 'multipart', - flags: Array.from(message.flags), - // Include preview content if available - preview: preview ? message.bodyParts?.TEXT?.toString() : null - }); + 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); } }