Files
tern-server/README.md
T
Mateusz PstruchaandClaude Opus 5 3b96f80f36
CI / test (push) Canceled after 0s
Store flags in IMAP wire format, add CI and ops docs
Flags were stored via Debug formatting of async-imap's Flag enum, which
produced Rust-internal spellings ("Seen", "Custom(\"$Junk\")") that don't
round-trip back through Flag::from and aren't what the schema documents.
Map to canonical wire spelling (\Seen, $Junk) before the webapp starts
depending on it, with tests pinning the format.

Adds Gitea Actions CI/release workflows, an nginx rate-limiting config
for the §7 gap, and a shared release-process doc. Records the decision
not to reconcile server-side flag changes or deletions, with the
conditions that should trigger revisiting it.

Also applies rustfmt and clears clippy, so CI passes from the first run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 19:55:09 +02:00

3.8 KiB
Raw Permalink Blame History

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, tracks UIDVALIDITY (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 /events delta 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.

Also deferred, and worth knowing because they aren't obvious from the code:

  • Mutation endpoints. There's no way to mark read, tag, or move a message yet. These arrive with phases 4–5, when there's something to mutate.
  • Permissive CORS. CorsLayer::permissive() is a dev convenience so the Vite dev server on another port can talk to the API. Tighten it when the webapp is served from the same origin behind nginx.
  • API-level rate limiting. The nginx layer covers most of this — see nginx-rate-limiting.md. A per-username limiter in the API is the remaining gap.

Decided: the sync engine only moves forward

fetch_new_headers only looks at UIDs above last_synced_uid, so flag changes and deletions made on the source IMAP server are never noticed. A message read or deleted in another client stays unread and present in Tern forever.

This is a deliberate call, not an oversight — it keeps the sync loop trivial, and §2.1's one-way model means the source server isn't treated as authoritative for state anyway. Revisit if either of these becomes true:

  • You start using another mail client against the same mailbox, so divergence becomes visible day to day.
  • The mailbox is large enough that never noticing server-side expunges bloats local storage.

The fix when it comes is a periodic reconciliation pass — UID SEARCH ALL to diff against stored UIDs for deletions, plus a UID FETCH <range> FLAGS sweep for flag drift. That's cheap to bolt on precisely because UIDs and UIDVALIDITY are already tracked per account.

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.