add sync notifications

This commit is contained in:
Tim Bendt
2025-08-06 15:35:40 -05:00
parent 8881b6933b
commit 5eddddc8ec
3 changed files with 133 additions and 3 deletions

View File

@@ -1,12 +1,12 @@
import click
import asyncio
import os
import sys
from rich.progress import Progress, SpinnerColumn, MofNCompleteColumn
from datetime import datetime, timedelta
from src.utils.mail_utils.helpers import ensure_directory_exists
from src.utils.calendar_utils import save_events_to_vdir, save_events_to_file
from src.utils.notifications import notify_new_emails
from src.services.microsoft_graph.calendar import (
fetch_calendar_events,
sync_local_calendar_changes,
@@ -103,7 +103,7 @@ async def fetch_calendar_async(
events, f"{ics_path}/events_latest.ics", progress, task_id, dry_run
)
progress.console.print(
f"[green]Finished saving events to ICS file[/green]"
"[green]Finished saving events to ICS file[/green]"
)
else:
# No destination specified
@@ -220,6 +220,7 @@ async def _sync_outlook_data(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
):
"""Synchronize data from external sources."""
@@ -297,6 +298,20 @@ async def _sync_outlook_data(
progress.console.print(
"\n[bold cyan]Step 2: Fetching new data from server...[/bold cyan]"
)
# Track messages before sync for notifications
maildir_path = (
os.getenv("MAILDIR_PATH", os.path.expanduser("~/Mail")) + f"/{org}"
)
messages_before = 0
if notify:
new_dir = os.path.join(maildir_path, "new")
cur_dir = os.path.join(maildir_path, "cur")
if os.path.exists(new_dir):
messages_before += len([f for f in os.listdir(new_dir) if ".eml" in f])
if os.path.exists(cur_dir):
messages_before += len([f for f in os.listdir(cur_dir) if ".eml" in f])
await asyncio.gather(
fetch_mail_async(
maildir_path,
@@ -320,6 +335,19 @@ async def _sync_outlook_data(
continue_iteration,
),
)
# Send notification for new emails if enabled
if notify and not dry_run:
messages_after = 0
if os.path.exists(new_dir):
messages_after += len([f for f in os.listdir(new_dir) if ".eml" in f])
if os.path.exists(cur_dir):
messages_after += len([f for f in os.listdir(cur_dir) if ".eml" in f])
new_message_count = messages_after - messages_before
if new_message_count > 0:
notify_new_emails(new_message_count, org)
progress.console.print("[bold green]Step 2: New data fetched.[/bold green]")
click.echo("Sync complete.")
@@ -380,6 +408,12 @@ async def _sync_outlook_data(
help="Run in daemon mode.",
default=False,
)
@click.option(
"--notify",
is_flag=True,
help="Send macOS notifications for new email messages",
default=False,
)
def sync(
dry_run,
vdir,
@@ -391,6 +425,7 @@ def sync(
download_attachments,
two_way_calendar,
daemon,
notify,
):
if daemon:
asyncio.run(
@@ -404,6 +439,7 @@ def sync(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
)
)
else:
@@ -418,6 +454,7 @@ def sync(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
)
)
@@ -498,13 +535,13 @@ async def daemon_mode(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
):
"""
Run the script in daemon mode, periodically syncing emails and calendar.
"""
from src.services.microsoft_graph.mail import get_inbox_count_async
from rich.console import Console
from rich.live import Live
from rich.panel import Panel
from rich.text import Text
from datetime import datetime
@@ -549,6 +586,7 @@ async def daemon_mode(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
)
last_sync_time = time.time()
@@ -625,6 +663,7 @@ async def daemon_mode(
continue_iteration,
download_attachments,
two_way_calendar,
notify,
)
last_sync_time = time.time()
console.print(create_status_display("Sync completed ✅", "green"))