courrier multi account restore compose
This commit is contained in:
parent
f47596bd9f
commit
efbc11bee1
@ -272,35 +272,54 @@ 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 {
|
||||||
// Open the mailbox with the folder name directly
|
// Extract base folder name (remove accountId suffix if present)
|
||||||
await client.mailboxOpen(folder);
|
const baseFolder = folder.split('-')[0];
|
||||||
|
console.log(`Fetching emails for folder: ${folder} (base: ${baseFolder})`);
|
||||||
// Get total message count
|
|
||||||
|
// 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;
|
const mailbox = client.mailbox;
|
||||||
if (!mailbox || typeof mailbox === 'boolean') {
|
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;
|
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
|
// Calculate message range for pagination
|
||||||
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));
|
||||||
|
console.log(`Fetching messages ${start}:${end} from ${baseFolder}`);
|
||||||
|
|
||||||
// Fetch messages
|
// Fetch messages
|
||||||
const messages = await client.fetch(`${start}:${end}`, {
|
const messages = await client.fetch(`${start}:${end}`, {
|
||||||
envelope: true,
|
envelope: true,
|
||||||
flags: true,
|
flags: true,
|
||||||
bodyStructure: true
|
bodyStructure: 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(),
|
||||||
@ -323,23 +342,29 @@ 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: baseFolder,
|
||||||
contentFetched: false,
|
contentFetched: false,
|
||||||
accountId: accountId || '',
|
accountId: accountId || 'default',
|
||||||
content: '',
|
content: {
|
||||||
messageId: message.envelope.messageId || undefined
|
text: '',
|
||||||
|
html: ''
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
emails.push(email);
|
emails.push(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache the result if accountId is provided
|
// Cache the result if we have an accountId
|
||||||
if (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 {
|
return {
|
||||||
emails,
|
emails,
|
||||||
@ -347,15 +372,15 @@ export async function getEmails(
|
|||||||
page,
|
page,
|
||||||
perPage,
|
perPage,
|
||||||
totalPages: Math.ceil(total / perPage),
|
totalPages: Math.ceil(total / perPage),
|
||||||
folder,
|
folder: baseFolder,
|
||||||
mailboxes
|
mailboxes: []
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching emails:', error);
|
console.error('Error fetching emails:', error);
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
await client.mailboxClose();
|
await client?.mailboxClose();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error closing mailbox:', error);
|
console.error('Error closing mailbox:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,7 +38,10 @@ export interface EmailMessage {
|
|||||||
cc?: EmailAddress[];
|
cc?: EmailAddress[];
|
||||||
bcc?: EmailAddress[];
|
bcc?: EmailAddress[];
|
||||||
subject: string;
|
subject: string;
|
||||||
content: string;
|
content: {
|
||||||
|
text: string;
|
||||||
|
html: string;
|
||||||
|
};
|
||||||
preview?: string;
|
preview?: string;
|
||||||
date: Date;
|
date: Date;
|
||||||
flags: {
|
flags: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user