Skip to content

Commit

Permalink
restore unread
Browse files Browse the repository at this point in the history
  • Loading branch information
christianlouis committed Feb 14, 2025
1 parent 9fc27d6 commit 17fc8fa
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions app/tasks/imap_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def pull_inbox(
):
"""
Connects to the IMAP inbox, fetches new emails (last 7 days),
and processes attachments without marking emails as read.
and processes attachments while preserving the original unread status.
"""
logger.info(f"Connecting to {mailbox_key} at {host}:{port} (SSL={use_ssl})")
processed_emails = load_processed_emails()
Expand All @@ -142,6 +142,14 @@ def pull_inbox(
logger.info(f"Found {len(msg_numbers)} emails from the last 7 days in {mailbox_key}.")

for num in msg_numbers:
# Check if email is unread
status, flag_data = mail.fetch(num, "(FLAGS)")
if status != "OK":
logger.warning(f"Failed to fetch flags for message {num} in {mailbox_key}. Status={status}")
continue

is_unread = b"\\Seen" not in flag_data[0] # Email was originally unread

status, msg_data = mail.fetch(num, "(RFC822)")
if status != "OK":
logger.warning(f"Failed to fetch message {num} in {mailbox_key}. Status={status}")
Expand All @@ -163,15 +171,18 @@ def pull_inbox(
# Process attachments
has_attachment = fetch_attachments_and_enqueue(email_message)

# If an attachment was found, store in cache
if has_attachment:
processed_emails[msg_id] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
save_processed_emails(cleanup_old_entries(processed_emails))

# Delete email if configured
if delete_after_process:
logger.info(f"Deleting message {num.decode()} from {mailbox_key}")
mail.store(num, "+FLAGS", "\\Deleted")
# Store processed email in cache
processed_emails[msg_id] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
save_processed_emails(cleanup_old_entries(processed_emails))

# Handle deletion or unread restoration
if delete_after_process:
logger.info(f"Deleting message {num.decode()} from {mailbox_key}")
mail.store(num, "+FLAGS", "\\Deleted")
else:
# Restore unread status if it was unread before processing
if is_unread:
mail.store(num, "-FLAGS", "\\Seen")

if delete_after_process:
mail.expunge()
Expand Down

0 comments on commit 17fc8fa

Please sign in to comment.