Files
Mateusz PstruchaandClaude Opus 5.5 90fc65dbf5 Add live change notifications and notify/don't-notify rules
Every change is appended to an events table and broadcast to open
WebSockets (GET /api/ws); GET /api/events?since= reads the same log as
the polling fallback and delta endpoint, so clients can switch
transports without losing their place. Inserts are serialised so ids
are ordered by commit, cursors older than the 7-day retention reset,
and the socket authenticates in-band since browsers can't set headers.

New mail notifies when it's unread and still in the inbox after rules;
rule actions notify / dont_notify override that, last one applied
wins. An account's first sync and UIDVALIDITY re-syncs are backlog and
never notify. Thread actions, outbox status, and tag/folder/rule/
account edits emit events too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 18:55:34 +02:00

29 lines
1.4 KiB
SQL

-- Change events for WebSocket push and the polling fallback
-- (server-architecture-spec.md §3.8, webapp-client-spec.md §3).
--
-- One append-only log per server, read per user. The id is the cursor
-- clients resume from, so it has to be monotonic in commit order — the
-- server serialises inserts (see src/events.rs) rather than relying on the
-- sequence alone, which hands out ids in *start* order across concurrent
-- transactions.
CREATE TABLE events (
id BIGSERIAL PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
kind TEXT NOT NULL,
payload JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX events_user_id_id_idx ON events(user_id, id);
CREATE INDEX events_created_at_idx ON events(created_at);
-- Set when a message is discovered by an incremental sync pass, as opposed
-- to the first sync of an account or a UIDVALIDITY re-sync — only mail that
-- genuinely just arrived is a candidate for a "new mail" notification.
ALTER TABLE messages ADD COLUMN arrived_live BOOLEAN NOT NULL DEFAULT false;
-- Whether the account has finished its first full pass. Until then every
-- message found is backlog, however recent, and shouldn't notify.
ALTER TABLE sync_state ADD COLUMN caught_up BOOLEAN NOT NULL DEFAULT false;
UPDATE sync_state SET caught_up = last_synced_uid > 0;