added reminder

This commit is contained in:
2026-06-11 11:04:22 +03:00
parent 363aca293a
commit f7cc238308
22 changed files with 1265 additions and 2 deletions
+60
View File
@@ -0,0 +1,60 @@
import logging
from datetime import datetime, timezone
from sqlalchemy import select
from sqlalchemy.orm import Session
from app.chat.notice_inbox import post_notice_to_latest_chat
from app.db.models import Reminder
from app.reminders.notify import bump_notify_seq
from app.reminders.service import RECURRENCE_NONE, _advance_due, _format_local
logger = logging.getLogger(__name__)
def _utcnow() -> datetime:
return datetime.now(timezone.utc)
def check_due_reminders(db: Session) -> int:
now = _utcnow()
stmt = (
select(Reminder)
.where(
Reminder.enabled.is_(True),
Reminder.completed_at.is_(None),
Reminder.due_at <= now,
)
.order_by(Reminder.due_at.asc())
)
rows = list(db.scalars(stmt).all())
fired = 0
for row in rows:
if row.last_fired_at and row.last_fired_at >= row.due_at:
continue
local_when = _format_local(row.due_at, row.timezone, all_day=row.all_day)
notice = f"📅 **Напоминание** · {row.title}\n\n_{local_when}_"
if row.notes:
notice += f"\n{row.notes}"
post_notice_to_latest_chat(notice)
row.last_fired_at = now
if row.recurrence == RECURRENCE_NONE:
row.completed_at = now
row.enabled = False
else:
row.due_at = _advance_due(row.due_at, row.recurrence)
row.last_fired_at = None
row.updated_at = now
fired += 1
if fired:
db.commit()
bump_notify_seq(db)
logger.info("Reminders fired: %d", fired)
return fired