feat: re-alert updated Web Push threads (Closes #571)
CI / lint (pull_request) Successful in 1m22s
CI / build-release (pull_request) Successful in 5s
CI / release-candidate (pull_request) Has been skipped

This commit is contained in:
timmy committed 2026-08-11 13:51:58 +00:00
1 parent 257bc0110e
commit e9271f5aab
4 files changed
+237 -38

No files matched your search

+26 -17
View File
@@ -59,12 +59,12 @@ async def dispatch_unread_updates(
return 0
try:
page = await unread()
thread_ids = {
int(item["id"])
thread_revisions = {
int(item["id"]): str(item.get("updated_at") or "")
for item in page.get("items", [])
if isinstance(item, dict) and str(item.get("id", "")).isdigit()
}
deliveries = await asyncio.to_thread(store.claim_unseen, thread_ids)
deliveries = await asyncio.to_thread(store.claim_unseen, thread_revisions)
if session_active is not None:
try:
authorized = [await session_active(item.session_id) for item in deliveries]
@@ -88,16 +88,21 @@ async def dispatch_unread_updates(
if ownership_lost.is_set():
return 0
count = 0
digest_pending = set(delivery.digest_ids)
new_ids = tuple(
thread_id
for thread_id in delivery.thread_ids
if thread_id not in digest_pending
digest_pending = set(delivery.digest_revisions)
new_revisions = tuple(
thread_revision
for thread_revision in delivery.thread_revisions
if thread_revision not in digest_pending
)
individual_revisions = new_revisions[
:max(0, max_individual_notifications)
]
overflow_revisions = (
delivery.digest_revisions
+ new_revisions[len(individual_revisions):]
)
individual_ids = new_ids[:max(0, max_individual_notifications)]
overflow_ids = delivery.digest_ids + new_ids[len(individual_ids):]
can_send_digest = True
for thread_id in individual_ids:
for thread_id, revision in individual_revisions:
still_owner = await asyncio.to_thread(
store.acquire_dispatch_lease,
owner,
@@ -138,10 +143,12 @@ async def dispatch_unread_updates(
# poll instead of paying the endpoint deadline repeatedly.
break
await asyncio.to_thread(
store.mark_delivered, delivery.session_id, (thread_id,)
store.mark_delivered,
delivery.session_id,
((thread_id, revision),),
)
count += 1
if overflow_ids and can_send_digest:
if overflow_revisions and can_send_digest:
still_owner = await asyncio.to_thread(
store.acquire_dispatch_lease,
owner,
@@ -153,11 +160,11 @@ async def dispatch_unread_updates(
return count
payload = json.dumps(
{
"title": f"{len(overflow_ids)} new work updates",
"title": f"{len(overflow_revisions)} new work updates",
"body": "Tap to review them in Stackchain.",
"route": "#/my-work/updates",
"tag": "stackchain-update-digest",
"update_count": len(overflow_ids),
"update_count": len(overflow_revisions),
},
separators=(",", ":"),
)
@@ -181,11 +188,13 @@ async def dispatch_unread_updates(
await asyncio.to_thread(
store.mark_digest_pending,
delivery.session_id,
overflow_ids,
overflow_revisions,
)
return count
await asyncio.to_thread(
store.mark_delivered, delivery.session_id, overflow_ids
store.mark_delivered,
delivery.session_id,
overflow_revisions,
)
count += 1
return count
+117 -20
View File
@@ -1,17 +1,45 @@
import json
import os
import sqlite3
from collections.abc import Iterable, Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
@dataclass(frozen=True)
class PushDelivery:
session_id: str
subscription: dict
thread_ids: tuple[int, ...]
digest_ids: tuple[int, ...] = ()
thread_revisions: tuple[tuple[int, str], ...]
digest_revisions: tuple[tuple[int, str], ...] = ()
@property
def thread_ids(self) -> tuple[int, ...]:
return tuple(thread_id for thread_id, _revision in self.thread_revisions)
@property
def digest_ids(self) -> tuple[int, ...]:
return tuple(thread_id for thread_id, _revision in self.digest_revisions)
def _revisions(
values: Mapping[int, str] | Iterable[int | tuple[int, str]],
) -> tuple[tuple[int, str], ...]:
if isinstance(values, Mapping):
items = list(values.items())
else:
items = []
for value in values:
if isinstance(value, tuple):
items.append(value)
else:
items.append((value, ""))
normalized = {}
for thread_id, revision in items:
thread_id = int(thread_id)
if thread_id > 0:
normalized[thread_id] = str(revision or "")
return tuple(sorted(normalized.items()))
class PushSubscriptionStore:
@@ -32,6 +60,7 @@ class PushSubscriptionStore:
CREATE TABLE IF NOT EXISTS push_deliveries (
session_id TEXT NOT NULL,
thread_id INTEGER NOT NULL,
revision TEXT NOT NULL DEFAULT '',
PRIMARY KEY (session_id, thread_id),
FOREIGN KEY (session_id) REFERENCES push_subscriptions(session_id)
ON DELETE CASCADE
@@ -44,12 +73,28 @@ class PushSubscriptionStore:
CREATE TABLE IF NOT EXISTS push_digest_pending (
session_id TEXT NOT NULL,
thread_id INTEGER NOT NULL,
revision TEXT NOT NULL DEFAULT '',
PRIMARY KEY (session_id, thread_id),
FOREIGN KEY (session_id) REFERENCES push_subscriptions(session_id)
ON DELETE CASCADE
);
"""
)
delivery_columns = {
row[1] for row in connection.execute("PRAGMA table_info(push_deliveries)")
}
if "revision" not in delivery_columns:
connection.execute(
"ALTER TABLE push_deliveries ADD COLUMN revision TEXT NOT NULL DEFAULT '*'"
)
digest_columns = {
row[1]
for row in connection.execute("PRAGMA table_info(push_digest_pending)")
}
if "revision" not in digest_columns:
connection.execute(
"ALTER TABLE push_digest_pending ADD COLUMN revision TEXT NOT NULL DEFAULT '*'"
)
os.chmod(self.path, 0o600)
def _connect(self):
@@ -109,8 +154,10 @@ class PushSubscriptionStore:
"SELECT 1 FROM push_subscriptions WHERE session_id = ?", (session_id,)
).fetchone() is not None
def claim_unseen(self, thread_ids: Iterable[int]) -> list[PushDelivery]:
candidates = tuple(sorted({int(value) for value in thread_ids if int(value) > 0}))
def claim_unseen(
self, thread_revisions: Mapping[int, str] | Iterable[int | tuple[int, str]]
) -> list[PushDelivery]:
candidates = _revisions(thread_revisions)
if not candidates:
return []
with self._connect() as connection:
@@ -120,42 +167,92 @@ class PushSubscriptionStore:
deliveries = []
for session_id, encoded in rows:
delivered = {
row[0]
row[0]: row[1]
for row in connection.execute(
"SELECT thread_id FROM push_deliveries WHERE session_id = ?",
"SELECT thread_id, revision FROM push_deliveries WHERE session_id = ?",
(session_id,),
)
}
unseen = tuple(value for value in candidates if value not in delivered)
for thread_id, revision in candidates:
if delivered.get(thread_id) == "*":
connection.execute(
"""UPDATE push_deliveries SET revision = ?
WHERE session_id = ? AND thread_id = ?""",
(revision, session_id, thread_id),
)
delivered[thread_id] = revision
unseen = tuple(
candidate
for candidate in candidates
if (
candidate[0] not in delivered
or (
bool(candidate[1])
and (
not delivered[candidate[0]]
or candidate[1] > delivered[candidate[0]]
)
)
)
)
if unseen:
pending = {
row[0]
row[0]: row[1]
for row in connection.execute(
"SELECT thread_id FROM push_digest_pending WHERE session_id = ?",
"SELECT thread_id, revision FROM push_digest_pending WHERE session_id = ?",
(session_id,),
)
}
digest_ids = tuple(value for value in unseen if value in pending)
digest_revisions = []
for thread_id, revision in unseen:
if pending.get(thread_id) in {revision, "*"}:
digest_revisions.append((thread_id, revision))
if pending[thread_id] == "*":
connection.execute(
"""UPDATE push_digest_pending SET revision = ?
WHERE session_id = ? AND thread_id = ?""",
(revision, session_id, thread_id),
)
deliveries.append(
PushDelivery(session_id, json.loads(encoded), unseen, digest_ids)
PushDelivery(
session_id,
json.loads(encoded),
unseen,
tuple(digest_revisions),
)
)
return deliveries
def mark_digest_pending(self, session_id: str, thread_ids: Iterable[int]) -> None:
def mark_digest_pending(
self,
session_id: str,
thread_revisions: Mapping[int, str] | Iterable[int | tuple[int, str]],
) -> None:
values = _revisions(thread_revisions)
with self._connect() as connection:
connection.executemany(
"INSERT OR IGNORE INTO push_digest_pending(session_id, thread_id) VALUES (?, ?)",
((session_id, int(thread_id)) for thread_id in thread_ids),
"""INSERT INTO push_digest_pending(session_id, thread_id, revision)
VALUES (?, ?, ?)
ON CONFLICT(session_id, thread_id) DO UPDATE SET
revision = excluded.revision""",
((session_id, thread_id, revision) for thread_id, revision in values),
)
def mark_delivered(self, session_id: str, thread_ids: Iterable[int]) -> None:
values = tuple(int(thread_id) for thread_id in thread_ids)
def mark_delivered(
self,
session_id: str,
thread_revisions: Mapping[int, str] | Iterable[int | tuple[int, str]],
) -> None:
values = _revisions(thread_revisions)
with self._connect() as connection:
connection.executemany(
"INSERT OR IGNORE INTO push_deliveries(session_id, thread_id) VALUES (?, ?)",
((session_id, thread_id) for thread_id in values),
"""INSERT INTO push_deliveries(session_id, thread_id, revision)
VALUES (?, ?, ?)
ON CONFLICT(session_id, thread_id) DO UPDATE SET
revision = excluded.revision""",
((session_id, thread_id, revision) for thread_id, revision in values),
)
connection.executemany(
"DELETE FROM push_digest_pending WHERE session_id = ? AND thread_id = ?",
((session_id, thread_id) for thread_id in values),
((session_id, thread_id) for thread_id, _revision in values),
)