mail page ui correction maj compose 20 bis 10
This commit is contained in:
parent
f1befb1515
commit
de4dfd0a7d
@ -116,7 +116,6 @@ export async function GET() {
|
|||||||
const allEmails: Email[] = [];
|
const allEmails: Email[] = [];
|
||||||
|
|
||||||
imap.once('ready', () => {
|
imap.once('ready', () => {
|
||||||
// First, get all mailboxes to find the correct folder names
|
|
||||||
imap.getBoxes((err, boxes) => {
|
imap.getBoxes((err, boxes) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error getting mailboxes:', err);
|
console.error('Error getting mailboxes:', err);
|
||||||
@ -125,10 +124,7 @@ export async function GET() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log available mailboxes to see their structure
|
|
||||||
console.log('Available mailboxes:', Object.keys(boxes));
|
console.log('Available mailboxes:', Object.keys(boxes));
|
||||||
|
|
||||||
// For Infomaniak, common folder names are:
|
|
||||||
const foldersToCheck = ['INBOX', 'Sent', 'Trash', 'Drafts'];
|
const foldersToCheck = ['INBOX', 'Sent', 'Trash', 'Drafts'];
|
||||||
let foldersProcessed = 0;
|
let foldersProcessed = 0;
|
||||||
|
|
||||||
@ -154,69 +150,97 @@ export async function GET() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate the range for the last 20 messages
|
||||||
const start = Math.max(1, box.messages.total - 19);
|
const start = Math.max(1, box.messages.total - 19);
|
||||||
const f = imap.seq.fetch(`${start}:${box.messages.total}`, {
|
const range = `${start}:${box.messages.total}`;
|
||||||
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
|
|
||||||
struct: true
|
|
||||||
});
|
|
||||||
|
|
||||||
f.on('message', (msg) => {
|
// Search for all messages in the range
|
||||||
const email: any = {
|
imap.search(['ALL'], (err, results) => {
|
||||||
id: '',
|
if (err) {
|
||||||
from: '',
|
console.error(`Search error in ${folderName}:`, err);
|
||||||
subject: '',
|
foldersProcessed++;
|
||||||
date: new Date(),
|
if (foldersProcessed === foldersToCheck.length) {
|
||||||
read: true,
|
finishProcessing();
|
||||||
starred: false,
|
}
|
||||||
body: '',
|
return;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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));
|
foldersToCheck.forEach(folder => processFolder(folder));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user