courrier multi account restore compose
This commit is contained in:
parent
814d63f434
commit
62101bd1d2
@ -271,77 +271,74 @@ export async function getEmails(
|
|||||||
perPage: number = 20,
|
perPage: number = 20,
|
||||||
accountId?: string
|
accountId?: string
|
||||||
): Promise<EmailListResult> {
|
): Promise<EmailListResult> {
|
||||||
|
const client = await getImapConnection(userId, accountId);
|
||||||
|
if (!client) {
|
||||||
|
throw new Error('IMAP client not found');
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract the base folder name by removing the accountId suffix if present
|
// Open the mailbox with the folder name directly
|
||||||
const baseFolder = accountId && folder.includes(`-${accountId}`)
|
await client.mailboxOpen(folder);
|
||||||
? folder.split(`-${accountId}`)[0]
|
|
||||||
: folder;
|
// Get total message count
|
||||||
|
const mailbox = client.mailbox;
|
||||||
console.log(`Fetching emails for folder: ${baseFolder} (original: ${folder})`);
|
if (!mailbox || typeof mailbox === 'boolean') {
|
||||||
|
throw new Error('Failed to open mailbox');
|
||||||
// Get IMAP connection for the account
|
|
||||||
const imap = await getImapConnection(userId, accountId);
|
|
||||||
if (!imap) {
|
|
||||||
throw new Error('Failed to establish IMAP connection');
|
|
||||||
}
|
}
|
||||||
|
const total = mailbox.exists || 0;
|
||||||
// Open the mailbox
|
|
||||||
await imap.mailboxOpen(baseFolder);
|
|
||||||
|
|
||||||
// Calculate message range for pagination
|
// Calculate message range for pagination
|
||||||
const totalMessages = await imap.status(baseFolder, { messages: true });
|
|
||||||
const total = totalMessages.messages || 0;
|
|
||||||
const start = Math.max(1, total - (page * perPage) + 1);
|
const start = Math.max(1, total - (page * perPage) + 1);
|
||||||
const end = Math.max(1, total - ((page - 1) * perPage));
|
const end = Math.max(1, total - ((page - 1) * perPage));
|
||||||
|
|
||||||
// Fetch messages
|
// Fetch messages
|
||||||
const messages = await imap.fetch(`${start}:${end}`, {
|
const messages = await client.fetch(`${start}:${end}`, {
|
||||||
envelope: true,
|
envelope: true,
|
||||||
flags: true,
|
flags: true,
|
||||||
bodyStructure: true,
|
bodyStructure: true
|
||||||
uid: true
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const emails: EmailMessage[] = [];
|
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(),
|
||||||
from: (message.envelope?.from || []).map(addr => ({
|
from: message.envelope.from?.map(addr => ({
|
||||||
name: addr.name || '',
|
name: addr.name || '',
|
||||||
address: addr.address || ''
|
address: addr.address || ''
|
||||||
})),
|
})) || [],
|
||||||
to: (message.envelope?.to || []).map(addr => ({
|
to: message.envelope.to?.map(addr => ({
|
||||||
name: addr.name || '',
|
name: addr.name || '',
|
||||||
address: addr.address || ''
|
address: addr.address || ''
|
||||||
})),
|
})) || [],
|
||||||
subject: message.envelope?.subject || '',
|
subject: message.envelope.subject || '',
|
||||||
date: message.envelope?.date || 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'),
|
|
||||||
flagged: message.flags.has('\\Flagged'),
|
flagged: message.flags.has('\\Flagged'),
|
||||||
|
answered: message.flags.has('\\Answered'),
|
||||||
draft: message.flags.has('\\Draft'),
|
draft: message.flags.has('\\Draft'),
|
||||||
deleted: message.flags.has('\\Deleted')
|
deleted: message.flags.has('\\Deleted')
|
||||||
},
|
},
|
||||||
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,
|
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
|
// Cache the result if accountId is provided
|
||||||
if (accountId) {
|
if (accountId) {
|
||||||
await cacheEmailList(userId, accountId, folder, page, perPage, emails);
|
await cacheEmailList(userId, accountId, folder, page, perPage, emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get mailboxes for this account
|
// Get mailboxes for this account
|
||||||
const mailboxes = await getMailboxes(imap, accountId);
|
const mailboxes = await getMailboxes(client, accountId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
emails,
|
emails,
|
||||||
@ -353,8 +350,14 @@ export async function getEmails(
|
|||||||
mailboxes
|
mailboxes
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching emails for folder ${folder}:`, error);
|
console.error('Error fetching emails:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
await client.mailboxClose();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error closing mailbox:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -603,17 +606,12 @@ export async function getMailboxes(client: ImapFlow, accountId?: string): Promis
|
|||||||
// Combine standard folders with custom folders
|
// Combine standard folders with custom folders
|
||||||
const uniqueFolders = new Set([...standardFolders, ...processedMailboxes]);
|
const uniqueFolders = new Set([...standardFolders, ...processedMailboxes]);
|
||||||
|
|
||||||
// If accountId is provided, append it to each folder name
|
// Return unique folders without appending accountId
|
||||||
return accountId
|
return Array.from(uniqueFolders);
|
||||||
? 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
|
// Return default folders on error
|
||||||
const defaultFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
|
return ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
|
||||||
return accountId
|
|
||||||
? defaultFolders.map(f => `${f}-${accountId}`)
|
|
||||||
: defaultFolders;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user