diff --git a/AGENTS.md b/AGENTS.md index b11237f..8cb1fe3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -61,8 +61,17 @@ update it only when dependencies change. - `src/player/` owns rodio playback and audio analysis. - `src/federation/` owns DHT discovery, peer catalogs, audio exchange, and federation caches. +- `src/podcasts/` owns podcast feed HTTP and refresh policy, the episode + stream reader (HTTP Range, bounded buffer) and the ordered progress writer; + podcast identity, merge rules and the SQLite store come from FRID + (`music_dht::podcast`, `furumi_library::podcasts`) and must not be + re-derived here. Queue items are identified by `state::track_key` / + `TrackItem::key`, never by numeric id (every episode has the same sentinel + id); music-only code must skip `TrackItem::is_podcast()` items. - `src/devices.rs` owns trusted-device replication, membership, and playback - coordination. + coordination. Its wire types are the shared `music_dht::device_sync` + definitions; do not add local copies (golden tests in + `devices/wire_compat_tests.rs` keep the v3 JSON). - `src/ui/` renders `AppState`; rendering code must not start background work or mutate persistence. - `src/media.rs` owns OS media controls. @@ -155,7 +164,25 @@ When changing it: - keep snapshots able to repair peers that missed older operations; - treat membership and revocation as replicated durable state; - keep playback commands targeted and deduplicated; -- test merge behavior from at least two operation orders. +- test merge behavior from at least two operation orders; +- never store this build's protocol version for another device: use the + device's own report (`profile_protocol_version`) and only raise a stored + version from relayed profiles (`relayed_protocol_version`); +- gate everything a peer cannot parse by the version it reported: podcast + operations, podcast snapshot sections and playback state with podcast items + go only to v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`, + `playback_allowed_for_peer`), a podcast queue is never handed to an older + device and its playback commands are ignored while the queue has episodes; + never strip episodes from a queue to make it fit an old peer; an older + peer still gets the playback coordination, with the queue redacted + (`redact_for_jam`), so its engine does not fail over, and the + "Private item" music entries it echoes back in its own states are left + out (`drop_echoed_private_items`), never adopted; +- apply podcast operations and snapshot sections only through + `PodcastStore` (`apply_remote_op`, `apply_snapshot`); never call the + podcast store while holding the sync-log connection; +- keep Jam snapshots redacted (`redact_for_jam`) and map guest placeholders + back with `restore_jam_private_items`. Do not replace eventual reconciliation with assumptions about a continuously connected leader. @@ -221,7 +248,9 @@ Add tests at the closest stable boundary: - pure state transitions in `app/update_tests.rs`; - SQLite behavior and migrations in `library/tests.rs`; - operation merge, tombstone, membership, and playback sync behavior in - `devices/tests.rs`; + `devices/tests.rs`; podcast sync between device stores and simulated v3 + peers in `devices/podcast_sync_tests.rs`; wire shapes in + `devices/wire_compat_tests.rs`; - search/ranking/federation conversion in `federation/tests.rs`; - protocol-specific tests beside `federation/audio.rs` or `federation/catalog.rs` when appropriate. diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 48b1cfa..6191f0c 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -66,12 +66,27 @@ This layer connects devices owned or trusted by the same user. It carries personal state such as: - likes and playlist operations; +- podcast subscriptions, episode progress and listening sessions; - device membership and revocation; - acknowledgements and synchronization progress; - playback state, commands, and handoff information. Pairing establishes the trust relationship. After that, changes are replicated through an append-only operation log and applied to materialized local tables. +The wire types are the shared `music_dht::device_sync` definitions (protocol +v4); golden tests keep the v3 JSON of furumi-tui 0.3.x byte for byte. + +Each device stores the protocol version that the other devices report +themselves (their hello and pairing profiles, the sender's own entry of a +`devices` list, their own profile announcements). A relayed profile only +raises a stored version, because older relays list every device with their +own constant; this build never stores its own version for another device. +Everything a peer cannot parse is gated by that version: podcast operations, +podcast snapshot sections and playback state that contains podcast items +reach only v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`, +`playback_allowed_for_peer`). An older client would reject a whole message +over one unknown operation kind, or silently drop an episode it does not +understand. ### Music federation @@ -97,8 +112,12 @@ audio. Other TUI peers use a dedicated Jam ALPN to submit the same portable playback commands used by connected-device control and receive the host's playback snapshot. They receive queue metadata, not audio. -Jam never exchanges trusted membership, likes, playlists, or listening -history. Volume remains local. Commands carry unique IDs and are retried until +Jam never exchanges trusted membership, likes, playlists, listening history +or podcast state. The host's podcast episodes are private: the published +snapshot replaces them with opaque placeholders (`redact_for_jam`), guest +commands that add episodes are dropped, and a guest's placeholders are mapped +back to the host's episodes (`restore_jam_private_items`); a change that +drops, duplicates or seeks in them is refused. Volume remains local. Commands carry unique IDs and are retried until the host acknowledges them, while inactive participants expire from the runtime session. Regenerating the capability or restarting the host invalidates the previous link. @@ -189,6 +208,12 @@ The synchronization model combines: - per-peer acknowledgements to determine when old tombstones can be compacted; - snapshots to repair a peer that missed older operations. +Vectors only grow, so operations hidden from a peer while it ran an older +protocol are never sent again after it upgrades. Snapshots are the recovery +path for that case; the podcast sections carry the subscription, share and +progress registers and the most recent listening sessions, bounded by the +shared FRID limits and by the line budget of each message. + This is eventual consistency scoped to a trusted device group. A temporarily offline laptop can modify playlists, another device can continue playback, and both can later converge without a permanently available coordinator. @@ -212,7 +237,25 @@ The synchronized state describes the queue, current item, position, pause state, volume, shuffle/repeat mode, and the active playback owner. Commands are targeted and deduplicated. Handoff transfers intent and position; the receiving device resolves the track against its own library or federation sources before -starting its audio engine. +starting its audio engine. Each queue item names its source: a podcast episode +travels as `PlaybackSource::PodcastEpisode` with the reference needed to play +it (the receiving device streams it from the publisher at the handed-off +position, with or without a subscription). A queue with episodes is not handed +to a device below protocol v4, and playback commands from such a device are +ignored while the local queue has episodes. Such a device still gets the +playback snapshot with its coordination envelope, but with every episode +replaced by an opaque "Private item" (`redact_for_jam`): an engine renews the +owner's liveness only from the owner's own announcements, so an old device +that heard nothing would take over by automatic failover after +`owner_timeout_ms` and replace the queue. This departs from FRID's +`PLAYBACK_PROTOCOL.md` rule 1 ("otherwise send `None`") and is reported to +FRID as a contract gap. An old device that mirrors such a queue drops the +`source` its types do not know, so each placeholder becomes a music item +titled "Private item"; when it later takes over (Play on it, a paused-startup +claim, failover while this device is gone) or sends a command, it announces +those. States from devices below v4 are therefore cleaned on receipt +(`drop_echoed_private_items`: bare music items with that title and no content +id, federated source, artist, release or duration), with a status message. Active-device leases and idle timing prevent stale snapshots from immediately taking control after a device reconnects. This provides practical coordination @@ -271,6 +314,105 @@ order and have their tab indices remapped. A `version` key that TOML attached to a binding (written after a `[[keymaps]]` header) is rejected, so a versioned file is never silently mistaken for a legacy one. +## Podcasts + +Podcasts are a separate content type, not library tracks. The shared contract +lives in FRID: `music_dht::podcast` defines podcast and episode identity, feed +URL canonicalization, the RSS/Atom parser and the merge rules, and +`furumi_library::podcasts::PodcastStore` persists the catalog, the personal +registers (subscription, share, progress) and listening sessions in +`podcasts.sqlite3`. The TUI does not re-derive ids or merge rules. + +The TUI owns what the contract leaves to clients: + +- **Fetching.** Feeds are fetched with conditional requests (ETag, + Last-Modified), bounded size (32 MiB per feed, 10 MiB per cover) and time + (15 s to connect, 60 s per request, 120 s for a whole fetch with its + redirects), and at most 10 redirects (http and https only) followed by + hand. The + address that the leading permanent hops (301/308) lead to becomes a feed + alias and the fetch URL, even when a temporary hop (CDN, tracker) follows + it; a chain that starts with a temporary hop never moves the feed. + Validators are dropped after a permanent redirect because they belong to + the old address. The prototype database's validators are dropped once at + import, so the first check parses the feed (seasons and episode numbers + were not stored by the prototype). +- **Scheduling.** The store keeps each feed's local fetch state (validators, + last error, consecutive failures, next check). A timer asks it which + subscriptions are due; feeds are checked at most a few at a time, a success + waits an hour, and failures back off exponentially (honouring + `Retry-After`). Errors never clear the stored catalog. +- **Runtime boundary.** Every store call, fetch and parse of the app runs on + the blocking pool or as a task and reports back through `AppEvent`s; + `update::podcasts` holds the pure state transitions. The device-sync + stream handlers call the store (snapshot export and merges) inside + `block_in_place`, so a store busy with a large feed refresh does not hold + a Tokio worker's other tasks; they are never called on the UI thread. Store open failures (newer schema, + foreign file) disable only the Podcasts tab and leave the file untouched. +- **Sync log.** Local actions (subscribe, unsubscribe, mark played, progress) + draw their operation stamps from the device-sync log through + `DeviceSync::run_podcast_action`, and the operations are appended with + exactly those stamps, so the store's registers and the log agree. The lock + order is podcast store, then sync log; device sync never calls the store + while it holds the log. Only the newest progress operation of an origin per + episode stays in the log (later operations of one origin always win the + register comparison). +- **Device sync.** Received podcast operations are kept in the log (and + relayed) and merged into the store with `PodcastStore::apply_remote_op`; + invalid ones are skipped but acknowledged. Operations that arrive before + the store opens wait in `sync_podcast_pending` and are applied when it is + attached. Snapshot sections are exported with `fill_snapshot` and merged + with `apply_snapshot` after the operations of the same message. The UI + reloads what it shows after such changes and checks the feed of a + subscription that came from another device. +- **Queue identity.** An episode is a `TrackItem` with an explicit + `podcast` reference (`TrackItem::podcast_episode`); its numeric id is a + shared sentinel and never identifies it. Queue keys check the episode id + first (`ep:…`), then music content ids, federated sources and library rows. + Music-only paths (likes, playlists, history, content-id resolution, + federation download, similarity, share links, metadata edits, release and + artist jumps) check `is_podcast()` and skip episodes. On the wire an + episode is `PlaybackSource::PodcastEpisode`; received ones become episodes + again (`TrackItem::from_playback_track`) and the library source mode never + drops them. Jam private placeholders are numbered queue entries that go + back on the wire as `PlaybackSource::Private`. +- **Playback.** Starting an episode resolves its current audio URL and + resume position from the store, opens `podcasts::stream` (a bounded + Range-request reader on its own thread), decodes and positions it with + coarse seeking, all off the UI and audio threads, then hands the source to + the audio thread with the player's source generation. Any later start or + stop bumps that generation, so a slow stream never replaces what the user + chose since. A stale audio URL (4xx) triggers one feed refresh and a retry + with the address the feed lists for the same episode id. A stream that + cannot seek does not start at a saved position it cannot reach, and seeks + in it (or in an episode still connecting) are refused. The stream is + cancelled by the same generation: a superseded start stops connecting or + reading up to its resume offset, and a read blocked on a stalled server + inside the audio callback returns within 100 ms, so it cannot hold the + audio thread past the next stop or start. The player does not report the + end of a superseded source. Pause and resume pressed while a source is + prepared update the intent it starts with. +- **End of a stream.** The reader records whether the decoder stopped on a + read error (lost connection, expired URL, cancellation) and whether the + server announced the file size. Only an end that the server described, or + one within the completion window, completes the episode and moves the + queue on; otherwise the last seen position is saved and playback stops on + the episode (`playback::played_to_the_end`). +- **Progress.** `podcasts::progress` runs one writer thread so every + progress write of this device is ordered. The app tick follows the + position and actual listening time and sends: an explicit start (or + "start over"), checkpoints every 20 s and on pause, explicit seeks (saved + once the audio thread reports that the seek worked, + `PlayerEvent::Seeked`), completion (`reaches_completion` or the real end + of the stream) and an end with the last + seen position on stop, track change, handoff and exit. When the store + reports a newer generation (another device started over, for example), the + playback stops checkpointing until the user's next explicit action. + Listening sessions are recorded at pause, completion and end; the store + keeps those that pass the shared threshold. Queue items carry the latest + progress generation this device saw, so a handoff target starts its + session above it. + ## Scripted visualizations Visualizations are an extension boundary rather than hard-coded rendering @@ -398,6 +540,7 @@ Furumi stores different kinds of state according to their lifetime: | State | Storage | Role | | --- | --- | --- | | Library, playlists, likes, history | SQLite | Durable local source of truth | +| Podcast subscriptions, episodes, progress | SQLite (`podcasts.sqlite3`, FRID store) | Durable personal state (synced with trusted devices) and a refreshable episode catalog | | Device operation log and replicas | SQLite | Offline synchronization | | Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data | | Federation catalog cache | SQLite/cache | Faster network browsing | @@ -405,7 +548,8 @@ Furumi stores different kinds of state according to their lifetime: | Settings, keymap, identity | Platform config/data dirs | Node configuration | | Queue and playback snapshots | Application/device sync state | Continuity and handoff | -Caches are replaceable. The local library and device operation log are durable. +Caches are replaceable. The local library, the podcast store's personal state +and the device operation log are durable. This distinction lets maintenance and recovery code discard derived network data without risking the user's collection. @@ -434,6 +578,10 @@ The source tree follows the architectural responsibilities: - `similarity.rs` owns model acquisition, preprocessing, background indexing, and the replaceable exact in-memory index; - `federation/` owns DHT-facing search, peer catalogs, and audio exchange; +- `podcasts/` owns feed HTTP (conditional requests, redirects, limits), + refresh scheduling policy, the episode stream reader and the progress + writer; the podcast model, identity, merge rules and SQLite store come + from FRID; - `devices.rs` owns trusted-device replication and playback coordination; - `app/` owns state transitions and runtime orchestration; - `ui/` renders the TUI; diff --git a/CHANGELOG.md b/CHANGELOG.md index a8beae7..2b7a022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,70 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.4.0] - unreleased + ### Added -- A Podcasts tab on `4`. It is a placeholder in this build and states that - podcasts are not available yet. +- A Podcasts tab on `4`: subscribe to RSS/Atom feeds by URL, browse + podcast → seasons → episodes with saved position, played state, duration and + date, search episodes, filter by listening state, change the order, mark + episodes played or unplayed, check one or all feeds, and unsubscribe (progress + and history are kept). Feeds are checked in the background with conditional + requests and back-off; permanent redirects become feed aliases. +- Podcast data uses the shared FRID store (`furumi_library::podcasts`) in + `podcasts.sqlite3`; a database of the earlier podcast test build is migrated + in place, and a newer or foreign file is refused without changes. +- Podcasts sync between trusted devices (device-sync protocol 4): + subscriptions, unsubscribing, feed addresses, episode progress ("played", + position, "start over") and listening sessions. A podcast subscribed on one + device appears on the others without its URL; each device checks the feed + itself. Podcast operations are applied to the podcast store, kept in the + sync log and relayed; snapshots carry the podcast state (bounded) so new + devices and devices that upgrade from an older version catch up. Periodic + progress saves are coalesced in the log. +- A queue with podcast episodes is handed to other devices with the episode, + its position and pause; the receiving device does not need the + subscription. +- Podcast episodes play from the Podcasts tab and the shared queue (mixed + with music): audio streams from the publisher through a bounded HTTP Range + buffer, Play continues from the saved position, `b` starts over, and the + position is saved every 20 s and on pause, seek, stop, track change and + exit. Completion and listening sessions follow the shared FRID rules. A + stale audio URL triggers one feed check for the episode's new address. A + lost connection stops playback at the saved position (the episode is not + completed and the queue does not move on). Without a file size from the + server, a closed stream counts as the end within the completion window of + the duration the audio file states, or past 80 % of a duration only the + feed announced; a superseded or stalled stream + is cancelled at the next start or stop; pause pressed while an episode + connects is kept; seeks that do not move the audio are not saved. +- Podcast key bindings in the `podcasts` context: `o`, `/`, `Shift+R`, + `Ctrl+R`, `Shift+S`, `f`, `m`, `Shift+U`, `b`. - An optional top-level `version` in `keymap.toml`; the current format is `version = 2`, and a `podcasts` binding context. ### Changed +- `music-dht` 0.6.0 and `furumi-library` 0.3.0. +- Queue items are keyed by their source: podcast episodes by episode id, + library rows as `lib:` instead of `local:`. Likes, playlists, + music history, similarity, share links and metadata edits skip episodes. +- Device sync speaks protocol 4 and uses the shared `music_dht::device_sync` + wire types instead of private copies; v3 messages are unchanged byte for + byte (golden tests). Devices below protocol 4 never receive podcast + operations, podcast snapshot sections or a queue with episodes: handing + such a queue to them is refused ("needs a Furumi update"), and their + playback commands are ignored while the queue has episodes. They still get + the playback coordination, with each episode shown as "Private item", so + they do not take playback over after two minutes of silence. When such a + device does take over, the "Private item" entries it echoes back as music + are left out of the queue here, with a status message. +- Jam guests see the host's podcast episodes as "Private item" (no title, + feed or address). Guest changes that add, drop or duplicate them, or seek + in them, are refused; a guest leaving a Jam drops the placeholders. +- Controlling another v4 device may queue and play episodes there; an + episode started by another device's command at position 0 resumes from its + saved position. - Tab order is now `1` Library, `2` Playlists, `3` Queue, `4` Podcasts, `5` Settings, `6` Logs, for the tab bar, Tab/Shift+Tab, and the default number keys. @@ -32,6 +87,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The help window names the target tab for number-key shortcuts ("Go to Settings") instead of "Go to tab N". +### Fixed + +- The protocol version stored for another device came from this build's own + constant (on profile announcements and accepted pairings), not from the + device. Versions now come from the device's own reports; relayed profiles + only raise them. Invites and profile announcements carry the version, and + the first start of a new version announces it. + ## [0.2.8] - 2026-09-02 ### Added diff --git a/Cargo.lock b/Cargo.lock index 51d9a33..096d153 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1559,8 +1559,7 @@ dependencies = [ [[package]] name = "federation-net" version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01e5c2e6e5af020dc202d4924125ef57719aa21f8d2567e2d943914386bc8980" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" dependencies = [ "blake3", "data-encoding", @@ -1698,9 +1697,8 @@ dependencies = [ [[package]] name = "furumi-library" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f78466b6345cb90d7bb37db5d9dd67476ab6c2c3b2202876d24f448c314a9a08" +version = "0.3.0" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" dependencies = [ "anyhow", "blake3", @@ -1715,7 +1713,7 @@ dependencies = [ [[package]] name = "furumi_tui" -version = "0.3.5" +version = "0.4.0" dependencies = [ "anyhow", "base64", @@ -3239,16 +3237,17 @@ dependencies = [ [[package]] name = "music-dht" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c3f75d49d3a742a6777a1c4cc53f397399dfc9ddb70034c57aeb44d75d2ee4f" +version = "0.6.0" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" dependencies = [ "async-trait", "blake3", "data-encoding", + "encoding_rs", "federation-net", "futures", "postcard", + "quick-xml", "rand 0.9.5", "rusqlite", "serde", @@ -3257,6 +3256,7 @@ dependencies = [ "tokio", "tracing", "unicode-normalization", + "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0c76128..fddcb74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "furumi_tui" -version = "0.3.5" +version = "0.4.0" edition = "2024" rust-version = "1.97" description = "A federated P2P player for personal music libraries" @@ -27,12 +27,12 @@ crokey = "1.4.0" crossterm = { version = "0.29.0", features = ["event-stream"] } directories = "6.0.0" futures-util = "0.3.32" -furumi-library = "0.2.0" +furumi-library = "0.3.0" image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] } lofty = "0.22" # P2P federation: library index in a shared DHT + audio streaming between # peers (same protocol as furumi-fd). -music-dht = "0.5.0" +music-dht = { version = "0.6.0", features = ["podcast-feed"] } ratatui = "0.30.1" reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] } rhai = { version = "1", features = ["sync"] } @@ -57,6 +57,9 @@ unicode-width = "0.2.2" # souvlaki 0.8.3 still depends on the unmaintained block 0.1.6. Keep its API # intact while using an opaque inhabited FFI type accepted by current Rust. block = { path = "vendor/block" } +# Unpublished frid (MR-F1, MR-F2); remove once music-dht 0.6.0 and furumi-library 0.3.0 are on crates.io. +music-dht = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" } +furumi-library = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" } [target.'cfg(target_os="macos")'.dependencies] core-foundation = "0.10.1" diff --git a/README.md b/README.md index fcc6902..5d52dde 100644 --- a/README.md +++ b/README.md @@ -163,9 +163,9 @@ Shift+Tab cycle through them: | `5` | Settings | | `6` | Logs | -The Podcasts tab is a placeholder in this build; podcast support is not -available yet. On narrow terminals the tab bar shortens titles and then shows -only the numbers, keeping the active tab's full name. Press `?` for the full list of bindings and commands. +On narrow terminals the tab bar shortens titles and then shows only the +numbers, keeping the active tab's full name. Press `?` for the full list of +bindings and commands. Bindings can be overridden in `keymap.toml` in the platform config directory (`~/.config/furumi/keymap.toml` on Linux, @@ -181,6 +181,112 @@ keep opening the same tabs, shows a short notice at startup, and never rewrites the file. Add `version = 2` once the indices match the new order to take them literally and silence the notice. +### Podcasts + +The Podcasts tab (`4`) follows RSS and Atom podcast feeds. Furumi stores only +the feed's episode information; audio stays on the publisher's server and is +never imported into the music library or offered to other peers. + +- **Add podcast** (`o`, or the first row) takes a feed URL; `feed://`, + `podcast://` and URLs without a scheme are accepted. Nothing is stored unless + the URL serves a feed. New subscriptions are private. +- The list shows each podcast's unplayed and total episode counts and its + check status; `!` marks a podcast whose last check failed, with the error in + the side panel (or below the list on narrow terminals). +- `Enter` opens a podcast. Podcasts with seasons show their seasons first + (plus "All episodes" and "Without a season"); `Esc` goes back. +- Episodes show the date, season/episode number, duration and listening + state: `●` not started, `◐` in progress with the saved position, `✓` played. + `/` searches titles and descriptions, `f` cycles all / unplayed / in + progress / played, `Shift+S` changes the order, `m` marks the selected + episode played or unplayed. +- `Shift+R` checks the selected podcast for new episodes, `Ctrl+R` checks all + of them. Feeds are also checked in the background about once an hour with + conditional requests; a failing feed is retried with growing pauses and its + known episodes stay available. A permanent redirect moves the subscription + to the new address. +- `Shift+U` (or `Shift+D`) unsubscribes after a confirmation. Listening + progress and history are kept, so subscribing again continues where you + left off. + +Episodes play in the same queue as music: + +- `Enter` on an episode plays it right after the current item and continues + from the saved position; a played episode starts from the beginning. `b` + ("start over") plays it from the beginning even when it is in progress. + `a` / `Shift+A` queue episodes like tracks, and the queue can mix music and + episodes. +- Audio streams from the publisher's server through a bounded buffer (about + 9 MB) and seeks with HTTP range requests. Nothing is downloaded ahead of + time and nothing is kept after playback. If the server cannot seek (no + file length), Furumi says that it cannot continue at the saved position + instead of starting from the wrong place; "start over" still works, and + seeking in such an episode is refused with a message. If the episode's + address no longer works, the feed is checked once for a new one. +- If the connection is lost during an episode (no data for 30 seconds, or + the address stops working), playback stops there: the position is saved, + the episode is not marked played and the queue does not move on; play + continues from that position. A pause pressed while an episode is still + connecting is kept when the audio arrives. +- Some servers send episodes without a file size. There a closed connection + looks like the end of the file, so Furumi compares the position with the + episode's duration: the one the audio file states, or else the + `itunes:duration` of the feed. Feed durations are often a little off, so + reaching 80 % of one is enough; an episode that stops earlier is treated as + a lost connection. Such servers do not allow seeking either, so play cannot + continue in the middle: start the episode over (`b`) or mark it played + (`m`). +- The position is saved every 20 seconds while playing and on pause, seek, + stop, track change and exit. An episode counts as played within the last + 30 seconds (or 5 %) or when its stream reaches its end; a listen enters the episode's + history when it completed or lasted at least 30 seconds. Seeking back and + "start over" are real changes: an older save never moves the position + forward again. +- Likes, playlists, music history, similarity search and share links are for + music only and skip episodes; `Shift+J` on an episode opens its podcast. + +Podcasts and your connected devices: + +- **Private by default.** Subscriptions, episode progress and listening + history are personal. They are synchronized only between your trusted + devices (Connected Devices), never published to the federation: no DHT + record, public catalog or search result contains them. Publishing a + podcast for discovery is a separate, explicit choice that this version + does not offer yet. +- **Synchronized between trusted devices.** A podcast you subscribe to on + one device appears on the others without typing its URL again; each + device then checks the feed itself. Unsubscribing, "played", the saved + position and "start over" follow the same way, also through a device + that relays them and after a device was offline for a while. When two + devices played the same episode offline, they agree on one position and + keep both listening sessions. +- **Handoff.** A queue with episodes moves to another device like music: + the episode continues there at the handed-off position, paused or + playing, even if that device is not subscribed. Devices that run an older + Furumi (device sync before v4) cannot keep episodes: handing such a queue + to them is refused with "needs a Furumi update", their remote control is + ignored while the queue has episodes, and they get no podcast data: they + see such a queue with "Private item" in place of each episode, so they + keep seeing this device as the playing one. An older device still takes + over when you choose it as the output (Play on it), when it starts while + this device is paused (`take_paused_on_startup`), or by itself once this + device has been closed, asleep or offline for about two minutes. Its queue + then has an unplayable "Private item" music entry for each episode; this + device shows that queue without them (with a status message), and the + episodes' saved positions stay. +- **Audio from the publisher.** Every device streams the episode from the + publisher's server itself. Furumi peers never pass podcast audio to each + other, and nothing is kept after playback. +- **Jam.** Guests see the host's episodes as "Private item" (no title, feed + or address), cannot add episodes to the host's queue and cannot seek in a + private item. They can still reorder the queue and skip. + +Podcast data lives in `podcasts.sqlite3` in the platform data directory (the +same file the earlier podcast test build used; it is upgraded in place). If +that file was written by a newer Furumi or is not a podcast database, the tab +explains why it is unavailable and the file is left unchanged; podcast +changes from other devices then wait in the device-sync log until it opens. + ### Now playing in tmux While Furumi is running, a second invocation can print a cheap, single-line @@ -268,11 +374,16 @@ Run the local protocol checks from the TUI repository: ```bash cargo test localhost_devices_exchange_state_and_handoff +cargo test localhost_devices_sync_podcasts +cargo test devices::podcast_sync_tests python scripts/test_device_interop.py ../furumusic ``` The first test uses real iroh streams, isolated SQLite databases, ownership -handoff, and a stalled peer. The second builds both player test binaries and +handoff, and a stalled peer. The podcast tests sync subscriptions, progress +and "start over" between TUI device stores (over iroh streams and through +the JSON messages), check a handoff with an episode and simulate a device +sync v3 peer with the exact JSON types of furumi-tui 0.3.x. The second builds both player test binaries and exchanges their actual JSON messages over `127.0.0.1`: queue metadata, bidirectional handoff, pause/seek and duplicate command fencing. It exercises the TUI command adapter and web player hub, but does not start a browser or diff --git a/src/app/action.rs b/src/app/action.rs index 229c01d..d1d4d2f 100644 --- a/src/app/action.rs +++ b/src/app/action.rs @@ -57,6 +57,24 @@ pub enum Action { OpenSearch, EditSelected, DeleteSelected, + /// Podcasts: subscribe to an RSS feed URL. + AddPodcast, + /// Podcasts: search the episodes of the opened podcast. + SearchEpisodes, + /// Podcasts: check the selected or opened podcast for new episodes. + RefreshPodcast, + /// Podcasts: check every subscription for new episodes. + RefreshAllPodcasts, + /// Podcasts: cycle the episode order. + CycleEpisodeSort, + /// Podcasts: cycle the played-state filter. + CycleEpisodeFilter, + /// Podcasts: mark the selected episode played / unplayed. + ToggleEpisodePlayed, + /// Podcasts: unsubscribe (progress and history are kept). + UnsubscribePodcast, + /// Podcasts: play the selected (or playing) episode from the beginning. + RestartEpisode, } /// Help-window sections, in display order. @@ -66,16 +84,18 @@ pub enum Category { Queue, Library, Navigation, + Podcasts, Search, System, } impl Category { - pub const ALL: [Category; 6] = [ + pub const ALL: [Category; 7] = [ Category::Playback, Category::Queue, Category::Library, Category::Navigation, + Category::Podcasts, Category::Search, Category::System, ]; @@ -86,6 +106,7 @@ impl Category { Category::Queue => "Queue & playlists", Category::Library => "Library", Category::Navigation => "Navigation", + Category::Podcasts => "Podcasts", Category::Search => "Search & commands", Category::System => "System", } @@ -139,6 +160,15 @@ impl Action { | Action::DeleteSelected | Action::CycleSourceMode | Action::OpenLibraryFilters => Category::Library, + Action::AddPodcast + | Action::SearchEpisodes + | Action::RefreshPodcast + | Action::RefreshAllPodcasts + | Action::CycleEpisodeSort + | Action::CycleEpisodeFilter + | Action::ToggleEpisodePlayed + | Action::UnsubscribePodcast + | Action::RestartEpisode => Category::Podcasts, Action::OpenSearch | Action::OpenCommandLine => Category::Search, Action::ToggleHelp | Action::Quit => Category::System, } @@ -218,6 +248,15 @@ impl Action { Action::OpenSearch => "Search artists, releases, tracks".into(), Action::EditSelected => "Edit the selected item".into(), Action::DeleteSelected => "Delete the selected item".into(), + Action::AddPodcast => "Add podcast (RSS URL)".into(), + Action::SearchEpisodes => "Search episodes".into(), + Action::RefreshPodcast => "Check podcast for new episodes".into(), + Action::RefreshAllPodcasts => "Check all podcasts".into(), + Action::CycleEpisodeSort => "Episode order".into(), + Action::CycleEpisodeFilter => "Filter: all/unplayed/played".into(), + Action::ToggleEpisodePlayed => "Mark played / unplayed".into(), + Action::UnsubscribePodcast => "Unsubscribe (keeps progress)".into(), + Action::RestartEpisode => "Play episode from the start".into(), } } } diff --git a/src/app/event.rs b/src/app/event.rs index 54feb60..89a9840 100644 --- a/src/app/event.rs +++ b/src/app/event.rs @@ -130,7 +130,8 @@ pub enum AppEvent { tracks: Vec, }, /// A status snapshot for the Federation tab. - FederationStatus(crate::federation::FedStatus), + /// Boxed to keep `AppEvent` small; the status is large and rare. + FederationStatus(Box), /// Federated live-search results (artists a card can be opened for, /// plus matching tracks). FedSearchLoaded { @@ -172,7 +173,8 @@ pub enum AppEvent { FedTrackInfoLoaded { placeholder_id: i64, item_id: String, - result: Result, + /// Boxed: `TrackItem` is large since it can carry a podcast episode. + result: Result, String>, }, /// This peer's connection ticket, requested from the Federation tab. FedTicket(Result), @@ -206,4 +208,64 @@ pub enum AppEvent { JamInvite(Result), /// Result of joining a Jam capability. JamJoined(Result), + /// Received podcast operations or snapshot sections changed the podcast + /// store: `progress` lists changed progress registers, `changed` means + /// anything else (subscriptions, feeds, sessions). + PodcastsSynced { + progress: Vec, + changed: bool, + }, + /// The podcast store opened (with the feed client), or why it did not. + PodcastStoreOpened(Result), + /// Subscribed podcasts reloaded from the podcast store. + PodcastsLoaded(Result, String>), + PodcastSeasonsLoaded { + podcast_id: music_dht::podcast::PodcastId, + result: Result, String>, + }, + /// One page of episodes; `seq` drops results of older requests. + PodcastEpisodesLoaded { + seq: u64, + offset: usize, + result: Result, String>, + }, + PodcastSubscribed(Result), + /// One feed check finished. + PodcastRefreshed { + podcast_id: music_dht::podcast::PodcastId, + result: Result, + /// Asked by the user (status bar), not the background scheduler. + manual: bool, + }, + PodcastUnsubscribed { + podcast_id: music_dht::podcast::PodcastId, + title: String, + result: Result<(), String>, + }, + /// Result of the progress writer (saves, supersedes, mark played). + PodcastProgress(crate::podcasts::progress::ProgressEvent), + /// An episode stream is playing; `generation` is the player source + /// generation of the start that requested it. + PodcastEpisodeStarted { + generation: u64, + episode: Box, + start_ms: i64, + duration_ms: Option, + /// The decoder measured `duration_ms` (else the feed announced it). + duration_measured: bool, + restart: bool, + /// The server allows seeking in the stream. + seekable: bool, + /// How the stream ended, once it does. + probe: crate::podcasts::stream::StreamProbe, + }, + /// Starting an episode failed (network, decoding, cannot resume). + PodcastEpisodeFailed { + generation: u64, + message: String, + }, + /// Scheduler timer: ask the store which feeds are due. + PodcastRefreshTick, + /// Feeds due for a background refresh. + PodcastsDue(Result, String>), } diff --git a/src/app/mod.rs b/src/app/mod.rs index 8c90611..23071bb 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -4,6 +4,9 @@ pub mod command; mod enrichment; pub mod event; pub mod input; +#[cfg(test)] +pub(crate) mod podcast_fixtures; +pub(crate) mod podcast_runtime; pub(crate) mod popup; pub mod state; pub mod update; @@ -24,8 +27,10 @@ use tokio::sync::mpsc; use tokio::time::MissedTickBehavior; use crate::config::keymap::{KeyResolution, Keymap}; +use crate::devices::PlaybackTrackExt as _; use crate::library::Library; use crate::player; +use crate::podcasts::playback::StartAt; use crate::ui; use event::AppEvent; use state::AppState; @@ -77,6 +82,11 @@ pub struct Runtime { pub last_media_push: Option, pub status_publisher: crate::status::Publisher, pub status_visualizer: crate::visualizer::status::Renderer, + /// Podcast store and feed client; `None` until opened (or if opening + /// failed, which only disables the Podcasts tab). + pub podcasts: Option, + /// The podcast episode this device plays, for progress saving. + pub podcast_playback: Option, } #[derive(Debug, Clone, Copy)] @@ -386,7 +396,9 @@ fn spawn_content_id_backfill(runtime: &Runtime) { if let Err(err) = federation.sync_now().await { tracing::warn!("federation sync after content id backfill failed: {err:#}"); } - let _ = tx.send(AppEvent::FederationStatus(federation.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new( + federation.status().await, + ))); let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status())); } }); @@ -482,8 +494,11 @@ pub async fn run( last_media_push: None, status_publisher: crate::status::Publisher::spawn(), status_visualizer: crate::visualizer::status::Renderer::default(), + podcasts: None, + podcast_playback: None, }; spawn_content_id_backfill(&runtime); + podcast_runtime::spawn_open(&runtime); if state.similarity.settings.enabled { runtime.similarity.start(); } @@ -495,7 +510,7 @@ pub async fn run( tokio::spawn(async move { fed.start_if_enabled().await; let status = fed.status().await; - let _ = tx.send(AppEvent::FederationStatus(status)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(status))); let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status())); }); } @@ -547,6 +562,7 @@ pub async fn run( state.advance_spinner(); expire_quit_confirmation(&mut state); sync_player_shared(&mut state, &runtime); + podcast_runtime::tick(&state, &mut runtime); runtime .status_publisher .publish(crate::status::PlaybackStatus::from_player(&state.player)); @@ -585,6 +601,7 @@ pub async fn run( } state.shutting_down = true; terminal.draw(|frame| ui::draw(frame, &state, &keymap))?; + podcast_runtime::shutdown(&mut runtime).await; runtime.federation.shutdown().await; return Ok(()); } @@ -691,10 +708,12 @@ fn playback_state_from_ui(state: &AppState) -> crate::devices::PlaybackStateWire volume: state.player.volume, shuffle: state.player.shuffle, repeat: playback_repeat_to_wire(state.player.repeat), + playback_rate: None, } } -fn playback_track_to_ui( +/// A received music item, resolved to the library copy when there is one. +fn music_track_to_ui( wire: &crate::devices::PlaybackTrack, library: Option<&Library>, ) -> crate::library::models::TrackItem { @@ -715,11 +734,11 @@ fn apply_playback_state_to_ui( wire: &crate::devices::PlaybackStateWire, library: Option<&Library>, ) { - let queue: Vec<_> = wire - .queue - .iter() - .map(|track| playback_track_to_ui(track, library)) - .collect(); + // Podcast episodes keep their explicit source; Jam private placeholders + // stay placeholders (numbered, so each has its own queue key). + let queue = crate::devices::track_items_from_wire(&wire.queue, |track| { + music_track_to_ui(track, library) + }); let queue_pos = queue .iter() .take(wire.queue_pos) @@ -844,9 +863,10 @@ fn extrapolate_control_position(state: &mut AppState) { pub(crate) fn become_control_device( state: &mut AppState, - runtime: &Runtime, + runtime: &mut Runtime, snapshot: crate::devices::PlaybackSnapshot, ) { + podcast_runtime::end_playback(runtime); runtime.player.set_playback_allowed(false); if state.device_playback.is_audio_owner() { runtime.player.stop(); @@ -880,12 +900,7 @@ pub(crate) fn become_active_device(state: &mut AppState, runtime: &mut Runtime, state.device_playback.last_remote_snapshot = None; state.device_playback.local_idle_since_ms = None; if was_control && start_audio && state.player.playing { - start_current_audio( - state, - runtime, - state.player.position_secs, - state.player.paused, - ); + start_current_audio_from(state, runtime, remote_start(state), state.player.paused); } publish_playback_snapshot(state, runtime); } @@ -916,7 +931,7 @@ pub(crate) fn transfer_active_to_this_device(state: &mut AppState, runtime: &mut } become_active_device(state, runtime, false); if should_start && state.player.current.is_some() { - start_current_audio(state, runtime, state.player.position_secs, false); + start_current_audio_from(state, runtime, remote_start(state), false); push_media_metadata(state, runtime); push_media_update(state, runtime, true); } @@ -937,6 +952,15 @@ pub(crate) fn transfer_active_to_remote_device( transfer_active_to_this_device(state, runtime); return; } + // A device below sync v4 could neither play nor keep podcast episodes: + // refuse before ownership changes (never strip them from the queue). + if let Err(error) = runtime + .devices + .ensure_playback_supported_by(&target_device_id, &playback_state_from_ui(state)) + { + state.status_message = Some(format!("device handoff: {error}")); + return; + } if let Err(error) = runtime.devices.claim_playback(&target_device_id) { state.status_message = Some(format!("device handoff: {error}")); return; @@ -1314,6 +1338,9 @@ fn maintenance(state: &mut AppState, runtime: &mut Runtime) { } } } + if let Some(path) = podcast_runtime::wanted_cover(state) { + wanted.push((path, header.0, header.1)); + } for (path, width, height) in wanted { let key = crate::art::cache_key(&path, width, height); if state.art.contains_key(&key) { @@ -1563,6 +1590,9 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { perform_control_playback_effect(state, runtime, effect); return; } + let Some(effect) = podcast_runtime::perform(state, runtime, effect) else { + return; + }; match effect { Effect::CheckUpdate => { let tx = runtime.event_tx.clone(); @@ -1594,6 +1624,11 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { push_media_metadata(state, runtime); push_media_update(state, runtime, true); } + Effect::PlayCurrentFromStart => { + start_current_audio_from(state, runtime, StartAt::Restart, false); + push_media_metadata(state, runtime); + push_media_update(state, runtime, true); + } Effect::TogglePause => { if state.player.paused { runtime.player.pause(); @@ -1603,17 +1638,23 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { push_media_update(state, runtime, true); } Effect::StopPlayback => { + podcast_runtime::end_playback(runtime); runtime.player_start_pending = false; runtime.player.stop(); push_media_update(state, runtime, true); } - Effect::SeekBy(delta) => { - let target = (state.player.position_secs + delta as f64).max(0.0); - state.player.position_secs = target; - runtime - .player - .seek(std::time::Duration::from_secs_f64(target)); - } + Effect::SeekBy(delta) => match podcast_runtime::seek_refusal(state, runtime) { + Some(reason) => state.status_message = Some(reason), + None => { + let target = (state.player.position_secs + delta as f64).max(0.0); + state.player.position_secs = target; + // An episode's new position is saved once the audio thread + // reports that the seek worked (`PlayerEvent::Seeked`). + runtime + .player + .seek(std::time::Duration::from_secs_f64(target)); + } + }, Effect::SetVolume(volume) => { runtime.player.set_volume(player::amplitude(volume)); save_app_settings(state); @@ -1686,6 +1727,14 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { } }); } + // Executed by podcast_runtime::perform above. + Effect::PodcastsReload + | Effect::PodcastLoadSeasons(_) + | Effect::PodcastLoadEpisodes { .. } + | Effect::PodcastSubscribe(_) + | Effect::PodcastRefresh { .. } + | Effect::PodcastUnsubscribe { .. } + | Effect::PodcastSetPlayed { .. } => {} Effect::LoadListenHistory => { let library = Arc::clone(&runtime.library); let devices = Arc::clone(&runtime.devices); @@ -1819,7 +1868,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { Ok(()) => "federation: library published".to_string(), Err(err) => format!("federation sync failed: {err:#}"), }; - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); let _ = tx.send(AppEvent::FedSyncFinished(message)); }); } @@ -1879,7 +1928,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { tokio::spawn(async move { let result = fed.device_invite().await.map_err(|err| format!("{err:#}")); let _ = tx.send(AppEvent::DeviceInvite(result)); - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status())); }); } @@ -2002,6 +2051,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { let result = federation .track_info(preview) .await + .map(Box::new) .map_err(|err| format!("{err:#}")); let _ = tx.send(AppEvent::FedTrackInfoLoaded { placeholder_id, @@ -2042,7 +2092,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { runtime.player.stop(); push_media_update(state, runtime, true); } else if let Some(paused) = restart_paused { - start_current_audio(state, runtime, 0.0, paused); + start_current_audio_from(state, runtime, StartAt::Resume, paused); push_media_metadata(state, runtime); push_media_update(state, runtime, true); } else { @@ -2056,6 +2106,7 @@ fn is_controlled_playback_effect(effect: &Effect) -> bool { matches!( effect, Effect::PlayCurrent + | Effect::PlayCurrentFromStart | Effect::TogglePause | Effect::StopPlayback | Effect::SeekBy(_) @@ -2072,7 +2123,7 @@ fn perform_control_playback_effect(state: &mut AppState, runtime: &mut Runtime, let local_only_volume = state.device_playback.role == state::DevicePlaybackRole::Jam && matches!(effect, Effect::SetVolume(_)); match effect { - Effect::PlayCurrent => { + Effect::PlayCurrent | Effect::PlayCurrentFromStart => { state.player.current = state.player.queue.get(state.player.queue_pos).cloned(); state.player.playing = state.player.current.is_some(); state.player.paused = false; @@ -2160,21 +2211,62 @@ fn shell_quote(path: &Path) -> String { } /// Start playing `queue[queue_pos]`: open the local file in a background -/// task and hand the reader to the audio thread. +/// task and hand the reader to the audio thread. A podcast episode resumes +/// from its saved position. fn play_current(state: &mut AppState, runtime: &mut Runtime) { - start_current_audio(state, runtime, 0.0, false); + start_current_audio_from(state, runtime, StartAt::Resume, false); } +/// Start `queue[queue_pos]` at an exact position (handoff, restarting the +/// same item after a queue change). fn start_current_audio( state: &mut AppState, runtime: &mut Runtime, position_secs: f64, paused: bool, ) { + let ms = (position_secs.max(0.0) * 1_000.0).round() as i64; + start_current_audio_from(state, runtime, StartAt::Position { ms }, paused); +} + +/// Where the current item starts when another device decided it (a +/// handoff, a command of a controlling device, taking playback over here). +/// +/// A podcast episode at position 0 resumes from its saved position: a +/// controlling device sends 0 for an ordinary Play, and the saved position +/// is at least as fresh. Any other position is the live position of the +/// handoff or seek. +fn remote_start(state: &AppState) -> StartAt { + let ms = (state.player.position_secs.max(0.0) * 1_000.0).round() as i64; + let episode = state + .player + .queue + .get(state.player.queue_pos) + .is_some_and(crate::library::models::TrackItem::is_podcast); + if episode && ms == 0 { + StartAt::Resume + } else { + StartAt::Position { ms } + } +} + +fn start_current_audio_from( + state: &mut AppState, + runtime: &mut Runtime, + start: StartAt, + paused: bool, +) { + let position_secs = match start { + StartAt::Position { ms } => ms as f64 / 1_000.0, + StartAt::Resume | StartAt::Restart => 0.0, + }; let Some(mut track) = state.player.queue.get(state.player.queue_pos).cloned() else { return; }; - if track_file_missing(&track) { + // The episode that was playing ends here (its last seen position is + // saved), whatever plays next. + podcast_runtime::end_playback(runtime); + if !track.is_podcast() && track_file_missing(&track) { if let Some(local) = local_track_for_playback(runtime, &track) { state.player.queue[state.player.queue_pos] = local.clone(); track = local; @@ -2253,6 +2345,12 @@ fn start_current_audio( state.status_message = Some(format!("▶ {} — {}", track.title, track.artist_line())); runtime.player_start_pending = true; + if track.is_podcast() { + // Streamed from the publisher; the start position (resume, exact or + // start over) is resolved with the store in the background. + podcast_runtime::start_episode(state, runtime, &track, start, paused); + return; + } if track.is_fed_pending() { // A federated track that is not on disk yet: silence the previous // audio, download it and resume through FedTrackResolved. @@ -2355,6 +2453,11 @@ fn maybe_prefetch_next(state: &mut AppState, runtime: &Runtime) { let Some(track) = &player.current else { return; }; + // An episode ends through its own completion path, and the next item + // must not start behind a stream; episodes are never prefetched either. + if track.is_podcast() { + return; + } if track.duration_seconds <= 0.0 || track.duration_seconds - player.position_secs > PREFETCH_MARGIN_SECS { @@ -2366,6 +2469,9 @@ fn maybe_prefetch_next(state: &mut AppState, runtime: &Runtime) { let Some(mut next) = player.queue.get(next_pos).cloned() else { return; }; + if next.is_podcast() { + return; + } if track_file_missing(&next) { if let Some(local) = local_track_for_playback(runtime, &next) { state.player.queue[next_pos] = local.clone(); @@ -2409,6 +2515,11 @@ fn report_history( listened_ms: i64, ended_reason: music_dht::device_sync::ListenEndReason, ) { + // Podcast episodes are not music history (no content id); their + // listening sessions go to the podcast store (`podcast_runtime`). + if track.is_podcast() { + return; + } let Some(listen_id) = listen_id else { return; }; @@ -2439,7 +2550,7 @@ pub(crate) fn fed_apply_settings(state: &mut AppState, runtime: &Runtime) { if let Err(err) = fed.apply_settings(settings).await { let _ = tx.send(AppEvent::StatusMessage(format!("federation: {err:#}"))); } - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); }); } @@ -2452,7 +2563,7 @@ pub(crate) fn fed_connect(runtime: &Runtime, ticket: String) { Ok(peer) => format!("federation: connected to {}…", &peer[..peer.len().min(10)]), Err(err) => format!("federation: {err:#}"), }; - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); let _ = tx.send(AppEvent::StatusMessage(message)); }); } @@ -2470,7 +2581,7 @@ pub(crate) fn device_connect(runtime: &Runtime, invite: String) { .device_connect(&invite) .await .map_err(|err| format!("{err:#}")); - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status())); let _ = tx.send(AppEvent::DeviceConnectResult(result)); }); @@ -2791,7 +2902,7 @@ fn fed_spawn_status(runtime: &Runtime) { let devices = Arc::clone(&runtime.devices); let tx = runtime.event_tx.clone(); tokio::spawn(async move { - let _ = tx.send(AppEvent::FederationStatus(fed.status().await)); + let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await))); let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status())); }); } @@ -2832,7 +2943,7 @@ fn handle_terminal_event( } TermEvent::Paste(pasted) => { if state.popup.is_some() { - popup::handle_paste(state, &pasted); + popup::handle_paste(state, runtime, &pasted); } else if state.cmdline.active { cmdline::handle_paste(state, runtime, &pasted); } @@ -3043,6 +3154,12 @@ fn reset_artist_pagination(state: &mut AppState) { global.reloading = false; } +/// A row of the local music library (not a podcast episode, not a pending +/// federated placeholder). +fn is_library_row(track: &crate::library::models::TrackItem) -> bool { + !track.is_podcast() && track.id >= 0 +} + /// Preserve the queue position while refreshing library-backed entries. /// /// A playback key identifies a track, not a queue occurrence: the same track @@ -3056,7 +3173,7 @@ fn queue_position_after_refresh( let removed_before_current = queue .iter() .take(queue_pos) - .filter(|track| track.id >= 0 && !refreshed_ids.contains(&track.id)) + .filter(|track| is_library_row(track) && !refreshed_ids.contains(&track.id)) .count(); queue_pos.saturating_sub(removed_before_current) } @@ -3078,9 +3195,22 @@ fn apply_queue_refresh( .cloned() .map(|track| (track_playback_key(&track), track)) .collect(); - let current_id = state.player.current.as_ref().map(|track| track.id); + // A current podcast episode (sentinel id) is never a deleted library + // row; it takes the "not a library row" branch below. + let current_id = state.player.current.as_ref().map(|track| { + if track.is_podcast() { + crate::library::models::TrackItem::NOT_A_LIBRARY_ROW + } else { + track.id + } + }); let current_key = state.player.current.as_ref().map(track_playback_key); for track in &mut state.player.queue { + // Podcast episodes are not library rows: their sentinel id matches + // nothing and they are never refreshed from the library. + if track.is_podcast() { + continue; + } if let Some(fresh) = by_id.get(&track.id) { *track = fresh.clone(); } else if let Some(fresh) = by_key.get(&track_playback_key(track)) { @@ -3091,12 +3221,12 @@ fn apply_queue_refresh( .player .queue .iter() - .any(|track| track.id >= 0 && !by_id.contains_key(&track.id)); + .any(|track| is_library_row(track) && !by_id.contains_key(&track.id)); if had_missing { state .player .queue - .retain(|track| track.id < 0 || by_id.contains_key(&track.id)); + .retain(|track| !is_library_row(track) || by_id.contains_key(&track.id)); state.player.prefetched_pos = None; } if state.player.queue.is_empty() { @@ -3229,6 +3359,8 @@ fn handle_playback_command( ) { match command { crate::devices::PlaybackCommand::SetState { state: wire, seek } => { + // Commands from devices below sync v4 never reach this point + // while the queue has podcast items (`DeviceSync`). let old_current_key = state.player.current.as_ref().map(track_playback_key); let old_playing = state.player.playing; let old_paused = state.player.paused; @@ -3246,18 +3378,19 @@ fn handle_playback_command( } let current_key = state.player.current.as_ref().map(track_playback_key); if !old_playing || old_current_key != current_key { - start_current_audio( - state, - runtime, - state.player.position_secs, - state.player.paused, - ); + start_current_audio_from(state, runtime, remote_start(state), state.player.paused); push_media_metadata(state, runtime); } else { if seek { - runtime.player.seek(std::time::Duration::from_secs_f64( - state.player.position_secs, - )); + match podcast_runtime::seek_refusal(state, runtime) { + Some(reason) => state.status_message = Some(reason), + // A seek from a controlling device is the user's + // explicit action too (podcast progress + // generation), saved once the seek worked. + None => runtime.player.seek(std::time::Duration::from_secs_f64( + state.player.position_secs, + )), + } } if state.player.paused && !old_paused { runtime.player.pause(); @@ -3304,8 +3437,101 @@ fn handle_playback_command( } } +/// Shown when a Jam guest's change would drop, add or reveal the host's +/// private items. +const JAM_PRIVATE_ITEMS_KEPT: &str = + "Jam: ignored a guest's change that would remove or add podcast episodes"; + +/// A Jam guest's queue state mapped back onto this host's queue. +/// +/// Guests see the host's podcast episodes as private placeholders +/// (`redact_for_jam`). Their placeholders become the host's episodes again +/// (`restore_jam_private_items`); a state that drops, duplicates or adds +/// private items is refused (`None`). Guests cannot seek in a private item: +/// while it stays current, the host's position is kept. The host keeps its +/// own volume. +fn jam_participant_state( + state: &AppState, + wire: crate::devices::PlaybackStateWire, + seek: bool, +) -> Option<(crate::devices::PlaybackStateWire, bool)> { + let host = playback_state_from_ui(state); + if !music_dht::device_sync::jam_command_allowed(&crate::devices::PlaybackCommand::SetState { + state: wire.clone(), + seek, + }) { + return None; + } + let mut restored = music_dht::device_sync::restore_jam_private_items(&host, wire)?; + restored.volume = state.player.volume; + let private_current = restored + .queue + .get(restored.queue_pos) + .filter(|track| track.requires_podcast_support()); + let same_current = private_current.is_some() + && restored.queue.get(restored.queue_pos) == host.queue.get(host.queue_pos); + if same_current { + restored.position_secs = host.position_secs; + return Some((restored, false)); + } + Some((restored, seek)) +} + +/// Removes Jam private placeholders from the queue (a guest leaving). +fn drop_private_items(state: &mut AppState) { + let player = &mut state.player; + let before = player + .queue + .iter() + .take(player.queue_pos) + .filter(|track| crate::devices::is_private_track_item(track)) + .count(); + let current_private = player + .queue + .get(player.queue_pos) + .is_some_and(crate::devices::is_private_track_item); + player + .queue + .retain(|track| !crate::devices::is_private_track_item(track)); + player.queue_pos = player + .queue_pos + .saturating_sub(before) + .min(player.queue.len().saturating_sub(1)); + player.play_next_end = None; + player.original_order = None; + player.prefetched_pos = None; + if current_private + || player + .current + .as_ref() + .is_some_and(crate::devices::is_private_track_item) + { + player.current = None; + player.playing = false; + player.paused = false; + player.position_secs = 0.0; + } +} + fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent) { + let Some(event) = podcast_runtime::handle_event(state, runtime, event) else { + return; + }; match event { + // Folded in by podcast_runtime::handle_event above. + AppEvent::PodcastStoreOpened(_) + | AppEvent::PodcastsSynced { .. } + | AppEvent::PodcastsLoaded(_) + | AppEvent::PodcastSeasonsLoaded { .. } + | AppEvent::PodcastEpisodesLoaded { .. } + | AppEvent::PodcastSubscribed(_) + | AppEvent::PodcastRefreshed { .. } + | AppEvent::PodcastUnsubscribed { .. } + | AppEvent::PodcastProgress(_) + | AppEvent::PodcastEpisodeStarted { .. } + | AppEvent::PodcastEpisodeFailed { .. } + | AppEvent::PodcastRefreshTick + | AppEvent::PodcastsDue(_) => {} AppEvent::StatusVisualizationReady(ready) => { runtime.status_visualizer.busy = false; state.status_visualizer.apply(ready); @@ -3391,7 +3617,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent } } AppEvent::FederationStatus(status) => { - state.federation.status = Some(status); + state.federation.status = Some(*status); } AppEvent::DeviceSyncStatus(status) => { state.federation.devices = Some(status); @@ -3488,6 +3714,11 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent } crate::jam::JamRole::None => { if state.device_playback.role == state::DevicePlaybackRole::Jam { + if !state.device_playback.jam_host { + // The host's private placeholders are not this + // device's queue items. + drop_private_items(state); + } state.device_playback.role = state::DevicePlaybackRole::Active; state.device_playback.jam_host = false; } @@ -3507,12 +3738,16 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent } AppEvent::JamCommand(command) => { let command = match command { - crate::devices::PlaybackCommand::SetState { - state: mut wire, - seek, - } => { - wire.volume = state.player.volume; - crate::devices::PlaybackCommand::SetState { state: wire, seek } + crate::devices::PlaybackCommand::SetState { state: wire, seek } => { + match jam_participant_state(state, wire, seek) { + Some((wire, seek)) => { + crate::devices::PlaybackCommand::SetState { state: wire, seek } + } + None => { + state.status_message = Some(JAM_PRIVATE_ITEMS_KEPT.into()); + return; + } + } } crate::devices::PlaybackCommand::ActiveChanged { .. } => { state.status_message = @@ -3754,7 +3989,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent && track.fed.as_ref().is_some_and(|fed| fed.item_id == item_id) }) { - *slot = enriched; + *slot = *enriched; } } Err(message) => { @@ -4003,6 +4238,15 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent }; state.art.insert(key, entry); } + AppEvent::Player(player::PlayerEvent::Seeked { + generation, + position, + result, + }) => { + if !state.device_playback.is_control() { + podcast_runtime::seek_done(state, runtime, generation, position, result); + } + } AppEvent::Player(event) if state.device_playback.is_control() => { runtime.player_start_pending = false; // A background decoder may finish after ownership was handed off. @@ -4017,6 +4261,15 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent runtime.player_start_pending = false; } AppEvent::Player(player::PlayerEvent::TrackFinished { has_next }) => { + // An episode that played to the end is complete; one whose + // stream broke off stops where it was, without moving on. + if let podcast_runtime::Finished::Interrupted(message) = + podcast_runtime::finished(state, runtime) + { + state.status_message = Some(message); + push_media_update(state, runtime, true); + return; + } // The finished track gets a full-duration, completed entry. if let Some(finished) = state.player.current.clone() { report_history( @@ -4053,6 +4306,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent } } AppEvent::Player(player::PlayerEvent::Failed(message)) => { + podcast_runtime::player_failed(runtime); runtime.player_start_pending = false; tracing::error!(%message, "playback failed"); state.player.playing = false; @@ -4361,6 +4615,7 @@ mod similarity_search_tests { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }, score, embedding_signature: [signature; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES], @@ -4438,6 +4693,7 @@ mod queue_refresh_tests { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, } } @@ -4524,3 +4780,193 @@ mod status_message_tests { assert_eq!(join_status_messages(None, None), None); } } + +#[cfg(test)] +mod playback_bridge_tests { + use super::*; + use crate::app::podcast_fixtures::episode_track; + use crate::devices::{PlaybackStateWire, PlaybackTrack, PlaybackTrackExt}; + use crate::library::models::TrackItem; + use music_dht::device_sync::PlaybackSource; + + fn music(id: i64) -> TrackItem { + TrackItem { + id, + title: format!("song {id}"), + track_number: None, + disc_number: None, + duration_seconds: 200.0, + artists: vec![], + featured_artists: vec![], + release_id: 1, + release_title: "release".into(), + release_year: None, + cover_path: None, + file_path: format!("/music/{id}"), + content_id: Some(format!("b3:{id:064x}")), + audio_format: None, + audio_bitrate: None, + audio_sample_rate: None, + audio_bit_depth: None, + file_size_bytes: None, + play_count: 0, + fed: None, + podcast: None, + } + } + + fn mixed_state() -> AppState { + let mut state = AppState::default(); + state.player.queue = vec![music(1), episode_track(1), music(2), episode_track(2)]; + state.player.queue_pos = 1; + state.player.current = Some(episode_track(1)); + state.player.playing = true; + state.player.paused = true; + state.player.position_secs = 754.5; + state + } + + fn keys(queue: &[TrackItem]) -> Vec { + queue.iter().map(state::track_key).collect() + } + + #[test] + fn a_mixed_queue_crosses_the_wire_with_its_episodes_position_and_pause() { + let sender = mixed_state(); + let wire = playback_state_from_ui(&sender); + assert_eq!(wire.playback_rate, None); + assert!(matches!( + wire.queue[1].source, + Some(PlaybackSource::PodcastEpisode(_)) + )); + assert_eq!(wire.queue[0].source, None, "music stays untyped (v3 shape)"); + let json = serde_json::to_string(&wire).unwrap(); + let received: PlaybackStateWire = serde_json::from_str(&json).unwrap(); + + let mut receiver = AppState::default(); + // Even a local-only source mode never drops episodes. + receiver.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Local; + apply_playback_state_to_ui(&mut receiver, &received, None); + assert_eq!(keys(&receiver.player.queue), keys(&sender.player.queue)); + assert_eq!(receiver.player.queue_pos, 1); + assert_eq!( + receiver.player.current.as_ref().map(state::track_key), + Some(state::track_key(&episode_track(1))) + ); + assert!(receiver.player.paused); + assert_eq!(receiver.player.position_secs, 754.5); + assert_eq!( + receiver.player.queue[1].podcast_episode_ref(), + episode_track(1).podcast_episode_ref() + ); + // Taken over there, the episode continues at the handed-off + // position; an episode handed over at 0 resumes its saved one. + assert_eq!(remote_start(&receiver), StartAt::Position { ms: 754_500 }); + receiver.player.position_secs = 0.0; + assert_eq!(remote_start(&receiver), StartAt::Resume); + receiver.player.queue_pos = 0; + assert_eq!(remote_start(&receiver), StartAt::Position { ms: 0 }); + } + + #[test] + fn jam_guests_see_placeholders_and_their_commands_keep_the_hosts_episodes() { + let host = mixed_state(); + let published = music_dht::device_sync::redact_for_jam(playback_state_from_ui(&host)); + let json = serde_json::to_string(&published).unwrap(); + for secret in ["Episode 1", "Episode 2", "example.test", "Show"] { + assert!(!json.contains(secret), "{secret} leaked to Jam: {json}"); + } + + // The guest shows (and sends back) numbered private items. + let mut guest = AppState::default(); + apply_playback_state_to_ui(&mut guest, &published, None); + assert!(crate::devices::is_private_track_item( + &guest.player.queue[1] + )); + assert_ne!( + state::track_key(&guest.player.queue[1]), + state::track_key(&guest.player.queue[3]) + ); + // It moves a placeholder and skips to the last item. + guest.player.queue.swap(2, 3); + guest.player.queue_pos = 3; + let command = playback_state_from_ui(&guest); + assert!(command.queue[2].is_private_placeholder()); + let (restored, _) = jam_participant_state(&host, command.clone(), true).unwrap(); + assert_eq!( + restored.queue[2].podcast_episode_ref(), + episode_track(2).podcast_episode_ref() + ); + assert_eq!( + restored.queue[3].content_id.as_deref(), + music(2).content_id.as_deref() + ); + + // Dropping, adding or duplicating private items is refused. + let mut dropped = command.clone(); + dropped.queue.remove(1); + assert!(jam_participant_state(&host, dropped, false).is_none()); + let mut injected = command.clone(); + injected + .queue + .push(PlaybackTrack::from_track(&episode_track(9))); + assert!(jam_participant_state(&host, injected, false).is_none()); + let mut duplicated = command; + duplicated.queue.push(PlaybackTrack::private_placeholder()); + assert!(jam_participant_state(&host, duplicated, false).is_none()); + + // Guests cannot seek in the host's private episode. + let mut seek = published.clone(); + seek.position_secs = 3.0; + let (restored, seek) = jam_participant_state(&host, seek, true).unwrap(); + assert!(!seek); + assert_eq!(restored.position_secs, host.player.position_secs); + } + + #[test] + fn an_older_jam_guest_cannot_drop_the_hosts_episodes() { + // A furumi 0.3 guest reads the redacted queue with its own types, + // which drop the `source` of every item + // (`devices::wire_compat_tests::v3_peers_would_drop_the_source_of_podcast_items`): + // the state it sends back has bare music items where the host's + // episodes are. + let host = mixed_state(); + let published = music_dht::device_sync::redact_for_jam(playback_state_from_ui(&host)); + let mut echoed = serde_json::to_value(&published).unwrap(); + for item in echoed["queue"].as_array_mut().unwrap() { + item.as_object_mut().unwrap().remove("source"); + } + echoed["paused"] = false.into(); + let echoed: PlaybackStateWire = serde_json::from_value(echoed).unwrap(); + assert_eq!(echoed.queue.len(), host.player.queue.len()); + assert!( + echoed + .queue + .iter() + .all(|track| !track.requires_podcast_support()) + ); + let json = serde_json::to_string(&echoed).unwrap(); + for secret in ["Episode 1", "Episode 2", "example.test", "Show"] { + assert!(!json.contains(secret), "{secret} reached the old guest"); + } + + // Its commands are refused while the queue holds episodes: the host + // keeps them instead of playing "Private item" as music. + assert!(jam_participant_state(&host, echoed.clone(), false).is_none()); + assert!(jam_participant_state(&host, echoed, true).is_none()); + } + + #[test] + fn a_guest_leaving_a_jam_drops_the_hosts_placeholders() { + let mut guest = AppState::default(); + let published = + music_dht::device_sync::redact_for_jam(playback_state_from_ui(&mixed_state())); + apply_playback_state_to_ui(&mut guest, &published, None); + guest.player.queue_pos = 2; + guest.player.current = guest.player.queue.get(2).cloned(); + drop_private_items(&mut guest); + assert_eq!(keys(&guest.player.queue), keys(&[music(1), music(2)])); + assert_eq!(guest.player.queue_pos, 1); + assert!(guest.player.current.is_some()); + } +} diff --git a/src/app/podcast_fixtures.rs b/src/app/podcast_fixtures.rs new file mode 100644 index 0000000..a60078e --- /dev/null +++ b/src/app/podcast_fixtures.rs @@ -0,0 +1,138 @@ +//! Podcast state fixtures shared by update and rendering tests. + +use furumi_library::podcasts::{ + EpisodeListItem, EpisodeQuery, FetchState, PodcastSummary, SeasonSummary, +}; +use music_dht::podcast::{ + EpisodeId, EpisodeMeta, EpisodeType, FeedUrl, PodcastId, PodcastMeta, ProgressRecord, + ProgressStatus, +}; + +use crate::app::state::{ + AppState, EpisodeList, Loadable, OpenedPodcast, PodcastRow, PodcastStoreStatus, Tab, +}; + +pub fn podcast_id() -> PodcastId { + PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/feed.xml").unwrap()) +} + +pub fn row(title: &str, unplayed: u64, episodes: u64, seasons: u64) -> PodcastRow { + PodcastRow { + summary: PodcastSummary { + podcast_id: podcast_id(), + meta: PodcastMeta { + feed_url: FeedUrl::parse("https://example.test/feed.xml").unwrap(), + title: title.into(), + author: Some("Host".into()), + description: Some("A show about tests.".into()), + link: None, + image_url: None, + }, + subscribed: true, + shared: false, + episode_count: episodes, + unplayed_count: unplayed, + in_progress_count: 0, + latest_published_at_ms: None, + season_count: seasons, + }, + fetch: FetchState::default(), + } +} + +pub fn episode(n: u32, title: &str, status: Option<(ProgressStatus, i64)>) -> EpisodeListItem { + let podcast = podcast_id(); + let episode_id = EpisodeId::from_guid(&podcast, &format!("ep-{n}")).unwrap(); + EpisodeListItem { + episode: EpisodeMeta { + episode_id: episode_id.clone(), + podcast_id: podcast.clone(), + guid: Some(format!("ep-{n}")), + title: title.into(), + description: Some(format!("Notes for {title}.")), + season: Some(1), + episode_number: Some(n), + episode_type: EpisodeType::Full, + published_at_ms: Some(1_718_409_600_000), + duration_ms: Some(2_700_000), + audio_url: format!("https://example.test/{n}.mp3"), + mime: Some("audio/mpeg".into()), + byte_len: None, + image_url: None, + link: None, + }, + progress: status.map(|(status, position_ms)| ProgressRecord { + episode_id, + podcast_id: podcast, + position_ms, + status, + generation: 1, + session_id: "s".into(), + hlc_ms: 1, + device_id: "dev".into(), + op_id: "dev:1".into(), + }), + completed_count: 0, + in_feed: true, + } +} + +pub fn ready_state(rows: Vec) -> AppState { + let mut state = AppState { + active_tab: Tab::Podcasts, + ..AppState::default() + }; + state.podcasts.store = PodcastStoreStatus::Ready; + state.podcasts.list = Some(Loadable::Ready(rows)); + state +} + +pub fn with_episodes(mut state: AppState, items: Vec) -> AppState { + let season = furumi_library::podcasts::SeasonFilter::Any; + state.podcasts.opened = Some(OpenedPodcast { + podcast_id: podcast_id(), + season_cursor: None, + season: Some(season), + episode_cursor: 0, + }); + state.podcasts.episodes = Some(EpisodeList { + seq: 1, + query: EpisodeQuery::new(podcast_id()), + items, + loading: false, + loading_more: false, + complete: true, + error: None, + focus: None, + }); + state +} + +pub fn seasons() -> Vec { + vec![ + SeasonSummary { + season: Some(1), + episode_count: 3, + unplayed_count: 1, + first_published_at_ms: Some(1_609_459_200_000), + last_published_at_ms: Some(1_640_995_200_000), + }, + SeasonSummary { + season: None, + episode_count: 2, + unplayed_count: 0, + first_published_at_ms: None, + last_published_at_ms: None, + }, + ] +} + +/// Queue item for episode `n` of the fixture podcast ("Show"). +pub fn episode_track(n: u32) -> crate::library::models::TrackItem { + let meta = row("Show", 0, 0, 0).summary.meta; + crate::library::models::TrackItem::podcast_episode( + episode(n, &format!("Episode {n}"), None) + .episode + .to_ref(&meta), + ) +} diff --git a/src/app/podcast_runtime.rs b/src/app/podcast_runtime.rs new file mode 100644 index 0000000..62af2ce --- /dev/null +++ b/src/app/podcast_runtime.rs @@ -0,0 +1,1145 @@ +//! Runtime side of podcasts: opens the podcast store, runs store queries, +//! feed fetches and sync-log writes off the UI thread, schedules background +//! refreshes, starts episode streams and feeds the progress writer, and +//! hands results back as `AppEvent`s. Decisions about state live in +//! `update::podcasts`. + +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use furumi_library::podcasts::{PodcastListFilter, PodcastStore}; +use music_dht::podcast::{PodcastEpisodeRef, reaches_completion}; + +use super::event::AppEvent; +use super::state::{AppState, PodcastRow}; +use super::update::{Effect, podcasts}; +use super::{Runtime, err_string, perform_effect}; +use crate::library::models::TrackItem; +use crate::podcasts::playback::{self, StartAt}; +use crate::podcasts::progress::{ + CHECKPOINT_INTERVAL, ProgressCommand, ProgressEvent, ProgressWriter, +}; +use crate::podcasts::refresh::{self, Services}; +use crate::podcasts::stream::{CancelToken, StreamEnd, StreamProbe}; + +/// How often the scheduler asks the store which feeds are due. Each feed's +/// own due time (success interval or failure back-off) lives in the store. +const SCHEDULER_INTERVAL: Duration = Duration::from_secs(60); +/// Delay before the first scheduler pass, so startup work goes first. +const SCHEDULER_FIRST_DELAY: Duration = Duration::from_secs(10); +/// Feeds fetched at the same time. +const MAX_CONCURRENT_REFRESHES: usize = 4; +/// Feeds started per scheduler pass. +const MAX_DUE_PER_PASS: usize = 16; + +/// Podcast services available once the store opened. +pub struct PodcastRuntime { + pub services: Services, + refresh_slots: Arc, + /// Writes progress in order on its own thread; `None` if it could not + /// start (episodes then play without saving progress). + progress: Option, +} + +/// The episode this device is playing (or starting), as the progress +/// tracker sees it. +#[derive(Debug)] +pub struct EpisodePlayback { + /// Player source generation of this start; identifies it everywhere. + generation: u64, + episode: PodcastEpisodeRef, + /// Queue key of the episode (`ep:…`). + key: String, + /// The stream plays; until then nothing is written. + started: bool, + duration_ms: Option, + /// `duration_ms` was measured by the decoder, not only announced by + /// the feed (`PreparedEpisode::duration_measured`). + duration_measured: bool, + /// Last position seen while this playback was current. + position_ms: i64, + /// Actual listening time (no pauses, no seeks). + listened_ms: i64, + last_tick: Instant, + last_checkpoint: Instant, + paused: bool, + completed: bool, + /// The server allows seeking in the stream. + seekable: bool, + /// How the stream ended, once the episode started. + probe: Option, +} + +/// What the end of the current source meant. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Finished { + /// Not an episode that was playing here. + NotEpisode, + /// The episode played to its end (completed). + Completed, + /// The stream stopped before the end (connection lost, URL expired): + /// the last position is saved and the queue must not move on. + Interrupted(String), +} + +fn now_ms() -> i64 { + super::unix_time_ms() +} + +/// Opens `podcasts.sqlite3` (migrating a prototype database in place) and +/// builds the HTTP client on the blocking pool. A failure only disables the +/// Podcasts tab; the store never modifies a file it refuses. +pub fn spawn_open(runtime: &Runtime) { + let tx = runtime.event_tx.clone(); + let log: Arc = runtime.devices.clone(); + tokio::task::spawn_blocking(move || { + let result = open(log).map_err(|err| { + let message = crate::podcasts::describe_open_error(&err); + tracing::warn!("podcast storage unavailable: {message}"); + message + }); + let _ = tx.send(AppEvent::PodcastStoreOpened(result)); + }); +} + +fn open(log: Arc) -> anyhow::Result { + crate::podcasts::open_services( + &crate::podcasts::db_path()?, + crate::podcasts::covers_dir()?, + log, + ) +} + +fn spawn_scheduler(tx: tokio::sync::mpsc::UnboundedSender) { + tokio::spawn(async move { + let start = tokio::time::Instant::now() + SCHEDULER_FIRST_DELAY; + let mut interval = tokio::time::interval_at(start, SCHEDULER_INTERVAL); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + loop { + interval.tick().await; + if tx.send(AppEvent::PodcastRefreshTick).is_err() { + return; + } + } + }); +} + +fn services(runtime: &Runtime) -> Option { + runtime + .podcasts + .as_ref() + .map(|podcasts| podcasts.services.clone()) +} + +/// Executes a podcast [`Effect`]; any other effect is handed back. +pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> Option { + let Some(services) = services(runtime) else { + if is_podcast_effect(&effect) { + if let Effect::PodcastRefresh { targets, .. } = &effect { + for (id, _) in targets { + state.podcasts.refreshing.remove(id); + } + } + if matches!(effect, Effect::PodcastSubscribe(_)) { + state.podcasts.subscribing = false; + } + state.status_message = Some("podcasts are unavailable (see the Podcasts tab)".into()); + return None; + } + return Some(effect); + }; + let tx = runtime.event_tx.clone(); + match effect { + Effect::PodcastsReload => { + tokio::task::spawn_blocking(move || { + let result = load_rows(&services.store).map_err(err_string); + let _ = tx.send(AppEvent::PodcastsLoaded(result)); + }); + } + Effect::PodcastLoadSeasons(podcast_id) => { + tokio::task::spawn_blocking(move || { + let result = services.store.seasons(&podcast_id).map_err(err_string); + let _ = tx.send(AppEvent::PodcastSeasonsLoaded { podcast_id, result }); + }); + } + Effect::PodcastLoadEpisodes { seq, query } => { + tokio::task::spawn_blocking(move || { + let offset = query.offset; + let result = services.store.episodes(&query).map_err(err_string); + let _ = tx.send(AppEvent::PodcastEpisodesLoaded { + seq, + offset, + result, + }); + }); + } + Effect::PodcastSubscribe(input) => { + tokio::spawn(async move { + let result = refresh::subscribe(&services, &input, now_ms()) + .await + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastSubscribed(result)); + }); + } + Effect::PodcastRefresh { targets, manual } => { + let slots = runtime + .podcasts + .as_ref() + .map(|podcasts| Arc::clone(&podcasts.refresh_slots))?; + for (podcast_id, url) in targets { + let services = services.clone(); + let slots = Arc::clone(&slots); + let tx = tx.clone(); + tokio::spawn(async move { + let Ok(_permit) = slots.acquire_owned().await else { + return; + }; + let result = refresh::refresh(&services, &podcast_id, &url, now_ms()) + .await + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastRefreshed { + podcast_id, + result, + manual, + }); + }); + } + } + Effect::PodcastUnsubscribe { podcast_id, title } => { + tokio::task::spawn_blocking(move || { + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + let result = services + .log + .record(&mut |stamps| store.unsubscribe(stamps, &id)) + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastUnsubscribed { + podcast_id, + title, + result, + }); + }); + } + Effect::PodcastSetPlayed { episode_id, played } => { + // Through the progress writer, so it is ordered with the + // checkpoints of an episode that is playing. + match runtime + .podcasts + .as_ref() + .and_then(|podcasts| podcasts.progress.as_ref()) + { + Some(progress) => progress.send(ProgressCommand::SetPlayed { episode_id, played }), + None => { + state.status_message = + Some("podcasts: progress cannot be saved (see the log)".into()); + } + } + } + other => return Some(other), + } + None +} + +fn is_podcast_effect(effect: &Effect) -> bool { + matches!( + effect, + Effect::PodcastsReload + | Effect::PodcastLoadSeasons(_) + | Effect::PodcastLoadEpisodes { .. } + | Effect::PodcastSubscribe(_) + | Effect::PodcastRefresh { .. } + | Effect::PodcastUnsubscribe { .. } + | Effect::PodcastSetPlayed { .. } + ) +} + +/// Subscribed podcasts with their local fetch state. +pub(crate) fn load_rows(store: &PodcastStore) -> anyhow::Result> { + store + .list_podcasts(PodcastListFilter::Subscribed)? + .into_iter() + .map(|summary| { + let fetch = store.fetch_state(&summary.podcast_id)?; + Ok(PodcastRow { summary, fetch }) + }) + .collect() +} + +/// Folds a podcast `AppEvent` into the state; any other event is handed +/// back. +pub fn handle_event( + state: &mut AppState, + runtime: &mut Runtime, + event: AppEvent, +) -> Option { + let effects: Vec = match event { + AppEvent::PodcastStoreOpened(result) => { + let result = result.map(|services| { + let tx = runtime.event_tx.clone(); + let progress = ProgressWriter::spawn(services.clone(), move |event| { + let _ = tx.send(AppEvent::PodcastProgress(event)); + }) + .inspect_err(|err| tracing::warn!("{err:#}")) + .ok(); + // Device sync applies received podcast operations and + // snapshot sections to the store from now on (and the ones + // that arrived before it opened). + let devices = Arc::clone(&runtime.devices); + let store = Arc::clone(&services.store); + tokio::task::spawn_blocking(move || { + if let Err(err) = devices.attach_podcast_store(store) { + tracing::warn!("podcast sync: {err:#}"); + } + }); + runtime.podcasts = Some(PodcastRuntime { + services, + refresh_slots: Arc::new(tokio::sync::Semaphore::new(MAX_CONCURRENT_REFRESHES)), + progress, + }); + spawn_scheduler(runtime.event_tx.clone()); + }); + podcasts::store_opened(state, result).into_iter().collect() + } + AppEvent::PodcastsLoaded(result) => { + podcasts::list_loaded(state, result); + Vec::new() + } + AppEvent::PodcastSeasonsLoaded { podcast_id, result } => { + podcasts::seasons_loaded(state, podcast_id, result); + Vec::new() + } + AppEvent::PodcastEpisodesLoaded { + seq, + offset, + result, + } => { + podcasts::episodes_loaded(state, seq, offset, result); + Vec::new() + } + AppEvent::PodcastSubscribed(result) => { + podcasts::subscribed(state, result).into_iter().collect() + } + AppEvent::PodcastRefreshed { + podcast_id, + result, + manual, + } => podcasts::refreshed(state, podcast_id, result, manual), + AppEvent::PodcastUnsubscribed { + podcast_id, + title, + result, + } => podcasts::unsubscribed(state, podcast_id, title, result) + .into_iter() + .collect(), + AppEvent::PodcastProgress(event) => progress_event(state, runtime, event), + AppEvent::PodcastsSynced { progress, changed } => { + if changed { + // A subscription that came from another device has no + // episodes here until its feed is fetched: check now. + let _ = runtime.event_tx.send(AppEvent::PodcastRefreshTick); + } + podcasts::synced(state, progress, changed) + } + AppEvent::PodcastEpisodeStarted { + generation, + episode, + start_ms, + duration_ms, + duration_measured, + restart, + seekable, + probe, + } => { + episode_started( + state, + runtime, + generation, + *episode, + Started { + start_ms, + duration_ms, + duration_measured, + restart, + seekable, + probe, + }, + ); + Vec::new() + } + AppEvent::PodcastEpisodeFailed { + generation, + message, + } => { + episode_failed(state, runtime, generation, message); + Vec::new() + } + AppEvent::PodcastRefreshTick => { + if let Some(services) = services(runtime) { + let tx = runtime.event_tx.clone(); + tokio::task::spawn_blocking(move || { + let result = services + .store + .podcasts_due_for_refresh(now_ms(), MAX_DUE_PER_PASS) + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastsDue(result)); + }); + } + Vec::new() + } + AppEvent::PodcastsDue(result) => match result { + Ok(due) => podcasts::refresh_due(state, due).into_iter().collect(), + Err(err) => { + tracing::warn!(%err, "podcast scheduler query failed"); + Vec::new() + } + }, + other => return Some(other), + }; + for effect in effects { + perform_effect(state, runtime, effect); + } + None +} + +/// Cover of the podcast under the cursor (or opened), for the art cache. +pub fn wanted_cover(state: &AppState) -> Option { + if state.active_tab != super::state::Tab::Podcasts { + return None; + } + state.podcasts.current_podcast()?.fetch.cover_path.clone() +} + +// --------------------------------------------------------------------------- +// Episode playback and progress + +fn progress_writer(runtime: &Runtime) -> Option<&ProgressWriter> { + runtime + .podcasts + .as_ref() + .and_then(|podcasts| podcasts.progress.as_ref()) +} + +fn send_progress(runtime: &Runtime, command: ProgressCommand) { + if let Some(progress) = progress_writer(runtime) { + progress.send(command); + } +} + +/// Starts streaming `track` (a podcast episode) for the current queue item. +/// The caller already updated the player state; this silences the previous +/// audio, ends the previous episode's session and prepares the stream in +/// the background. The stream plays only if no other start or stop +/// happened meanwhile. +pub(crate) fn start_episode( + state: &mut AppState, + runtime: &mut Runtime, + track: &TrackItem, + start: StartAt, + paused: bool, +) { + let Some(queued) = track.podcast_episode_ref().cloned() else { + return; + }; + end_playback(runtime); + runtime.player.stop(); + let generation = runtime.player.source_generation(); + // Pause and resume pressed while the stream connects update this. + runtime.player.expect_source(paused); + runtime.podcast_playback = Some(EpisodePlayback { + generation, + episode: queued.clone(), + key: super::state::track_key(track), + started: false, + duration_ms: queued.duration_ms, + duration_measured: false, + position_ms: 0, + listened_ms: 0, + last_tick: Instant::now(), + last_checkpoint: Instant::now(), + paused, + completed: false, + seekable: false, + probe: None, + }); + if runtime.podcasts.is_none() { + tracing::warn!("podcast store unavailable: this episode plays without saving progress"); + } + state.status_message = Some(format!("podcast: connecting to \"{}\"…", track.title)); + let services = services(runtime); + let controller = runtime.player.clone(); + let tx = runtime.event_tx.clone(); + let volume = crate::player::amplitude(state.player.volume); + let restart = start == StartAt::Restart; + // Another start or stop cancels the connection, a resume still reading + // up to its offset, and later reads of this stream. + let cancel = CancelToken::new(controller.superseded_check(generation)); + tokio::spawn(async move { + let prepared = playback::prepare(services, queued, start, now_ms(), cancel).await; + // A newer start or stop happened meanwhile: neither play nor report. + if controller.source_generation() != generation { + return; + } + match prepared { + Ok(prepared) => { + let start = Duration::from_millis(prepared.start_ms.max(0) as u64); + controller.play_source(prepared.source, volume, start, generation); + let _ = tx.send(AppEvent::PodcastEpisodeStarted { + generation, + episode: Box::new(prepared.episode), + start_ms: prepared.start_ms, + duration_ms: prepared.duration_ms, + duration_measured: prepared.duration_measured, + restart, + seekable: prepared.seekable, + probe: prepared.probe, + }); + } + Err(err) => { + let message = playback::describe_failure(&err); + tracing::warn!("podcast episode cannot start: {message}"); + let _ = tx.send(AppEvent::PodcastEpisodeFailed { + generation, + message, + }); + } + } + }); +} + +/// What [`AppEvent::PodcastEpisodeStarted`] reports about the stream. +struct Started { + start_ms: i64, + duration_ms: Option, + duration_measured: bool, + restart: bool, + seekable: bool, + probe: StreamProbe, +} + +fn episode_started( + state: &mut AppState, + runtime: &mut Runtime, + generation: u64, + episode: PodcastEpisodeRef, + started: Started, +) { + let Started { + start_ms, + duration_ms, + duration_measured, + restart, + seekable, + probe, + } = started; + let Some(playback) = runtime + .podcast_playback + .as_mut() + .filter(|playback| playback.generation == generation) + else { + return; + }; + playback.started = true; + playback.seekable = seekable; + playback.probe = Some(probe); + playback.position_ms = start_ms; + playback.duration_ms = duration_ms; + playback.duration_measured = duration_measured; + playback.last_tick = Instant::now(); + playback.last_checkpoint = Instant::now(); + playback.episode = episode.clone(); + state.player.position_secs = start_ms as f64 / 1_000.0; + if let Some(current) = state.player.current.as_mut() + && current.is_podcast() + && current.duration_seconds <= 0.0 + && let Some(ms) = duration_ms + { + current.duration_seconds = ms as f64 / 1_000.0; + } + state.status_message = Some(if start_ms > 0 { + format!( + "▶ {} — continuing at {}", + episode.episode_title, + format_clock(start_ms) + ) + } else { + format!("▶ {} — {}", episode.episode_title, episode.podcast_title) + }); + let session_id = runtime.devices.new_listen_id(); + send_progress( + runtime, + ProgressCommand::Begin { + playback: generation, + episode: Box::new(episode), + session_id, + position_ms: start_ms, + restart, + now_ms: now_ms(), + }, + ); +} + +fn episode_failed(state: &mut AppState, runtime: &mut Runtime, generation: u64, message: String) { + if runtime + .podcast_playback + .as_ref() + .is_none_or(|playback| playback.generation != generation) + { + return; + } + runtime.podcast_playback = None; + runtime.player_start_pending = false; + stop_on_episode(state); + state.status_message = Some(format!("podcast: {message}")); +} + +/// Leaves the queue on the current episode, stopped: Play starts it again +/// (from its saved position). +fn stop_on_episode(state: &mut AppState) { + state.player.playing = false; + state.player.paused = false; + state.player.current = None; + state.player.listen_id = None; + state.player.prefetched_pos = None; +} + +fn format_clock(ms: i64) -> String { + let secs = ms.max(0) / 1_000; + let (hours, minutes, seconds) = (secs / 3_600, secs / 60 % 60, secs % 60); + if hours > 0 { + format!("{hours}:{minutes:02}:{seconds:02}") + } else { + format!("{minutes}:{seconds:02}") + } +} + +/// Ends the session of the episode that was playing, saving its last seen +/// position (never the state after a stop, which may already be reset). +pub(crate) fn end_playback(runtime: &mut Runtime) { + let Some(playback) = runtime.podcast_playback.take() else { + return; + }; + if !playback.started { + return; + } + send_progress( + runtime, + ProgressCommand::End { + playback: playback.generation, + position_ms: playback.position_ms, + listened_ms: playback.listened_ms, + now_ms: now_ms(), + }, + ); +} + +impl EpisodePlayback { + /// Why this episode cannot be seeked now, if it cannot. + fn seek_refusal(&self) -> Option<&'static str> { + if !self.started { + Some("podcast: the episode is still connecting; seek once it plays") + } else if !self.seekable { + Some("podcast: the publisher's server does not allow seeking in this episode") + } else { + None + } + } + + /// The stream ended by itself (`end`). Completes the episode only if it + /// was played to the end (`playback::played_to_the_end`); otherwise the + /// last seen position stays what is saved. Returns the commands to send + /// and whether it was played to the end. + fn finish(&mut self, end: StreamEnd, now_ms: i64) -> (Vec, bool) { + if !playback::played_to_the_end( + end, + self.position_ms, + self.duration_ms, + self.duration_measured, + ) { + return (Vec::new(), false); + } + let position_ms = self + .duration_ms + .unwrap_or(self.position_ms) + .max(self.position_ms); + self.position_ms = position_ms; + let mut commands = Vec::new(); + if !self.completed { + self.completed = true; + commands.push(ProgressCommand::Complete { + playback: self.generation, + position_ms, + listened_ms: self.listened_ms, + now_ms, + }); + } + (commands, true) + } + + /// Folds one player observation in and returns what to save: counts + /// actual listening time (not pauses, seeks or stalls), saves on pause, + /// every [`CHECKPOINT_INTERVAL`] while playing, and completes the + /// episode once the position reaches the shared completion criterion. + fn observe( + &mut self, + position_ms: i64, + paused: bool, + now: Instant, + now_ms: i64, + ) -> Vec { + let mut commands = Vec::new(); + let elapsed_ms = now.saturating_duration_since(self.last_tick).as_millis() as i64; + if !paused && !self.paused { + let advanced = position_ms - self.position_ms; + if advanced >= 0 && advanced <= elapsed_ms + 1_000 { + self.listened_ms += advanced; + } + } + self.position_ms = position_ms; + self.last_tick = now; + if paused != self.paused { + self.paused = paused; + if paused { + commands.push(ProgressCommand::Pause { + playback: self.generation, + position_ms, + listened_ms: self.listened_ms, + now_ms, + }); + } + self.last_checkpoint = now; + } + if paused { + return commands; + } + if !self.completed && reaches_completion(position_ms, self.duration_ms) { + self.completed = true; + commands.push(ProgressCommand::Complete { + playback: self.generation, + position_ms, + listened_ms: self.listened_ms, + now_ms, + }); + } else if now.saturating_duration_since(self.last_checkpoint) >= CHECKPOINT_INTERVAL { + self.last_checkpoint = now; + commands.push(ProgressCommand::Checkpoint { + playback: self.generation, + position_ms, + }); + } + commands + } +} + +/// Whether `state` still plays the tracked episode on this device. +fn still_current(state: &AppState, playback: &EpisodePlayback) -> bool { + state.device_playback.is_audio_owner() + && state.player.playing + && state + .player + .current + .as_ref() + .is_some_and(|track| super::state::track_key(track) == playback.key) +} + +/// Player tick (every 250 ms): follows the position, counts listening time, +/// saves on pause, periodically while playing, and completes the episode +/// near its end. +pub(crate) fn tick(state: &AppState, runtime: &mut Runtime) { + let Some(playback) = runtime.podcast_playback.as_ref() else { + return; + }; + if !still_current(state, playback) { + // Stopped, replaced or handed off by a path without its own hook: + // the last seen position is what gets saved. + end_playback(runtime); + return; + } + if !playback.started || runtime.player_start_pending { + return; + } + let position_ms = (state.player.position_secs * 1_000.0).round() as i64; + let paused = state.player.paused; + let commands = runtime + .podcast_playback + .as_mut() + .expect("checked above") + .observe(position_ms, paused, Instant::now(), now_ms()); + for command in commands { + send_progress(runtime, command); + } +} + +/// Why the current episode cannot be seeked now, if it cannot: it is still +/// connecting, or its server does not allow seeking. Callers skip the seek +/// and show the reason. +pub(crate) fn seek_refusal(state: &AppState, runtime: &Runtime) -> Option { + let playback = runtime.podcast_playback.as_ref()?; + if !still_current(state, playback) { + return None; + } + playback.seek_refusal().map(str::to_string) +} + +/// The audio thread handled a seek of the source started under +/// `generation`. Only a seek that moved the episode is saved, as an +/// explicit action at the position actually reached. +pub(crate) fn seek_done( + state: &mut AppState, + runtime: &mut Runtime, + generation: u64, + position: Duration, + result: Result<(), String>, +) { + if runtime + .podcast_playback + .as_ref() + .is_none_or(|playback| playback.generation != generation) + { + return; + } + match result { + Ok(()) => seeked(state, runtime, position.as_secs_f64()), + Err(err) => { + state.status_message = Some(format!("podcast: cannot seek in this episode ({err})")); + } + } +} + +/// The playing episode moved to `target_secs` by the user: an explicit action. +fn seeked(state: &AppState, runtime: &mut Runtime, target_secs: f64) { + let Some(playback) = runtime.podcast_playback.as_mut() else { + return; + }; + if !playback.started || !still_current(state, playback) { + return; + } + let mut position_ms = (target_secs.max(0.0) * 1_000.0).round() as i64; + // rodio stops a seek at the end of a source of known length. + if let Some(duration_ms) = playback.duration_ms { + position_ms = position_ms.min(duration_ms); + } + playback.position_ms = position_ms; + playback.last_tick = Instant::now(); + playback.last_checkpoint = Instant::now(); + // The next tick completes it again if the seek went into the last + // seconds; a seek back makes it "in progress" (the writer agrees). + playback.completed = false; + let command = ProgressCommand::Seek { + playback: playback.generation, + position_ms, + }; + send_progress(runtime, command); +} + +/// The source of the current episode ended by itself. Completes the +/// episode when its stream reached the end; a stream that broke off (lost +/// connection, expired URL) keeps the last position, and the caller must +/// stop on the episode instead of moving on. +pub(crate) fn finished(state: &mut AppState, runtime: &mut Runtime) -> Finished { + let Some(playback) = runtime.podcast_playback.as_mut() else { + return Finished::NotEpisode; + }; + if !playback.started { + return Finished::NotEpisode; + } + let end = playback + .probe + .as_ref() + .map_or(StreamEnd::Complete, StreamProbe::end); + let (commands, reached) = playback.finish(end, now_ms()); + let position_ms = playback.position_ms; + let seekable = playback.seekable; + for command in commands { + send_progress(runtime, command); + } + end_playback(runtime); + if reached { + return Finished::Completed; + } + tracing::warn!(?end, position_ms, "podcast stream stopped before the end"); + runtime.player_start_pending = false; + stop_on_episode(state); + Finished::Interrupted(interrupted_message(position_ms, seekable)) +} + +/// Status for an episode whose stream stopped before its end. Without +/// seeking (no file size from the server) Play cannot continue there, so +/// the way out is to start over or mark it played. +fn interrupted_message(position_ms: i64, seekable: bool) -> String { + let at = format_clock(position_ms); + if seekable { + format!( + "podcast: the audio stopped at {at} (the publisher's server stopped sending it); \ + the position is saved, press play to continue" + ) + } else { + format!( + "podcast: the audio stopped at {at} (the publisher's server stopped sending it); \ + the position is saved, but this server does not allow continuing there: start it \ + over (b) or mark it played (m) in Podcasts" + ) + } +} + +/// The audio thread failed while an episode played or started. +pub(crate) fn player_failed(runtime: &mut Runtime) { + end_playback(runtime); +} + +fn progress_event( + state: &mut AppState, + runtime: &mut Runtime, + event: ProgressEvent, +) -> Vec { + match event { + ProgressEvent::Saved(record) => podcasts::progress_saved(state, record), + ProgressEvent::Superseded { current } => { + if runtime + .podcast_playback + .as_ref() + .is_some_and(|playback| playback.episode.episode_id == current.episode_id) + { + state.status_message = Some( + "podcast: this episode's progress changed (marked, or played on another \ + device); its position is saved again after a seek or start over" + .into(), + ); + } + podcasts::progress_saved(state, current) + } + ProgressEvent::Ended { .. } => vec![Effect::PodcastsReload], + ProgressEvent::PlayedSet { played, result } => podcasts::played_set(state, played, result), + ProgressEvent::Failed(message) => { + state.status_message = Some(message); + Vec::new() + } + } +} + +/// Before exit: end the playing episode's session and give the progress +/// writer a moment to save it. +pub(crate) async fn shutdown(runtime: &mut Runtime) { + end_playback(runtime); + let Some(mut progress) = runtime + .podcasts + .as_mut() + .and_then(|podcasts| podcasts.progress.take()) + else { + return; + }; + let _ = tokio::task::spawn_blocking(move || progress.close(Duration::from_secs(3))).await; +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tracked(duration_ms: Option) -> (EpisodePlayback, Instant) { + let track = crate::app::podcast_fixtures::episode_track(1); + let episode = track.podcast_episode_ref().unwrap().clone(); + let start = Instant::now(); + let playback = EpisodePlayback { + generation: 7, + key: super::super::state::track_key(&track), + episode, + started: true, + duration_ms, + duration_measured: duration_ms.is_some(), + position_ms: 0, + listened_ms: 0, + last_tick: start, + last_checkpoint: start, + paused: false, + completed: false, + seekable: true, + probe: None, + }; + (playback, start) + } + + fn at(start: Instant, ms: u64) -> Instant { + start + Duration::from_millis(ms) + } + + #[test] + fn listening_time_skips_jumps_and_pauses_and_checkpoints_every_interval() { + let (mut playback, start) = tracked(Some(3_600_000)); + let mut commands = Vec::new(); + // Ten seconds of ordinary playback, observed every 250 ms. + for step in 1..=40u64 { + commands.extend(playback.observe(step as i64 * 250, false, at(start, step * 250), 0)); + } + assert!(commands.is_empty(), "{commands:?}"); + assert_eq!(playback.listened_ms, 10_000); + // A forward jump (a seek) is not listening time. + playback.observe(600_000, false, at(start, 10_250), 0); + assert_eq!(playback.listened_ms, 10_000); + // Pausing saves once; time while paused is not counted. + let paused = playback.observe(600_000, true, at(start, 10_500), 1); + assert!(matches!( + paused.as_slice(), + [ProgressCommand::Pause { + playback: 7, + position_ms: 600_000, + listened_ms: 10_000, + now_ms: 1, + }] + )); + assert!( + playback + .observe(600_000, true, at(start, 60_000), 2) + .is_empty() + ); + assert!( + playback + .observe(600_000, false, at(start, 60_250), 3) + .is_empty() + ); + // The next periodic save comes one interval after the resume. + let interval = CHECKPOINT_INTERVAL.as_millis() as u64; + let mut saved = Vec::new(); + let mut position = 600_000; + for step in 1..=(interval / 250) { + position += 250; + saved.extend(playback.observe(position, false, at(start, 60_250 + step * 250), 4)); + } + assert!(matches!( + saved.as_slice(), + [ProgressCommand::Checkpoint { playback: 7, position_ms }] if *position_ms == position + )); + assert_eq!(playback.listened_ms, 10_000 + interval as i64); + } + + #[test] + fn a_stream_that_broke_off_keeps_the_episode_in_progress() { + let (mut playback, start) = tracked(Some(3_600_000)); + playback.observe(600_000, false, at(start, 250), 0); + // Connection lost far from the end: nothing is completed; the + // caller ends the session at the last seen position. + let (commands, reached) = playback.finish(StreamEnd::Failed, 1); + assert!(commands.is_empty() && !reached); + assert_eq!(playback.position_ms, 600_000); + assert!(!playback.completed); + // Same for a stream without a size that closed early. + let (commands, reached) = playback.finish(StreamEnd::Unknown, 1); + assert!(commands.is_empty() && !reached); + // A failure within the completion window, or a real end, completes. + playback.observe(3_590_000, false, at(start, 500), 0); + playback.completed = false; + let (commands, reached) = playback.finish(StreamEnd::Failed, 2); + assert!(reached); + assert!(matches!( + commands.as_slice(), + [ProgressCommand::Complete { + position_ms: 3_600_000, + .. + }] + )); + let (mut ended, start) = tracked(Some(3_600_000)); + ended.observe(1_000, false, at(start, 250), 0); + let (commands, reached) = ended.finish(StreamEnd::Complete, 3); + assert!(reached); + assert_eq!(commands.len(), 1); + // Without a size or a duration, the end of the stream is all there is. + let (mut unknown, start) = tracked(None); + unknown.observe(1_000, false, at(start, 250), 0); + assert!(unknown.finish(StreamEnd::Unknown, 4).1); + } + + /// A stream without a size whose decoder could not measure it (CBR MP3 + /// without a Xing header, say) only has the feed's `itunes:duration`, + /// which may be longer than the audio by more than the completion + /// window. Played to its real end, it completes and the queue moves on. + #[test] + fn a_stream_shorter_than_its_feed_duration_completes_at_its_end() { + // The feed says 60 min; the audio ends at 55 min (more than the + // 30 s completion window earlier). + let (mut feed_only, start) = tracked(Some(3_600_000)); + feed_only.duration_measured = false; + feed_only.observe(3_300_000, false, at(start, 250), 0); + let (commands, reached) = feed_only.finish(StreamEnd::Unknown, 1); + assert!(reached, "the clean close is the end"); + assert!(matches!( + commands.as_slice(), + [ProgressCommand::Complete { + position_ms: 3_600_000, + .. + }] + )); + // An early close (a fifth of the announced length) is still not. + let (mut early, start) = tracked(Some(3_600_000)); + early.duration_measured = false; + early.observe(600_000, false, at(start, 250), 0); + assert!(!early.finish(StreamEnd::Unknown, 2).1); + // A duration the decoder measured keeps the strict window. + let (mut measured, start) = tracked(Some(3_600_000)); + measured.observe(3_300_000, false, at(start, 250), 0); + assert!(!measured.finish(StreamEnd::Unknown, 3).1); + // A failed read never counts outside the window. + let (mut failed, start) = tracked(Some(3_600_000)); + failed.duration_measured = false; + failed.observe(3_300_000, false, at(start, 250), 0); + assert!(!failed.finish(StreamEnd::Failed, 4).1); + } + + #[test] + fn an_interrupted_stream_without_seeking_says_how_to_move_on() { + let message = interrupted_message(65_000, true); + assert!( + message.contains("1:05") && message.contains("press play"), + "{message}" + ); + let message = interrupted_message(65_000, false); + assert!(!message.contains("press play"), "{message}"); + assert!(message.contains("start it over (b)"), "{message}"); + assert!(message.contains("mark it played (m)"), "{message}"); + } + + #[test] + fn seeks_are_refused_while_connecting_and_without_range_support() { + let (mut playback, _) = tracked(Some(3_600_000)); + assert_eq!(playback.seek_refusal(), None); + playback.seekable = false; + assert!( + playback + .seek_refusal() + .is_some_and(|reason| reason.contains("does not allow seeking")) + ); + playback.started = false; + assert!( + playback + .seek_refusal() + .is_some_and(|reason| reason.contains("still connecting")) + ); + } + + #[test] + fn the_end_of_an_episode_completes_it_once() { + let (mut playback, start) = tracked(Some(2_700_000)); + assert!( + playback + .observe(2_669_000, false, at(start, 250), 0) + .is_empty() + ); + let completed = playback.observe(2_670_500, false, at(start, 500), 0); + assert!(matches!( + completed.as_slice(), + [ProgressCommand::Complete { + position_ms: 2_670_500, + .. + }] + )); + assert!( + playback + .observe(2_671_000, false, at(start, 750), 0) + .is_empty() + ); + // Without a known duration only the end of the stream completes. + let (mut unknown, start) = tracked(None); + assert!( + unknown + .observe(10_000_000, false, at(start, 250), 0) + .is_empty() + ); + } +} diff --git a/src/app/popup.rs b/src/app/popup.rs index 1d82205..4bba6e9 100644 --- a/src/app/popup.rs +++ b/src/app/popup.rs @@ -115,6 +115,15 @@ pub fn handle_key(state: &mut AppState, runtime: &mut Runtime, key: KeyEvent) { Popup::ConfirmDelete { target, label } => { handle_confirm_delete(state, runtime, target, label, key); } + Popup::ConfirmPodcastUnsubscribe { podcast_id, title } => match key.code { + KeyCode::Enter | KeyCode::Char('y') => super::perform_effect( + state, + runtime, + crate::app::update::Effect::PodcastUnsubscribe { podcast_id, title }, + ), + KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {} + _ => state.popup = Some(Popup::ConfirmPodcastUnsubscribe { podcast_id, title }), + }, Popup::SimilarityPrivacyConsent { enable_federation } => match key.code { KeyCode::Enter | KeyCode::Char('y') => { state.similarity.settings.federation_consent = true; @@ -557,10 +566,31 @@ fn handle_fed_input( key: KeyEvent, ) { match key.code { - KeyCode::Esc => {} + KeyCode::Esc => { + // Esc in the episode search clears it. + if field == FedInputField::PodcastSearch + && let Some(effect) = crate::app::update::podcasts::set_search(state, String::new()) + { + super::perform_effect(state, runtime, effect); + } + } KeyCode::Enter => { let value = input.trim().to_string(); match field { + FedInputField::PodcastFeedUrl => { + if let Some(effect) = + crate::app::update::podcasts::submit_feed_url(state, &value) + { + super::perform_effect(state, runtime, effect); + } + } + // The search is applied while typing; Enter keeps it. + FedInputField::PodcastSearch => { + let search = input.to_string(); + if let Some(effect) = crate::app::update::podcasts::set_search(state, search) { + super::perform_effect(state, runtime, effect); + } + } FedInputField::NetworkId => { state.federation.settings.network_id = value; // An empty id turns federation off rather than leaving a @@ -666,7 +696,13 @@ fn handle_fed_input( } _ => { input.handle_key(key); + let search = (field == FedInputField::PodcastSearch).then(|| input.to_string()); state.popup = Some(Popup::FedInput { field, input }); + if let Some(search) = search + && let Some(effect) = crate::app::update::podcasts::set_search(state, search) + { + super::perform_effect(state, runtime, effect); + } } } } @@ -785,11 +821,17 @@ fn handle_device_leave(state: &mut AppState, runtime: &mut Runtime, key: KeyEven } /// Pasted text goes into the focused text field when one is open. -pub fn handle_paste(state: &mut AppState, pasted: &str) { +pub fn handle_paste(state: &mut AppState, runtime: &mut Runtime, pasted: &str) { let cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect(); + let mut search = None; match &mut state.popup { Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned), - Some(Popup::FedInput { input, .. }) => input.insert_str(&cleaned), + Some(Popup::FedInput { field, input }) => { + input.insert_str(&cleaned); + if *field == FedInputField::PodcastSearch { + search = Some(input.to_string()); + } + } Some(Popup::Edit { fields, focus, .. }) => { if let Some(field) = fields.get_mut(*focus) { field.value.insert_str(&cleaned); @@ -797,6 +839,11 @@ pub fn handle_paste(state: &mut AppState, pasted: &str) { } _ => {} } + if let Some(search) = search + && let Some(effect) = crate::app::update::podcasts::set_search(state, search) + { + super::perform_effect(state, runtime, effect); + } } // --------------------------------------------------------------------------- @@ -1018,6 +1065,21 @@ fn handle_track_info( let len = tracks.len(); match key.code { KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q') => {} + // An episode's "artist" is its podcast title: never look it up as a + // federated artist (that would send a private title to the + // network); open the podcast instead. + KeyCode::Char('a') + if tracks + .get(cursor.min(len.saturating_sub(1))) + .is_some_and(TrackItem::is_podcast) => + { + let track = tracks[cursor.min(len.saturating_sub(1))].clone(); + if let Some(effect) = + crate::app::update::podcasts::reveal_episode_podcast(state, &track) + { + super::perform_effect(state, runtime, effect); + } + } KeyCode::Char('a') => { let artists = tracks .get(cursor.min(len.saturating_sub(1))) @@ -1079,6 +1141,10 @@ fn handle_track_info( KeyCode::Char('c') => { if let Some(track) = tracks.get(cursor.min(len.saturating_sub(1))) { match crate::share::track_share_link(track) { + None if track.is_podcast() => { + state.status_message = + Some("podcast episodes have no track share link".into()) + } Some(link) => { state.popup = Some(Popup::FedCopyText { title: "Track share link".into(), @@ -1108,7 +1174,7 @@ fn handle_track_info( cursor, scroll, }); - } else if track.id < 0 || track.file_path.is_empty() { + } else if track.is_podcast() || track.id < 0 || track.file_path.is_empty() { state.status_message = Some("similarity search starts from a local track".into()); state.popup = Some(Popup::TrackInfo { tracks, diff --git a/src/app/state.rs b/src/app/state.rs index a787484..37c14dc 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -338,6 +338,7 @@ mod tests { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }], }; let mut state = AppState::default(); @@ -762,6 +763,11 @@ pub enum Popup { }, /// Delete confirmation; Enter/y deletes, Esc/n cancels. ConfirmDelete { target: DeleteTarget, label: String }, + /// Unsubscribe confirmation; progress and history are kept. + ConfirmPodcastUnsubscribe { + podcast_id: music_dht::podcast::PodcastId, + title: String, + }, /// Enabling network similarity reveals an embedding query to peers. SimilarityPrivacyConsent { enable_federation: bool }, /// Derived data is safe to recreate but potentially expensive. @@ -899,6 +905,10 @@ pub enum FedInputField { DeviceName, ConnectInvite, JamInvite, + /// Podcasts: RSS/Atom feed URL of a new subscription. + PodcastFeedUrl, + /// Podcasts: live search in the opened episode list. + PodcastSearch, } impl FedInputField { @@ -913,6 +923,8 @@ impl FedInputField { FedInputField::DeviceName => "Device name", FedInputField::ConnectInvite => "Connect device (paste frid://i invite)", FedInputField::JamInvite => "Join Jam (paste frid://j invite)", + FedInputField::PodcastFeedUrl => "Add podcast (RSS feed URL)", + FedInputField::PodcastSearch => "Search episodes", } } @@ -945,6 +957,12 @@ impl FedInputField { FedInputField::JamInvite => { "Paste a frid://j capability to control the host player for this Jam only." } + FedInputField::PodcastFeedUrl => { + "Paste the podcast's RSS or Atom feed URL. Only episode information is stored; audio streams from the publisher when you play. New subscriptions are private." + } + FedInputField::PodcastSearch => { + "Words from episode titles or descriptions; the list updates as you type. Enter keeps the search, Esc clears it." + } } } } @@ -1382,7 +1400,7 @@ impl SimilaritySearchHit { Self::Local { track, .. } => track .content_id .clone() - .unwrap_or_else(|| format!("local:{}", track.id)), + .unwrap_or_else(|| track.key().to_string()), Self::Federated { track, .. } => track .content_id .clone() @@ -1665,6 +1683,197 @@ impl DevicePlaybackState { } } +// --------------------------------------------------------------------------- +// Podcasts tab +// --------------------------------------------------------------------------- + +/// Episodes loaded per page; the next page loads when the cursor nears the end. +pub const PODCAST_EPISODE_PAGE: usize = 500; +/// "Add podcast" and "Check all" rows above the subscriptions. +pub const PODCAST_ACTION_ROWS: usize = 2; + +/// State of the podcast store (`podcasts.sqlite3`). +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum PodcastStoreStatus { + #[default] + Opening, + Ready, + /// The store could not be opened (newer schema, foreign file, I/O). The + /// file was left unchanged; only the Podcasts tab is unavailable. + Failed(String), +} + +/// A subscribed podcast with its local fetch state. +#[derive(Debug, Clone, PartialEq)] +pub struct PodcastRow { + pub summary: furumi_library::podcasts::PodcastSummary, + pub fetch: furumi_library::podcasts::FetchState, +} + +impl PodcastRow { + pub fn id(&self) -> &music_dht::podcast::PodcastId { + &self.summary.podcast_id + } + + pub fn title(&self) -> &str { + &self.summary.meta.title + } +} + +/// One row of the subscriptions list. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PodcastListRow { + Add, + RefreshAll, + /// Index into the loaded podcasts. + Podcast(usize), +} + +/// One row of a podcast's season list. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SeasonRow { + All, + /// Index into the loaded seasons. + Group(usize), +} + +/// The podcast opened from the list: its season list (when the feed has +/// seasons) and the episode list of the chosen group. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct OpenedPodcast { + pub podcast_id: music_dht::podcast::PodcastId, + /// Season list cursor; `None` when the podcast has no seasons and its + /// episodes open directly. + pub season_cursor: Option, + /// The episode group shown; `None` while the season list is shown. + pub season: Option, + pub episode_cursor: usize, +} + +/// The episode list of the opened group, loaded page by page. +#[derive(Debug, Clone)] +pub struct EpisodeList { + /// Request sequence: results of older requests are dropped. + pub seq: u64, + /// Query of the first page (`offset` 0, `limit` one page). + pub query: furumi_library::podcasts::EpisodeQuery, + pub items: Vec, + /// The first page of `query` is in flight (`items` may still hold the + /// previous result, so the list does not flash empty). + pub loading: bool, + pub loading_more: bool, + /// Every matching episode is loaded. + pub complete: bool, + pub error: Option, + /// Episode to keep the cursor on when the first page arrives. + pub focus: Option, +} + +/// Podcasts tab: subscriptions, seasons and episodes from the podcast store. +/// Nothing here touches SQLite or the network; see `app::podcast_runtime`. +#[derive(Debug, Default)] +pub struct PodcastsTab { + pub store: PodcastStoreStatus, + /// Subscribed podcasts; `None` until the first load. + pub list: Option>>, + /// Cursor over [`PodcastsTab::list_rows`] (action rows first). + pub selected: usize, + pub opened: Option, + /// Season groups of the opened podcast. + pub seasons: Option<( + music_dht::podcast::PodcastId, + Loadable>, + )>, + pub episodes: Option, + /// Episode search text (titles and descriptions). + pub search: String, + pub played: furumi_library::podcasts::PlayedFilter, + pub sort: furumi_library::podcasts::EpisodeSort, + /// Feeds with a refresh in flight (spinner and request de-duplication). + pub refreshing: HashSet, + pub subscribing: bool, + /// Podcast to select once the reloaded list arrives. + pub pending_select: Option, + /// Last episode request sequence handed out. + pub episode_seq: u64, +} + +impl PodcastsTab { + pub fn podcasts(&self) -> &[PodcastRow] { + match &self.list { + Some(Loadable::Ready(list)) => list, + _ => &[], + } + } + + pub fn podcast(&self, id: &music_dht::podcast::PodcastId) -> Option<&PodcastRow> { + self.podcasts().iter().find(|row| row.id() == id) + } + + pub fn list_rows(&self) -> Vec { + let mut rows = vec![PodcastListRow::Add, PodcastListRow::RefreshAll]; + rows.extend((0..self.podcasts().len()).map(PodcastListRow::Podcast)); + rows + } + + /// The opened podcast, or the one under the list cursor. + pub fn current_podcast(&self) -> Option<&PodcastRow> { + if let Some(opened) = &self.opened { + return self.podcast(&opened.podcast_id); + } + match self.list_rows().get(self.selected)? { + PodcastListRow::Podcast(index) => self.podcasts().get(*index), + _ => None, + } + } + + /// Season groups of the opened podcast, when loaded. + pub fn opened_seasons(&self) -> Option<&[furumi_library::podcasts::SeasonSummary]> { + let opened = self.opened.as_ref()?; + match &self.seasons { + Some((id, Loadable::Ready(seasons))) if *id == opened.podcast_id => Some(seasons), + _ => None, + } + } + + pub fn season_rows(&self) -> Vec { + let mut rows = vec![SeasonRow::All]; + rows.extend((0..self.opened_seasons().map_or(0, <[_]>::len)).map(SeasonRow::Group)); + rows + } + + /// Whether the episode list (rather than the season list) is shown. + pub fn showing_episodes(&self) -> bool { + self.opened + .as_ref() + .is_some_and(|opened| opened.season.is_some()) + } + + /// Episodes of the opened group, when they belong to it. + pub fn opened_episodes(&self) -> Option<&EpisodeList> { + let opened = self.opened.as_ref()?; + let season = opened.season?; + self.episodes.as_ref().filter(|list| { + list.query.podcast_id == opened.podcast_id && list.query.season == season + }) + } + + pub fn selected_episode(&self) -> Option<&furumi_library::podcasts::EpisodeListItem> { + let opened = self.opened.as_ref()?; + let list = self.opened_episodes()?; + list.items.get(opened.episode_cursor) + } + + /// The selected episode as a queue item (explicit podcast source). + pub fn selected_episode_track(&self) -> Option { + let item = self.selected_episode()?; + let row = self.podcast(&item.episode.podcast_id)?; + let mut episode = item.episode.to_ref(&row.summary.meta); + episode.progress_generation = item.progress.as_ref().map(|record| record.generation); + Some(TrackItem::podcast_episode(episode)) + } +} + /// Single source of truth for the UI. Mutated only by `update()` and the /// event handlers in the main loop; views render from `&AppState`. #[derive(Debug, Default)] @@ -1717,6 +1926,7 @@ pub struct AppState { pub queue_tab: QueueTab, pub federation: FederationTab, pub similarity: SimilarityTab, + pub podcasts: PodcastsTab, /// The one federated artist card being viewed (name + loading state); /// opening another card replaces it. pub fed_artist_view: Option<(String, Loadable)>, @@ -2181,7 +2391,13 @@ fn fed_release_slot_availability( } } +/// Audio content id of a music track (library or federated). Podcast +/// episodes have none: they are never content-addressed, resolved by hash +/// or fetched from peers. pub fn track_content_id(track: &TrackItem) -> Option { + if track.is_podcast() { + return None; + } track .content_id .as_deref() @@ -2195,12 +2411,24 @@ pub fn track_content_id(track: &TrackItem) -> Option { }) } +/// Identity of a queue item for matching it across queue edits, shuffle, +/// resolution and playback state. +/// +/// Podcast episodes are keyed by their episode id only (`ep:…`, from +/// `TrackItem::key`): all of them share the sentinel numeric id, which must +/// never identify anything. Music is keyed content-first (a pending +/// federated item and its library copy are the same track), then by its +/// federated source, then by `TrackItem::key` (`lib:` for library +/// rows, `tmp:` for process-local placeholders). pub fn track_key(track: &TrackItem) -> String { + if track.is_podcast() { + return track.key().to_string(); + } if let Some(content_id) = track_content_id(track) { return format!("content:{content_id}"); } if let Some(fed) = &track.fed { return format!("fed:{}:{}", fed.owner, fed.item_id); } - format!("local:{}", track.id) + track.key().to_string() } diff --git a/src/app/update.rs b/src/app/update.rs index 2d091c9..6fc379e 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -20,8 +20,12 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit"; pub enum Effect { CheckUpdate, InstallUpdate(crate::updater::Update), - /// (Re)start playback of `queue[queue_pos]`. + /// (Re)start playback of `queue[queue_pos]` (a podcast episode resumes + /// from its saved position). PlayCurrent, + /// Play `queue[queue_pos]`, a podcast episode, from the beginning as an + /// explicit "start over". + PlayCurrentFromStart, TogglePause, StopPlayback, /// Seek relative to the current position, in seconds. @@ -106,6 +110,32 @@ pub enum Effect { }, /// Load qualified listening history without blocking the UI thread. LoadListenHistory, + /// Reload the subscribed podcasts with their fetch state. + PodcastsReload, + /// Load the season groups of a podcast. + PodcastLoadSeasons(music_dht::podcast::PodcastId), + /// Load one page of episodes; `seq` drops stale results. + PodcastLoadEpisodes { + seq: u64, + query: furumi_library::podcasts::EpisodeQuery, + }, + /// Fetch a feed the user typed and subscribe to it. + PodcastSubscribe(String), + /// Check feeds for new episodes (`manual`: asked by the user). + PodcastRefresh { + targets: Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>, + manual: bool, + }, + /// Unsubscribe; progress and history are kept. + PodcastUnsubscribe { + podcast_id: music_dht::podcast::PodcastId, + title: String, + }, + /// Mark an episode played or unplayed (an explicit progress action). + PodcastSetPlayed { + episode_id: music_dht::podcast::EpisodeId, + played: bool, + }, } pub fn update(state: &mut AppState, action: Action) -> Option { @@ -115,6 +145,14 @@ pub fn update(state: &mut AppState, action: Action) -> Option { .take() .is_some_and(|deadline| Instant::now() <= deadline); state.status_message = None; + if state.active_tab == Tab::Podcasts + && !state.help_visible + && !state.visualizer.active + && !state.additional_settings_open + && let Some(effect) = podcasts::handle(state, &action) + { + return effect; + } if state.additional_settings_open && !matches!( action, @@ -352,7 +390,14 @@ pub fn update(state: &mut AppState, action: Action) -> Option { let mut local_tracks: Vec = Vec::new(); let mut fed_tracks: Vec = Vec::new(); let mut seen = std::collections::HashSet::new(); + let mut episodes = 0usize; for track in tracks { + // Likes are music library state; episodes have their own + // "played" progress instead. + if track.is_podcast() { + episodes += 1; + continue; + } if !seen.insert(track_key(&track)) { continue; } @@ -365,7 +410,11 @@ pub fn update(state: &mut AppState, action: Action) -> Option { } } if local_tracks.is_empty() && fed_tracks.is_empty() { - state.status_message = Some("no track selected".into()); + state.status_message = Some(if episodes > 0 { + PODCAST_NOT_MUSIC.into() + } else { + "no track selected".into() + }); None } else { let should_like = local_tracks.iter().any(|track| !state.track_liked(track)) @@ -411,6 +460,9 @@ pub fn update(state: &mut AppState, action: Action) -> Option { Action::GoToRelease => { let track = selected_track(state).or_else(|| state.player.current.clone()); match track { + Some(track) if track.is_podcast() => { + return podcasts::reveal_episode_podcast(state, &track); + } Some(track) => open_release_for_track(state, &track), None => state.status_message = Some("no track selected".into()), } @@ -418,12 +470,17 @@ pub fn update(state: &mut AppState, action: Action) -> Option { } Action::AddToPlaylist => { let target = selected_playlist_target(state, true); + let target = without_podcast_episodes(state, target); match target { Some(target) => { state.popup = Some(super::state::Popup::AddToPlaylist { target, cursor: 0 }); state.track_selection.clear(); } - None => state.status_message = Some("no track selected".into()), + None => { + state + .status_message + .get_or_insert_with(|| "no track selected".into()); + } } None } @@ -442,7 +499,8 @@ pub fn update(state: &mut AppState, action: Action) -> Option { } } Action::NewPlaylist => { - let for_target = selected_playlist_target(state, false); + let for_target = + without_podcast_episodes(state, selected_playlist_target(state, false)); state.popup = Some(super::state::Popup::NewPlaylist { for_target, input: crate::app::input::LineEdit::default(), @@ -475,6 +533,21 @@ pub fn update(state: &mut AppState, action: Action) -> Option { None } Action::DeleteSelected => delete_selected(state), + Action::RestartEpisode => podcasts::restart_current(state), + Action::AddPodcast + | Action::SearchEpisodes + | Action::RefreshPodcast + | Action::RefreshAllPodcasts + | Action::CycleEpisodeSort + | Action::CycleEpisodeFilter + | Action::ToggleEpisodePlayed + | Action::UnsubscribePodcast => { + state.status_message = Some(format!( + "open the Podcasts tab ({}) for this command", + Tab::Podcasts.index() + 1 + )); + None + } } } @@ -489,7 +562,9 @@ fn current_track_for_info(state: &AppState) -> Option { queued_at_position .filter(|queued| { super::state::track_key(queued) == current_key - || queued.id == current.id + // Podcast episodes share the sentinel id; only their key + // identifies them. + || (!current.is_podcast() && !queued.is_podcast() && queued.id == current.id) || current.is_fed_pending() }) .or_else(|| { @@ -542,6 +617,36 @@ fn open_track_info( } } +/// Status text when a music-only action meets a podcast episode. +pub const PODCAST_NOT_MUSIC: &str = + "podcast episodes are not music: likes, playlists and library actions do not apply"; + +/// Playlists hold music only: drops podcast episodes from a local target +/// (and says so when nothing is left). +fn without_podcast_episodes( + state: &mut AppState, + target: Option, +) -> Option { + use super::state::PlaylistAddTarget; + match target { + Some(PlaylistAddTarget::Local(tracks)) => { + let total = tracks.len(); + let music: Vec = tracks + .into_iter() + .filter(|track| !track.is_podcast()) + .collect(); + if music.is_empty() { + if total > 0 { + state.status_message = Some(PODCAST_NOT_MUSIC.into()); + } + return None; + } + Some(PlaylistAddTarget::Local(music)) + } + other => other, + } +} + fn selected_playlist_target( state: &AppState, include_current: bool, @@ -629,6 +734,10 @@ fn open_edit_popup(state: &mut AppState) { return; } if let Some(track) = selected_track(state).or_else(|| state.player.current.clone()) { + if track.is_podcast() { + state.status_message = Some("podcast details come from the RSS feed".into()); + return; + } state.popup = Some(track_edit_popup(&track)); return; } @@ -1371,7 +1480,8 @@ pub fn selected_track(state: &AppState) -> Option { .map(|track| (*track).clone()) } Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(), - Tab::Podcasts | Tab::Federation | Tab::Logs => None, + Tab::Podcasts => state.podcasts.selected_episode_track(), + Tab::Federation | Tab::Logs => None, } } @@ -1390,8 +1500,14 @@ pub fn playlist_tracks(state: &AppState, id: i64) -> Option> { } } +/// Whether the library source mode lets a queue item stay in a received +/// queue. Only pending federated music depends on it: podcast episodes +/// (streamed from their publisher) and Jam private placeholders are never +/// dropped, or a received queue would lose them. pub(crate) fn track_allowed_by_source_mode(state: &AppState, track: &TrackItem) -> bool { - state.global.filters.source_mode.includes_network() || !track.is_fed_pending() + track.is_podcast() + || state.global.filters.source_mode.includes_network() + || !track.is_fed_pending() } /// A *release* under the cursor (artist-view tile/row or a search release). @@ -1427,6 +1543,9 @@ fn selected_release_id(state: &AppState) -> Option { /// an async fetch effect. fn queue_add(state: &mut AppState, next: bool) -> Option { let tracks = selected_tracks(state); + if tracks.iter().any(TrackItem::is_podcast) && !podcasts::episodes_play_here(state) { + return None; + } if !tracks.is_empty() { let count = tracks.len(); let title = tracks[0].title.clone(); @@ -3250,7 +3369,8 @@ fn reset_tab(state: &mut AppState, tab: Tab) { state.logs.follow = true; state.logs.selected_seq = None; } - Tab::Queue | Tab::Podcasts => {} + Tab::Queue => {} + Tab::Podcasts => podcasts::close(state), } } @@ -3258,6 +3378,8 @@ fn not_yet(state: &mut AppState, what: &str) { state.status_message = Some(format!("{what}: coming in a later milestone")); } +pub(crate) mod podcasts; + #[cfg(test)] #[path = "update_tests.rs"] mod tests; diff --git a/src/app/update/podcasts.rs b/src/app/update/podcasts.rs new file mode 100644 index 0000000..7679718 --- /dev/null +++ b/src/app/update/podcasts.rs @@ -0,0 +1,975 @@ +//! Podcasts tab state transitions. +//! +//! Pure functions over `AppState`: they request store, HTTP and sync work +//! through [`Effect`]s (executed by `app::podcast_runtime`) and fold the +//! results back in when the runtime reports them. + +use furumi_library::podcasts::{ + EpisodeListItem, EpisodeQuery, EpisodeSort, PlayedFilter, SeasonFilter, SeasonSummary, +}; +use music_dht::podcast::{EpisodeId, FeedUrl, PodcastId, ProgressRecord, ProgressStatus}; + +use super::Effect; +use crate::app::action::Action; +use crate::app::input::LineEdit; +use crate::app::state::track_key; +use crate::app::state::{ + AppState, EpisodeList, FedInputField, Loadable, OpenedPodcast, PODCAST_ACTION_ROWS, + PODCAST_EPISODE_PAGE, PodcastListRow, PodcastRow, PodcastStoreStatus, Popup, SeasonRow, Tab, +}; +use crate::library::models::TrackItem; +use crate::podcasts::refresh::{RefreshReport, SubscribeReport}; + +/// The next page starts loading when the cursor is this close to the end. +const LOAD_MORE_MARGIN: usize = 20; + +/// Podcasts-tab handling of `action`; `None` lets the global handler run. +pub(super) fn handle(state: &mut AppState, action: &Action) -> Option> { + let effect = match action { + Action::MoveUp => move_by(state, -1), + Action::MoveDown => move_by(state, 1), + Action::PageUp => move_by(state, -super::page_step(state)), + Action::PageDown => move_by(state, super::page_step(state)), + Action::SelectFirst => move_by(state, isize::MIN), + Action::SelectLast => move_by(state, isize::MAX), + Action::Select => select(state), + Action::Back if state.podcasts.opened.is_some() => { + back(state); + None + } + Action::OpenSearch | Action::SearchEpisodes => { + open_search(state); + None + } + Action::DeleteSelected | Action::UnsubscribePodcast => { + confirm_unsubscribe(state); + None + } + Action::EditSelected => { + state.status_message = Some("podcast details come from the RSS feed".into()); + None + } + Action::AddPodcast => { + open_add(state); + None + } + Action::RefreshPodcast => refresh_current(state), + Action::RefreshAllPodcasts => refresh_all(state), + Action::CycleEpisodeSort => cycle_sort(state), + Action::CycleEpisodeFilter => cycle_played_filter(state), + Action::ToggleEpisodePlayed => toggle_played(state), + Action::RestartEpisode => restart_selected(state), + _ => return None, + }; + Some(effect) +} + +/// Whether podcast actions may run; explains why not in the status bar. +fn store_ready(state: &mut AppState) -> bool { + match &state.podcasts.store { + PodcastStoreStatus::Ready => true, + PodcastStoreStatus::Opening => { + state.status_message = Some("podcasts: the podcast database is still opening".into()); + false + } + PodcastStoreStatus::Failed(err) => { + state.status_message = Some(format!("podcasts are unavailable: {err}")); + false + } + } +} + +// --------------------------------------------------------------------------- +// Navigation + +fn move_by(state: &mut AppState, delta: isize) -> Option { + let tab = &mut state.podcasts; + let len = match &tab.opened { + None => tab.list_rows().len(), + Some(opened) if opened.season.is_none() => tab.season_rows().len(), + Some(_) => tab.opened_episodes().map_or(0, |list| list.items.len()), + }; + if len == 0 { + return None; + } + let target = |cursor: usize| { + (cursor as isize) + .saturating_add(delta) + .clamp(0, len as isize - 1) as usize + }; + match &mut tab.opened { + None => tab.selected = target(tab.selected), + Some(opened) => match (opened.season, &mut opened.season_cursor) { + (None, Some(cursor)) => *cursor = target(*cursor), + (None, None) => {} + (Some(_), _) => opened.episode_cursor = target(opened.episode_cursor), + }, + } + load_more_if_needed(state) +} + +/// Requests the next episode page when the cursor nears the loaded end. +fn load_more_if_needed(state: &mut AppState) -> Option { + let cursor = state.podcasts.opened.as_ref()?.episode_cursor; + state.podcasts.opened_episodes()?; + let list = state.podcasts.episodes.as_mut()?; + if list.complete + || list.loading + || list.loading_more + || cursor + LOAD_MORE_MARGIN < list.items.len() + { + return None; + } + list.loading_more = true; + let mut query = list.query.clone(); + query.offset = list.items.len(); + Some(Effect::PodcastLoadEpisodes { + seq: list.seq, + query, + }) +} + +fn select(state: &mut AppState) -> Option { + let Some(opened) = state.podcasts.opened.clone() else { + return match state + .podcasts + .list_rows() + .get(state.podcasts.selected) + .copied()? + { + PodcastListRow::Add => { + open_add(state); + None + } + PodcastListRow::RefreshAll => refresh_all(state), + PodcastListRow::Podcast(index) => open_podcast(state, index), + }; + }; + if opened.season.is_none() { + let cursor = opened.season_cursor.unwrap_or(0); + let season = match state.podcasts.season_rows().get(cursor).copied()? { + SeasonRow::All => SeasonFilter::Any, + SeasonRow::Group(index) => match state.podcasts.opened_seasons()?.get(index)?.season { + Some(number) => SeasonFilter::Season(number), + None => SeasonFilter::NoSeason, + }, + }; + if let Some(opened) = &mut state.podcasts.opened { + opened.season = Some(season); + opened.episode_cursor = 0; + } + state.podcasts.search.clear(); + return Some(load_episodes(state, None)); + } + let track = state.podcasts.selected_episode_track()?; + play_episode_now(state, track, false) +} + +// --------------------------------------------------------------------------- +// Playback + +/// Whether podcast episodes may be added to the queue that plays. +/// +/// On this device, always. A device controlling another one sends the +/// queue there, so that device must speak sync v4 (an older one would drop +/// the episodes). A Jam guest never adds episodes to the host's queue. +/// Says why and returns `false` otherwise. +pub fn episodes_play_here(state: &mut AppState) -> bool { + use crate::app::state::DevicePlaybackRole; + match state.device_playback.role { + DevicePlaybackRole::Jam if !state.device_playback.jam_host => { + state.status_message = + Some("podcasts: Jam guests cannot add podcast episodes to the host's queue".into()); + false + } + DevicePlaybackRole::Control => { + let target = state.device_playback.active_device_id.clone(); + let version = target.as_deref().and_then(|target| { + state + .federation + .devices + .as_ref()? + .devices + .iter() + .find(|row| row.device_id == target) + .map(|row| row.protocol_version) + }); + if version.is_some_and(music_dht::device_sync::supports_podcasts) { + return true; + } + state.status_message = Some(format!( + "podcasts: {} needs a Furumi update to play podcast episodes", + state.device_playback.active_label() + )); + false + } + _ => true, + } +} + +/// Enter on an episode: play it now, right after the current item, keeping +/// the rest of the queue. Ordinary Play resumes from the saved position; +/// `restart` is the explicit "start over". +pub(super) fn play_episode_now( + state: &mut AppState, + track: TrackItem, + restart: bool, +) -> Option { + if !episodes_play_here(state) { + return None; + } + let key = track_key(&track); + let player = &mut state.player; + if let Some(index) = player + .current + .as_ref() + .filter(|current| track_key(current) == key) + .and_then(|_| { + if player + .queue + .get(player.queue_pos) + .is_some_and(|queued| track_key(queued) == key) + { + Some(player.queue_pos) + } else { + player + .queue + .iter() + .position(|queued| track_key(queued) == key) + } + }) + { + // Already the current item. + player.queue_pos = index; + if restart { + return Some(Effect::PlayCurrentFromStart); + } + if player.playing && player.paused { + player.paused = false; + return Some(Effect::TogglePause); + } + if player.playing { + state.status_message = Some("podcasts: this episode is playing".into()); + return None; + } + return Some(Effect::PlayCurrent); + } + let len = player.queue.len(); + let at = if player.current.is_some() { + (player.queue_pos + 1).min(len) + } else { + player.queue_pos.min(len) + }; + player.queue.insert(at, track); + if let Some(end) = player.play_next_end + && end >= at + { + player.play_next_end = Some(end + 1); + } + player.prefetched_pos = None; + player.queue_pos = at; + super::normalize_play_next_block(player); + Some(if restart { + Effect::PlayCurrentFromStart + } else { + Effect::PlayCurrent + }) +} + +/// `b` on the episode list: play the selected episode from the beginning. +fn restart_selected(state: &mut AppState) -> Option { + match state.podcasts.selected_episode_track() { + Some(track) => play_episode_now(state, track, true), + None => restart_current(state), + } +} + +/// "Start over" for the episode that is playing. +pub fn restart_current(state: &mut AppState) -> Option { + let Some(track) = state.player.current.clone().filter(TrackItem::is_podcast) else { + state.status_message = Some("podcasts: select an episode or play one first".into()); + return None; + }; + play_episode_now(state, track, true) +} + +/// The progress writer saved (or found) a progress register: show it in +/// the episode list without reloading it, and carry its generation in the +/// queue items of that episode (a handoff passes it on). +pub fn progress_saved(state: &mut AppState, record: ProgressRecord) -> Vec { + if let Some(list) = state.podcasts.episodes.as_mut() { + for item in &mut list.items { + if item.episode.episode_id == record.episode_id { + item.progress = Some(record.clone()); + } + } + } + let player = &mut state.player; + for track in player.queue.iter_mut().chain(player.current.as_mut()) { + if let Some(episode) = track.podcast.as_mut() + && episode.episode_id == record.episode_id + { + episode.progress_generation = Some( + episode + .progress_generation + .unwrap_or(0) + .max(record.generation), + ); + } + } + Vec::new() +} + +/// Podcast state changed by other devices (received operations or +/// snapshot sections): progress registers update the list in place, the +/// rest reloads what is on screen. +pub fn synced(state: &mut AppState, progress: Vec, changed: bool) -> Vec { + let mut effects = Vec::new(); + for record in progress { + effects.extend(progress_saved(state, record)); + } + if state.podcasts.store != PodcastStoreStatus::Ready { + return effects; + } + // Counts in the list (unplayed, in progress) follow both. + effects.push(Effect::PodcastsReload); + if changed { + if let Some(opened) = &state.podcasts.opened + && opened.season_cursor.is_some() + { + effects.push(Effect::PodcastLoadSeasons(opened.podcast_id.clone())); + } + effects.extend(reload_episodes(state)); + } + effects +} + +fn open_podcast(state: &mut AppState, index: usize) -> Option { + let row = state.podcasts.podcasts().get(index)?; + let podcast_id = row.id().clone(); + let has_seasons = row.summary.season_count > 0; + state.podcasts.opened = Some(OpenedPodcast { + podcast_id: podcast_id.clone(), + season_cursor: has_seasons.then_some(0), + season: (!has_seasons).then_some(SeasonFilter::Any), + episode_cursor: 0, + }); + state.podcasts.search.clear(); + if has_seasons { + state.podcasts.seasons = Some((podcast_id.clone(), Loadable::Loading)); + Some(Effect::PodcastLoadSeasons(podcast_id)) + } else { + Some(load_episodes(state, None)) + } +} + +/// Shift-J (or the info popup's artist jump) on a podcast episode: opens its +/// podcast on the Podcasts tab instead of a music release or artist. +pub fn reveal_episode_podcast(state: &mut AppState, track: &TrackItem) -> Option { + let episode = track.podcast_episode_ref()?; + let podcast_id = episode.podcast_id.clone(); + let title = episode.podcast_title.clone(); + state.active_tab = Tab::Podcasts; + state.help_visible = false; + state.track_selection.clear(); + state.jump_origin = None; + if !store_ready(state) { + return None; + } + if state + .podcasts + .opened + .as_ref() + .is_some_and(|opened| opened.podcast_id == podcast_id) + { + return None; + } + close(state); + match state + .podcasts + .podcasts() + .iter() + .position(|row| *row.id() == podcast_id) + { + Some(index) => { + state.podcasts.selected = index + PODCAST_ACTION_ROWS; + open_podcast(state, index) + } + None => { + state.status_message = Some(format!( + "podcasts: {} is not subscribed on this device", + display_title(&title) + )); + None + } + } +} + +/// Esc: episodes -> seasons (when the podcast has them) -> podcast list. +fn back(state: &mut AppState) { + let tab = &mut state.podcasts; + match &mut tab.opened { + Some(opened) if opened.season.is_some() && opened.season_cursor.is_some() => { + opened.season = None; + tab.search.clear(); + } + Some(_) => close(state), + None => {} + } +} + +/// Closes the opened podcast and puts the list cursor on it. +pub(super) fn close(state: &mut AppState) { + let tab = &mut state.podcasts; + if let Some(opened) = tab.opened.take() + && let Some(index) = tab + .podcasts() + .iter() + .position(|row| *row.id() == opened.podcast_id) + { + tab.selected = index + PODCAST_ACTION_ROWS; + } + tab.search.clear(); +} + +// --------------------------------------------------------------------------- +// Episode queries + +/// Starts loading the first page of the opened group with the current +/// search, filter and sort. `focus` keeps the cursor on that episode when +/// it is still listed (reloads after a refresh); otherwise the cursor goes +/// to the top. +fn load_episodes(state: &mut AppState, focus: Option) -> Effect { + let tab = &mut state.podcasts; + let opened = tab + .opened + .as_mut() + .expect("load_episodes needs an opened podcast"); + let season = opened.season.unwrap_or_default(); + let search = tab.search.trim(); + let query = EpisodeQuery { + podcast_id: opened.podcast_id.clone(), + season, + played: tab.played, + search: (!search.is_empty()).then(|| search.to_string()), + sort: tab.sort, + limit: Some(PODCAST_EPISODE_PAGE), + offset: 0, + }; + if focus.is_none() { + opened.episode_cursor = 0; + } + tab.episode_seq += 1; + let seq = tab.episode_seq; + // Keep showing the previous rows of the same group while the new query + // runs, so typing a search does not flash an empty list. + let items = match tab.episodes.take() { + Some(list) if list.query.podcast_id == query.podcast_id && list.query.season == season => { + list.items + } + _ => Vec::new(), + }; + tab.episodes = Some(EpisodeList { + seq, + query: query.clone(), + items, + loading: true, + loading_more: false, + complete: false, + error: None, + focus, + }); + Effect::PodcastLoadEpisodes { seq, query } +} + +/// Reloads the visible episodes, keeping the cursor on the same episode. +fn reload_episodes(state: &mut AppState) -> Option { + if !state.podcasts.showing_episodes() { + return None; + } + let focus = state + .podcasts + .selected_episode() + .map(|item| item.episode.episode_id.clone()); + Some(load_episodes(state, focus)) +} + +/// Live search: called for every edit of the search field. +pub fn set_search(state: &mut AppState, search: String) -> Option { + if state.podcasts.search == search { + return None; + } + state.podcasts.search = search; + state + .podcasts + .showing_episodes() + .then(|| load_episodes(state, None)) +} + +fn open_search(state: &mut AppState) { + if !state.podcasts.showing_episodes() { + state.status_message = Some("podcasts: open a podcast to search its episodes".into()); + return; + } + state.popup = Some(Popup::FedInput { + field: FedInputField::PodcastSearch, + input: LineEdit::new(state.podcasts.search.clone()), + }); +} + +fn cycle_sort(state: &mut AppState) -> Option { + let next = match state.podcasts.sort { + EpisodeSort::NewestFirst => EpisodeSort::OldestFirst, + EpisodeSort::OldestFirst => EpisodeSort::EpisodeNumber, + EpisodeSort::EpisodeNumber => EpisodeSort::EpisodeNumberDesc, + EpisodeSort::EpisodeNumberDesc => EpisodeSort::NewestFirst, + }; + state.podcasts.sort = next; + state.status_message = Some(format!("episodes: {}", sort_label(next))); + state + .podcasts + .showing_episodes() + .then(|| load_episodes(state, None)) +} + +fn cycle_played_filter(state: &mut AppState) -> Option { + let next = match state.podcasts.played { + PlayedFilter::Any => PlayedFilter::Unplayed, + PlayedFilter::Unplayed => PlayedFilter::InProgress, + PlayedFilter::InProgress => PlayedFilter::Played, + PlayedFilter::Played | PlayedFilter::NotStarted => PlayedFilter::Any, + }; + state.podcasts.played = next; + state.status_message = Some(format!("episodes: {}", played_label(next))); + state + .podcasts + .showing_episodes() + .then(|| load_episodes(state, None)) +} + +pub fn sort_label(sort: EpisodeSort) -> &'static str { + match sort { + EpisodeSort::NewestFirst => "newest first", + EpisodeSort::OldestFirst => "oldest first", + EpisodeSort::EpisodeNumber => "by episode number", + EpisodeSort::EpisodeNumberDesc => "by episode number, descending", + } +} + +pub fn played_label(filter: PlayedFilter) -> &'static str { + match filter { + PlayedFilter::Any => "all episodes", + PlayedFilter::Unplayed => "unplayed", + PlayedFilter::NotStarted => "not started", + PlayedFilter::InProgress => "in progress", + PlayedFilter::Played => "played", + } +} + +// --------------------------------------------------------------------------- +// Subscriptions + +fn open_add(state: &mut AppState) { + if !store_ready(state) { + return; + } + if state.podcasts.subscribing { + state.status_message = Some("podcasts: a subscription is already being added".into()); + return; + } + state.popup = Some(Popup::FedInput { + field: FedInputField::PodcastFeedUrl, + input: LineEdit::default(), + }); +} + +/// Enter in the "Add podcast" field. +pub fn submit_feed_url(state: &mut AppState, input: &str) -> Option { + let input = input.trim(); + if input.is_empty() { + state.status_message = Some("podcasts: the feed URL is empty".into()); + return None; + } + if !store_ready(state) { + return None; + } + if let Err(err) = FeedUrl::from_user_input(input) { + state.status_message = Some(format!("podcasts: {err}")); + return None; + } + state.podcasts.subscribing = true; + state.status_message = Some("podcasts: reading the feed…".into()); + Some(Effect::PodcastSubscribe(input.to_string())) +} + +fn confirm_unsubscribe(state: &mut AppState) { + if !store_ready(state) { + return; + } + let Some(row) = state.podcasts.current_podcast() else { + state.status_message = Some("podcasts: select a podcast to unsubscribe".into()); + return; + }; + state.popup = Some(Popup::ConfirmPodcastUnsubscribe { + podcast_id: row.id().clone(), + title: row.title().to_string(), + }); +} + +fn refresh_targets(state: &mut AppState, rows: Vec<&PodcastRow>) -> Vec<(PodcastId, FeedUrl)> { + rows.into_iter() + .filter(|row| !state.podcasts.refreshing.contains(row.id())) + .map(|row| (row.id().clone(), row.summary.meta.feed_url.clone())) + .collect() +} + +fn start_refresh( + state: &mut AppState, + targets: Vec<(PodcastId, FeedUrl)>, + manual: bool, +) -> Option { + if targets.is_empty() { + return None; + } + state + .podcasts + .refreshing + .extend(targets.iter().map(|(id, _)| id.clone())); + Some(Effect::PodcastRefresh { targets, manual }) +} + +/// Shift-R: the opened podcast or the one under the cursor. +fn refresh_current(state: &mut AppState) -> Option { + if !store_ready(state) { + return None; + } + let Some(row) = state.podcasts.current_podcast().cloned() else { + return refresh_all(state); + }; + if state.podcasts.refreshing.contains(row.id()) { + state.status_message = Some(format!("podcasts: already checking {}", row.title())); + return None; + } + let targets = refresh_targets(state, vec![&row]); + state.status_message = Some(format!("podcasts: checking {}…", row.title())); + start_refresh(state, targets, true) +} + +/// Ctrl-R / "Check all": every subscription. +fn refresh_all(state: &mut AppState) -> Option { + if !store_ready(state) { + return None; + } + let rows: Vec = state.podcasts.podcasts().to_vec(); + if rows.is_empty() { + state.status_message = Some("podcasts: no subscriptions yet".into()); + return None; + } + let targets = refresh_targets(state, rows.iter().collect()); + if targets.is_empty() { + state.status_message = Some("podcasts: already checking every feed".into()); + return None; + } + state.status_message = Some(format!( + "podcasts: checking {} feed(s) for new episodes…", + targets.len() + )); + start_refresh(state, targets, true) +} + +/// Scheduler: feeds the store reports as due (back-off already applied). +pub fn refresh_due(state: &mut AppState, due: Vec<(PodcastId, FeedUrl)>) -> Option { + let targets: Vec<_> = due + .into_iter() + .filter(|(id, _)| !state.podcasts.refreshing.contains(id)) + .collect(); + start_refresh(state, targets, false) +} + +fn toggle_played(state: &mut AppState) -> Option { + if !store_ready(state) { + return None; + } + let Some(item) = state.podcasts.selected_episode() else { + state.status_message = Some("podcasts: select an episode".into()); + return None; + }; + let played = item.status() != ProgressStatus::Completed; + Some(Effect::PodcastSetPlayed { + episode_id: item.episode.episode_id.clone(), + played, + }) +} + +// --------------------------------------------------------------------------- +// Results from the runtime + +/// The store opened (or failed to). +pub fn store_opened(state: &mut AppState, result: Result<(), String>) -> Option { + match result { + Ok(()) => { + state.podcasts.store = PodcastStoreStatus::Ready; + state.podcasts.list = Some(Loadable::Loading); + Some(Effect::PodcastsReload) + } + Err(err) => { + state.podcasts.store = PodcastStoreStatus::Failed(err); + None + } + } +} + +pub fn list_loaded(state: &mut AppState, result: Result, String>) { + let tab = &mut state.podcasts; + let previous = match (&tab.opened, tab.list_rows().get(tab.selected)) { + (None, Some(PodcastListRow::Podcast(index))) => { + tab.podcasts().get(*index).map(|row| row.id().clone()) + } + _ => None, + }; + match result { + Ok(rows) => tab.list = Some(Loadable::Ready(rows)), + Err(err) => { + tracing::warn!(%err, "podcast list load failed"); + if matches!(tab.list, Some(Loadable::Ready(_))) { + state.status_message = Some(format!("podcasts: {err}")); + return; + } + tab.list = Some(Loadable::Failed(err)); + return; + } + } + if let Some(id) = tab.pending_select.take().or(previous) + && let Some(index) = tab.podcasts().iter().position(|row| *row.id() == id) + { + tab.selected = index + PODCAST_ACTION_ROWS; + } + tab.selected = tab.selected.min(tab.list_rows().len().saturating_sub(1)); + // A podcast unsubscribed elsewhere closes. + if let Some(opened) = &tab.opened + && tab.podcast(&opened.podcast_id).is_none() + { + close(state); + } +} + +pub fn seasons_loaded( + state: &mut AppState, + podcast_id: PodcastId, + result: Result, String>, +) { + let tab = &mut state.podcasts; + if tab.opened.as_ref().map(|opened| &opened.podcast_id) != Some(&podcast_id) { + return; + } + let loaded = match result { + Ok(seasons) => Loadable::Ready(seasons), + Err(err) => { + if matches!(&tab.seasons, Some((id, Loadable::Ready(_))) if *id == podcast_id) { + state.status_message = Some(format!("podcasts: {err}")); + return; + } + Loadable::Failed(err) + } + }; + tab.seasons = Some((podcast_id, loaded)); + let rows = tab.season_rows().len(); + if let Some(OpenedPodcast { + season_cursor: Some(cursor), + .. + }) = &mut tab.opened + { + *cursor = (*cursor).min(rows.saturating_sub(1)); + } +} + +/// A page of episodes arrived. Stale sequences and pages that no longer +/// line up with the loaded rows are dropped. +pub fn episodes_loaded( + state: &mut AppState, + seq: u64, + offset: usize, + result: Result, String>, +) { + let tab = &mut state.podcasts; + let Some(list) = tab.episodes.as_mut().filter(|list| list.seq == seq) else { + return; + }; + match result { + Ok(page) => { + let complete = page.len() < PODCAST_EPISODE_PAGE; + if offset == 0 { + list.items = page; + list.loading = false; + } else if offset == list.items.len() && list.loading_more { + list.items.extend(page); + list.loading_more = false; + } else { + return; + } + list.complete = complete; + list.error = None; + } + Err(err) => { + tracing::warn!(%err, "podcast episodes load failed"); + list.loading = false; + list.loading_more = false; + list.error = Some(err); + return; + } + } + let focus = list.focus.take(); + let len = list.items.len(); + let focused = focus.and_then(|id| { + list.items + .iter() + .position(|item| item.episode.episode_id == id) + }); + if let Some(opened) = &mut tab.opened { + if let Some(index) = focused { + opened.episode_cursor = index; + } + opened.episode_cursor = opened.episode_cursor.min(len.saturating_sub(1)); + } +} + +pub fn subscribed(state: &mut AppState, result: Result) -> Option { + state.podcasts.subscribing = false; + match result { + Ok(report) => { + let title = display_title(&report.title); + state.status_message = Some(if report.already_subscribed { + format!("podcasts: already subscribed to {title}") + } else { + format!( + "podcasts: subscribed to {title} ({} episode(s); private)", + report.new_episodes + ) + }); + if state.podcasts.opened.is_some() { + close(state); + } + state.podcasts.pending_select = Some(report.podcast_id); + Some(Effect::PodcastsReload) + } + Err(err) => { + state.status_message = Some(format!("podcasts: cannot subscribe: {err}")); + None + } + } +} + +fn display_title(title: &str) -> String { + if title.trim().is_empty() { + "the podcast".to_string() + } else { + format!("\"{title}\"") + } +} + +pub fn refreshed( + state: &mut AppState, + podcast_id: PodcastId, + result: Result, + manual: bool, +) -> Vec { + state.podcasts.refreshing.remove(&podcast_id); + let title = state + .podcasts + .podcast(&podcast_id) + .map(|row| display_title(row.title())) + .unwrap_or_else(|| "the podcast".to_string()); + let mut effects = vec![Effect::PodcastsReload]; + match result { + Ok(report) => { + if report.new_episodes > 0 { + state.status_message = Some(format!( + "podcasts: {} new episode(s) in {title}", + report.new_episodes + )); + } else if manual && state.podcasts.refreshing.is_empty() { + state.status_message = Some("podcasts: no new episodes".into()); + } + let changed = report.new_episodes > 0 || report.updated_episodes > 0; + let opened_here = state + .podcasts + .opened + .as_ref() + .is_some_and(|opened| opened.podcast_id == podcast_id); + if changed && opened_here { + if state + .podcasts + .opened + .as_ref() + .is_some_and(|opened| opened.season_cursor.is_some()) + { + effects.push(Effect::PodcastLoadSeasons(podcast_id.clone())); + } + effects.extend(reload_episodes(state)); + } + } + Err(err) => { + tracing::warn!(podcast = %podcast_id, %err, "podcast refresh failed"); + if manual { + state.status_message = Some(format!("podcasts: {title}: {err}")); + } + } + } + effects +} + +pub fn unsubscribed( + state: &mut AppState, + podcast_id: PodcastId, + title: String, + result: Result<(), String>, +) -> Option { + match result { + Ok(()) => { + state.status_message = Some(format!( + "podcasts: unsubscribed from {}; progress and history are kept", + display_title(&title) + )); + if state + .podcasts + .opened + .as_ref() + .is_some_and(|opened| opened.podcast_id == podcast_id) + { + close(state); + } + Some(Effect::PodcastsReload) + } + Err(err) => { + state.status_message = Some(format!("podcasts: unsubscribe failed: {err}")); + None + } + } +} + +pub fn played_set(state: &mut AppState, played: bool, result: Result<(), String>) -> Vec { + match result { + Ok(()) => { + state.status_message = Some( + if played { + "podcasts: marked as played" + } else { + "podcasts: marked as unplayed" + } + .into(), + ); + let mut effects = vec![Effect::PodcastsReload]; + if let Some(opened) = &state.podcasts.opened + && opened.season_cursor.is_some() + { + effects.push(Effect::PodcastLoadSeasons(opened.podcast_id.clone())); + } + effects.extend(reload_episodes(state)); + effects + } + Err(err) => { + state.status_message = Some(format!("podcasts: {err}")); + Vec::new() + } + } +} diff --git a/src/app/update_tests.rs b/src/app/update_tests.rs index b4efc57..59812d2 100644 --- a/src/app/update_tests.rs +++ b/src/app/update_tests.rs @@ -128,6 +128,7 @@ fn test_track(id: i64) -> TrackItem { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, } } @@ -522,6 +523,7 @@ fn queue_advances_and_respects_repeat() { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; let mut state = AppState::default(); state.player.queue = vec![track(1), track(2)]; @@ -587,6 +589,7 @@ fn queue_tab_select_and_clear() { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; let mut state = AppState { active_tab: Tab::Queue, @@ -1062,6 +1065,7 @@ fn shuffle_reorders_tail_and_restores() { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; let mut state = AppState::default(); state.player.queue = (1..=8).map(track).collect(); @@ -1109,6 +1113,7 @@ fn shift_j_opens_release_from_queue() { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; let mut state = AppState { active_tab: Tab::Queue, @@ -1437,3 +1442,781 @@ fn card_refresh_animates_while_previous_results_stay_visible() { assert!(!render_enrichment_card(&state).contains("searching peers")); } } + +// --------------------------------------------------------------------------- +// Podcasts tab + +mod podcast_tab { + use furumi_library::podcasts::{EpisodeSort, PlayedFilter, SeasonFilter}; + use music_dht::podcast::{FeedUrl, PodcastId, ProgressStatus}; + + use super::*; + use crate::app::podcast_fixtures::{self as fx, podcast_id}; + use crate::app::state::{ + FedInputField, PODCAST_ACTION_ROWS, PODCAST_EPISODE_PAGE, PodcastStoreStatus, Popup, + }; + use crate::app::update::podcasts; + use crate::podcasts::refresh::{RefreshReport, SubscribeReport}; + + fn ready(rows: Vec) -> AppState { + let mut state = fx::ready_state(rows); + state.podcasts.selected = PODCAST_ACTION_ROWS; + state + } + + fn other_id() -> PodcastId { + PodcastId::from_feed_url(&FeedUrl::parse("https://other.test/feed.xml").unwrap()) + } + + fn episode_request(effect: Option) -> (u64, furumi_library::podcasts::EpisodeQuery) { + match effect { + Some(Effect::PodcastLoadEpisodes { seq, query }) => (seq, query), + other => panic!("expected an episode load, got {other:?}"), + } + } + + fn page(count: u32) -> Vec { + (1..=count) + .map(|n| fx::episode(n, &format!("Episode {n}"), None)) + .collect() + } + + #[test] + fn podcast_actions_wait_for_the_store() { + let mut state = AppState { + active_tab: Tab::Podcasts, + ..AppState::default() + }; + assert_eq!(update(&mut state, Action::AddPodcast), None); + assert!(state.popup.is_none()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("still opening")) + ); + state.podcasts.store = PodcastStoreStatus::Failed("newer schema".into()); + assert_eq!(update(&mut state, Action::RefreshAllPodcasts), None); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("unavailable: newer schema")) + ); + // Opening the store loads the list. + assert_eq!( + podcasts::store_opened(&mut state, Ok(())), + Some(Effect::PodcastsReload) + ); + assert_eq!(state.podcasts.store, PodcastStoreStatus::Ready); + } + + #[test] + fn podcast_actions_outside_the_tab_only_explain() { + let mut state = fx::ready_state(vec![fx::row("Show", 0, 1, 0)]); + state.active_tab = Tab::Global; + assert_eq!(update(&mut state, Action::RefreshAllPodcasts), None); + assert!(state.podcasts.refreshing.is_empty()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("Podcasts tab (4)")) + ); + } + + #[test] + fn podcast_opens_seasons_then_a_season_then_goes_back() { + let mut state = ready(vec![fx::row("Show", 1, 5, 1)]); + assert_eq!( + update(&mut state, Action::Select), + Some(Effect::PodcastLoadSeasons(podcast_id())) + ); + assert!(!state.podcasts.showing_episodes()); + podcasts::seasons_loaded(&mut state, podcast_id(), Ok(fx::seasons())); + // Rows: All, Season 1, Without a season. + assert_eq!(state.podcasts.season_rows().len(), 3); + update(&mut state, Action::MoveDown); + let (seq, query) = episode_request(update(&mut state, Action::Select)); + assert_eq!(query.season, SeasonFilter::Season(1)); + assert_eq!(query.limit, Some(PODCAST_EPISODE_PAGE)); + assert_eq!(query.offset, 0); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(3))); + update(&mut state, Action::MoveDown); + assert_eq!( + state + .podcasts + .selected_episode() + .map(|item| item.episode.title.as_str()), + Some("Episode 2") + ); + + // Esc: episodes -> seasons (cursor kept) -> list (on the podcast). + update(&mut state, Action::Back); + let opened = state.podcasts.opened.as_ref().unwrap(); + assert_eq!((opened.season, opened.season_cursor), (None, Some(1))); + update(&mut state, Action::Back); + assert!(state.podcasts.opened.is_none()); + assert_eq!(state.podcasts.selected, PODCAST_ACTION_ROWS); + // A late seasons result for the closed podcast is ignored. + podcasts::seasons_loaded(&mut state, podcast_id(), Ok(Vec::new())); + assert!(state.podcasts.opened_seasons().is_none()); + } + + #[test] + fn podcast_without_seasons_opens_its_episodes_and_tab_key_resets() { + let mut state = ready(vec![fx::row("Show", 1, 2, 0)]); + let (_, query) = episode_request(update(&mut state, Action::Select)); + assert_eq!(query.season, SeasonFilter::Any); + assert!(state.podcasts.showing_episodes()); + // Pressing the tab's number again returns to the list. + update(&mut state, Action::GoToTab(Tab::Podcasts.index())); + assert!(state.podcasts.opened.is_none()); + } + + #[test] + fn live_search_drops_stale_pages_and_keeps_the_rows_meanwhile() { + let mut state = ready(vec![fx::row("Show", 1, 3, 0)]); + let (first, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, first, 0, Ok(page(3))); + + update(&mut state, Action::SearchEpisodes); + assert!(matches!( + state.popup, + Some(Popup::FedInput { + field: FedInputField::PodcastSearch, + .. + }) + )); + let (second, query) = episode_request(podcasts::set_search(&mut state, "pilot".into())); + assert_eq!(query.search.as_deref(), Some("pilot")); + assert!(second > first); + // The old rows stay visible while the search runs. + let list = state.podcasts.opened_episodes().unwrap(); + assert!(list.loading); + assert_eq!(list.items.len(), 3); + // Typing on: the previous request's result arrives late and is dropped. + let (third, _) = episode_request(podcasts::set_search(&mut state, "pilot 2".into())); + podcasts::episodes_loaded(&mut state, second, 0, Ok(page(2))); + assert_eq!(state.podcasts.opened_episodes().unwrap().items.len(), 3); + podcasts::episodes_loaded(&mut state, third, 0, Ok(page(1))); + let list = state.podcasts.opened_episodes().unwrap(); + assert_eq!(list.items.len(), 1); + assert!(!list.loading && list.complete); + // An unchanged search requests nothing. + assert_eq!(podcasts::set_search(&mut state, "pilot 2".into()), None); + // Search, filter and sort are per opened podcast view. + update(&mut state, Action::Back); + assert!(state.podcasts.search.is_empty()); + } + + #[test] + fn sort_and_played_filter_reload_from_the_top() { + let mut state = ready(vec![fx::row("Show", 1, 3, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(3))); + update(&mut state, Action::SelectLast); + let (_, query) = episode_request(update(&mut state, Action::CycleEpisodeSort)); + assert_eq!(query.sort, EpisodeSort::OldestFirst); + assert_eq!(state.podcasts.opened.as_ref().unwrap().episode_cursor, 0); + let (_, query) = episode_request(update(&mut state, Action::CycleEpisodeFilter)); + assert_eq!(query.played, PlayedFilter::Unplayed); + assert_eq!(query.sort, EpisodeSort::OldestFirst); + } + + #[test] + fn long_episode_lists_load_page_by_page() { + let mut state = ready(vec![fx::row("Show", 600, 600, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(PODCAST_EPISODE_PAGE as u32))); + assert!(!state.podcasts.opened_episodes().unwrap().complete); + let (more_seq, query) = episode_request(update(&mut state, Action::SelectLast)); + assert_eq!(more_seq, seq); + assert_eq!(query.offset, PODCAST_EPISODE_PAGE); + // No duplicate request while the page is in flight. + assert_eq!(update(&mut state, Action::MoveUp), None); + podcasts::episodes_loaded(&mut state, seq, PODCAST_EPISODE_PAGE, Ok(page(100))); + let list = state.podcasts.opened_episodes().unwrap(); + assert_eq!(list.items.len(), PODCAST_EPISODE_PAGE + 100); + assert!(list.complete); + assert_eq!(update(&mut state, Action::SelectLast), None); + } + + #[test] + fn manual_and_scheduled_refreshes_are_deduplicated() { + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + let url = FeedUrl::parse("https://example.test/feed.xml").unwrap(); + assert_eq!( + update(&mut state, Action::RefreshPodcast), + Some(Effect::PodcastRefresh { + targets: vec![(podcast_id(), url.clone())], + manual: true, + }) + ); + assert!(state.podcasts.refreshing.contains(&podcast_id())); + assert_eq!(update(&mut state, Action::RefreshPodcast), None); + assert_eq!(update(&mut state, Action::RefreshAllPodcasts), None); + // The scheduler skips feeds already in flight. + let due = vec![(podcast_id(), url.clone()), (other_id(), url.clone())]; + assert_eq!( + podcasts::refresh_due(&mut state, due), + Some(Effect::PodcastRefresh { + targets: vec![(other_id(), url)], + manual: false, + }) + ); + let effects = podcasts::refreshed( + &mut state, + podcast_id(), + Ok(RefreshReport { + new_episodes: 2, + ..RefreshReport::default() + }), + true, + ); + assert_eq!(effects, vec![Effect::PodcastsReload]); + assert!(!state.podcasts.refreshing.contains(&podcast_id())); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("2 new episode(s) in \"Show\"")) + ); + // A background failure stays out of the status bar. + state.status_message = None; + podcasts::refreshed(&mut state, other_id(), Err("HTTP 500".into()), false); + assert!(state.status_message.is_none()); + } + + #[test] + fn refreshing_the_opened_podcast_reloads_it_on_the_same_episode() { + let mut state = ready(vec![fx::row("Show", 0, 3, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(3))); + update(&mut state, Action::MoveDown); + let effects = podcasts::refreshed( + &mut state, + podcast_id(), + Ok(RefreshReport { + new_episodes: 1, + ..RefreshReport::default() + }), + false, + ); + assert_eq!(effects.len(), 2); + let (seq, _) = episode_request(effects.into_iter().nth(1)); + // A new episode arrived on top: the cursor follows "Episode 2". + let mut rows = vec![fx::episode(9, "Newest", None)]; + rows.extend(page(3)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(rows)); + assert_eq!( + state + .podcasts + .selected_episode() + .map(|item| item.episode.title.as_str()), + Some("Episode 2") + ); + } + + #[test] + fn adding_a_feed_validates_the_url_then_selects_the_new_podcast() { + let mut state = ready(Vec::new()); + state.podcasts.selected = 0; + update(&mut state, Action::Select); + assert!(matches!( + state.popup, + Some(Popup::FedInput { + field: FedInputField::PodcastFeedUrl, + .. + }) + )); + state.popup = None; + assert_eq!(podcasts::submit_feed_url(&mut state, " "), None); + assert_eq!( + podcasts::submit_feed_url(&mut state, "ftp://example.test/feed"), + None + ); + assert!(!state.podcasts.subscribing); + assert_eq!( + podcasts::submit_feed_url(&mut state, "example.test/feed.xml"), + Some(Effect::PodcastSubscribe("example.test/feed.xml".into())) + ); + assert!(state.podcasts.subscribing); + // A second add while one is running is refused. + update(&mut state, Action::AddPodcast); + assert!(state.popup.is_none()); + + let effect = podcasts::subscribed( + &mut state, + Ok(SubscribeReport { + podcast_id: podcast_id(), + title: "Show".into(), + already_subscribed: false, + new_episodes: 4, + }), + ); + assert_eq!(effect, Some(Effect::PodcastsReload)); + assert!(!state.podcasts.subscribing); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("subscribed to \"Show\" (4 episode(s); private)")) + ); + let mut other = fx::row("Another", 0, 1, 0); + other.summary.podcast_id = other_id(); + podcasts::list_loaded(&mut state, Ok(vec![other, fx::row("Show", 4, 4, 0)])); + assert_eq!(state.podcasts.selected, PODCAST_ACTION_ROWS + 1); + assert_eq!( + state.podcasts.current_podcast().map(|row| row.title()), + Some("Show") + ); + } + + #[test] + fn unsubscribing_asks_first_and_keeps_progress() { + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(1))); + update(&mut state, Action::UnsubscribePodcast); + assert!(matches!( + &state.popup, + Some(Popup::ConfirmPodcastUnsubscribe { podcast_id: id, title }) + if *id == podcast_id() && title == "Show" + )); + state.popup = None; + let effect = podcasts::unsubscribed(&mut state, podcast_id(), "Show".into(), Ok(())); + assert_eq!(effect, Some(Effect::PodcastsReload)); + assert!(state.podcasts.opened.is_none()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("progress and history are kept")) + ); + // Unsubscribed elsewhere: the reloaded list closes an open view. + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + update(&mut state, Action::Select); + podcasts::list_loaded(&mut state, Ok(Vec::new())); + assert!(state.podcasts.opened.is_none()); + } + + #[test] + fn marking_played_toggles_by_status() { + let mut state = ready(vec![fx::row("Show", 1, 2, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + let items = vec![ + fx::episode(1, "Done", Some((ProgressStatus::Completed, 0))), + fx::episode(2, "Half", Some((ProgressStatus::InProgress, 60_000))), + ]; + let done = items[0].episode.episode_id.clone(); + let half = items[1].episode.episode_id.clone(); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(items)); + assert_eq!( + update(&mut state, Action::ToggleEpisodePlayed), + Some(Effect::PodcastSetPlayed { + episode_id: done, + played: false, + }) + ); + update(&mut state, Action::MoveDown); + assert_eq!( + update(&mut state, Action::ToggleEpisodePlayed), + Some(Effect::PodcastSetPlayed { + episode_id: half, + played: true, + }) + ); + let effects = podcasts::played_set(&mut state, true, Ok(())); + assert_eq!(effects[0], Effect::PodcastsReload); + assert!(matches!( + effects.last(), + Some(Effect::PodcastLoadEpisodes { .. }) + )); + } +} + +/// Podcast episodes in the shared queue: identity by episode id (never the +/// shared sentinel id) and music-only actions that must skip them. +mod podcast_queue { + use super::*; + use crate::app::podcast_fixtures::{self as fx, episode_track}; + use crate::app::state::{PODCAST_ACTION_ROWS, Popup, RepeatMode, track_content_id}; + use crate::app::update::PODCAST_NOT_MUSIC; + use music_dht::podcast::ProgressStatus; + + fn keys(queue: &[TrackItem]) -> Vec { + queue.iter().map(track_key).collect() + } + + #[test] + fn episodes_are_keyed_by_episode_id_not_by_the_sentinel_id() { + let (one, two) = (episode_track(1), episode_track(2)); + assert_eq!(one.id, TrackItem::NOT_A_LIBRARY_ROW); + assert_eq!(one.id, two.id); + assert!(track_key(&one).starts_with("ep:ep1"), "{}", track_key(&one)); + assert_ne!(track_key(&one), track_key(&two)); + assert_eq!(track_key(&one), track_key(&episode_track(1))); + // Music keeps its content-first key; a library row without a + // content id is keyed by its row, never as `local:` / an episode. + assert!(track_key(&test_track(5)).starts_with("content:")); + let mut bare = test_track(5); + bare.content_id = None; + assert_eq!(track_key(&bare), "lib:5"); + } + + #[test] + fn removing_an_episode_keeps_the_other_episode_current() { + let mut state = AppState::default(); + state.player.queue = vec![episode_track(1), test_track(7), episode_track(2)]; + state.player.queue_pos = 2; + state.player.current = Some(episode_track(2)); + state.player.playing = true; + state.active_tab = Tab::Queue; + state.queue_tab.cursor = 0; + let effect = update(&mut state, Action::RemoveFromQueue); + assert_eq!( + effect, + Some(Effect::RemoveQueueIndices { + indices: vec![0], + restart_paused: None, + stop: false, + }) + ); + assert_eq!(state.player.queue_pos, 1); + assert_eq!( + state.player.current.as_ref().map(track_key), + Some(track_key(&episode_track(2))) + ); + } + + #[test] + fn removing_the_playing_episode_moves_to_the_next_item_not_another_episode() { + let mut state = AppState::default(); + state.player.queue = vec![episode_track(1), episode_track(2), test_track(7)]; + state.player.queue_pos = 0; + state.player.current = Some(episode_track(1)); + state.player.playing = true; + state.active_tab = Tab::Queue; + state.queue_tab.cursor = 0; + let effect = update(&mut state, Action::RemoveFromQueue); + assert!(matches!( + effect, + Some(Effect::RemoveQueueIndices { + restart_paused: Some(false), + .. + }) + )); + assert_eq!( + state.player.current.as_ref().map(track_key), + Some(track_key(&episode_track(2))) + ); + } + + #[test] + fn shuffle_and_repeat_keep_a_mixed_queue_intact() { + let mut state = AppState::default(); + let queue = vec![ + test_track(1), + episode_track(1), + episode_track(2), + test_track(2), + episode_track(3), + test_track(3), + ]; + let original = keys(&queue); + state.player.queue = queue; + state.player.current = Some(test_track(1)); + state.player.playing = true; + for _ in 0..8 { + update(&mut state, Action::ToggleShuffle); + let mut shuffled = keys(&state.player.queue); + shuffled.sort(); + let mut expected = original.clone(); + expected.sort(); + assert_eq!(shuffled, expected, "no item lost or duplicated"); + update(&mut state, Action::ToggleShuffle); + assert_eq!(keys(&state.player.queue), original); + } + // Next/previous step through episodes like any other item, and + // repeat-all wraps around. + state.player.repeat = RepeatMode::All; + state.player.queue_pos = 4; + state.player.current = Some(episode_track(3)); + assert_eq!( + update(&mut state, Action::NextTrack), + Some(Effect::PlayCurrent) + ); + assert_eq!(state.player.queue_pos, 5); + assert_eq!( + update(&mut state, Action::NextTrack), + Some(Effect::PlayCurrent) + ); + assert_eq!(state.player.queue_pos, 0); + assert_eq!( + update(&mut state, Action::PrevTrack), + Some(Effect::PlayCurrent) + ); + assert_eq!(state.player.queue_pos, 0); + } + + #[test] + fn current_track_info_finds_the_playing_episode_among_episodes() { + let mut state = AppState::default(); + state.player.queue = vec![episode_track(1), episode_track(2)]; + // The queue position points at another episode (same sentinel id). + state.player.queue_pos = 0; + state.player.current = Some(episode_track(2)); + state.player.playing = true; + update(&mut state, Action::OpenCurrentTrackInfo); + let Some(Popup::TrackInfo { tracks, .. }) = &state.popup else { + panic!("track info expected"); + }; + assert_eq!(track_key(&tracks[0]), track_key(&episode_track(2))); + } + + #[test] + fn music_only_actions_skip_episodes() { + let mut state = fx::ready_state(vec![fx::row("Show", 1, 2, 0)]); + state.active_tab = Tab::Queue; + state.player.queue = vec![episode_track(1)]; + state.player.current = Some(episode_track(1)); + state.player.playing = true; + + assert_eq!(update(&mut state, Action::ToggleLike), None); + assert_eq!(state.status_message.as_deref(), Some(PODCAST_NOT_MUSIC)); + assert_eq!(update(&mut state, Action::AddToPlaylist), None); + assert!(state.popup.is_none()); + assert_eq!(state.status_message.as_deref(), Some(PODCAST_NOT_MUSIC)); + update(&mut state, Action::NewPlaylist); + assert!(matches!( + state.popup.take(), + Some(Popup::NewPlaylist { + for_target: None, + .. + }) + )); + update(&mut state, Action::EditSelected); + assert!(state.popup.is_none(), "episode metadata is not editable"); + assert_eq!(update(&mut state, Action::DownloadSelected), None); + + // No content id, no share link, no music history; on the wire an + // explicit podcast source, never a music item. + let episode = episode_track(1); + assert_eq!(track_content_id(&episode), None); + assert_eq!(crate::share::track_content_id(&episode), None); + assert_eq!(crate::share::track_share_link(&episode), None); + let wire = ::from_track( + &episode, + ); + assert_eq!(wire.podcast_episode_ref(), episode.podcast_episode_ref()); + assert!(wire.file_path.is_empty()); + assert_eq!((wire.id, wire.content_id, wire.fed), (0, None, None)); + + // Shift-J opens the podcast instead of a music release. + let effect = update(&mut state, Action::GoToRelease); + assert_eq!(state.active_tab, Tab::Podcasts); + assert_eq!(state.podcasts.selected, PODCAST_ACTION_ROWS); + assert!(state.podcasts.opened.is_some()); + assert!(matches!(effect, Some(Effect::PodcastLoadEpisodes { .. }))); + } + + fn episodes_tab(statuses: &[Option<(music_dht::podcast::ProgressStatus, i64)>]) -> AppState { + let items = statuses + .iter() + .enumerate() + .map(|(index, status)| { + let n = index as u32 + 1; + fx::episode(n, &format!("Episode {n}"), *status) + }) + .collect(); + fx::with_episodes(fx::ready_state(vec![fx::row("Show", 2, 2, 0)]), items) + } + + #[test] + fn enter_plays_the_episode_right_after_the_current_item() { + let mut state = episodes_tab(&[None, None]); + state.player.queue = vec![test_track(1), test_track(2)]; + state.player.queue_pos = 0; + state.player.current = Some(test_track(1)); + state.player.playing = true; + state.podcasts.opened.as_mut().unwrap().episode_cursor = 1; + + assert_eq!( + update(&mut state, Action::Select), + Some(Effect::PlayCurrent) + ); + assert_eq!( + keys(&state.player.queue), + keys(&[test_track(1), episode_track(2), test_track(2)]) + ); + assert_eq!(state.player.queue_pos, 1); + let queued = state.player.queue[1].podcast_episode_ref().unwrap(); + assert_eq!(queued.podcast_title, "Show"); + assert_eq!(queued.audio_url, "https://example.test/2.mp3"); + + // Enter again on the playing episode does not queue it twice. + state.player.current = Some(state.player.queue[1].clone()); + assert_eq!(update(&mut state, Action::Select), None); + assert_eq!(state.player.queue.len(), 3); + state.player.paused = true; + assert_eq!( + update(&mut state, Action::Select), + Some(Effect::TogglePause) + ); + assert!(!state.player.paused); + // "Start over" restarts it in place. + assert_eq!( + update(&mut state, Action::RestartEpisode), + Some(Effect::PlayCurrentFromStart) + ); + assert_eq!(state.player.queue.len(), 3); + assert_eq!(state.player.queue_pos, 1); + } + + #[test] + fn start_over_plays_a_selected_episode_from_the_beginning() { + let mut state = episodes_tab(&[Some(( + music_dht::podcast::ProgressStatus::InProgress, + 600_000, + ))]); + assert_eq!( + update(&mut state, Action::RestartEpisode), + Some(Effect::PlayCurrentFromStart) + ); + assert_eq!(keys(&state.player.queue), keys(&[episode_track(1)])); + assert_eq!(state.player.queue_pos, 0); + // The queue item carries the generation the list showed (handoff + // and start-over order after it). + let queued = state.player.queue[0].podcast_episode_ref().unwrap(); + assert_eq!(queued.progress_generation, Some(1)); + // On another tab, `b` (if bound there) restarts the playing episode. + state.active_tab = Tab::Queue; + state.player.current = Some(state.player.queue[0].clone()); + state.player.playing = true; + assert_eq!( + update(&mut state, Action::RestartEpisode), + Some(Effect::PlayCurrentFromStart) + ); + state.player.current = Some(test_track(3)); + assert_eq!(update(&mut state, Action::RestartEpisode), None); + } + + #[test] + fn episodes_are_queued_for_v4_outputs_only() { + let mut state = episodes_tab(&[None, None]); + assert_eq!( + update(&mut state, Action::QueueAddLast), + Some(Effect::QueueOrderChanged { + restart_current: false + }) + ); + state.podcasts.opened.as_mut().unwrap().episode_cursor = 1; + update(&mut state, Action::QueueAddLast); + assert_eq!( + keys(&state.player.queue), + keys(&[episode_track(1), episode_track(2)]) + ); + + // Controlling a device that has not reported sync v4. + state.device_playback.role = crate::app::state::DevicePlaybackRole::Control; + state.device_playback.active_device_id = Some("dev_old".into()); + state.device_playback.active_device_name = Some("Old laptop".into()); + let row = |version| crate::devices::DeviceStatusRow { + device_id: "dev_old".into(), + name: "Old laptop".into(), + protocol_version: version, + ..Default::default() + }; + state.federation.devices = Some(crate::devices::DeviceSyncStatus { + devices: vec![row(3)], + ..Default::default() + }); + assert_eq!(update(&mut state, Action::QueueAddNext), None); + assert_eq!(update(&mut state, Action::Select), None); + assert_eq!(state.player.queue.len(), 2); + assert_eq!( + state.status_message.as_deref(), + Some("podcasts: Old laptop needs a Furumi update to play podcast episodes") + ); + // Once it reports v4, the queue goes there with the episodes. + state.federation.devices.as_mut().unwrap().devices = vec![row(4)]; + assert!(update(&mut state, Action::QueueAddNext).is_some()); + assert_eq!(state.player.queue.len(), 3); + + // A Jam guest never adds episodes to the host's queue. + state.device_playback.role = crate::app::state::DevicePlaybackRole::Jam; + state.device_playback.jam_host = false; + assert_eq!(update(&mut state, Action::QueueAddNext), None); + assert!( + state + .status_message + .as_deref() + .unwrap() + .contains("Jam guests") + ); + } + + #[test] + fn saved_progress_updates_the_episode_list_in_place() { + let mut state = episodes_tab(&[None, None]); + let mut record = fx::episode( + 2, + "Episode 2", + Some((music_dht::podcast::ProgressStatus::InProgress, 90_000)), + ) + .progress + .unwrap(); + record.generation = 4; + assert!(crate::app::update::podcasts::progress_saved(&mut state, record).is_empty()); + let items = &state.podcasts.episodes.as_ref().unwrap().items; + assert!(items[0].progress.is_none()); + assert_eq!(items[1].resume_position_ms(), 90_000); + assert_eq!(items[1].progress.as_ref().unwrap().generation, 4); + } + + #[test] + fn saved_progress_raises_the_generation_a_handoff_carries() { + let mut state = AppState::default(); + state.player.queue = vec![episode_track(1), episode_track(2), test_track(3)]; + state.player.current = Some(episode_track(2)); + let mut record = fx::episode(2, "Episode 2", Some((ProgressStatus::InProgress, 1))) + .progress + .unwrap(); + record.generation = 7; + crate::app::update::podcasts::progress_saved(&mut state, record.clone()); + let generation = |track: &TrackItem| track.podcast.as_ref().unwrap().progress_generation; + assert_eq!(generation(&state.player.queue[0]), None); + assert_eq!(generation(&state.player.queue[1]), Some(7)); + assert_eq!(generation(state.player.current.as_ref().unwrap()), Some(7)); + // Never lowered by an older register. + record.generation = 3; + crate::app::update::podcasts::progress_saved(&mut state, record); + assert_eq!(generation(&state.player.queue[1]), Some(7)); + } + + #[test] + fn podcast_changes_from_other_devices_reload_what_is_shown() { + let mut state = episodes_tab(&[None, None]); + let record = fx::episode(1, "Episode 1", Some((ProgressStatus::InProgress, 5_000))) + .progress + .unwrap(); + // A progress register is shown in place; the list counts reload. + let effects = crate::app::update::podcasts::synced(&mut state, vec![record], false); + assert_eq!(effects, [Effect::PodcastsReload]); + let items = &state.podcasts.episodes.as_ref().unwrap().items; + assert_eq!(items[0].resume_position_ms(), 5_000); + // Anything else reloads the opened podcast too. + let effects = crate::app::update::podcasts::synced(&mut state, Vec::new(), true); + assert_eq!(effects[0], Effect::PodcastsReload); + assert!( + effects + .iter() + .any(|effect| matches!(effect, Effect::PodcastLoadEpisodes { .. })), + "{effects:?}" + ); + // Before the store is ready there is nothing to reload. + let mut opening = AppState::default(); + assert!(crate::app::update::podcasts::synced(&mut opening, Vec::new(), true).is_empty()); + } +} diff --git a/src/config/default_keymap.toml b/src/config/default_keymap.toml index ace50f5..3935b3b 100644 --- a/src/config/default_keymap.toml +++ b/src/config/default_keymap.toml @@ -271,3 +271,51 @@ command = "OpenCommandLine" [[keymaps]] key_sequence = "/" command = "OpenSearch" + +# Podcasts tab. Enter opens a podcast, its seasons and episodes, and plays +# an episode (continuing where you stopped); Esc goes back. a / shift-a queue +# the episode; Shift-D also unsubscribes here. +[[keymaps]] +key_sequence = "o" +command = "AddPodcast" +context = "podcasts" + +[[keymaps]] +key_sequence = "/" +command = "SearchEpisodes" +context = "podcasts" + +[[keymaps]] +key_sequence = "shift-r" +command = "RefreshPodcast" +context = "podcasts" + +[[keymaps]] +key_sequence = "ctrl-r" +command = "RefreshAllPodcasts" +context = "podcasts" + +[[keymaps]] +key_sequence = "shift-s" +command = "CycleEpisodeSort" +context = "podcasts" + +[[keymaps]] +key_sequence = "f" +command = "CycleEpisodeFilter" +context = "podcasts" + +[[keymaps]] +key_sequence = "m" +command = "ToggleEpisodePlayed" +context = "podcasts" + +[[keymaps]] +key_sequence = "shift-u" +command = "UnsubscribePodcast" +context = "podcasts" + +[[keymaps]] +key_sequence = "b" +command = "RestartEpisode" +context = "podcasts" diff --git a/src/config/keymap.rs b/src/config/keymap.rs index b126271..c6e6fe4 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -890,6 +890,44 @@ mod tests { assert_eq!(KeyContext::Podcasts.label(), "podcasts"); } + #[test] + fn default_podcast_keys_resolve_only_in_the_podcasts_context() { + let mut km = keymap_from(DEFAULT_KEYMAP); + let shift = |c| KeyCombination::new(KeyCode::Char(c), KeyModifiers::SHIFT); + let cases = [ + (key!(o), Action::AddPodcast), + (key!('/'), Action::SearchEpisodes), + (shift('R'), Action::RefreshPodcast), + (key!(ctrl - r), Action::RefreshAllPodcasts), + (shift('S'), Action::CycleEpisodeSort), + (key!(f), Action::CycleEpisodeFilter), + (key!(m), Action::ToggleEpisodePlayed), + (shift('U'), Action::UnsubscribePodcast), + (key!(b), Action::RestartEpisode), + ]; + for (key, action) in cases { + assert_eq!( + km.resolve(key, KeyContext::Podcasts), + KeyResolution::Action(action.clone()), + "{action:?}" + ); + assert_ne!( + km.resolve(key, KeyContext::Library), + KeyResolution::Action(action.clone()), + "{action:?} leaks into the library" + ); + } + // Playback keys keep working on the Podcasts tab. + assert_eq!( + km.resolve(key!(space), KeyContext::Podcasts), + KeyResolution::Action(Action::PlayPause) + ); + assert_eq!( + km.resolve(key!(n), KeyContext::Podcasts), + KeyResolution::Action(Action::NextTrack) + ); + } + #[test] fn default_listening_history_key_resolves() { let mut km = keymap_from(DEFAULT_KEYMAP); diff --git a/src/devices.rs b/src/devices.rs index 60259c1..9e35cff 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -4,20 +4,42 @@ //! byte-stream ALPN. Local state is stored as an append-only operation log plus //! materialized tables, so offline clients can merge likes, playlists and //! membership changes deterministically. +//! +//! The wire types are the shared `music_dht::device_sync` definitions. This +//! client speaks protocol v4: podcast operations stay in the log, are +//! applied to the podcast store (`furumi_library::podcasts::PodcastStore`) +//! and are relayed; podcast snapshot sections are exported from and merged +//! into that store. Everything sent to a peer is gated by the version that +//! peer reported itself (`op_allowed_for_peer`, `SyncSnapshot::for_peer`, +//! `playback_allowed_for_peer`; a queue with podcast items reaches an older +//! peer only redacted, for its ownership engine, and the placeholders such a +//! peer echoes back as music are left out of what it sends: +//! `drop_echoed_private_items`). use music_dht::playback::{Checkpoint, CommandStamp, Config as PlaybackConfig, Engine}; use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; -use std::str::FromStr; use std::sync::Arc; use std::time::Duration; use anyhow::{Context as _, Result}; -use music_dht::device_sync::{ListenEvent, ListenTrackMetadata}; +use furumi_library::podcasts::{ + ApplyOutcome as PodcastApplyOutcome, OpStamp as PodcastOpStamp, PodcastLocalOp, PodcastStore, + StampSource as PodcastStampSource, +}; +use music_dht::device_sync::{ + DeviceProfileWire, InviteWire, ListenEvent, ListenTrackMetadata, SnapshotLike, + SnapshotLikeTombstone, SnapshotPlaylist, SnapshotPlaylistItem, SnapshotPlaylistItemTombstone, + SnapshotPlaylistTombstone, SyncOpWire, SyncSnapshot, WireMessage, encode_invite, finish_send, + hash_secret, random_hex, read_msg, ticket_endpoint_id, +}; +pub use music_dht::device_sync::{ + PlaybackCommand, PlaybackRepeat, PlaybackSnapshot, PlaybackStateWire, PlaybackTrack, + SyncOpPayload, SyncedFedTrack, +}; +use music_dht::podcast::ProgressRecord; use music_dht::{ByteStream, MusicDhtService, NetworkId, PeerTicket, SecretKey, StreamAcceptor}; use rusqlite::{Connection, OptionalExtension, params}; -use serde::{Deserialize, Serialize}; -use tokio::io::{AsyncRead, AsyncReadExt}; use crate::app::event::AppEvent; use crate::library::Library; @@ -25,13 +47,24 @@ use crate::library::models::{ArtistRef, TrackItem}; pub const SYNC_ALPN: &[u8] = b"furumi/sync/2"; const CLIENT_VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const PROTOCOL_VERSION: u16 = 3; +/// Device-sync protocol version of this client. +/// +/// Version 4 is announced because podcast operations, podcast snapshot +/// sections and podcast queue items are applied, kept and relayed (see the +/// module documentation). Review that support before following a newer +/// shared protocol version. +pub const PROTOCOL_VERSION: u16 = 4; +const _: () = assert!( + PROTOCOL_VERSION == music_dht::device_sync::DEVICE_SYNC_PROTOCOL_VERSION, + "review device-sync support before announcing a new protocol version" +); +/// Lowest protocol version of a peer that speaks `furumi/sync/2`. +const SYNC_V2_PROTOCOL_VERSION: u16 = 2; const INVITE_TTL_MS: i64 = 10 * 60 * 1000; const PAIRING_WAIT_MS: i64 = 5 * 60 * 1000; const PAIRING_RETRY_DELAY: Duration = Duration::from_secs(1); const RESPONSE_DRAIN_TIMEOUT: Duration = Duration::from_secs(2); const SYNC_INTERVAL: Duration = Duration::from_secs(2); -const MAX_LINE: usize = 8 * 1024 * 1024; /// Playback control is ephemeral. Keeping old full-queue commands in a new /// peer's catch-up batch can make the initial pairing frame arbitrarily large. const PLAYBACK_COMMAND_TTL_MS: i64 = 5 * 60 * 1_000; @@ -52,6 +85,8 @@ pub struct DeviceStatusRow { pub device_id: String, pub name: String, pub client_version: String, + /// Device-sync protocol version the device reported itself. + pub protocol_version: u16, pub endpoint_id: String, pub last_seen_ms: Option, pub revoked: bool, @@ -90,80 +125,185 @@ pub struct DeviceSync { library: Arc, event_tx: Arc>>>, playback: Arc>, + podcasts: Arc>, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] -#[serde(rename_all = "snake_case")] -pub enum PlaybackRepeat { - #[default] - Off, - One, - All, +/// The podcast store as device sync sees it. +/// +/// The store opens in the background after device sync started. Podcast +/// operations received before that (or whose application failed) are kept +/// in `sync_podcast_pending` and applied when the store is attached. +#[derive(Default)] +struct PodcastLink { + store: Option>, + /// Progress registers that received operations changed since the last + /// notification. + progress: Vec, + /// Other podcast state (subscriptions, shares, feed URLs, sessions) + /// changed since the last notification. + changed: bool, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct PlaybackTrack { - pub id: i64, - pub title: String, - pub track_number: Option, - pub disc_number: Option, - pub duration_seconds: f64, - #[serde(default)] - pub artist_names: Vec, - #[serde(default)] - pub featured_artist_names: Vec, - pub release_id: i64, - pub release_title: String, - pub release_year: Option, - /// Legacy compatibility only. Paths are device-local, so playback sync - /// must resolve tracks from content/federation identifiers instead. - #[serde(default)] - pub file_path: String, - pub content_id: Option, - pub audio_format: Option, - pub audio_bitrate: Option, - pub audio_sample_rate: Option, - pub audio_bit_depth: Option, - pub file_size_bytes: Option, - #[serde(default)] - pub play_count: i64, - #[serde(default)] - pub fed: Option, +/// Converts queue items to and from the shared wire form +/// ([`PlaybackTrack`]). The wire types themselves are the shared +/// `music_dht::device_sync` definitions; this client keeps no copies. +/// +/// A podcast episode travels with its explicit source +/// (`PlaybackSource::PodcastEpisode`), never as a music item. Peers below +/// protocol v4 never get the episodes (`playback_allowed_for_peer`): they +/// see the queue redacted to "Private item" placeholders, only so that their +/// ownership engine keeps hearing from the owner. +pub trait PlaybackTrackExt { + /// Wire form of a queue item. Device-local paths are never sent. + fn from_track(track: &TrackItem) -> Self; + /// Queue item for a wire track received from another device. A Jam + /// private placeholder becomes [`private_track_item`]`(0)`; callers + /// that keep a queue number them ([`track_items_from_wire`]). + fn to_track_item(&self) -> TrackItem; } -impl PlaybackTrack { - fn portable_placeholder_id(&self) -> i64 { - let key = self - .content_id - .as_deref() - .and_then(music_dht::normalize_content_id) - .map(|content_id| format!("content:{content_id}")) - .or_else(|| { - self.fed - .as_ref() - .map(|fed| fed.content_id.as_str()) - .and_then(music_dht::normalize_content_id) - .map(|content_id| format!("content:{content_id}")) - }) - .or_else(|| { - self.fed - .as_ref() - .map(|fed| format!("fed:{}:{}", fed.owner, fed.item_id)) - }) - .unwrap_or_else(|| { - format!( - "remote:{}:{}:{}:{}", - self.id, self.title, self.release_title, self.duration_seconds - ) - }); - let hash = blake3::hash(key.as_bytes()); - let mut bytes = [0u8; 8]; - bytes.copy_from_slice(&hash.as_bytes()[..8]); - let positive = (i64::from_be_bytes(bytes) & i64::MAX).max(1); - -positive +/// Ids of the "private item" queue entries that stand for a Jam host's +/// podcast episodes on a guest: `PRIVATE_ITEM_ID_BASE - k` for the k-th, so +/// each has its own queue key. Process-local; never sent (they go back on +/// the wire as `PlaybackSource::Private`). +const PRIVATE_ITEM_ID_BASE: i64 = -(1 << 62); +const PRIVATE_ITEM_ID_SPAN: i64 = 1 << 32; + +/// Queue entry for the k-th Jam private placeholder: no title but +/// [`music_dht::device_sync::PRIVATE_ITEM_TITLE`], no URL, not playable. +pub fn private_track_item(k: usize) -> TrackItem { + TrackItem { + id: PRIVATE_ITEM_ID_BASE - (k as i64 % PRIVATE_ITEM_ID_SPAN), + title: music_dht::device_sync::PRIVATE_ITEM_TITLE.to_string(), + track_number: None, + disc_number: None, + duration_seconds: 0.0, + artists: Vec::new(), + featured_artists: Vec::new(), + release_id: TrackItem::NOT_A_LIBRARY_ROW, + release_title: String::new(), + release_year: None, + file_path: String::new(), + content_id: None, + cover_path: None, + audio_format: None, + audio_bitrate: None, + audio_sample_rate: None, + audio_bit_depth: None, + file_size_bytes: None, + play_count: 0, + fed: None, + podcast: None, } +} - pub fn from_track(track: &TrackItem) -> Self { +/// Whether a queue entry is a Jam private placeholder. +pub fn is_private_track_item(track: &TrackItem) -> bool { + track.podcast.is_none() + && track.fed.is_none() + && track.content_id.is_none() + && track.id <= PRIVATE_ITEM_ID_BASE + && track.id > PRIVATE_ITEM_ID_BASE - PRIVATE_ITEM_ID_SPAN + && track.title == music_dht::device_sync::PRIVATE_ITEM_TITLE +} + +/// Whether a music item received from a device below protocol v4 is a +/// "Private item" placeholder that it echoes back. +/// +/// Such a device gets this device's queue redacted +/// ([`DeviceSync::local_playback_snapshot_for`]) and, while it controls this +/// device, mirrors it with its own types, which have no +/// `PlaybackTrack::source`. Each placeholder then turns into a music item +/// titled "Private item" with nothing else: no content id, federated +/// source, artist, release or duration. When that device takes playback +/// over (Play on it, a paused-startup claim, automatic failover) or sends a +/// command, it announces those items as its queue. +fn is_echoed_private_item(track: &PlaybackTrack) -> bool { + track.source.is_none() + && track.title == music_dht::device_sync::PRIVATE_ITEM_TITLE + && track.content_id.is_none() + && track.fed.is_none() + && track.artist_names.is_empty() + && track.featured_artist_names.is_empty() + && track.release_title.is_empty() + && track.duration_seconds == 0.0 +} + +/// Removes the "Private item" placeholders that a device below protocol v4 +/// echoed back ([`is_echoed_private_item`]) from a state it sent, and +/// returns how many there were. They stand for episodes this device keeps +/// private; as music items they could never play, and they would spread to +/// every queue as such. The position moves with the queue; when the +/// current item was one of them, the next item is current from its start. +pub fn drop_echoed_private_items(state: &mut PlaybackStateWire) -> usize { + let current = state.queue_pos; + let mut dropped = 0; + let mut kept_before = 0; + let mut current_dropped = false; + let mut index = 0; + state.queue.retain(|track| { + let echoed = is_echoed_private_item(track); + if echoed { + dropped += 1; + current_dropped |= index == current; + } else if index < current { + kept_before += 1; + } + index += 1; + !echoed + }); + if dropped == 0 { + return 0; + } + state.queue_pos = kept_before.min(state.queue.len().saturating_sub(1)); + if current_dropped { + state.position_secs = 0.0; + } + if state.queue.is_empty() { + state.playing = false; + } + dropped +} + +/// Status for placeholders left out of a state from an older device. +fn echoed_private_items_message(device: &str, dropped: usize) -> String { + let items = if dropped == 1 { "entry" } else { "entries" }; + format!( + "left out {dropped} \"{}\" {items} of {device}'s queue: they stand for podcast \ + episodes, and {device} needs a Furumi update to play them", + music_dht::device_sync::PRIVATE_ITEM_TITLE + ) +} + +/// Queue entries for a received queue, with `resolve_music` deciding music +/// items (a library copy, for example). Podcast episodes keep their typed +/// reference; Jam private placeholders are numbered in queue order. +pub fn track_items_from_wire( + queue: &[PlaybackTrack], + mut resolve_music: impl FnMut(&PlaybackTrack) -> TrackItem, +) -> Vec { + let mut private = 0; + queue + .iter() + .map(|track| match TrackItem::from_playback_track(track) { + furumi_library::models::PlaybackItem::Music => resolve_music(track), + furumi_library::models::PlaybackItem::PodcastEpisode(item) => *item, + furumi_library::models::PlaybackItem::Private => { + private += 1; + private_track_item(private - 1) + } + }) + .collect() +} + +impl PlaybackTrackExt for PlaybackTrack { + fn from_track(track: &TrackItem) -> Self { + if let Some(episode) = &track.podcast { + return Self::podcast_episode(episode.clone()); + } + if is_private_track_item(track) { + return Self::private_placeholder(); + } Self { id: track.id, title: track.title.clone(), @@ -195,10 +335,16 @@ impl PlaybackTrack { file_size_bytes: track.file_size_bytes, play_count: track.play_count, fed: track.fed.as_ref().and_then(SyncedFedTrack::from_fed), + source: None, } } - pub fn to_track_item(&self) -> TrackItem { + fn to_track_item(&self) -> TrackItem { + match TrackItem::from_playback_track(self) { + furumi_library::models::PlaybackItem::Music => {} + furumi_library::models::PlaybackItem::PodcastEpisode(item) => return *item, + furumi_library::models::PlaybackItem::Private => return private_track_item(0), + } let refs = |names: &[String]| -> Vec { names .iter() @@ -209,7 +355,7 @@ impl PlaybackTrack { .collect() }; TrackItem { - id: self.portable_placeholder_id(), + id: portable_placeholder_id(self), title: self.title.clone(), track_number: self.track_number, disc_number: self.disc_number, @@ -229,246 +375,53 @@ impl PlaybackTrack { file_size_bytes: self.file_size_bytes, play_count: self.play_count, fed: self.fed.as_ref().map(SyncedFedTrack::to_fed_track), + podcast: None, } } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct PlaybackStateWire { - #[serde(default)] - pub queue: Vec, - #[serde(default)] - pub queue_pos: usize, - pub playing: bool, - pub paused: bool, - #[serde(default)] - pub idle_since_ms: Option, - pub position_secs: f64, - #[serde(default)] - pub volume: u8, - pub shuffle: bool, - pub repeat: PlaybackRepeat, +/// Process-local negative id of a music item received from another device, +/// derived from its portable identity (content id, then federated source). +fn portable_placeholder_id(track: &PlaybackTrack) -> i64 { + let key = track + .content_id + .as_deref() + .and_then(music_dht::normalize_content_id) + .map(|content_id| format!("content:{content_id}")) + .or_else(|| { + track + .fed + .as_ref() + .map(|fed| fed.content_id.as_str()) + .and_then(music_dht::normalize_content_id) + .map(|content_id| format!("content:{content_id}")) + }) + .or_else(|| { + track + .fed + .as_ref() + .map(|fed| format!("fed:{}:{}", fed.owner, fed.item_id)) + }) + .unwrap_or_else(|| { + format!( + "remote:{}:{}:{}:{}", + track.id, track.title, track.release_title, track.duration_seconds + ) + }); + let hash = blake3::hash(key.as_bytes()); + let mut bytes = [0u8; 8]; + bytes.copy_from_slice(&hash.as_bytes()[..8]); + let positive = (i64::from_be_bytes(bytes) & i64::MAX).max(1); + -positive } -pub type PlaybackSnapshot = music_dht::playback::Snapshot; -pub type PlaybackCommand = music_dht::playback::Command; - -#[derive(Debug, Clone, Default)] -struct PlaybackShared { - engine: Option, - engine_group: String, - local: Option, - remote: BTreeMap, +/// Converts federated tracks to and from the synced metadata form. +trait SyncedFedTrackExt: Sized { + fn from_fed(fed: &crate::federation::FedTrack) -> Option; + fn to_fed_track(&self) -> crate::federation::FedTrack; } -#[derive(Debug, Clone)] -struct LocalIdentity { - device_id: String, - group_id: String, - name: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct InviteWire { - v: u16, - #[serde(rename = "t")] - ticket: String, - #[serde(rename = "d")] - device_id: String, - #[serde(rename = "i")] - invite_id: String, - #[serde(rename = "s")] - secret: String, - #[serde(rename = "e")] - expires_at_ms: i64, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct DeviceProfileWire { - device_id: String, - name: String, - client_version: String, - protocol_version: u16, - endpoint_id: String, - endpoint_ticket: String, - #[serde(default)] - revoked: bool, - #[serde(default)] - revoke_cutoff_seq: Option, - #[serde(default)] - updated_at_ms: i64, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SyncOpWire { - op_id: String, - origin_device_id: String, - seq: i64, - hlc_ms: i64, - payload: SyncOpPayload, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "snake_case")] -pub enum SyncOpPayload { - TrackLikeSet { - content_id: String, - liked: bool, - #[serde(default)] - fed: Option, - }, - PlaylistCreated { - playlist_id: String, - title: String, - }, - PlaylistRenamed { - playlist_id: String, - title: String, - }, - PlaylistDeleted { - playlist_id: String, - }, - PlaylistTrackAdded { - playlist_id: String, - content_id: String, - position: i64, - #[serde(default)] - fed: Option, - }, - PlaylistTrackRemoved { - playlist_id: String, - content_id: String, - }, - DeviceProfileSet { - name: String, - client_version: String, - endpoint_ticket: String, - endpoint_id: String, - }, - DeviceTrusted { - target_device_id: String, - }, - DeviceRevoked { - target_device_id: String, - target_max_seq_seen: i64, - }, - PlaybackCommand { - target_device_id: String, - command: PlaybackCommand, - #[serde(default)] - authority: Option, - }, - ListenRecorded { - event: ListenEvent, - }, -} - -impl SyncOpPayload { - fn is_tombstone(&self) -> bool { - matches!( - self, - SyncOpPayload::TrackLikeSet { liked: false, .. } - | SyncOpPayload::PlaylistDeleted { .. } - | SyncOpPayload::PlaylistTrackRemoved { .. } - | SyncOpPayload::DeviceRevoked { .. } - ) - } -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -struct SyncSnapshot { - #[serde(default)] - likes: Vec, - #[serde(default)] - unlikes: Vec, - #[serde(default)] - playlists: Vec, - #[serde(default)] - deleted_playlists: Vec, - #[serde(default)] - removed_playlist_items: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotLike { - content_id: String, - hlc_ms: i64, - op_id: String, - #[serde(default)] - fed: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotLikeTombstone { - content_id: String, - hlc_ms: i64, - op_id: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylist { - playlist_id: String, - title: String, - hlc_ms: i64, - op_id: String, - #[serde(default)] - items: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistTombstone { - playlist_id: String, - hlc_ms: i64, - op_id: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistItem { - content_id: String, - position: i64, - hlc_ms: i64, - op_id: String, - #[serde(default)] - fed: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct SnapshotPlaylistItemTombstone { - playlist_id: String, - content_id: String, - hlc_ms: i64, - op_id: String, -} - -enum PairAttempt { - Accepted(String), - Pending, - Denied(String), -} - -struct PairingStatus { - status: String, - use_requester_group: bool, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct SyncedFedTrack { - item_id: String, - owner: String, - title: String, - #[serde(default)] - artist_names: Vec, - #[serde(default)] - featured_artist_names: Vec, - year: Option, - duration_seconds: Option, - content_id: String, - release_title: Option, - track_number: Option, - disc_number: Option, -} - -impl SyncedFedTrack { +impl SyncedFedTrackExt for SyncedFedTrack { fn from_fed(fed: &crate::federation::FedTrack) -> Option { let content_id = fed .content_id @@ -507,71 +460,82 @@ impl SyncedFedTrack { } } -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(tag = "type", rename_all = "snake_case")] -enum WireMessage { - PairRequest { - invite_id: String, - secret: String, - profile: DeviceProfileWire, - #[serde(default)] - group_id: Option, - #[serde(default)] - group_active_devices: usize, - #[serde(default)] - devices: Vec, - vector: BTreeMap, - ops: Vec, - snapshot: SyncSnapshot, - #[serde(default)] - playback: Option, - }, - PairResponse { - accepted: bool, - #[serde(default)] - pending: bool, - #[serde(default)] - error: Option, - #[serde(default)] - group_id: Option, - #[serde(default)] - profile: Option, - #[serde(default)] - devices: Vec, - #[serde(default)] - vector: BTreeMap, - #[serde(default)] - ops: Vec, - #[serde(default)] - snapshot: SyncSnapshot, - #[serde(default)] - playback: Option, - }, - Hello { - group_id: String, - profile: DeviceProfileWire, - devices: Vec, - vector: BTreeMap, - ops: Vec, - snapshot: SyncSnapshot, - #[serde(default)] - playback: Option, - }, - SyncResponse { - accepted: bool, - #[serde(default)] - error: Option, - #[serde(default)] - devices: Vec, - #[serde(default)] - vector: BTreeMap, - #[serde(default)] - ops: Vec, - #[serde(default)] - snapshot: SyncSnapshot, - #[serde(default)] - playback: Option, - }, +#[derive(Debug, Clone, Default)] +struct PlaybackShared { + engine: Option, + engine_group: String, + local: Option, + remote: BTreeMap, + /// Echoed placeholders last left out of each older device's snapshot + /// (`drop_echoed_private_items`), to report a change only once. + echoed: BTreeMap, +} + +#[derive(Debug, Clone)] +struct LocalIdentity { + device_id: String, + group_id: String, + name: String, +} + +enum PairAttempt { + Accepted(String), + Pending, + Denied(String), +} + +struct PairingStatus { + status: String, + use_requester_group: bool, +} + +/// How the protocol version in a device profile became known. +/// +/// Version gating (podcast operations, snapshot sections and playback +/// state need v4) depends on these values, so this device never stores its +/// own [`PROTOCOL_VERSION`] for another device. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum VersionReport { + /// The device reported its own version (`None`: an older client left it + /// out). Replaces the stored version, so a downgrade is noticed. + SelfReported(Option), + /// Another device relayed the profile. Only raises the stored version: + /// v1–v3 relays substitute their own constant. + Relayed(u16), +} + +impl VersionReport { + fn resolve(self, stored: Option) -> u16 { + match self { + VersionReport::SelfReported(reported) => { + music_dht::device_sync::profile_protocol_version(reported, stored) + } + VersionReport::Relayed(relayed) => { + music_dht::device_sync::relayed_protocol_version(relayed, stored) + } + } + } + + /// Report carried by an entry of a `devices` list sent by `sender`. + fn for_listed(profile: &DeviceProfileWire, sender: Option<&str>) -> Self { + if sender == Some(profile.device_id.as_str()) { + VersionReport::SelfReported(Some(profile.protocol_version)) + } else { + VersionReport::Relayed(profile.protocol_version) + } + } +} + +/// The stored protocol version of a device, if any row exists. +fn stored_protocol_version(conn: &Connection, device_id: &str) -> Result> { + Ok(conn + .query_row( + "SELECT protocol_version FROM sync_devices WHERE device_id = ?1", + [device_id], + |row| row.get::<_, i64>(0), + ) + .optional()? + .and_then(|version| u16::try_from(version).ok())) } fn lock(mutex: &std::sync::Mutex) -> std::sync::MutexGuard<'_, T> { @@ -634,6 +598,7 @@ impl DeviceSync { library, event_tx: Arc::new(std::sync::Mutex::new(None)), playback: Arc::new(std::sync::Mutex::new(PlaybackShared::default())), + podcasts: Arc::default(), }); sync.ensure_identity()?; sync.repair_like_order_from_sync_state()?; @@ -794,11 +759,188 @@ impl DeviceSync { client_version: CLIENT_VERSION.to_string(), endpoint_ticket: ticket.to_string(), endpoint_id, + protocol_version: Some(PROTOCOL_VERSION), })?; } Ok(()) } + /// Protocol version a device reported itself, for version gating. + /// Unknown devices count as the oldest protocol. + pub fn peer_protocol_version(&self, device_id: &str) -> Result { + let stored = stored_protocol_version(&lock(&self.conn), device_id)?; + Ok(music_dht::device_sync::profile_protocol_version( + None, stored, + )) + } + + /// Connects the podcast store. Podcast operations received from now on + /// are applied to it, and those that arrived while it was unavailable + /// (or whose application failed) are applied first. Blocking. + pub fn attach_podcast_store(&self, store: Arc) -> Result<()> { + let pending = { + // Same lock order as `apply_podcast_op`: link, then log. An + // operation received concurrently is either in this list or + // sees the attached store. + let mut link = lock(&self.podcasts); + link.store = Some(Arc::clone(&store)); + let conn = lock(&self.conn); + // Rows whose operation was coalesced away have nothing to apply. + conn.execute( + "DELETE FROM sync_podcast_pending + WHERE op_id NOT IN (SELECT op_id FROM sync_ops)", + [], + )?; + let mut stmt = conn.prepare( + "SELECT o.op_id, o.origin_device_id, o.seq, o.hlc_ms, o.payload_json + FROM sync_podcast_pending p + JOIN sync_ops o ON o.op_id = p.op_id + ORDER BY o.hlc_ms, o.op_id", + )?; + stmt.query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, i64>(2)?, + row.get::<_, i64>(3)?, + row.get::<_, String>(4)?, + )) + })? + .collect::>>()? + }; + let mut applied = 0usize; + for (op_id, origin_device_id, seq, hlc_ms, payload_json) in pending { + let op = match serde_json::from_str(&payload_json) { + Ok(payload) => SyncOpWire { + op_id: op_id.clone(), + origin_device_id, + seq, + hlc_ms, + payload, + }, + Err(err) => { + tracing::warn!(%op_id, "dropping an unreadable podcast operation: {err}"); + lock(&self.conn).execute( + "DELETE FROM sync_podcast_pending WHERE op_id = ?1", + [&op_id], + )?; + continue; + } + }; + match self.apply_podcast_op_to(&store, &op) { + Ok(()) => { + applied += 1; + lock(&self.conn).execute( + "DELETE FROM sync_podcast_pending WHERE op_id = ?1", + [&op_id], + )?; + } + Err(err) => { + tracing::warn!(%op_id, "podcast operation still not applied: {err:#}"); + } + } + } + if applied > 0 { + tracing::info!(applied, "applied podcast operations received earlier"); + } + self.flush_podcast_changes(); + Ok(()) + } + + fn podcast_store(&self) -> Option> { + lock(&self.podcasts).store.clone() + } + + /// Merges a received podcast operation into the podcast store, or keeps + /// it for [`DeviceSync::attach_podcast_store`]. A podcast store problem + /// never fails music sync: the operation stays in the log either way. + fn apply_podcast_op(&self, op: &SyncOpWire) -> Result<()> { + let store = { + let link = lock(&self.podcasts); + match &link.store { + Some(store) => Arc::clone(store), + None => { + lock(&self.conn).execute( + "INSERT OR IGNORE INTO sync_podcast_pending (op_id) VALUES (?1)", + [&op.op_id], + )?; + return Ok(()); + } + } + }; + if let Err(err) = self.apply_podcast_op_to(&store, op) { + tracing::warn!(op_id = %op.op_id, "podcast operation not applied yet: {err:#}"); + lock(&self.conn).execute( + "INSERT OR IGNORE INTO sync_podcast_pending (op_id) VALUES (?1)", + [&op.op_id], + )?; + } + Ok(()) + } + + fn apply_podcast_op_to(&self, store: &PodcastStore, op: &SyncOpWire) -> Result<()> { + match store.apply_remote_op(op)? { + PodcastApplyOutcome::Changed => { + let mut link = lock(&self.podcasts); + match op.podcast_progress() { + Some(record) => link.progress.push(record), + None => link.changed = true, + } + } + // Acknowledged anyway so the log keeps moving (FRID contract). + PodcastApplyOutcome::Rejected(reason) => tracing::warn!( + op_id = %op.op_id, + kind = payload_kind(&op.payload), + %reason, + "skipped an invalid podcast operation" + ), + _ => {} + } + Ok(()) + } + + /// Merges the podcast sections of a received snapshot. + fn apply_podcast_snapshot(&self, snapshot: &SyncSnapshot) { + let Some(store) = self.podcast_store() else { + // Snapshots come with every sync; a later one is merged. + return; + }; + match store.apply_snapshot(snapshot) { + Ok(stats) => { + if stats.ignored_over_limits { + tracing::warn!("ignored podcast snapshot sections over the shared limits"); + } + if stats.rejected > 0 { + tracing::warn!( + rejected = stats.rejected, + "skipped invalid podcast snapshot records" + ); + } + if stats.changed > 0 { + lock(&self.podcasts).changed = true; + } + } + Err(err) => tracing::warn!("podcast snapshot not merged: {err:#}"), + } + } + + /// Tells the UI about podcast state that received operations or + /// snapshots changed (one event per batch). + fn flush_podcast_changes(&self) { + let (progress, changed) = { + let mut link = lock(&self.podcasts); + ( + std::mem::take(&mut link.progress), + std::mem::take(&mut link.changed), + ) + }; + if (changed || !progress.is_empty()) + && let Some(tx) = lock(&self.event_tx).as_ref() + { + let _ = tx.send(AppEvent::PodcastsSynced { progress, changed }); + } + } + pub fn status(&self) -> DeviceSyncStatus { match self.status_inner() { Ok(status) => status, @@ -830,9 +972,9 @@ impl DeviceSync { invite_id, secret, expires_at_ms, + protocol_version: Some(PROTOCOL_VERSION), }; - let bytes = serde_json::to_vec(&payload)?; - Ok(format!("frid://i/{}", base64url_encode(&bytes))) + Ok(encode_invite(&payload)?) } pub async fn connect_invite( @@ -891,9 +1033,11 @@ impl DeviceSync { let profile = self.own_profile(&own_ticket)?; let devices = self.device_profiles()?; let vector = self.vector()?; - let ops = self.ops_for_peer(&invite.device_id)?; - let snapshot = self.snapshot()?; - let playback = self.local_playback_snapshot(); + // The inviter has not reported anything yet but its invite. + let peer_version = invite.inviter_protocol_version(); + let ops = self.ops_for_peer(&invite.device_id, peer_version)?; + let snapshot = self.snapshot_for(peer_version)?; + let playback = self.local_playback_snapshot_for(peer_version); let mut stream = service.open_stream(peer, SYNC_ALPN).await?; crate::federation::record_stream_transport( &transport_stats, @@ -942,15 +1086,18 @@ impl DeviceSync { .. } => { self.set_group_id(&group_id)?; - if let Some(profile) = profile { - self.apply_device_profile(&profile, true)?; + if let Some(profile) = &profile { + self.apply_device_profile( + profile, + true, + VersionReport::SelfReported(Some(profile.protocol_version)), + )?; if let Some(playback) = playback { self.apply_playback_snapshot(&profile.device_id, playback)?; } } - self.apply_device_profiles(&devices)?; - self.apply_snapshot(snapshot)?; - self.apply_ops(ops)?; + self.apply_device_profiles(&devices, Some(&invite.device_id))?; + self.apply_received(snapshot, ops)?; self.record_local_op(SyncOpPayload::DeviceTrusted { target_device_id: invite.device_id.clone(), })?; @@ -989,7 +1136,8 @@ impl DeviceSync { conn.query_row( "SELECT device_id, name, client_version, endpoint_id, endpoint_ticket, created_at_ms, - requester_group_id, requester_group_devices_json + requester_group_id, requester_group_devices_json, + protocol_version FROM sync_pending_pairing WHERE request_id = ?1", [request_id], @@ -999,7 +1147,7 @@ impl DeviceSync { device_id: row.get(0)?, name: row.get(1)?, client_version: row.get(2)?, - protocol_version: PROTOCOL_VERSION, + protocol_version: music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION, endpoint_id: row.get(3)?, endpoint_ticket: row.get(4)?, revoked: false, @@ -1008,6 +1156,8 @@ impl DeviceSync { }, row.get::<_, Option>(6)?, row.get::<_, String>(7)?, + row.get::<_, Option>(8)? + .and_then(|version| u16::try_from(version).ok()), )) }, ) @@ -1028,7 +1178,13 @@ impl DeviceSync { )? }; if accept && changed > 0 { - if let Some((profile, requester_group_id, requester_group_devices_json)) = pending { + if let Some(( + profile, + requester_group_id, + requester_group_devices_json, + requester_version, + )) = pending + { if use_requester_group { if let Some(group_id) = requester_group_id .as_deref() @@ -1038,9 +1194,15 @@ impl DeviceSync { } let requester_devices: Vec = serde_json::from_str(&requester_group_devices_json)?; - self.apply_device_profiles(&requester_devices)?; + self.apply_device_profiles(&requester_devices, Some(&profile.device_id))?; } - self.apply_device_profile(&profile, true)?; + // The version the requester reported in its own pairing + // request (requests stored by older builds have none). + self.apply_device_profile( + &profile, + true, + VersionReport::SelfReported(requester_version), + )?; self.record_local_op(SyncOpPayload::DeviceTrusted { target_device_id: profile.device_id, })?; @@ -1199,6 +1361,11 @@ impl DeviceSync { listened_ms: i64, ended_reason: music_dht::device_sync::ListenEndReason, ) -> Option { + // Music listening history only: episodes record podcast listening + // sessions in the podcast store instead. + if track.is_podcast() { + return None; + } let content_id = track .content_id .as_deref() @@ -1324,6 +1491,30 @@ impl DeviceSync { Ok(()) } + /// Runs a `PodcastStore` local action with stamps reserved in this + /// device's sync log and appends the operations it returns with exactly + /// those stamps (`op_id`, `seq`, `hlc_ms`). + /// + /// The log connection stays locked from the first stamp until the + /// operations are appended, so no sync batch can read past a reserved + /// `seq` before its operation exists. Lock order is podcast store, then + /// this log: never call the podcast store while holding `self.conn`. + pub fn run_podcast_action( + &self, + action: impl FnOnce(&mut dyn PodcastStampSource) -> Result<(T, Vec)>, + ) -> Result { + let identity = self.ensure_identity()?; + let mut log = PodcastLogStamps { + conn: &self.conn, + guard: None, + device_id: identity.device_id, + reserved: BTreeMap::new(), + }; + let (value, ops) = action(&mut log)?; + log.append(ops)?; + Ok(value) + } + pub async fn sync_once( &self, service: Arc, @@ -1358,9 +1549,10 @@ impl DeviceSync { let profile = self.own_profile(&own_ticket)?; let devices = self.device_profiles()?; let vector = self.vector()?; - let ops = self.ops_for_peer(&device.device_id)?; - let snapshot = self.snapshot()?; - let playback = self.local_playback_snapshot(); + let peer_version = self.peer_protocol_version(&device.device_id)?; + let ops = self.ops_for_peer(&device.device_id, peer_version)?; + let snapshot = self.snapshot_for(peer_version)?; + let playback = self.local_playback_snapshot_for(peer_version); let mut stream = service.open_stream(peer, SYNC_ALPN).await?; crate::federation::record_stream_transport( &transport_stats, @@ -1403,12 +1595,12 @@ impl DeviceSync { playback, .. } => { - self.apply_device_profiles(&devices)?; + // The responder lists itself: that entry is its own report. + self.apply_device_profiles(&devices, Some(&device.device_id))?; if let Some(playback) = playback { self.apply_playback_snapshot(&device.device_id, playback)?; } - self.apply_snapshot(snapshot)?; - self.apply_ops(ops)?; + self.apply_received(snapshot, ops)?; self.note_peer_vector(&device.device_id, &vector)?; self.mark_seen(&device.device_id, Some(peer.to_string()))?; self.set_last_sync(Some(format!("synced {}", short_id(&device.device_id))))?; @@ -1474,8 +1666,9 @@ impl DeviceSync { fn own_profile(&self, endpoint_ticket: &str) -> Result { let identity = self.ensure_identity()?; let endpoint_id = ticket_endpoint_id(endpoint_ticket).unwrap_or_default(); - { + let previous_version = { let conn = lock(&self.conn); + let previous_version = stored_protocol_version(&conn, &identity.device_id)?; conn.execute( "INSERT INTO sync_devices (device_id, name, client_version, protocol_version, endpoint_id, @@ -1498,6 +1691,19 @@ impl DeviceSync { now_ms(), ], )?; + previous_version + }; + if previous_version.is_some_and(|version| version < PROTOCOL_VERSION) { + // First start of a new protocol version: announce it in the log + // too, so devices that only hear of this one through relays + // learn its version (`profile_protocol_version`). + self.record_local_op(SyncOpPayload::DeviceProfileSet { + name: identity.name.clone(), + client_version: CLIENT_VERSION.to_string(), + endpoint_ticket: endpoint_ticket.to_string(), + endpoint_id: endpoint_id.clone(), + protocol_version: Some(PROTOCOL_VERSION), + })?; } Ok(DeviceProfileWire { device_id: identity.device_id, @@ -1575,12 +1781,23 @@ impl DeviceSync { ) }; let _ = self.apply_op(&op)?; - tracing::debug!( - op_id = %op.op_id, - tombstone, - payload = %payload_json, - "recorded personal-sync op" - ); + if payload_may_hold_private_urls(&op.payload) { + // Feed and audio URLs may carry private tokens, and the Logs tab + // keeps this crate's debug output: log the kind only. + tracing::debug!( + op_id = %op.op_id, + tombstone, + kind = payload_kind(&op.payload), + "recorded personal-sync op" + ); + } else { + tracing::debug!( + op_id = %op.op_id, + tombstone, + payload = %payload_json, + "recorded personal-sync op" + ); + } if gc_after_record { let _ = self.gc_tombstones(); } @@ -1614,6 +1831,7 @@ impl DeviceSync { }; if inserted > 0 { changed |= self.apply_op(&op)?; + coalesce_progress_ops(&lock(&self.conn), &op)?; } let conn = lock(&self.conn); conn.execute( @@ -1627,6 +1845,7 @@ impl DeviceSync { if changed { self.notify_library_changed(); } + self.flush_podcast_changes(); Ok(()) } @@ -1706,19 +1925,27 @@ impl DeviceSync { client_version, endpoint_ticket, endpoint_id, + protocol_version, } => { let profile = DeviceProfileWire { device_id: op.origin_device_id.clone(), name: name.clone(), client_version: client_version.clone(), - protocol_version: PROTOCOL_VERSION, + protocol_version: protocol_version + .unwrap_or(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION), endpoint_id: endpoint_id.clone(), endpoint_ticket: endpoint_ticket.clone(), revoked: false, revoke_cutoff_seq: None, updated_at_ms: op.hlc_ms, }; - self.apply_device_profile(&profile, false)?; + // The origin announces its own profile; profiles before v4 + // carry no version and keep the stored one. + self.apply_device_profile( + &profile, + false, + VersionReport::SelfReported(*protocol_version), + )?; false } SyncOpPayload::DeviceTrusted { target_device_id } => { @@ -1750,6 +1977,16 @@ impl DeviceSync { SyncOpPayload::ListenRecorded { event } => self .library .apply_listen_event(event, &op.origin_device_id)?, + // Podcast operations (protocol v4) stay in the log, so they are + // relayed, and are merged into the podcast store. + SyncOpPayload::PodcastSubscriptionSet { .. } + | SyncOpPayload::PodcastShareSet { .. } + | SyncOpPayload::PodcastFeedAliasesAdded { .. } + | SyncOpPayload::PodcastProgressSet { .. } + | SyncOpPayload::PodcastListenRecorded { .. } => { + self.apply_podcast_op(op)?; + false + } }; Ok(changed) } @@ -1768,6 +2005,43 @@ impl DeviceSync { let Some(authority) = authority else { return Ok(()); }; + // A peer below v4 does not know podcast items: while this queue has + // any, its state would silently drop them. Checked before the + // ownership engine sees the command. + let peer_version = self.peer_protocol_version(origin)?; + let local = lock(&self.playback) + .local + .as_ref() + .map(|snapshot| snapshot.state.clone()); + if let Some(local) = local + && !music_dht::device_sync::playback_command_allowed_from_peer(&local, peer_version) + { + tracing::info!( + origin = %short_id(origin), + peer_version, + "ignored a playback command from a device without podcast support" + ); + if let Some(tx) = lock(&self.event_tx).as_ref() { + let _ = tx.send(AppEvent::StatusMessage(format!( + "ignored playback control from {}: it needs a Furumi update to keep the \ + podcast episodes in this queue", + self.device_label(origin) + ))); + } + return Ok(()); + } + // Its state may hold the placeholders it was sent, echoed back as + // music items: they are left out, never adopted. + let mut command = command.clone(); + let echoed = if music_dht::device_sync::supports_podcasts(peer_version) { + 0 + } else { + match &mut command { + PlaybackCommand::SetState { state, .. } + | PlaybackCommand::ActiveChanged { state, .. } => drop_echoed_private_items(state), + } + }; + let command = &command; let handoff = matches!(command, PlaybackCommand::ActiveChanged { .. }); if !handoff && target_device_id != identity.device_id { return Ok(()); @@ -1803,6 +2077,12 @@ impl DeviceSync { authority: authority.clone(), origin: origin.to_string(), }); + if echoed > 0 { + let _ = tx.send(AppEvent::StatusMessage(echoed_private_items_message( + &self.device_label(origin), + echoed, + ))); + } } Ok(()) } @@ -2135,6 +2415,38 @@ impl DeviceSync { Ok(visible_changed) } + /// Applies what a peer sent: the music snapshot, the operations, then + /// the podcast snapshot sections. Podcast merges are order-independent; + /// operations go first so a changed progress register reaches the UI as + /// such (it is shown in place) instead of as a generic change. + fn apply_received(&self, snapshot: SyncSnapshot, ops: Vec) -> Result<()> { + // Called from the async stream handlers; podcast store merges can + // wait on a feed refresh holding the store. + off_worker(|| self.apply_received_blocking(snapshot, ops)) + } + + fn apply_received_blocking( + &self, + mut snapshot: SyncSnapshot, + ops: Vec, + ) -> Result<()> { + let podcast = SyncSnapshot { + podcast_subscriptions: std::mem::take(&mut snapshot.podcast_subscriptions), + podcast_shares: std::mem::take(&mut snapshot.podcast_shares), + podcast_progress: std::mem::take(&mut snapshot.podcast_progress), + podcast_listen_sessions: std::mem::take(&mut snapshot.podcast_listen_sessions), + ..SyncSnapshot::default() + }; + self.apply_snapshot(snapshot)?; + self.apply_ops(ops)?; + if podcast.has_podcast_state() { + self.apply_podcast_snapshot(&podcast); + self.flush_podcast_changes(); + } + Ok(()) + } + + /// Applies the music sections of a snapshot. fn apply_snapshot(&self, snapshot: SyncSnapshot) -> Result<()> { let mut changed = false; for like in snapshot.likes { @@ -2196,14 +2508,26 @@ impl DeviceSync { Ok(()) } - fn apply_device_profiles(&self, devices: &[DeviceProfileWire]) -> Result<()> { + /// Stores the profiles of a `devices` list sent by `sender`. The entry + /// of the sender itself is its own version report; the others are + /// relayed. + fn apply_device_profiles( + &self, + devices: &[DeviceProfileWire], + sender: Option<&str>, + ) -> Result<()> { for profile in devices { - self.apply_device_profile(profile, false)?; + self.apply_device_profile(profile, false, VersionReport::for_listed(profile, sender))?; } Ok(()) } - fn apply_device_profile(&self, profile: &DeviceProfileWire, trusted: bool) -> Result<()> { + fn apply_device_profile( + &self, + profile: &DeviceProfileWire, + trusted: bool, + version: VersionReport, + ) -> Result<()> { let own = self.ensure_identity()?.device_id; if profile.device_id == own { return Ok(()); @@ -2215,6 +2539,8 @@ impl DeviceSync { }; { let conn = lock(&self.conn); + let protocol_version = + version.resolve(stored_protocol_version(&conn, &profile.device_id)?); conn.execute( "INSERT INTO sync_devices (device_id, name, client_version, protocol_version, endpoint_id, @@ -2250,7 +2576,7 @@ impl DeviceSync { profile.device_id, profile.name, profile.client_version, - profile.protocol_version, + protocol_version, profile.endpoint_id, profile.endpoint_ticket, trusted_at_ms, @@ -2301,7 +2627,20 @@ impl DeviceSync { Ok(()) } - fn ops_for_peer(&self, peer_device_id: &str) -> Result> { + /// Operations the peer has not acknowledged and can parse. + /// + /// Kinds the peer's version does not know are excluded in SQL, so a + /// batch of hidden operations cannot stall the per-peer cursor; + /// `op_allowed_for_peer` stays the final check (playback commands that + /// carry podcast items). Hidden operations are never re-sent after the + /// peer upgrades (vectors only grow): snapshots are the recovery path. + fn ops_for_peer(&self, peer_device_id: &str, peer_version: u16) -> Result> { + // Every peer of `furumi/sync/2` keeps listen history, even when it + // never reported a version (an invite without one, a device known + // only from `device_trusted`). Listen events are not part of + // snapshots, so skipping them would lose them for good. + let peer_version = peer_version.max(SYNC_V2_PROTOCOL_VERSION); + let hide_podcasts = !music_dht::device_sync::supports_podcasts(peer_version); let conn = lock(&self.conn); let mut stmt = conn.prepare( "SELECT o.op_id, o.origin_device_id, o.seq, o.hlc_ms, o.payload_json @@ -2309,6 +2648,7 @@ impl DeviceSync { LEFT JOIN sync_peer_acks a ON a.peer_device_id = ?1 AND a.origin_device_id = o.origin_device_id WHERE o.seq > COALESCE(a.max_seq, 0) + AND NOT (?4 AND substr(o.kind, 1, 8) = 'podcast_') AND ( o.kind != 'playback_command' OR ( @@ -2323,7 +2663,8 @@ impl DeviceSync { params![ peer_device_id, now_ms().saturating_sub(PLAYBACK_COMMAND_TTL_MS), - MAX_OPS_PER_BATCH as i64 + MAX_OPS_PER_BATCH as i64, + hide_podcasts, ], |row| { let payload_json: String = row.get(4)?; @@ -2342,9 +2683,32 @@ impl DeviceSync { }) }, )?; - Ok(rows.collect::>>()?) + let mut ops = rows.collect::>>()?; + ops.retain(|op| music_dht::device_sync::op_allowed_for_peer(&op.payload, peer_version)); + Ok(ops) } + /// The materialized snapshot for a peer: podcast sections (bounded by + /// the shared snapshot limits) only for v4 peers. + fn snapshot_for(&self, peer_version: u16) -> Result { + let mut snapshot = self.snapshot()?; + if music_dht::device_sync::supports_podcasts(peer_version) + && let Some(store) = self.podcast_store() + && let Err(err) = off_worker(|| { + store.fill_snapshot( + &mut snapshot, + music_dht::podcast::MAX_PODCAST_SNAPSHOT_BYTES, + ) + }) + { + // Music sync goes on; podcast state comes with a later sync. + tracing::warn!("podcast snapshot export failed: {err:#}"); + snapshot.strip_podcast_state(); + } + Ok(snapshot.for_peer(peer_version)) + } + + /// Music sections of the materialized snapshot. fn snapshot(&self) -> Result { let like_rows = { let conn = lock(&self.conn); @@ -2496,6 +2860,7 @@ impl DeviceSync { playlists, deleted_playlists, removed_playlist_items, + ..SyncSnapshot::default() }) } @@ -2638,7 +3003,7 @@ impl DeviceSync { let last_error = get_meta(&conn, "last_error")?; let mut stmt = conn.prepare( "SELECT device_id, name, client_version, endpoint_id, last_seen_ms, - revoked_at_ms IS NOT NULL + revoked_at_ms IS NOT NULL, protocol_version FROM sync_devices WHERE trusted_at_ms IS NOT NULL ORDER BY revoked_at_ms IS NOT NULL, name COLLATE NOCASE, device_id", @@ -2654,6 +3019,8 @@ impl DeviceSync { endpoint_id: row.get(3)?, last_seen_ms: row.get(4)?, revoked: row.get::<_, i64>(5)? != 0, + protocol_version: u16::try_from(row.get::<_, i64>(6)?) + .unwrap_or(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION), }) })? .collect::>>()?; @@ -2724,11 +3091,63 @@ impl DeviceSync { } } - fn local_playback_snapshot(&self) -> Option { - lock(&self.playback).local.clone() + /// Refuses to hand a queue with podcast items to a device below v4 + /// ("that device needs an update"), before ownership changes. + pub fn ensure_playback_supported_by( + &self, + device_id: &str, + state: &PlaybackStateWire, + ) -> Result<()> { + let version = self.peer_protocol_version(device_id)?; + anyhow::ensure!( + music_dht::device_sync::playback_allowed_for_peer(state, version), + "{} needs a Furumi update to play podcast episodes (device sync v{}); update \ + Furumi there, or remove the episodes from the queue", + self.device_label(device_id), + music_dht::device_sync::PODCAST_PROTOCOL_VERSION + ); + Ok(()) } - fn apply_playback_snapshot(&self, sender: &str, snapshot: PlaybackSnapshot) -> Result<()> { + /// Display name of a device, or a short id. + fn device_label(&self, device_id: &str) -> String { + lock(&self.conn) + .query_row( + "SELECT name FROM sync_devices WHERE device_id = ?1", + [device_id], + |row| row.get::<_, String>(0), + ) + .optional() + .ok() + .flatten() + .filter(|name| !name.trim().is_empty()) + .unwrap_or_else(|| short_id(device_id)) + } + + /// The playback snapshot for a peer. + /// + /// A peer below v4 cannot carry a queue with podcast items + /// (`playback_allowed_for_peer`), but it still needs the coordination + /// envelope: an engine renews the owner's liveness only from the + /// owner's own announcements, so an old device that hears nothing takes + /// playback over by automatic failover after `owner_timeout_ms` (two + /// minutes by default) and this device would then adopt its queue, + /// losing the episodes. Such a peer therefore gets the announcement with + /// the queue redacted like for Jam (`redact_for_jam`: every episode + /// becomes an opaque "Private item", order and transport kept). The + /// episodes are never stripped, and the commands the peer derives from + /// that state are refused (`playback_command_allowed_from_peer`). + /// FRID's PLAYBACK_PROTOCOL.md rule 1 still says "send `None`"; this is + /// reported as a contract gap. + fn local_playback_snapshot_for(&self, peer_version: u16) -> Option { + let mut snapshot = lock(&self.playback).local.clone()?; + if !music_dht::device_sync::playback_allowed_for_peer(&snapshot.state, peer_version) { + snapshot.state = music_dht::device_sync::redact_for_jam(snapshot.state); + } + Some(snapshot) + } + + fn apply_playback_snapshot(&self, sender: &str, mut snapshot: PlaybackSnapshot) -> Result<()> { let identity = self.ensure_identity()?; if snapshot.device_id != sender || snapshot.device_id == identity.device_id { return Ok(()); @@ -2736,12 +3155,18 @@ impl DeviceSync { let Some(coordination) = &snapshot.coordination else { return Ok(()); }; + let echoed = + if music_dht::device_sync::supports_podcasts(self.peer_protocol_version(sender)?) { + 0 + } else { + drop_echoed_private_items(&mut snapshot.state) + }; if !self.with_playback_engine(|engine| { engine.observe(&snapshot.device_id, coordination, playback_clock()) })? { return Ok(()); } - let should_send = { + let (should_send, report_echoed) = { let mut shared = lock(&self.playback); let changed = shared.remote.get(&snapshot.device_id).is_none_or(|old| { old.coordination.as_ref().is_none_or(|c| { @@ -2754,10 +3179,17 @@ impl DeviceSync { .remote .insert(snapshot.device_id.clone(), snapshot.clone()); } - changed + let reported = shared.echoed.insert(snapshot.device_id.clone(), echoed); + (changed, changed && echoed > 0 && reported != Some(echoed)) }; if should_send && let Some(tx) = lock(&self.event_tx).as_ref() { + let device = snapshot.device_name.clone(); let _ = tx.send(AppEvent::DevicePlayback(snapshot)); + if report_echoed { + let _ = tx.send(AppEvent::StatusMessage(echoed_private_items_message( + &device, echoed, + ))); + } } Ok(()) } @@ -2799,6 +3231,101 @@ impl DeviceSync { } } +/// Stamp source over the sync log for [`DeviceSync::run_podcast_action`]. +struct PodcastLogStamps<'a> { + conn: &'a std::sync::Mutex, + /// Taken at the first stamp and held until the operations are appended. + guard: Option>, + device_id: String, + /// `op_id` -> reserved `seq`. + reserved: BTreeMap, +} + +impl PodcastStampSource for PodcastLogStamps<'_> { + fn device_id(&self) -> &str { + &self.device_id + } + + fn next_stamp(&mut self) -> Result { + let conn = self.guard.get_or_insert_with(|| lock(self.conn)); + let seq = get_meta(conn, "local_seq")? + .and_then(|v| v.parse::().ok()) + .unwrap_or(0) + + 1; + let last_hlc = get_meta(conn, "last_hlc_ms")? + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + let hlc_ms = now_ms().max(last_hlc + 1); + set_meta(conn, "local_seq", &seq.to_string())?; + set_meta(conn, "last_hlc_ms", &hlc_ms.to_string())?; + let op_id = format!("{}:{seq}", self.device_id); + self.reserved.insert(op_id.clone(), seq); + Ok(PodcastOpStamp::new(op_id, self.device_id.clone(), hlc_ms)) + } +} + +impl PodcastLogStamps<'_> { + fn append(mut self, ops: Vec) -> Result<()> { + if ops.len() != self.reserved.len() { + tracing::warn!( + reserved = self.reserved.len(), + returned = ops.len(), + "podcast action reserved stamps it did not return operations for" + ); + } + if ops.is_empty() { + return Ok(()); + } + let conn = self.guard.get_or_insert_with(|| lock(self.conn)); + for op in ops { + let Some(&seq) = self.reserved.get(&op.stamp.op_id) else { + anyhow::bail!( + "podcast operation {} was not stamped by this sync log", + op.stamp.op_id + ); + }; + let op = op.into_wire(seq); + let payload = serde_json::to_value(&op.payload)?; + let kind = payload_kind(&op.payload); + conn.execute( + "INSERT OR IGNORE INTO sync_ops + (op_id, origin_device_id, seq, kind, payload_json, hlc_ms, + received_at_ms, tombstone) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)", + params![ + op.op_id, + op.origin_device_id, + op.seq, + kind, + payload.to_string(), + op.hlc_ms, + now_ms(), + i64::from(op.payload.is_tombstone()), + ], + )?; + coalesce_progress_ops(conn, &op)?; + conn.execute( + "INSERT INTO sync_vectors (device_id, max_seq) + VALUES (?1, ?2) + ON CONFLICT(device_id) DO UPDATE SET + max_seq = MAX(max_seq, excluded.max_seq)", + params![op.origin_device_id, op.seq], + )?; + tracing::debug!(op_id = %op.op_id, kind, "recorded podcast sync op"); + } + Ok(()) + } +} + +impl crate::podcasts::OpLog for DeviceSync { + fn record( + &self, + action: &mut dyn FnMut(&mut dyn PodcastStampSource) -> Result>, + ) -> Result<()> { + self.run_podcast_action(|stamps| Ok(((), action(stamps)?))) + } +} + #[derive(Debug)] struct StoredDevice { device_id: String, @@ -3015,9 +3542,10 @@ async fn handle_pair_request( (request_id, device_id, name, client_version, endpoint_id, endpoint_ticket, invite_id, created_at_ms, status, requester_group_id, requester_group_active_devices, - requester_group_devices_json, use_requester_group) + requester_group_devices_json, use_requester_group, + protocol_version) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, 'pending', - ?9, ?10, ?11, 0)", + ?9, ?10, ?11, 0, ?12)", params![ request_id, profile.device_id, @@ -3030,6 +3558,7 @@ async fn handle_pair_request( requester_group_id.as_deref(), requester_group_active_devices as i64, requester_group_devices_json, + profile.protocol_version, ], )? }; @@ -3107,14 +3636,17 @@ async fn handle_pair_request( { sync.set_group_id(group_id)?; response_group_id = group_id.to_string(); - sync.apply_device_profiles(&requester_group_devices)?; + sync.apply_device_profiles(&requester_group_devices, Some(&profile.device_id))?; } - sync.apply_device_profile(&profile, true)?; + sync.apply_device_profile( + &profile, + true, + VersionReport::SelfReported(Some(profile.protocol_version)), + )?; if let Some(playback) = playback { sync.apply_playback_snapshot(&profile.device_id, playback)?; } - sync.apply_snapshot(snapshot)?; - sync.apply_ops(ops)?; + sync.apply_received(snapshot, ops)?; sync.note_peer_vector(&profile.device_id, &vector)?; { let conn = lock(&sync.conn); @@ -3126,9 +3658,10 @@ async fn handle_pair_request( sync.set_last_sync(Some(format!("paired {}", short_id(&profile.device_id))))?; let devices = sync.device_profiles()?; let vector = sync.vector()?; - let ops = sync.ops_for_peer(&profile.device_id)?; - let snapshot = sync.snapshot()?; - let playback = sync.local_playback_snapshot(); + let peer_version = sync.peer_protocol_version(&profile.device_id)?; + let ops = sync.ops_for_peer(&profile.device_id, peer_version)?; + let snapshot = sync.snapshot_for(peer_version)?; + let playback = sync.local_playback_snapshot_for(peer_version); write_msg( &mut stream, &WireMessage::PairResponse { @@ -3198,13 +3731,16 @@ async fn handle_hello( return Ok(()); } profile.endpoint_id = stream.peer_id.to_string(); - sync.apply_device_profile(&profile, false)?; - sync.apply_device_profiles(&devices)?; + sync.apply_device_profile( + &profile, + false, + VersionReport::SelfReported(Some(profile.protocol_version)), + )?; + sync.apply_device_profiles(&devices, Some(&profile.device_id))?; if let Some(playback) = playback { sync.apply_playback_snapshot(&profile.device_id, playback)?; } - sync.apply_snapshot(snapshot)?; - sync.apply_ops(ops)?; + sync.apply_received(snapshot, ops)?; sync.note_peer_vector(&profile.device_id, &vector)?; sync.mark_seen(&profile.device_id, Some(stream.peer_id.to_string()))?; sync.set_last_sync(Some(format!("synced {}", short_id(&profile.device_id))))?; @@ -3213,9 +3749,10 @@ async fn handle_hello( let own_profile = sync.own_profile(&own_ticket)?; let devices = sync.device_profiles()?; let vector = sync.vector()?; - let ops = sync.ops_for_peer(&profile.device_id)?; - let snapshot = sync.snapshot()?; - let playback = sync.local_playback_snapshot(); + let peer_version = sync.peer_protocol_version(&profile.device_id)?; + let ops = sync.ops_for_peer(&profile.device_id, peer_version)?; + let snapshot = sync.snapshot_for(peer_version)?; + let playback = sync.local_playback_snapshot_for(peer_version); write_msg( &mut stream, &WireMessage::SyncResponse { @@ -3412,6 +3949,10 @@ CREATE TABLE IF NOT EXISTS sync_playback_applied ( op_id TEXT PRIMARY KEY, applied_at_ms INTEGER NOT NULL ); +CREATE TABLE IF NOT EXISTS sync_podcast_pending ( + op_id TEXT PRIMARY KEY +); +CREATE INDEX IF NOT EXISTS idx_sync_ops_kind ON sync_ops(kind); "#, )?; ensure_column( @@ -3441,6 +3982,13 @@ CREATE TABLE IF NOT EXISTS sync_playback_applied ( "ALTER TABLE sync_pending_pairing ADD COLUMN use_requester_group INTEGER NOT NULL DEFAULT 0", )?; + // Protocol version the requester reported in its pairing request. + ensure_column( + conn, + "sync_pending_pairing", + "protocol_version", + "ALTER TABLE sync_pending_pairing ADD COLUMN protocol_version INTEGER", + )?; Ok(()) } @@ -3486,6 +4034,41 @@ fn delete_meta(conn: &Connection, key: &str) -> Result<()> { Ok(()) } +/// Keeps only the newest `podcast_progress_set` of an origin per episode in +/// the log (the periodic checkpoints of a playing episode would grow it +/// without bound). +/// +/// Safe because the operations of one origin are ordered: a later one has +/// a later `hlc_ms` and never a lower progress generation, so it wins every +/// register comparison against the earlier ones. Peers that have not +/// received the removed operations get the later one, and their vector +/// skips the gap as usual. +fn coalesce_progress_ops(conn: &Connection, op: &SyncOpWire) -> Result<()> { + let SyncOpPayload::PodcastProgressSet { episode_id, .. } = &op.payload else { + return Ok(()); + }; + conn.execute( + "DELETE FROM sync_ops + WHERE origin_device_id = ?1 + AND kind = 'podcast_progress_set' + AND seq < ?2 + AND json_extract(payload_json, '$.episode_id') = ?3", + params![op.origin_device_id, op.seq, episode_id.as_str()], + )?; + Ok(()) +} + +/// Podcast operations and playback commands with podcast items carry feed +/// and audio URLs, which may hold private tokens: never log their payload. +fn payload_may_hold_private_urls(payload: &SyncOpPayload) -> bool { + payload.is_podcast() + || matches!( + payload, + SyncOpPayload::PlaybackCommand { command, .. } + if music_dht::device_sync::playback_command_has_podcasts(command) + ) +} + fn payload_kind(payload: &SyncOpPayload) -> &'static str { match payload { SyncOpPayload::TrackLikeSet { .. } => "track_like_set", @@ -3499,6 +4082,11 @@ fn payload_kind(payload: &SyncOpPayload) -> &'static str { SyncOpPayload::DeviceRevoked { .. } => "device_revoked", SyncOpPayload::PlaybackCommand { .. } => "playback_command", SyncOpPayload::ListenRecorded { .. } => "listen_recorded", + SyncOpPayload::PodcastSubscriptionSet { .. } => "podcast_subscription_set", + SyncOpPayload::PodcastShareSet { .. } => "podcast_share_set", + SyncOpPayload::PodcastFeedAliasesAdded { .. } => "podcast_feed_aliases_added", + SyncOpPayload::PodcastProgressSet { .. } => "podcast_progress_set", + SyncOpPayload::PodcastListenRecorded { .. } => "podcast_listen_recorded", } } @@ -3659,55 +4247,47 @@ fn peer_ack_floor_label(conn: &Connection) -> Result { } async fn write_msg(stream: &mut ByteStream, message: &WireMessage) -> Result<()> { - let mut payload = serde_json::to_vec(message)?; - payload.push(b'\n'); - stream.send.write_all(&payload).await?; + let mut message = message.clone(); + fit_podcast_snapshot(&mut message); + music_dht::device_sync::write_msg(stream, &message).await?; Ok(()) } -async fn finish_send(stream: &mut ByteStream) -> Result<()> { - stream.send.finish()?; - Ok(()) +fn message_snapshot(message: &mut WireMessage) -> &mut SyncSnapshot { + match message { + WireMessage::PairRequest { snapshot, .. } + | WireMessage::PairResponse { snapshot, .. } + | WireMessage::Hello { snapshot, .. } + | WireMessage::SyncResponse { snapshot, .. } => snapshot, + } +} + +/// Bounds the podcast snapshot sections so the whole line stays under the +/// protocol limit (`podcast_snapshot_budget`): the operations, music +/// snapshot and playback queue of the same message come first. +fn fit_podcast_snapshot(message: &mut WireMessage) { + if !message_snapshot(message).has_podcast_state() { + return; + } + let podcast = message_snapshot(message).clone(); + message_snapshot(message).strip_podcast_state(); + let other_bytes = serde_json::to_vec(message).map_or(usize::MAX, |bytes| bytes.len()); + let snapshot = message_snapshot(message); + *snapshot = podcast; + snapshot.bound_podcast_state_to(music_dht::device_sync::podcast_snapshot_budget(other_bytes)); } async fn finish_response(stream: &mut ByteStream) -> Result<()> { - stream.send.finish()?; - let _ = tokio::time::timeout(RESPONSE_DRAIN_TIMEOUT, stream.send.stopped()).await; + music_dht::device_sync::finish_response(stream, RESPONSE_DRAIN_TIMEOUT).await?; Ok(()) } -async fn read_msg(stream: &mut ByteStream) -> Result { - let line = read_line(&mut stream.recv).await?; - Ok(serde_json::from_slice(&line)?) -} - -async fn read_line(reader: &mut R) -> Result> { - let mut out = Vec::new(); - loop { - let mut byte = [0u8; 1]; - let read = reader.read(&mut byte).await?; - if read == 0 { - break; - } - if byte[0] == b'\n' { - break; - } - out.push(byte[0]); - if out.len() > MAX_LINE { - anyhow::bail!("protocol line is too large"); - } - } - Ok(out) -} - fn parse_invite(value: &str) -> Result { - let Some(token) = value.trim().strip_prefix("frid://i/") else { - anyhow::bail!("usage: :connect frid://i/"); - }; - let bytes = base64url_decode(token)?; - let invite: InviteWire = serde_json::from_slice(&bytes)?; - anyhow::ensure!(invite.v == 1, "unsupported invite version"); - Ok(invite) + anyhow::ensure!( + value.trim().starts_with("frid://i/"), + "usage: :connect frid://i/" + ); + Ok(music_dht::device_sync::parse_invite(value)?) } pub fn invite_network_id(value: &str) -> Result { @@ -3719,28 +4299,10 @@ pub fn invite_network_id(value: &str) -> Result { Ok(ticket.network_id) } -fn hash_secret(secret: &str) -> String { - blake3::hash(secret.as_bytes()).to_hex().to_string() -} - -fn random_hex(bytes: usize) -> String { - let key = SecretKey::generate(); - let mut seed = key.to_bytes().to_vec(); - while seed.len() < bytes { - seed.extend_from_slice(blake3::hash(&seed).as_bytes()); - } - hex_encode(&seed[..bytes]) -} - fn hex_encode(bytes: &[u8]) -> String { bytes.iter().map(|byte| format!("{byte:02x}")).collect() } -fn ticket_endpoint_id(ticket: &str) -> Option { - let ticket = PeerTicket::from_str(ticket).ok()?; - Some(ticket.endpoint_id().to_string()) -} - fn default_device_name(device_id: &str) -> String { let host = std::env::var("HOSTNAME") .or_else(|_| std::env::var("COMPUTERNAME")) @@ -3754,59 +4316,6 @@ fn short_id(value: &str) -> String { value.chars().take(10).collect() } -fn base64url_encode(bytes: &[u8]) -> String { - const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - let mut out = String::new(); - let mut i = 0; - while i < bytes.len() { - let b0 = bytes[i]; - let b1 = bytes.get(i + 1).copied().unwrap_or(0); - let b2 = bytes.get(i + 2).copied().unwrap_or(0); - out.push(TABLE[(b0 >> 2) as usize] as char); - out.push(TABLE[(((b0 & 0b0000_0011) << 4) | (b1 >> 4)) as usize] as char); - if i + 1 < bytes.len() { - out.push(TABLE[(((b1 & 0b0000_1111) << 2) | (b2 >> 6)) as usize] as char); - } - if i + 2 < bytes.len() { - out.push(TABLE[(b2 & 0b0011_1111) as usize] as char); - } - i += 3; - } - out -} - -fn base64url_decode(value: &str) -> Result> { - fn val(byte: u8) -> Option { - match byte { - b'A'..=b'Z' => Some(byte - b'A'), - b'a'..=b'z' => Some(byte - b'a' + 26), - b'0'..=b'9' => Some(byte - b'0' + 52), - b'-' => Some(62), - b'_' => Some(63), - _ => None, - } - } - let bytes = value.as_bytes(); - let mut out = Vec::new(); - let mut i = 0; - while i < bytes.len() { - let a = val(bytes[i]).context("invalid base64url invite")?; - let b = val(*bytes.get(i + 1).context("truncated base64url invite")?) - .context("invalid base64url invite")?; - let c = bytes.get(i + 2).and_then(|byte| val(*byte)); - let d = bytes.get(i + 3).and_then(|byte| val(*byte)); - out.push((a << 2) | (b >> 4)); - if let Some(c) = c { - out.push(((b & 0b0000_1111) << 4) | (c >> 2)); - if let Some(d) = d { - out.push(((c & 0b0000_0011) << 6) | d); - } - } - i += 4; - } - Ok(out) -} - #[cfg(test)] #[path = "devices/tests.rs"] mod tests; @@ -3814,6 +4323,27 @@ mod tests; #[cfg(test)] mod interop_tests; +#[cfg(test)] +mod wire_compat_tests; + +#[cfg(test)] +mod podcast_sync_tests; + +/// Runs podcast store work from an async device-sync handler without +/// stalling a Tokio worker: the store may be busy with a feed refresh (one +/// transaction, up to about half a second for a very large feed). On the +/// multi-thread runtime the worker hands its other tasks over while `work` +/// runs (`block_in_place`); elsewhere (blocking pool, tests without a +/// runtime, a current-thread runtime) it simply runs. +fn off_worker(work: impl FnOnce() -> T) -> T { + match tokio::runtime::Handle::try_current() { + Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => { + tokio::task::block_in_place(work) + } + _ => work(), + } +} + fn playback_clock() -> u64 { static START: std::sync::OnceLock = std::sync::OnceLock::new(); START diff --git a/src/devices/fixtures/v3_hello.golden.json b/src/devices/fixtures/v3_hello.golden.json new file mode 100644 index 0000000..ad7622e --- /dev/null +++ b/src/devices/fixtures/v3_hello.golden.json @@ -0,0 +1 @@ +{"type":"hello","group_id":"grp_1","profile":{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},"devices":[{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},{"device_id":"web_b","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5}],"vector":{"tui_a":12,"web_b":4},"ops":[{"op_id":"tui_a:1","origin_device_id":"tui_a","seq":1,"hlc_ms":1700000000001,"payload":{"kind":"track_like_set","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","liked":true,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}}},{"op_id":"tui_a:2","origin_device_id":"tui_a","seq":2,"hlc_ms":1700000000002,"payload":{"kind":"playlist_created","playlist_id":"pl","title":"Mix"}},{"op_id":"tui_a:3","origin_device_id":"tui_a","seq":3,"hlc_ms":1700000000003,"payload":{"kind":"playlist_renamed","playlist_id":"pl","title":"Mix 2"}},{"op_id":"tui_a:4","origin_device_id":"tui_a","seq":4,"hlc_ms":1700000000004,"payload":{"kind":"playlist_track_added","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","position":0,"fed":null}},{"op_id":"tui_a:5","origin_device_id":"tui_a","seq":5,"hlc_ms":1700000000005,"payload":{"kind":"playlist_track_removed","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111"}},{"op_id":"tui_a:6","origin_device_id":"tui_a","seq":6,"hlc_ms":1700000000006,"payload":{"kind":"playlist_deleted","playlist_id":"pl"}},{"op_id":"tui_a:7","origin_device_id":"tui_a","seq":7,"hlc_ms":1700000000007,"payload":{"kind":"device_profile_set","name":"TUI","client_version":"0.3.4","endpoint_ticket":"t","endpoint_id":"e"}},{"op_id":"tui_a:8","origin_device_id":"tui_a","seq":8,"hlc_ms":1700000000008,"payload":{"kind":"device_trusted","target_device_id":"web_b"}},{"op_id":"tui_a:9","origin_device_id":"tui_a","seq":9,"hlc_ms":1700000000009,"payload":{"kind":"device_revoked","target_device_id":"old","target_max_seq_seen":3}},{"op_id":"tui_a:10","origin_device_id":"tui_a","seq":10,"hlc_ms":1700000000010,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"set_state","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","track_number":null,"disc_number":null,"duration_seconds":0.0,"artist_names":[],"featured_artist_names":[],"release_id":0,"release_title":"","release_year":null,"file_path":"","content_id":null,"audio_format":null,"audio_bitrate":null,"audio_sample_rate":null,"audio_bit_depth":null,"file_size_bytes":null,"play_count":0,"fed":null}],"queue_pos":1,"playing":true,"paused":false,"idle_since_ms":null,"position_secs":42.5,"volume":73,"shuffle":true,"repeat":"all"},"seek":true},"authority":{"version":1,"claim":{"counter":3,"issuer":"tui_a","owner":"tui_a"},"sequence":9}}},{"op_id":"tui_a:11","origin_device_id":"tui_a","seq":11,"hlc_ms":1700000000011,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"active_changed","active_device_id":"web_b","active_device_name":"WEB","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","track_number":null,"disc_number":null,"duration_seconds":0.0,"artist_names":[],"featured_artist_names":[],"release_id":0,"release_title":"","release_year":null,"file_path":"","content_id":null,"audio_format":null,"audio_bitrate":null,"audio_sample_rate":null,"audio_bit_depth":null,"file_size_bytes":null,"play_count":0,"fed":null}],"queue_pos":1,"playing":true,"paused":false,"idle_since_ms":null,"position_secs":42.5,"volume":73,"shuffle":true,"repeat":"all"}},"authority":null}},{"op_id":"tui_a:12","origin_device_id":"tui_a","seq":12,"hlc_ms":1700000000012,"payload":{"kind":"listen_recorded","event":{"listen_id":"l1","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","started_at_ms":1,"listened_ms":60000,"track_duration_ms":180000,"ended_reason":"finished","track":{"title":"Song","artist_names":["A"],"featured_artist_names":[],"release_title":null}}}}],"snapshot":{"likes":[{"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","hlc_ms":1,"op_id":"tui_a:1","fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}}],"unlikes":[{"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","hlc_ms":2,"op_id":"tui_a:2"}],"playlists":[{"playlist_id":"pl","title":"Mix","hlc_ms":3,"op_id":"tui_a:3","items":[{"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","position":0,"hlc_ms":4,"op_id":"tui_a:4","fed":null}]}],"deleted_playlists":[{"playlist_id":"gone","hlc_ms":5,"op_id":"tui_a:5"}],"removed_playlist_items":[{"playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","hlc_ms":6,"op_id":"tui_a:6"}]},"playback":{"device_id":"tui_a","device_name":"TUI","active":true,"updated_at_ms":9,"state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","track_number":null,"disc_number":null,"duration_seconds":0.0,"artist_names":[],"featured_artist_names":[],"release_id":0,"release_title":"","release_year":null,"file_path":"","content_id":null,"audio_format":null,"audio_bitrate":null,"audio_sample_rate":null,"audio_bit_depth":null,"file_size_bytes":null,"play_count":0,"fed":null}],"queue_pos":1,"playing":true,"paused":false,"idle_since_ms":null,"position_secs":42.5,"volume":73,"shuffle":true,"repeat":"all"},"coordination":{"version":1,"claim":{"counter":3,"issuer":"tui_a","owner":"tui_a"},"heartbeat":4,"available":true,"playing":true}}} diff --git a/src/devices/interop_tests.rs b/src/devices/interop_tests.rs index 4c7c802..60fc2a2 100644 --- a/src/devices/interop_tests.rs +++ b/src/devices/interop_tests.rs @@ -25,6 +25,7 @@ async fn localhost_web_peer() { library: Arc::new(Library::open(&dir.join("interop-library.sqlite3")).unwrap()), event_tx: Default::default(), playback: Default::default(), + podcasts: Default::default(), }; let id = sync.ensure_identity().unwrap(); let (tx, mut events) = tokio::sync::mpsc::unbounded_channel(); @@ -55,13 +56,16 @@ async fn localhost_web_peer() { vector: BTreeMap::new(), ops: vec![], snapshot: SyncSnapshot::default(), - playback: sync.local_playback_snapshot(), + playback: sync.local_playback_snapshot_for(PROTOCOL_VERSION), }; let mut bytes = serde_json::to_vec(&hello).unwrap(); bytes.push(b'\n'); stream.write_all(&bytes).await.unwrap(); let response: WireMessage = - serde_json::from_slice(&read_line(&mut stream).await.unwrap()).unwrap(); + serde_json::from_slice( + &music_dht::device_sync::read_line(&mut stream).await.unwrap(), + ) + .unwrap(); let WireMessage::SyncResponse { accepted: true, playback: Some(snapshot), diff --git a/src/devices/podcast_sync_tests.rs b/src/devices/podcast_sync_tests.rs new file mode 100644 index 0000000..e17e718 --- /dev/null +++ b/src/devices/podcast_sync_tests.rs @@ -0,0 +1,1248 @@ +//! Podcast sync between two (or three) TUI device stores, with the same +//! functions the stream handlers use (`ops_for_peer`, `snapshot_for`, +//! `local_playback_snapshot_for`, `apply_*`). Every message goes through its +//! JSON line, like on the wire. The network round trip itself is covered by +//! `localhost_devices_sync_podcasts` in `tests.rs`. + +use super::*; +use furumi_library::podcasts::{ + CheckpointOutcome, PodcastListFilter, ProgressSession, SubscribeOutcome, +}; +use music_dht::podcast::{PodcastEpisodeRef, PodcastId, PodcastMeta, ProgressStatus}; +use tokio::sync::mpsc::UnboundedReceiver; + +pub(super) struct Device { + pub(super) sync: DeviceSync, + pub(super) store: Arc, + pub(super) events: UnboundedReceiver, + pub(super) id: String, + _library_dir: tempfile::TempDir, +} + +/// A device with an in-memory sync log and its podcast store attached. +pub(super) fn device() -> Device { + device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), true) +} + +pub(super) fn device_with_store(store: Arc, attach: bool) -> Device { + let library_dir = tempfile::tempdir().unwrap(); + let conn = Connection::open_in_memory().unwrap(); + init_schema(&conn).unwrap(); + let sync = DeviceSync { + _identity_lock: None, + conn: Arc::new(std::sync::Mutex::new(conn)), + library: Arc::new(Library::open(&library_dir.path().join("library.sqlite3")).unwrap()), + event_tx: Default::default(), + playback: Default::default(), + podcasts: Default::default(), + }; + let (tx, events) = tokio::sync::mpsc::unbounded_channel(); + sync.set_event_tx(tx); + let id = sync.ensure_identity().unwrap().device_id; + if attach { + sync.attach_podcast_store(Arc::clone(&store)).unwrap(); + } + Device { + sync, + store, + events, + id, + _library_dir: library_dir, + } +} + +/// Trusts `b` on `a` and the other way round, in `a`'s group, with the +/// versions their profiles report. +pub(super) fn pair(a: &Device, b: &Device) { + b.sync + .set_group_id(&a.sync.ensure_identity().unwrap().group_id) + .unwrap(); + for (left, right) in [(a, b), (b, a)] { + let profile = right.sync.own_profile("").unwrap(); + left.sync + .apply_device_profile( + &profile, + true, + VersionReport::SelfReported(Some(profile.protocol_version)), + ) + .unwrap(); + } +} + +/// The `hello` that `sync_device` sends from `from` to `to`, as JSON. +pub(super) fn hello_json(from: &Device, to_id: &str) -> String { + let version = from.sync.peer_protocol_version(to_id).unwrap(); + let mut message = WireMessage::Hello { + group_id: from.sync.ensure_identity().unwrap().group_id, + profile: from.sync.own_profile("").unwrap(), + devices: from.sync.device_profiles().unwrap(), + vector: from.sync.vector().unwrap(), + ops: from.sync.ops_for_peer(to_id, version).unwrap(), + snapshot: from.sync.snapshot_for(version).unwrap(), + playback: from.sync.local_playback_snapshot_for(version), + }; + fit_podcast_snapshot(&mut message); + serde_json::to_string(&message).unwrap() +} + +/// What `handle_hello` does with a `hello` line, minus the transport. +pub(super) fn receive(to: &DeviceSync, json: &str) { + let WireMessage::Hello { + profile, + devices, + vector, + ops, + snapshot, + playback, + .. + } = serde_json::from_str(json).unwrap() + else { + panic!("expected a hello"); + }; + to.apply_device_profile( + &profile, + false, + VersionReport::SelfReported(Some(profile.protocol_version)), + ) + .unwrap(); + to.apply_device_profiles(&devices, Some(&profile.device_id)) + .unwrap(); + if let Some(playback) = playback { + to.apply_playback_snapshot(&profile.device_id, playback) + .unwrap(); + } + to.apply_received(snapshot, ops).unwrap(); + to.note_peer_vector(&profile.device_id, &vector).unwrap(); +} + +pub(super) fn deliver(from: &Device, to: &Device) { + receive(&to.sync, &hello_json(from, &to.id)); +} + +/// One sync in each direction. +pub(super) fn sync_round(a: &Device, b: &Device) { + deliver(a, b); + deliver(b, a); +} + +pub(super) fn meta() -> PodcastMeta { + crate::app::podcast_fixtures::row("Show", 0, 0, 0) + .summary + .meta +} + +pub(super) fn episode_ref(n: u32) -> PodcastEpisodeRef { + crate::app::podcast_fixtures::episode(n, &format!("Episode {n}"), None) + .episode + .to_ref(&meta()) +} + +pub(super) fn subscribe(device: &Device) -> SubscribeOutcome { + let store = Arc::clone(&device.store); + device + .sync + .run_podcast_action(|stamps| { + let outcome = store.subscribe(stamps, &meta())?; + let ops = outcome.ops.clone(); + Ok((outcome, ops)) + }) + .unwrap() +} + +pub(super) fn start(device: &Device, episode: &PodcastEpisodeRef, at_ms: i64) -> ProgressSession { + let store = Arc::clone(&device.store); + let session_id = device.sync.new_listen_id(); + device + .sync + .run_podcast_action(|stamps| { + let write = store.start_session(stamps, episode, &session_id, at_ms, None)?; + Ok((write.session.clone(), vec![write.op])) + }) + .unwrap() +} + +pub(super) fn checkpoint( + device: &Device, + session: &ProgressSession, + at_ms: i64, +) -> CheckpointOutcome { + let store = Arc::clone(&device.store); + device + .sync + .run_podcast_action(|stamps| { + let outcome = store.checkpoint(stamps, session, at_ms)?; + let ops = match &outcome { + CheckpointOutcome::Written(write) => vec![write.op.clone()], + _ => Vec::new(), + }; + Ok((outcome, ops)) + }) + .unwrap() +} + +fn restart(device: &Device, episode: &PodcastEpisodeRef) -> ProgressSession { + let store = Arc::clone(&device.store); + let session_id = device.sync.new_listen_id(); + device + .sync + .run_podcast_action(|stamps| { + let write = store.restart(stamps, episode, &session_id)?; + Ok((write.session.clone(), vec![write.op])) + }) + .unwrap() +} + +fn synced_events(device: &mut Device) -> Vec<(Vec, bool)> { + let mut events = Vec::new(); + while let Ok(event) = device.events.try_recv() { + if let AppEvent::PodcastsSynced { progress, changed } = event { + events.push((progress, changed)); + } + } + events +} + +fn subscribed_ids(store: &PodcastStore) -> Vec { + store + .list_podcasts(PodcastListFilter::Subscribed) + .unwrap() + .into_iter() + .map(|summary| summary.podcast_id) + .collect() +} + +fn logged_kinds(device: &Device) -> Vec { + let conn = lock(&device.sync.conn); + let mut stmt = conn + .prepare("SELECT kind FROM sync_ops ORDER BY seq") + .unwrap(); + stmt.query_map([], |row| row.get(0)) + .unwrap() + .collect::>>() + .unwrap() +} + +#[test] +fn a_subscription_on_one_device_appears_on_the_other_without_its_url() { + let a = device(); + let mut b = device(); + pair(&a, &b); + let outcome = subscribe(&a); + assert!(!outcome.already_subscribed); + + deliver(&a, &b); + assert_eq!( + subscribed_ids(&b.store), + std::slice::from_ref(&outcome.podcast_id) + ); + let podcast = b.store.podcast(&outcome.podcast_id).unwrap().unwrap(); + assert_eq!(podcast.meta.title, "Show"); + assert!(!podcast.shared, "synced subscriptions stay private"); + // B fetches the feed itself, from the synced URL. + assert_eq!( + b.store.podcasts_due_for_refresh(now_ms(), 16).unwrap(), + [(outcome.podcast_id.clone(), meta().feed_url)] + ); + // The UI hears about it once. + assert_eq!(synced_events(&mut b), [(Vec::new(), true)]); + + // Delivering the same state again (operations and snapshot) changes + // nothing and creates no duplicate. + receive(&b.sync, &hello_json(&a, &b.id)); + deliver(&a, &b); + assert_eq!(subscribed_ids(&b.store).len(), 1); + assert!(synced_events(&mut b).is_empty()); +} + +#[test] +fn progress_saved_on_one_device_resumes_on_the_other_after_a_restart() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("podcasts.sqlite3"); + let a = device(); + let mut b = device_with_store(Arc::new(PodcastStore::open(&path).unwrap()), true); + pair(&a, &b); + subscribe(&a); + let episode = episode_ref(1); + let session = start(&a, &episode, 0); + checkpoint(&a, &session, 600_000); + checkpoint(&a, &session, 1_800_000); + // Periodic checkpoints are coalesced: one progress operation remains. + assert_eq!( + logged_kinds(&a) + .iter() + .filter(|kind| *kind == "podcast_progress_set") + .count(), + 1 + ); + + deliver(&a, &b); + assert_eq!( + b.store.resume_position_ms(&episode.episode_id).unwrap(), + 1_800_000 + ); + assert!(!synced_events(&mut b).is_empty()); + // A later checkpoint arrives as an operation only: the UI gets the + // register to show in place. + checkpoint(&a, &session, 1_830_000); + deliver(&a, &b); + let events = synced_events(&mut b); + assert!( + events + .iter() + .flat_map(|(progress, _)| progress) + .any(|record| record.position_ms == 1_830_000), + "{events:?}" + ); + + // Days later: B starts again with the same files. + drop(b); + let store = Arc::new(PodcastStore::open(&path).unwrap()); + let b = device_with_store(Arc::clone(&store), true); + let queued = TrackItem::podcast_episode(episode.clone()); + let (resolved, start_ms) = crate::podcasts::playback::resolve( + Some(&b.store), + queued.podcast_episode_ref().unwrap(), + crate::podcasts::playback::StartAt::Resume, + ) + .unwrap(); + assert_eq!(start_ms, 1_830_000); + assert_eq!(resolved.episode_id, episode.episode_id); + assert!(resolved.progress_generation >= Some(1)); +} + +#[test] +fn start_over_on_one_device_beats_a_late_checkpoint_from_the_other() { + let a = device(); + let b = device(); + pair(&a, &b); + subscribe(&a); + let episode = episode_ref(2); + let session_a = start(&a, &episode, 0); + checkpoint(&a, &session_a, 2_400_000); + sync_round(&a, &b); + assert_eq!( + b.store.resume_position_ms(&episode.episode_id).unwrap(), + 2_400_000 + ); + + // B starts over while A (offline) still plays and saves a checkpoint + // later in wall-clock time. + restart(&b, &episode); + std::thread::sleep(Duration::from_millis(5)); + assert!(matches!( + checkpoint(&a, &session_a, 2_460_000), + CheckpointOutcome::Written(_) + )); + // Delivery in both orders converges on the start over. + deliver(&a, &b); + deliver(&b, &a); + for device in [&a, &b] { + let progress = device.store.progress(&episode.episode_id).unwrap().unwrap(); + assert_eq!(progress.position_ms, 0, "{progress:?}"); + assert_eq!(progress.status, ProgressStatus::InProgress); + assert_eq!(progress.device_id, b.id); + } + // A's session stops saving: its next checkpoint is superseded. + assert!(matches!( + checkpoint(&a, &session_a, 2_470_000), + CheckpointOutcome::Superseded { .. } + )); +} + +#[test] +fn unsubscribing_keeps_progress_and_resubscribing_continues() { + let a = device(); + let b = device(); + pair(&a, &b); + let podcast_id = subscribe(&a).podcast_id; + let episode = episode_ref(3); + let session = start(&a, &episode, 0); + checkpoint(&a, &session, 900_000); + sync_round(&a, &b); + + let store = Arc::clone(&a.store); + a.sync + .run_podcast_action(|stamps| { + let ops = store.unsubscribe(stamps, &podcast_id)?; + Ok(((), ops)) + }) + .unwrap(); + sync_round(&a, &b); + for device in [&a, &b] { + assert!(subscribed_ids(&device.store).is_empty()); + assert!( + !device + .store + .subscription(&podcast_id) + .unwrap() + .unwrap() + .subscribed + ); + assert_eq!( + device + .store + .resume_position_ms(&episode.episode_id) + .unwrap(), + 900_000 + ); + } + + // Subscribing again on B reuses the podcast and its progress, and the + // later decision wins on A as well. + let again = subscribe(&b); + assert_eq!(again.podcast_id, podcast_id); + sync_round(&b, &a); + for device in [&a, &b] { + assert_eq!( + subscribed_ids(&device.store), + std::slice::from_ref(&podcast_id) + ); + assert_eq!( + device + .store + .resume_position_ms(&episode.episode_id) + .unwrap(), + 900_000 + ); + } +} + +#[test] +fn operations_received_before_the_store_opens_are_applied_when_it_does() { + let a = device(); + let mut b = device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), false); + pair(&a, &b); + let podcast_id = subscribe(&a).podcast_id; + let episode = episode_ref(4); + let session = start(&a, &episode, 0); + checkpoint(&a, &session, 120_000); + deliver(&a, &b); + assert!(subscribed_ids(&b.store).is_empty()); + // The operations are in B's log (and acknowledged) already: nothing + // would deliver them again. + assert!( + logged_kinds(&b) + .iter() + .any(|kind| kind == "podcast_subscription_set") + ); + + b.sync.attach_podcast_store(Arc::clone(&b.store)).unwrap(); + assert_eq!(subscribed_ids(&b.store), [podcast_id]); + assert_eq!( + b.store.resume_position_ms(&episode.episode_id).unwrap(), + 120_000 + ); + assert!(!synced_events(&mut b).is_empty()); + let pending: i64 = lock(&b.sync.conn) + .query_row("SELECT COUNT(*) FROM sync_podcast_pending", [], |row| { + row.get(0) + }) + .unwrap(); + assert_eq!(pending, 0); +} + +#[test] +fn podcast_operations_are_relayed_to_devices_that_were_not_online() { + let a = device(); + let b = device(); + let c = device(); + pair(&a, &b); + pair(&b, &c); + let podcast_id = subscribe(&a).podcast_id; + deliver(&a, &b); + // C never talks to A. + deliver(&b, &c); + assert_eq!(subscribed_ids(&c.store), [podcast_id]); +} + +/// A peer that still runs protocol v3 (furumi-tui 0.3.x, furumi-web +/// 0.12.x), simulated with its exact JSON types. +#[test] +fn a_v3_peer_gets_no_podcast_state_and_catches_up_from_the_snapshot_after_upgrading() { + use super::wire_compat_tests::legacy; + + let mut a = device(); + let old = device(); + a.sync.set_group_id("grp_test").unwrap(); + let old_profile = legacy::DeviceProfileWire { + device_id: old.id.clone(), + name: "old".into(), + client_version: "0.3.5".into(), + protocol_version: 3, + endpoint_id: String::new(), + endpoint_ticket: String::new(), + revoked: false, + revoke_cutoff_seq: None, + updated_at_ms: 1, + }; + // Its hello, as a 0.3.x client writes it, with a profile announcement + // that has no version field. + let old_hello = legacy::WireMessage::Hello { + group_id: "grp_test".into(), + profile: old_profile.clone(), + devices: vec![old_profile.clone()], + vector: BTreeMap::from([(old.id.clone(), 1)]), + ops: vec![legacy::SyncOpWire { + op_id: format!("{}:1", old.id), + origin_device_id: old.id.clone(), + seq: 1, + hlc_ms: 1, + payload: legacy::SyncOpPayload::DeviceProfileSet { + name: "old".into(), + client_version: "0.3.5".into(), + endpoint_ticket: String::new(), + endpoint_id: String::new(), + }, + }], + snapshot: legacy::SyncSnapshot::default(), + playback: None, + }; + a.sync.apply_device_trusted(&old.id, 1).unwrap(); + receive(&a.sync, &serde_json::to_string(&old_hello).unwrap()); + assert_eq!(a.sync.peer_protocol_version(&old.id).unwrap(), 3); + + let podcast_id = subscribe(&a).podcast_id; + let episode = episode_ref(5); + let session = start(&a, &episode, 0); + checkpoint(&a, &session, 300_000); + a.sync + .record_content_like(&format!("b3:{}", "c".repeat(64)), true) + .unwrap(); + a.sync.publish_playback(PlaybackSnapshot { + device_id: a.id.clone(), + device_name: "A".into(), + active: true, + updated_at_ms: now_ms(), + state: PlaybackStateWire { + queue: vec![PlaybackTrack::podcast_episode(episode.clone())], + queue_pos: 0, + playing: true, + paused: false, + idle_since_ms: None, + position_secs: 300.0, + volume: 80, + shuffle: false, + repeat: PlaybackRepeat::Off, + playback_rate: None, + }, + coordination: None, + }); + + // What A sends the v3 peer parses with the v3 types and has no podcast + // operation, podcast section or podcast-bearing playback state. + let json = hello_json(&a, &old.id); + let parsed: legacy::WireMessage = serde_json::from_str(&json).unwrap(); + let legacy::WireMessage::Hello { + ops, + snapshot, + playback, + vector, + .. + } = parsed + else { + panic!("expected a hello"); + }; + assert!(!json.contains("podcast"), "{json}"); + assert!(!json.contains(&episode.audio_url), "{json}"); + assert_eq!(ops.len(), 1, "only the like: {ops:?}"); + assert_eq!(snapshot.likes.len(), 1); + // The playback snapshot comes for its coordination, with the episode + // redacted to an opaque placeholder. + let playback = playback.expect("coordination for the old engine"); + assert!(playback.coordination.is_some()); + assert_eq!(playback.state.queue.len(), 1); + assert_eq!( + playback.state.queue[0].title, + music_dht::device_sync::PRIVATE_ITEM_TITLE + ); + assert_eq!(playback.state.queue[0].id, 0); + assert_eq!(playback.state.position_secs, 300.0); + // The old peer acknowledges everything it was sent. + a.sync.note_peer_vector(&old.id, &vector).unwrap(); + + // After upgrading, its hello reports v4: the skipped operations are not + // sent again, but the snapshot carries the podcast state. + receive(&a.sync, &hello_json(&old, &a.id)); + assert_eq!( + a.sync.peer_protocol_version(&old.id).unwrap(), + PROTOCOL_VERSION + ); + let json = hello_json(&a, &old.id); + let WireMessage::Hello { + ops, + snapshot, + playback, + .. + } = serde_json::from_str(&json).unwrap() + else { + panic!("expected a hello"); + }; + assert!(ops.iter().all(|op| !op.payload.is_podcast()), "{ops:?}"); + assert_eq!(snapshot.podcast_subscriptions.len(), 1); + assert_eq!(snapshot.podcast_progress.len(), 1); + assert!(playback.is_some()); + receive(&old.sync, &json); + assert_eq!(subscribed_ids(&old.store), [podcast_id]); + assert_eq!( + old.store.resume_position_ms(&episode.episode_id).unwrap(), + 300_000 + ); + let _ = synced_events(&mut a); +} + +#[test] +fn podcast_snapshot_sections_shrink_to_fit_the_line_limit() { + let a = device(); + let b = device(); + pair(&a, &b); + subscribe(&a); + let mut message = WireMessage::Hello { + group_id: String::new(), + profile: a.sync.own_profile("").unwrap(), + devices: Vec::new(), + vector: BTreeMap::new(), + ops: Vec::new(), + snapshot: a.sync.snapshot_for(PROTOCOL_VERSION).unwrap(), + playback: None, + }; + assert_eq!( + message_snapshot(&mut message).podcast_subscriptions.len(), + 1 + ); + // A music part that leaves no room for podcast state. + message_snapshot(&mut message).likes = (0..70_000) + .map(|n| SnapshotLike { + content_id: format!("b3:{n:064x}"), + hlc_ms: n, + op_id: format!("x:{n}"), + fed: None, + }) + .collect(); + fit_podcast_snapshot(&mut message); + assert!(!message_snapshot(&mut message).has_podcast_state()); + // The music part is never cut for podcast state. + assert_eq!(message_snapshot(&mut message).likes.len(), 70_000); +} + +fn state_with(queue: Vec, position_secs: f64) -> PlaybackStateWire { + PlaybackStateWire { + queue, + queue_pos: 0, + playing: true, + paused: true, + idle_since_ms: None, + position_secs, + volume: 80, + shuffle: false, + repeat: PlaybackRepeat::Off, + playback_rate: None, + } +} + +fn publish(device: &Device, state: PlaybackStateWire) { + device.sync.publish_playback(PlaybackSnapshot { + device_id: device.id.clone(), + device_name: "A".into(), + active: true, + updated_at_ms: now_ms(), + state, + coordination: None, + }); +} + +fn music_wire() -> PlaybackTrack { + let mut track = PlaybackTrack::private_placeholder(); + track.title = "Song".into(); + track.content_id = Some(format!("b3:{}", "d".repeat(64))); + track.source = None; + track +} + +#[test] +fn a_handoff_between_v4_devices_carries_the_episode_and_its_position() { + let a = device(); + let mut b = device(); + pair(&a, &b); + subscribe(&a); + let episode = episode_ref(6); + let session = start(&a, &episode, 0); + checkpoint(&a, &session, 1_000_000); + let mut queued = TrackItem::podcast_episode(episode.clone()); + queued.podcast.as_mut().unwrap().progress_generation = Some(session.generation); + let state = state_with( + vec![PlaybackTrack::from_track(&queued), music_wire()], + 1_000.5, + ); + a.sync + .ensure_playback_supported_by(&b.id, &state) + .expect("B speaks v4"); + a.sync.claim_playback(&a.id).unwrap(); + a.sync + .record_playback_command( + &b.id, + PlaybackCommand::ActiveChanged { + active_device_id: b.id.clone(), + active_device_name: "B".into(), + state, + }, + ) + .unwrap(); + deliver(&a, &b); + let mut handed = None; + while let Ok(event) = b.events.try_recv() { + if let AppEvent::PlaybackCommand { + command: PlaybackCommand::ActiveChanged { state, .. }, + .. + } = event + { + handed = Some(state); + } + } + let handed = handed.expect("the handoff reaches B's player"); + assert_eq!(handed.position_secs, 1_000.5); + assert!(handed.paused); + assert_eq!(handed.playback_rate, None); + let items = track_items_from_wire(&handed.queue, PlaybackTrack::to_track_item); + let item = items[0].podcast_episode_ref().expect("still an episode"); + assert_eq!(item.episode_id, episode.episode_id); + assert_eq!(item.audio_url, episode.audio_url); + assert_eq!(item.progress_generation, Some(session.generation)); + assert!(!items[1].is_podcast()); +} + +/// Trusts `dev_old`, a furumi-tui 0.3.x device ("Old laptop", sync v3). +fn trust_old_device(a: &Device) { + a.sync.apply_device_trusted("dev_old", 1).unwrap(); + a.sync + .apply_device_profile( + &DeviceProfileWire { + device_id: "dev_old".into(), + name: "Old laptop".into(), + client_version: "0.3.5".into(), + protocol_version: 3, + endpoint_id: String::new(), + endpoint_ticket: String::new(), + revoked: false, + revoke_cutoff_seq: None, + updated_at_ms: 1, + }, + false, + VersionReport::SelfReported(Some(3)), + ) + .unwrap(); +} + +#[test] +fn a_podcast_queue_is_not_handed_to_or_replaced_by_a_v3_device() { + let mut a = device(); + trust_old_device(&a); + let with_episode = state_with( + vec![music_wire(), PlaybackTrack::podcast_episode(episode_ref(7))], + 12.0, + ); + // Handoff to it: refused with the reason, before ownership changes. + let error = a + .sync + .ensure_playback_supported_by("dev_old", &with_episode) + .unwrap_err() + .to_string(); + assert!( + error.contains("Old laptop needs a Furumi update"), + "{error}" + ); + a.sync + .ensure_playback_supported_by("dev_old", &state_with(vec![music_wire()], 0.0)) + .expect("music-only queues go anywhere"); + + // Its handoff to itself while this queue has an episode is ignored. + let mut old = Engine::new( + "dev_old".into(), + PlaybackConfig::default(), + Default::default(), + 0, + ); + old.transfer("dev_old", 0); + let stamp = old.stamp().unwrap(); + let take_over = PlaybackCommand::ActiveChanged { + active_device_id: "dev_old".into(), + active_device_name: "Old laptop".into(), + state: state_with(vec![music_wire()], 0.0), + }; + a.sync.claim_playback(&a.id).unwrap(); + publish(&a, with_episode); + a.sync + .apply_playback_command(&a.id, &take_over, Some(&stamp), "dev_old", "dev_old:5") + .unwrap(); + let mut commands = 0; + let mut messages = Vec::new(); + while let Ok(event) = a.events.try_recv() { + match event { + AppEvent::PlaybackCommand { .. } => commands += 1, + AppEvent::StatusMessage(message) => messages.push(message), + _ => {} + } + } + assert_eq!(commands, 0); + assert!(messages[0].contains("Old laptop"), "{messages:?}"); + assert_eq!( + a.sync + .with_playback_engine(|engine| engine.owner().map(str::to_owned)) + .unwrap() + .as_deref(), + Some(a.id.as_str()) + ); + + // Without episodes in the queue it is an ordinary handoff. + publish(&a, state_with(vec![music_wire()], 0.0)); + a.sync + .apply_playback_command(&a.id, &take_over, Some(&stamp), "dev_old", "dev_old:6") + .unwrap(); + assert!( + std::iter::from_fn(|| a.events.try_recv().ok()) + .any(|event| matches!(event, AppEvent::PlaybackCommand { .. })) + ); +} + +/// A 0.3.x device runs the shared engine with the default configuration: +/// automatic failover once the owner has not been heard from for +/// `owner_timeout_ms`. While this device owns a queue with an episode, the +/// old device must keep hearing from it (redacted queue, real coordination) +/// so that it never takes over by itself and replaces the queue. +#[test] +fn an_old_device_keeps_hearing_from_a_podcast_queue_owner_and_never_fails_over() { + use super::wire_compat_tests::legacy; + + let a = device(); + trust_old_device(&a); + a.sync.claim_playback(&a.id).unwrap(); + let episode = episode_ref(8); + let mut state = state_with( + vec![ + music_wire(), + PlaybackTrack::podcast_episode(episode.clone()), + ], + 12.0, + ); + state.queue_pos = 1; + state.paused = false; + + let config = PlaybackConfig::default(); + assert!(config.automatic_failover); + let start_engine = || { + let mut engine = Engine::new("dev_old".into(), config.clone(), Default::default(), 0); + engine.set_output(true, false); + engine + }; + // Without announcements the old engine takes over after the timeout: + // this is what a missing playback snapshot used to cause. + let mut starved = start_engine(); + let mut now = 0; + let first = serde_json::from_str::(&{ + a.sync.playback_tick(true, true).unwrap(); + publish(&a, state.clone()); + hello_json(&a, "dev_old") + }) + .unwrap(); + let legacy::WireMessage::Hello { + playback: Some(first), + .. + } = first + else { + panic!("the old device gets a playback snapshot"); + }; + starved.observe(&a.id, first.coordination.as_ref().unwrap(), now); + while now <= config.owner_timeout_ms + 2 * config.discovery_ms { + now += 1_000; + starved.heartbeat(now); + starved.tick(now); + } + assert_eq!(starved.owner(), Some("dev_old")); + + // With the redacted snapshots of every sync round it never does. + let mut old = start_engine(); + let mut now = 0; + while now <= 3 * config.owner_timeout_ms { + a.sync.playback_tick(true, true).unwrap(); + publish(&a, state.clone()); + let json = hello_json(&a, "dev_old"); + assert!(!json.contains(&episode.audio_url), "{json}"); + assert!(!json.contains(&episode.episode_title), "{json}"); + let legacy::WireMessage::Hello { + playback: Some(snapshot), + .. + } = serde_json::from_str(&json).unwrap() + else { + panic!("the old device gets a playback snapshot"); + }; + assert_eq!(snapshot.state.queue.len(), 2, "no item is stripped"); + assert_eq!(snapshot.state.queue[0].title, "Song"); + assert_eq!( + snapshot.state.queue[1].title, + music_dht::device_sync::PRIVATE_ITEM_TITLE + ); + old.observe(&a.id, snapshot.coordination.as_ref().unwrap(), now); + old.heartbeat(now); + old.tick(now); + assert_eq!(old.owner(), Some(a.id.as_str()), "at {now} ms"); + now += 2_000; + } + // The queue this device keeps is unchanged. + let local = lock(&a.sync.playback).local.clone().unwrap(); + assert!(local.state.queue[1].podcast_episode_ref().is_some()); +} + +/// What a 0.3.x device makes of a queue item it received and announces +/// again: it mirrors the item with its own types, which have no +/// `PlaybackTrack::source`, and serializes the queue entry back (the same +/// music path as `to_track_item` / `from_track` here). +fn republished_by_old_device( + received: &super::wire_compat_tests::legacy::PlaybackTrack, +) -> PlaybackTrack { + let parsed: PlaybackTrack = + serde_json::from_value(serde_json::to_value(received).unwrap()).unwrap(); + assert!(parsed.source.is_none(), "the old types carry no source"); + PlaybackTrack::from_track(&parsed.to_track_item()) +} + +fn other_music_wire() -> PlaybackTrack { + let mut track = music_wire(); + track.title = "Other song".into(); + track.content_id = Some(format!("b3:{}", "e".repeat(64))); + track +} + +fn queue_titles(state: &PlaybackStateWire) -> Vec<&str> { + state + .queue + .iter() + .map(|track| track.title.as_str()) + .collect() +} + +/// An old device mirrors the redacted queue it is sent while this device +/// owns playback. When it then takes over (Play on it, a paused-startup +/// claim, or automatic failover once this device was gone for two +/// minutes), it announces that queue as its own, each "Private item" now an +/// ordinary music item that nothing can play. This device leaves those out +/// instead of adopting them, and says why. +#[test] +fn placeholders_an_old_device_echoes_back_are_left_out() { + use super::wire_compat_tests::legacy; + + let mut a = device(); + trust_old_device(&a); + a.sync.claim_playback(&a.id).unwrap(); + let episode = episode_ref(9); + let mut state = state_with( + vec![ + music_wire(), + PlaybackTrack::podcast_episode(episode.clone()), + other_music_wire(), + ], + 42.0, + ); + state.queue_pos = 1; + state.paused = false; + a.sync.playback_tick(true, true).unwrap(); + publish(&a, state); + + // The old device receives the redacted queue and mirrors it. + let legacy::WireMessage::Hello { + playback: Some(received), + .. + } = serde_json::from_str(&hello_json(&a, "dev_old")).unwrap() + else { + panic!("the old device gets a playback snapshot"); + }; + let mirrored: Vec = received + .state + .queue + .iter() + .map(republished_by_old_device) + .collect(); + let echoed = mirrored[1].clone(); + assert_eq!(echoed.title, music_dht::device_sync::PRIVATE_ITEM_TITLE); + assert!(echoed.source.is_none(), "a music item now"); + assert!(echoed.id < 0, "the old device's portable id"); + assert!( + !is_private_track_item(&echoed.to_track_item()), + "not a placeholder of this device" + ); + + // It takes over and announces the mirrored queue, with a higher claim. + let mut old = Engine::new( + "dev_old".into(), + PlaybackConfig::default(), + Default::default(), + 0, + ); + assert!(old.observe(&a.id, received.coordination.as_ref().unwrap(), 0)); + assert!(old.transfer("dev_old", 1)); + let announced: legacy::PlaybackStateWire = serde_json::from_value( + serde_json::to_value(PlaybackStateWire { + queue: mirrored, + ..received_state_v4(&received.state) + }) + .unwrap(), + ) + .unwrap(); + let old_profile = legacy::DeviceProfileWire { + device_id: "dev_old".into(), + name: "Old laptop".into(), + client_version: "0.3.5".into(), + protocol_version: 3, + endpoint_id: String::new(), + endpoint_ticket: String::new(), + revoked: false, + revoke_cutoff_seq: None, + updated_at_ms: 1, + }; + let hello = legacy::WireMessage::Hello { + group_id: a.sync.ensure_identity().unwrap().group_id, + profile: old_profile.clone(), + devices: vec![old_profile], + vector: BTreeMap::new(), + ops: Vec::new(), + snapshot: legacy::SyncSnapshot::default(), + playback: Some(legacy::PlaybackSnapshot { + device_id: "dev_old".into(), + device_name: "Old laptop".into(), + active: true, + updated_at_ms: now_ms(), + state: announced, + coordination: Some(old.announcement()), + }), + }; + while a.events.try_recv().is_ok() {} + receive(&a.sync, &serde_json::to_string(&hello).unwrap()); + + // This device follows the takeover with the music items only. + let mut adopted = None; + let mut messages = Vec::new(); + while let Ok(event) = a.events.try_recv() { + match event { + AppEvent::DevicePlayback(snapshot) => adopted = Some(snapshot), + AppEvent::StatusMessage(message) => messages.push(message), + _ => {} + } + } + let adopted = adopted.expect("the takeover reaches the UI"); + assert_eq!( + a.sync + .with_playback_engine(|engine| engine.owner().map(str::to_owned)) + .unwrap() + .as_deref(), + Some("dev_old") + ); + assert_eq!(queue_titles(&adopted.state), ["Song", "Other song"]); + // The echoed item was current: the next one is, from its start. + assert_eq!(adopted.state.queue_pos, 1); + assert_eq!(adopted.state.position_secs, 0.0); + let items = track_items_from_wire(&adopted.state.queue, PlaybackTrack::to_track_item); + assert!( + items + .iter() + .all(|item| item.title != music_dht::device_sync::PRIVATE_ITEM_TITLE), + "{items:?}" + ); + assert!( + messages + .iter() + .any(|message| message.contains("Old laptop") && message.contains("Private item")), + "{messages:?}" + ); + // Its next announcements say nothing new. + let mut again = adopted.clone(); + again.state.queue.insert(1, echoed.clone()); + old.heartbeat(5_000); + again.coordination = Some(old.announcement()); + a.sync.apply_playback_snapshot("dev_old", again).unwrap(); + assert!( + std::iter::from_fn(|| a.events.try_recv().ok()) + .all(|event| !matches!(event, AppEvent::StatusMessage(_))) + ); + + // A command it builds from its stale mirror, once this device's queue + // has no episode left (so commands from it are accepted), is cleaned + // the same way. + let mut b = device(); + trust_old_device(&b); + b.sync.claim_playback(&b.id).unwrap(); + publish(&b, state_with(vec![music_wire()], 0.0)); + let mut old = Engine::new( + "dev_old".into(), + PlaybackConfig::default(), + Default::default(), + 0, + ); + old.transfer("dev_old", 0); + let stamp = old.stamp().unwrap(); + let mut stale = state_with(vec![music_wire(), echoed, other_music_wire()], 7.0); + stale.queue_pos = 2; + let take_over = PlaybackCommand::ActiveChanged { + active_device_id: "dev_old".into(), + active_device_name: "Old laptop".into(), + state: stale, + }; + b.sync + .apply_playback_command(&b.id, &take_over, Some(&stamp), "dev_old", "dev_old:7") + .unwrap(); + let mut applied = None; + let mut messages = Vec::new(); + while let Ok(event) = b.events.try_recv() { + match event { + AppEvent::PlaybackCommand { + command: PlaybackCommand::ActiveChanged { state, .. }, + .. + } => applied = Some(state), + AppEvent::StatusMessage(message) => messages.push(message), + _ => {} + } + } + let applied = applied.expect("an ordinary handoff"); + assert_eq!(queue_titles(&applied), ["Song", "Other song"]); + assert_eq!(applied.queue_pos, 1); + assert_eq!(applied.position_secs, 7.0, "the current item was kept"); + assert!( + messages + .iter() + .any(|message| message.contains("Old laptop")), + "{messages:?}" + ); +} + +/// The v4 form of a state an old device received (its queue is replaced). +fn received_state_v4( + state: &super::wire_compat_tests::legacy::PlaybackStateWire, +) -> PlaybackStateWire { + serde_json::from_value(serde_json::to_value(state).unwrap()).unwrap() +} + +/// Only a bare music item titled "Private item" counts as an echoed +/// placeholder: a song with that title and an artist or a content id stays, +/// and so does a real placeholder (its source says so). +#[test] +fn only_bare_music_items_from_old_devices_count_as_echoed_placeholders() { + let echoed = { + let mut track = PlaybackTrack::private_placeholder(); + track.source = None; + track.id = -42; + track + }; + let mut named_song = echoed.clone(); + named_song.artist_names = vec!["Band".into()]; + let mut with_content = echoed.clone(); + with_content.content_id = Some(format!("b3:{}", "f".repeat(64))); + let mut state = state_with( + vec![ + echoed.clone(), + named_song, + with_content, + PlaybackTrack::private_placeholder(), + echoed, + ], + 3.0, + ); + state.queue_pos = 2; + assert_eq!(drop_echoed_private_items(&mut state), 2); + assert_eq!(state.queue.len(), 3); + assert_eq!(state.queue_pos, 1, "the same current item"); + assert_eq!(state.position_secs, 3.0); + assert!(state.queue[2].is_private_placeholder()); + + let mut only_echoes = state_with(vec![state.queue[0].clone()], 1.0); + only_echoes.queue[0].artist_names.clear(); + assert_eq!(drop_echoed_private_items(&mut only_echoes), 1); + assert!(only_echoes.queue.is_empty()); + assert!(!only_echoes.playing); +} + +/// Feed and audio URLs may carry private tokens; the debug log (shown in +/// the Logs tab) never prints them. +#[test] +fn podcast_payloads_are_not_written_to_the_debug_log() { + use std::io::Write; + use tracing_subscriber::Layer as _; + use tracing_subscriber::layer::SubscriberExt as _; + + #[derive(Clone, Default)] + struct Buffer(Arc>>); + impl Write for Buffer { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + lock(&self.0).extend_from_slice(bytes); + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let buffer = Buffer::default(); + let writer = buffer.clone(); + let subscriber = tracing_subscriber::registry().with( + tracing_subscriber::fmt::layer() + .with_writer(move || writer.clone()) + .with_ansi(false) + .with_filter(tracing_subscriber::filter::LevelFilter::TRACE), + ); + let a = device(); + let b = device(); + pair(&a, &b); + let mut episode = episode_ref(9); + episode.audio_url = "https://cdn.example/ep9.mp3?token=SECRET-AUDIO".into(); + tracing::subscriber::with_default(subscriber, || { + a.sync.claim_playback(&a.id).unwrap(); + a.sync + .record_playback_command( + &b.id, + PlaybackCommand::SetState { + state: state_with(vec![PlaybackTrack::podcast_episode(episode.clone())], 1.0), + seek: false, + }, + ) + .unwrap(); + a.sync + .record_playback_command( + &b.id, + PlaybackCommand::SetState { + state: state_with(vec![music_wire()], 2.0), + seek: false, + }, + ) + .unwrap(); + }); + let logged = String::from_utf8(lock(&buffer.0).clone()).unwrap(); + assert!(!logged.contains("SECRET-AUDIO"), "{logged}"); + assert!(logged.contains("kind=\"playback_command\""), "{logged}"); + // Music payloads are still logged in full. + assert!(logged.contains("\"title\":\"Song\""), "{logged}"); +} + +/// Podcast store work done by the sync handlers does not hold a Tokio +/// worker: another task keeps running meanwhile, even with one worker. +#[test] +fn podcast_store_work_in_sync_handlers_leaves_the_worker_free() { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .enable_time() + .build() + .unwrap(); + runtime.block_on(async { + let started = std::time::Instant::now(); + let busy = tokio::spawn(async { + off_worker(|| std::thread::sleep(Duration::from_millis(600))); + std::time::Instant::now() + }); + tokio::time::sleep(Duration::from_millis(50)).await; + let other = tokio::spawn(async { std::time::Instant::now() }); + let other_ran = other.await.unwrap(); + let busy_done = busy.await.unwrap(); + assert!(other_ran < busy_done, "the other task waited for the store"); + assert!(other_ran.duration_since(started) < Duration::from_millis(400)); + }); + // Without a runtime (or on the blocking pool) it simply runs. + assert_eq!(off_worker(|| 7), 7); + let current = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + assert_eq!(current.block_on(async { off_worker(|| 8) }), 8); +} diff --git a/src/devices/tests.rs b/src/devices/tests.rs index a47a0cc..b858a80 100644 --- a/src/devices/tests.rs +++ b/src/devices/tests.rs @@ -30,6 +30,7 @@ async fn localhost_devices_exchange_state_and_handoff() { library: Arc::new(Library::open(&dir.path().join("library.sqlite3")).unwrap()), event_tx: Default::default(), playback: Default::default(), + podcasts: Default::default(), }); let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); sync.set_event_tx(tx); @@ -54,8 +55,18 @@ async fn localhost_devices_exchange_state_and_handoff() { let profile_b = b .own_profile(&service_b.ticket().await.unwrap().to_string()) .unwrap(); - a.apply_device_profile(&profile_b, true).unwrap(); - b.apply_device_profile(&profile_a, true).unwrap(); + a.apply_device_profile( + &profile_b, + true, + VersionReport::SelfReported(Some(profile_b.protocol_version)), + ) + .unwrap(); + b.apply_device_profile( + &profile_a, + true, + VersionReport::SelfReported(Some(profile_a.protocol_version)), + ) + .unwrap(); a.claim_playback(&profile_a.device_id).unwrap(); a.playback_tick(true, true).unwrap(); let mut state = empty_playback_state(); @@ -163,7 +174,12 @@ async fn localhost_devices_exchange_state_and_handoff() { silent_profile.device_id = "silent-peer".into(); silent_profile.endpoint_id = silent.endpoint_id().to_string(); silent_profile.endpoint_ticket = silent.ticket().await.unwrap().to_string(); - a.apply_device_profile(&silent_profile, true).unwrap(); + a.apply_device_profile( + &silent_profile, + true, + VersionReport::SelfReported(Some(silent_profile.protocol_version)), + ) + .unwrap(); lock(&a.conn) .execute( "UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'", @@ -218,6 +234,121 @@ async fn localhost_devices_exchange_state_and_handoff() { .expect("localhost sync must complete within 30 seconds"); } +/// Podcast state over the production stream handlers: a subscription and +/// progress from A reach B through `sync_device` → `handle_hello`, and B's +/// start over reaches A through the response of B's own sync. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn localhost_devices_sync_podcasts() { + use super::podcast_sync_tests::{checkpoint, device_with_store, episode_ref, start, subscribe}; + tokio::time::timeout(Duration::from_secs(30), async { + let dirs = [tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()]; + let network = NetworkId::from_name(&format!("podcast-sync-test-{}", random_hex(16))); + let mut peers = Vec::new(); + let mut servers = Vec::new(); + for dir in &dirs { + std::fs::create_dir_all(dir.path().join("network")).unwrap(); + let config = music_dht::MusicDhtConfig::builder() + .data_dir(dir.path().join("network")) + .network_id(network) + .stream_protocol(SYNC_ALPN) + .build() + .unwrap(); + let (service, _events) = MusicDhtService::start(config).await.unwrap(); + let service = Arc::new(service); + let store = Arc::new( + furumi_library::podcasts::PodcastStore::open(&dir.path().join("podcasts.sqlite3")) + .unwrap(), + ); + let device = device_with_store(store, true); + let sync = Arc::new(device.sync.clone()); + let stats = Arc::new(crate::federation::TransportStats::default()); + servers.push(tokio::spawn(serve_peers( + service.stream_acceptor(SYNC_ALPN).unwrap(), + sync.clone(), + service.clone(), + stats.clone(), + ))); + peers.push((device, sync, service, stats)); + } + let (a, sync_a, service_a, stats_a) = &peers[0]; + let (b, sync_b, service_b, stats_b) = &peers[1]; + sync_b + .set_group_id(&sync_a.ensure_identity().unwrap().group_id) + .unwrap(); + let profile_a = sync_a + .own_profile(&service_a.ticket().await.unwrap().to_string()) + .unwrap(); + let profile_b = sync_b + .own_profile(&service_b.ticket().await.unwrap().to_string()) + .unwrap(); + for (sync, profile) in [(sync_a, &profile_b), (sync_b, &profile_a)] { + sync.apply_device_profile( + profile, + true, + VersionReport::SelfReported(Some(profile.protocol_version)), + ) + .unwrap(); + } + + let podcast_id = subscribe(a).podcast_id; + let episode = episode_ref(1); + let session = start(a, &episode, 0); + checkpoint(a, &session, 1_200_000); + sync_a + .sync_device( + service_a.clone(), + &sync_a.active_remote_devices().unwrap()[0], + stats_a.clone(), + ) + .await + .unwrap(); + let subscribed = b + .store + .list_podcasts(furumi_library::podcasts::PodcastListFilter::Subscribed) + .unwrap(); + assert_eq!(subscribed.len(), 1); + assert_eq!(subscribed[0].podcast_id, podcast_id); + assert_eq!( + b.store.resume_position_ms(&episode.episode_id).unwrap(), + 1_200_000 + ); + + // B starts over; its next sync carries the start over to A, which + // then refuses A's older session. + let store = Arc::clone(&b.store); + sync_b + .run_podcast_action(|stamps| { + let write = store.restart(stamps, &episode, "restart-on-b")?; + Ok(((), vec![write.op])) + }) + .unwrap(); + sync_b + .sync_device( + service_b.clone(), + &sync_b.active_remote_devices().unwrap()[0], + stats_b.clone(), + ) + .await + .unwrap(); + let progress = a.store.progress(&episode.episode_id).unwrap().unwrap(); + assert_eq!(progress.position_ms, 0); + assert_eq!(progress.session_id, "restart-on-b"); + assert!(matches!( + checkpoint(a, &session, 1_230_000), + furumi_library::podcasts::CheckpointOutcome::Superseded { .. } + )); + + for server in servers { + server.abort(); + } + for (_, _, service, _) in &peers { + service.shutdown().await.unwrap(); + } + }) + .await + .expect("localhost podcast sync must complete within 30 seconds"); +} + fn empty_playback_state() -> PlaybackStateWire { PlaybackStateWire { queue: vec![], @@ -229,6 +360,7 @@ fn empty_playback_state() -> PlaybackStateWire { volume: 80, shuffle: false, repeat: PlaybackRepeat::Off, + playback_rate: None, } } @@ -282,7 +414,7 @@ fn coordination_ignores_legacy_commands_and_wrong_snapshot_sender() { coordination: None, }); let gossip = sync - .local_playback_snapshot() + .local_playback_snapshot_for(PROTOCOL_VERSION) .unwrap() .coordination .unwrap(); @@ -342,6 +474,7 @@ fn test_sync() -> DeviceSync { library: Arc::new(Library::open(&library_path).unwrap()), event_tx: Arc::new(std::sync::Mutex::new(None)), playback: Arc::new(std::sync::Mutex::new(PlaybackShared::default())), + podcasts: Default::default(), }; sync.ensure_identity().unwrap(); sync @@ -424,12 +557,25 @@ fn test_fed_track(content_id: &str) -> crate::federation::FedTrack { } #[test] -fn base64url_round_trip_without_padding() { - for input in [b"".as_slice(), b"a", b"ab", b"abc", b"abcdef"] { - let encoded = base64url_encode(input); - assert!(!encoded.contains('=')); - assert_eq!(base64url_decode(&encoded).unwrap(), input); - } +fn invites_round_trip_through_the_shared_encoding() { + let invite = InviteWire { + v: 1, + ticket: "ticket".into(), + device_id: "dev_a".into(), + invite_id: "inv".into(), + secret: "s3cr3t".into(), + expires_at_ms: 10, + protocol_version: None, + }; + let link = encode_invite(&invite).unwrap(); + assert!(!link.contains('=')); + assert_eq!(parse_invite(&link).unwrap(), invite); + assert!( + parse_invite("frid://x/abc") + .unwrap_err() + .to_string() + .contains(":connect") + ); } #[test] @@ -452,6 +598,35 @@ fn tombstone_detection() { ); } +fn music_track(id: i64, content_id: &str) -> TrackItem { + TrackItem { + id, + title: "Local Song".to_string(), + track_number: Some(1), + disc_number: Some(1), + duration_seconds: 180.0, + artists: vec![ArtistRef { + id: 1, + name: "Local Artist".to_string(), + }], + featured_artists: Vec::new(), + release_id: 2, + release_title: "Local Release".to_string(), + release_year: Some(2026), + file_path: format!("/music/{id}.mp3"), + content_id: Some(content_id.to_string()), + cover_path: None, + audio_format: Some("mp3".to_string()), + audio_bitrate: Some(320), + audio_sample_rate: Some(44_100), + audio_bit_depth: None, + file_size_bytes: Some(123_456), + play_count: 3, + fed: None, + podcast: None, + } +} + #[test] fn playback_tracks_do_not_sync_device_local_paths() { let source = TrackItem { @@ -478,6 +653,7 @@ fn playback_tracks_do_not_sync_device_local_paths() { file_size_bytes: Some(123_456), play_count: 3, fed: None, + podcast: None, }; let wire = PlaybackTrack::from_track(&source); @@ -565,6 +741,7 @@ fn playback_command_is_targeted_and_deduplicated() { volume: 42, shuffle: false, repeat: PlaybackRepeat::Off, + playback_rate: None, }, seek: false, }; @@ -621,6 +798,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() { volume: 42, shuffle: false, repeat: PlaybackRepeat::Off, + playback_rate: None, }, seek: false, }; @@ -629,7 +807,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() { .unwrap(); sync.record_playback_command("dev_other", command).unwrap(); - let target_ops = sync.ops_for_peer("dev_target").unwrap(); + let target_ops = sync.ops_for_peer("dev_target", PROTOCOL_VERSION).unwrap(); assert_eq!( target_ops .iter() @@ -638,7 +816,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() { 1 ); assert!( - sync.ops_for_peer("dev_unknown") + sync.ops_for_peer("dev_unknown", PROTOCOL_VERSION) .unwrap() .iter() .all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. })) @@ -653,7 +831,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() { ) .unwrap(); assert!( - sync.ops_for_peer("dev_target") + sync.ops_for_peer("dev_target", PROTOCOL_VERSION) .unwrap() .iter() .all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. })) @@ -692,6 +870,7 @@ fn newer_device_trust_reactivates_revoked_device() { updated_at_ms: 20, }, false, + VersionReport::Relayed(PROTOCOL_VERSION), ) .unwrap(); assert!(!device_revoked(&sync, device_id)); @@ -936,3 +1115,312 @@ fn stale_synced_playlist_item_metadata_repairs_pending_fed_track() { assert!(detail.tracks[0].is_fed_pending()); assert_eq!(detail.tracks[0].title, fed.title); } + +fn podcast_meta() -> music_dht::podcast::PodcastMeta { + music_dht::podcast::PodcastMeta { + feed_url: music_dht::podcast::FeedUrl::parse("https://example.test/feed.xml").unwrap(), + title: "Show".into(), + author: None, + description: None, + link: None, + image_url: None, + } +} + +/// Rows of the local sync log: (op_id, seq, kind, payload_json, hlc_ms). +fn logged_ops(sync: &DeviceSync) -> Vec<(String, i64, String, String, i64)> { + let conn = lock(&sync.conn); + let mut stmt = conn + .prepare("SELECT op_id, seq, kind, payload_json, hlc_ms FROM sync_ops ORDER BY seq") + .unwrap(); + stmt.query_map([], |row| { + Ok(( + row.get(0)?, + row.get(1)?, + row.get(2)?, + row.get(3)?, + row.get(4)?, + )) + }) + .unwrap() + .collect::>>() + .unwrap() +} + +#[test] +fn podcast_actions_are_appended_to_the_sync_log_with_their_stamps() { + let sync = test_sync(); + let device_id = sync.ensure_identity().unwrap().device_id; + let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap(); + sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true) + .unwrap(); + let outcome = sync + .run_podcast_action(|stamps| { + let outcome = store.subscribe(stamps, &podcast_meta())?; + let ops = outcome.ops.clone(); + Ok((outcome, ops)) + }) + .unwrap(); + let ops = logged_ops(&sync); + assert_eq!(ops.len(), 2, "{ops:?}"); + let (op_id, seq, kind, payload_json, hlc_ms) = &ops[1]; + assert_eq!(kind, "podcast_subscription_set"); + assert_eq!(*seq, 2); + assert_eq!(*op_id, format!("{device_id}:2")); + assert!(*hlc_ms > ops[0].4, "hlc keeps increasing"); + // The log carries the shared FRID payload, and the store's register + // was written with exactly this stamp. + let payload: music_dht::device_sync::SyncOpPayload = + serde_json::from_str(payload_json).unwrap(); + assert!(payload.is_podcast()); + let register = store.subscription(&outcome.podcast_id).unwrap().unwrap(); + assert_eq!(register.op_id, *op_id); + assert_eq!(register.hlc_ms, *hlc_ms); + let vector = sync.vector().unwrap(); + assert_eq!(vector.get(&device_id), Some(&2)); + + // Unsubscribing emits two operations with consecutive stamps; a music + // operation afterwards continues the same sequence. + let unsubscribe = sync + .run_podcast_action(|stamps| { + let ops = store.unsubscribe(stamps, &outcome.podcast_id)?; + Ok((ops.len(), ops)) + }) + .unwrap(); + assert_eq!(unsubscribe, 2); + sync.record_content_like(&format!("b3:{}", "b".repeat(64)), true) + .unwrap(); + let ops = logged_ops(&sync); + let kinds: Vec<(i64, &str)> = ops + .iter() + .map(|(_, seq, kind, ..)| (*seq, kind.as_str())) + .collect(); + assert_eq!( + kinds, + [ + (1, "track_like_set"), + (2, "podcast_subscription_set"), + (3, "podcast_subscription_set"), + (4, "podcast_share_set"), + (5, "track_like_set"), + ] + ); + + // An action that emits nothing leaves the log alone. + sync.run_podcast_action(|stamps| { + let ops = store.unsubscribe(stamps, &outcome.podcast_id)?; + Ok(((), ops)) + }) + .unwrap(); + assert_eq!(logged_ops(&sync).len(), 5); +} + +#[test] +fn podcast_ops_are_sent_only_to_v4_peers() { + let sync = test_sync(); + let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap(); + sync.run_podcast_action(|stamps| { + let outcome = store.subscribe(stamps, &podcast_meta())?; + let ops = outcome.ops.clone(); + Ok(((), ops)) + }) + .unwrap(); + sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true) + .unwrap(); + // A v3 peer cannot parse the podcast payload (an unknown `kind` breaks + // its whole message), so its batch keeps only the music operation. + let ops = sync.ops_for_peer("dev_peer", 3).unwrap(); + assert_eq!(ops.len(), 1); + assert!(matches!(ops[0].payload, SyncOpPayload::TrackLikeSet { .. })); + let ops = sync.ops_for_peer("dev_peer", 4).unwrap(); + assert_eq!(ops.len(), 2); + assert!(ops[0].payload.is_podcast()); + // Unreported versions are the oldest protocol, except that every + // `furumi/sync/2` peer keeps listen history. + let event = sync + .listen_event_for_track( + sync.new_listen_id(), + &music_track(7, &format!("b3:{}", "b".repeat(64))), + 1, + 120_000, + music_dht::device_sync::ListenEndReason::Finished, + ) + .unwrap(); + sync.record_listen(event).unwrap(); + let kinds: Vec<_> = sync + .ops_for_peer( + "dev_peer", + music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION, + ) + .unwrap() + .into_iter() + .map(|op| payload_kind(&op.payload)) + .collect(); + assert_eq!(kinds, ["track_like_set", "listen_recorded"]); +} + +#[test] +fn failed_podcast_actions_append_nothing() { + let sync = test_sync(); + let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap(); + let unknown = music_dht::podcast::PodcastId::from_feed_url( + &music_dht::podcast::FeedUrl::parse("https://example.test/other.xml").unwrap(), + ); + let result = sync.run_podcast_action(|stamps| { + let ops = store.set_shared(stamps, &unknown, true)?; + Ok(((), ops)) + }); + assert!(result.is_err()); + assert!(logged_ops(&sync).is_empty()); +} + +#[test] +fn podcast_episodes_never_enter_music_listening_history() { + let sync = test_sync(); + let episode = crate::app::podcast_fixtures::episode_track(1); + assert!( + sync.listen_event_for_track( + "listen-1".into(), + &episode, + 1_000, + 120_000, + music_dht::device_sync::ListenEndReason::Finished, + ) + .is_none() + ); +} + +fn stored_version(sync: &DeviceSync, device_id: &str) -> Option { + stored_protocol_version(&lock(&sync.conn), device_id).unwrap() +} + +fn profile_wire(device_id: &str, protocol_version: u16) -> DeviceProfileWire { + DeviceProfileWire { + device_id: device_id.to_string(), + name: device_id.to_string(), + client_version: "0.0.0".to_string(), + protocol_version, + endpoint_id: String::new(), + endpoint_ticket: String::new(), + revoked: false, + revoke_cutoff_seq: None, + updated_at_ms: 1, + } +} + +fn profile_set_op(origin: &str, seq: i64, protocol_version: Option) -> SyncOpWire { + SyncOpWire { + op_id: format!("{origin}:{seq}"), + origin_device_id: origin.to_string(), + seq, + hlc_ms: seq, + payload: SyncOpPayload::DeviceProfileSet { + name: origin.to_string(), + client_version: "0.0.0".to_string(), + endpoint_ticket: String::new(), + endpoint_id: String::new(), + protocol_version, + }, + } +} + +#[test] +fn peer_versions_come_from_the_peer_itself_never_from_this_build() { + let sync = test_sync(); + sync.apply_device_trusted("dev_new", 1).unwrap(); + sync.apply_device_trusted("dev_old", 1).unwrap(); + // A device known only from `device_trusted` has reported nothing. + assert_eq!( + stored_version(&sync, "dev_new"), + Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION) + ); + + // Its own profile announcement (v4) is a self-report. + sync.apply_op(&profile_set_op("dev_new", 1, Some(4))) + .unwrap(); + assert_eq!(stored_version(&sync, "dev_new"), Some(4)); + // An announcement of an older client carries no version: this device + // keeps what it knows and never substitutes its own constant. + sync.apply_op(&profile_set_op("dev_old", 1, None)).unwrap(); + assert_eq!( + stored_version(&sync, "dev_old"), + Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION) + ); + + // Relays of v1-v3 clients list every device with their own constant: + // a relayed value only raises the stored version. + sync.apply_device_profiles( + &[profile_wire("dev_new", 3), profile_wire("dev_old", 3)], + Some("dev_relay"), + ) + .unwrap(); + assert_eq!(stored_version(&sync, "dev_new"), Some(4)); + assert_eq!(stored_version(&sync, "dev_old"), Some(3)); + + // The sender's own entry of a `devices` list is a self-report, so a + // downgraded device is noticed. + sync.apply_device_profiles(&[profile_wire("dev_new", 3)], Some("dev_new")) + .unwrap(); + assert_eq!(stored_version(&sync, "dev_new"), Some(3)); +} + +#[test] +fn accepted_pairing_keeps_the_version_the_requester_reported() { + let sync = test_sync(); + let insert = |request_id: &str, device_id: &str, version: Option| { + lock(&sync.conn) + .execute( + "INSERT INTO sync_pending_pairing + (request_id, device_id, name, client_version, endpoint_id, + endpoint_ticket, invite_id, created_at_ms, status, protocol_version) + VALUES (?1, ?2, 'phone', '0.0.0', '', '', 'inv', 1, 'pending', ?3)", + params![request_id, device_id, version], + ) + .unwrap(); + }; + insert("req_v3", "dev_v3", Some(3)); + // A request stored by an older build of this client has no version. + insert("req_legacy", "dev_legacy", None); + sync.answer_pairing("req_v3", true, false).unwrap(); + sync.answer_pairing("req_legacy", true, false).unwrap(); + assert_eq!(stored_version(&sync, "dev_v3"), Some(3)); + assert_eq!( + stored_version(&sync, "dev_legacy"), + Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION) + ); +} + +#[test] +fn a_new_protocol_version_is_announced_once_in_the_log() { + let sync = test_sync(); + let device_id = sync.ensure_identity().unwrap().device_id; + let announcements = |sync: &DeviceSync| { + logged_ops(sync) + .into_iter() + .filter(|(_, _, kind, ..)| kind == "device_profile_set") + .map(|(_, _, _, payload, _)| serde_json::from_str::(&payload).unwrap()) + .collect::>() + }; + // A fresh identity already reports this version in every hello. + sync.own_profile("").unwrap(); + assert!(announcements(&sync).is_empty()); + // The row written by the previous release. + lock(&sync.conn) + .execute( + "UPDATE sync_devices SET protocol_version = ?2 WHERE device_id = ?1", + params![device_id, PROTOCOL_VERSION - 1], + ) + .unwrap(); + sync.own_profile("").unwrap(); + sync.own_profile("").unwrap(); + let announced = announcements(&sync); + assert_eq!(announced.len(), 1, "{announced:?}"); + assert!(matches!( + announced[0], + SyncOpPayload::DeviceProfileSet { + protocol_version: Some(PROTOCOL_VERSION), + .. + } + )); + assert_eq!(stored_version(&sync, &device_id), Some(PROTOCOL_VERSION)); +} diff --git a/src/devices/wire_compat_tests.rs b/src/devices/wire_compat_tests.rs new file mode 100644 index 0000000..75f3491 --- /dev/null +++ b/src/devices/wire_compat_tests.rs @@ -0,0 +1,576 @@ +//! Golden wire-compatibility tests: the shared `music_dht::device_sync` +//! types this client now uses must serialize v3 content byte-for-byte like +//! the private copies of furumi-tui 0.3.x (kept verbatim in `legacy` +//! below), so 0.3.x clients and furumi-web 0.12.x keep interoperating. + +use super::*; + +/// Private wire types of furumi-tui 0.3.x (`src/devices.rs` before sync +/// v4), copied verbatim apart from visibility, `PartialEq` and doc comments. +#[allow(dead_code)] +pub(super) mod legacy { + use music_dht::device_sync::ListenEvent; + use music_dht::playback::CommandStamp; + use serde::{Deserialize, Serialize}; + use std::collections::BTreeMap; + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct InviteWire { + pub v: u16, + #[serde(rename = "t")] + pub ticket: String, + #[serde(rename = "d")] + pub device_id: String, + #[serde(rename = "i")] + pub invite_id: String, + #[serde(rename = "s")] + pub secret: String, + #[serde(rename = "e")] + pub expires_at_ms: i64, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct DeviceProfileWire { + pub device_id: String, + pub name: String, + pub client_version: String, + pub protocol_version: u16, + pub endpoint_id: String, + pub endpoint_ticket: String, + #[serde(default)] + pub revoked: bool, + #[serde(default)] + pub revoke_cutoff_seq: Option, + #[serde(default)] + pub updated_at_ms: i64, + } + + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + pub struct SyncedFedTrack { + pub item_id: String, + pub owner: String, + pub title: String, + #[serde(default)] + pub artist_names: Vec, + #[serde(default)] + pub featured_artist_names: Vec, + pub year: Option, + pub duration_seconds: Option, + pub content_id: String, + pub release_title: Option, + pub track_number: Option, + pub disc_number: Option, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct PlaybackTrack { + pub id: i64, + pub title: String, + pub track_number: Option, + pub disc_number: Option, + pub duration_seconds: f64, + #[serde(default)] + pub artist_names: Vec, + #[serde(default)] + pub featured_artist_names: Vec, + pub release_id: i64, + pub release_title: String, + pub release_year: Option, + #[serde(default)] + pub file_path: String, + pub content_id: Option, + pub audio_format: Option, + pub audio_bitrate: Option, + pub audio_sample_rate: Option, + pub audio_bit_depth: Option, + pub file_size_bytes: Option, + #[serde(default)] + pub play_count: i64, + #[serde(default)] + pub fed: Option, + } + + #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] + #[serde(rename_all = "snake_case")] + pub enum PlaybackRepeat { + #[default] + Off, + One, + All, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct PlaybackStateWire { + #[serde(default)] + pub queue: Vec, + #[serde(default)] + pub queue_pos: usize, + pub playing: bool, + pub paused: bool, + #[serde(default)] + pub idle_since_ms: Option, + pub position_secs: f64, + #[serde(default)] + pub volume: u8, + pub shuffle: bool, + pub repeat: PlaybackRepeat, + } + + pub type PlaybackSnapshot = music_dht::playback::Snapshot; + pub type PlaybackCommand = music_dht::playback::Command; + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SyncOpWire { + pub op_id: String, + pub origin_device_id: String, + pub seq: i64, + pub hlc_ms: i64, + pub payload: SyncOpPayload, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + #[serde(tag = "kind", rename_all = "snake_case")] + pub enum SyncOpPayload { + TrackLikeSet { + content_id: String, + liked: bool, + #[serde(default)] + fed: Option, + }, + PlaylistCreated { + playlist_id: String, + title: String, + }, + PlaylistRenamed { + playlist_id: String, + title: String, + }, + PlaylistDeleted { + playlist_id: String, + }, + PlaylistTrackAdded { + playlist_id: String, + content_id: String, + position: i64, + #[serde(default)] + fed: Option, + }, + PlaylistTrackRemoved { + playlist_id: String, + content_id: String, + }, + DeviceProfileSet { + name: String, + client_version: String, + endpoint_ticket: String, + endpoint_id: String, + }, + DeviceTrusted { + target_device_id: String, + }, + DeviceRevoked { + target_device_id: String, + target_max_seq_seen: i64, + }, + PlaybackCommand { + target_device_id: String, + command: PlaybackCommand, + #[serde(default)] + authority: Option, + }, + ListenRecorded { + event: ListenEvent, + }, + } + + #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] + pub struct SyncSnapshot { + #[serde(default)] + pub likes: Vec, + #[serde(default)] + pub unlikes: Vec, + #[serde(default)] + pub playlists: Vec, + #[serde(default)] + pub deleted_playlists: Vec, + #[serde(default)] + pub removed_playlist_items: Vec, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotLike { + pub content_id: String, + pub hlc_ms: i64, + pub op_id: String, + #[serde(default)] + pub fed: Option, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotLikeTombstone { + pub content_id: String, + pub hlc_ms: i64, + pub op_id: String, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotPlaylist { + pub playlist_id: String, + pub title: String, + pub hlc_ms: i64, + pub op_id: String, + #[serde(default)] + pub items: Vec, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotPlaylistTombstone { + pub playlist_id: String, + pub hlc_ms: i64, + pub op_id: String, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotPlaylistItem { + pub content_id: String, + pub position: i64, + pub hlc_ms: i64, + pub op_id: String, + #[serde(default)] + pub fed: Option, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + pub struct SnapshotPlaylistItemTombstone { + pub playlist_id: String, + pub content_id: String, + pub hlc_ms: i64, + pub op_id: String, + } + + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] + #[serde(tag = "type", rename_all = "snake_case")] + pub enum WireMessage { + PairRequest { + invite_id: String, + secret: String, + profile: DeviceProfileWire, + #[serde(default)] + group_id: Option, + #[serde(default)] + group_active_devices: usize, + #[serde(default)] + devices: Vec, + vector: BTreeMap, + ops: Vec, + snapshot: SyncSnapshot, + #[serde(default)] + playback: Option, + }, + PairResponse { + accepted: bool, + #[serde(default)] + pending: bool, + #[serde(default)] + error: Option, + #[serde(default)] + group_id: Option, + #[serde(default)] + profile: Option, + #[serde(default)] + devices: Vec, + #[serde(default)] + vector: BTreeMap, + #[serde(default)] + ops: Vec, + #[serde(default)] + snapshot: SyncSnapshot, + #[serde(default)] + playback: Option, + }, + Hello { + group_id: String, + profile: DeviceProfileWire, + devices: Vec, + vector: BTreeMap, + ops: Vec, + snapshot: SyncSnapshot, + #[serde(default)] + playback: Option, + }, + SyncResponse { + accepted: bool, + #[serde(default)] + error: Option, + #[serde(default)] + devices: Vec, + #[serde(default)] + vector: BTreeMap, + #[serde(default)] + ops: Vec, + #[serde(default)] + snapshot: SyncSnapshot, + #[serde(default)] + playback: Option, + }, + } +} + +const CONTENT_ID: &str = "b3:1111111111111111111111111111111111111111111111111111111111111111"; + +/// A v3 `hello` exercising every v3 operation kind, every snapshot section +/// and a coordinated playback snapshot (input order is irrelevant). +fn v3_hello_input() -> String { + let fed = serde_json::json!({ + "item_id": "item", "owner": "owner", "title": "Song", "artist_names": ["A"], + "featured_artist_names": [], "year": 2001, "duration_seconds": 180, + "content_id": CONTENT_ID, "release_title": "Album", "track_number": 1, "disc_number": 1, + }); + let track = serde_json::json!({ + "id": 7, "title": "Song", "track_number": 1, "disc_number": null, + "duration_seconds": 180.5, "artist_names": ["A"], "featured_artist_names": ["B"], + "release_id": 3, "release_title": "Album", "release_year": 2001, "file_path": "", + "content_id": CONTENT_ID, "audio_format": "flac", "audio_bitrate": 900, + "audio_sample_rate": 44100, "audio_bit_depth": 16, "file_size_bytes": 12345, + "play_count": 2, "fed": fed, + }); + let state = serde_json::json!({ + "queue": [track, {"id": 0, "title": "Remote", "track_number": null, "disc_number": null, + "duration_seconds": 0.0, "release_id": 0, "release_title": "", "release_year": null, + "content_id": null, "audio_format": null, "audio_bitrate": null, + "audio_sample_rate": null, "audio_bit_depth": null, "file_size_bytes": null}], + "queue_pos": 1, "playing": true, "paused": false, "idle_since_ms": null, + "position_secs": 42.5, "volume": 73, "shuffle": true, "repeat": "all", + }); + let claim = serde_json::json!({"counter": 3, "issuer": "tui_a", "owner": "tui_a"}); + let stamp = serde_json::json!({"version": 1, "claim": claim, "sequence": 9}); + let op = |seq: i64, payload: serde_json::Value| { + serde_json::json!({"op_id": format!("tui_a:{seq}"), "origin_device_id": "tui_a", + "seq": seq, "hlc_ms": 1_700_000_000_000i64 + seq, "payload": payload}) + }; + let profile = |id: &str| { + serde_json::json!({"device_id": id, "name": "TUI", "client_version": "0.3.4", + "protocol_version": 3, "endpoint_id": "e", "endpoint_ticket": "t", + "revoked": false, "revoke_cutoff_seq": null, "updated_at_ms": 5}) + }; + serde_json::json!({ + "type": "hello", + "group_id": "grp_1", + "profile": profile("tui_a"), + "devices": [profile("tui_a"), profile("web_b")], + "vector": {"tui_a": 12, "web_b": 4}, + "ops": [ + op(1, serde_json::json!({"kind": "track_like_set", "content_id": CONTENT_ID, "liked": true, "fed": fed})), + op(2, serde_json::json!({"kind": "playlist_created", "playlist_id": "pl", "title": "Mix"})), + op(3, serde_json::json!({"kind": "playlist_renamed", "playlist_id": "pl", "title": "Mix 2"})), + op(4, serde_json::json!({"kind": "playlist_track_added", "playlist_id": "pl", "content_id": CONTENT_ID, "position": 0, "fed": null})), + op(5, serde_json::json!({"kind": "playlist_track_removed", "playlist_id": "pl", "content_id": CONTENT_ID})), + op(6, serde_json::json!({"kind": "playlist_deleted", "playlist_id": "pl"})), + op(7, serde_json::json!({"kind": "device_profile_set", "name": "TUI", "client_version": "0.3.4", "endpoint_ticket": "t", "endpoint_id": "e"})), + op(8, serde_json::json!({"kind": "device_trusted", "target_device_id": "web_b"})), + op(9, serde_json::json!({"kind": "device_revoked", "target_device_id": "old", "target_max_seq_seen": 3})), + op(10, serde_json::json!({"kind": "playback_command", "target_device_id": "web_b", + "command": {"kind": "set_state", "state": state, "seek": true}, "authority": stamp})), + op(11, serde_json::json!({"kind": "playback_command", "target_device_id": "web_b", + "command": {"kind": "active_changed", "active_device_id": "web_b", "active_device_name": "WEB", "state": state}})), + op(12, serde_json::json!({"kind": "listen_recorded", "event": {"listen_id": "l1", + "content_id": CONTENT_ID, "started_at_ms": 1, "listened_ms": 60000, "track_duration_ms": 180000, + "ended_reason": "finished", "track": {"title": "Song", "artist_names": ["A"], "featured_artist_names": [], "release_title": null}}})), + ], + "snapshot": { + "likes": [{"content_id": CONTENT_ID, "hlc_ms": 1, "op_id": "tui_a:1", "fed": fed}], + "unlikes": [{"content_id": CONTENT_ID, "hlc_ms": 2, "op_id": "tui_a:2"}], + "playlists": [{"playlist_id": "pl", "title": "Mix", "hlc_ms": 3, "op_id": "tui_a:3", + "items": [{"content_id": CONTENT_ID, "position": 0, "hlc_ms": 4, "op_id": "tui_a:4", "fed": null}]}], + "deleted_playlists": [{"playlist_id": "gone", "hlc_ms": 5, "op_id": "tui_a:5"}], + "removed_playlist_items": [{"playlist_id": "pl", "content_id": CONTENT_ID, "hlc_ms": 6, "op_id": "tui_a:6"}], + }, + "playback": {"device_id": "tui_a", "device_name": "TUI", "active": true, "updated_at_ms": 9, + "state": state, "coordination": {"version": 1, "claim": claim, "heartbeat": 4, "available": true, "playing": true}}, + }) + .to_string() +} + +fn v3_other_inputs() -> Vec { + vec![ + r#"{"type":"sync_response","accepted":false,"error":"device is not trusted"}"#.into(), + r#"{"type":"pair_response","accepted":false,"pending":true,"error":"pairing pending"}"# + .into(), + serde_json::json!({"type": "pair_request", "invite_id": "i", "secret": "s", + "profile": {"device_id": "tui_a", "name": "TUI", "client_version": "0.3.4", + "protocol_version": 3, "endpoint_id": "e", "endpoint_ticket": "t"}, + "vector": {}, "ops": [], "snapshot": {}}) + .to_string(), + ] +} + +/// Parses `input` with the 0.3.x types and the shared types and checks +/// that both serialize to the same bytes, and that each side parses the +/// other's output. +fn assert_identical(input: &str) -> String { + let old: legacy::WireMessage = serde_json::from_str(input).unwrap(); + let new: WireMessage = serde_json::from_str(input).unwrap(); + let old_json = serde_json::to_string(&old).unwrap(); + let new_json = serde_json::to_string(&new).unwrap(); + assert_eq!(old_json, new_json); + assert_eq!( + serde_json::from_str::(&new_json).unwrap(), + old + ); + assert_eq!(serde_json::from_str::(&old_json).unwrap(), new); + new_json +} + +#[test] +fn v3_messages_serialize_like_furumi_tui_0_3() { + let hello = assert_identical(&v3_hello_input()); + let golden = concat!( + env!("CARGO_MANIFEST_DIR"), + "/src/devices/fixtures/v3_hello.golden.json" + ); + if std::env::var_os("FURUMI_BLESS_GOLDEN").is_some() { + // Only for a deliberate change of the v3 shape. + std::fs::write(golden, format!("{hello}\n")).unwrap(); + } + assert_eq!(hello, std::fs::read_to_string(golden).unwrap().trim_end()); + for input in v3_other_inputs() { + assert_identical(&input); + } +} + +#[test] +fn music_queue_items_keep_their_v3_shape() { + let track = TrackItem { + id: 7, + title: "Song".into(), + track_number: Some(1), + disc_number: None, + duration_seconds: 180.5, + artists: vec![ArtistRef { + id: 1, + name: "A".into(), + }], + featured_artists: Vec::new(), + release_id: 3, + release_title: "Album".into(), + release_year: Some(2001), + file_path: "/Users/me/Music/song.flac".into(), + content_id: Some(CONTENT_ID.into()), + cover_path: None, + audio_format: Some("flac".into()), + audio_bitrate: Some(900), + audio_sample_rate: Some(44_100), + audio_bit_depth: Some(16), + file_size_bytes: Some(12_345), + play_count: 2, + fed: None, + podcast: None, + }; + let wire = PlaybackTrack::from_track(&track); + let json = serde_json::to_string(&wire).unwrap(); + assert!( + !json.contains("source"), + "music items carry no source: {json}" + ); + let old: legacy::PlaybackTrack = serde_json::from_str(&json).unwrap(); + assert_eq!(serde_json::to_string(&old).unwrap(), json); +} + +#[test] +fn invites_from_older_clients_stay_readable() { + let old = legacy::InviteWire { + v: 1, + ticket: "ticket".into(), + device_id: "tui_a".into(), + invite_id: "inv".into(), + secret: "s3cr3t".into(), + expires_at_ms: 10, + }; + let old_json = serde_json::to_string(&old).unwrap(); + assert_eq!( + old_json, + r#"{"v":1,"t":"ticket","d":"tui_a","i":"inv","s":"s3cr3t","e":10}"# + ); + let shared: InviteWire = serde_json::from_str(&old_json).unwrap(); + assert_eq!( + shared.inviter_protocol_version(), + music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION + ); + assert_eq!(serde_json::to_string(&shared).unwrap(), old_json); +} + +#[test] +fn v3_peers_cannot_parse_podcast_operations() { + // Why podcast operations must be gated: an unknown `kind` breaks the + // whole message for a 0.3.x peer, not one operation. + let mut hello: serde_json::Value = serde_json::from_str(&v3_hello_input()).unwrap(); + hello["ops"].as_array_mut().unwrap().push(serde_json::json!({ + "op_id": "tui_a:13", "origin_device_id": "tui_a", "seq": 13, "hlc_ms": 13, + "payload": {"kind": "podcast_share_set", + "podcast_id": music_dht::podcast::PodcastId::from_feed_url( + &music_dht::podcast::FeedUrl::parse("https://podcasts.example/feed.xml").unwrap()), + "shared": false, "generation": 1}, + })); + assert!(serde_json::from_value::(hello.clone()).is_err()); + assert!(serde_json::from_value::(hello).is_ok()); +} + +#[test] +fn v3_peers_would_drop_the_source_of_podcast_items() { + // Why podcast playback must be gated: older types ignore `source` and + // forward the episode as a broken music item. + let episode = crate::app::podcast_fixtures::episode_track(1) + .podcast + .unwrap(); + let wire = PlaybackTrack::podcast_episode(episode); + let json = serde_json::to_string(&wire).unwrap(); + assert!(json.contains(r#""source":{"kind":"podcast_episode""#)); + let old: legacy::PlaybackTrack = serde_json::from_str(&json).unwrap(); + let forwarded: PlaybackTrack = + serde_json::from_str(&serde_json::to_string(&old).unwrap()).unwrap(); + assert_eq!(forwarded.source, None); + assert_eq!(forwarded.id, 0); +} + +#[test] +fn version_reports_stay_readable_by_older_clients() { + let link = encode_invite(&InviteWire { + v: 1, + ticket: "ticket".into(), + device_id: "tui_a".into(), + invite_id: "inv".into(), + secret: "s3cr3t".into(), + expires_at_ms: 10, + protocol_version: Some(PROTOCOL_VERSION), + }) + .unwrap(); + let invite = parse_invite(&link).unwrap(); + assert_eq!(invite.inviter_protocol_version(), PROTOCOL_VERSION); + let json = serde_json::to_string(&invite).unwrap(); + assert!( + json.ends_with(&format!(r#","p":{PROTOCOL_VERSION}}}"#)), + "{json}" + ); + // `p` is an unknown field for 0.3.x and is ignored there. + assert_eq!( + serde_json::from_str::(&json) + .unwrap() + .invite_id, + "inv" + ); + + let announcement = SyncOpPayload::DeviceProfileSet { + name: "TUI".into(), + client_version: CLIENT_VERSION.into(), + endpoint_ticket: "t".into(), + endpoint_id: "e".into(), + protocol_version: Some(PROTOCOL_VERSION), + }; + let json = serde_json::to_string(&announcement).unwrap(); + assert!(json.ends_with(&format!(r#","protocol_version":{PROTOCOL_VERSION}}}"#))); + let old: legacy::SyncOpPayload = serde_json::from_str(&json).unwrap(); + assert!(matches!( + old, + legacy::SyncOpPayload::DeviceProfileSet { .. } + )); +} diff --git a/src/federation/mod.rs b/src/federation/mod.rs index 5ac9604..019d857 100644 --- a/src/federation/mod.rs +++ b/src/federation/mod.rs @@ -2333,6 +2333,7 @@ fn metadata_preview_track( .or(base.file_size_bytes), play_count: base.play_count, fed: Some(fed.clone()), + podcast: None, } } @@ -2948,6 +2949,7 @@ pub fn pending_track(fed: &FedTrack) -> TrackItem { file_size_bytes: None, play_count: 0, fed: Some(fed.clone()), + podcast: None, } } @@ -3020,6 +3022,7 @@ fn ephemeral_track( // Keep the federation reference: the cached track can still be // liked, re-downloaded and marked as federated in the lists. fed: Some(fed.clone()), + podcast: None, } } diff --git a/src/jam.rs b/src/jam.rs index b6bff7a..2fd3e5c 100644 --- a/src/jam.rs +++ b/src/jam.rs @@ -225,10 +225,16 @@ impl JamManager { JamStatus::default() } + /// Publishes the host's playback to participants. Podcast episodes are + /// private: participants get opaque placeholders (`redact_for_jam`), + /// never their titles, feeds or URLs. pub fn publish_host_playback(&self, snapshot: PlaybackSnapshot) { let mut state = lock(&self.state); if state.host.invite.is_some() { - state.host.playback = Some(snapshot); + state.host.playback = Some(PlaybackSnapshot { + state: music_dht::device_sync::redact_for_jam(snapshot.state), + ..snapshot + }); } } @@ -374,7 +380,15 @@ async fn serve_one(mut stream: ByteStream, manager: Arc) -> Result<( acknowledged.push(command.command_id.clone()); if state.host.seen_commands.insert(command.command_id.clone()) { state.host.seen_order.push_back(command.command_id.clone()); - let _ = manager.event_tx.send(AppEvent::JamCommand(command.command)); + // Participants cannot add podcast episodes to the + // host queue; placeholders are mapped back by the + // host (`restore_jam_private_items`). + if music_dht::device_sync::jam_command_allowed(&command.command) { + let _ = + manager.event_tx.send(AppEvent::JamCommand(command.command)); + } else { + tracing::info!("ignored a Jam command that adds podcast episodes"); + } } } while state.host.seen_order.len() > 4096 { @@ -656,8 +670,14 @@ mod tests { .join(&invite, "dev_guest".into(), "Guest".into()) .unwrap(); + // The host plays a private podcast episode. + let episode = crate::app::podcast_fixtures::episode_track(1) + .podcast + .unwrap(); let playback_state = crate::devices::PlaybackStateWire { - queue: Vec::new(), + queue: vec![crate::devices::PlaybackTrack::podcast_episode( + episode.clone(), + )], queue_pos: 0, playing: false, paused: false, @@ -666,6 +686,7 @@ mod tests { volume: 73, shuffle: false, repeat: crate::devices::PlaybackRepeat::Off, + playback_rate: None, }; host.publish_host_playback(PlaybackSnapshot { coordination: None, @@ -690,12 +711,27 @@ mod tests { .unwrap(); assert_eq!(playback.device_id, "dev_host"); assert_eq!(playback.state.volume, 73); + // Guests get an opaque placeholder: no title, feed or URL. + assert!(playback.state.queue[0].is_private_placeholder()); + let seen = serde_json::to_string(&playback).unwrap(); + assert!(!seen.contains(&episode.audio_url) && !seen.contains(&episode.episode_title)); + // A guest cannot add an episode to the host's queue; its ordinary + // command (with the placeholder) still arrives. + guest + .submit_command(PlaybackCommand::SetState { + state: crate::devices::PlaybackStateWire { + queue: vec![crate::devices::PlaybackTrack::podcast_episode(episode)], + ..playback_state.clone() + }, + seek: false, + }) + .unwrap(); guest .submit_command(PlaybackCommand::SetState { state: crate::devices::PlaybackStateWire { paused: true, - ..playback_state + ..playback.state }, seek: false, }) @@ -710,13 +746,17 @@ mod tests { }) .await .unwrap(); - assert!(matches!( - command, - PlaybackCommand::SetState { - state: crate::devices::PlaybackStateWire { paused: true, .. }, - seek: false - } - )); + let PlaybackCommand::SetState { + state: received, + seek: false, + } = command + else { + panic!("expected the guest's set_state"); + }; + // The command that adds an episode was sent first; had it been + // forwarded, it would have arrived first. + assert!(received.paused); + assert!(received.queue[0].is_private_placeholder()); server.abort(); host_drain.abort(); diff --git a/src/main.rs b/src/main.rs index c906a76..9cbff22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod jam; mod library; mod media; mod player; +mod podcasts; mod share; mod similarity; mod status; diff --git a/src/player/mod.rs b/src/player/mod.rs index f25d167..2f9c7bd 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -41,6 +41,14 @@ pub enum PlayerEvent { has_next: bool, }, Failed(String), + /// A seek was handled for the source started under `generation` + /// ([`Controller::source_generation`]); `result` says whether the source + /// actually moved to `position`. + Seeked { + generation: u64, + position: Duration, + result: Result<(), String>, + }, } enum Command { @@ -51,6 +59,18 @@ enum Command { seekable: bool, volume: f32, }, + /// Play a source that was already opened, probed and positioned off + /// this thread (remote podcast streams, whose probing and seeking wait + /// on the network). Dropped unless `generation` is still current. It + /// starts paused if the latest pause/resume says so + /// ([`Controller::expect_source`]), not as it was when requested. + PlaySource { + source: DecodedSource, + volume: f32, + /// Where `source` was positioned before it was handed over. + start: Duration, + generation: u64, + }, /// Append the next track behind the current one without interrupting /// playback — rodio switches sources back to back (gapless-ish). Enqueue { @@ -91,6 +111,13 @@ pub struct Controller { tx: Sender, pub shared: Arc, playback_allowed: Arc, + /// Bumped by every start and stop; a source prepared in the background + /// plays only if no start or stop happened since it was requested. + generation: Arc, + /// The user's latest play/pause intent for a source still being + /// prepared: set by [`Controller::expect_source`], [`Controller::pause`] + /// and [`Controller::resume`], read when the source is handed over. + start_paused: Arc, } impl Controller { @@ -102,7 +129,52 @@ impl Controller { } } + /// The current source generation: capture it when starting to prepare a + /// source in the background and pass it to [`Controller::play_source`]. + pub fn source_generation(&self) -> u64 { + self.generation.load(Ordering::SeqCst) + } + + fn supersede(&self) { + self.generation.fetch_add(1, Ordering::SeqCst); + } + + /// Whether a start or stop happened since `generation` was read; for + /// cancelling background preparation and reads of a superseded source. + pub fn superseded_check(&self, generation: u64) -> impl Fn() -> bool + Send + Sync + 'static { + let current = Arc::clone(&self.generation); + move || current.load(Ordering::SeqCst) != generation + } + + /// A source is being prepared in the background and should start + /// paused or not. Pause and resume calls made before it is handed over + /// update this intent, so a pause pressed while it connects is kept. + pub fn expect_source(&self, paused: bool) { + self.start_paused.store(paused, Ordering::SeqCst); + } + + /// Play a source decoded (and possibly positioned at `start`) outside + /// the audio thread, built with [`decode_source_with`]. The audio thread + /// drops it when another start or stop happened after `generation` was + /// read, so a slow stream never interrupts what the user chose since. + pub fn play_source( + &self, + source: DecodedSource, + volume: f32, + start: Duration, + generation: u64, + ) { + let _ = self.tx.send(Command::PlaySource { + source, + volume, + start, + generation, + }); + } + pub fn play(&self, reader: TrackReader, byte_len: Option, volume: f32) { + self.supersede(); + self.start_paused.store(false, Ordering::SeqCst); let _ = self.tx.send(Command::Play { reader, byte_len, @@ -113,6 +185,8 @@ impl Controller { } pub fn play_stream(&self, reader: TrackReader, mime_type: Option, volume: f32) { + self.supersede(); + self.start_paused.store(false, Ordering::SeqCst); let _ = self.tx.send(Command::Play { reader, byte_len: None, @@ -127,14 +201,17 @@ impl Controller { } pub fn pause(&self) { + self.start_paused.store(true, Ordering::SeqCst); let _ = self.tx.send(Command::Pause); } pub fn resume(&self) { + self.start_paused.store(false, Ordering::SeqCst); let _ = self.tx.send(Command::Resume); } pub fn stop(&self) { + self.supersede(); let _ = self.tx.send(Command::Stop); } @@ -153,31 +230,73 @@ pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Controller { let thread_shared = Arc::clone(&shared); let playback_allowed = Arc::new(AtomicBool::new(true)); let thread_allowed = Arc::clone(&playback_allowed); + let generation = Arc::new(AtomicU64::new(0)); + let thread_generation = Arc::clone(&generation); + let start_paused = Arc::new(AtomicBool::new(false)); + let thread_start_paused = Arc::clone(&start_paused); std::thread::Builder::new() .name("audio".to_string()) - .spawn(move || run(rx, thread_shared, thread_allowed, on_event)) + .spawn(move || { + run( + rx, + thread_shared, + thread_allowed, + Generations { + current: thread_generation, + start_paused: thread_start_paused, + }, + on_event, + ) + }) .expect("spawning the audio thread cannot fail"); Controller { tx, shared, playback_allowed, + generation, + start_paused, } } +/// Controller state the audio thread reads. +struct Generations { + current: Arc, + start_paused: Arc, +} + struct Output { /// Keeps the audio device open; dropping it stops the mixer. _device: MixerDeviceSink, player: Player, } +/// Audio-thread state besides the output device. +struct Playing { + track_loaded: bool, + /// Position of the current source when it was appended: sources + /// positioned before playback (resumed podcast episodes) start there, + /// while rodio counts from zero until the next seek. + base: Duration, + /// Controller generation the current source was started under. When + /// the controller moved on, the end of this source is not reported: a + /// stop or another start is already on its way. + generation: u64, +} + fn run( rx: Receiver, shared: Arc, playback_allowed: Arc, + generations: Generations, on_event: impl Fn(PlayerEvent), ) { + let generation = &generations.current; let mut output: Option = None; - let mut track_loaded = false; + let mut playing = Playing { + track_loaded: false, + base: Duration::ZERO, + generation: generation.load(Ordering::SeqCst), + }; let mut last_len = 0usize; loop { @@ -186,31 +305,68 @@ fn run( if !playback_allowed.load(Ordering::Acquire) && matches!( command, - Command::Play { .. } | Command::Enqueue { .. } | Command::Resume + Command::Play { .. } + | Command::PlaySource { .. } + | Command::Enqueue { .. } + | Command::Resume ) { continue; } + if let Command::PlaySource { + generation: requested, + .. + } = &command + && *requested != generation.load(Ordering::SeqCst) + { + // Superseded while it was being prepared. + continue; + } + let started_under = match &command { + Command::PlaySource { + generation: requested, + .. + } => Some(*requested), + Command::Play { .. } | Command::Stop => Some(generation.load(Ordering::SeqCst)), + _ => None, + }; // Commands change the source queue legitimately; resync the // length so the next tick doesn't read it as a track ending. - handle(command, &shared, &mut output, &mut track_loaded, &on_event); + handle( + command, + &shared, + &generations.start_paused, + &mut output, + &mut playing, + &on_event, + ); + if let Some(started_under) = started_under { + playing.generation = started_under; + } last_len = output.as_ref().map_or(0, |out| out.player.len()); } Err(RecvTimeoutError::Timeout) => { if let Some(out) = &output { + let position = out.player.get_pos() + playing.base; shared .position_ms - .store(out.player.get_pos().as_millis() as u64, Ordering::Relaxed); + .store(position.as_millis() as u64, Ordering::Relaxed); shared .paused .store(out.player.is_paused(), Ordering::Relaxed); let len = out.player.len(); - if track_loaded && len < last_len { + if playing.track_loaded && len < last_len { + // The next (gapless) source starts at zero. + playing.base = Duration::ZERO; if len == 0 { - track_loaded = false; + playing.track_loaded = false; } - for _ in len..last_len { - on_event(PlayerEvent::TrackFinished { has_next: len > 0 }); + // A source that ended because it was superseded + // (its stream cancelled) did not finish. + if generation.load(Ordering::SeqCst) == playing.generation { + for _ in len..last_len { + on_event(PlayerEvent::TrackFinished { has_next: len > 0 }); + } } } last_len = len; @@ -224,8 +380,9 @@ fn run( fn handle( command: Command, shared: &Arc, + start_paused: &AtomicBool, output: &mut Option, - track_loaded: &mut bool, + playing: &mut Playing, on_event: &impl Fn(PlayerEvent), ) { match command { @@ -236,42 +393,35 @@ fn handle( seekable, volume, } => { - // The device is opened lazily on first playback so the app works - // on machines with no audio output until you actually press play. - if output.is_none() { - match DeviceSinkBuilder::open_default_sink() { - Ok(device) => { - let player = Player::connect_new(device.mixer()); - *output = Some(Output { - _device: device, - player, - }); - } - Err(err) => { - on_event(PlayerEvent::Failed(format!("no audio device: {err}"))); - return; - } - } - } - let out = output.as_ref().expect("output opened above"); - + let Some(out) = ensure_output(output, on_event) else { + return; + }; match decode_source(reader, byte_len, mime_type.as_deref(), seekable) { - Ok(source) => { - shared.analysis.clear(); - out.player.stop(); - out.player.set_volume(volume); - out.player.append(analyzer::AnalyzedSource::new( - source, - Arc::clone(&shared.analysis), - )); - out.player.play(); - *track_loaded = true; - on_event(PlayerEvent::Started); - } + Ok(source) => start_source(out, source, volume, false, shared, on_event), Err(err) => { on_event(PlayerEvent::Failed(format!("cannot decode track: {err}"))); + return; } } + playing.track_loaded = true; + playing.base = Duration::ZERO; + } + Command::PlaySource { + source, + volume, + start, + generation: _, + } => { + let Some(out) = ensure_output(output, on_event) else { + return; + }; + let paused = start_paused.load(Ordering::SeqCst); + start_source(out, source, volume, paused, shared, on_event); + playing.track_loaded = true; + playing.base = start; + shared + .position_ms + .store(start.as_millis() as u64, Ordering::Relaxed); } Command::Enqueue { reader, byte_len } => { let Some(out) = output.as_ref() else { @@ -304,15 +454,25 @@ fn handle( out.player.stop(); } shared.analysis.clear(); - *track_loaded = false; + playing.track_loaded = false; + playing.base = Duration::ZERO; } Command::Seek(position) => { - if let Some(out) = output - && let Err(err) = out.player.try_seek(position) - { - tracing::warn!(%err, "seek failed"); + let result = match output { + Some(out) => out.player.try_seek(position).map_err(|err| err.to_string()), + None => Err("nothing is playing".to_string()), + }; + match &result { + // rodio now reports absolute positions. + Ok(()) => playing.base = Duration::ZERO, + Err(err) => tracing::warn!(%err, "seek failed"), } shared.analysis.clear(); + on_event(PlayerEvent::Seeked { + generation: playing.generation, + position, + result, + }); } Command::SetVolume(volume) => { if let Some(out) = output { @@ -322,13 +482,74 @@ fn handle( } } +/// The device is opened lazily on first playback so the app works on +/// machines with no audio output until you actually press play. +fn ensure_output<'a>( + output: &'a mut Option, + on_event: &impl Fn(PlayerEvent), +) -> Option<&'a Output> { + if output.is_none() { + match DeviceSinkBuilder::open_default_sink() { + Ok(device) => { + let player = Player::connect_new(device.mixer()); + *output = Some(Output { + _device: device, + player, + }); + } + Err(err) => { + on_event(PlayerEvent::Failed(format!("no audio device: {err}"))); + return None; + } + } + } + output.as_ref() +} + +/// Replace whatever is playing with `source`. +fn start_source( + out: &Output, + source: DecodedSource, + volume: f32, + paused: bool, + shared: &Shared, + on_event: &impl Fn(PlayerEvent), +) { + shared.analysis.clear(); + out.player.stop(); + out.player.set_volume(volume); + out.player.append(analyzer::AnalyzedSource::new( + source, + Arc::clone(&shared.analysis), + )); + if paused { + out.player.pause(); + } else { + out.player.play(); + } + on_event(PlayerEvent::Started); +} + pub(crate) type DecodedSource = Box; pub(crate) fn decode_source( + reader: TrackReader, + byte_len: Option, + mime_type: Option<&str>, + seekable: bool, +) -> Result { + decode_source_with(reader, byte_len, mime_type, seekable, false) +} + +/// `decode_source` with optional coarse (byte-estimate) seeking, which +/// remote streams need: an accurate MP3 seek decodes every frame before the +/// target, i.e. downloads it. +pub(crate) fn decode_source_with( mut reader: TrackReader, byte_len: Option, mime_type: Option<&str>, seekable: bool, + coarse_seek: bool, ) -> Result { let mime_is_opus = mime_type.is_some_and(|mime| { let mime = mime.to_ascii_lowercase(); @@ -344,6 +565,7 @@ pub(crate) fn decode_source( let mut builder = Decoder::builder() .with_data(reader) .with_seekable(seekable) + .with_coarse_seek(coarse_seek) .with_gapless(true); if let Some(len) = byte_len { builder = builder.with_byte_len(len); @@ -356,3 +578,60 @@ pub(crate) fn decode_source( .map(|decoder| Box::new(decoder) as DecodedSource) .map_err(|error| error.to_string()) } + +#[cfg(test)] +mod tests { + use super::*; + + /// A controller without the audio thread; the commands it sends are + /// returned instead (no audio device in tests). + fn detached() -> (Controller, Receiver) { + let (tx, rx) = std::sync::mpsc::channel(); + let controller = Controller { + tx, + shared: Arc::default(), + playback_allowed: Arc::new(AtomicBool::new(true)), + generation: Arc::new(AtomicU64::new(0)), + start_paused: Arc::new(AtomicBool::new(false)), + }; + (controller, rx) + } + + /// The audio thread starts a prepared source with the intent read when + /// the source arrives (`Command::PlaySource` reads `start_paused`), so a + /// pause pressed while an episode was connecting is kept, and a resume + /// pressed after a paused start is too. + #[test] + fn play_and_pause_pressed_while_a_source_is_prepared_are_kept() { + let (controller, rx) = detached(); + controller.stop(); + let generation = controller.source_generation(); + let superseded = controller.superseded_check(generation); + controller.expect_source(false); + controller.pause(); + controller.play_source( + Box::new(rodio::source::Zero::new( + std::num::NonZero::new(1).unwrap(), + std::num::NonZero::new(8_000).unwrap(), + )), + 1.0, + Duration::ZERO, + generation, + ); + let commands: Vec<_> = rx.try_iter().collect(); + assert!(matches!( + commands.as_slice(), + [Command::Stop, Command::Pause, Command::PlaySource { .. }] + )); + assert!(controller.start_paused.load(Ordering::SeqCst)); + assert!(!superseded(), "pausing does not supersede the source"); + + controller.expect_source(true); + controller.resume(); + assert!(!controller.start_paused.load(Ordering::SeqCst)); + + // Another start or a stop supersedes the prepared source. + controller.stop(); + assert!(superseded()); + } +} diff --git a/src/podcasts/fetch.rs b/src/podcasts/fetch.rs new file mode 100644 index 0000000..f6cbb3f --- /dev/null +++ b/src/podcasts/fetch.rs @@ -0,0 +1,389 @@ +//! HTTP for podcast feeds and cover images. +//! +//! Redirects are followed by hand so the caller learns where the feed +//! really lives: the address reached by the leading permanent (301/308) +//! hops is where the feed moved, and the store records it as the new fetch +//! URL (the old one becomes an alias). Temporary hops after it (a CDN or a +//! tracking prefix) do not move the feed further. Conditional requests +//! (`If-None-Match` / `If-Modified-Since`), response size caps, timeouts and +//! the user agent are fixed here; parsing and storage happen elsewhere. + +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::Duration; + +use anyhow::{Context, Result, anyhow, bail}; +use music_dht::podcast::{FeedUrl, PodcastId}; +use reqwest::header::{ + ACCEPT, ETAG, HeaderMap, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, RETRY_AFTER, +}; +use reqwest::{StatusCode, Url}; + +/// Hard cap on a feed document (large catalogs are a few MiB). +pub const MAX_FEED_BYTES: usize = 32 * 1024 * 1024; +/// Hard cap on a cover image. +pub const MAX_COVER_BYTES: usize = 10 * 1024 * 1024; +/// Redirect hops followed before giving up. +pub const MAX_REDIRECTS: usize = 10; +/// TCP + TLS connection setup. +pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(15); +/// Whole request, including the body, per hop. +pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60); +/// A whole feed or cover fetch, all redirect hops and the body included, so +/// a slow server holds one of the few refresh slots for at most this long. +#[cfg(not(test))] +pub const FETCH_DEADLINE: Duration = Duration::from_secs(120); +#[cfg(test)] +pub const FETCH_DEADLINE: Duration = Duration::from_secs(3); +/// Sent with every podcast request. +pub const USER_AGENT: &str = concat!("furumi/", env!("CARGO_PKG_VERSION"), " (podcast reader)"); + +const FEED_ACCEPT: &str = "application/rss+xml, application/atom+xml, application/xml;q=0.9, \ + text/xml;q=0.9, */*;q=0.5"; + +/// HTTP validators of the last successful fetch. +#[derive(Debug, Clone, Copy, Default)] +pub struct Validators<'a> { + pub etag: Option<&'a str>, + pub last_modified: Option<&'a str>, +} + +/// What a feed request returned. +#[derive(Debug)] +pub enum FeedBody { + /// `304 Not Modified` to a conditional request. + NotModified, + /// A new document with its validators. + Modified { + body: Vec, + etag: Option, + last_modified: Option, + }, +} + +/// A feed response and the redirect chain that led to it. +#[derive(Debug)] +pub struct FeedResponse { + pub body: FeedBody, + /// URL that finally answered, canonicalized; the base for relative + /// URLs in the document. + pub final_url: FeedUrl, + /// Where the leading permanent redirects (301/308, one or more, before + /// any temporary one) lead: the feed's new address. `None` when the + /// first hop was temporary or there was none. Temporary redirects + /// (tracking prefixes, CDNs) never move a feed. + pub moved_permanently_to: Option, + /// Redirect hops followed. + pub redirects: usize, +} + +/// A non-success HTTP status, with the server's `Retry-After` (seconds form) +/// so the scheduler can back off as asked. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct HttpStatusError { + pub status: u16, + pub retry_after: Option, +} + +impl std::fmt::Display for HttpStatusError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let reason = StatusCode::from_u16(self.status) + .ok() + .and_then(|status| status.canonical_reason()) + .unwrap_or("error"); + write!(f, "HTTP {} {reason}", self.status) + } +} + +impl std::error::Error for HttpStatusError {} + +/// Shared HTTP client for feeds and covers. +pub struct Fetcher { + client: reqwest::Client, + covers_dir: PathBuf, +} + +impl Fetcher { + pub fn new(covers_dir: PathBuf) -> Result { + let _ = rustls::crypto::ring::default_provider().install_default(); + let client = reqwest::Client::builder() + .connect_timeout(CONNECT_TIMEOUT) + .timeout(REQUEST_TIMEOUT) + .redirect(reqwest::redirect::Policy::none()) + .user_agent(USER_AGENT) + .build() + .context("building the podcast HTTP client")?; + Ok(Fetcher { client, covers_dir }) + } + + /// GET of a feed. `validators` make it conditional; they are dropped + /// after a permanent redirect because they belong to the old URL. + pub async fn fetch_feed( + &self, + url: &FeedUrl, + validators: Validators<'_>, + ) -> Result { + within_deadline("feed", self.fetch_feed_unbounded(url, validators)).await + } + + async fn fetch_feed_unbounded( + &self, + url: &FeedUrl, + validators: Validators<'_>, + ) -> Result { + let Followed { + response, + url: final_url, + moved_permanently_to, + redirects, + conditional, + } = self + .get_following(url.as_str(), Some(FEED_ACCEPT), validators) + .await?; + let final_url = FeedUrl::parse(final_url.as_str()).map_err(|err| { + anyhow!( + "the feed was redirected to an unsupported URL {}: {err}", + redact(&final_url) + ) + })?; + // An address that is not a valid feed URL is not worth moving to. + let moved_permanently_to = + moved_permanently_to.and_then(|url| FeedUrl::parse(url.as_str()).ok()); + let status = response.status(); + if status == StatusCode::NOT_MODIFIED { + if !conditional { + bail!("the server answered 304 Not Modified to an unconditional request"); + } + return Ok(FeedResponse { + body: FeedBody::NotModified, + final_url, + moved_permanently_to, + redirects, + }); + } + if !status.is_success() { + return Err(status_error(status, response.headers()).into()); + } + let etag = header_string(response.headers(), ETAG); + let last_modified = header_string(response.headers(), LAST_MODIFIED); + let body = read_capped(response, MAX_FEED_BYTES, "feed").await?; + Ok(FeedResponse { + body: FeedBody::Modified { + body, + etag, + last_modified, + }, + final_url, + moved_permanently_to, + redirects, + }) + } + + /// Downloads a cover image into the covers directory; returns its path. + pub async fn fetch_cover(&self, podcast_id: &PodcastId, image_url: &str) -> Result { + let body = within_deadline("cover image", self.download_cover(image_url)).await?; + let format = image::guess_format(&body).context("the cover is not a supported image")?; + let extension = format.extensions_str().first().copied().unwrap_or("img"); + let name = cover_file_name(podcast_id, image_url, extension); + let dir = self.covers_dir.clone(); + tokio::task::spawn_blocking(move || write_atomically(&dir, &name, &body)) + .await + .map_err(|err| anyhow!("cover writer task failed: {err}"))? + } + + async fn download_cover(&self, image_url: &str) -> Result> { + let response = self + .get_following(image_url, None, Validators::default()) + .await? + .response; + let status = response.status(); + if !status.is_success() { + return Err(status_error(status, response.headers()).into()); + } + read_capped(response, MAX_COVER_BYTES, "cover image").await + } + + /// GET following up to [`MAX_REDIRECTS`] http(s) redirects by hand. + async fn get_following( + &self, + url: &str, + accept: Option<&str>, + validators: Validators<'_>, + ) -> Result { + let mut current = Url::parse(url).with_context(|| format!("invalid URL {url:?}"))?; + check_scheme(¤t)?; + // Still in the leading run of permanent hops. + let mut permanent_run = true; + let mut moved_permanently_to = None; + let mut send_validators = true; + let mut hops = 0; + loop { + let mut request = self.client.get(current.clone()); + if let Some(accept) = accept { + request = request.header(ACCEPT, accept); + } + if send_validators { + if let Some(etag) = validators.etag { + request = request.header(IF_NONE_MATCH, etag); + } + if let Some(last_modified) = validators.last_modified { + request = request.header(IF_MODIFIED_SINCE, last_modified); + } + } + let response = request + .send() + .await + // reqwest errors print the full URL, query tokens included. + .map_err(reqwest::Error::without_url) + .with_context(|| format!("requesting {}", redact(¤t)))?; + let status = response.status(); + if !status.is_redirection() || status == StatusCode::NOT_MODIFIED { + let conditional = send_validators + && (validators.etag.is_some() || validators.last_modified.is_some()); + return Ok(Followed { + response, + url: current, + moved_permanently_to, + redirects: hops, + conditional, + }); + } + let location = response + .headers() + .get(LOCATION) + .and_then(|value| value.to_str().ok()) + .ok_or_else(|| anyhow!("HTTP {status} redirect without a Location header"))?; + let next = current + .join(location.trim()) + .with_context(|| format!("invalid redirect location {location:?}"))?; + check_scheme(&next)?; + hops += 1; + if hops > MAX_REDIRECTS { + bail!("too many redirects (more than {MAX_REDIRECTS})"); + } + let permanent = matches!( + status, + StatusCode::MOVED_PERMANENTLY | StatusCode::PERMANENT_REDIRECT + ); + permanent_run &= permanent; + if permanent_run { + moved_permanently_to = Some(next.clone()); + } + if permanent { + send_validators = false; + } + current = next; + } + } +} + +/// Fails `fetch` once [`FETCH_DEADLINE`] has passed. +async fn within_deadline(what: &str, fetch: impl Future>) -> Result { + tokio::time::timeout(FETCH_DEADLINE, fetch) + .await + .unwrap_or_else(|_| { + bail!( + "the {what} took longer than {} seconds", + FETCH_DEADLINE.as_secs() + ) + }) +} + +struct Followed { + response: reqwest::Response, + /// URL that answered. + url: Url, + /// Target of the leading permanent hops. + moved_permanently_to: Option, + redirects: usize, + /// The final request carried validators. + conditional: bool, +} + +fn check_scheme(url: &Url) -> Result<()> { + if !matches!(url.scheme(), "http" | "https") { + bail!("unsupported URL scheme {:?}", url.scheme()); + } + if url.host_str().is_none_or(str::is_empty) { + bail!("URL has no host"); + } + Ok(()) +} + +/// The URL without query and fragment, for messages and logs: feed URLs +/// may carry private tokens in their query. +pub fn redact(url: &Url) -> String { + let mut shown = url.clone(); + if shown.query().is_some() { + shown.set_query(Some("…")); + } + shown.set_fragment(None); + shown.to_string() +} + +fn header_string(headers: &HeaderMap, name: reqwest::header::HeaderName) -> Option { + headers + .get(name) + .and_then(|value| value.to_str().ok()) + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_string) +} + +fn status_error(status: StatusCode, headers: &HeaderMap) -> HttpStatusError { + let retry_after = header_string(headers, RETRY_AFTER) + .and_then(|value| value.parse::().ok()) + .map(Duration::from_secs); + HttpStatusError { + status: status.as_u16(), + retry_after, + } +} + +async fn read_capped(mut response: reqwest::Response, cap: usize, what: &str) -> Result> { + if let Some(len) = response.content_length() + && len > cap as u64 + { + bail!("the {what} is too large ({len} bytes, limit {cap})"); + } + let mut body = Vec::new(); + while let Some(chunk) = response + .chunk() + .await + .map_err(reqwest::Error::without_url) + .with_context(|| format!("reading the {what}"))? + { + if body.len() + chunk.len() > cap { + bail!("the {what} is too large (over {cap} bytes)"); + } + body.extend_from_slice(&chunk); + } + Ok(body) +} + +/// Stable per podcast and image URL, so a changed image gets a new file and +/// the previous one can be removed. +fn cover_file_name(podcast_id: &PodcastId, image_url: &str, extension: &str) -> String { + let podcast = blake3::hash(podcast_id.as_str().as_bytes()).to_hex(); + let image = blake3::hash(image_url.as_bytes()).to_hex(); + format!("{}-{}.{extension}", &podcast[..16], &image[..16]) +} + +fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result { + static COUNTER: AtomicU64 = AtomicU64::new(0); + std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?; + let target = dir.join(name); + let temp = dir.join(format!( + ".{name}.{}-{}.tmp", + std::process::id(), + COUNTER.fetch_add(1, Ordering::Relaxed) + )); + let result = std::fs::write(&temp, bytes) + .and_then(|()| std::fs::rename(&temp, &target)) + .with_context(|| format!("writing {}", target.display())); + if result.is_err() { + let _ = std::fs::remove_file(&temp); + } + result?; + Ok(target) +} diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs new file mode 100644 index 0000000..aafa3f2 --- /dev/null +++ b/src/podcasts/mod.rs @@ -0,0 +1,231 @@ +//! Podcast runtime of the TUI: feed HTTP, refresh scheduling and the glue +//! between the shared podcast store and the device-sync log. +//! +//! The data model, identity rules, merge rules and the SQLite store are not +//! defined here: they come from FRID (`music_dht::podcast` and +//! `furumi_library::podcasts::PodcastStore`, file `podcasts.sqlite3`). This +//! module only does what the contract leaves to clients: +//! +//! * [`fetch`] downloads feeds (conditional GET, redirects reported so +//! permanent moves become feed aliases, size and time limits) and covers; +//! * [`refresh`] subscribes and refreshes feeds: it parses them with the +//! shared `music_dht::podcast::feed` parser, ingests them with +//! `PodcastStore::apply_feed` and keeps the local fetch state (validators, +//! errors, back-off); +//! * [`OpLog`] hands store actions the stamps of the device-sync log and +//! appends the operations they return; +//! * [`stream`] plays an episode straight from the publisher through a +//! bounded HTTP Range reader, and [`playback`] resolves the current audio +//! URL and resume position, opens and positions it; +//! * [`progress`] writes resume positions, completions and listening +//! sessions in order on its own thread. +//! +//! Only RSS metadata is stored. Audio stays on the publisher's server. + +pub mod fetch; +pub mod playback; +pub mod progress; +pub mod refresh; +pub mod stream; + +#[cfg(test)] +mod playback_tests; +#[cfg(test)] +mod stream_tests; +#[cfg(test)] +mod test_server; +#[cfg(test)] +mod tests; + +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use anyhow::{Result, anyhow}; +use furumi_library::podcasts::{ + LEGACY_PROTOTYPE_SCHEMA_VERSION, PODCAST_DB_FILE, PodcastListFilter, PodcastLocalOp, + PodcastStore, PodcastStoreError, StampSource, +}; + +pub use fetch::Fetcher; +use refresh::Services; + +/// Appends podcast operations to the device-sync log. +/// +/// [`OpLog::record`] gives `action` a [`StampSource`] that reserves stamps +/// in the log. The action runs a `PodcastStore` local action and returns the +/// operations it emitted; they are appended with exactly those stamps. +pub trait OpLog: Send + Sync { + fn record( + &self, + action: &mut dyn FnMut(&mut dyn StampSource) -> Result>, + ) -> Result<()>; +} + +/// The podcast store file. It is the file the prototype podcast build +/// used, so an existing prototype database is migrated in place. +pub fn db_path() -> Result { + let dirs = crate::config::project_dirs() + .ok_or_else(|| anyhow!("no home directory for podcast data"))?; + Ok(dirs.data_dir().join(PODCAST_DB_FILE)) +} + +/// Replaceable cache of downloaded podcast covers. +pub fn covers_dir() -> Result { + let dirs = crate::config::project_dirs() + .ok_or_else(|| anyhow!("no cache directory for podcast covers"))?; + Ok(dirs.cache_dir().join("podcasts").join("covers")) +} + +/// Opens the podcast store at `db_path` and builds the feed client. +/// +/// `PodcastStore::open` migrates a prototype database in place and refuses, +/// without writing anything, a database from a newer Furumi or a file that +/// is not a podcast database. Blocking: call it on the blocking pool. +pub fn open_services(db_path: &Path, covers_dir: PathBuf, log: Arc) -> Result { + // The import commits inside `PodcastStore::open`; dropping its + // validators happens afterwards. A marker written before the import and + // removed only once every podcast was handled makes that step resumable + // after a crash or a failed write in between. + let marker = validators_marker(db_path); + let mut pending = marker.is_file(); + if is_prototype_database(db_path) { + if let Err(err) = std::fs::write(&marker, b"") { + tracing::warn!("cannot record the pending prototype feed checks: {err}"); + } + pending = true; + } + let store = PodcastStore::open(db_path)?; + tracing::info!(path = %db_path.display(), "podcast store opened"); + if pending && forget_prototype_validators(&store) { + match std::fs::remove_file(&marker) { + Ok(()) => {} + Err(err) if err.kind() == std::io::ErrorKind::NotFound => {} + Err(err) => tracing::warn!("cannot remove {}: {err}", marker.display()), + } + } + match store.legacy_quarantine() { + Ok(entries) if !entries.is_empty() => tracing::warn!( + count = entries.len(), + "some prototype podcast rows could not be imported; they are kept in the legacy_* tables" + ), + Ok(_) => {} + Err(err) => tracing::debug!("reading the podcast import quarantine failed: {err:#}"), + } + Ok(Services { + store: Arc::new(store), + fetcher: Arc::new(Fetcher::new(covers_dir)?), + log, + }) +} + +/// Whether `path` is a database of the prototype podcast build, which +/// `PodcastStore::open` is about to import. Read-only; any doubt is "no". +fn is_prototype_database(path: &Path) -> bool { + if !path.is_file() { + return false; + } + let flags = + rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX; + rusqlite::Connection::open_with_flags(path, flags) + .and_then(|conn| conn.query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0))) + .is_ok_and(|version| version == LEGACY_PROTOTYPE_SCHEMA_VERSION) +} + +/// Marker next to the podcast store: the validators imported from a +/// prototype database still have to be dropped +/// ([`forget_prototype_validators`]). +fn validators_marker(db_path: &Path) -> PathBuf { + let mut name = db_path + .file_name() + .map(std::ffi::OsStr::to_os_string) + .unwrap_or_default(); + name.push(".full-check-pending"); + db_path.with_file_name(name) +} + +/// Drops the HTTP validators imported from a prototype database. Returns +/// whether every podcast was handled (otherwise it runs again at the next +/// start). +/// +/// The prototype stored no seasons, episode numbers or episode types, and +/// its GUID-less episodes stay provisional until a feed is parsed again. With +/// the imported `ETag` / `Last-Modified` the first check would be answered +/// "304 Not Modified" and the feed never parsed while it does not change, so +/// a finished series would never show its seasons. Without them the first +/// check downloads and applies the feed. The fetch state is local (never +/// synced). +/// +/// Only the public fetch-state API exists for this: the validators are +/// cleared by recording a success at the last check, keeping the next check +/// time, and an imported error is recorded again as many times as it was +/// counted, so the back-off stays as imported. The one side effect is that a +/// podcast imported with an error gets a last success time (its last check), +/// which is not shown while the error is. Running this again only costs one +/// unconditional feed download per podcast. Contract gap: FRID's prototype +/// import should not carry the validators over. +fn forget_prototype_validators(store: &PodcastStore) -> bool { + let result = (|| -> Result { + let mut cleared = 0; + for podcast in store.list_podcasts(PodcastListFilter::All)? { + let id = &podcast.podcast_id; + let state = store.fetch_state(id)?; + if state.etag.is_none() && state.last_modified.is_none() { + continue; + } + let checked = state + .last_success_at_ms + .or(state.checked_at_ms) + .unwrap_or(0); + store.record_fetch_success(id, checked, None, None, state.next_check_at_ms)?; + if let Some(error) = &state.last_error { + for _ in 0..state.consecutive_failures.max(1) { + store.record_fetch_failure( + id, + state.checked_at_ms.unwrap_or(checked), + error, + state.next_check_at_ms, + )?; + } + } + cleared += 1; + } + Ok(cleared) + })(); + match result { + Ok(cleared) => { + if cleared > 0 { + tracing::info!( + cleared, + "imported podcasts get a full first feed check (prototype validators dropped)" + ); + } + true + } + Err(err) => { + tracing::warn!( + "dropping imported podcast validators failed (retried at the next start): {err:#}" + ); + false + } + } +} + +/// A readable reason why the store did not open; the typed store errors +/// already say that the file was left unchanged. +pub fn describe_open_error(err: &anyhow::Error) -> String { + match err.downcast_ref::() { + Some(store_error) => store_error.to_string(), + None => format!("{err:#}"), + } +} + +/// Runs blocking store or filesystem work off the Tokio workers. +pub(crate) async fn blocking(work: F) -> Result +where + T: Send + 'static, + F: FnOnce() -> Result + Send + 'static, +{ + tokio::task::spawn_blocking(work) + .await + .map_err(|err| anyhow!("background task failed: {err}"))? +} diff --git a/src/podcasts/playback.rs b/src/podcasts/playback.rs new file mode 100644 index 0000000..9c4b61b --- /dev/null +++ b/src/podcasts/playback.rs @@ -0,0 +1,291 @@ +//! Preparing an episode for playback: resolve its current audio URL and +//! resume position from the store, open the HTTP stream, decode it and +//! position it, all off the UI and audio threads. +//! +//! The queue item ([`PodcastEpisodeRef`]) carries the URL that was current +//! when the episode was queued (or received); the store is asked again at +//! play time, so a feed refresh that changed the episode's audio URL is +//! picked up. When the publisher answers a stale URL with a client error, +//! the feed is refreshed once and the episode retried with the address the +//! feed now lists for the same episode id. + +use std::sync::Arc; +use std::time::Duration; + +use anyhow::{Context as _, Result, anyhow}; +use furumi_library::podcasts::PodcastStore; +use music_dht::podcast::{PodcastEpisodeRef, reaches_completion}; +use rodio::Source as _; + +use super::blocking; +use super::refresh::{self, Services}; +use super::stream::{self, CancelToken, Cancelled, StreamEnd, StreamProbe, StreamStatusError}; +use crate::player::{DecodedSource, decode_source_with}; + +/// Where playback of an episode starts. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StartAt { + /// Ordinary Play: the saved resume position (the beginning for a new + /// or completed episode). + Resume, + /// An exact position (handoff, restarting the same item). + Position { ms: i64 }, + /// "Start over": from the beginning, recorded as an explicit restart. + Restart, +} + +/// An episode ready to hand to the audio thread. +pub struct PreparedEpisode { + /// The episode as the store knows it now (current URL and duration), or + /// the queued reference when the store does not know it. + pub episode: PodcastEpisodeRef, + pub source: DecodedSource, + /// Position the source was moved to. + pub start_ms: i64, + /// Duration from the decoder, else the one the feed announced. + pub duration_ms: Option, + /// `duration_ms` was measured by the decoder (container header, frame + /// count or file size); otherwise it is only the feed's announcement, + /// which is often off (rounded `itunes:duration`, inserted ads). + pub duration_measured: bool, + /// The server allows seeking (it sent the file size). + pub seekable: bool, + /// How the stream ended, once it did. + pub probe: StreamProbe, +} + +impl std::fmt::Debug for PreparedEpisode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PreparedEpisode") + .field("episode", &self.episode.episode_id) + .field("start_ms", &self.start_ms) + .field("duration_ms", &self.duration_ms) + .finish_non_exhaustive() + } +} + +/// The episode cannot start where it should, so it does not start at all: +/// the saved position is kept and the user is told why. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CannotResume { + pub position_ms: i64, +} + +impl std::fmt::Display for CannotResume { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "the server does not allow seeking in this episode, so it cannot continue at {}; \ + the saved position is kept (use \"start over\" to play it from the beginning)", + format_position(self.position_ms) + ) + } +} + +impl std::error::Error for CannotResume {} + +fn format_position(ms: i64) -> String { + let secs = ms.max(0) / 1_000; + let (hours, minutes, seconds) = (secs / 3_600, secs / 60 % 60, secs % 60); + if hours > 0 { + format!("{hours}:{minutes:02}:{seconds:02}") + } else { + format!("{minutes}:{seconds:02}") + } +} + +/// The episode reference and start position to use, from the store when it +/// knows the episode. Blocking. +pub fn resolve( + store: Option<&PodcastStore>, + queued: &PodcastEpisodeRef, + start: StartAt, +) -> Result<(PodcastEpisodeRef, i64)> { + let current = match store { + Some(store) => store.episode_ref(&queued.episode_id)?, + None => None, + }; + let mut episode = current.unwrap_or_else(|| queued.clone()); + // Keep the greatest generation either side has seen (handoff). + episode.progress_generation = episode.progress_generation.max(queued.progress_generation); + let start_ms = match start { + StartAt::Resume => match store { + Some(store) => store.resume_position_ms(&queued.episode_id)?, + None => 0, + }, + StartAt::Position { ms } => ms.max(0), + StartAt::Restart => 0, + }; + Ok((episode, start_ms)) +} + +/// Share of a feed-announced duration (percent) that a stream of unknown +/// size must have played before its clean close counts as its end. +const FEED_DURATION_END_PERCENT: i64 = 80; + +/// Whether the end of an episode's stream means it was played to the end: +/// +/// * a stream that ended where the server said it would is the end; +/// * a stream that failed (connection lost, URL expired, outage) only +/// counts when it failed within the shared completion window; +/// * a stream of unknown size that closed cleanly (where an early close +/// looks like the end) counts when the position is within the completion +/// window of a duration the decoder measured. A duration that only the +/// feed announced is a hint: then reaching [`FEED_DURATION_END_PERCENT`] +/// of it is enough, and with no duration at all the close is the end. +pub fn played_to_the_end( + end: StreamEnd, + position_ms: i64, + duration_ms: Option, + duration_measured: bool, +) -> bool { + let near_end = duration_ms.is_some() && reaches_completion(position_ms, duration_ms); + match end { + StreamEnd::Complete => true, + StreamEnd::Failed => near_end, + StreamEnd::Unknown => match duration_ms { + None => true, + Some(_) if duration_measured => near_end, + Some(ms) => { + near_end + || position_ms.saturating_mul(100) + >= ms.saturating_mul(FEED_DURATION_END_PERCENT) + } + }, + } +} + +/// Opens, decodes and positions the audio of `episode`. Blocking (network). +/// Stops with [`Cancelled`] once `cancel` fires: a superseded resume does +/// not keep downloading up to its offset. +pub fn open_at( + episode: &PodcastEpisodeRef, + start_ms: i64, + cancel: &CancelToken, +) -> Result { + let opened = stream::open_with(&episode.audio_url, cancel.clone())?; + let seekable = opened.seekable; + let probe = opened.probe.clone(); + // Coarse seeking maps a time to a byte offset (one Range request) + // instead of decoding everything before it. + let coarse_seek = seekable && opened.byte_len.is_some(); + let mime = opened.mime_type.clone().or_else(|| episode.mime.clone()); + let mut source = decode_source_with( + Box::new(opened.reader), + opened.byte_len, + mime.as_deref(), + seekable, + coarse_seek, + ) + .map_err(|err| { + if cancel.is_cancelled() { + anyhow!(Cancelled) + } else { + anyhow!("cannot decode the episode audio: {err}") + } + })?; + if start_ms > 0 { + if !seekable { + return Err(CannotResume { + position_ms: start_ms, + } + .into()); + } + let target = Duration::from_millis(start_ms as u64); + if let Err(err) = source.try_seek(target) { + if cancel.is_cancelled() { + return Err(Cancelled.into()); + } + tracing::warn!(%err, "seeking a podcast stream failed"); + return Err(CannotResume { + position_ms: start_ms, + } + .into()); + } + } + let measured = source + .total_duration() + .and_then(|duration| i64::try_from(duration.as_millis()).ok()) + .filter(|ms| *ms > 0); + Ok(PreparedEpisode { + episode: episode.clone(), + source, + start_ms, + duration_ms: measured.or(episode.duration_ms), + duration_measured: measured.is_some(), + seekable, + probe, + }) +} + +/// [`resolve`] + [`open_at`], refreshing the feed once when the audio URL +/// answers with a client error (moved or expired file). +pub async fn prepare( + services: Option, + queued: PodcastEpisodeRef, + start: StartAt, + now_ms: i64, + cancel: CancelToken, +) -> Result { + let store = services + .as_ref() + .map(|services| Arc::clone(&services.store)); + let lookup = queued.clone(); + let (episode, start_ms) = blocking(move || resolve(store.as_deref(), &lookup, start)).await?; + let attempt = episode.clone(); + let attempt_cancel = cancel.clone(); + let error = match blocking(move || open_at(&attempt, start_ms, &attempt_cancel)).await { + Ok(prepared) => return Ok(prepared), + Err(error) => error, + }; + if cancel.is_cancelled() { + return Err(Cancelled.into()); + } + let stale = error + .downcast_ref::() + .is_some_and(|status| status.url_is_stale()); + let Some(services) = services.filter(|_| stale) else { + return Err(error); + }; + let store = Arc::clone(&services.store); + let podcast_id = episode.podcast_id.clone(); + let feed_url = blocking(move || { + Ok(store + .podcast(&podcast_id)? + .map(|summary| summary.meta.feed_url)) + }) + .await?; + let Some(feed_url) = feed_url else { + return Err(error); + }; + tracing::info!( + episode = %episode.episode_id, + "episode audio unavailable; checking the feed for a new address" + ); + if let Err(err) = refresh::refresh(&services, &episode.podcast_id, &feed_url, now_ms).await { + tracing::warn!("refreshing the feed of an unavailable episode failed: {err:#}"); + return Err(error); + } + let store = Arc::clone(&services.store); + let lookup = queued.clone(); + let (fresh, _) = blocking(move || resolve(Some(&store), &lookup, StartAt::Restart)).await?; + if fresh.audio_url == episode.audio_url { + return Err(error).context("the feed lists the same audio address"); + } + if cancel.is_cancelled() { + return Err(Cancelled.into()); + } + blocking(move || open_at(&fresh, start_ms, &cancel)).await +} + +/// Message for a failed start; the URL never appears (it may carry a +/// private token). +pub fn describe_failure(err: &anyhow::Error) -> String { + if err.downcast_ref::().is_some() { + return Cancelled.to_string(); + } + if let Some(cannot) = err.downcast_ref::() { + return cannot.to_string(); + } + format!("{err:#}") +} diff --git a/src/podcasts/playback_tests.rs b/src/podcasts/playback_tests.rs new file mode 100644 index 0000000..36a9a17 --- /dev/null +++ b/src/podcasts/playback_tests.rs @@ -0,0 +1,836 @@ +//! Episode playback preparation and durable progress, against a local HTTP +//! server and a temp-dir store (no internet, no audio device). + +use std::sync::PoisonError; +use std::sync::mpsc::{Receiver, RecvTimeoutError}; +use std::time::Duration; + +use furumi_library::podcasts::{EpisodeQuery, PodcastStore}; +use music_dht::podcast::{ + EpisodeId, PodcastEpisodeRef, PodcastId, ProgressStatus, reaches_completion, +}; + +use super::playback::{self, CannotResume, StartAt}; +use super::progress::{ProgressCommand, ProgressEvent, ProgressWriter}; +use super::refresh::{Services, refresh, subscribe}; +use super::stream::{CancelToken, StreamEnd}; +use super::test_server::{Route, WAV_RATE, wav}; +use super::tests::{Fixture, NOW, fixture, rss}; + +const MINUTE_MS: i64 = 60_000; + +/// Subscribes the fixture to a feed with episodes `a` and `b` and returns +/// the podcast and the reference of episode `a`. +async fn subscribed(f: &Fixture) -> (PodcastId, PodcastEpisodeRef) { + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ), + ); + let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW) + .await + .unwrap(); + let episode = episode_ref(&f.services.store, &report.podcast_id, "Pilot"); + (report.podcast_id, episode) +} + +fn episode_ref(store: &PodcastStore, podcast_id: &PodcastId, title: &str) -> PodcastEpisodeRef { + let item = store + .episodes(&EpisodeQuery::new(podcast_id.clone())) + .unwrap() + .into_iter() + .find(|item| item.episode.title == title) + .expect("episode in the catalog"); + store + .episode_ref(&item.episode.episode_id) + .unwrap() + .unwrap() +} + +/// A progress writer whose events can be awaited. +struct Writer { + writer: ProgressWriter, + events: Receiver, +} + +impl Writer { + fn spawn(services: &Services) -> Writer { + let (tx, events) = std::sync::mpsc::channel(); + let tx = std::sync::Mutex::new(tx); + let writer = ProgressWriter::spawn(services.clone(), move |event| { + let _ = tx + .lock() + .unwrap_or_else(PoisonError::into_inner) + .send(event); + }) + .unwrap(); + Writer { writer, events } + } + + fn send(&self, command: ProgressCommand) { + self.writer.send(command); + } + + /// The next event (the writer handles commands in order). + fn next(&self) -> ProgressEvent { + match self.events.recv_timeout(Duration::from_secs(10)) { + Ok(event) => event, + Err(RecvTimeoutError::Timeout) => panic!("no progress event"), + Err(RecvTimeoutError::Disconnected) => panic!("writer stopped"), + } + } + + fn saved(&self) -> music_dht::podcast::ProgressRecord { + match self.next() { + ProgressEvent::Saved(record) => record, + other => panic!("expected a save, got {other:?}"), + } + } + + fn close(mut self) { + assert!(self.writer.close(Duration::from_secs(10))); + } +} + +fn begin( + playback: u64, + episode: &PodcastEpisodeRef, + position_ms: i64, + restart: bool, +) -> ProgressCommand { + ProgressCommand::Begin { + playback, + episode: Box::new(episode.clone()), + session_id: format!("session-{playback}"), + position_ms, + restart, + now_ms: NOW, + } +} + +fn resume(store: &PodcastStore, id: &EpisodeId) -> (i64, ProgressStatus) { + let progress = store.progress(id).unwrap().expect("progress"); + (store.resume_position_ms(id).unwrap(), progress.status) +} + +#[tokio::test] +async fn resume_position_survives_restarts_and_late_checkpoints_lose() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + let store = &f.services.store; + let id = &episode.episode_id; + + // First run: play, save periodically, quit. + let first = Writer::spawn(&f.services); + first.send(begin(1, &episode, 0, false)); + let started = first.saved(); + assert_eq!(started.status, ProgressStatus::InProgress); + first.send(ProgressCommand::Checkpoint { + playback: 1, + position_ms: 10 * MINUTE_MS, + }); + assert_eq!( + first.saved().generation, + started.generation, + "checkpoints keep it" + ); + assert_eq!( + resume(store, id), + (10 * MINUTE_MS, ProgressStatus::InProgress) + ); + + // "Start over" in a later run takes a newer generation… + let second = Writer::spawn(&f.services); + second.send(begin(2, &episode, 0, true)); + let restarted = second.saved(); + assert!(restarted.generation > started.generation); + assert_eq!(resume(store, id), (0, ProgressStatus::InProgress)); + + // …so a late checkpoint of the old session loses, whatever its clock. + first.send(ProgressCommand::Checkpoint { + playback: 1, + position_ms: 20 * MINUTE_MS, + }); + match first.next() { + ProgressEvent::Superseded { current } => assert_eq!(current.position_ms, 0), + other => panic!("expected supersede, got {other:?}"), + } + assert_eq!(resume(store, id), (0, ProgressStatus::InProgress)); + // The old session no longer writes, not even when it ends. + first.send(ProgressCommand::End { + playback: 1, + position_ms: 21 * MINUTE_MS, + listened_ms: 0, + now_ms: NOW + 1, + }); + assert!(matches!(first.next(), ProgressEvent::Ended { .. })); + first.close(); + assert_eq!(resume(store, id), (0, ProgressStatus::InProgress)); + + // The new session saves normally, including a seek backwards. + second.send(ProgressCommand::Checkpoint { + playback: 2, + position_ms: 5 * MINUTE_MS, + }); + second.saved(); + second.send(ProgressCommand::Seek { + playback: 2, + position_ms: MINUTE_MS, + }); + let seeked = second.saved(); + assert!( + seeked.generation > restarted.generation, + "a seek is explicit" + ); + assert_eq!(resume(store, id), (MINUTE_MS, ProgressStatus::InProgress)); + second.send(ProgressCommand::End { + playback: 2, + position_ms: MINUTE_MS + 2_000, + listened_ms: 2_000, + now_ms: NOW + 2, + }); + second.saved(); + assert!(matches!(second.next(), ProgressEvent::Ended { .. })); + second.close(); + assert_eq!( + resume(store, id), + (MINUTE_MS + 2_000, ProgressStatus::InProgress) + ); + // The sync log carries every write, in order. + let kinds = f.log.kinds(); + assert_eq!(kinds[0], "podcast_subscription_set"); + assert!( + kinds[1..].iter().all(|kind| kind == "podcast_progress_set"), + "{kinds:?}" + ); +} + +#[tokio::test] +async fn completion_counts_once_and_short_sessions_stay_out_of_history() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + let store = &f.services.store; + let id = &episode.episode_id; + let duration = episode.duration_ms.expect("the feed has a duration"); + assert_eq!(duration, 45 * MINUTE_MS); + let near_end = duration - 20_000; + assert!(reaches_completion(near_end, Some(duration))); + + // A short listen: progress is saved, history is not. + let writer = Writer::spawn(&f.services); + writer.send(begin(1, &episode, 0, false)); + writer.saved(); + writer.send(ProgressCommand::End { + playback: 1, + position_ms: 5_000, + listened_ms: 5_000, + now_ms: NOW + 5_000, + }); + writer.saved(); + assert!(matches!(writer.next(), ProgressEvent::Ended { .. })); + assert!(store.listen_history(None, 10).unwrap().is_empty()); + + // A full listen completes the episode once, even if the end is + // reported twice and the session ends afterwards. + writer.send(begin(2, &episode, 5_000, false)); + writer.saved(); + for _ in 0..2 { + writer.send(ProgressCommand::Complete { + playback: 2, + position_ms: near_end, + listened_ms: near_end - 5_000, + now_ms: NOW + duration, + }); + } + let completed = writer.saved(); + assert_eq!(completed.status, ProgressStatus::Completed); + writer.send(ProgressCommand::End { + playback: 2, + position_ms: duration, + listened_ms: duration - 5_000, + now_ms: NOW + duration + 20_000, + }); + assert!(matches!(writer.next(), ProgressEvent::Ended { .. })); + writer.close(); + assert_eq!( + resume(store, id), + (0, ProgressStatus::Completed), + "Play starts over" + ); + assert_eq!(store.completed_listen_count(id).unwrap(), 1); + let history = store.listen_history(None, 10).unwrap(); + assert_eq!(history.len(), 1); + assert!(history[0].session.completed); + assert_eq!(history[0].session.listened_ms, duration - 5_000); +} + +#[tokio::test] +async fn marking_played_while_playing_stops_checkpoints_until_the_next_seek() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + let store = &f.services.store; + let id = &episode.episode_id; + let writer = Writer::spawn(&f.services); + writer.send(begin(1, &episode, 0, false)); + writer.saved(); + writer.send(ProgressCommand::SetPlayed { + episode_id: id.clone(), + played: true, + }); + assert_eq!(writer.saved().status, ProgressStatus::Completed); + assert!(matches!( + writer.next(), + ProgressEvent::PlayedSet { + played: true, + result: Ok(()) + } + )); + writer.send(ProgressCommand::Checkpoint { + playback: 1, + position_ms: 90_000, + }); + assert!(matches!(writer.next(), ProgressEvent::Superseded { .. })); + assert_eq!(resume(store, id).1, ProgressStatus::Completed); + // Seeking is the user's explicit choice to keep listening here. + writer.send(ProgressCommand::Seek { + playback: 1, + position_ms: 30_000, + }); + assert_eq!(writer.saved().status, ProgressStatus::InProgress); + assert_eq!(resume(store, id), (30_000, ProgressStatus::InProgress)); + writer.close(); +} + +/// Serves a position-coded WAV for episode `a` and returns its seconds. +fn serve_audio(f: &Fixture, route: impl FnOnce(Route) -> Route, seconds: u32) -> u32 { + f.server + .set("/audio/a.mp3", route(Route::new(wav(seconds), "audio/wav"))); + seconds +} + +/// Seconds marker of the next decoded sample (see `test_server::wav`). +fn second_marker(source: &mut crate::player::DecodedSource) -> i64 { + let sample = source.next().expect("a sample"); + (sample * 32_768.0 / 40.0).round() as i64 +} + +/// The source plays from `second`. A seek may land up to one decoder packet +/// early (1024 frames, 0.128 s at 8 kHz) in the previous second. +fn assert_starts_at(source: &mut crate::player::DecodedSource, second: i64) { + let marker = second_marker(source); + assert!( + marker == second || marker == second - 1, + "starts in second {marker}, expected {second}" + ); + // A quarter of a second later it is exactly there. + let marker = (0..WAV_RATE / 4) + .map(|_| second_marker(source)) + .last() + .unwrap(); + assert_eq!(marker, second); +} + +async fn save_position(services: &Services, episode: &PodcastEpisodeRef, position_ms: i64) { + let writer = Writer::spawn(services); + writer.send(begin(99, episode, position_ms, false)); + writer.saved(); + writer.close(); +} + +#[tokio::test] +async fn play_resumes_at_the_saved_position_with_a_range_request() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + let seconds = serve_audio(&f, |route| route, 12 * 60); + save_position(&f.services, &episode, 11 * MINUTE_MS).await; + + let prepared = playback::prepare( + Some(f.services.clone()), + episode.clone(), + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert_eq!(prepared.start_ms, 11 * MINUTE_MS); + assert_eq!(prepared.duration_ms, Some(i64::from(seconds) * 1_000)); + let mut source = prepared.source; + assert_starts_at(&mut source, 11 * 60); + // The far seek became a ranged request instead of a full download. + let byte = 11 * 60 * WAV_RATE as usize * 2; + assert!( + f.server.ranges("/audio/a.mp3").iter().any(|range| range + .as_deref() + .and_then(|range| range.strip_prefix("bytes=")) + .and_then(|range| range.trim_end_matches('-').parse::().ok()) + .is_some_and(|start| start + 4_096 >= byte && start <= byte + 4_096)), + "{:?}", + f.server.ranges("/audio/a.mp3") + ); + + // "Start over" ignores the saved position. + let restarted = playback::prepare( + Some(f.services.clone()), + episode, + StartAt::Restart, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert_eq!(restarted.start_ms, 0); + let mut source = restarted.source; + assert_eq!(second_marker(&mut source), 0); +} + +#[tokio::test] +async fn a_server_without_range_support_still_resumes_at_the_right_place() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + serve_audio(&f, Route::no_ranges, 30); + save_position(&f.services, &episode, 20_000).await; + let prepared = playback::prepare( + Some(f.services.clone()), + episode, + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert_eq!(prepared.start_ms, 20_000); + let mut source = prepared.source; + assert_starts_at(&mut source, 20); + assert!( + f.server + .ranges("/audio/a.mp3") + .iter() + .skip(1) + .all(Option::is_none), + "no ranges are sent to a server that ignores them" + ); +} + +#[tokio::test] +async fn an_unseekable_stream_does_not_pretend_to_resume() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + serve_audio(&f, |route| route.no_ranges().no_length(), 30); + save_position(&f.services, &episode, 20_000).await; + let err = playback::prepare( + Some(f.services.clone()), + episode.clone(), + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .expect_err("cannot resume"); + assert_eq!( + err.downcast_ref::(), + Some(&CannotResume { + position_ms: 20_000 + }) + ); + assert!(playback::describe_failure(&err).contains("0:20")); + // The saved position is untouched, and "start over" still plays. + let store = &f.services.store; + assert_eq!( + store.resume_position_ms(&episode.episode_id).unwrap(), + 20_000 + ); + let prepared = playback::prepare( + Some(f.services.clone()), + episode, + StartAt::Restart, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert_eq!(prepared.start_ms, 0); +} + +#[tokio::test] +async fn the_audio_url_comes_from_the_feed_at_play_time() { + let f = fixture(); + let (podcast_id, queued) = subscribed(&f).await; + // The publisher moved the file and the feed already says so. + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]).pipe_replace("/audio/a.mp3", "/audio/a-v2.mp3"), + "application/rss+xml", + ), + ); + let url = f + .services + .store + .podcast(&podcast_id) + .unwrap() + .unwrap() + .meta + .feed_url; + refresh(&f.services, &podcast_id, &url, NOW + 1) + .await + .unwrap(); + f.server + .set("/audio/a-v2.mp3", Route::new(wav(5), "audio/wav")); + // The queue item still has the old URL; the store's is used. + assert!(queued.audio_url.ends_with("/audio/a.mp3")); + let prepared = playback::prepare( + Some(f.services.clone()), + queued.clone(), + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert!(prepared.episode.audio_url.ends_with("/audio/a-v2.mp3")); + assert!(f.server.requests("/audio/a.mp3").is_empty()); + assert_eq!( + prepared.episode.episode_id, queued.episode_id, + "same episode" + ); +} + +#[tokio::test] +async fn a_stale_audio_url_refreshes_the_feed_once_and_retries() { + let f = fixture(); + let (_, queued) = subscribed(&f).await; + // The old file is gone (404) and only the feed knows the new one. + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]).pipe_replace("/audio/a.mp3", "/audio/a-v3.mp3"), + "application/rss+xml", + ), + ); + f.server + .set("/audio/a-v3.mp3", Route::new(wav(5), "audio/wav")); + let prepared = playback::prepare( + Some(f.services.clone()), + queued.clone(), + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + assert!(prepared.episode.audio_url.ends_with("/audio/a-v3.mp3")); + assert_eq!(f.server.requests("/audio/a.mp3").len(), 1); + assert_eq!( + f.server.requests("/feed.xml").len(), + 2, + "one extra feed check" + ); + + // Without a newer address the original error is reported, once. + f.server.set( + "/audio/a-v3.mp3", + Route::new("gone", "text/plain").status(410), + ); + let err = playback::prepare( + Some(f.services.clone()), + queued, + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .expect_err("still unavailable"); + assert!(format!("{err:#}").contains("410"), "{err:#}"); + assert_eq!(f.server.requests("/feed.xml").len(), 3); +} + +#[tokio::test] +async fn without_a_store_the_queued_reference_plays_from_the_start() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + serve_audio(&f, |route| route, 5); + save_position(&f.services, &episode, 3_000).await; + let prepared = playback::prepare(None, episode, StartAt::Resume, NOW, CancelToken::never()) + .await + .unwrap(); + assert_eq!(prepared.start_ms, 0); +} + +trait PipeReplace { + fn pipe_replace(self, from: &str, to: &str) -> Vec; +} + +impl PipeReplace for Vec { + fn pipe_replace(self, from: &str, to: &str) -> Vec { + String::from_utf8(self) + .unwrap() + .replace(from, to) + .into_bytes() + } +} + +/// Decodes `source` to its end, as the audio thread would; returns the +/// position reached (mono samples of `test_server::wav`). +fn play_out(source: &mut crate::player::DecodedSource) -> i64 { + let mut samples = 0i64; + while source.next().is_some() { + samples += 1; + } + samples * 1_000 / i64::from(WAV_RATE) +} + +/// Byte offset of second `second` in `test_server::wav`. +fn wav_offset(second: u32) -> usize { + 44 + (second * WAV_RATE * 2) as usize +} + +#[tokio::test] +async fn a_lost_connection_does_not_complete_the_episode_and_keeps_its_position() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + // Two minutes of audio; the server stops sending after 30 s for good. + serve_audio(&f, |route| route.cut(wav_offset(30), usize::MAX), 120); + let prepared = playback::prepare( + Some(f.services.clone()), + episode.clone(), + StartAt::Resume, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + let writer = Writer::spawn(&f.services); + writer.send(begin(1, &episode, 0, false)); + writer.saved(); + + let mut source = prepared.source; + // The decoder ends once the stalled read gives up (STARVE_TIMEOUT). + let position_ms = play_out(&mut source); + assert!( + (29_000..=30_500).contains(&position_ms), + "stopped at {position_ms} ms" + ); + assert_eq!(prepared.probe.end(), StreamEnd::Failed); + assert_eq!(prepared.duration_ms, Some(120_000)); + assert!(!playback::played_to_the_end( + prepared.probe.end(), + position_ms, + prepared.duration_ms, + prepared.duration_measured + )); + + // What the runtime then saves: the last position, not a completion. + writer.send(ProgressCommand::End { + playback: 1, + position_ms, + listened_ms: position_ms, + now_ms: NOW + 1, + }); + while !matches!(writer.next(), ProgressEvent::Ended { .. }) {} + writer.close(); + let store = &f.services.store; + assert_eq!( + resume(store, &episode.episode_id), + (position_ms, ProgressStatus::InProgress) + ); + assert_eq!( + store.completed_listen_count(&episode.episode_id).unwrap(), + 0 + ); +} + +#[tokio::test] +async fn a_stream_of_unknown_length_that_closes_early_is_not_the_end() { + let f = fixture(); + let (_, episode) = subscribed(&f).await; + serve_audio( + &f, + |route| route.no_length().cut(wav_offset(30), usize::MAX), + 120, + ); + let prepared = playback::prepare( + Some(f.services.clone()), + episode.clone(), + StartAt::Restart, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + let mut source = prepared.source; + let position_ms = play_out(&mut source); + assert!((29_000..=30_500).contains(&position_ms), "{position_ms} ms"); + // A closed connection looks like the end of the file here... + assert_eq!(prepared.probe.end(), StreamEnd::Unknown); + // ...but the duration the decoder read from the header says otherwise. + assert_eq!(prepared.duration_ms, Some(120_000)); + assert!(prepared.duration_measured); + assert!(!playback::played_to_the_end( + StreamEnd::Unknown, + position_ms, + Some(120_000), + true + )); + assert!(playback::played_to_the_end( + StreamEnd::Unknown, + 119_000, + Some(120_000), + true + )); + // A duration only the feed announced is a hint: an audio file that ends + // more than the completion window before it still ends there... + assert!(playback::played_to_the_end( + StreamEnd::Unknown, + 100_000, + Some(120_000), + false + )); + // ...while a close at a quarter of it is still a lost connection. + assert!(!playback::played_to_the_end( + StreamEnd::Unknown, + position_ms, + Some(120_000), + false + )); + + // A whole file with a size is played to its end. + serve_audio(&f, |route| route, 3); + let prepared = playback::prepare( + Some(f.services.clone()), + episode, + StartAt::Restart, + NOW, + CancelToken::never(), + ) + .await + .unwrap(); + let mut source = prepared.source; + assert_eq!(play_out(&mut source), 3_000); + assert_eq!(prepared.probe.end(), StreamEnd::Complete); + assert!(playback::played_to_the_end( + StreamEnd::Complete, + 3_000, + prepared.duration_ms, + prepared.duration_measured + )); + // A failure in the last seconds still counts as the end, whatever + // announced the duration; earlier it never does. + for measured in [true, false] { + assert!(playback::played_to_the_end( + StreamEnd::Failed, + 119_500, + Some(120_000), + measured + )); + assert!(!playback::played_to_the_end( + StreamEnd::Failed, + 100_000, + Some(120_000), + measured + )); + } +} + +/// About `seconds` of silent MPEG-1 Layer III (mono, 44.1 kHz, 128 kbit/s) +/// frames with no Xing/Info header: sent without a size, the decoder +/// cannot measure its duration. +fn mp3_without_header(seconds: u32) -> Vec { + const FRAME_LEN: usize = 417; // 144 * 128_000 / 44_100 + let frames = (u64::from(seconds) * 44_100 / 1_152) as usize; + let mut frame = vec![0u8; FRAME_LEN]; + frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0xC0]); + frame.repeat(frames) +} + +/// Decodes `source` to its end; returns the position reached. +fn play_out_any(source: &mut crate::player::DecodedSource) -> i64 { + let per_second = i64::from(source.sample_rate().get()) * i64::from(source.channels().get()); + let mut samples = 0i64; + while source.next().is_some() { + samples += 1; + } + samples * 1_000 / per_second +} + +/// Without a size from the server and a duration the decoder could +/// measure, only the feed's duration is known, and it may be longer than +/// the audio by more than the completion window. The clean end of the +/// stream then still completes the episode. +#[tokio::test] +async fn a_stream_shorter_than_the_feed_says_completes_at_its_end() { + let f = fixture(); + let (_, mut episode) = subscribed(&f).await; + f.server.set( + "/audio/a.mp3", + Route::new(mp3_without_header(100), "audio/mpeg").no_length(), + ); + // The feed announces two minutes, 20 s more than the audio. + episode.duration_ms = Some(120_000); + let prepared = + tokio::task::spawn_blocking(move || playback::open_at(&episode, 0, &CancelToken::never())) + .await + .unwrap() + .unwrap(); + assert!(!prepared.seekable); + assert!(!prepared.duration_measured, "{:?}", prepared.duration_ms); + assert_eq!(prepared.duration_ms, Some(120_000)); + let mut source = prepared.source; + let position_ms = play_out_any(&mut source); + assert!( + (99_000..=100_100).contains(&position_ms), + "{position_ms} ms" + ); + assert_eq!(prepared.probe.end(), StreamEnd::Unknown); + assert!(!reaches_completion(position_ms, Some(120_000))); + assert!(playback::played_to_the_end( + prepared.probe.end(), + position_ms, + prepared.duration_ms, + prepared.duration_measured + )); +} + +#[test] +fn a_superseded_resume_stops_reading_up_to_its_offset() { + let f = fixture(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let (_, episode) = rt.block_on(subscribed(&f)); + // No Range support: resuming at 60 s means reading and discarding + // everything before it (while positioning, or on the first reads after + // it, depending on the format), and this server stalls after 10 s. + serve_audio( + &f, + |route| route.no_ranges().cut(wav_offset(10), usize::MAX), + 120, + ); + let superseded = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let flag = std::sync::Arc::clone(&superseded); + let cancel = CancelToken::new(move || flag.load(std::sync::atomic::Ordering::SeqCst)); + let started = std::time::Instant::now(); + let starting = std::thread::spawn(move || { + let prepared = playback::open_at(&episode, 60_000, &cancel)?; + let mut source = prepared.source; + Ok::<_, anyhow::Error>((source.next(), prepared.probe)) + }); + std::thread::sleep(Duration::from_millis(300)); + superseded.store(true, std::sync::atomic::Ordering::SeqCst); + match starting.join().unwrap() { + Err(err) => assert!( + err.downcast_ref::().is_some(), + "{err:#}" + ), + Ok((sample, probe)) => { + assert_eq!(sample, None, "nothing plays after the cancel"); + assert_eq!(probe.end(), StreamEnd::Failed); + } + } + // Well before a stalled read would give up by itself. + assert!( + started.elapsed() < super::stream::STARVE_TIMEOUT / 2, + "{:?}", + started.elapsed() + ); +} diff --git a/src/podcasts/progress.rs b/src/podcasts/progress.rs new file mode 100644 index 0000000..a9c94b3 --- /dev/null +++ b/src/podcasts/progress.rs @@ -0,0 +1,488 @@ +//! Durable episode progress: one background thread writes every progress +//! action of this device to the podcast store and the sync log, in order. +//! +//! The rules come from the FRID contract (`music_dht::podcast::state`, +//! `PodcastStore` progress actions); this module only decides *when* to +//! call them: +//! +//! * a playback start is an explicit action (`start_session`, or `restart` +//! for "start over") that takes a new progress generation; +//! * periodic checkpoints, pause and the final save keep the session's +//! generation (`checkpoint`), so a late checkpoint of an older session +//! always loses against a newer explicit action; +//! * a seek is explicit (`seek`) and becomes the session's generation; +//! * reaching the end (`reaches_completion` or the end of the stream) is +//! `complete_session`; +//! * when the store reports a newer generation (`Superseded`), the session +//! stops writing checkpoints until the user's next explicit action; +//! * the listening session itself (`ListenSession`) is recorded at pause, +//! completion and end; the store only keeps sessions that pass the shared +//! history threshold and only when they grew. +//! +//! "Mark played" / "Mark unplayed" go through the same thread so they are +//! ordered with the checkpoints of a playing episode. +//! +//! Writes never happen on the UI or audio thread, and a failed write is +//! reported and logged without stopping playback. + +use std::sync::Arc; +use std::sync::mpsc::{Receiver, Sender}; +use std::thread::JoinHandle; +use std::time::Duration; + +use anyhow::{Result, anyhow}; +use furumi_library::podcasts::{ + CheckpointOutcome, PodcastLocalOp, PodcastStore, ProgressSession, ProgressWrite, StampSource, +}; +use music_dht::podcast::{EpisodeId, ListenSession, PodcastEpisodeRef, ProgressRecord}; + +use super::OpLog; +use super::refresh::Services; + +/// How often a playing episode saves its position. +pub const CHECKPOINT_INTERVAL: Duration = Duration::from_secs(20); + +/// What the UI asks the writer to do. `playback` identifies one start of +/// one episode on this device; commands for a playback that is no longer +/// the active one are ignored (they can only arrive late). +#[derive(Debug, Clone)] +pub enum ProgressCommand { + /// Playback actually started at `position_ms`. + Begin { + playback: u64, + episode: Box, + session_id: String, + position_ms: i64, + /// "Start over" instead of an ordinary start or resume. + restart: bool, + now_ms: i64, + }, + /// Periodic save while playing. + Checkpoint { playback: u64, position_ms: i64 }, + /// Paused: save the position and the listening session so far. + Pause { + playback: u64, + position_ms: i64, + listened_ms: i64, + now_ms: i64, + }, + /// The user moved to `position_ms`. + Seek { playback: u64, position_ms: i64 }, + /// The position reached the end of the episode. + Complete { + playback: u64, + position_ms: i64, + listened_ms: i64, + now_ms: i64, + }, + /// Playback of this start ended (stop, next item, handoff, exit…). + End { + playback: u64, + position_ms: i64, + listened_ms: i64, + now_ms: i64, + }, + /// "Mark played" / "Mark unplayed" from the episode list. + SetPlayed { episode_id: EpisodeId, played: bool }, +} + +/// What the writer reports back. +#[derive(Debug, Clone, PartialEq)] +pub enum ProgressEvent { + /// The progress register of an episode was written. + Saved(ProgressRecord), + /// A newer explicit action holds the register; this playback stopped + /// saving its position. + Superseded { current: ProgressRecord }, + /// A playback ended (its session was recorded when long enough). + Ended { episode_id: EpisodeId }, + /// The result of a `SetPlayed` command. + PlayedSet { + played: bool, + result: Result<(), String>, + }, + /// A write failed; playback goes on. + Failed(String), +} + +/// Handle of the writer thread. +pub struct ProgressWriter { + tx: Option>, + thread: Option>, +} + +impl ProgressWriter { + pub fn spawn( + services: Services, + on_event: impl Fn(ProgressEvent) + Send + 'static, + ) -> Result { + let (tx, rx) = std::sync::mpsc::channel(); + let thread = std::thread::Builder::new() + .name("podcast-progress".to_string()) + .spawn(move || run(rx, &services.store, services.log.as_ref(), on_event)) + .map_err(|err| anyhow!("starting the podcast progress writer: {err}"))?; + Ok(ProgressWriter { + tx: Some(tx), + thread: Some(thread), + }) + } + + pub fn send(&self, command: ProgressCommand) { + if let Some(tx) = &self.tx + && tx.send(command).is_err() + { + tracing::warn!("podcast progress writer stopped; a progress update was lost"); + } + } + + /// Stops accepting commands and waits up to `timeout` for the queued + /// ones to be written. Blocking; used at exit. + pub fn close(&mut self, timeout: Duration) -> bool { + self.tx = None; + let Some(thread) = self.thread.take() else { + return true; + }; + let deadline = std::time::Instant::now() + timeout; + while !thread.is_finished() { + if std::time::Instant::now() >= deadline { + tracing::warn!("podcast progress writer did not finish before exit"); + return false; + } + std::thread::sleep(Duration::from_millis(10)); + } + let _ = thread.join(); + true + } +} + +/// The playback whose session the writer is maintaining. +struct Active { + playback: u64, + /// `None` when starting the session failed: nothing is written for it. + session: Option, + episode: PodcastEpisodeRef, + session_id: String, + started_at_ms: i64, + start_position_ms: i64, + /// A newer generation holds the register: no more checkpoints. + superseded: bool, + /// The register says this session completed the episode (a later seek + /// back makes it "in progress" again). + completed: bool, + /// The session reached the end at least once; it counts as a completed + /// listen whatever happens afterwards. + listen_completed: bool, +} + +fn run( + rx: Receiver, + store: &Arc, + log: &dyn OpLog, + on_event: impl Fn(ProgressEvent), +) { + let mut writer = Writer { + store, + log, + active: None, + on_event: &on_event, + }; + while let Ok(command) = rx.recv() { + writer.handle(command); + } +} + +struct Writer<'a, F: Fn(ProgressEvent)> { + store: &'a PodcastStore, + log: &'a dyn OpLog, + active: Option, + on_event: &'a F, +} + +/// Runs a store action through the sync log and returns its value. +fn record( + log: &dyn OpLog, + mut action: impl FnMut(&mut dyn StampSource) -> Result<(T, Vec)>, +) -> Result { + let mut value = None; + log.record(&mut |stamps| { + let (result, ops) = action(stamps)?; + value = Some(result); + Ok(ops) + })?; + value.ok_or_else(|| anyhow!("the podcast action did not run")) +} + +impl Writer<'_, F> { + fn emit(&self, event: ProgressEvent) { + (self.on_event)(event); + } + + fn fail(&self, what: &str, err: anyhow::Error) { + let message = format!("podcast progress: {what} failed: {err:#}"); + tracing::warn!("{message}"); + self.emit(ProgressEvent::Failed(message)); + } + + fn active_mut(&mut self, playback: u64) -> Option<&mut Active> { + self.active + .as_mut() + .filter(|active| active.playback == playback) + } + + fn handle(&mut self, command: ProgressCommand) { + match command { + ProgressCommand::Begin { + playback, + episode, + session_id, + position_ms, + restart, + now_ms, + } => self.begin(playback, *episode, session_id, position_ms, restart, now_ms), + ProgressCommand::Checkpoint { + playback, + position_ms, + } => self.checkpoint(playback, position_ms), + ProgressCommand::Pause { + playback, + position_ms, + listened_ms, + now_ms, + } => { + self.checkpoint(playback, position_ms); + self.record_session(playback, position_ms, listened_ms, now_ms); + } + ProgressCommand::Seek { + playback, + position_ms, + } => self.seek(playback, position_ms), + ProgressCommand::Complete { + playback, + position_ms, + listened_ms, + now_ms, + } => { + self.complete(playback, position_ms); + self.record_session(playback, position_ms, listened_ms, now_ms); + } + ProgressCommand::End { + playback, + position_ms, + listened_ms, + now_ms, + } => { + self.checkpoint(playback, position_ms); + self.record_session(playback, position_ms, listened_ms, now_ms); + if let Some(active) = self.active.take_if(|active| active.playback == playback) { + self.emit(ProgressEvent::Ended { + episode_id: active.episode.episode_id, + }); + } + } + ProgressCommand::SetPlayed { episode_id, played } => { + let result = self.set_played(&episode_id, played); + if let Err(err) = &result { + tracing::warn!("podcast progress: marking played failed: {err:#}"); + } + self.emit(ProgressEvent::PlayedSet { + played, + result: result.map_err(|err| format!("{err:#}")), + }); + } + } + } + + fn begin( + &mut self, + playback: u64, + episode: PodcastEpisodeRef, + session_id: String, + position_ms: i64, + restart: bool, + now_ms: i64, + ) { + let store = self.store; + let carried = episode.progress_generation; + let result = record(self.log, |stamps| { + let write = if restart { + store.restart(stamps, &episode, &session_id)? + } else { + store.start_session(stamps, &episode, &session_id, position_ms, carried)? + }; + let op = write.op.clone(); + Ok((write, vec![op])) + }); + let session = match result { + Ok(write) => { + let session = write.session.clone(); + self.emit(ProgressEvent::Saved(write.record)); + Some(session) + } + Err(err) => { + self.fail("starting a listening session", err); + None + } + }; + self.active = Some(Active { + playback, + session, + episode, + session_id, + started_at_ms: now_ms, + start_position_ms: position_ms, + superseded: false, + completed: false, + listen_completed: false, + }); + } + + fn checkpoint(&mut self, playback: u64, position_ms: i64) { + let store = self.store; + let log = self.log; + let Some(active) = self.active_mut(playback) else { + return; + }; + if active.superseded { + return; + } + let Some(session) = active.session.clone() else { + return; + }; + let result = record(log, |stamps| { + let outcome = store.checkpoint(stamps, &session, position_ms)?; + let ops = match &outcome { + CheckpointOutcome::Written(write) => vec![write.op.clone()], + _ => Vec::new(), + }; + Ok((outcome, ops)) + }); + match result { + Ok(CheckpointOutcome::Written(write)) => self.emit(ProgressEvent::Saved(write.record)), + Ok(CheckpointOutcome::Superseded { current }) => { + if let Some(active) = self.active_mut(playback) { + active.superseded = true; + } + tracing::info!( + episode = %current.episode_id, + "a newer progress action took over; this playback stops saving" + ); + self.emit(ProgressEvent::Superseded { current }); + } + Ok(CheckpointOutcome::AlreadyCompleted { .. }) => {} + Err(err) => self.fail("saving the position", err), + } + } + + /// Explicit actions of a running session (seek, completion): they take + /// a new generation and resume saving after a supersede. + fn explicit( + &mut self, + playback: u64, + what: &str, + action: impl Fn(&PodcastStore, &mut dyn StampSource, &ProgressSession) -> Result, + ) -> bool { + let store = self.store; + let log = self.log; + let Some(active) = self.active_mut(playback) else { + return false; + }; + let Some(session) = active.session.clone() else { + return false; + }; + let result = record(log, |stamps| { + let write = action(store, stamps, &session)?; + let op = write.op.clone(); + Ok((write, vec![op])) + }); + match result { + Ok(write) => { + if let Some(active) = self.active_mut(playback) { + active.session = Some(write.session.clone()); + active.superseded = false; + } + self.emit(ProgressEvent::Saved(write.record)); + true + } + Err(err) => { + self.fail(what, err); + false + } + } + } + + fn seek(&mut self, playback: u64, position_ms: i64) { + if self.explicit(playback, "saving a seek", |store, stamps, session| { + store.seek(stamps, session, position_ms) + }) && let Some(active) = self.active_mut(playback) + { + active.completed = false; + } + } + + fn complete(&mut self, playback: u64, position_ms: i64) { + if self + .active_mut(playback) + .is_none_or(|active| active.completed) + { + return; + } + if self.explicit( + playback, + "marking the episode finished", + |store, stamps, session| store.complete_session(stamps, session, position_ms), + ) && let Some(active) = self.active_mut(playback) + { + active.completed = true; + active.listen_completed = true; + } + } + + /// Records (or grows) the listening session of `playback`. + fn record_session(&mut self, playback: u64, position_ms: i64, listened_ms: i64, now_ms: i64) { + let store = self.store; + let log = self.log; + let Some(active) = self.active_mut(playback) else { + return; + }; + let mut session = ListenSession { + session_id: active.session_id.clone(), + episode_id: active.episode.episode_id.clone(), + podcast_id: active.episode.podcast_id.clone(), + device_id: String::new(), + started_at_ms: active.started_at_ms, + ended_at_ms: now_ms.max(active.started_at_ms), + start_position_ms: active.start_position_ms.max(0), + end_position_ms: position_ms.max(0), + listened_ms: listened_ms.max(0), + completed: active.listen_completed, + }; + let episode = active.episode.clone(); + let result = record(log, |stamps| { + session.device_id = stamps.device_id().to_string(); + let op = store.record_listen_session(stamps, &session, Some(&episode))?; + Ok(((), op.into_iter().collect())) + }); + if let Err(err) = result { + self.fail("recording the listening session", err); + } + } + + fn set_played(&mut self, episode_id: &EpisodeId, played: bool) -> Result<()> { + let episode = self + .store + .episode_ref(episode_id)? + .ok_or_else(|| anyhow!("the episode is no longer in the catalog"))?; + let store = self.store; + let write = record(self.log, |stamps| { + let write = if played { + store.mark_played(stamps, &episode)? + } else { + store.mark_unplayed(stamps, &episode)? + }; + let op = write.op.clone(); + Ok((write, vec![op])) + })?; + self.emit(ProgressEvent::Saved(write.record)); + Ok(()) + } +} diff --git a/src/podcasts/refresh.rs b/src/podcasts/refresh.rs new file mode 100644 index 0000000..f4ddfd9 --- /dev/null +++ b/src/podcasts/refresh.rs @@ -0,0 +1,386 @@ +//! Subscribing and refreshing feeds. +//! +//! Every function records its HTTP outcome in the store's local fetch state +//! (validators, last error, consecutive failures, next check) so the +//! scheduler never needs its own bookkeeping, and an error never clears the +//! stored catalog. + +use std::path::Path; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::{Context as _, Result, anyhow, bail}; +use furumi_library::podcasts::{FeedRefresh, PodcastStore}; +use music_dht::podcast::feed::{ParsedFeed, parse_feed_with_base}; +use music_dht::podcast::{FeedUrl, PodcastId}; + +use super::fetch::{FeedBody, Fetcher, HttpStatusError, Validators}; +use super::{OpLog, blocking}; + +/// A successfully checked feed is checked again after this long. +pub const REFRESH_INTERVAL: Duration = Duration::from_secs(60 * 60); +/// First retry after a failed check; doubles per consecutive failure. +pub const FAILURE_BACKOFF_BASE: Duration = Duration::from_secs(5 * 60); +/// Longest wait between checks of a failing feed (also caps `Retry-After`). +pub const FAILURE_BACKOFF_MAX: Duration = Duration::from_secs(24 * 60 * 60); + +/// Store, HTTP client and sync log shared by podcast background tasks. +#[derive(Clone)] +pub struct Services { + pub store: Arc, + pub fetcher: Arc, + pub log: Arc, +} + +impl std::fmt::Debug for Services { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Services") + .field("store", &self.store) + .finish_non_exhaustive() + } +} + +/// Result of [`subscribe`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SubscribeReport { + pub podcast_id: PodcastId, + pub title: String, + /// The podcast was already subscribed; nothing was fetched or written. + pub already_subscribed: bool, + /// Episodes the first refresh added to the catalog. + pub new_episodes: usize, +} + +/// Result of [`refresh`]. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct RefreshReport { + /// The server answered 304 Not Modified. + pub not_modified: bool, + pub new_episodes: usize, + pub updated_episodes: usize, + /// Feed items without identity or with invalid metadata. + pub skipped_items: usize, + /// New fetch URL after a permanent redirect or `itunes:new-feed-url`. + pub moved_to: Option, +} + +/// When to check a feed again after a success. +pub fn next_check_after_success(now_ms: i64) -> i64 { + now_ms.saturating_add(duration_ms(REFRESH_INTERVAL)) +} + +/// When to check a feed again after its `failures`-th consecutive failure: +/// exponential back-off, at least the server's `Retry-After`, at most +/// [`FAILURE_BACKOFF_MAX`]. +pub fn next_check_after_failure(now_ms: i64, failures: u32, retry_after: Option) -> i64 { + let exponent = failures.saturating_sub(1).min(16); + let backoff = FAILURE_BACKOFF_BASE + .saturating_mul(1 << exponent) + .max(retry_after.unwrap_or_default()) + .min(FAILURE_BACKOFF_MAX); + now_ms.saturating_add(duration_ms(backoff)) +} + +fn duration_ms(duration: Duration) -> i64 { + i64::try_from(duration.as_millis()).unwrap_or(i64::MAX) +} + +/// Subscribes to the feed the user typed (`FeedUrl::from_user_input`). +/// +/// Nothing is stored unless the URL serves a parsable feed. A URL that +/// resolves (directly or through a known alias or redirect) to a podcast +/// this device already knows reuses its id, so resubscribing continues the +/// saved progress and history. The subscription is private. +pub async fn subscribe(services: &Services, input: &str, now_ms: i64) -> Result { + let url = FeedUrl::from_user_input(input).map_err(|err| anyhow!("{err}"))?; + let store = Arc::clone(&services.store); + let lookup = url.clone(); + let existing = blocking(move || { + let id = store.resolve_podcast_id(&lookup)?; + let subscribed = store + .subscription(&id)? + .is_some_and(|record| record.subscribed); + let title = store.podcast(&id)?.map(|summary| summary.meta.title); + Ok(subscribed.then_some((id, title))) + }) + .await?; + if let Some((podcast_id, title)) = existing { + return Ok(SubscribeReport { + podcast_id, + title: title.unwrap_or_default(), + already_subscribed: true, + new_episodes: 0, + }); + } + + let response = services + .fetcher + .fetch_feed(&url, Validators::default()) + .await?; + let FeedBody::Modified { + body, + etag, + last_modified, + } = response.body + else { + bail!("the server answered 304 Not Modified to an unconditional request"); + }; + let final_url = response.final_url; + let feed = parse(body, &final_url).await?; + // The feed's own address: where its permanent redirects lead. + let moved_permanently = response.moved_permanently_to.is_some(); + let landed = response.moved_permanently_to.unwrap_or(final_url); + + let store = Arc::clone(&services.store); + let log = Arc::clone(&services.log); + let image_url = feed.image_url.clone(); + let report = blocking(move || { + // Identity comes from the URL of the first subscription. Reuse a + // podcast this device already knows under the typed or the final + // URL; otherwise the typed URL defines the id and a permanent + // redirect becomes an alias below. + let identity_url = if knows(&store, &url)? || !knows(&store, &landed)? { + url.clone() + } else { + landed.clone() + }; + let meta = feed.to_podcast_meta(identity_url); + let mut subscribed = None; + log.record(&mut |stamps| { + let outcome = store.subscribe(stamps, &meta)?; + let ops = outcome.ops.clone(); + subscribed = Some(outcome); + Ok(ops) + })?; + let outcome = subscribed.ok_or_else(|| anyhow!("subscription was not recorded"))?; + let podcast_id = outcome.podcast_id; + let mut refreshed = None; + log.record(&mut |stamps| { + let outcome = store.apply_feed( + stamps, + &FeedRefresh { + podcast_id: &podcast_id, + feed: &feed, + fetched_from: &landed, + moved_permanently, + }, + )?; + let ops = outcome.ops.clone(); + refreshed = Some(outcome); + Ok(ops) + })?; + let refreshed = refreshed.ok_or_else(|| anyhow!("feed refresh was not recorded"))?; + store.record_fetch_success( + &podcast_id, + now_ms, + etag.as_deref(), + last_modified.as_deref(), + Some(next_check_after_success(now_ms)), + )?; + Ok(SubscribeReport { + podcast_id, + title: meta.title, + already_subscribed: outcome.already_subscribed, + new_episodes: refreshed.new_episodes, + }) + }) + .await?; + if let Some(image_url) = image_url { + update_cover(services, &report.podcast_id, &image_url).await; + } + Ok(report) +} + +fn knows(store: &PodcastStore, url: &FeedUrl) -> Result { + let id = store.resolve_podcast_id(url)?; + Ok(store.podcast(&id)?.is_some() || store.subscription(&id)?.is_some()) +} + +/// Checks one feed (conditional GET from `url`, the store's fetch URL) and +/// ingests a changed document. Failures are recorded in the fetch state +/// with back-off and returned; the stored catalog is kept. +pub async fn refresh( + services: &Services, + podcast_id: &PodcastId, + url: &FeedUrl, + now_ms: i64, +) -> Result { + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + let fetch_state = blocking(move || store.fetch_state(&id)).await?; + let result = fetch_and_apply(services, podcast_id, url, &fetch_state, now_ms).await; + if let Err(err) = &result { + let message = format!("{err:#}"); + let retry_after = err + .downcast_ref::() + .and_then(|status| status.retry_after); + let next = next_check_after_failure( + now_ms, + fetch_state.consecutive_failures.saturating_add(1), + retry_after, + ); + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + if let Err(record_err) = blocking(move || { + store + .record_fetch_failure(&id, now_ms, &message, Some(next)) + .map(|_| ()) + }) + .await + { + tracing::warn!("recording a podcast refresh error failed: {record_err:#}"); + } + } + result +} + +async fn fetch_and_apply( + services: &Services, + podcast_id: &PodcastId, + url: &FeedUrl, + fetch_state: &furumi_library::podcasts::FetchState, + now_ms: i64, +) -> Result { + let response = services + .fetcher + .fetch_feed( + url, + Validators { + etag: fetch_state.etag.as_deref(), + last_modified: fetch_state.last_modified.as_deref(), + }, + ) + .await?; + tracing::debug!( + podcast = %podcast_id, + redirects = response.redirects, + moved_permanently = response.moved_permanently_to.is_some(), + not_modified = matches!(response.body, FeedBody::NotModified), + "podcast feed checked" + ); + let next = Some(next_check_after_success(now_ms)); + let (body, etag, last_modified) = match response.body { + FeedBody::NotModified => { + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + blocking(move || store.record_fetch_not_modified(&id, now_ms, next)).await?; + return Ok(RefreshReport { + not_modified: true, + ..RefreshReport::default() + }); + } + FeedBody::Modified { + body, + etag, + last_modified, + } => (body, etag, last_modified), + }; + let final_url = response.final_url; + let feed = parse(body, &final_url).await?; + let moved_permanently = response.moved_permanently_to.is_some(); + let landed = response.moved_permanently_to.unwrap_or(final_url); + let image_url = feed.image_url.clone(); + let store = Arc::clone(&services.store); + let log = Arc::clone(&services.log); + let id = podcast_id.clone(); + let previous_url = url.clone(); + let report = blocking(move || { + let mut refreshed = None; + log.record(&mut |stamps| { + let outcome = store.apply_feed( + stamps, + // `fetched_from` is the address the permanent redirects led + // to; the document itself may come from a temporary hop + // behind it (parsed against that URL above). + &FeedRefresh { + podcast_id: &id, + feed: &feed, + fetched_from: &landed, + moved_permanently, + }, + )?; + let ops = outcome.ops.clone(); + refreshed = Some(outcome); + Ok(ops) + })?; + let outcome = refreshed.ok_or_else(|| anyhow!("feed refresh was not recorded"))?; + store.record_fetch_success(&id, now_ms, etag.as_deref(), last_modified.as_deref(), next)?; + Ok(RefreshReport { + not_modified: false, + new_episodes: outcome.new_episodes, + updated_episodes: outcome.updated_episodes, + skipped_items: outcome.skipped_items, + moved_to: outcome + .feed_url + .filter(|feed_url| *feed_url != previous_url), + }) + }) + .await?; + if let Some(image_url) = image_url { + update_cover(services, podcast_id, &image_url).await; + } + Ok(report) +} + +/// Parses a feed document on the blocking pool; relative URLs resolve +/// against where the document was served from. +async fn parse(body: Vec, base: &FeedUrl) -> Result { + let base = base.as_str().to_string(); + blocking(move || { + parse_feed_with_base(&body, Some(&base)).map_err(|err| anyhow!("unusable feed: {err}")) + }) + .await +} + +/// Downloads the cover when the feed image changed or the cached file is +/// gone, records it and deletes the replaced file. Best effort: failures are +/// only logged, covers are a replaceable cache. +async fn update_cover(services: &Services, podcast_id: &PodcastId, image_url: &str) { + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + let current = blocking(move || { + let state = store.fetch_state(&id)?; + Ok((state.cover_url, state.cover_path)) + }) + .await; + let (old_url, old_path) = match current { + Ok(current) => current, + Err(err) => { + tracing::warn!("reading podcast cover state failed: {err:#}"); + return; + } + }; + let cached = old_path + .as_deref() + .is_some_and(|path| Path::new(path).is_file()); + if old_url.as_deref() == Some(image_url) && cached { + return; + } + let path = match services.fetcher.fetch_cover(podcast_id, image_url).await { + Ok(path) => path, + Err(err) => { + tracing::debug!(podcast = %podcast_id, "podcast cover download failed: {err:#}"); + return; + } + }; + let store = Arc::clone(&services.store); + let id = podcast_id.clone(); + let image_url = image_url.to_string(); + let result = blocking(move || { + let new_path = std::path::absolute(&path) + .unwrap_or(path) + .to_string_lossy() + .into_owned(); + store + .set_cover_cache(&id, Some(&image_url), Some(&new_path)) + .context("recording the cover")?; + if let Some(old_path) = old_path.filter(|old| *old != new_path) { + let _ = std::fs::remove_file(old_path); + } + Ok(()) + }) + .await; + if let Err(err) = result { + tracing::warn!("recording a podcast cover failed: {err:#}"); + } +} diff --git a/src/podcasts/stream.rs b/src/podcasts/stream.rs new file mode 100644 index 0000000..12d74ce --- /dev/null +++ b/src/podcasts/stream.rs @@ -0,0 +1,971 @@ +//! Seekable playback of a remote episode without downloading it first. +//! +//! Audio is never stored: the reader keeps a bounded in-memory window and +//! the episode stays on the publisher's server (it is not imported, hashed +//! or offered to peers). +//! +//! [`open`] issues one `Range: bytes=0-` request and hands the response to a +//! dedicated downloader thread. The thread fills a bounded in-memory window +//! around the reader position: +//! +//! * up to [`READ_AHEAD`] bytes ahead of the reader (then it pauses), +//! * up to [`KEEP_BEHIND`] bytes behind it (cheap small backward seeks), +//! * plus the first [`HEAD_KEEP`] bytes of the file, kept permanently because +//! decoders re-read the container header while probing. +//! +//! The window buffer is allocated once at its maximum size and never grows, +//! so memory stays bounded (and no large reallocation happens under the lock +//! the audio callback reads through). +//! +//! A seek inside the window is free. A read outside it restarts the +//! downloader at the new position with a fresh `Range` request; a generation +//! counter makes sure bytes from the superseded response are dropped. Servers +//! that ignore `Range` are still supported: forward seeks read and discard, +//! backward seeks restart from the beginning. +//! +//! Network errors: the downloader never gives up on a transient error by +//! itself. It resumes from its current offset with capped exponential backoff +//! (shortened while the reader is waiting for data), so a short outage is +//! bridged by the buffered window. The decision to fail belongs to the +//! reader: a `read` that is blocked on missing data and sees no download +//! progress for [`STARVE_TIMEOUT`] returns `io::ErrorKind::TimedOut`; the next +//! `read` starts a fresh attempt. Client errors (HTTP 4xx other than 408/429, +//! e.g. an expired signed URL) are reported to the reader immediately. +//! +//! Timeouts: `reqwest::blocking` applies `ClientBuilder::timeout` to waiting +//! for the response head and then *separately to every `Response::read` +//! call* (verified in reqwest 0.12.28: `blocking::Response::read` wraps each +//! read in `wait::timeout(.., self.timeout)`, and the blocking client does not +//! forward that value to the async client's total timeout). So +//! `timeout(STALL_TIMEOUT)` is a per-read stall timeout, not a cap on the whole +//! body, and long episodes are fine. The request-level +//! `RequestBuilder::timeout` (which *is* a total timeout) is never used. +//! +//! Cancellation: a stream belongs to one playback start ([`CancelToken`]). +//! Once that start is superseded (another item, stop, restart), every read, +//! including one blocked waiting for data inside the audio callback, fails +//! within [`CANCEL_POLL`], and the downloader thread stops. So a stalled +//! stream never holds the audio thread past the user's next choice, and a +//! superseded resume (which may be reading and discarding up to its offset) +//! stops downloading. +//! +//! End of playback: [`StreamProbe::end`] tells whether the reader saw the +//! file end the way the server described it or stopped on an error, so an +//! episode whose connection was lost is not taken as played to the end. +//! +//! Decoder note for the player integration: symphonia's MP3 demuxer in +//! accurate seek mode never seeks by byte offset (a backward seek re-parses +//! from the first frame), which would re-download the episode. Build rodio +//! decoders for these streams with `.with_coarse_seek(true)` (and +//! `.with_byte_len(..)`), so a seek maps to a single range request. + +use std::collections::VecDeque; +use std::io::{self, Read, Seek, SeekFrom}; +use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; +use std::time::{Duration, Instant}; + +use anyhow::{Context, Result, anyhow, bail}; +use reqwest::blocking::{Client, Response}; +use reqwest::header::{CONTENT_RANGE, CONTENT_TYPE, RANGE}; +use reqwest::{StatusCode, Url}; + +use super::fetch::redact; + +/// Maximum bytes buffered ahead of the reader position. +pub const READ_AHEAD: u64 = 8 * 1024 * 1024; +/// Bytes kept behind the reader position for small backward seeks. +pub const KEEP_BEHIND: u64 = 1024 * 1024; +/// Leading bytes of the file kept for the lifetime of the stream. +pub const HEAD_KEEP: usize = 256 * 1024; +/// Size of a single network read. +const CHUNK: usize = 64 * 1024; +/// Largest the window can get: the read-ahead may overshoot by less than one +/// chunk before the downloader pauses. +const WINDOW_CAP: usize = READ_AHEAD as usize + KEEP_BEHIND as usize + CHUNK; +/// A forward gap up to this size is bridged by waiting for the downloader +/// instead of issuing a new range request. +const NEAR_GAP: u64 = 512 * 1024; +/// A restart this close ahead of the current response offset keeps the +/// response and discards bytes instead of reconnecting. +const DISCARD_LIMIT: u64 = 256 * 1024; +const CONNECT_TIMEOUT: Duration = Duration::from_secs(15); +/// No progress for this long on one read (or the response head) is an error. +const STALL_TIMEOUT: Duration = Duration::from_secs(30); +/// First retry delay; doubles per consecutive failure. +const RETRY_BASE: Duration = Duration::from_millis(250); +/// Longest delay between attempts while the reader still has buffered data. +const MAX_BACKOFF: Duration = Duration::from_secs(10); +/// Longest delay between attempts while the reader is waiting for data. +const STARVED_BACKOFF: Duration = Duration::from_secs(1); +/// A `read` blocked on missing data fails after this long without any +/// download progress. Shorter in tests to keep failure tests fast. +#[cfg(not(test))] +pub const STARVE_TIMEOUT: Duration = Duration::from_secs(30); +#[cfg(test)] +pub const STARVE_TIMEOUT: Duration = Duration::from_secs(4); +/// How often a blocked read or an idle downloader checks its +/// [`CancelToken`]. +pub const CANCEL_POLL: Duration = Duration::from_millis(100); + +/// Tells a stream that the playback start it belongs to was superseded. +#[derive(Clone)] +pub struct CancelToken(Option bool + Send + Sync>>); + +impl CancelToken { + /// A stream that is only closed by dropping it. + #[cfg(test)] + pub fn never() -> CancelToken { + CancelToken(None) + } + + /// `cancelled` becomes true once the start is superseded; it is polled. + pub fn new(cancelled: impl Fn() -> bool + Send + Sync + 'static) -> CancelToken { + CancelToken(Some(Arc::new(cancelled))) + } + + pub fn is_cancelled(&self) -> bool { + self.0.as_ref().is_some_and(|cancelled| cancelled()) + } +} + +/// The playback start was superseded while the stream was opened or read. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Cancelled; + +impl std::fmt::Display for Cancelled { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("the episode start was superseded") + } +} + +impl std::error::Error for Cancelled {} + +fn cancelled_error() -> io::Error { + io::Error::other(Cancelled) +} + +/// How the reader's last read ended, for telling a finished episode from a +/// lost connection. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StreamEnd { + /// No read failed and the server announced the file size, so the + /// decoder ended at the end the server described. + Complete, + /// No read failed, but the server sent no size: a connection closed + /// early looks exactly like the end of the file. + Unknown, + /// The last read failed (outage longer than [`STARVE_TIMEOUT`], a client + /// error such as an expired URL, or cancellation). + Failed, +} + +/// Read-only view of a stream's state that outlives handing the reader to +/// the decoder. +#[derive(Clone)] +pub struct StreamProbe(Arc); + +impl std::fmt::Debug for StreamProbe { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("StreamProbe").field(&self.end()).finish() + } +} + +impl StreamProbe { + pub fn end(&self) -> StreamEnd { + let state = self.0.lock(); + if state.read_failed { + StreamEnd::Failed + } else if state.declared_total { + StreamEnd::Complete + } else { + StreamEnd::Unknown + } + } +} + +pub struct OpenedStream { + pub reader: HttpStream, + pub probe: StreamProbe, + pub byte_len: Option, + pub mime_type: Option, + pub seekable: bool, +} + +/// `Read + Seek` view of a remote file; see the module docs. +pub struct HttpStream { + pos: u64, + shared: Arc, +} + +struct Shared { + state: Mutex, + cancel: CancelToken, + /// Signalled when data, EOF, an error or downloader exit is available to + /// the reader. + data_ready: Condvar, + /// Signalled when the reader moved, requested a restart, started waiting + /// or went away. + space_ready: Condvar, +} + +struct State { + /// Bumped on every restart; downloader output for older generations is + /// discarded. + generation: u64, + /// Start offset requested for the current generation. + target: u64, + /// File offset of `window[0]`. + window_start: u64, + window: VecDeque, + /// Bytes `0..head.len()` of the file. + head: Vec, + /// Last position the reader consumed from the window. + reader_pos: u64, + total: Option, + /// `total` comes from the server (`Content-Length` / `Content-Range`), + /// not from a connection that closed. + declared_total: bool, + /// The reader's last read returned an error (cleared by data). + read_failed: bool, + ranges: bool, + /// The current generation reached the end of the body. + done: bool, + /// The current generation was abandoned (fatal error, or the reader gave + /// up waiting); the downloader idles until the reader restarts it. + failed: bool, + /// Fatal error to report to the reader once. + error: Option<(io::ErrorKind, String)>, + /// Most recent transient failure, for the reader's timeout message. + last_error: Option, + /// Bytes received from the network for the current generation + /// (including discarded ones); the reader's stall detection watches it. + progress: u64, + /// The reader is blocked waiting for data. + reader_waiting: bool, + /// The reader went away. + closed: bool, + /// The downloader thread exited. + exited: bool, + peak_buffered: usize, + peak_capacity: usize, +} + +impl State { + fn window_end(&self) -> u64 { + self.window_start + self.window.len() as u64 + } + + fn buffered(&self) -> usize { + self.window.len() + self.head.len() + } + + fn restart_at(&mut self, pos: u64) { + self.generation = self.generation.wrapping_add(1); + self.target = pos; + self.window_start = pos; + // Keeps the allocation: the window never needs more than WINDOW_CAP. + self.window.clear(); + self.done = false; + self.failed = false; + self.error = None; + self.last_error = None; + self.progress = 0; + } + + fn trim_behind(&mut self) { + let keep_from = self.reader_pos.saturating_sub(KEEP_BEHIND); + if keep_from > self.window_start { + let drop = (keep_from - self.window_start).min(self.window.len() as u64) as usize; + self.window.drain(..drop); + self.window_start += drop as u64; + } + } +} + +impl Shared { + fn lock(&self) -> MutexGuard<'_, State> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } +} + +/// [`open_with`] without cancellation. +#[cfg(test)] +pub fn open(url: &str) -> Result { + open_with(url, CancelToken::never()) +} + +/// Opens a remote audio file for streaming playback. Blocking: call it from +/// `spawn_blocking` or a plain thread, never directly on a Tokio worker. +/// +/// Only http(s) URLs are accepted. Errors never contain the URL's query, +/// which may hold a private token. Fails with [`Cancelled`] when `cancel` +/// fires before the stream is ready. +pub fn open_with(url: &str, cancel: CancelToken) -> Result { + let parsed = Url::parse(url).context("the episode audio URL is invalid")?; + if !matches!(parsed.scheme(), "http" | "https") { + bail!("unsupported audio URL scheme {:?}", parsed.scheme()); + } + let shown = redact(&parsed); + let _ = rustls::crypto::ring::default_provider().install_default(); + let client = Client::builder() + .connect_timeout(CONNECT_TIMEOUT) + // Per-read stall timeout, see the module docs. + .timeout(STALL_TIMEOUT) + .redirect(reqwest::redirect::Policy::custom(|attempt| { + if attempt.previous().len() >= 10 { + attempt.error("too many redirects") + } else if matches!(attempt.url().scheme(), "http" | "https") { + attempt.follow() + } else { + attempt.stop() + } + })) + .user_agent(concat!( + "furumi/", + env!("CARGO_PKG_VERSION"), + " (podcast reader)" + )) + .build() + .context("building the streaming HTTP client")?; + let response = client + .get(parsed) + .header(RANGE, "bytes=0-") + .send() + .map_err(|err| anyhow!(err.without_url())) + .with_context(|| format!("requesting {shown}"))?; + if cancel.is_cancelled() { + return Err(Cancelled.into()); + } + let opened = classify(&response, 0).with_context(|| format!("requesting {shown}"))?; + let mime_type = response + .headers() + .get(CONTENT_TYPE) + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.split(';').next()) + .map(|value| value.trim().to_ascii_lowercase()) + .filter(|value| !value.is_empty()); + + let window = VecDeque::with_capacity(WINDOW_CAP); + let peak_capacity = window.capacity(); + let shared = Arc::new(Shared { + state: Mutex::new(State { + generation: 0, + target: 0, + window_start: 0, + window, + head: Vec::with_capacity(HEAD_KEEP), + reader_pos: 0, + total: opened.total, + declared_total: opened.total.is_some(), + read_failed: false, + ranges: opened.ranges, + done: false, + failed: false, + error: None, + last_error: None, + progress: 0, + reader_waiting: false, + closed: false, + exited: false, + peak_buffered: 0, + peak_capacity, + }), + cancel, + data_ready: Condvar::new(), + space_ready: Condvar::new(), + }); + // Requests continue at the final URL of the redirect chain. + let downloader = Downloader { + shared: Arc::clone(&shared), + client, + url: response.url().clone(), + }; + std::thread::Builder::new() + .name("podcast-stream".to_string()) + .spawn(move || downloader.run(response)) + .context("spawning the podcast stream thread")?; + Ok(OpenedStream { + probe: StreamProbe(Arc::clone(&shared)), + reader: HttpStream { pos: 0, shared }, + byte_len: opened.total, + mime_type, + seekable: opened.total.is_some(), + }) +} + +struct Classified { + /// Offset of the first body byte. + start: u64, + total: Option, + ranges: bool, +} + +/// Interprets a response to `Range: bytes=-` (or a plain GET). +fn classify(response: &Response, requested: u64) -> Result { + let status = response.status(); + if status == StatusCode::PARTIAL_CONTENT { + let range = response + .headers() + .get(CONTENT_RANGE) + .and_then(|value| value.to_str().ok()) + .and_then(parse_content_range); + let Some((start, total)) = range else { + bail!("HTTP 206 without a usable Content-Range"); + }; + if start != requested { + bail!("server returned range starting at {start}, expected {requested}"); + } + return Ok(Classified { + start, + total, + ranges: true, + }); + } + if status.is_success() { + return Ok(Classified { + start: 0, + total: response.content_length(), + ranges: false, + }); + } + Err(StreamStatusError(status.as_u16()).into()) +} + +/// The audio server answered with an error status. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct StreamStatusError(pub u16); + +impl StreamStatusError { + /// The URL itself is wrong or expired (4xx other than 408/429): the + /// feed may know a newer address for the episode. + pub fn url_is_stale(self) -> bool { + (400..500).contains(&self.0) && self.0 != 408 && self.0 != 429 + } +} + +impl std::fmt::Display for StreamStatusError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match StatusCode::from_u16(self.0) { + Ok(status) => write!(f, "HTTP {status}"), + Err(_) => write!(f, "HTTP {}", self.0), + } + } +} + +impl std::error::Error for StreamStatusError {} + +/// Parses `bytes -/` into `(start, total)`. +fn parse_content_range(value: &str) -> Option<(u64, Option)> { + let value = value.trim(); + let rest = value + .get(..6) + .filter(|unit| unit.eq_ignore_ascii_case("bytes ")) + .map(|_| &value[6..])?; + let (range, total) = rest.split_once('/')?; + let (start, _end) = range.trim().split_once('-')?; + let start = start.trim().parse().ok()?; + let total = match total.trim() { + "*" => None, + total => Some(total.parse().ok()?), + }; + Some((start, total)) +} + +/// Why a (re)connection attempt failed. +enum Failure { + /// Worth retrying (network error, 5xx, 408/429, odd range answer). + Transient(String), + /// Retrying will not help (4xx such as 403/404/410). + Fatal(String), +} + +struct Downloader { + shared: Arc, + client: Client, + url: Url, +} + +/// Marks the downloader as exited however `run` returns (including a panic). +struct ExitFlag(Arc); + +impl Drop for ExitFlag { + fn drop(&mut self) { + // Set under the lock so a reader that checked `exited` and is about + // to wait cannot miss the notification. + let mut state = self.0.lock(); + state.exited = true; + drop(state); + self.0.data_ready.notify_all(); + } +} + +impl Downloader { + fn run(self, first: Response) { + let _exit = ExitFlag(Arc::clone(&self.shared)); + let mut generation = 0u64; + let mut response = Some(first); + // File offset of the next byte `response` yields, or where the next + // request should start when there is no response. + let mut offset = 0u64; + // Consecutive failures of the current generation (drives backoff). + let mut failures = 0u32; + let mut buf = vec![0u8; CHUNK]; + loop { + if !self.wait_for_work(&mut generation, &mut response, &mut offset, &mut failures) { + return; + } + if response.is_none() { + match self.reopen(offset) { + Ok(Some((next, start))) => { + response = Some(next); + offset = start; + } + Ok(None) => { + // Nothing left at this offset (HTTP 416). + self.mark_done(generation, offset, false); + continue; + } + Err(Failure::Fatal(message)) => { + failures = 0; + self.fail(generation, message); + continue; + } + Err(Failure::Transient(message)) => { + if !self.retry(generation, &mut failures, &message) { + return; + } + continue; + } + } + } + let Some(body) = response.as_mut() else { + continue; + }; + let read = body.read(&mut buf); + if self.shared.cancel.is_cancelled() { + return; + } + match read { + Ok(0) => { + response = None; + let total = self.shared.lock().total; + if total.is_some_and(|total| offset < total) { + // Connection closed early: resume from `offset`. + let message = format!("connection closed at byte {offset}"); + if !self.retry(generation, &mut failures, &message) { + return; + } + continue; + } + self.mark_done(generation, offset, true); + } + Ok(read) => { + failures = 0; + let start = offset; + offset += read as u64; + self.publish(generation, start, &buf[..read]); + } + Err(err) if err.kind() == io::ErrorKind::Interrupted => {} + Err(err) => { + response = None; + if !self.retry(generation, &mut failures, &err.to_string()) { + return; + } + } + } + } + } + + /// Blocks until there is something to download, adopting restarts + /// requested by the reader. Returns `false` when the reader is gone. + fn wait_for_work( + &self, + generation: &mut u64, + response: &mut Option, + offset: &mut u64, + failures: &mut u32, + ) -> bool { + let mut state = self.shared.lock(); + loop { + if state.closed || self.shared.cancel.is_cancelled() { + return false; + } + if state.generation != *generation { + *generation = state.generation; + // A new generation gets a fresh backoff. + *failures = 0; + // Keep the open response when the new target is a little + // ahead of it (or anywhere ahead if ranges are unsupported): + // discarding is cheaper than reconnecting. + let keep = response.is_some() + && state.target >= *offset + && (!state.ranges || state.target - *offset <= DISCARD_LIMIT); + if !keep { + *response = None; + *offset = state.target; + } + continue; + } + let paused = state.done + || state.failed + || state.window_end() >= state.reader_pos.saturating_add(READ_AHEAD); + if !paused { + return true; + } + state = self + .shared + .space_ready + .wait_timeout(state, CANCEL_POLL) + .unwrap_or_else(PoisonError::into_inner) + .0; + } + } + + /// Requests the file from `offset` (from 0 when ranges are unsupported). + /// `Ok(None)` means the server has nothing at that offset. + fn reopen(&self, offset: u64) -> std::result::Result, Failure> { + let ranges = self.shared.lock().ranges; + let mut request = self.client.get(self.url.clone()); + if ranges { + request = request.header(RANGE, format!("bytes={offset}-")); + } + let response = request + .send() + .map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err.without_url()))))?; + let status = response.status(); + if ranges && status == StatusCode::RANGE_NOT_SATISFIABLE { + return Ok(None); + } + if status.is_client_error() + && status != StatusCode::REQUEST_TIMEOUT + && status != StatusCode::TOO_MANY_REQUESTS + { + return Err(Failure::Fatal(format!("HTTP {status}"))); + } + let classified = classify(&response, if ranges { offset } else { 0 }) + .map_err(|err| Failure::Transient(format!("{err:#}")))?; + let mut state = self.shared.lock(); + if !classified.ranges && state.ranges { + // The server stopped honouring Range: read from 0 and discard. + state.ranges = false; + } + if state.total.is_none() && classified.total.is_some() { + state.total = classified.total; + state.declared_total = true; + } + Ok(Some((response, classified.start))) + } + + fn mark_done(&self, generation: u64, offset: u64, learn_total: bool) { + let mut state = self.shared.lock(); + if state.generation != generation { + return; + } + state.done = true; + if learn_total && state.total.is_none() { + state.total = Some(offset.max(state.window_end())); + } + self.shared.data_ready.notify_all(); + } + + /// Abandons the current generation with an error for the reader. + fn fail(&self, generation: u64, message: String) { + let mut state = self.shared.lock(); + if state.generation != generation { + return; + } + tracing::warn!("podcast stream failed: {message}"); + state.failed = true; + state.error = Some((io::ErrorKind::Other, message)); + self.shared.data_ready.notify_all(); + } + + /// Records a transient failure and backs off before the next attempt. + /// Returns `false` when the thread should exit. + fn retry(&self, generation: u64, failures: &mut u32, message: &str) -> bool { + let mut state = self.shared.lock(); + if state.closed { + return false; + } + if state.generation != generation { + // The reader moved on; `wait_for_work` adopts the new target. + return true; + } + *failures = failures.saturating_add(1); + state.last_error = Some(message.to_string()); + tracing::debug!("podcast stream attempt failed ({failures} in a row): {message}"); + let delay = RETRY_BASE + .saturating_mul(1 << (*failures - 1).min(6)) + .min(MAX_BACKOFF); + let started = Instant::now(); + // Wakes early when the reader goes away, seeks elsewhere or starts + // waiting for data (which shortens the delay). + loop { + if state.closed || self.shared.cancel.is_cancelled() { + return false; + } + if state.generation != generation { + return true; + } + let limit = if state.reader_waiting { + delay.min(STARVED_BACKOFF) + } else { + delay + }; + let elapsed = started.elapsed(); + if elapsed >= limit { + return true; + } + state = self + .shared + .space_ready + .wait_timeout(state, (limit - elapsed).min(CANCEL_POLL)) + .unwrap_or_else(PoisonError::into_inner) + .0; + } + } + + fn publish(&self, generation: u64, start: u64, bytes: &[u8]) { + let mut state = self.shared.lock(); + if state.generation != generation { + return; + } + state.progress = state.progress.wrapping_add(bytes.len() as u64); + state.last_error = None; + let end = start + bytes.len() as u64; + // Permanent copy of the file head for format probing. + let head_len = state.head.len() as u64; + if start <= head_len && end > head_len && head_len < HEAD_KEEP as u64 { + let from = (head_len - start) as usize; + let to = (HEAD_KEEP as u64).min(end) - start; + state.head.extend_from_slice(&bytes[from..to as usize]); + } + // Only bytes at or after the window end are new; earlier ones are + // being skipped (Range-less seek or resume) or are already buffered. + let want = state.window_end(); + if end > want { + if start > want { + // A gap should not happen; resynchronise rather than corrupt. + state.window.clear(); + state.window_start = start; + state.window.extend(bytes); + } else { + state.window.extend(&bytes[(want - start) as usize..]); + } + state.trim_behind(); + } + state.peak_buffered = state.peak_buffered.max(state.buffered()); + state.peak_capacity = state.peak_capacity.max(state.window.capacity()); + self.shared.data_ready.notify_all(); + } +} + +fn downloader_stopped() -> io::Error { + io::Error::other("podcast stream downloader stopped") +} + +impl Read for HttpStream { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + if buf.is_empty() { + return Ok(0); + } + let shared = &*self.shared; + let state = shared.lock(); + let (mut state, result) = read_locked(shared, &mut self.pos, state, buf); + state.reader_waiting = false; + match &result { + Ok(0) => {} + Ok(_) => state.read_failed = false, + Err(_) => state.read_failed = true, + } + result + } +} + +/// The body of [`HttpStream::read`]; hands the guard back so the caller can +/// clear `reader_waiting` on every exit path. +fn read_locked<'a>( + shared: &Shared, + pos: &mut u64, + mut state: MutexGuard<'a, State>, + buf: &mut [u8], +) -> (MutexGuard<'a, State>, io::Result) { + // When this read started waiting, and the download progress then. + let mut starved: Option<(Instant, u64)> = None; + loop { + if shared.cancel.is_cancelled() { + return (state, Err(cancelled_error())); + } + if state.total.is_some_and(|total| *pos >= total) { + return (state, Ok(0)); + } + if *pos < state.head.len() as u64 { + let from = *pos as usize; + let len = buf.len().min(state.head.len() - from); + buf[..len].copy_from_slice(&state.head[from..from + len]); + *pos += len as u64; + return (state, Ok(len)); + } + let window_end = state.window_end(); + if *pos >= state.window_start && *pos < window_end { + let from = (*pos - state.window_start) as usize; + let len = buf.len().min(state.window.len() - from); + let (front, back) = state.window.as_slices(); + copy_from_ring(front, back, from, &mut buf[..len]); + *pos += len as u64; + state.reader_pos = *pos; + // Free what fell out of the keep-behind range even when the + // downloader is idle (paused or done). + state.trim_behind(); + shared.space_ready.notify_all(); + return (state, Ok(len)); + } + // Not buffered. Wait for the current generation if it is heading + // here, otherwise restart the downloader at the reader position. + let reachable = *pos >= state.window_start + && (!state.ranges || *pos <= window_end.saturating_add(NEAR_GAP)); + if reachable && !state.failed { + if state.done { + return (state, Ok(0)); + } + if state.exited { + return (state, Err(downloader_stopped())); + } + if state.reader_pos != *pos { + state.reader_pos = *pos; + state.trim_behind(); + shared.space_ready.notify_all(); + } + let now = Instant::now(); + let since = match starved { + Some((since, seen)) if seen == state.progress => since, + _ => { + starved = Some((now, state.progress)); + now + } + }; + let waited = now.saturating_duration_since(since); + if waited >= STARVE_TIMEOUT { + // Give up on this generation; the next read retries. + state.failed = true; + state.error = None; + let cause = state + .last_error + .take() + .unwrap_or_else(|| "no data received".to_string()); + let error = io::Error::new( + io::ErrorKind::TimedOut, + format!( + "podcast stream stalled for {}s: {cause}", + STARVE_TIMEOUT.as_secs() + ), + ); + return (state, Err(error)); + } + if !state.reader_waiting { + // Lets a downloader in backoff retry sooner. + state.reader_waiting = true; + shared.space_ready.notify_all(); + } + state = shared + .data_ready + .wait_timeout(state, (STARVE_TIMEOUT - waited).min(CANCEL_POLL)) + .unwrap_or_else(PoisonError::into_inner) + .0; + continue; + } + if let Some((kind, message)) = state.error.take() { + // Report once; the next read retries from scratch. + return (state, Err(io::Error::new(kind, message))); + } + if state.exited { + return (state, Err(downloader_stopped())); + } + state.restart_at(*pos); + state.reader_pos = *pos; + starved = None; + shared.space_ready.notify_all(); + } +} + +fn copy_from_ring(front: &[u8], back: &[u8], from: usize, out: &mut [u8]) { + let mut written = 0; + if from < front.len() { + let len = out.len().min(front.len() - from); + out[..len].copy_from_slice(&front[from..from + len]); + written = len; + } + if written < out.len() { + let back_from = from + written - front.len(); + let len = out.len() - written; + out[written..].copy_from_slice(&back[back_from..back_from + len]); + } +} + +impl Seek for HttpStream { + fn seek(&mut self, position: SeekFrom) -> io::Result { + let target = match position { + SeekFrom::Start(offset) => i128::from(offset), + SeekFrom::Current(delta) => i128::from(self.pos) + i128::from(delta), + SeekFrom::End(delta) => { + let Some(total) = self.shared.lock().total else { + return Err(io::Error::new( + io::ErrorKind::Unsupported, + "stream length is unknown", + )); + }; + i128::from(total) + i128::from(delta) + } + }; + let target = u64::try_from(target).map_err(|_| { + io::Error::new( + io::ErrorKind::InvalidInput, + "invalid seek to a negative or overflowing position", + ) + })?; + // Lazy: the downloader is only redirected when a read needs data. + self.pos = target; + Ok(target) + } +} + +impl Drop for HttpStream { + fn drop(&mut self) { + let mut state = self.shared.lock(); + state.closed = true; + state.window = VecDeque::new(); + state.head = Vec::new(); + drop(state); + self.shared.space_ready.notify_all(); + self.shared.data_ready.notify_all(); + } +} + +#[cfg(test)] +impl HttpStream { + /// Bytes currently held in memory (window + head). + pub(super) fn buffered_bytes(&self) -> usize { + self.shared.lock().buffered() + } + + /// Highest number of bytes ever held in memory at once. + pub(super) fn peak_buffered_bytes(&self) -> usize { + self.shared.lock().peak_buffered + } + + /// Largest allocation (window + head capacity) ever held. + pub(super) fn peak_allocated_bytes(&self) -> usize { + let state = self.shared.lock(); + state.peak_capacity + state.head.capacity() + } + + /// Flag the downloader thread sets when it exits. + pub(super) fn exit_probe(&self) -> impl Fn() -> bool + Send + 'static { + let shared = Arc::clone(&self.shared); + move || shared.lock().exited + } +} + +/// Upper bound of [`HttpStream`] memory use, for tests. +#[cfg(test)] +pub(super) const MAX_BUFFERED: usize = WINDOW_CAP + HEAD_KEEP; diff --git a/src/podcasts/stream_tests.rs b/src/podcasts/stream_tests.rs new file mode 100644 index 0000000..0fb4714 --- /dev/null +++ b/src/podcasts/stream_tests.rs @@ -0,0 +1,405 @@ +//! Tests of the HTTP Range stream reader against the local test server: +//! sequential reads, seeks inside and outside the window, servers without +//! Range or Content-Length, transient cuts, persistent failures, bounded +//! memory and thread shutdown. + +use super::stream::{self, MAX_BUFFERED}; +use super::test_server::{Route, TestServer, noise}; +use std::io::{ErrorKind, Read, Seek, SeekFrom}; +use std::time::{Duration, Instant}; + +const MIB: usize = 1024 * 1024; + +fn read_to_end(reader: &mut impl Read) -> Vec { + let mut out = Vec::new(); + reader.read_to_end(&mut out).unwrap(); + out +} + +fn read_n(reader: &mut impl Read, len: usize) -> Vec { + let mut out = vec![0; len]; + reader.read_exact(&mut out).unwrap(); + out +} + +fn serve(route: Route) -> (TestServer, String) { + let server = TestServer::start(); + server.set("/audio.mp3", route); + let url = server.url("/audio.mp3"); + (server, url) +} + +#[test] +fn sequential_read_matches_source() { + let body = noise(3 * MIB + 123, 1); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg; charset=binary")); + let mut opened = stream::open(&url).unwrap(); + assert_eq!(opened.byte_len, Some(body.len() as u64)); + assert_eq!(opened.mime_type.as_deref(), Some("audio/mpeg")); + assert!(opened.seekable); + assert_eq!(read_to_end(&mut opened.reader), body); + let mut buf = [0u8; 16]; + assert_eq!(opened.reader.read(&mut buf).unwrap(), 0, "EOF stays EOF"); + assert_eq!(server.ranges("/audio.mp3"), [Some("bytes=0-".to_string())]); +} + +#[test] +fn seeks_forward_backward_and_from_end() { + let body = noise(12 * MIB, 2); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg")); + let mut reader = stream::open(&url).unwrap().reader; + assert_eq!(read_n(&mut reader, 1000), body[..1000]); + + // Far forward: outside the read-ahead window -> new range request. + let far = 11 * MIB + 7; + assert_eq!( + reader.seek(SeekFrom::Start(far as u64)).unwrap(), + far as u64 + ); + assert_eq!(read_n(&mut reader, 70_000), body[far..far + 70_000]); + assert!( + server + .ranges("/audio.mp3") + .contains(&Some(format!("bytes={far}-"))) + ); + + // Small backward seek within the kept window: no new request. + let requests = server.ranges("/audio.mp3").len(); + reader.seek(SeekFrom::Current(-50_000)).unwrap(); + let pos = far + 20_000; + assert_eq!(read_n(&mut reader, 1000), body[pos..pos + 1000]); + assert_eq!(server.ranges("/audio.mp3").len(), requests); + + // Back into the permanently kept head: no new request. + reader.seek(SeekFrom::Start(10)).unwrap(); + assert_eq!(read_n(&mut reader, 100), body[10..110]); + assert_eq!(server.ranges("/audio.mp3").len(), requests); + + // Backward outside the window and the head. + let back = 2 * MIB + 3; + reader.seek(SeekFrom::Start(back as u64)).unwrap(); + assert_eq!(read_n(&mut reader, 300_000), body[back..back + 300_000]); + + // From the end. + let end = reader.seek(SeekFrom::End(-100)).unwrap(); + assert_eq!(end, body.len() as u64 - 100); + assert_eq!(read_to_end(&mut reader), body[body.len() - 100..]); + + // Past the end. + let beyond = reader.seek(SeekFrom::End(50)).unwrap(); + assert_eq!(beyond, body.len() as u64 + 50); + let mut buf = [0u8; 8]; + assert_eq!(reader.read(&mut buf).unwrap(), 0); + assert_eq!( + reader + .seek(SeekFrom::Current(-(beyond as i64) - 1)) + .unwrap_err() + .kind(), + ErrorKind::InvalidInput + ); +} + +#[test] +fn server_without_range_support() { + let body = noise(3 * MIB, 3); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").no_ranges()); + let opened = stream::open(&url).unwrap(); + assert!(opened.seekable, "length is known"); + let mut reader = opened.reader; + assert_eq!(read_n(&mut reader, 400_000), body[..400_000]); + // Forward seek: read and discard on the same response. + reader.seek(SeekFrom::Start(2 * MIB as u64)).unwrap(); + assert_eq!(read_n(&mut reader, 1000), body[2 * MIB..2 * MIB + 1000]); + // Backward seek outside window and head: restart from 0. + reader.seek(SeekFrom::Start(300_000)).unwrap(); + let _ = read_n(&mut reader, 1); + reader.seek(SeekFrom::Start(300_000)).unwrap(); + assert_eq!(read_to_end(&mut reader), body[300_000..]); + let requests = server.ranges("/audio.mp3"); + assert!(requests.len() >= 2, "{requests:?}"); + assert!(requests[1..].iter().all(Option::is_none), "{requests:?}"); + reader.seek(SeekFrom::End(-10)).unwrap(); + assert_eq!(read_to_end(&mut reader), body[body.len() - 10..]); +} + +#[test] +fn unknown_length_is_not_seekable_from_end() { + let body = noise(MIB + 5, 4); + let (_server, url) = serve( + Route::new(body.clone(), "audio/mpeg") + .no_ranges() + .no_length(), + ); + let opened = stream::open(&url).unwrap(); + assert!(!opened.seekable); + assert_eq!(opened.byte_len, None); + let mut reader = opened.reader; + assert_eq!( + reader.seek(SeekFrom::End(0)).unwrap_err().kind(), + ErrorKind::Unsupported + ); + assert_eq!(read_to_end(&mut reader), body); +} + +#[test] +fn transient_disconnects_are_retried() { + let body = noise(2 * MIB, 5); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(700_000, 2)); + let mut reader = stream::open(&url).unwrap().reader; + assert_eq!(read_to_end(&mut reader), body); + let requests = server.ranges("/audio.mp3"); + assert_eq!(requests.len(), 3, "{requests:?}"); + assert_eq!(requests[1], Some("bytes=700000-".to_string())); +} + +#[test] +fn persistent_failure_surfaces_an_error() { + let body = noise(2 * MIB, 6); + let (_server, url) = serve(Route::new(body, "audio/mpeg").cut(100_000, 100)); + let mut reader = stream::open(&url).unwrap().reader; + let mut out = Vec::new(); + let started = Instant::now(); + let err = reader.read_to_end(&mut out).unwrap_err(); + assert_eq!(out.len(), 100_000); + assert_eq!(err.kind(), ErrorKind::TimedOut, "{err}"); + assert!(err.to_string().contains("stalled"), "{err}"); + // Bounded by the starvation deadline, not by a retry count. + assert!(started.elapsed() >= stream::STARVE_TIMEOUT); + assert!(started.elapsed() < stream::STARVE_TIMEOUT * 3); +} + +/// An outage that ends while buffered audio is still playing must not +/// end playback: the downloader keeps retrying instead of giving up +/// after a fixed number of attempts. +#[test] +fn outage_shorter_than_the_buffer_is_bridged() { + let body = noise(3 * MIB, 10); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(MIB, 4)); + let mut reader = stream::open(&url).unwrap().reader; + assert_eq!(read_n(&mut reader, 1000), body[..1000]); + // "Play" buffered audio while the server fails four times and then + // recovers. + std::thread::sleep(Duration::from_millis(4500)); + assert_eq!(read_to_end(&mut reader), body[1000..]); + let requests = server.ranges("/audio.mp3"); + assert_eq!(requests.len(), 5, "{requests:?}"); + assert!( + requests[1..] + .iter() + .all(|range| range.as_deref() == Some("bytes=1048576-")), + "{requests:?}" + ); +} + +#[test] +fn client_errors_are_reported_without_waiting() { + let body = noise(2 * MIB, 11); + let (server, url) = serve(Route::new(body, "audio/mpeg").cut(100_000, 1)); + let mut reader = stream::open(&url).unwrap().reader; + // The signed URL "expires" after the first response. + server.set("/audio.mp3", Route::new("gone", "text/plain").status(404)); + let started = Instant::now(); + let mut out = Vec::new(); + let err = reader.read_to_end(&mut out).unwrap_err(); + assert_eq!(out.len(), 100_000); + assert!(err.to_string().contains("404"), "{err}"); + assert!( + started.elapsed() < stream::STARVE_TIMEOUT, + "{:?}", + started.elapsed() + ); +} + +#[test] +fn large_body_keeps_memory_bounded() { + let body = noise(20 * MIB, 7); + let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg")); + let mut reader = stream::open(&url).unwrap().reader; + let mut buf = vec![0u8; 32 * 1024]; + let mut pos = 0usize; + let mut hasher_ok = true; + loop { + let read = reader.read(&mut buf).unwrap(); + if read == 0 { + break; + } + hasher_ok &= buf[..read] == body[pos..pos + read]; + pos += read; + if pos % (2 * MIB) < 32 * 1024 && pos + stream::READ_AHEAD as usize <= body.len() { + // Let the downloader run ahead until it pauses at its cap. + let deadline = Instant::now() + Duration::from_secs(5); + while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline + { + std::thread::sleep(Duration::from_millis(5)); + } + } + assert!(reader.buffered_bytes() <= MAX_BUFFERED); + } + assert_eq!(pos, body.len()); + assert!(hasher_ok, "data mismatch"); + let peak = reader.peak_buffered_bytes(); + assert!(peak <= MAX_BUFFERED, "peak {peak} > {MAX_BUFFERED}"); + assert!( + peak > 4 * MIB, + "read-ahead should have buffered, peak {peak}" + ); + // Far seeks restart the window without growing its allocation. + for target in [MIB + 5, 15 * MIB, 3 * MIB] { + reader.seek(SeekFrom::Start(target as u64)).unwrap(); + assert_eq!(read_n(&mut reader, 1000), body[target..target + 1000]); + let deadline = Instant::now() + Duration::from_secs(5); + while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(5)); + } + } + // The allocation, not just the length, is bounded. + let allocated = reader.peak_allocated_bytes(); + assert!( + allocated <= MAX_BUFFERED, + "allocated {allocated} > {MAX_BUFFERED}" + ); +} + +#[test] +fn dropping_the_reader_stops_the_thread() { + let body = noise(20 * MIB, 8); + let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg")); + let mut reader = stream::open(&url).unwrap().reader; + assert_eq!(read_n(&mut reader, 100_000), body[..100_000]); + let exited = reader.exit_probe(); + let deadline = Instant::now() + Duration::from_secs(5); + // Let it fill the read-ahead window (it then pauses). + while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(!exited()); + drop(reader); + let deadline = Instant::now() + Duration::from_secs(5); + while !exited() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(exited(), "downloader thread did not stop"); + + // Also while actively downloading (not paused). + let mut reader = stream::open(&url).unwrap().reader; + let exited = reader.exit_probe(); + let _ = read_n(&mut reader, 10); + drop(reader); + let deadline = Instant::now() + Duration::from_secs(5); + while !exited() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(exited(), "downloader thread did not stop"); +} + +/// The documented calling convention: `open` on Tokio's blocking pool, +/// reads on another plain thread (like the audio thread). +#[test] +fn open_from_spawn_blocking() { + let body = noise(MIB, 9); + let (server, url) = serve(Route::new(body.clone(), "audio/mpeg")); + server.set("/missing.mp3", Route::new("x", "text/plain").status(404)); + let missing = server.url("/missing.mp3"); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .unwrap(); + let (opened, failed) = runtime.block_on(async move { + let opened = tokio::task::spawn_blocking(move || stream::open(&url)) + .await + .unwrap(); + let failed = tokio::task::spawn_blocking(move || stream::open(&missing).is_err()) + .await + .unwrap(); + (opened, failed) + }); + assert!(failed); + let mut reader = opened.unwrap().reader; + let read = std::thread::spawn(move || read_to_end(&mut reader)) + .join() + .unwrap(); + assert_eq!(read, body); + drop(runtime); +} + +#[test] +fn open_fails_for_http_errors() { + let (_server, url) = serve(Route::new("gone", "text/plain").status(404)); + let err = stream::open(&url).err().expect("404 must fail"); + assert!(format!("{err:#}").contains("404"), "{err:#}"); +} + +#[test] +fn errors_do_not_show_the_url_query() { + let server = TestServer::start(); + // The test server matches paths with their query, so this is a 404. + let err = stream::open(&server.url("/private.mp3?token=s3cr3t")) + .err() + .expect("404 must fail"); + let message = format!("{err:#}"); + assert!(message.contains("404"), "{message}"); + assert!(!message.contains("s3cr3t"), "{message}"); + let err = stream::open("file:///etc/passwd").err().expect("not http"); + assert!(format!("{err:#}").contains("scheme"), "{err:#}"); +} + +#[test] +fn http_stream_is_send_and_sync() { + fn check() {} + check::(); +} + +/// A read blocked on a stalled server (inside the audio callback, in the +/// player) returns as soon as its playback is superseded, instead of after +/// `STARVE_TIMEOUT`, and the downloader stops; the stream then reports a +/// failed end. +#[test] +fn a_blocked_read_fails_as_soon_as_its_playback_is_superseded() { + use std::sync::Arc; + use std::sync::atomic::{AtomicBool, Ordering}; + + let body = noise(4 * MIB, 10); + let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(MIB, usize::MAX)); + let superseded = Arc::new(AtomicBool::new(false)); + let flag = Arc::clone(&superseded); + let opened = stream::open_with( + &url, + stream::CancelToken::new(move || flag.load(Ordering::SeqCst)), + ) + .unwrap(); + let probe = opened.probe.clone(); + let mut reader = opened.reader; + let exited = reader.exit_probe(); + assert_eq!(read_n(&mut reader, MIB), body[..MIB]); + assert_eq!(probe.end(), stream::StreamEnd::Complete); + + let reading = std::thread::spawn(move || { + let mut buf = [0u8; 1024]; + let started = Instant::now(); + let result = reader.read(&mut buf); + (result, started.elapsed(), reader) + }); + std::thread::sleep(Duration::from_millis(300)); + superseded.store(true, Ordering::SeqCst); + let (result, waited, mut reader) = reading.join().unwrap(); + let err = result.expect_err("superseded"); + assert!( + err.get_ref() + .is_some_and(|inner| inner.is::()), + "{err}" + ); + assert!( + waited < stream::STARVE_TIMEOUT / 2, + "blocked for {waited:?}" + ); + assert_eq!(probe.end(), stream::StreamEnd::Failed); + // Later reads fail at once, even from buffered data. + reader.seek(SeekFrom::Start(0)).unwrap(); + assert!(reader.read(&mut [0u8; 16]).is_err()); + let deadline = Instant::now() + Duration::from_secs(2); + while !exited() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(exited(), "downloader thread did not stop"); +} diff --git a/src/podcasts/test_server.rs b/src/podcasts/test_server.rs new file mode 100644 index 0000000..65c395c --- /dev/null +++ b/src/podcasts/test_server.rs @@ -0,0 +1,364 @@ +//! Minimal HTTP/1.1 server on 127.0.0.1 for podcast tests (no internet). +//! One request per connection (`Connection: close`). Besides feeds it serves +//! audio like a publisher: `Range` requests (206 / 416) unless disabled, +//! optionally without `Content-Length`, and connections cut after a number +//! of bytes to simulate outages. + +use std::collections::HashMap; +use std::io::{BufRead, BufReader, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +#[derive(Clone)] +pub(super) struct Route { + pub status: u16, + pub body: Arc>, + pub content_type: String, + pub etag: Option, + pub last_modified: Option, + /// `Location` of a redirect status. + pub location: Option, + pub retry_after: Option, + /// Honour `Range` requests with 206 responses. + pub ranges: bool, + /// Send `Content-Length` (and the total in `Content-Range`). + pub send_length: bool, + /// Cut the body after this many bytes (absolute file offset)... + pub cut_after: Option, + /// ...for this many requests; later requests are served in full. + pub fail_times: usize, + /// Wait this long before answering (a hanging server). + pub stall: Option, +} + +impl Route { + pub fn new(body: impl Into>, content_type: &str) -> Route { + Route { + status: 200, + body: Arc::new(body.into()), + content_type: content_type.to_string(), + etag: None, + last_modified: None, + location: None, + retry_after: None, + ranges: true, + send_length: true, + cut_after: None, + fail_times: 0, + stall: None, + } + } + + /// Answer only after `delay`. + pub fn stall(mut self, delay: Duration) -> Route { + self.stall = Some(delay); + self + } + + /// A server that ignores `Range` and always answers 200 with the file. + pub fn no_ranges(mut self) -> Route { + self.ranges = false; + self + } + + /// No `Content-Length`: the client cannot know the file size. + pub fn no_length(mut self) -> Route { + self.send_length = false; + self + } + + /// The first `times` responses stop after byte `after`. + pub fn cut(mut self, after: usize, times: usize) -> Route { + self.cut_after = Some(after); + self.fail_times = times; + self + } + + pub fn redirect(status: u16, location: &str) -> Route { + Route { + status, + location: Some(location.to_string()), + ..Route::new(Vec::new(), "text/plain") + } + } + + pub fn status(mut self, status: u16) -> Route { + self.status = status; + self + } + + pub fn etag(mut self, etag: &str) -> Route { + self.etag = Some(etag.to_string()); + self + } + + pub fn last_modified(mut self, value: &str) -> Route { + self.last_modified = Some(value.to_string()); + self + } + + pub fn retry_after(mut self, value: &str) -> Route { + self.retry_after = Some(value.to_string()); + self + } +} + +/// Headers of one received request (names lowercased). +pub(super) type RequestHeaders = HashMap; + +#[derive(Default)] +struct ServerState { + routes: HashMap, + requests: HashMap>, + /// Cut responses served so far, per path. + cuts: HashMap, +} + +pub(super) struct TestServer { + base: String, + state: Arc>, +} + +impl TestServer { + pub fn start() -> TestServer { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server"); + let base = format!("http://{}", listener.local_addr().expect("local addr")); + let state = Arc::new(Mutex::new(ServerState::default())); + let accept_state = Arc::clone(&state); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(stream) = stream else { continue }; + let state = Arc::clone(&accept_state); + std::thread::spawn(move || handle(stream, &state)); + } + }); + TestServer { base, state } + } + + pub fn url(&self, path: &str) -> String { + format!("{}{path}", self.base) + } + + pub fn set(&self, path: &str, route: Route) { + self.lock().routes.insert(path.to_string(), route); + } + + /// Headers of every request received for `path`, in order. + pub fn requests(&self, path: &str) -> Vec { + self.lock().requests.get(path).cloned().unwrap_or_default() + } + + /// The `Range` header of every request for `path` (`None` if absent). + pub fn ranges(&self, path: &str) -> Vec> { + self.requests(path) + .into_iter() + .map(|headers| headers.get("range").cloned()) + .collect() + } + + fn lock(&self) -> std::sync::MutexGuard<'_, ServerState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } +} + +fn handle(stream: TcpStream, state: &Mutex) { + let _ = stream.set_read_timeout(Some(Duration::from_secs(10))); + let mut reader = BufReader::new(match stream.try_clone() { + Ok(stream) => stream, + Err(_) => return, + }); + let mut request_line = String::new(); + if reader.read_line(&mut request_line).is_err() { + return; + } + let path = request_line + .split_whitespace() + .nth(1) + .unwrap_or("/") + .to_string(); + let mut headers = RequestHeaders::new(); + loop { + let mut line = String::new(); + match reader.read_line(&mut line) { + Ok(0) | Err(_) => return, + Ok(_) => {} + } + let line = line.trim_end(); + if line.is_empty() { + break; + } + if let Some((name, value)) = line.split_once(':') { + headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_string()); + } + } + let (route, cut) = { + let mut state = state.lock().unwrap_or_else(PoisonError::into_inner); + state + .requests + .entry(path.clone()) + .or_default() + .push(headers.clone()); + let route = state.routes.get(&path).cloned(); + let cut = match &route { + Some(route) if route.cut_after.is_some() => { + let served = state.cuts.entry(path.clone()).or_default(); + if *served < route.fail_times { + *served += 1; + route.cut_after + } else { + None + } + } + _ => None, + }; + (route, cut) + }; + let mut out = stream; + if let Some(delay) = route.as_ref().and_then(|route| route.stall) { + std::thread::sleep(delay); + } + let Some(route) = route else { + let _ = out + .write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); + return; + }; + let etag_matches = route + .etag + .as_ref() + .is_some_and(|etag| headers.get("if-none-match") == Some(etag)); + let date_matches = route + .last_modified + .as_ref() + .is_some_and(|date| headers.get("if-modified-since") == Some(date)); + if route.status == 200 && (etag_matches || date_matches) { + let _ = write!( + out, + "HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n" + ); + return; + } + let total = route.body.len(); + let mut status = route.status; + let (mut start, mut end) = (0usize, total); + let mut extra = String::new(); + if route.status == 200 + && route.ranges + && let Some(range) = headers + .get("range") + .and_then(|range| range.strip_prefix("bytes=")) + && let Some((from, to)) = range.split_once('-') + && let Ok(from) = from.trim().parse::() + { + if from >= total { + let _ = write!( + out, + "HTTP/1.1 416 Range Not Satisfiable\r\nContent-Range: bytes */{total}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" + ); + return; + } + status = 206; + start = from; + end = to + .trim() + .parse::() + .map_or(total, |to| (to + 1).min(total)); + let size = if route.send_length { + total.to_string() + } else { + "*".to_string() + }; + extra.push_str(&format!( + "Content-Range: bytes {start}-{}/{size}\r\n", + end - 1 + )); + } + if route.ranges { + extra.push_str("Accept-Ranges: bytes\r\n"); + } + if route.send_length { + extra.push_str(&format!("Content-Length: {}\r\n", end - start)); + } + for (name, value) in [ + ("ETag", &route.etag), + ("Last-Modified", &route.last_modified), + ("Location", &route.location), + ("Retry-After", &route.retry_after), + ] { + if let Some(value) = value { + extra.push_str(&format!("{name}: {value}\r\n")); + } + } + let reason = match status { + 200 => "OK", + 206 => "Partial Content", + 301 => "Moved Permanently", + 302 => "Found", + 307 => "Temporary Redirect", + 308 => "Permanent Redirect", + 404 => "Not Found", + 503 => "Service Unavailable", + _ => "Status", + }; + if write!( + out, + "HTTP/1.1 {status} {reason}\r\nContent-Type: {}\r\n{extra}Connection: close\r\n\r\n", + route.content_type, + ) + .is_err() + { + return; + } + let limit = cut.map_or(end, |cut| cut.clamp(start, end)); + for chunk in route.body[start..limit].chunks(64 * 1024) { + if out.write_all(chunk).is_err() { + return; + } + } + let _ = out.flush(); + let _ = out.shutdown(std::net::Shutdown::Both); +} + +/// Deterministic pseudo-random bytes. +pub(super) fn noise(len: usize, seed: u64) -> Vec { + let mut state = seed | 1; + let mut out = Vec::with_capacity(len); + while out.len() < len { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + out.extend_from_slice(&state.to_le_bytes()); + } + out.truncate(len); + out +} + +/// Sample rate of [`wav`]. +pub(super) const WAV_RATE: u32 = 8_000; + +/// A mono 16-bit PCM WAV file of `seconds` (8 kHz). Every sample of second +/// `n` has the value `n * 40` (up to 13 minutes), so a decoded sample tells where playback +/// is. +pub(super) fn wav(seconds: u32) -> Vec { + const RATE: u32 = WAV_RATE; + let samples = RATE * seconds; + let data_len = samples * 2; + let mut out = Vec::with_capacity(44 + data_len as usize); + out.extend_from_slice(b"RIFF"); + out.extend_from_slice(&(36 + data_len).to_le_bytes()); + out.extend_from_slice(b"WAVEfmt "); + out.extend_from_slice(&16u32.to_le_bytes()); + out.extend_from_slice(&1u16.to_le_bytes()); + out.extend_from_slice(&1u16.to_le_bytes()); + out.extend_from_slice(&RATE.to_le_bytes()); + out.extend_from_slice(&(RATE * 2).to_le_bytes()); + out.extend_from_slice(&2u16.to_le_bytes()); + out.extend_from_slice(&16u16.to_le_bytes()); + out.extend_from_slice(b"data"); + out.extend_from_slice(&data_len.to_le_bytes()); + for n in 0..samples { + let second = (n / RATE) as i16; + out.extend_from_slice(&second.saturating_mul(40).to_le_bytes()); + } + out +} diff --git a/src/podcasts/tests.rs b/src/podcasts/tests.rs new file mode 100644 index 0000000..482d19d --- /dev/null +++ b/src/podcasts/tests.rs @@ -0,0 +1,994 @@ +//! Store opening, subscribing and refreshing against a local HTTP server. +//! No internet, no real user directories: every test uses a temp dir. + +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +use anyhow::Result; +use furumi_library::podcasts::{ + EpisodeQuery, OpStamp, PodcastListFilter, PodcastLocalOp, PodcastStore, StampSource, +}; +use music_dht::device_sync::{SyncOpPayload, SyncOpWire}; +use music_dht::podcast::{FeedUrl, PodcastId}; + +use super::OpLog; +use super::fetch::{FETCH_DEADLINE, MAX_COVER_BYTES, MAX_FEED_BYTES, MAX_REDIRECTS, Validators}; +use super::refresh::{ + FAILURE_BACKOFF_BASE, FAILURE_BACKOFF_MAX, REFRESH_INTERVAL, Services, + next_check_after_failure, next_check_after_success, refresh, subscribe, +}; +use super::test_server::{Route, TestServer}; + +pub(super) const NOW: i64 = 1_700_000_000_000; + +/// In-memory sync log: hands out stamps and keeps the appended operations. +#[derive(Default)] +pub(super) struct MemoryLog { + ops: Mutex>, +} + +struct MemoryStamps { + next_seq: i64, + reserved: Vec, +} + +impl StampSource for MemoryStamps { + fn device_id(&self) -> &str { + "test-device" + } + + fn next_stamp(&mut self) -> Result { + let seq = self.next_seq + self.reserved.len() as i64; + self.reserved.push(seq); + Ok(OpStamp::new( + format!("test-device:{seq}"), + "test-device", + NOW + seq, + )) + } +} + +impl OpLog for MemoryLog { + fn record( + &self, + action: &mut dyn FnMut(&mut dyn StampSource) -> Result>, + ) -> Result<()> { + let mut ops = self.ops.lock().unwrap_or_else(PoisonError::into_inner); + let mut stamps = MemoryStamps { + next_seq: ops.len() as i64 + 1, + reserved: Vec::new(), + }; + for (op, seq) in action(&mut stamps)?.into_iter().zip(stamps.reserved) { + ops.push(op.into_wire(seq)); + } + Ok(()) + } +} + +impl MemoryLog { + pub(super) fn kinds(&self) -> Vec { + self.ops + .lock() + .unwrap_or_else(PoisonError::into_inner) + .iter() + .map(|op| { + serde_json::to_value(&op.payload).unwrap()["kind"] + .as_str() + .unwrap() + .to_string() + }) + .collect() + } +} + +pub(super) struct Fixture { + _dir: tempfile::TempDir, + pub(super) services: Services, + pub(super) log: Arc, + pub(super) server: TestServer, +} + +pub(super) fn fixture() -> Fixture { + let dir = tempfile::tempdir().unwrap(); + let log = Arc::new(MemoryLog::default()); + let services = super::open_services( + &dir.path().join("podcasts.sqlite3"), + dir.path().join("covers"), + log.clone(), + ) + .unwrap(); + Fixture { + _dir: dir, + services, + log, + server: TestServer::start(), + } +} + +pub(super) fn rss(items: &[(&str, &str)]) -> Vec { + let items: String = items + .iter() + .map(|(guid, title)| { + format!( + "{title}{guid}\ + Sat, 15 Jun 2024 10:00:00 GMT\ + 45:00\ + " + ) + }) + .collect(); + format!( + "\ + Test ShowHost{items}" + ) + .into_bytes() +} + +fn episode_count(store: &PodcastStore, id: &PodcastId) -> usize { + store + .episodes(&EpisodeQuery::new(id.clone())) + .unwrap() + .len() +} + +#[tokio::test] +async fn subscribe_then_conditional_refresh_uses_validators() { + let f = fixture(); + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ) + .etag("\"v1\"") + .last_modified("Sat, 15 Jun 2024 10:00:00 GMT"), + ); + let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW) + .await + .unwrap(); + assert!(!report.already_subscribed); + assert_eq!(report.new_episodes, 2); + assert_eq!(report.title, "Test Show"); + assert_eq!(f.log.kinds(), ["podcast_subscription_set"]); + let store = &f.services.store; + let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap(); + assert_eq!(rows.len(), 1); + assert!(!rows[0].shared, "a new subscription is private"); + let state = store.fetch_state(&report.podcast_id).unwrap(); + assert_eq!(state.etag.as_deref(), Some("\"v1\"")); + assert_eq!(state.next_check_at_ms, Some(next_check_after_success(NOW))); + let first = &f.server.requests("/feed.xml")[0]; + assert!(!first.contains_key("if-none-match"), "{first:?}"); + assert!( + first + .get("user-agent") + .is_some_and(|agent| agent.starts_with("furumi/")), + "{first:?}" + ); + + // Unchanged: 304, nothing re-parsed, validators sent. + let url = rows[0].meta.feed_url.clone(); + let checked = refresh(&f.services, &report.podcast_id, &url, NOW + 1) + .await + .unwrap(); + assert!(checked.not_modified); + let second = &f.server.requests("/feed.xml")[1]; + assert_eq!( + second.get("if-none-match").map(String::as_str), + Some("\"v1\"") + ); + assert_eq!( + second.get("if-modified-since").map(String::as_str), + Some("Sat, 15 Jun 2024 10:00:00 GMT") + ); + + // Changed: a new episode arrives, the known ones are not duplicated. + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot (remastered)"), ("b", "Second"), ("c", "Third")]), + "application/rss+xml", + ) + .etag("\"v2\""), + ); + let changed = refresh(&f.services, &report.podcast_id, &url, NOW + 2) + .await + .unwrap(); + assert!(!changed.not_modified); + assert_eq!(changed.new_episodes, 1); + assert_eq!(changed.updated_episodes, 1); + assert_eq!(episode_count(store, &report.podcast_id), 3); + // A refresh never writes personal state: no new operation. + assert_eq!(f.log.kinds(), ["podcast_subscription_set"]); + + // Subscribing again is recognised without another fetch. + let requests = f.server.requests("/feed.xml").len(); + let again = subscribe(&f.services, &f.server.url("/feed.xml"), NOW + 3) + .await + .unwrap(); + assert!(again.already_subscribed); + assert_eq!(again.podcast_id, report.podcast_id); + assert_eq!(f.server.requests("/feed.xml").len(), requests); +} + +#[tokio::test] +async fn permanent_redirect_becomes_an_alias_and_the_new_fetch_url() { + let f = fixture(); + f.server + .set("/old.xml", Route::redirect(301, &f.server.url("/new.xml"))); + f.server.set( + "/new.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let old = FeedUrl::parse(&f.server.url("/old.xml")).unwrap(); + let new = FeedUrl::parse(&f.server.url("/new.xml")).unwrap(); + let report = subscribe(&f.services, old.as_str(), NOW).await.unwrap(); + // The id comes from the URL of the first subscription. + assert_eq!(report.podcast_id, PodcastId::from_feed_url(&old)); + assert_eq!( + f.log.kinds(), + ["podcast_subscription_set", "podcast_feed_aliases_added"] + ); + let store = &f.services.store; + assert!( + store + .feed_aliases(&report.podcast_id) + .unwrap() + .contains(&new) + ); + let summary = store.podcast(&report.podcast_id).unwrap().unwrap(); + assert_eq!(summary.meta.feed_url, new, "fetched from the new URL now"); + assert_eq!(store.resolve_podcast_id(&new).unwrap(), report.podcast_id); + + // The next refresh goes straight to the new URL; no new alias op. + let checked = refresh(&f.services, &report.podcast_id, &new, NOW + 1) + .await + .unwrap(); + assert!(checked.moved_to.is_none()); + assert_eq!(f.server.requests("/old.xml").len(), 1); + assert_eq!(f.log.kinds().len(), 2); + + // Subscribing through the new URL finds the same podcast. + let again = subscribe(&f.services, new.as_str(), NOW + 2).await.unwrap(); + assert!(again.already_subscribed); + assert_eq!(again.podcast_id, report.podcast_id); +} + +/// A feed that moved (301) to a host that answers through a temporary +/// redirect (302, a CDN or tracker) moves to the permanent target only; the +/// following checks start there and are conditional again. +#[tokio::test] +async fn a_permanent_move_behind_a_temporary_redirect_still_moves_the_feed() { + let f = fixture(); + f.server.set( + "/old.xml", + Route::redirect(301, &f.server.url("/moved.xml")), + ); + f.server + .set("/moved.xml", Route::redirect(302, "/cdn/feed.xml")); + f.server.set( + "/cdn/feed.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml").etag("\"v1\""), + ); + let old = FeedUrl::parse(&f.server.url("/old.xml")).unwrap(); + let moved = FeedUrl::parse(&f.server.url("/moved.xml")).unwrap(); + let report = subscribe(&f.services, old.as_str(), NOW).await.unwrap(); + let store = &f.services.store; + let summary = store.podcast(&report.podcast_id).unwrap().unwrap(); + assert_eq!( + summary.meta.feed_url, moved, + "the permanent target, not the CDN" + ); + assert!( + store + .feed_aliases(&report.podcast_id) + .unwrap() + .contains(&moved) + ); + // Relative enclosures resolve against the document's own address. + let episodes = store + .episodes(&EpisodeQuery::new(report.podcast_id.clone())) + .unwrap(); + assert_eq!(episodes[0].episode.audio_url, f.server.url("/audio/a.mp3")); + + // The next check starts at the new address and keeps its validators + // through the temporary hop. + let checked = refresh(&f.services, &report.podcast_id, &moved, NOW + 1) + .await + .unwrap(); + assert!(checked.not_modified); + assert_eq!(f.server.requests("/old.xml").len(), 1); + let cdn = f.server.requests("/cdn/feed.xml"); + assert_eq!(cdn.len(), 2); + assert_eq!( + cdn[1].get("if-none-match").map(String::as_str), + Some("\"v1\"") + ); + + // A temporary hop first never moves the feed, whatever follows it. + f.server.set( + "/tracked.xml", + Route::redirect(302, &f.server.url("/old.xml")), + ); + let tracked = FeedUrl::parse(&f.server.url("/tracked.xml")).unwrap(); + let other = subscribe(&f.services, tracked.as_str(), NOW + 2) + .await + .unwrap(); + let summary = store.podcast(&other.podcast_id).unwrap().unwrap(); + assert_eq!(summary.meta.feed_url, tracked); +} + +#[tokio::test] +async fn temporary_redirect_keeps_the_feed_url() { + let f = fixture(); + f.server + .set("/tracked.xml", Route::redirect(302, "/real.xml")); + f.server.set( + "/real.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let tracked = FeedUrl::parse(&f.server.url("/tracked.xml")).unwrap(); + let report = subscribe(&f.services, tracked.as_str(), NOW).await.unwrap(); + assert_eq!(f.log.kinds(), ["podcast_subscription_set"]); + let summary = f + .services + .store + .podcast(&report.podcast_id) + .unwrap() + .unwrap(); + assert_eq!(summary.meta.feed_url, tracked); + // Episode URLs resolve against where the document was served from. + let episodes = f + .services + .store + .episodes(&EpisodeQuery::new(report.podcast_id.clone())) + .unwrap(); + assert_eq!(episodes[0].episode.audio_url, f.server.url("/audio/a.mp3")); +} + +#[tokio::test] +async fn malformed_feed_and_server_errors_back_off_and_keep_the_catalog() { + let f = fixture(); + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ), + ); + let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW) + .await + .unwrap(); + let id = report.podcast_id; + let url = FeedUrl::parse(&f.server.url("/feed.xml")).unwrap(); + + f.server.set( + "/feed.xml", + Route::new("Moved to our app!", "text/html"), + ); + let err = refresh(&f.services, &id, &url, NOW + 1).await.unwrap_err(); + assert!(format!("{err:#}").contains("unusable feed"), "{err:#}"); + let store = &f.services.store; + let state = store.fetch_state(&id).unwrap(); + assert_eq!(state.consecutive_failures, 1); + assert!( + state + .last_error + .as_deref() + .unwrap() + .contains("unusable feed") + ); + assert_eq!( + state.next_check_at_ms, + Some(next_check_after_failure(NOW + 1, 1, None)) + ); + assert_eq!(episode_count(store, &id), 2, "an error keeps the catalog"); + + f.server.set( + "/feed.xml", + Route::new("busy", "text/plain") + .status(503) + .retry_after("7200"), + ); + let err = refresh(&f.services, &id, &url, NOW + 2).await.unwrap_err(); + assert!(format!("{err:#}").contains("HTTP 503"), "{err:#}"); + let state = store.fetch_state(&id).unwrap(); + assert_eq!(state.consecutive_failures, 2); + assert_eq!(state.next_check_at_ms, Some(NOW + 2 + 7_200_000)); + assert_eq!(episode_count(store, &id), 2); + // A failing feed is not due before its back-off expires. + assert!( + store + .podcasts_due_for_refresh(NOW + 3, 10) + .unwrap() + .is_empty() + ); + assert_eq!( + store + .podcasts_due_for_refresh(NOW + 2 + 7_200_000, 10) + .unwrap() + .len(), + 1 + ); + + // Recovery clears the error. + f.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ), + ); + refresh(&f.services, &id, &url, NOW + 3).await.unwrap(); + let state = store.fetch_state(&id).unwrap(); + assert_eq!(state.consecutive_failures, 0); + assert!(state.last_error.is_none()); +} + +#[tokio::test] +async fn subscribing_to_a_non_feed_stores_nothing() { + let f = fixture(); + f.server + .set("/page", Route::new("hello", "text/html")); + let err = subscribe(&f.services, &f.server.url("/page"), NOW) + .await + .unwrap_err(); + assert!(format!("{err:#}").contains("unusable feed"), "{err:#}"); + let err = subscribe(&f.services, &f.server.url("/missing"), NOW) + .await + .unwrap_err(); + assert!(format!("{err:#}").contains("HTTP 404"), "{err:#}"); + let err = subscribe(&f.services, "ftp://example.test/feed", NOW) + .await + .unwrap_err(); + assert!(!format!("{err:#}").is_empty()); + assert!( + f.services + .store + .list_podcasts(PodcastListFilter::All) + .unwrap() + .is_empty() + ); + assert!(f.log.kinds().is_empty()); +} + +#[tokio::test] +async fn unsubscribe_then_resubscribe_keeps_the_podcast_id() { + let f = fixture(); + f.server.set( + "/feed.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let first = subscribe(&f.services, &f.server.url("/feed.xml"), NOW) + .await + .unwrap(); + let store = Arc::clone(&f.services.store); + let id = first.podcast_id.clone(); + f.log + .record(&mut |stamps| store.unsubscribe(stamps, &id)) + .unwrap(); + assert!( + store + .list_podcasts(PodcastListFilter::Subscribed) + .unwrap() + .is_empty() + ); + let again = subscribe(&f.services, &f.server.url("/feed.xml"), NOW + 1) + .await + .unwrap(); + assert!(!again.already_subscribed); + assert_eq!(again.podcast_id, first.podcast_id); + assert_eq!( + f.log.kinds(), + [ + "podcast_subscription_set", + "podcast_subscription_set", + "podcast_share_set", + "podcast_subscription_set" + ] + ); + let ops = f.log.ops.lock().unwrap(); + assert!(matches!( + ops.last().map(|op| &op.payload), + Some(SyncOpPayload::PodcastSubscriptionSet { + subscribed: true, + .. + }) + )); +} + +#[test] +fn back_off_doubles_and_honours_retry_after() { + let base = FAILURE_BACKOFF_BASE.as_millis() as i64; + assert_eq!(next_check_after_failure(0, 1, None), base); + assert_eq!(next_check_after_failure(0, 2, None), 2 * base); + assert_eq!(next_check_after_failure(0, 4, None), 8 * base); + let max = FAILURE_BACKOFF_MAX.as_millis() as i64; + assert_eq!(next_check_after_failure(0, 40, None), max); + assert_eq!( + next_check_after_failure(0, 1, Some(Duration::from_secs(3_600))), + 3_600_000 + ); + assert_eq!( + next_check_after_failure(0, 1, Some(Duration::from_secs(10 * 86_400))), + max + ); + assert_eq!( + next_check_after_success(0), + REFRESH_INTERVAL.as_millis() as i64 + ); +} + +/// The prototype podcast build's schema (user_version 1), verbatim. +const PROTOTYPE_SCHEMA: &str = " + CREATE TABLE IF NOT EXISTS podcasts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + feed_url TEXT NOT NULL UNIQUE, + title TEXT NOT NULL, + author TEXT, + description TEXT, + link TEXT, + image_url TEXT, + cover_url TEXT, + cover_path TEXT, + etag TEXT, + last_modified TEXT, + added_at INTEGER NOT NULL, + last_checked_at INTEGER, + last_error TEXT + ); + CREATE TABLE IF NOT EXISTS podcast_episodes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + podcast_id INTEGER NOT NULL REFERENCES podcasts(id) ON DELETE CASCADE, + guid TEXT NOT NULL, + title TEXT NOT NULL, + description TEXT, + published_at INTEGER, + duration_seconds REAL, + audio_url TEXT NOT NULL, + mime_type TEXT, + byte_len INTEGER, + link TEXT, + image_url TEXT, + first_seen_at INTEGER NOT NULL, + UNIQUE (podcast_id, guid) + ); + CREATE INDEX IF NOT EXISTS podcast_episodes_by_date + ON podcast_episodes (podcast_id, published_at DESC, id DESC); +"; + +#[test] +fn prototype_database_is_migrated_in_place() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE); + { + let conn = rusqlite::Connection::open(&path).unwrap(); + conn.execute_batch(PROTOTYPE_SCHEMA).unwrap(); + conn.execute( + "INSERT INTO podcasts (feed_url, title, etag, added_at, last_checked_at) + VALUES ('https://example.test/feed.xml', 'Old Show', '\"e1\"', 1700000000, 1700000100)", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO podcast_episodes + (podcast_id, guid, title, published_at, duration_seconds, audio_url, first_seen_at) + VALUES (1, 'ep-1', 'Old Pilot', 1700000000, 1800.0, + 'https://example.test/1.mp3', 1700000000)", + [], + ) + .unwrap(); + conn.pragma_update(None, "user_version", 1).unwrap(); + } + let services = super::open_services( + &path, + dir.path().join("covers"), + Arc::new(MemoryLog::default()), + ) + .unwrap(); + let store = &services.store; + assert_eq!( + store.schema_version().unwrap(), + furumi_library::podcasts::PODCAST_SCHEMA_VERSION + ); + let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap(); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].meta.title, "Old Show"); + assert_eq!(rows[0].episode_count, 1); + let expected = + PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/feed.xml").unwrap()); + assert_eq!(rows[0].podcast_id, expected); + // The prototype's validators are dropped: the first check parses the + // feed (`imported_podcasts_get_their_seasons_on_the_first_check`). + let fetch = store.fetch_state(&expected).unwrap(); + assert_eq!(fetch.etag, None); + assert_eq!(fetch.last_success_at_ms, Some(1_700_000_100_000)); + assert_eq!(fetch.next_check_at_ms, None, "due at once"); + drop(services); + + // The original rows are kept, renamed. + let conn = rusqlite::Connection::open(&path).unwrap(); + let legacy: i64 = conn + .query_row("SELECT COUNT(*) FROM legacy_podcast_episodes", [], |row| { + row.get(0) + }) + .unwrap(); + assert_eq!(legacy, 1); +} + +#[test] +fn foreign_or_newer_databases_are_refused_unchanged() { + let dir = tempfile::tempdir().unwrap(); + let log: Arc = Arc::new(MemoryLog::default()); + + let foreign = dir.path().join("foreign.sqlite3"); + { + let conn = rusqlite::Connection::open(&foreign).unwrap(); + conn.execute_batch("CREATE TABLE tracks (id INTEGER PRIMARY KEY, title TEXT);") + .unwrap(); + } + let newer = dir.path().join("newer.sqlite3"); + { + let conn = rusqlite::Connection::open(&newer).unwrap(); + conn.pragma_update(None, "user_version", 99).unwrap(); + } + let garbage = dir.path().join("garbage.sqlite3"); + std::fs::write( + &garbage, + b"this is not a database at all, just some text bytes", + ) + .unwrap(); + + for (path, expected) in [ + (&foreign, "not a Furumi podcast database"), + (&newer, "newer than this Furumi supports"), + (&garbage, ""), + ] { + let before = std::fs::read(path).unwrap(); + let Err(err) = super::open_services(path, dir.path().join("covers"), Arc::clone(&log)) + else { + panic!("{} opened", path.display()); + }; + let message = super::describe_open_error(&err); + assert!(message.contains(expected), "{message}"); + assert_eq!(std::fs::read(path).unwrap(), before, "{}", path.display()); + } +} + +/// A prototype database knew no seasons or episode numbers. Its stored +/// `ETag` would make the first check "304 Not Modified" forever for a feed +/// that no longer changes, so the seasons would never appear. +#[tokio::test] +async fn imported_podcasts_get_their_seasons_on_the_first_check() { + let dir = tempfile::tempdir().unwrap(); + let server = TestServer::start(); + let feed = "\ + Old ShowOld Pilotep-1\ + Sat, 15 Jun 2024 10:00:00 GMT\ + 11\ + \ + "; + // The server answers the prototype's validator with 304. + server.set( + "/feed.xml", + Route::new(feed, "application/rss+xml").etag("\"e1\""), + ); + let feed_url = server.url("/feed.xml"); + let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE); + { + let conn = rusqlite::Connection::open(&path).unwrap(); + conn.execute_batch(PROTOTYPE_SCHEMA).unwrap(); + conn.execute( + "INSERT INTO podcasts (feed_url, title, etag, last_modified, added_at, last_checked_at) + VALUES (?1, 'Old Show', '\"e1\"', 'Sat, 15 Jun 2024 10:00:00 GMT', + 1700000000, 1700000100)", + [&feed_url], + ) + .unwrap(); + conn.execute( + "INSERT INTO podcast_episodes + (podcast_id, guid, title, published_at, duration_seconds, audio_url, first_seen_at) + VALUES (1, 'ep-1', 'Old Pilot', 1718445600, 1800.0, ?1, 1700000000)", + [server.url("/audio/1.mp3")], + ) + .unwrap(); + conn.pragma_update(None, "user_version", 1).unwrap(); + } + let services = super::open_services( + &path, + dir.path().join("covers"), + Arc::new(MemoryLog::default()), + ) + .unwrap(); + let store = Arc::clone(&services.store); + let id = PodcastId::from_feed_url(&FeedUrl::parse(&feed_url).unwrap()); + assert!( + store + .seasons(&id) + .unwrap() + .iter() + .all(|s| s.season.is_none()), + "the prototype had no seasons" + ); + let due = store.podcasts_due_for_refresh(NOW, 16).unwrap(); + assert_eq!(due.len(), 1); + let report = refresh(&services, &id, &due[0].1, NOW).await.unwrap(); + assert!(!report.not_modified); + let requests = server.requests("/feed.xml"); + assert_eq!(requests.len(), 1); + assert!(!requests[0].contains_key("if-none-match")); + assert!(!requests[0].contains_key("if-modified-since")); + let seasons = store.seasons(&id).unwrap(); + assert_eq!( + seasons.iter().map(|s| s.season).collect::>(), + [Some(1)] + ); + // From then on the checks are conditional again. + let report = refresh(&services, &id, &due[0].1, NOW + 1).await.unwrap(); + assert!(report.not_modified); + drop(services); + + // A store that was already migrated keeps its validators on reopen. + let services = super::open_services( + &path, + dir.path().join("covers"), + Arc::new(MemoryLog::default()), + ) + .unwrap(); + assert_eq!( + services.store.fetch_state(&id).unwrap().etag.as_deref(), + Some("\"e1\"") + ); +} + +/// The import commits inside `PodcastStore::open` and the validators are +/// dropped afterwards. A start that stopped in between (killed, or a failed +/// write) finishes the job at the next start, and the imported back-off is +/// kept. +#[test] +fn dropping_prototype_validators_resumes_after_an_interrupted_start() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE); + { + let conn = rusqlite::Connection::open(&path).unwrap(); + conn.execute_batch(PROTOTYPE_SCHEMA).unwrap(); + conn.execute( + "INSERT INTO podcasts (feed_url, title, etag, added_at, last_checked_at) + VALUES ('https://example.test/ok.xml', 'Fine Show', '\"e1\"', 1700000000, 1700000100)", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO podcasts + (feed_url, title, last_modified, added_at, last_checked_at, last_error) + VALUES ('https://example.test/broken.xml', 'Broken Show', + 'Sat, 15 Jun 2024 10:00:00 GMT', 1700000000, 1700000200, 'HTTP 500')", + [], + ) + .unwrap(); + conn.pragma_update(None, "user_version", 1).unwrap(); + } + let ok = PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/ok.xml").unwrap()); + let broken = + PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/broken.xml").unwrap()); + + // The first start: the marker is written, then the import commits, and + // the process stops before the validators are dropped. + let marker = super::validators_marker(&path); + assert!(super::is_prototype_database(&path)); + std::fs::write(&marker, b"").unwrap(); + drop(PodcastStore::open(&path).unwrap()); + assert!(!super::is_prototype_database(&path), "migrated"); + + // The next start finishes the job. + let services = super::open_services( + &path, + dir.path().join("covers"), + Arc::new(MemoryLog::default()), + ) + .unwrap(); + let store = &services.store; + let fetch = store.fetch_state(&ok).unwrap(); + assert_eq!(fetch.etag, None); + assert_eq!(fetch.last_error, None); + let fetch = store.fetch_state(&broken).unwrap(); + assert_eq!(fetch.last_modified, None); + assert_eq!(fetch.last_error.as_deref(), Some("HTTP 500")); + assert_eq!( + fetch.consecutive_failures, 1, + "the imported back-off is kept" + ); + assert_eq!(fetch.checked_at_ms, Some(1_700_000_200_000)); + assert!(!marker.exists(), "done once"); + + // Validators of later checks are kept on every start after that. + store + .record_fetch_success(&ok, NOW, Some("\"e2\""), None, None) + .unwrap(); + drop(services); + let services = super::open_services( + &path, + dir.path().join("covers"), + Arc::new(MemoryLog::default()), + ) + .unwrap(); + assert_eq!( + services.store.fetch_state(&ok).unwrap().etag.as_deref(), + Some("\"e2\"") + ); +} + +fn feed_url(f: &Fixture, path: &str) -> FeedUrl { + FeedUrl::parse(&f.server.url(path)).unwrap() +} + +#[tokio::test] +async fn feeds_over_the_size_limit_are_refused_with_or_without_a_length() { + let f = fixture(); + f.server.set( + "/feed.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let url = feed_url(&f, "/feed.xml"); + let id = subscribe(&f.services, url.as_str(), NOW) + .await + .unwrap() + .podcast_id; + let mut huge = rss(&[("a", "Pilot"), ("b", "Second")]); + huge.resize(MAX_FEED_BYTES + 1, b' '); + let store = &f.services.store; + for (attempt, route) in [ + Route::new(huge.clone(), "application/rss+xml"), + Route::new(huge, "application/rss+xml").no_length(), + ] + .into_iter() + .enumerate() + { + let failures = attempt as u32 + 1; + f.server.set("/feed.xml", route); + let now = NOW + i64::from(failures); + let err = refresh(&f.services, &id, &url, now).await.unwrap_err(); + assert!(format!("{err:#}").contains("too large"), "{err:#}"); + let state = store.fetch_state(&id).unwrap(); + assert_eq!(state.consecutive_failures, failures); + assert!(state.last_error.unwrap().contains("too large")); + assert_eq!( + state.next_check_at_ms, + Some(next_check_after_failure(now, failures, None)) + ); + assert_eq!(episode_count(store, &id), 1, "the catalog is kept"); + } +} + +#[tokio::test] +async fn redirect_chains_are_bounded_and_lead_only_to_http() { + let f = fixture(); + let fetch = |path: &'static str| { + let url = feed_url(&f, path); + let fetcher = std::sync::Arc::clone(&f.services.fetcher); + async move { fetcher.fetch_feed(&url, Validators::default()).await } + }; + for n in 0..=MAX_REDIRECTS { + f.server.set( + &format!("/r{n}.xml"), + Route::redirect(302, &format!("/r{}.xml", n + 1)), + ); + } + f.server.set( + &format!("/r{}.xml", MAX_REDIRECTS + 1), + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + // MAX_REDIRECTS hops are followed, one more is refused. + let followed = fetch("/r1.xml").await.unwrap(); + assert_eq!(followed.redirects, MAX_REDIRECTS); + let err = fetch("/r0.xml").await.unwrap_err(); + assert!(format!("{err:#}").contains("too many redirects"), "{err:#}"); + // A loop ends the same way. + f.server.set("/a.xml", Route::redirect(301, "/b.xml")); + f.server.set("/b.xml", Route::redirect(301, "/a.xml")); + let err = fetch("/a.xml").await.unwrap_err(); + assert!(format!("{err:#}").contains("too many redirects"), "{err:#}"); + assert_eq!(f.server.requests("/a.xml").len(), MAX_REDIRECTS / 2 + 1); + // Only http(s) is followed. + f.server.set( + "/ftp.xml", + Route::redirect(302, "ftp://example.test/feed.xml"), + ); + let err = fetch("/ftp.xml").await.unwrap_err(); + assert!( + format!("{err:#}").contains("unsupported URL scheme"), + "{err:#}" + ); + // Subscribing through any of them stores nothing. + for path in ["/r0.xml", "/a.xml", "/ftp.xml"] { + assert!( + subscribe(&f.services, &f.server.url(path), NOW) + .await + .is_err() + ); + } + assert!( + f.services + .store + .list_podcasts(PodcastListFilter::All) + .unwrap() + .is_empty() + ); +} + +#[tokio::test] +async fn a_hanging_server_fails_the_check_at_the_deadline() { + let f = fixture(); + f.server.set( + "/feed.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let url = feed_url(&f, "/feed.xml"); + let id = subscribe(&f.services, url.as_str(), NOW) + .await + .unwrap() + .podcast_id; + f.server.set( + "/feed.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml") + .stall(FETCH_DEADLINE + Duration::from_secs(5)), + ); + let started = std::time::Instant::now(); + let err = refresh(&f.services, &id, &url, NOW + 1).await.unwrap_err(); + assert!(format!("{err:#}").contains("took longer than"), "{err:#}"); + assert!(started.elapsed() < FETCH_DEADLINE + Duration::from_secs(2)); + let state = f.services.store.fetch_state(&id).unwrap(); + assert_eq!(state.consecutive_failures, 1); + assert_eq!(episode_count(&f.services.store, &id), 1); +} + +#[tokio::test] +async fn covers_must_be_images_within_the_limit_and_are_written_whole() { + let f = fixture(); + let png = { + let mut out = std::io::Cursor::new(Vec::new()); + image::RgbImage::new(2, 2) + .write_to(&mut out, image::ImageFormat::Png) + .unwrap(); + out.into_inner() + }; + f.server + .set("/cover.png", Route::new(png.clone(), "image/png")); + f.server.set( + "/page.png", + Route::new("not an image", "image/png"), + ); + f.server.set( + "/huge.png", + Route::new(vec![0u8; MAX_COVER_BYTES + 1], "image/png"), + ); + let id = PodcastId::from_feed_url(&feed_url(&f, "/feed.xml")); + let fetcher = &f.services.fetcher; + let path = fetcher + .fetch_cover(&id, &f.server.url("/cover.png")) + .await + .unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), png); + assert_eq!(path.extension().unwrap(), "png"); + let err = fetcher + .fetch_cover(&id, &f.server.url("/page.png")) + .await + .unwrap_err(); + assert!( + format!("{err:#}").contains("not a supported image"), + "{err:#}" + ); + let err = fetcher + .fetch_cover(&id, &f.server.url("/huge.png")) + .await + .unwrap_err(); + assert!(format!("{err:#}").contains("too large"), "{err:#}"); + // Only the one finished file: nothing partial, no temporary files. + let files: Vec<_> = std::fs::read_dir(path.parent().unwrap()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .collect(); + assert_eq!(files, [path]); +} diff --git a/src/share.rs b/src/share.rs index bd753a8..9f8344d 100644 --- a/src/share.rs +++ b/src/share.rs @@ -1,6 +1,11 @@ use crate::library::models::TrackItem; +/// Content id of a music track. Podcast episodes have none: they are not +/// content-addressed and never shared as `frid://` track links. pub fn track_content_id(track: &TrackItem) -> Option { + if track.is_podcast() { + return None; + } track .fed .as_ref() @@ -14,6 +19,9 @@ pub fn track_can_share(track: &TrackItem) -> bool { } pub fn track_share_link(track: &TrackItem) -> Option { + if track.is_podcast() { + return None; + } let content_id = track_content_id(track).or_else(|| { (!track.file_path.trim().is_empty()) .then(|| crate::library::audio_content_id(&track.file_path)) @@ -154,6 +162,7 @@ mod tests { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; assert_eq!( @@ -231,6 +240,7 @@ mod tests { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }; let link = track_share_link(&track).expect("link"); let parsed = parse_frid_link(&link).expect("parses back"); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 1b199fa..96777cb 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -134,33 +134,126 @@ mod tests { assert_eq!((cell.fg, cell.bg), (highlight.fg, highlight.bg)); } + use crate::app::podcast_fixtures; + #[test] - fn podcasts_tab_shows_placeholder() { + fn podcasts_tab_reports_opening_and_failed_store() { let mut state = AppState::default(); state.active_tab = Tab::Podcasts; let screen = render(&state, 100, 12); - assert!(screen.contains(podcasts::UNAVAILABLE), "{screen}"); - } + assert!(screen.contains("opening the podcast database"), "{screen}"); - #[test] - fn podcasts_placeholder_wraps_without_losing_text_on_narrow_terminals() { - let mut state = AppState::default(); - state.active_tab = Tab::Podcasts; - for width in [60, 40] { - let screen = render(&state, width, 14); + state.podcasts.store = crate::app::state::PodcastStoreStatus::Failed( + "podcast database schema v9 is newer than this Furumi supports".into(), + ); + for width in [100, 50] { + let screen = render(&state, width, 16); let words: Vec<&str> = screen .split(|c: char| c.is_whitespace() || c == '│') .filter(|word| !word.is_empty()) .collect(); - for word in ["available", "progress", "appear", "here."] { - assert!( - words.contains(&word), - "{word} missing at {width}:\n{screen}" - ); + for word in ["unavailable", "v9", "newer", "unchanged."] { + assert!(words.contains(&word), "{word} at {width}:\n{screen}"); } } } + #[test] + fn podcasts_list_shows_actions_counts_and_errors() { + let state = podcast_fixtures::ready_state(Vec::new()); + let screen = render(&state, 100, 14); + assert!(screen.contains("+ Add podcast (RSS feed URL)"), "{screen}"); + assert!(screen.contains("Check all podcasts"), "{screen}"); + assert!(screen.contains("No subscriptions yet"), "{screen}"); + + let mut row = podcast_fixtures::row("Test Show", 2, 3, 0); + row.fetch.last_error = Some("HTTP 503 Service Unavailable".into()); + row.fetch.consecutive_failures = 2; + let mut state = podcast_fixtures::ready_state(vec![row]); + state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS; + // Wide: side panel with details; narrow: status below the list. + for width in [110, 60] { + let screen = render(&state, width, 30); + assert!(screen.contains("! Test Show"), "{screen}"); + assert!(screen.contains("2 unplayed · 3 ep"), "{screen}"); + assert!(screen.contains("last check failed"), "{screen}"); + assert!(screen.contains("503"), "{screen}"); + } + } + + #[test] + fn podcast_seasons_and_episodes_render_progress() { + use music_dht::podcast::ProgressStatus; + + let mut state = + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 1, 5, 1)]); + state.podcasts.opened = Some(crate::app::state::OpenedPodcast { + podcast_id: podcast_fixtures::podcast_id(), + season_cursor: Some(0), + season: None, + episode_cursor: 0, + }); + state.podcasts.seasons = Some(( + podcast_fixtures::podcast_id(), + Loadable::Ready(podcast_fixtures::seasons()), + )); + let screen = render(&state, 100, 14); + assert!(screen.contains("All episodes"), "{screen}"); + assert!(screen.contains("1 unplayed · 5 ep"), "{screen}"); + assert!(screen.contains("Season 1"), "{screen}"); + assert!(screen.contains("2021–2022"), "{screen}"); + assert!(screen.contains("Without a season"), "{screen}"); + + let state = podcast_fixtures::with_episodes( + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 1, 3, 0)]), + vec![ + podcast_fixtures::episode(1, "Pilot", Some((ProgressStatus::Completed, 0))), + podcast_fixtures::episode(2, "Second", Some((ProgressStatus::InProgress, 754_000))), + podcast_fixtures::episode(3, "Third", None), + ], + ); + let screen = render(&state, 110, 30); + assert!( + screen.contains("3 episodes · all episodes · newest first"), + "{screen}" + ); + assert!(screen.contains("✓ 2024-06-15 S1E1 Pilot"), "{screen}"); + assert!(screen.contains("played · 45:00"), "{screen}"); + assert!(screen.contains("◐ 2024-06-15 S1E2 Second"), "{screen}"); + assert!(screen.contains("12:34 / 45:00"), "{screen}"); + assert!(screen.contains("● 2024-06-15 S1E3 Third"), "{screen}"); + assert!(screen.contains("Notes for Pilot."), "{screen}"); + + // The episode playing here shows its live position. + let mut state = state; + let mut playing = podcast_fixtures::episode_track(3); + playing.title = "Third".into(); + state.player.queue = vec![playing.clone()]; + state.player.current = Some(playing); + state.player.playing = true; + state.player.position_secs = 61.0; + let screen = render(&state, 110, 30); + assert!(screen.contains("▶ 2024-06-15 S1E3 Third"), "{screen}"); + assert!(screen.contains("1:01 / 45:00"), "{screen}"); + assert!(screen.contains("◐ 2024-06-15 S1E2 Second"), "{screen}"); + } + + #[test] + fn podcast_unsubscribe_confirmation_says_progress_is_kept() { + let mut state = + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 1, 0)]); + state.popup = Some(crate::app::state::Popup::ConfirmPodcastUnsubscribe { + podcast_id: podcast_fixtures::podcast_id(), + title: "Test Show".into(), + }); + let screen = render(&state, 100, 24); + assert!( + screen.contains("Unsubscribe from \"Test Show\"?"), + "{screen}" + ); + assert!(screen.contains("progress and history are kept"), "{screen}"); + } + #[test] fn help_lists_tab_names_for_digit_shortcuts() { let mut state = AppState::default(); diff --git a/src/ui/podcasts.rs b/src/ui/podcasts.rs index 3891593..c7e6cff 100644 --- a/src/ui/podcasts.rs +++ b/src/ui/podcasts.rs @@ -1,58 +1,768 @@ +//! Podcasts tab: subscriptions with their check status, a podcast's +//! seasons, and its episodes with saved position, played state, duration +//! and date. Rendering only reads `AppState`. + +use furumi_library::podcasts::{EpisodeListItem, SeasonSummary}; +use music_dht::podcast::ProgressStatus; use ratatui::Frame; -use ratatui::layout::{Alignment, Rect}; -use ratatui::text::Line; -use ratatui::widgets::{Block, Paragraph, Wrap}; +use ratatui::layout::{Alignment, Constraint, Layout, Rect}; +use ratatui::style::{Color, Modifier, Style}; +use ratatui::text::{Line, Span}; +use ratatui::widgets::{Block, Borders, Paragraph, Wrap}; +use unicode_width::UnicodeWidthStr; -use super::theme; -use crate::app::state::AppState; +use super::{loading_line, theme}; +use crate::app::state::{ + ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow, PodcastRow, + PodcastStoreStatus, SeasonRow, +}; +use crate::app::update::podcasts::{played_label, sort_label}; -/// Placeholder notice shown until podcast support lands: the tab already -/// holds its final position so the numeric shortcuts stay stable. -pub(crate) const UNAVAILABLE: &str = "Podcasts are not available in this build yet."; +/// Width below which the side panel with the cover is dropped. +const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80; +const SIDE_PANEL_WIDTH: u16 = ART_HEADER_WIDTH + 2; + +pub(crate) const EMPTY_HINT: &str = + "No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL."; +pub(crate) const STORE_FAILED_NOTE: &str = "The podcast database was left unchanged. \ + Music, playlists and sync keep working."; pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) { + let tab = &state.podcasts; + match &tab.store { + PodcastStoreStatus::Opening => { + let inner = bordered(frame, area, state, " Podcasts ".into()); + return centered_line( + frame, + inner, + loading_line(state, "opening the podcast database…"), + ); + } + PodcastStoreStatus::Failed(err) => { + let inner = bordered(frame, area, state, " Podcasts ".into()); + let lines = vec![ + Line::styled("Podcasts are unavailable", theme::header_for(state)), + Line::default(), + Line::styled(err.clone(), error_style()), + Line::default(), + Line::styled(STORE_FAILED_NOTE, theme::dim()), + ]; + frame.render_widget( + Paragraph::new(lines) + .alignment(Alignment::Center) + .wrap(Wrap { trim: true }), + inner, + ); + return; + } + PodcastStoreStatus::Ready => {} + } + match &tab.opened { + None => draw_list(frame, area, state), + Some(opened) if opened.season.is_none() => draw_seasons(frame, area, state), + Some(_) => draw_episodes(frame, area, state), + } +} + +fn bordered(frame: &mut Frame, area: Rect, state: &AppState, title: String) -> Rect { let block = Block::bordered() - .title(" Podcasts ") + .title(title) .title_style(theme::header_for(state)) .border_style(theme::border_for(state)); let inner = block.inner(area); frame.render_widget(block, area); - if inner.height == 0 { + inner +} + +fn centered_line(frame: &mut Frame, area: Rect, line: Line) { + if area.height == 0 { return; } + let middle = Rect { + y: area.y + area.height / 2, + height: 1, + ..area + }; + frame.render_widget(Paragraph::new(line).alignment(Alignment::Center), middle); +} - let lines = vec![ - Line::styled(UNAVAILABLE, theme::header_for(state)), - Line::default(), - Line::styled( - "Subscriptions, episodes and listening progress will appear here.", - theme::dim(), - ), - ]; - // Center on the estimated wrapped height, but let the paragraph use every - // row down to the bottom so narrow terminals never cut off the text. - let wrapped = estimated_rows(&lines, inner.width).min(inner.height); - let top = inner.height.saturating_sub(wrapped) / 2; - let body = Rect { - y: inner.y + top, - height: inner.height - top, +fn error_style() -> Style { + Style::new().fg(Color::Red) +} + +/// First row so that `cursor` stays visible, roughly centered. +fn first_visible(cursor: usize, len: usize, visible: usize) -> usize { + cursor + .saturating_sub(visible / 2) + .min(len.saturating_sub(visible)) +} + +fn row_area(area: Rect, offset: usize) -> Rect { + Rect { + y: area.y + offset as u16, + height: 1, + ..area + } +} + +/// A row with a left part and a right-aligned dim part. +fn draw_row( + frame: &mut Frame, + area: Rect, + state: &AppState, + left: Line<'static>, + right: Option, + selected: bool, +) { + frame.render_widget(Paragraph::new(left), area); + if let Some(right) = right { + frame.render_widget( + Paragraph::new(Line::styled(format!("{right} "), theme::dim())) + .alignment(Alignment::Right), + area, + ); + } + if selected { + frame + .buffer_mut() + .set_style(area, theme::tab_active_for(state)); + } +} + +// --------------------------------------------------------------------------- +// Subscriptions + +fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) { + let tab = &state.podcasts; + let inner = bordered(frame, area, state, " Podcasts ".to_string()); + match &tab.list { + Some(Loadable::Failed(error)) => { + return centered_line(frame, inner, Line::styled(error.clone(), error_style())); + } + None | Some(Loadable::Loading) => { + return centered_line(frame, inner, loading_line(state, "loading podcasts…")); + } + Some(Loadable::Ready(_)) => {} + } + let (list_area, side_area) = split_side_panel(inner); + if let Some(side) = side_area { + draw_side_panel(frame, side, state, tab.current_podcast()); + } + let rows = tab.list_rows(); + let visible = usize::from(list_area.height.max(1)); + let first = first_visible(tab.selected, rows.len(), visible); + for (index, row) in rows.iter().enumerate().skip(first).take(visible) { + let (left, right) = list_row(state, *row, list_area.width); + draw_row( + frame, + row_area(list_area, index - first), + state, + left, + right, + index == tab.selected, + ); + } + let used = rows.len().saturating_sub(first).min(visible) as u16; + let below = Rect { + y: list_area.y + used + 1, + height: list_area.height.saturating_sub(used + 1), + ..list_area + }; + if below.height == 0 { + return; + } + let hint = if tab.podcasts().is_empty() { + Line::styled(EMPTY_HINT, theme::dim()) + } else if side_area.is_none() + && let Some(row) = tab.current_podcast() + { + // Narrow terminal: the selected podcast's status below the list. + fetch_status_line(state, row) + } else { + Line::default() + }; + frame.render_widget(Paragraph::new(hint).wrap(Wrap { trim: true }), below); +} + +fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>, Option) { + let tab = &state.podcasts; + match row { + PodcastListRow::Add => { + let label = if tab.subscribing { + format!("{} Adding podcast…", state.spinner()) + } else { + "+ Add podcast (RSS feed URL)…".to_string() + }; + (Line::styled(label, theme::accent_for(state)), None) + } + PodcastListRow::RefreshAll => { + let label = if tab.refreshing.is_empty() { + "↻ Check all podcasts for new episodes".to_string() + } else { + format!( + "{} Checking {} feed(s) for new episodes…", + state.spinner(), + tab.refreshing.len() + ) + }; + (Line::styled(label, theme::accent_for(state)), None) + } + PodcastListRow::Podcast(index) => { + let Some(podcast) = tab.podcasts().get(index) else { + return (Line::raw(""), None); + }; + let marker = if tab.refreshing.contains(podcast.id()) { + Span::styled(format!("{} ", state.spinner()), theme::accent_for(state)) + } else if podcast.fetch.last_error.is_some() { + Span::styled("! ", error_style()) + } else { + Span::raw(" ") + }; + let summary = &podcast.summary; + let meta = if summary.unplayed_count > 0 { + format!( + "{} unplayed · {} ep", + summary.unplayed_count, summary.episode_count + ) + } else { + format!("{} ep", summary.episode_count) + }; + let title_width = usize::from(width).saturating_sub(2 + meta.width() + 2); + let left = Line::from(vec![ + marker, + Span::raw(fit_width(podcast.title(), title_width)), + ]); + (left, Some(meta)) + } + } +} + +/// Main area plus an optional right-hand panel for the cover and details. +fn split_side_panel(area: Rect) -> (Rect, Option) { + if area.width < SIDE_PANEL_MIN_TOTAL_WIDTH { + return (area, None); + } + let [main, side] = + Layout::horizontal([Constraint::Min(0), Constraint::Length(SIDE_PANEL_WIDTH)]).areas(area); + (main, Some(side)) +} + +fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&PodcastRow>) { + let block = Block::default() + .borders(Borders::LEFT) + .border_style(theme::border_for(state)); + let inner = block.inner(area); + frame.render_widget(block, area); + let Some(podcast) = podcast else { + return; + }; + let inner = Rect { + x: inner.x + 1, + width: inner.width.saturating_sub(1), ..inner }; - frame.render_widget( - Paragraph::new(lines) - .alignment(Alignment::Center) - .wrap(Wrap { trim: true }), - body, + let art_height = ART_HEADER_HEIGHT.min(inner.height); + draw_cover( + frame, + Rect { + height: art_height, + width: ART_HEADER_WIDTH.min(inner.width), + ..inner + }, + state, + podcast, + ); + let text_area = Rect { + y: inner.y + art_height + 1, + height: inner.height.saturating_sub(art_height + 1), + ..inner + }; + if text_area.height == 0 { + return; + } + let summary = &podcast.summary; + let mut lines = vec![Line::styled( + podcast.title().to_string(), + Style::new().add_modifier(Modifier::BOLD), + )]; + if let Some(author) = &summary.meta.author { + lines.push(Line::styled(author.clone(), theme::dim())); + } + let mut counts = format!("{} episodes", summary.episode_count); + if summary.season_count > 0 { + counts.push_str(&format!(" · {} seasons", summary.season_count)); + } + lines.push(Line::styled(counts, theme::dim())); + lines.push(Line::styled("private", theme::dim())); + lines.push(fetch_status_line(state, podcast)); + if let Some(description) = &summary.meta.description { + lines.push(Line::raw("")); + lines.push(Line::raw(description.clone())); + } + frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area); +} + +/// Refresh status of one podcast: checking, the last error, or when it was +/// last checked. +fn fetch_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> { + if state.podcasts.refreshing.contains(podcast.id()) { + return loading_line(state, "checking for new episodes…"); + } + let fetch = &podcast.fetch; + if let Some(error) = &fetch.last_error { + let mut text = format!("last check failed: {error}"); + if fetch.consecutive_failures > 1 { + text.push_str(&format!(" ({} times)", fetch.consecutive_failures)); + } + if let Some(next) = fetch.next_check_at_ms { + text.push_str(&format!("; next try {}", relative_time(next))); + } + return Line::styled(text, error_style()); + } + match fetch.last_success_at_ms { + Some(at) => Line::styled(format!("checked {}", relative_time(at)), theme::dim()), + None => Line::styled("not checked yet", theme::dim()), + } +} + +fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &PodcastRow) { + let art = podcast.fetch.cover_path.as_ref().and_then(|path| { + state.art.get(&crate::art::cache_key( + path, + ART_HEADER_WIDTH, + ART_HEADER_HEIGHT, + )) + }); + match art { + Some(ArtState::Ready(image)) => { + frame.render_widget(Paragraph::new(super::art::to_text(image)), area); + } + Some(ArtState::Loading) => centered_line(frame, area, Line::styled("…", theme::dim())), + _ => centered_line(frame, area, Line::styled("🎙", theme::dim())), + } +} + +// --------------------------------------------------------------------------- +// Seasons + +fn draw_seasons(frame: &mut Frame, area: Rect, state: &AppState) { + let tab = &state.podcasts; + let podcast = tab.current_podcast(); + let title = podcast.map_or("…".to_string(), |row| row.title().to_string()); + let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} ")); + let seasons = match &tab.seasons { + Some((_, Loadable::Failed(error))) => { + return centered_line(frame, inner, Line::styled(error.clone(), error_style())); + } + _ => match tab.opened_seasons() { + Some(seasons) => seasons, + None => return centered_line(frame, inner, loading_line(state, "loading seasons…")), + }, + }; + let (main, side) = split_side_panel(inner); + if let Some(side) = side { + draw_side_panel(frame, side, state, podcast); + } + let cursor = tab + .opened + .as_ref() + .and_then(|opened| opened.season_cursor) + .unwrap_or(0); + let rows = tab.season_rows(); + let visible = usize::from(main.height.max(1)); + let first = first_visible(cursor, rows.len(), visible); + for (index, row) in rows.iter().enumerate().skip(first).take(visible) { + let (label, meta) = match row { + SeasonRow::All => { + let (episodes, unplayed) = seasons.iter().fold((0, 0), |(e, u), season| { + (e + season.episode_count, u + season.unplayed_count) + }); + ("All episodes".to_string(), counts(episodes, unplayed)) + } + SeasonRow::Group(index) => { + let Some(season) = seasons.get(*index) else { + continue; + }; + (season_title(season), season_meta(season)) + } + }; + let width = usize::from(main.width).saturating_sub(meta.width() + 4); + draw_row( + frame, + row_area(main, index - first), + state, + Line::raw(format!(" {}", fit_width(&label, width))), + Some(meta), + index == cursor, + ); + } +} + +fn season_title(season: &SeasonSummary) -> String { + match season.season { + Some(number) => format!("Season {number}"), + None => "Without a season".to_string(), + } +} + +fn counts(episodes: u64, unplayed: u64) -> String { + if unplayed > 0 { + format!("{unplayed} unplayed · {episodes} ep") + } else { + format!("{episodes} ep") + } +} + +fn season_meta(season: &SeasonSummary) -> String { + let mut meta = counts(season.episode_count, season.unplayed_count); + let years = ( + season.first_published_at_ms.map(year_of), + season.last_published_at_ms.map(year_of), + ); + match years { + (Some(first), Some(last)) if first != last => meta.push_str(&format!(" · {first}–{last}")), + (Some(year), _) | (None, Some(year)) => meta.push_str(&format!(" · {year}")), + (None, None) => {} + } + meta +} + +// --------------------------------------------------------------------------- +// Episodes + +fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState) { + let tab = &state.podcasts; + let Some(opened) = &tab.opened else { + return; + }; + let podcast = tab.current_podcast(); + let mut title = podcast.map_or("…".to_string(), |row| row.title().to_string()); + if opened.season_cursor.is_some() { + let season = match opened.season { + Some(furumi_library::podcasts::SeasonFilter::Season(number)) => { + format!("Season {number}") + } + Some(furumi_library::podcasts::SeasonFilter::NoSeason) => { + "Without a season".to_string() + } + _ => "All episodes".to_string(), + }; + title = format!("{title} ▸ {season}"); + } + let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} ")); + let Some(list) = tab.opened_episodes() else { + return centered_line(frame, inner, loading_line(state, "loading episodes…")); + }; + + let (main, side) = split_side_panel(inner); + if let Some(side) = side { + draw_side_panel(frame, side, state, podcast); + } + let notes_height = if main.height >= 18 { 6 } else { 0 }; + let [summary_area, list_area, notes_area] = Layout::vertical([ + Constraint::Length(1), + Constraint::Min(1), + Constraint::Length(notes_height), + ]) + .areas(main); + + let mut summary = vec![Span::styled( + episode_summary(state, list.items.len(), list.complete), + theme::dim(), + )]; + if list.loading || list.loading_more { + summary.push(Span::raw(" ")); + summary.extend(loading_line(state, "loading…").spans); + } else if podcast.is_some_and(|row| tab.refreshing.contains(row.id())) { + summary.push(Span::raw(" ")); + summary.extend(loading_line(state, "checking for new episodes…").spans); + } + frame.render_widget(Paragraph::new(Line::from(summary)), summary_area); + + if let Some(error) = &list.error { + return centered_line(frame, list_area, Line::styled(error.clone(), error_style())); + } + if list.items.is_empty() { + if list.loading { + return; + } + let message = if tab.search.trim().is_empty() + && tab.played == furumi_library::podcasts::PlayedFilter::Any + { + "this feed has no playable episodes yet" + } else { + "no episode matches the search or filter" + }; + return centered_line(frame, list_area, Line::styled(message, theme::dim())); + } + + let cursor = opened.episode_cursor.min(list.items.len() - 1); + let visible = usize::from(list_area.height.max(1)); + let first = first_visible(cursor, list.items.len(), visible); + for (index, item) in list.items.iter().enumerate().skip(first).take(visible) { + draw_episode_row( + frame, + row_area(list_area, index - first), + state, + item, + index == cursor, + ); + } + if notes_height > 0 + && let Some(item) = list.items.get(cursor) + { + draw_notes(frame, notes_area, state, item); + } +} + +fn episode_summary(state: &AppState, loaded: usize, complete: bool) -> String { + let tab = &state.podcasts; + let count = if complete { + format!("{loaded} episodes") + } else { + format!("{loaded}+ episodes") + }; + let mut parts = vec![ + count, + played_label(tab.played).to_string(), + sort_label(tab.sort).to_string(), + ]; + let search = tab.search.trim(); + if !search.is_empty() { + parts.push(format!("search: \"{search}\"")); + } + parts.join(" · ") +} + +/// Marker and right-hand text for an episode's listening state. +pub(crate) fn episode_state(item: &EpisodeListItem) -> (&'static str, String) { + let duration = item.episode.duration_ms.filter(|ms| *ms > 0); + match item.status() { + ProgressStatus::Completed => ( + "✓", + match duration { + Some(ms) => format!("played · {}", format_duration_ms(ms)), + None => "played".to_string(), + }, + ), + ProgressStatus::InProgress => { + let position = format_duration_ms(item.resume_position_ms()); + ( + "◐", + match duration { + Some(ms) => format!("{position} / {}", format_duration_ms(ms)), + None => format!("at {position}"), + }, + ) + } + ProgressStatus::NotStarted => ("●", duration.map(format_duration_ms).unwrap_or_default()), + } +} + +fn draw_episode_row( + frame: &mut Frame, + area: Rect, + state: &AppState, + item: &EpisodeListItem, + selected: bool, +) { + let episode = &item.episode; + let date = episode + .published_at_ms + .map(|ms| format_date(ms.div_euclid(1000))) + .unwrap_or_else(|| " ".repeat(10)); + let (mut marker, mut right) = episode_state(item); + let mut marker_style = match item.status() { + ProgressStatus::NotStarted => theme::accent_for(state), + _ => theme::dim(), + }; + // The episode playing on this device shows its live position. + if let Some(position_ms) = playing_position_ms(state, &episode.episode_id) { + marker = "▶"; + marker_style = theme::accent_for(state); + let position = format_duration_ms(position_ms); + right = match episode.duration_ms.filter(|ms| *ms > 0) { + Some(ms) => format!("{position} / {}", format_duration_ms(ms)), + None => position, + }; + } + let number = match (episode.season, episode.episode_number) { + (Some(season), Some(number)) => format!("S{season}E{number} "), + (None, Some(number)) => format!("#{number} "), + _ => String::new(), + }; + let mut title = episode.title.clone(); + if !item.in_feed { + title.push_str(" (no longer in the feed)"); + } + let title_width = + usize::from(area.width).saturating_sub(2 + 10 + 2 + number.width() + right.width() + 2); + let title_style = match item.status() { + ProgressStatus::Completed => theme::dim(), + _ => Style::default(), + }; + let line = Line::from(vec![ + Span::styled(format!("{marker} "), marker_style), + Span::styled(date, theme::dim()), + Span::raw(" "), + Span::styled(number, theme::dim()), + Span::styled(fit_width(&title, title_width), title_style), + ]); + draw_row( + frame, + area, + state, + line, + (!right.is_empty()).then_some(right), + selected, ); } -/// Rows the lines need when wrapped to `width` (a lower bound: word wrapping -/// may need one more row per line, which the paragraph area leaves room for). -fn estimated_rows(lines: &[Line], width: u16) -> u16 { - let width = usize::from(width.max(1)); - let rows: usize = lines - .iter() - .map(|line| line.width().div_ceil(width).max(1)) - .sum(); - u16::try_from(rows).unwrap_or(u16::MAX) +/// Position of `episode_id` when it is the item playing (or paused) here. +fn playing_position_ms( + state: &AppState, + episode_id: &music_dht::podcast::EpisodeId, +) -> Option { + let player = &state.player; + let current = player.current.as_ref()?.podcast_episode_ref()?; + (player.playing && current.episode_id == *episode_id) + .then(|| (player.position_secs.max(0.0) * 1_000.0).round() as i64) +} + +fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, item: &EpisodeListItem) { + let block = Block::default() + .borders(Borders::TOP) + .border_style(theme::border_for(state)) + .title(Span::styled(" Episode notes ", theme::dim())); + let inner = block.inner(area); + frame.render_widget(block, area); + let mut text = item + .episode + .description + .clone() + .unwrap_or_else(|| "No description in the feed.".to_string()); + if item.completed_count > 1 { + text = format!("Listened {} times. {text}", item.completed_count); + } + frame.render_widget( + Paragraph::new(Line::raw(text)).wrap(Wrap { trim: true }), + inner, + ); +} + +// --------------------------------------------------------------------------- +// Formatting + +/// Truncate to a display width, adding an ellipsis when cut. +pub(crate) fn fit_width(text: &str, width: usize) -> String { + if UnicodeWidthStr::width(text) <= width { + return text.to_string(); + } + if width == 0 { + return String::new(); + } + let mut out = String::new(); + let mut used = 0; + for ch in text.chars() { + let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0); + if used + w + 1 > width { + break; + } + out.push(ch); + used += w; + } + out.push('…'); + out +} + +pub(crate) fn format_duration_ms(ms: i64) -> String { + let total = (ms.max(0) + 500) / 1000; + let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60); + if hours > 0 { + format!("{hours}:{minutes:02}:{secs:02}") + } else { + format!("{minutes}:{secs:02}") + } +} + +/// `YYYY-MM-DD` (UTC) for unix seconds: Howard Hinnant's days-to-civil. +pub(crate) fn format_date(unix_seconds: i64) -> String { + let (year, month, day) = civil(unix_seconds); + format!("{year:04}-{month:02}-{day:02}") +} + +fn year_of(unix_ms: i64) -> i64 { + civil(unix_ms.div_euclid(1000)).0 +} + +fn civil(unix_seconds: i64) -> (i64, i64, i64) { + let days = unix_seconds.div_euclid(86_400); + let z = days + 719_468; + let era = z.div_euclid(146_097); + let doe = z.rem_euclid(146_097); + let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let day = doy - (153 * mp + 2) / 5 + 1; + let month = if mp < 10 { mp + 3 } else { mp - 9 }; + let year = yoe + era * 400 + i64::from(month <= 2); + (year, month, day) +} + +/// "5 min ago", "in 2 h", or a date for anything further away. +fn relative_time(at_ms: i64) -> String { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis() as i64) + .unwrap_or(0); + relative_to(at_ms, now) +} + +pub(crate) fn relative_to(at_ms: i64, now_ms: i64) -> String { + let delta = (now_ms - at_ms) / 1000; + let (amount, future) = (delta.unsigned_abs(), delta < 0); + let text = match amount { + 0..60 => { + return if future { + "soon".into() + } else { + "just now".into() + }; + } + 60..3_600 => format!("{} min", amount / 60), + 3_600..86_400 => format!("{} h", amount / 3_600), + _ => return format_date(at_ms.div_euclid(1000)), + }; + if future { + format!("in {text}") + } else { + format!("{text} ago") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn formats_dates_durations_and_relative_times() { + assert_eq!(format_date(0), "1970-01-01"); + assert_eq!(format_date(951_782_400), "2000-02-29"); + assert_eq!(format_date(-86_400), "1969-12-31"); + assert_eq!(format_duration_ms(59_600), "1:00"); + assert_eq!(format_duration_ms(754_000), "12:34"); + assert_eq!(format_duration_ms(3_723_000), "1:02:03"); + let now = 1_700_000_000_000; + assert_eq!(relative_to(now - 30_000, now), "just now"); + assert_eq!(relative_to(now - 5 * 60_000, now), "5 min ago"); + assert_eq!(relative_to(now + 2 * 3_600_000, now), "in 2 h"); + assert_eq!(relative_to(now - 3 * 86_400_000, now), "2023-11-11"); + } + + #[test] + fn fits_display_width_with_ellipsis() { + assert_eq!(fit_width("Выпуск 650", 20), "Выпуск 650"); + assert_eq!(fit_width("Выпуск 650", 5), "Выпу…"); + assert_eq!(fit_width("abc", 0), ""); + } } diff --git a/src/ui/popup.rs b/src/ui/popup.rs index d6ba588..f4a779a 100644 --- a/src/ui/popup.rs +++ b/src/ui/popup.rs @@ -25,6 +25,9 @@ pub fn draw(frame: &mut Frame, state: &AppState) { .. }) => draw_edit(frame, state, title, fields, *focus, error.as_deref()), Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label), + Some(Popup::ConfirmPodcastUnsubscribe { title, .. }) => { + draw_confirm_podcast_unsubscribe(frame, state, title) + } Some(Popup::SimilarityPrivacyConsent { .. }) => { draw_similarity_privacy_consent(frame, state) } @@ -43,7 +46,12 @@ pub fn draw(frame: &mut Frame, state: &AppState) { }) => draw_track_artists(frame, state, tracks, *cursor, *selected), Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry), Some(Popup::FedInput { field, input }) => { - draw_fed_input(frame, state, field.title(), field.help(), input) + let keys = if *field == crate::app::state::FedInputField::PodcastSearch { + "enter: keep · esc: clear" + } else { + "enter: apply · esc: cancel" + }; + draw_fed_input(frame, state, field.title(), field.help(), keys, input) } Some(Popup::FedText { title, text }) => draw_fed_text(frame, state, title, text), Some(Popup::FedCopyText { @@ -891,6 +899,7 @@ fn draw_fed_input( state: &AppState, title: &str, help: &str, + keys: &str, input: &crate::app::input::LineEdit, ) { let area = centered(frame.area(), 72, 8); @@ -916,8 +925,7 @@ fn draw_fed_input( let spans = super::line_edit_spans(input, usize::from(entry_area.width.saturating_sub(1))); frame.render_widget(Paragraph::new(Line::from(spans)), entry_area); frame.render_widget( - Paragraph::new(Line::styled("enter: apply · esc: cancel", theme::dim())) - .alignment(Alignment::Center), + Paragraph::new(Line::styled(keys.to_string(), theme::dim())).alignment(Alignment::Center), hint_area, ); } @@ -1266,6 +1274,50 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) { ); } +/// Text of the unsubscribe confirmation: unsubscribing keeps progress and +/// listening history, so subscribing again continues where the user left off. +pub(crate) const PODCAST_UNSUBSCRIBE_NOTE: &str = "Listening progress and history are kept: \ + subscribe again later to continue where you left off."; + +fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title: &str) { + let width = 66.min(frame.area().width.saturating_sub(4)).max(30); + let area = centered(frame.area(), width, 9); + let block = Block::bordered() + .title(" Unsubscribe? ") + .title_style(theme::header_for(state)) + .border_style(theme::strong_border_for(state)); + let inner = block.inner(area); + frame.render_widget(Clear, area); + frame.render_widget(block, area); + + let [question, note, footer] = Layout::vertical([ + Constraint::Length(2), + Constraint::Min(1), + Constraint::Length(1), + ]) + .areas(inner); + frame.render_widget( + Paragraph::new(format!("Unsubscribe from \"{title}\"?")) + .wrap(Wrap { trim: false }) + .alignment(Alignment::Center), + question, + ); + frame.render_widget( + Paragraph::new(Line::styled(PODCAST_UNSUBSCRIBE_NOTE, theme::dim())) + .wrap(Wrap { trim: true }) + .alignment(Alignment::Center), + note, + ); + frame.render_widget( + Paragraph::new(Line::styled( + "enter/y unsubscribe · esc/n cancel", + theme::dim(), + )) + .alignment(Alignment::Center), + footer, + ); +} + fn draw_log_detail(frame: &mut Frame, state: &AppState, entry: &crate::config::logging::LogEntry) { let width = 90.min(frame.area().width.saturating_sub(4)).max(40); let height = 18.min(frame.area().height.saturating_sub(2)).max(7); diff --git a/src/ui/status_tests.rs b/src/ui/status_tests.rs index c6656a4..fe48f73 100644 --- a/src/ui/status_tests.rs +++ b/src/ui/status_tests.rs @@ -24,6 +24,7 @@ fn playing_state() -> AppState { file_size_bytes: None, play_count: 0, fed: None, + podcast: None, }); state.player.playing = true; state.player.position_secs = 12.0;