panel 2 courier api
This commit is contained in:
parent
f6c4c77993
commit
1028a3285e
@ -21,8 +21,17 @@ interface Email {
|
|||||||
preview?: string | null;
|
preview?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure efficient caching with TTL
|
interface CachedData {
|
||||||
const emailCache = new LRUCache<string, Email>({
|
emails: Email[];
|
||||||
|
folders: string[];
|
||||||
|
total: number;
|
||||||
|
hasMore: boolean;
|
||||||
|
page: number;
|
||||||
|
limit: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure efficient caching with TTL - fix the type to allow any data
|
||||||
|
const emailCache = new LRUCache<string, any>({
|
||||||
max: 500, // Store up to 500 emails
|
max: 500, // Store up to 500 emails
|
||||||
ttl: 1000 * 60 * 5, // Cache for 5 minutes
|
ttl: 1000 * 60 * 5, // Cache for 5 minutes
|
||||||
});
|
});
|
||||||
@ -53,6 +62,7 @@ async function getImapClient(userId: string, credentials: any): Promise<ImapFlow
|
|||||||
return existing.client;
|
return existing.client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove invalid options
|
||||||
const client = new ImapFlow({
|
const client = new ImapFlow({
|
||||||
host: credentials.host,
|
host: credentials.host,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
@ -66,11 +76,7 @@ async function getImapClient(userId: string, credentials: any): Promise<ImapFlow
|
|||||||
tls: {
|
tls: {
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false
|
||||||
},
|
},
|
||||||
// Add connection optimizations
|
disableAutoIdle: true
|
||||||
disableAutoIdle: true,
|
|
||||||
idleTimeout: 30000,
|
|
||||||
connectTimeout: 10000,
|
|
||||||
greetingTimeout: 5000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await client.connect();
|
await client.connect();
|
||||||
@ -140,7 +146,7 @@ export async function GET(request: Request) {
|
|||||||
const cachedFolders = emailCache.get(foldersCacheKey);
|
const cachedFolders = emailCache.get(foldersCacheKey);
|
||||||
|
|
||||||
if (cachedFolders) {
|
if (cachedFolders) {
|
||||||
availableFolders = cachedFolders as unknown as string[];
|
availableFolders = cachedFolders;
|
||||||
} else {
|
} else {
|
||||||
const mailboxes = await client.list();
|
const mailboxes = await client.list();
|
||||||
availableFolders = mailboxes.map(box => box.path);
|
availableFolders = mailboxes.map(box => box.path);
|
||||||
@ -153,28 +159,30 @@ export async function GET(request: Request) {
|
|||||||
const result: Email[] = [];
|
const result: Email[] = [];
|
||||||
|
|
||||||
// 8. Fetch emails (if any exist)
|
// 8. Fetch emails (if any exist)
|
||||||
|
// Define start and end variables HERE
|
||||||
|
let start = 1;
|
||||||
|
let end = 0;
|
||||||
|
|
||||||
if (mailbox.exists > 0) {
|
if (mailbox.exists > 0) {
|
||||||
// Calculate range with boundaries
|
// Calculate range with boundaries
|
||||||
const start = Math.min((page - 1) * limit + 1, mailbox.exists);
|
start = Math.min((page - 1) * limit + 1, mailbox.exists);
|
||||||
const end = Math.min(start + limit - 1, mailbox.exists);
|
end = Math.min(start + limit - 1, mailbox.exists);
|
||||||
|
|
||||||
// Use sequence numbers in descending order for newest first
|
// Use sequence numbers in descending order for newest first
|
||||||
const range = mailbox.exists - end + 1 + ':' + (mailbox.exists - start + 1);
|
const range = `${mailbox.exists - end + 1}:${mailbox.exists - start + 1}`;
|
||||||
|
|
||||||
// Fetch messages with optimized options
|
// Fetch messages with optimized options
|
||||||
const options = {
|
const options: any = {
|
||||||
envelope: true,
|
envelope: true,
|
||||||
flags: true,
|
flags: true,
|
||||||
bodyStructure: true,
|
bodyStructure: true
|
||||||
// Only fetch preview if requested
|
|
||||||
...(preview ? {
|
|
||||||
bodyParts: ['TEXT', 'HTML'],
|
|
||||||
bodyPartsOptions: {
|
|
||||||
maxLength: 1000 // Limit preview length
|
|
||||||
}
|
|
||||||
} : {})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Only fetch preview if requested
|
||||||
|
if (preview) {
|
||||||
|
options.bodyParts = ['TEXT', 'HTML'];
|
||||||
|
}
|
||||||
|
|
||||||
const messages = await client.fetch(range, options);
|
const messages = await client.fetch(range, options);
|
||||||
|
|
||||||
// Process messages
|
// Process messages
|
||||||
@ -193,7 +201,7 @@ export async function GET(request: Request) {
|
|||||||
from: message.envelope.from?.[0]?.address || '',
|
from: message.envelope.from?.[0]?.address || '',
|
||||||
fromName: message.envelope.from?.[0]?.name ||
|
fromName: message.envelope.from?.[0]?.name ||
|
||||||
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
||||||
to: message.envelope.to?.map(addr => addr.address).join(', ') || '',
|
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
|
||||||
subject: message.envelope.subject || '(No subject)',
|
subject: message.envelope.subject || '(No subject)',
|
||||||
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
||||||
read: message.flags.has('\\Seen'),
|
read: message.flags.has('\\Seen'),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user