From 119180c214690e69b25f9c41cd1d551e6f68521b Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 16 Apr 2025 14:47:37 +0200 Subject: [PATCH] mail page ui correction maj compose 20 bis 17 --- app/api/mail/route.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index 3527a9b..074ceb3 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -113,10 +113,10 @@ export async function GET() { }); return new Promise((resolve) => { - const allEmails: Email[] = []; + // Create a map to store emails by folder + const emailsByFolder: { [key: string]: any[] } = {}; imap.once('ready', () => { - // Get all mailboxes first imap.getBoxes((err, boxes) => { if (err) { console.error('Error getting mailboxes:', err); @@ -125,17 +125,19 @@ export async function GET() { return; } - // Get the list of available mailboxes const availableMailboxes = Object.keys(boxes); console.log('Available mailboxes:', availableMailboxes); - // These are the folders we want to process based on your IMAP structure + // Process only the mailboxes we want to use const foldersToCheck = ['INBOX', 'Sent', 'Trash', 'Spam', 'Drafts', 'Archives', 'Archive']; let foldersProcessed = 0; const processFolder = (folderName: string) => { console.log(`Processing folder: ${folderName}`); + // Initialize array for this folder + emailsByFolder[folderName] = []; + imap.openBox(folderName, false, (err, box) => { if (err) { console.error(`Error opening ${folderName}:`, err); @@ -156,8 +158,10 @@ export async function GET() { return; } - // Search for all messages in the folder - imap.search(['ALL'], (err, results) => { + // Search for messages in this folder + const searchCriteria = ['ALL']; + + imap.search(searchCriteria, (err, results) => { if (err) { console.error(`Search error in ${folderName}:`, err); foldersProcessed++; @@ -167,7 +171,6 @@ export async function GET() { return; } - // Get the most recent messages (up to 20) const messageNumbers = results .sort((a, b) => b - a) .slice(0, 20); @@ -195,7 +198,7 @@ export async function GET() { starred: false, body: '', to: '', - folder: folderName // Keep exact folder name + folder: folderName }; msg.on('body', (stream, info) => { @@ -223,7 +226,8 @@ export async function GET() { }); msg.once('end', () => { - allEmails.push(email); + // Add email to its folder's array + emailsByFolder[folderName].push(email); }); }); @@ -232,7 +236,7 @@ export async function GET() { }); f.once('end', () => { - console.log(`Finished processing ${folderName}, emails found: ${allEmails.length}`); + console.log(`Finished processing ${folderName}, emails found: ${emailsByFolder[folderName].length}`); foldersProcessed++; if (foldersProcessed === foldersToCheck.length) { finishProcessing(); @@ -248,7 +252,14 @@ export async function GET() { }); function finishProcessing() { + // Combine all emails from all folders + const allEmails = Object.entries(emailsByFolder).flatMap(([folder, emails]) => emails); + + console.log('Emails by folder:', Object.fromEntries( + Object.entries(emailsByFolder).map(([folder, emails]) => [folder, emails.length]) + )); console.log('All folders processed, total emails:', allEmails.length); + const response = { emails: allEmails, mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null