4c1a6457c52c64f708cbb9b86a8d839263fdc15b
Covers phases 1-2 of the build order in the architecture spec: the IMAP sync engine (IDLE + UIDVALIDITY tracking + jittered reconnect, headers first with lazy body fetch), the full data model, and a minimal REST API for auth, accounts, folders, messages and delta polling. Credentials are encrypted at rest with ChaCha20-Poly1305 rather than libsodium secretbox, to avoid a system dependency for the same construction. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
tern-server
The sync server for Tern — the only component that ever speaks IMAP/SMTP. See docs/server-architecture-spec.md for the full design.
What's built (build-order phases 1–2)
- DB schema (
migrations/): users/sessions, accounts + per-account sync state, messages/bodies/attachments, folders, tags, rules, devices — the full data model from the spec, even though rules/tags/threading execution isn't implemented yet. - IMAP sync engine (
src/imap_sync/): one task per account — connects over TLS, selects INBOX, tracksUIDVALIDITY(re-syncing the account from scratch if it changes), fetches new mail headers-first (ENVELOPE/FLAGS) then lazily fills in bodies/attachments in a trailing pass, and IDLEs for pushed changes with a periodic renewal and jittered-backoff reconnect on any failure. - Minimal REST API (
src/api/): local username/password auth (Argon2 + bearer session tokens), account management (IMAP credentials encrypted at rest with ChaCha20-Poly1305), folder listing, cursor-paginated message listing, single-message + thread-by-id fetch, and a polling/eventsdelta endpoint.
What's explicitly deferred
Per the spec's suggested build order (§9), not yet built: SMTP sending/outbox, tag & rule execution (schema exists, engine doesn't), the JWZ threading algorithm (each message currently gets its own thread_id — no linking/merging pass yet), spam header rules, WebSocket push (the /events poll endpoint is the interim/fallback path), and the Android/webapp-facing UnifiedPush integration.
Running locally
cp .env.example .env
openssl rand -base64 32 # paste into .env as CREDENTIAL_KEY
docker compose up -d db
cargo run
Or the whole stack (server + Postgres + nginx) via docker compose up -d once CREDENTIAL_KEY is exported in your shell or a .env file docker compose picks up.
Migrations run automatically on startup (sqlx::migrate!).
Notes on deviations from the spec
- Encryption: §7 suggests libsodium secretbox; this uses ChaCha20-Poly1305 (RustCrypto) instead — same AEAD construction family, but pure Rust with no libsodium system dependency. See
src/crypto.rs. - Web framework: axum, per your choice between the spec's axum/actix-web options.
Languages
Rust
99.8%
Dockerfile
0.2%