# redseat-api
Rust client for [Redseat](https://github.com/neckaros/redseat_rust) media servers. Work in progress.
```rust
let cloud = CloudClient::http()?;
let session = cloud.sign_in_with_google_id_token(&google_id_token).await?;
let servers = cloud.list_servers(&session.token).await?;
let client = RedseatClient::connect_http(&servers[0], cloud, &session.token, Default::default()).await?;
let server = ServerApi::new(client);
let libraries = server.get_libraries().await?;
let page = server.library(libraries[0].clone())
.get_medias_page(&MediaQuery { limit: Some(10), ..Default::default() })
.await?;
```
A media's file: `library.get_media(&media, &MediaFileQuery::default())` downloads it whole (decrypted in an encrypted library), and `library.media_url(&media.id, ...)` gives its URL with a server token, for a video player that streams it with range requests. In an encrypted library, `library.open_encrypted_media(&media)` reads its plaintext by ranges (`read_at`), decrypting only what is read.
Lists for a sync: `get_tags`, `get_people`, `get_series`, `get_movies`, `get_episodes` and `get_books` take the server's queries; with `after`, only what was modified strictly after it comes back, paged with `limit` and `offset`. `get_deleted` lists deletions the same way.
The other calls of a library, and of a server (`ping`, `get_history`):
| Tags | `create_tag`, `update_tag` (rename, move, alternative names, external ids), `merge_tag`, `remove_tag` |
| People | `get_person`, `create_person`, `update_person`, `remove_person`, `merge_people`, `get_movie_people`, `get_serie_people`, `get_book_people` |
| Series | `create_serie`, `update_serie`, `remove_serie`, `set_episode_watched`, `set_episode_progress`, `add_searched_episode_media` |
| Movies | `create_movie`, `update_movie`, `remove_movie`, `set_movie_watched`, `set_movie_progress`, `add_searched_movie_media` |
| Books | `get_book`, `create_book`, `update_book`, `remove_book`, `set_book_watched`, `update_book_poster` |
| Medias | `get_media_metadata`, `get_media_by_hash`, `update_media`, `set_media_progress`, `remove_medias`, `media_share_url`, `download_medias`, `download_medias_in_background` |
| Channels | `get_channels`, `add_tag_to_channel`, `remove_tag_from_channel`, `channel_stream_url`, `channel_hls_url`, `stop_channel_hls` |
| Images | `get_serie_image`, `get_episode_image`, `get_movie_image`, `get_book_image`, `get_person_image`, `get_channel_image` (bytes), `channel_image_url` |
| Requests through plugins | `process_request`, `process_request_bytes`, `check_request_permanent`, `add_request`, `request_share_url` |
Ids, flags and dates are plain arguments; items to create (`TagForAdd`, `PersonForAdd`), their changes (`TagForUpdate`, `MediaForUpdate`...), image options (`ImageRequestOptions`) and requests (`RsRequest`, `RsGroupDownload`, in `redseat_api::rs_plugin_common_interfaces::request`) are the server's types.
Coming from the TypeScript client `@redseat/api`, where a call has no method of the same name here:
| `setKey(passphrase)` | `library.set_passphrase(passphrase)` |
| `decryptMedia(passphrase, data, iv)` | `library.decrypt_thumb(&media, &data)` once the passphrase is set, or `crypto::decrypt_buffer(&crypto::derive_key(passphrase), &iv, &data)` |
| `encryptText(passphrase, text)` | `crypto::encrypt_text(&crypto::derive_text_key(passphrase), text)` |
| `decryptText(passphrase, text)` | `crypto::decrypt_text(&crypto::derive_text_key(passphrase), text)` |
| `renameTag`, `tagChangeParent`, `tagAddAlt`, `tagRemoveAlt`, `tagAddOtherId`, `tagRemoveOtherId` | `update_tag` with the matching field of `TagForUpdate` |
| `getAllEpisodes(query)` | `get_episodes`, paged with `limit` and `offset` |
| `mediaUpdateProgress` | `set_media_progress` |
| `uploadGroup(download, { spawn })` | `download_medias`, or `download_medias_in_background` with `spawn` |
| `processRequestBlob` | `process_request_bytes` |
| `getMediaShareUrl`, `getRequestShareUrl` | `media_share_url`, `request_share_url` |
| `getChannelStreamUrl`, `getChannelHlsUrl`, `getChannelImageUrl` | `channel_stream_url`, `channel_hls_url`, `channel_image_url` (`AuthorizedUrl`: the token is apart from the URL) |
| `client.get('/libraries/:id/<kind>/:id/image?...')` | `get_serie_image`, `get_movie_image`... with `ImageRequestOptions` |
| `client.getFullUrl(path)` with `getValidAuthToken()` | `client.authorized_url(path, &[])` |
## Mirror (feature `mirror`)
A local SQLite copy of a server's libraries, kept in sync in the background and read at once, offline too, like the Redseat phone app does.
```rust
use redseat_api::mirror::{Mirror, MirrorEvent, MirrorOptions};
let mut options = MirrorOptions::new(data_dir.join(&server_id).join("mirror.db"));
options.thumbs_limit = 2 * 1024 * 1024 * 1024; // thumbnail cache: 2 GB
let mirror = Mirror::open(options).await?; // reads work now, offline too
mirror.connect(server); // ServerApi: sync every library, live events
mirror.set_priority(Some(&library_id)); // the library on screen syncs first
let mut events = mirror.subscribe(); // Changed { library, kinds }, Sync { .. }, ...
let count = mirror.count_medias(&library_id, &MediaQuery::default()).await?;
let medias = mirror.medias_at(&library_id, &MediaQuery::default(), 0, 100).await?;
let thumb = mirror.thumb(&library_id, &medias[0].item).await?; // disk cache, fetched once
mirror.disconnect(); // sign-out: stop syncing, keep the data
```
- Everything per library: medias, tags, people, series, movies, episodes and books, with their relations, and the library list. One file per server, at the path the app gives; thumbnails go in a `thumbs` folder next to it.
- The first sync is full; later ones fetch what changed since the last one (with a 1 ms overlap) and apply the deletions. Live events are applied as they come, and a reconnection catches up. A lost sync state means one full repair.
- Encrypted libraries are stored as the server sends them; `set_passphrase` then decrypts thumbnails on read. They stay encrypted on disk.
- Hidden libraries (a per-user preference on the server) are kept with `hidden` set; `set_library_hidden` hides or shows one again, on the server then in the local copy.
- `Mirror::open` is for native targets: SQLite, thumbnail files and tokio. `Mirror::with_parts` takes the app's own `MirrorStore` (items), `ThumbStore` (thumbnail bytes) and `Runtime` (background tasks and timers), for another runtime or for `wasm32-unknown-unknown`. This crate has no web implementation of them yet.
Desktop apps can get the Google ID token with `google::GoogleSignIn` (browser sign-in with a loopback redirect and PKCE). See `examples/sign_in.rs`.
- Data types are the server's own, from [`rs-plugin-common-interfaces`](https://crates.io/crates/rs-plugin-common-interfaces) (`redseat_api::types`).
- Encrypted libraries use the server's own implementation, from [`redseat-crypto`](https://crates.io/crates/redseat-crypto) (`redseat_api::crypto`).
- Builds for `wasm32-unknown-unknown`. UniFFI and wasm-bindgen bindings are planned.
Licensed under MIT or Apache-2.0, at your option.