feat(backend): watches report alert counts, alerts carry watch params and time away, webhooks their last delivery

This commit is contained in:
hh committed 2026-10-19 02:57:27 +00:00
1 parent 9da0c06ac1
commit 6a4fd6ad47
4 files changed
+76 -37

No files matched your search

+20 -2
View File
@@ -44,9 +44,15 @@ WITH alert AS (
SELECT * FROM alert
""" # noqa: S608
_WATCH_STATS = """
SELECT watch_id, count(*) AS count, max(ts) AS last FROM alerts
WHERE watch_id = ANY($1::int[]) GROUP BY watch_id
"""
_ALERT_ROWS = """
SELECT a.id, a.account_id, a.ts, a.seen, a.payload,
w.id AS watch_id, w.kind AS watch_kind, w.name AS watch_name,
w.params AS watch_params,
m.text AS stored_text,
m.sender_id AS stored_sender, m.date AS stored_date,
m.deleted_at IS NOT NULL AS stored_deleted,
@@ -225,9 +231,17 @@ async def describe_watches(
{chat for chat, _ in targets if chat is not None},
{person for _, person in targets if person is not None},
)
stats = {
row["watch_id"]: row
for row in await pool.fetch(_WATCH_STATS, [watch.id for watch in watches])
}
return [
WatchItem(
**watch.model_dump(), chat=refs.chat(chat), person=refs.person(person)
**watch.model_dump(),
chat=refs.chat(chat),
person=refs.person(person),
alert_count=stats[watch.id]["count"] if watch.id in stats else 0,
last_alert_at=stats[watch.id]["last"] if watch.id in stats else None,
)
for watch, (chat, person) in zip(watches, targets, strict=True)
]
@@ -267,11 +281,15 @@ def _alert_item(row: asyncpg.Record, refs: Refs) -> AlertItem:
seen=row["seen"],
event=event,
watch=AlertWatch(
id=row["watch_id"], kind=row["watch_kind"], name=row["watch_name"]
id=row["watch_id"],
kind=row["watch_kind"],
name=row["watch_name"],
params=json.loads(row["watch_params"]),
),
chat=refs.chat(chat_id),
message=message,
peer=refs.person(peer_id),
away_since=payload.get("since"),
path=_path(chat_id, message_id, peer_id),
)