Improve email session management and add IMAP connection pooling
- Switch from localStorage to sessionStorage for email sessions - Add session validation on component mount - Implement IMAP connection pool with folder search capabilities - Add operation locking utility for concurrent request handling - Refactor EOI section component structure - Update API endpoints for better email thread management
This commit is contained in:
@@ -2,6 +2,7 @@ import Imap from 'imap';
|
||||
import { simpleParser } from 'mailparser';
|
||||
import { getCredentialsFromSession, decryptCredentials } from '~/server/utils/encryption';
|
||||
import { listFiles, getFileStats, getMinioClient, uploadFile } from '~/server/utils/minio';
|
||||
import { getIMAPPool } from '~/server/utils/imap-pool';
|
||||
|
||||
interface EmailMessage {
|
||||
id: string;
|
||||
@@ -13,6 +14,7 @@ interface EmailMessage {
|
||||
timestamp: string;
|
||||
direction: 'sent' | 'received';
|
||||
threadId?: string;
|
||||
attachments?: any[];
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
@@ -144,7 +146,7 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
try {
|
||||
imapEmails = await Promise.race([
|
||||
fetchImapEmails(imapConfig, userEmail, clientEmail, limit, interestId),
|
||||
fetchImapEmailsWithPool(sessionId, userEmail, clientEmail, limit, interestId),
|
||||
timeoutPromise
|
||||
]);
|
||||
} catch (imapError) {
|
||||
@@ -187,7 +189,217 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Separate function for IMAP fetching with proper cleanup
|
||||
// Function for IMAP fetching using connection pool
|
||||
async function fetchImapEmailsWithPool(
|
||||
sessionId: string,
|
||||
userEmail: string,
|
||||
clientEmail: string,
|
||||
limit: number,
|
||||
interestId?: string
|
||||
): Promise<EmailMessage[]> {
|
||||
const pool = getIMAPPool();
|
||||
const imap = await pool.getConnection(sessionId);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const allEmails: EmailMessage[] = [];
|
||||
|
||||
// Search in both INBOX and Sent folders
|
||||
const foldersToSearch = ['INBOX', 'Sent', 'Sent Items', 'Sent Mail'];
|
||||
let currentFolderIndex = 0;
|
||||
|
||||
const searchNextFolder = () => {
|
||||
if (currentFolderIndex >= foldersToSearch.length) {
|
||||
resolve(allEmails);
|
||||
return;
|
||||
}
|
||||
|
||||
const folderName = foldersToSearch[currentFolderIndex];
|
||||
currentFolderIndex++;
|
||||
|
||||
imap.openBox(folderName, true, (err: any, box: any) => {
|
||||
if (err) {
|
||||
console.log(`[IMAPPool] Folder ${folderName} not found, trying next...`);
|
||||
searchNextFolder();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[IMAPPool] Searching in folder: ${folderName}`);
|
||||
|
||||
if (!clientEmail || clientEmail.trim() === '') {
|
||||
console.log('[IMAPPool] No client email provided, skipping search');
|
||||
searchNextFolder();
|
||||
return;
|
||||
}
|
||||
|
||||
imap.search(['ALL'], (err: any, results: number[]) => {
|
||||
if (err) {
|
||||
console.error(`[IMAPPool] Search error in ${folderName}:`, err);
|
||||
searchNextFolder();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!results || results.length === 0) {
|
||||
console.log(`[IMAPPool] No emails found in ${folderName}`);
|
||||
searchNextFolder();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[IMAPPool] Found ${results.length} emails in ${folderName}`);
|
||||
const messagesToFetch = results.slice(-limit);
|
||||
let messagesProcessed = 0;
|
||||
|
||||
const fetch = imap.fetch(messagesToFetch, {
|
||||
bodies: '',
|
||||
struct: true,
|
||||
envelope: true
|
||||
});
|
||||
|
||||
fetch.on('message', (msg: any, seqno: number) => {
|
||||
msg.on('body', (stream: any, info: any) => {
|
||||
simpleParser(stream as any, async (err: any, parsed: any) => {
|
||||
if (err) {
|
||||
console.error('[IMAPPool] Parse error:', err);
|
||||
messagesProcessed++;
|
||||
if (messagesProcessed === messagesToFetch.length) {
|
||||
searchNextFolder();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this email involves the client
|
||||
const fromEmail = parsed.from?.text || '';
|
||||
const toEmails = Array.isArray(parsed.to)
|
||||
? parsed.to.map((addr: any) => addr.text).join(', ')
|
||||
: parsed.to?.text || '';
|
||||
const ccEmails = Array.isArray(parsed.cc)
|
||||
? parsed.cc.map((addr: any) => addr.text).join(', ')
|
||||
: parsed.cc?.text || '';
|
||||
|
||||
// Filter to only include emails to/from the client
|
||||
const involvesClient =
|
||||
fromEmail.toLowerCase().includes(clientEmail.toLowerCase()) ||
|
||||
toEmails.toLowerCase().includes(clientEmail.toLowerCase()) ||
|
||||
ccEmails.toLowerCase().includes(clientEmail.toLowerCase());
|
||||
|
||||
if (!involvesClient) {
|
||||
messagesProcessed++;
|
||||
if (messagesProcessed === messagesToFetch.length) {
|
||||
searchNextFolder();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Process attachments
|
||||
const attachments: any[] = [];
|
||||
if (parsed.attachments && parsed.attachments.length > 0) {
|
||||
for (const attachment of parsed.attachments) {
|
||||
try {
|
||||
// Save attachment to MinIO
|
||||
const attachmentId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const fileName = attachment.filename || `attachment-${attachmentId}`;
|
||||
const sanitizedFileName = fileName.replace(/[^a-zA-Z0-9.-]/g, '_');
|
||||
const objectName = `interest-${interestId}/attachments/${attachmentId}-${sanitizedFileName}`;
|
||||
|
||||
const client = getMinioClient();
|
||||
await client.putObject('client-emails', objectName, attachment.content, attachment.content?.length || 0, {
|
||||
'Content-Type': attachment.contentType || 'application/octet-stream',
|
||||
'Content-Disposition': `attachment; filename="${sanitizedFileName}"`
|
||||
});
|
||||
|
||||
attachments.push({
|
||||
id: attachmentId,
|
||||
filename: sanitizedFileName,
|
||||
originalName: attachment.filename,
|
||||
contentType: attachment.contentType,
|
||||
size: attachment.content?.length || attachment.size || 0,
|
||||
path: objectName,
|
||||
bucket: 'client-emails'
|
||||
});
|
||||
|
||||
console.log(`[IMAPPool] Saved attachment: ${sanitizedFileName} (${attachment.content?.length || 0} bytes)`);
|
||||
} catch (attachmentError) {
|
||||
console.error('[IMAPPool] Failed to save attachment:', attachment.filename, attachmentError);
|
||||
// Still include attachment info even if save failed
|
||||
attachments.push({
|
||||
filename: attachment.filename || 'Unknown',
|
||||
originalName: attachment.filename,
|
||||
contentType: attachment.contentType,
|
||||
size: attachment.content?.length || attachment.size || 0,
|
||||
error: 'Failed to save'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const email: EmailMessage = {
|
||||
id: parsed.messageId || `${Date.now()}-${seqno}`,
|
||||
from: fromEmail,
|
||||
to: toEmails,
|
||||
subject: parsed.subject || '',
|
||||
body: parsed.text || '',
|
||||
html: parsed.html || undefined,
|
||||
timestamp: parsed.date?.toISOString() || new Date().toISOString(),
|
||||
direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received',
|
||||
attachments: attachments.length > 0 ? attachments : undefined
|
||||
};
|
||||
|
||||
if (parsed.headers.has('in-reply-to')) {
|
||||
email.threadId = parsed.headers.get('in-reply-to') as string;
|
||||
}
|
||||
|
||||
allEmails.push(email);
|
||||
|
||||
// Cache this email if we have an interestId
|
||||
if (interestId && involvesClient) {
|
||||
try {
|
||||
const emailData = {
|
||||
...email,
|
||||
interestId: interestId
|
||||
};
|
||||
|
||||
const objectName = `interest-${interestId}/${Date.now()}-${email.direction}.json`;
|
||||
const buffer = Buffer.from(JSON.stringify(emailData, null, 2));
|
||||
|
||||
// Upload to the client-emails bucket
|
||||
const client = getMinioClient();
|
||||
client.putObject('client-emails', objectName, buffer, buffer.length, {
|
||||
'Content-Type': 'application/json',
|
||||
}).catch(err => {
|
||||
console.error('[IMAPPool] Failed to cache email:', err);
|
||||
});
|
||||
} catch (cacheError) {
|
||||
console.error('[IMAPPool] Failed to cache email:', cacheError);
|
||||
}
|
||||
}
|
||||
|
||||
messagesProcessed++;
|
||||
|
||||
if (messagesProcessed === messagesToFetch.length) {
|
||||
searchNextFolder();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
fetch.once('error', (err: any) => {
|
||||
console.error('[IMAPPool] Fetch error:', err);
|
||||
searchNextFolder();
|
||||
});
|
||||
|
||||
fetch.once('end', () => {
|
||||
if (messagesProcessed === 0) {
|
||||
searchNextFolder();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
searchNextFolder();
|
||||
});
|
||||
}
|
||||
|
||||
// Separate function for IMAP fetching with proper cleanup (legacy)
|
||||
async function fetchImapEmails(
|
||||
imapConfig: any,
|
||||
userEmail: string,
|
||||
@@ -304,6 +516,48 @@ async function fetchImapEmails(
|
||||
return;
|
||||
}
|
||||
|
||||
// Process attachments
|
||||
const attachments: any[] = [];
|
||||
if (parsed.attachments && parsed.attachments.length > 0) {
|
||||
for (const attachment of parsed.attachments) {
|
||||
try {
|
||||
// Save attachment to MinIO
|
||||
const attachmentId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const fileName = attachment.filename || `attachment-${attachmentId}`;
|
||||
const sanitizedFileName = fileName.replace(/[^a-zA-Z0-9.-]/g, '_');
|
||||
const objectName = `interest-${interestId}/attachments/${attachmentId}-${sanitizedFileName}`;
|
||||
|
||||
const client = getMinioClient();
|
||||
await client.putObject('client-emails', objectName, attachment.content, attachment.content?.length || 0, {
|
||||
'Content-Type': attachment.contentType || 'application/octet-stream',
|
||||
'Content-Disposition': `attachment; filename="${sanitizedFileName}"`
|
||||
});
|
||||
|
||||
attachments.push({
|
||||
id: attachmentId,
|
||||
filename: sanitizedFileName,
|
||||
originalName: attachment.filename,
|
||||
contentType: attachment.contentType,
|
||||
size: attachment.content?.length || attachment.size || 0,
|
||||
path: objectName,
|
||||
bucket: 'client-emails'
|
||||
});
|
||||
|
||||
console.log(`Saved attachment: ${sanitizedFileName} (${attachment.content?.length || 0} bytes)`);
|
||||
} catch (attachmentError) {
|
||||
console.error('Failed to save attachment:', attachment.filename, attachmentError);
|
||||
// Still include attachment info even if save failed
|
||||
attachments.push({
|
||||
filename: attachment.filename || 'Unknown',
|
||||
originalName: attachment.filename,
|
||||
contentType: attachment.contentType,
|
||||
size: attachment.content?.length || attachment.size || 0,
|
||||
error: 'Failed to save'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const email: EmailMessage = {
|
||||
id: parsed.messageId || `${Date.now()}-${seqno}`,
|
||||
from: fromEmail,
|
||||
@@ -312,7 +566,8 @@ async function fetchImapEmails(
|
||||
body: parsed.text || '',
|
||||
html: parsed.html || undefined,
|
||||
timestamp: parsed.date?.toISOString() || new Date().toISOString(),
|
||||
direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received'
|
||||
direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received',
|
||||
attachments: attachments.length > 0 ? attachments : undefined
|
||||
};
|
||||
|
||||
if (parsed.headers.has('in-reply-to')) {
|
||||
|
||||
Reference in New Issue
Block a user