scaffolding for processing

This commit is contained in:
2025-08-06 20:16:12 -07:00
parent 8d0d0976b9
commit ab376f5317
2 changed files with 144 additions and 8 deletions

View File

@@ -4,7 +4,8 @@ import logging
from typing import List, Dict, Optional, Tuple
from email.header import decode_header
from email.utils import parsedate_to_datetime
from app.models import db, Folder, User
from app.models import db, Folder, User, ProcessedEmail
from app.processed_emails_service import ProcessedEmailsService
from app import create_app
class IMAPService:
@@ -12,6 +13,7 @@ class IMAPService:
self.user = user
self.config = user.imap_config or {}
self.connection = None
self.processed_emails_service = ProcessedEmailsService(user)
def _connect(self):
"""Create an IMAP connection based on configuration."""
@@ -294,6 +296,14 @@ class IMAPService:
self.connection = None
return []
def get_pending_emails(self, folder_name: str) -> List[str]:
"""Get list of email UIDs that are pending processing in a folder."""
try:
return self.processed_emails_service.get_pending_emails(folder_name)
except Exception as e:
logging.error(f"Error getting pending emails for folder {folder_name}: {str(e)}")
return []
def get_email_headers(self, folder_name: str, email_uid: str) -> Dict[str, str]:
"""Get email headers for a specific email UID."""
try:
@@ -348,7 +358,7 @@ class IMAPService:
return {}
def sync_folders(self) -> Tuple[bool, str]:
"""Sync IMAP folders with local database."""
"""Sync IMAP folders with local database and track email processing status."""
try:
if not self.config:
return False, "No IMAP configuration found"
@@ -401,15 +411,34 @@ class IMAPService:
db.session.add(new_folder)
synced_count += 1
else:
# Update existing folder with email counts and recent emails
# Get the total count of emails in this folder
total_count = self.get_folder_email_count(folder_name)
existing_folder.total_count = total_count
existing_folder.pending_count = 0 # Initially set to 0
# Get the list of email UIDs in this folder
email_uids = self.get_folder_email_uids(folder_name)
# Sync with processed emails service to track which emails are pending
new_uids_count = self.processed_emails_service.sync_folder_emails(
folder_name=folder_name,
email_uids=email_uids
)
# Update folder with email counts
existing_folder.total_count = len(email_uids)
# Update pending count based on processed emails service
pending_count = self.processed_emails_service.get_pending_count(folder_name)
existing_folder.pending_count = pending_count
# Get the most recent emails for this folder
recent_emails = self.get_recent_emails(folder_name, 3)
existing_folder.recent_emails = recent_emails
# Clean up records for emails that no longer exist
if new_uids_count > 0:
deleted_count = self.processed_emails_service.cleanup_old_records(
folder_name=folder_name,
current_uids=email_uids
)
if deleted_count > 0:
print(f" - Cleaned up {deleted_count} old email records")
db.session.commit()
return True, f"Successfully synced {synced_count} folders"