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.name }}

+

{{ folder.name }}

- - -
-
- -
-

{{ folder.rule_text }}

+ + +
-
+ +
+
+ {{ folder.total_count }} emails + {{ folder.pending_count }} pending +
{% if folder.priority == 1 %} High Priority {% elif folder.priority == -1 %} @@ -39,6 +40,13 @@ {% else %} Normal Priority {% endif %} +
+ +
+

{{ folder.rule_text }}

+
+ +
Organize: - +
diff --git a/migrations/versions/a3ad1b9a0e5f_add_email_count_fields_to_folders.py b/migrations/versions/a3ad1b9a0e5f_add_email_count_fields_to_folders.py new file mode 100644 index 0000000..ebbbf9a --- /dev/null +++ b/migrations/versions/a3ad1b9a0e5f_add_email_count_fields_to_folders.py @@ -0,0 +1,34 @@ +"""Add email count fields to folders + +Revision ID: a3ad1b9a0e5f +Revises: f8ba65458ba2 +Create Date: 2025-08-05 11:00:46.880458 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a3ad1b9a0e5f' +down_revision = 'f8ba65458ba2' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('folders', schema=None) as batch_op: + batch_op.add_column(sa.Column('total_count', sa.Integer(), nullable=True)) + batch_op.add_column(sa.Column('pending_count', sa.Integer(), nullable=True)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('folders', schema=None) as batch_op: + batch_op.drop_column('pending_count') + batch_op.drop_column('total_count') + + # ### end Alembic commands ###