diff --git a/app/imap_service.py b/app/imap_service.py index c0bc3e9..f11452c 100644 --- a/app/imap_service.py +++ b/app/imap_service.py @@ -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" diff --git a/app/models.py b/app/models.py index 4156eab..febf6af 100644 --- a/app/models.py +++ b/app/models.py @@ -40,5 +40,7 @@ class Folder(Base): rule_text = db.Column(db.Text) priority = db.Column(db.Integer) organize_enabled = db.Column(db.Boolean, default=True) + total_count = db.Column(db.Integer, default=0) + pending_count = db.Column(db.Integer, default=0) user = db.relationship('User', backref=db.backref('folders', lazy=True)) \ No newline at end of file diff --git a/app/templates/partials/folder_card.html b/app/templates/partials/folder_card.html index fd27f7e..23b9148 100644 --- a/app/templates/partials/folder_card.html +++ b/app/templates/partials/folder_card.html @@ -1,37 +1,38 @@
{{ folder.rule_text }}
+ + +{{ folder.rule_text }}
+