courrier multi account restore compose
This commit is contained in:
parent
ccd8b8d762
commit
5f2da42848
@ -197,7 +197,8 @@ export default function CourrierPage() {
|
||||
(emailWithFlags.flags && !emailWithFlags.flags.seen) ||
|
||||
false;
|
||||
|
||||
if (isUnread && email.folder === 'INBOX') {
|
||||
// Count unread emails regardless of current folder
|
||||
if (isUnread) {
|
||||
// If email has an accountId, increment that account's count
|
||||
if (email.accountId) {
|
||||
const currentCount = accountUnreadCounts.get(email.accountId) || 0;
|
||||
|
||||
@ -302,60 +302,80 @@ export async function getEmails(
|
||||
const start = Math.max(1, totalEmails - (page * perPage) + 1);
|
||||
const end = Math.max(1, totalEmails - ((page - 1) * perPage));
|
||||
|
||||
// Fetch messages
|
||||
const messages = await client.fetch(`${start}:${end}`, {
|
||||
envelope: true,
|
||||
flags: true,
|
||||
bodyStructure: true,
|
||||
internalDate: true,
|
||||
size: true,
|
||||
bodyParts: ['HEADER']
|
||||
});
|
||||
// Only fetch if we have messages
|
||||
if (totalEmails > 0) {
|
||||
// Fetch messages
|
||||
const messages = await client.fetch(`${start}:${end}`, {
|
||||
envelope: true,
|
||||
flags: true,
|
||||
bodyStructure: true,
|
||||
internalDate: true,
|
||||
size: true,
|
||||
bodyParts: ['HEADER']
|
||||
});
|
||||
|
||||
// Process messages
|
||||
const emails: EmailMessage[] = [];
|
||||
for await (const message of messages) {
|
||||
const email: EmailMessage = {
|
||||
id: message.uid.toString(),
|
||||
from: (message.envelope?.from || []).map(addr => ({
|
||||
name: addr.name || '',
|
||||
address: addr.address || ''
|
||||
})),
|
||||
to: (message.envelope?.to || []).map(addr => ({
|
||||
name: addr.name || '',
|
||||
address: addr.address || ''
|
||||
})),
|
||||
subject: message.envelope?.subject || '',
|
||||
date: message.internalDate || new Date(),
|
||||
flags: {
|
||||
seen: message.flags.has('\\Seen'),
|
||||
answered: message.flags.has('\\Answered'),
|
||||
flagged: message.flags.has('\\Flagged'),
|
||||
draft: message.flags.has('\\Draft'),
|
||||
deleted: message.flags.has('\\Deleted')
|
||||
},
|
||||
size: message.size || 0,
|
||||
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
|
||||
folder: folder,
|
||||
contentFetched: false
|
||||
// Process messages
|
||||
const emails: EmailMessage[] = [];
|
||||
for await (const message of messages) {
|
||||
const email: EmailMessage = {
|
||||
id: message.uid.toString(),
|
||||
from: (message.envelope?.from || []).map(addr => ({
|
||||
name: addr.name || '',
|
||||
address: addr.address || ''
|
||||
})),
|
||||
to: (message.envelope?.to || []).map(addr => ({
|
||||
name: addr.name || '',
|
||||
address: addr.address || ''
|
||||
})),
|
||||
subject: message.envelope?.subject || '',
|
||||
date: message.internalDate || new Date(),
|
||||
flags: {
|
||||
seen: message.flags.has('\\Seen'),
|
||||
answered: message.flags.has('\\Answered'),
|
||||
flagged: message.flags.has('\\Flagged'),
|
||||
draft: message.flags.has('\\Draft'),
|
||||
deleted: message.flags.has('\\Deleted')
|
||||
},
|
||||
size: message.size || 0,
|
||||
hasAttachments: message.bodyStructure?.childNodes?.some(node => node.disposition === 'attachment') || false,
|
||||
folder: folder,
|
||||
contentFetched: false,
|
||||
accountId: accountId
|
||||
};
|
||||
emails.push(email);
|
||||
}
|
||||
|
||||
const result: EmailListResult = {
|
||||
emails,
|
||||
totalEmails,
|
||||
page,
|
||||
perPage,
|
||||
totalPages,
|
||||
folder,
|
||||
mailboxes: await getMailboxes(client)
|
||||
};
|
||||
emails.push(email);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching emails for ${userId}${accountId ? ` account ${accountId}` : ''}:`, error);
|
||||
throw error;
|
||||
|
||||
17
lib/types.ts
17
lib/types.ts
@ -33,29 +33,26 @@ export interface EmailAttachment {
|
||||
|
||||
export interface EmailMessage {
|
||||
id: string;
|
||||
messageId?: string;
|
||||
subject: string;
|
||||
from: EmailAddress[];
|
||||
to: EmailAddress[];
|
||||
cc?: EmailAddress[];
|
||||
bcc?: EmailAddress[];
|
||||
subject: string;
|
||||
content: string;
|
||||
preview?: string;
|
||||
date: Date;
|
||||
flags: {
|
||||
seen: boolean;
|
||||
flagged: boolean;
|
||||
answered: boolean;
|
||||
deleted: boolean;
|
||||
flagged: boolean;
|
||||
draft: boolean;
|
||||
deleted: boolean;
|
||||
};
|
||||
preview?: string;
|
||||
content?: string;
|
||||
html?: string;
|
||||
text?: string;
|
||||
size: number;
|
||||
hasAttachments: boolean;
|
||||
attachments?: EmailAttachment[];
|
||||
folder: string;
|
||||
size?: number;
|
||||
contentFetched: boolean;
|
||||
accountId?: string;
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user