redseat-api 0.4.0

Rust client for Redseat media servers
Documentation
# REDSEAT-API

Rust client library for Redseat media servers. Rust counterpart of the TypeScript packages `@redseat/api`, `@redseat/api-http` and `@redseat/api-webrtc` (in `C:\Devs\redseat-svelte\packages`, read them with `git show HEAD:packages/...` if the working copy is empty).

Used by `C:\Devs\redseat_gpui` (desktop app) and meant to be embedded in other apps.

## Design decisions

- **One crate, cargo features per transport.** A core client (`RedseatClient`, `ServerApi`, `LibraryApi`, like the TS) talks through a `Transport` trait. HTTP + SSE is the first transport, behind a feature; WebRTC can come later the same way.
- **No hand-written API types.** Data types come from `rs-plugin-common-interfaces` (`crate::types`), the crate the server itself uses. A type the client needs that is still private to the server (`redseat_rust/src/domain` or `src/model`) is moved to that shared crate, not copied here.
- **Crypto is shared too.** Encrypted libraries use `redseat-crypto` (`crate::crypto`), the implementation the server uses. Never reimplement encryption here.
- **No UI or runtime ties.** No GPUI dependency. Async API; the app owns the runtime (the GPUI app runs tokio on a background thread).
- **Future bindings.** UniFFI (Swift, Kotlin, Python, React Native) and wasm-bindgen (web) wrappers are a future target. Keep the public API to plain data and async functions, and keep `cargo check --target wasm32-unknown-unknown` passing.

## Dependencies

`rs-plugin-common-interfaces` and `redseat-crypto` come from crates.io. Their sources are in `C:\Devs\redseat-interfaces`. To change one, publish a new version there, or use a temporary `[patch.crates-io]` path override while developing and remove it before merging.

## Structure

| Module | Port of | What |
|--------|---------|------|
| `transport` | `api/src/transport.ts` | `Transport` trait, `Request`/`Response`, raw events |
| `error` | `RedseatErrorKind` | One `Error` enum, `ErrorKind`, `Error::from_status` |
| `cloud` | mobile `core/src/services/auth.ts`, `api-http/src/control-plane.ts` | Cloud sign-in (better-auth), session, server list, server token. Cloud types (`CloudServer`, `ServerToken`...) live here |
| `client` | `api/src/client.ts` | `RedseatClient`: auth header, token refresh before expiry and after a 401, typed `/sse` stream |
| `server`, `library` | `api/src/server.ts`, `api/src/library.ts` | `ServerApi`, `LibraryApi`: libraries (`ServerLibraryForRead`), medias pages and count, thumbnails, passphrase, whole media files (`get_media`), stream URLs for players (`media_url`), lists with `after` for a sync (tags, people, series, movies, episodes, books) and deletions (`get_deleted`) |
| `encrypted` | none (the TS client downloads encrypted files whole) | `EncryptedMediaReader`: the plaintext of an encrypted media by ranges, decrypting only the CBC blocks read |
| `events` | `api/src/sse-types.ts` | `ServerEvent`, server event names |
| `sse` | `api-http/src/sse-fetch.ts` | SSE parsing, transport-neutral |
| `direct` | `api-http/src/direct.ts` | Server address candidates and probing |
| `http` (feature `http`) | `api-http/src/http-transport.ts`, `client.ts` | reqwest + rustls transport, `connect` picks the address |
| `google` (feature `desktop-auth`, native only) | none | Google sign-in in the browser: loopback redirect, PKCE |
| `mirror` (feature `mirror`, native only) | phone app `redseat-gallery/packages/core/src/services/sync.ts`, `sse-sync.ts`, `db/migrations.ts` | Local SQLite copy of a server's libraries: background sync with a priority library, live events, local reads, thumbnail disk cache. See "Mirror" below |

Media files: `media_url` gives a URL and the server token (`AuthorizedUrl`) for players that stream with HTTP range requests; it needs a transport with `Transport::http_base_url`. In an encrypted (`crypt`) library the server sends the file after its header and thumb: fetch it whole with `get_media`, which decrypts it, or read it by ranges with `open_encrypted_media` (servers with range support for encrypted files, redseat_rust #75).

Features: `http` and `desktop-auth` are default; `open-browser` adds `google::open_browser`; `mirror` adds the local copy (rusqlite `bundled`, the 0.31 that `rs-plugin-common-interfaces`' own `rusqlite` feature uses, so one SQLite is linked). Library code never unwraps (`deny(clippy::unwrap_used...)`).

## Mirror

`Mirror` keeps one server's libraries in a SQLite file the app chooses (one per server), with the thumbnails in `thumbs/` next to it.

- **Files**: `mod.rs` (public API, scheduler, connection loop), `sync.rs` (sync of one library, live events, thumbnail fetch), `sqlite.rs` (schema, migrations, queries), `store.rs` (`MirrorStore` trait: storage is swappable, an IndexedDB backend can come later), `thumbs.rs` (thumbnail files), `tests.rs` (a fake server in memory).
- **Schema** (`sqlite.rs`, version in `user_version`, migrations in `MIGRATIONS`): one table per kind (`medias`, `tags`, `people`, `series`, `movies`, `episodes`, `books`) with the columns queries need (library, id, name, dates, type, rating, size...) and `data`, the item as JSON of its shared type. A new server field needs no migration; a new filtered or sorted column does. Media relations also go in `media_tags`, `media_people`, `media_series`, `media_movies`, `media_books`. Plus `libraries`, `sync_state` (one JSON `SyncState` per library) and `thumbs` (size, version, last use).
- **Sync** (`sync.rs`): per library, kinds in `SyncKind::ALL` order. A kind without a watermark is fetched in full, then what was not fetched again is removed (rows carry the `generation` of the sync state). With a watermark, `after = watermark - 1 ms` (the server's `after` is strict). Medias page by `modified` with the server's page keys (`<modified>|<id>`), the others by `limit`/`offset`, deletions last (an item modified after its deletion date came back and is kept). Writes never replace a newer `modified` (a live event may have landed first).
- **Scheduling** (`mod.rs`): `connect` starts `concurrency` workers on a queue. `set_priority` moves a library to the front; a running sync finishes first. Each (re)connection of the event stream refreshes the library list and queues every library (the catch-up).
- **Encrypted libraries**: stored as sent; thumbnails stay encrypted on disk and are decrypted on read with the key from `set_passphrase` (checked on a cached thumbnail, else with the server).
- Not mirrored: watch history, channels, and the `media_progress` and `media_rating` events (per user; the medias the next sync brings carry the requesting user's values).

## Commands

```bash
cargo test
cargo clippy --all-targets --all-features -- -D warnings
cargo fmt --check
cargo check --target wasm32-unknown-unknown --no-default-features
cargo check --target wasm32-unknown-unknown --no-default-features --features http
cargo test --all-features mirror                         # the mirror against a fake server
cargo run --example sign_in --features open-browser   # needs REDSEAT_GOOGLE_DESKTOP_CLIENT_ID/_SECRET
```