feat: Enhance home route to filter threads requiring replies and waiting over an hour

This commit is contained in:
bolade
2025-08-13 16:23:12 +01:00
parent cbc587205f
commit 0c94225ff8
+14 -6
View File
@@ -55,7 +55,7 @@ def home(
account: str | None = None,
alerts_processed: int | None = None,
):
from datetime import datetime
from datetime import datetime, timedelta
from sqlalchemy import func
@@ -69,15 +69,23 @@ def home(
)
# Main query joining threads with their latest message dates
q = (
db.query(Thread, latest_message_subq.c.latest_message_date)
.outerjoin(latest_message_subq, Thread.id == latest_message_subq.c.thread_id)
.order_by(latest_message_subq.c.latest_message_date.desc().nulls_last())
q = db.query(Thread, latest_message_subq.c.latest_message_date).outerjoin(
latest_message_subq, Thread.id == latest_message_subq.c.thread_id
)
# Only include threads that currently require a reply and have been waiting >= 1 hour
cutoff = datetime.utcnow() - timedelta(hours=1)
q = q.filter(
Thread.requires_reply.is_(True),
latest_message_subq.c.latest_message_date <= cutoff,
)
if account:
q = q.filter(Thread.account_email == account.lower())
# Order by most recently updated (still respecting the cutoff filter)
q = q.order_by(latest_message_subq.c.latest_message_date.desc().nulls_last())
results = q.limit(100).all()
# Create threads with additional info including sequential frontend ID and formatted latest message date
@@ -382,7 +390,7 @@ def _sync_emails_once(cfg: dict) -> int:
)
db = SessionLocal()
try:
send_to_all("Fetching emails from inbox...")
inbox = client.fetch_folder_emails(