mail page ui correction maj compose 20 bis 10

This commit is contained in:
alma 2025-04-16 14:18:34 +02:00
parent f1befb1515
commit de4dfd0a7d

View File

@ -116,7 +116,6 @@ export async function GET() {
const allEmails: Email[] = [];
imap.once('ready', () => {
// First, get all mailboxes to find the correct folder names
imap.getBoxes((err, boxes) => {
if (err) {
console.error('Error getting mailboxes:', err);
@ -125,10 +124,7 @@ export async function GET() {
return;
}
// Log available mailboxes to see their structure
console.log('Available mailboxes:', Object.keys(boxes));
// For Infomaniak, common folder names are:
const foldersToCheck = ['INBOX', 'Sent', 'Trash', 'Drafts'];
let foldersProcessed = 0;
@ -154,69 +150,97 @@ export async function GET() {
return;
}
// Calculate the range for the last 20 messages
const start = Math.max(1, box.messages.total - 19);
const f = imap.seq.fetch(`${start}:${box.messages.total}`, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
const range = `${start}:${box.messages.total}`;
f.on('message', (msg) => {
const email: any = {
id: '',
from: '',
subject: '',
date: new Date(),
read: true,
starred: false,
body: '',
to: '',
folder: folderName
};
msg.on('body', (stream, info) => {
let buffer = '';
stream.on('data', (chunk) => {
buffer += chunk.toString('utf8');
});
stream.on('end', () => {
if (info.which === 'HEADER.FIELDS (FROM TO SUBJECT DATE)') {
const headers = Imap.parseHeader(buffer);
email.from = headers.from?.[0] || '';
email.to = headers.to?.[0] || '';
email.subject = headers.subject?.[0] || '(No subject)';
email.date = headers.date?.[0] || new Date().toISOString();
} else {
email.body = buffer;
}
});
});
msg.once('attributes', (attrs) => {
email.id = attrs.uid;
email.read = attrs.flags?.includes('\\Seen') || false;
email.starred = attrs.flags?.includes('\\Flagged') || false;
});
msg.once('end', () => {
allEmails.push(email);
});
});
f.once('error', (err) => {
console.error(`Fetch error in ${folderName}:`, err);
});
f.once('end', () => {
console.log(`Finished processing ${folderName}`);
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
// Search for all messages in the range
imap.search(['ALL'], (err, results) => {
if (err) {
console.error(`Search error in ${folderName}:`, err);
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
return;
}
// Filter results to get only the last 20 messages
const messageNumbers = results
.sort((a, b) => b - a) // Sort in descending order
.slice(0, 20); // Take only the last 20
if (messageNumbers.length === 0) {
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
return;
}
const f = imap.fetch(messageNumbers, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
f.on('message', (msg, seqno) => {
const email: any = {
id: '',
from: '',
subject: '',
date: new Date(),
read: true,
starred: false,
body: '',
to: '',
folder: folderName
};
msg.on('body', (stream, info) => {
let buffer = '';
stream.on('data', (chunk) => {
buffer += chunk.toString('utf8');
});
stream.on('end', () => {
if (info.which === 'HEADER.FIELDS (FROM TO SUBJECT DATE)') {
const headers = Imap.parseHeader(buffer);
email.from = headers.from?.[0] || '';
email.to = headers.to?.[0] || '';
email.subject = headers.subject?.[0] || '(No subject)';
email.date = headers.date?.[0] || new Date().toISOString();
} else {
email.body = buffer;
}
});
});
msg.once('attributes', (attrs) => {
email.id = attrs.uid;
email.read = attrs.flags?.includes('\\Seen') || false;
email.starred = attrs.flags?.includes('\\Flagged') || false;
});
msg.once('end', () => {
allEmails.push(email);
});
});
f.once('error', (err) => {
console.error(`Fetch error in ${folderName}:`, err);
});
f.once('end', () => {
console.log(`Finished processing ${folderName}`);
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
});
});
});
};
// Process each folder sequentially to avoid IMAP connection issues
// Process folders sequentially
foldersToCheck.forEach(folder => processFolder(folder));
});
});