Skip to content

Simplify IMAP implementation#16295

Merged
charlesBochet merged 1 commit intomainfrom
imap-error-refactoring
Dec 4, 2025
Merged

Simplify IMAP implementation#16295
charlesBochet merged 1 commit intomainfrom
imap-error-refactoring

Conversation

@neo773
Copy link
Copy Markdown
Member

@neo773 neo773 commented Dec 3, 2025

No description provided.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Dec 3, 2025

Greptile Overview

Greptile Summary

This PR significantly simplifies the IMAP implementation by consolidating fragmented services and switching from mailparser to postal-mime for email parsing.

Key Changes:

  • Replaced mailparser with postal-mime library for email parsing
  • Removed 4 services that added unnecessary abstraction layers (ImapFetchByBatchService, ImapIncrementalSyncService, ImapMessageFetcherService, ImapMessageProcessorService, ImapNetworkErrorHandler)
  • Created 2 new focused services: ImapSyncService (handles folder sync with QRESYNC/UID range logic) and ImapMessageParserService (handles message fetching and parsing)
  • Consolidated error handling by calling parseImapError utility directly instead of through service wrappers
  • Removed empty constructor from ImapClientProvider
  • Updated utility functions to use isDefined helper following codebase conventions
  • Updated tests to match new postal-mime types

Benefits:

  • Net reduction of ~350 lines of code
  • Clearer separation of concerns with focused services
  • Eliminated unnecessary service wrapper (ImapNetworkErrorHandler was just calling a utility function)
  • Simplified dependency injection tree
  • More maintainable architecture with fewer indirection layers

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk - it's a well-executed refactoring that simplifies the codebase
  • The refactoring consolidates logic without changing behavior, removes unnecessary abstraction layers following codebase guidelines, includes test updates, and maintains proper error handling. The changes are well-structured and improve maintainability.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/twenty-server/package.json 5/5 Added postal-mime dependency to replace mailparser for email parsing
packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/messaging-imap-driver.module.ts 5/5 Removed unused services (batch fetch, incremental sync, message fetcher, processor, network error handler) and added new simplified services (sync, parser)
packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/services/imap-sync.service.ts 5/5 New service consolidating sync logic with QRESYNC support and UID range fallback, replacing imap-incremental-sync.service.ts
packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/services/imap-message-parser.service.ts 5/5 New service for parsing IMAP messages using postal-mime, replaces functionality from imap-message-processor.service.ts
packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/services/imap-get-message-list.service.ts 5/5 Refactored to use new ImapSyncService for folder synchronization, simplified flow
packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/services/imap-get-messages.service.ts 5/5 Refactored to use new ImapMessageParserService, simplified message building and extraction logic

Sequence Diagram

sequenceDiagram
    participant Client as IMAP Client
    participant GetList as ImapGetMessageListService
    participant Sync as ImapSyncService
    participant GetMsg as ImapGetMessagesService
    participant Parser as ImapMessageParserService
    participant Extractor as ImapMessageTextExtractorService
    
    Note over Client,Extractor: Message List Sync Flow
    GetList->>Client: getMailboxLock(folderPath)
    Client-->>GetList: lock acquired
    GetList->>Client: Read mailbox state
    Client-->>GetList: uidValidity, maxUid, highestModSeq
    GetList->>Sync: syncFolder(client, folder, cursor, state)
    Sync->>Sync: validateUidValidity()
    alt QRESYNC Available
        Sync->>Client: search({modseq, uid}, {uid: true})
        Client-->>Sync: new/modified message UIDs
    else Fallback to UID Range
        Sync->>Client: search({uid: range}, {uid: true})
        Client-->>Sync: new message UIDs
    end
    Sync-->>GetList: {messageUids}
    GetList->>GetList: Create sync cursor
    GetList->>Client: release lock
    GetList-->>Client: messageExternalIds, nextSyncCursor
    
    Note over Client,Extractor: Message Fetch & Parse Flow
    GetMsg->>GetMsg: groupByFolder(messageExternalIds)
    loop For each folder
        GetMsg->>Parser: parseMessagesFromFolder(uids, folder, client)
        Parser->>Client: getMailboxLock(folderPath)
        Client-->>Parser: lock acquired
        Parser->>Client: fetchAll(uidSet, {uid, source}, {uid: true})
        Client-->>Parser: raw message data
        loop For each message
            Parser->>Parser: PostalMime.parse(source)
            Parser-->>Parser: parsed email
        end
        Parser->>Client: release lock
        Parser-->>GetMsg: MessageParseResult[]
        loop For each parsed result
            GetMsg->>Extractor: extractTextWithoutReplyQuotations(parsed)
            Extractor-->>GetMsg: cleaned text
            GetMsg->>GetMsg: buildMessage(parsed, uid, folder)
        end
    end
    GetMsg-->>Client: MessageWithParticipants[]
Loading

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

21 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 3, 2025

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:32493

This environment will automatically shut down when the PR is closed or after 5 hours.

@neo773 neo773 requested a review from charlesBochet December 3, 2025 18:12
@charlesBochet charlesBochet self-assigned this Dec 3, 2025
@charlesBochet charlesBochet merged commit e112eb4 into main Dec 4, 2025
73 checks passed
@charlesBochet charlesBochet deleted the imap-error-refactoring branch December 4, 2025 13:32
@twenty-eng-sync
Copy link
Copy Markdown

Hey @charlesBochet! After you've done the QA of your Pull Request, you can mark it as done here. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants