From 6d319af06107c2d7e90c6fdea6c207a29f591a75 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 25 Apr 2025 18:27:59 +0200 Subject: [PATCH] panel 2 courier api restore --- app/api/courrier/route.ts | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index a886a51d..e0fe14a0 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -159,9 +159,13 @@ export async function GET(request: Request) { } console.log('Fetching messages with options:', fetchOptions); - const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, fetchOptions); + const fetchPromises = []; + for (let i = adjustedStart; i <= adjustedEnd; i++) { + fetchPromises.push(client.fetchOne(i, fetchOptions)); + } + const results = await Promise.all(fetchPromises); - for await (const message of messages) { + for await (const message of results) { console.log('Processing message ID:', message.uid); const emailData: any = { id: message.uid, @@ -209,12 +213,17 @@ export async function GET(request: Request) { }; return NextResponse.json(responseData); - } finally { - try { - await client.logout(); - console.log('IMAP client logged out'); - } catch (e) { - console.error('Error during logout:', e); + } catch (error) { + if (error.source === 'timeout') { + // Retry with exponential backoff + return retryOperation(operation, attempt + 1); + } else if (error.source === 'auth') { + // Prompt for credentials refresh + return NextResponse.json({ error: 'Authentication failed', code: 'AUTH_ERROR' }); + } else { + // General error handling + console.error('Operation failed:', error); + return NextResponse.json({ error: 'Operation failed', details: error.message }); } } } catch (error) { @@ -224,4 +233,15 @@ export async function GET(request: Request) { { status: 500 } ); } +} + +export async function POST(request: Request, { params }: { params: { id: string } }) { + // Mark as read logic... + + // Invalidate cache entries for this folder + Object.keys(emailListCache).forEach(key => { + if (key.includes(`${userId}:${folderName}`)) { + delete emailListCache[key]; + } + }); } \ No newline at end of file