The app/services/gmail directory is well-structured and modular. It follows a clear separation of concerns, with each file handling a specific aspect of Gmail operations (scanning, deleting, archiving, etc.). The __init__.py file acts as a clean facade, exposing necessary functions while hiding internal implementation details.
The codebase demonstrates high consistency in the following areas:
- State Management: All modules consistently use the global
stateobject fromapp.core.stateto track progress, store results, and handle errors. This ensures a unified way of communicating status to the frontend. - Service Retrieval: Every module retrieves the Gmail service using
get_gmail_service()fromapp.services.auth, handling potential errors in a uniform way. - Batch Processing: Operations like scanning and deleting consistently use Gmail API's batch capabilities (
new_batch_http_requestorbatchModify) to optimize performance and respect rate limits. - Status Reporting: Each module provides a
get_<operation>_status()function that returns a copy of the relevant state, maintaining a consistent interface for status polling. - Error Handling:
try-exceptblocks are used consistently to catch exceptions, update the state with the error message, and mark the operation as done.
app/services/gmail/helpers.py is effectively used to share common logic, preventing code duplication. Key helpers include:
build_gmail_query: Centralizes query construction logic.get_sender_info&get_subject: Standardizes header parsing.get_unsubscribe_from_headers: Encapsulates the logic for finding unsubscribe links.validate_unsafe_url: Provides shared security validation.
While most files are focused, a few areas show signs of high complexity:
app/services/auth.py(External togmaildir but critical): This file is a "God Object" candidate (685 lines). Theget_gmail_servicefunction contains a massive nestedrun_oauthfunction that handles the entire OAuth flow, including starting a local HTTP server. This logic should ideally be extracted into a separateOAuthManagerclass or module.app/services/gmail/delete.py: This file (439 lines) handles both the scanning of senders for deletion and the actual deletion (both single sender and bulk). Splitting this intodelete_scan.pyanddelete_action.pywould improve maintainability and separation of concerns.
Status: Clean. No circular dependencies were found. The dependency graph is unidirectional and healthy:
app/services/gmail/*importsapp.core.state,app.services.auth, andapp.services.gmail.helpers.app/services/authimportsapp.core.stateandapp.core.settings.app/core/statehas no internal dependencies.app/services/gmail/__init__.pyimports from submodules but submodules do not import back from__init__.
- Refactor
app/services/auth.py: Extract the OAuth flow logic into a dedicated handler to reduce the size and complexity of the auth service. - Split
app/services/gmail/delete.py: Separate the scanning logic (scan_senders_for_delete) from the deletion logic (delete_emails_by_sender,delete_emails_bulk) to align with the single-responsibility principle.