diff --git a/lib/utils/email-adapter.ts b/lib/utils/email-adapter.ts index 298f2b19..dc25d7c7 100644 --- a/lib/utils/email-adapter.ts +++ b/lib/utils/email-adapter.ts @@ -9,12 +9,73 @@ import { EmailMessage, - EmailContent, EmailAddress, - EmailAttachment, - EmailFlags -} from '@/types/email'; -import { normalizeEmailContent } from './email-utils'; + EmailAttachment +} from '@/lib/types'; + +// Define a local interface for email flags structure +interface EmailFlags { + seen: boolean; + flagged: boolean; + answered: boolean; + deleted: boolean; + draft: boolean; +} + +/** + * Normalize email content from various formats to our standard structure + */ +function normalizeEmailContent(email: any): { text: string; html: string; isHtml: boolean; direction: string } { + // Initialize with default values + let text = ''; + let html = ''; + let isHtml = false; + + // Extract content from various possible formats + if (email.content) { + if (typeof email.content === 'string') { + // If content is a string, determine if it's HTML or plain text + if (email.content.includes(']*>/g, ''); // Basic HTML stripping + isHtml = true; + } else { + text = email.content; + html = ''; + } + } else if (typeof email.content === 'object') { + // If content is already an object, try to read text/html properties + text = email.content.text || email.content.plainText || ''; + html = email.content.html || ''; + isHtml = !!email.content.isHtml || !!html; + } + } else { + // Try to find content in other common properties + if (email.html) { + html = email.html; + isHtml = true; + } + + if (email.text) { + text = email.text; + } else if (email.plainText) { + text = email.plainText; + } + + // If we have HTML but no text, create a basic text version + if (html && !text) { + text = html.replace(/<[^>]*>/g, ''); + } + } + + // Return standardized content object + return { + text, + html, + isHtml, + direction: 'ltr' // Default to left-to-right + }; +} /** * Convert a legacy email format to our standardized EmailMessage format @@ -85,7 +146,6 @@ export function adaptLegacyEmail(legacyEmail: any): EmailMessage { return { id: legacyEmail.id || legacyEmail.uid?.toString() || `email-${Date.now()}`, messageId: legacyEmail.messageId, - uid: typeof legacyEmail.uid === 'number' ? legacyEmail.uid : undefined, subject: legacyEmail.subject || '(No subject)', from: Array.isArray(legacyEmail.from) ? legacyEmail.from : [], to: Array.isArray(legacyEmail.to) ? legacyEmail.to : [], @@ -96,8 +156,11 @@ export function adaptLegacyEmail(legacyEmail: any): EmailMessage { preview: legacyEmail.preview || '', content: normalizedContent, attachments: normalizedAttachments, - folder: legacyEmail.folder || undefined, - size: typeof legacyEmail.size === 'number' ? legacyEmail.size : undefined + folder: legacyEmail.folder || 'INBOX', + contentFetched: true, + size: typeof legacyEmail.size === 'number' ? legacyEmail.size : 0, + hasAttachments: normalizedAttachments.length > 0, + accountId: legacyEmail.accountId }; }