courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 15:12:38 +02:00
parent e2805dca07
commit 266805950e

View File

@ -266,59 +266,44 @@ interface FetchOptions {
*/ */
export async function getEmails( export async function getEmails(
userId: string, userId: string,
folder: string = 'INBOX', folder: string,
page: number = 1, page: number = 1,
perPage: number = 20, perPage: number = 20,
searchQuery: string = '',
accountId?: string accountId?: string
): Promise<EmailListResult> { ): Promise<EmailMessage[]> {
console.log(`Fetching emails for user ${userId}${accountId ? ` account ${accountId}` : ''} in folder ${folder}`); try {
// Extract the base folder name by removing the accountId suffix if present
// Extract the base folder name if it's an account-specific folder
const baseFolder = accountId && folder.includes(`-${accountId}`) const baseFolder = accountId && folder.includes(`-${accountId}`)
? folder.split(`-${accountId}`)[0] ? folder.split(`-${accountId}`)[0]
: folder; : folder;
console.log(`Using base folder name: ${baseFolder} for IMAP operations`); console.log(`Fetching emails for folder: ${baseFolder} (original: ${folder})`);
// Try to get from cache first // Get IMAP connection for the account
const cached = await getCachedEmailList(userId, accountId || 'default', folder, page, perPage); const imap = await getImapConnection(userId, accountId);
if (cached) { if (!imap) {
console.log(`Using cached email list for ${userId}${accountId ? ` account ${accountId}` : ''} in folder ${folder}`); throw new Error('Failed to establish IMAP connection');
return cached;
} }
// Get IMAP connection for the specific account // Open the mailbox
const client = await getImapConnection(userId, accountId); await imap.mailboxOpen(baseFolder);
if (!client) {
throw new Error('Failed to get IMAP connection');
}
try { // Calculate message range for pagination
// Open the mailbox using the base folder name const totalMessages = await imap.status(baseFolder, { messages: true });
await client.mailboxOpen(baseFolder); const total = totalMessages.messages || 0;
const start = Math.max(1, total - (page * perPage) + 1);
const end = Math.max(1, total - ((page - 1) * perPage));
// Get total count of messages // Fetch messages
const status = await client.status(baseFolder, { messages: true }); const messages = await imap.fetch(`${start}:${end}`, {
const totalEmails = status.messages || 0;
const totalPages = Math.ceil(totalEmails / perPage);
// Calculate the range of messages to fetch
const start = (page - 1) * perPage + 1;
const end = Math.min(start + perPage - 1, totalEmails);
const emails: EmailMessage[] = [];
if (totalEmails > 0) {
// Fetch messages in the specified range
const messages = await client.fetch(`${start}:${end}`, {
envelope: true, envelope: true,
flags: true, flags: true,
bodyStructure: true, bodyStructure: true,
size: true, uid: true
bodyParts: ['HEADER']
}); });
const emails: EmailMessage[] = [];
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(),
@ -331,7 +316,7 @@ export async function getEmails(
address: addr.address || '' address: addr.address || ''
})), })),
subject: message.envelope?.subject || '', subject: message.envelope?.subject || '',
date: message.internalDate || new Date(), date: message.envelope?.date || new Date(),
flags: { flags: {
seen: message.flags.has('\\Seen'), seen: message.flags.has('\\Seen'),
answered: message.flags.has('\\Answered'), answered: message.flags.has('\\Answered'),
@ -341,39 +326,24 @@ export async function getEmails(
}, },
size: message.size || 0, size: message.size || 0,
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false, hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
folder: folder, // Use the original folder name with account ID folder: folder,
contentFetched: false, contentFetched: false,
accountId: accountId, accountId: accountId || '',
content: '', content: '',
messageId: message.envelope?.messageId || undefined messageId: message.envelope?.messageId || undefined
}; };
emails.push(email); emails.push(email);
} }
// Cache the result
if (accountId) {
await cacheEmailList(userId, accountId, folder, page, perPage, emails);
} }
const result: EmailListResult = { return emails;
emails,
totalEmails,
page,
perPage,
totalPages,
folder,
mailboxes: await getMailboxes(client, accountId) // Pass accountId to getMailboxes
};
// Cache the result with the account-specific folder
await cacheEmailList(userId, accountId || 'default', folder, page, perPage, result);
return result;
} catch (error) { } catch (error) {
console.error(`Error fetching emails for folder ${baseFolder}:`, error); console.error(`Error fetching emails for folder ${folder}:`, error);
throw error; throw error;
} finally {
try {
await client.mailboxClose();
} catch (error) {
console.error('Error closing mailbox:', error);
}
} }
} }
@ -594,7 +564,7 @@ export async function getMailboxes(client: ImapFlow, accountId?: string): Promis
try { try {
const mailboxes = await client.list(); const mailboxes = await client.list();
// Map special folders to standard names, but keep account-specific paths // Map special folders to standard names
const specialFolders = new Map<string, string>([ const specialFolders = new Map<string, string>([
['Sent Messages', 'Sent'], ['Sent Messages', 'Sent'],
['Sent Items', 'Sent'], ['Sent Items', 'Sent'],
@ -604,31 +574,31 @@ export async function getMailboxes(client: ImapFlow, accountId?: string): Promis
['Spam', 'Junk'] ['Spam', 'Junk']
]); ]);
// Process mailboxes and map special folders while preserving account-specific paths // Process mailboxes and map special folders
const processedMailboxes = mailboxes.map(mailbox => { const processedMailboxes = mailboxes.map(mailbox => {
const path = mailbox.path; const path = mailbox.path;
// Check if this is a special folder // Check if this is a special folder
for (const [special, standard] of specialFolders) { for (const [special, standard] of specialFolders) {
if (path.includes(special)) { if (path.includes(special)) {
// Include accountId in the folder name to make it unique per account return standard;
return accountId ? `${standard}-${accountId}` : standard;
} }
} }
// Include accountId in the folder name to make it unique per account return path;
return accountId ? `${path}-${accountId}` : path;
}); });
// Ensure we have all standard folders with account-specific names // Ensure we have all standard folders
const standardFolders = accountId const standardFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
? ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'].map(f => `${f}-${accountId}`)
: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
// Combine standard folders with custom folders
const uniqueFolders = new Set([...standardFolders, ...processedMailboxes]); const uniqueFolders = new Set([...standardFolders, ...processedMailboxes]);
return Array.from(uniqueFolders); // If accountId is provided, append it to each folder name
return accountId
? Array.from(uniqueFolders).map(f => `${f}-${accountId}`)
: Array.from(uniqueFolders);
} catch (error) { } catch (error) {
console.error('Error fetching mailboxes:', error); console.error('Error fetching mailboxes:', error);
// Return default folders on error, with account-specific names if accountId is provided // Return default folders on error
const defaultFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; const defaultFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
return accountId return accountId
? defaultFolders.map(f => `${f}-${accountId}`) ? defaultFolders.map(f => `${f}-${accountId}`)