courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 14:35:48 +02:00
parent 1506cc7390
commit 181375fe08

View File

@ -292,16 +292,19 @@ export async function getEmails(
} }
try { try {
// Select the mailbox // Select the mailbox and wait for it to be fully opened
await client.mailboxOpen(folder); const mailbox = await client.mailboxOpen(folder);
if (!mailbox) {
throw new Error(`Failed to open mailbox ${folder}`);
}
// Get total count // Get total count from the mailbox object
const totalEmails = await client.mailbox.messages.total; const totalEmails = mailbox.exists || 0;
const totalPages = Math.ceil(totalEmails / perPage); const totalPages = Math.ceil(totalEmails / perPage);
// Calculate range for this page // Calculate range for this page
const start = (page - 1) * perPage + 1; const start = Math.max(1, totalEmails - (page * perPage) + 1);
const end = Math.min(start + perPage - 1, totalEmails); const end = Math.max(1, totalEmails - ((page - 1) * perPage));
// Fetch messages // Fetch messages
const messages = await client.fetch(`${start}:${end}`, { const messages = await client.fetch(`${start}:${end}`, {
@ -310,9 +313,7 @@ export async function getEmails(
bodyStructure: true, bodyStructure: true,
internalDate: true, internalDate: true,
size: true, size: true,
bodyParts: [ bodyParts: ['HEADER']
{ part: 'TEXT', query: { headers: true } }
]
}); });
// Process messages // Process messages
@ -320,8 +321,14 @@ export async function getEmails(
for await (const message of messages) { for await (const message of messages) {
const email: EmailMessage = { const email: EmailMessage = {
id: message.uid.toString(), id: message.uid.toString(),
from: message.envelope.from[0]?.address || '', from: message.envelope.from.map(addr => ({
to: message.envelope.to.map(addr => addr.address).join(', '), name: addr.name || '',
address: addr.address
})),
to: message.envelope.to.map(addr => ({
name: addr.name || '',
address: addr.address
})),
subject: message.envelope.subject || '', subject: message.envelope.subject || '',
date: message.internalDate, date: message.internalDate,
flags: { flags: {