mail page connected with folders 5
This commit is contained in:
parent
42a8c1dab8
commit
7cf49d8cf3
@ -116,8 +116,19 @@ export async function GET() {
|
||||
connTimeout: 30000
|
||||
});
|
||||
|
||||
// Add a timeout to prevent hanging
|
||||
const timeout = setTimeout(() => {
|
||||
console.error('IMAP connection timeout');
|
||||
imap.end();
|
||||
resolve(NextResponse.json({
|
||||
emails: [],
|
||||
error: 'Connection timeout'
|
||||
}));
|
||||
}, 60000);
|
||||
|
||||
imap.once('error', (err: Error) => {
|
||||
console.error('IMAP error:', err);
|
||||
clearTimeout(timeout);
|
||||
resolve(NextResponse.json({
|
||||
emails: [],
|
||||
error: 'IMAP connection error'
|
||||
@ -128,6 +139,7 @@ export async function GET() {
|
||||
imap.getBoxes((err, boxes) => {
|
||||
if (err) {
|
||||
console.error('Error getting mailboxes:', err);
|
||||
clearTimeout(timeout);
|
||||
imap.end();
|
||||
resolve(NextResponse.json({ emails: [], error: 'Failed to get mailboxes' }));
|
||||
return;
|
||||
@ -143,6 +155,15 @@ export async function GET() {
|
||||
// Process each mailbox
|
||||
const foldersToProcess = availableMailboxes;
|
||||
let processedFolders = 0;
|
||||
let activeFetches = 0;
|
||||
|
||||
function checkCompletion() {
|
||||
processedFolders++;
|
||||
if (processedFolders === foldersToProcess.length && activeFetches === 0) {
|
||||
clearTimeout(timeout);
|
||||
finishProcessing();
|
||||
}
|
||||
}
|
||||
|
||||
function finishProcessing() {
|
||||
// Combine all emails from all folders
|
||||
@ -169,10 +190,7 @@ export async function GET() {
|
||||
imap.openBox(folderName, false, (err, box) => {
|
||||
if (err) {
|
||||
console.error(`Error opening box ${folderName}:`, err);
|
||||
processedFolders++;
|
||||
if (processedFolders === foldersToProcess.length) {
|
||||
finishProcessing();
|
||||
}
|
||||
checkCompletion();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -180,21 +198,16 @@ export async function GET() {
|
||||
imap.search(['ALL'], (err, results) => {
|
||||
if (err) {
|
||||
console.error(`Error searching in ${folderName}:`, err);
|
||||
processedFolders++;
|
||||
if (processedFolders === foldersToProcess.length) {
|
||||
finishProcessing();
|
||||
}
|
||||
checkCompletion();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!results || results.length === 0) {
|
||||
processedFolders++;
|
||||
if (processedFolders === foldersToProcess.length) {
|
||||
finishProcessing();
|
||||
}
|
||||
checkCompletion();
|
||||
return;
|
||||
}
|
||||
|
||||
activeFetches++;
|
||||
// Fetch emails
|
||||
const fetch = imap.fetch(results, {
|
||||
bodies: ['HEADER', 'TEXT'],
|
||||
@ -250,13 +263,13 @@ export async function GET() {
|
||||
|
||||
fetch.on('error', (err) => {
|
||||
console.error(`Error fetching emails from ${folderName}:`, err);
|
||||
activeFetches--;
|
||||
checkCompletion();
|
||||
});
|
||||
|
||||
fetch.on('end', () => {
|
||||
processedFolders++;
|
||||
if (processedFolders === foldersToProcess.length) {
|
||||
finishProcessing();
|
||||
}
|
||||
activeFetches--;
|
||||
checkCompletion();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user