feat: Add email count tracking to folders with total and pending counts

This commit is contained in:
Bryce
2025-08-05 11:04:37 -07:00
parent 31871ed8ec
commit f2fe9d646b
4 changed files with 122 additions and 29 deletions

View File

@@ -130,6 +130,49 @@ class IMAPService:
except:
pass
def get_folder_email_count(self, folder_name: str) -> int:
"""Get the count of emails in a specific folder."""
try:
# Connect to IMAP server
self._connect()
# Login
self.connection.login(
self.config.get('username', ''),
self.config.get('password', '')
)
# Select the folder
resp_code, content = self.connection.select(folder_name)
if resp_code != 'OK':
return 0
# Get email count
resp_code, content = self.connection.search(None, 'ALL')
if resp_code != 'OK':
return 0
# Count the emails
email_ids = content[0].split()
count = len(email_ids)
# Close folder and logout
self.connection.close()
self.connection.logout()
self.connection = None
return count
except Exception as e:
logging.error(f"Error getting email count for folder {folder_name}: {str(e)}")
if self.connection:
try:
self.connection.logout()
except:
pass
self.connection = None
return 0
def sync_folders(self) -> Tuple[bool, str]:
"""Sync IMAP folders with local database."""
try:
@@ -171,6 +214,12 @@ class IMAPService:
)
db.session.add(new_folder)
synced_count += 1
else:
# Update existing folder with email counts
# 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
db.session.commit()
return True, f"Successfully synced {synced_count} folders"