courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 15:30:36 +02:00
parent f47596bd9f
commit efbc11bee1
2 changed files with 57 additions and 29 deletions

View File

@ -272,35 +272,54 @@ export async function getEmails(
perPage: number = 20,
accountId?: string
): Promise<EmailListResult> {
const client = await getImapConnection(userId, accountId);
if (!client) {
throw new Error('IMAP client not found');
}
try {
// Open the mailbox with the folder name directly
await client.mailboxOpen(folder);
// Get total message count
// Extract base folder name (remove accountId suffix if present)
const baseFolder = folder.split('-')[0];
console.log(`Fetching emails for folder: ${folder} (base: ${baseFolder})`);
// Get IMAP connection
const client = await getImapConnection(userId, accountId);
if (!client) {
throw new Error('Failed to establish IMAP connection');
}
// Open mailbox
await client.mailboxOpen(baseFolder);
const mailbox = client.mailbox;
if (!mailbox || typeof mailbox === 'boolean') {
throw new Error('Failed to open mailbox');
throw new Error(`Failed to open mailbox: ${baseFolder}`);
}
// Get total messages
const total = mailbox.exists || 0;
console.log(`Total messages in ${baseFolder}: ${total}`);
// If no messages, return empty result
if (total === 0) {
return {
emails: [],
totalEmails: 0,
page,
perPage,
totalPages: 0,
folder: baseFolder,
mailboxes: []
};
}
// Calculate message range for pagination
const start = Math.max(1, total - (page * perPage) + 1);
const end = Math.max(1, total - ((page - 1) * perPage));
console.log(`Fetching messages ${start}:${end} from ${baseFolder}`);
// Fetch messages
const messages = await client.fetch(`${start}:${end}`, {
envelope: true,
flags: true,
bodyStructure: true
});
const emails: EmailMessage[] = [];
for await (const message of messages) {
const email: EmailMessage = {
id: message.uid.toString(),
@ -323,23 +342,29 @@ export async function getEmails(
},
size: message.size || 0,
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
folder,
folder: baseFolder,
contentFetched: false,
accountId: accountId || '',
content: '',
messageId: message.envelope.messageId || undefined
accountId: accountId || 'default',
content: {
text: '',
html: ''
}
};
emails.push(email);
}
// Cache the result if accountId is provided
// Cache the result if we have an accountId
if (accountId) {
await cacheEmailList(userId, accountId, folder, page, perPage, emails);
await cacheEmailList(userId, accountId, baseFolder, page, perPage, {
emails,
totalEmails: total,
page,
perPage,
totalPages: Math.ceil(total / perPage),
folder: baseFolder,
mailboxes: []
});
}
// Get mailboxes for this account
const mailboxes = await getMailboxes(client, accountId);
return {
emails,
@ -347,15 +372,15 @@ export async function getEmails(
page,
perPage,
totalPages: Math.ceil(total / perPage),
folder,
mailboxes
folder: baseFolder,
mailboxes: []
};
} catch (error) {
console.error('Error fetching emails:', error);
throw error;
} finally {
try {
await client.mailboxClose();
await client?.mailboxClose();
} catch (error) {
console.error('Error closing mailbox:', error);
}

View File

@ -38,7 +38,10 @@ export interface EmailMessage {
cc?: EmailAddress[];
bcc?: EmailAddress[];
subject: string;
content: string;
content: {
text: string;
html: string;
};
preview?: string;
date: Date;
flags: {