courrier multi account restore compose
This commit is contained in:
parent
3bb31e580e
commit
098075010f
@ -288,34 +288,30 @@ export async function getEmails(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Select the mailbox and wait for it to be fully opened
|
// Open the mailbox
|
||||||
const mailbox = await client.mailboxOpen(folder);
|
await client.mailboxOpen(folder);
|
||||||
if (!mailbox) {
|
|
||||||
throw new Error(`Failed to open mailbox ${folder}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get total count from the mailbox object
|
// Get total count of messages
|
||||||
const totalEmails = mailbox.exists || 0;
|
const status = await client.status(folder, { messages: true });
|
||||||
|
const totalEmails = status.messages || 0;
|
||||||
const totalPages = Math.ceil(totalEmails / perPage);
|
const totalPages = Math.ceil(totalEmails / perPage);
|
||||||
|
|
||||||
// Calculate range for this page
|
// Calculate the range of messages to fetch
|
||||||
const start = Math.max(1, totalEmails - (page * perPage) + 1);
|
const start = (page - 1) * perPage + 1;
|
||||||
const end = Math.max(1, totalEmails - ((page - 1) * perPage));
|
const end = Math.min(start + perPage - 1, totalEmails);
|
||||||
|
|
||||||
|
const emails: EmailMessage[] = [];
|
||||||
|
|
||||||
// Only fetch if we have messages
|
|
||||||
if (totalEmails > 0) {
|
if (totalEmails > 0) {
|
||||||
// Fetch messages
|
// Fetch messages in the specified range
|
||||||
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,
|
||||||
internalDate: true,
|
|
||||||
size: true,
|
size: true,
|
||||||
bodyParts: ['HEADER']
|
bodyParts: ['HEADER']
|
||||||
});
|
});
|
||||||
|
|
||||||
// Process messages
|
|
||||||
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(),
|
||||||
@ -340,47 +336,48 @@ export async function getEmails(
|
|||||||
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
|
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
|
||||||
folder: folder,
|
folder: folder,
|
||||||
contentFetched: false,
|
contentFetched: false,
|
||||||
accountId: accountId
|
accountId: accountId,
|
||||||
|
content: '',
|
||||||
|
messageId: message.envelope?.messageId || undefined
|
||||||
};
|
};
|
||||||
emails.push(email);
|
emails.push(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result: EmailListResult = {
|
|
||||||
emails,
|
|
||||||
totalEmails,
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
totalPages,
|
|
||||||
folder,
|
|
||||||
mailboxes: await getMailboxes(client)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Cache the result with the correct accountId
|
|
||||||
await cacheEmailList(userId, accountId || 'default', folder, page, perPage, result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
// Return empty result for empty folders
|
|
||||||
const result: EmailListResult = {
|
|
||||||
emails: [],
|
|
||||||
totalEmails: 0,
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
totalPages: 0,
|
|
||||||
folder,
|
|
||||||
mailboxes: await getMailboxes(client)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Cache the empty result
|
|
||||||
await cacheEmailList(userId, accountId || 'default', folder, page, perPage, result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error fetching emails for ${userId}${accountId ? ` account ${accountId}` : ''}:`, error);
|
const result: EmailListResult = {
|
||||||
throw error;
|
emails,
|
||||||
|
totalEmails,
|
||||||
|
page,
|
||||||
|
perPage,
|
||||||
|
totalPages,
|
||||||
|
folder,
|
||||||
|
mailboxes: await getMailboxes(client)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cache the result with the correct accountId
|
||||||
|
await cacheEmailList(userId, accountId || 'default', folder, page, perPage, result);
|
||||||
|
|
||||||
|
// Invalidate cache for other accounts' folders to prevent stale data
|
||||||
|
if (accountId) {
|
||||||
|
const accounts = await prisma.mailCredentials.findMany({
|
||||||
|
where: { userId },
|
||||||
|
select: { id: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const account of accounts) {
|
||||||
|
if (account.id !== accountId) {
|
||||||
|
await invalidateFolderCache(userId, account.id, folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
// Don't close the connection, it's managed by the connection pool
|
try {
|
||||||
|
await client.mailboxClose();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error closing mailbox:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -53,6 +53,8 @@ export interface EmailMessage {
|
|||||||
folder: string;
|
folder: string;
|
||||||
contentFetched: boolean;
|
contentFetched: boolean;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
|
messageId?: string;
|
||||||
|
attachments?: EmailAttachment[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Account {
|
export interface Account {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user