mail page fix

This commit is contained in:
alma 2025-04-21 17:27:04 +02:00
parent dbe55757be
commit 6444f67555

View File

@ -64,28 +64,36 @@ export async function GET(request: Request) {
// Open the requested mailbox // Open the requested mailbox
const mailbox = await client.mailboxOpen(folder); const mailbox = await client.mailboxOpen(folder);
// Fetch messages from the current folder
const messages = await client.fetch(`${start}:${end}`, {
envelope: true,
flags: true,
bodyStructure: true
});
const result = []; const result = [];
for await (const message of messages) {
result.push({ // Only try to fetch if the mailbox has messages
id: message.uid, if (mailbox.exists > 0) {
from: message.envelope.from?.[0]?.address || '', // Adjust start and end to be within bounds
fromName: message.envelope.from?.[0]?.name || message.envelope.from?.[0]?.address?.split('@')[0] || '', const adjustedStart = Math.min(start, mailbox.exists);
to: message.envelope.to?.map(addr => addr.address).join(', ') || '', const adjustedEnd = Math.min(end, mailbox.exists);
subject: message.envelope.subject || '(No subject)',
date: message.envelope.date?.toISOString() || new Date().toISOString(), // Fetch messages from the current folder
read: message.flags.has('\\Seen'), const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, {
starred: message.flags.has('\\Flagged'), envelope: true,
folder: mailbox.path, flags: true,
hasAttachments: message.bodyStructure?.type === 'multipart', bodyStructure: true
flags: Array.from(message.flags)
}); });
for await (const message of messages) {
result.push({
id: message.uid,
from: message.envelope.from?.[0]?.address || '',
fromName: message.envelope.from?.[0]?.name || message.envelope.from?.[0]?.address?.split('@')[0] || '',
to: message.envelope.to?.map(addr => addr.address).join(', ') || '',
subject: message.envelope.subject || '(No subject)',
date: message.envelope.date?.toISOString() || new Date().toISOString(),
read: message.flags.has('\\Seen'),
starred: message.flags.has('\\Flagged'),
folder: mailbox.path,
hasAttachments: message.bodyStructure?.type === 'multipart',
flags: Array.from(message.flags)
});
}
} }
return NextResponse.json({ return NextResponse.json({