Compare commits

..
Author SHA1 Message Date
sanapachandClaude Opus 5.5 0da9862cf3 Add podcast RSS subscriptions with streaming playback
(cherry picked from commit 20167c5b5e2a55b1965fe7f7a095b909ce2d9267)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:15:13 +02:00
60 changed files with 6541 additions and 23418 deletions

No files matched your search

+6 -50
View File
@@ -61,32 +61,13 @@ 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. Public podcast cards are published only through
`podcasts::publication` (`PodcastDht::sync_shares` over the merged
registers): never publish a podcast directly, never log feed URLs or ids of
podcasts that are not shared, never put progress, history, counters or
device names into a card. A local share-off or unsubscribe asks for an
immediate pass (`Reconciler::revoke_now`), which quitting waits for, and
privacy confirmations change only through `publication::set_shared` /
`ShareConfirmations` (bound to the share decision, never synced). Nothing
is published before `DeviceSync::podcast_registers_current` (a restarted
device may hold a revoked share decision); passes before it only revoke.
Public cards found by search are publisher claims:
subscribe and preview only through `podcasts::discovery`, which uses the
card's origin URL (`PodcastSearchHit::feed_url`), never a claimed URL, and
never stores card metadata. 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. 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).
coordination.
- `src/ui/` renders `AppState`; rendering code must not start background work
or mutate persistence.
- `src/podcasts/` owns RSS parsing, podcast persistence, feed fetching and
the HTTP range-request stream reader; `src/app/podcast_runtime.rs`
schedules its work and folds results into `AppState`.
- `src/media.rs` owns OS media controls.
- `src/visualizer.rs` and `src/visualizations/` own the Rhai visualization
host and bundled scripts.
@@ -177,25 +158,7 @@ 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;
- 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`.
- test merge behavior from at least two operation orders.
Do not replace eventual reconciliation with assumptions about a continuously
connected leader.
@@ -226,11 +189,6 @@ The TUI is keyboard-first and cross-platform.
- Use semantic `Action` values rather than checking raw keys inside views.
- Add default bindings in `src/config/default_keymap.toml`.
- Preserve user overrides and context-specific bindings.
- Tabs are ordered Library, Playlists, Queue, Podcasts, Settings, Logs
(`1`–`6`). `Tab::ALL` defines that order for the tab bar, Tab/Shift+Tab,
and `GoToTab` indices. Changing it, or the meaning of any other
positional action argument, requires a new `KEYMAP_VERSION` and an
in-memory migration of older keymap files; never rewrite the user's file.
- Account for narrow terminals, Unicode display width, and empty/loading/error
states.
- Restore raw mode, alternate screen, bracketed paste, and keyboard
@@ -261,9 +219,7 @@ 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`; podcast sync between device stores and simulated v3
peers in `devices/podcast_sync_tests.rs`; wire shapes in
`devices/wire_compat_tests.rs`;
`devices/tests.rs`;
- search/ranking/federation conversion in `federation/tests.rs`;
- protocol-specific tests beside `federation/audio.rs` or
`federation/catalog.rs` when appropriate.
+29 -261
View File
@@ -66,27 +66,12 @@ 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
@@ -112,12 +97,8 @@ 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, 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
Jam never exchanges trusted membership, likes, playlists, or listening
history. 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.
@@ -208,12 +189,6 @@ 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.
@@ -237,25 +212,7 @@ 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. 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.
starting its audio engine.
Active-device leases and idle timing prevent stale snapshots from immediately
taking control after a device reconnects. This provides practical coordination
@@ -300,176 +257,6 @@ The audio engine is similarly isolated. The application owns the logical
queue, while `player::Controller` owns rodio playback and receives explicit
commands. Prefetching prepares the next source before the current item ends.
### Versioned key bindings
Key bindings are semantic `Action` values loaded from the bundled defaults and
merged with the user's `keymap.toml`. Some actions carry positional meaning
(`GoToTab(index)` follows the displayed tab order), so changing the tab order
changes what an existing user file means. The keymap file therefore carries a
format `version`. Files from an older format are adapted in memory when they
are loaded and the user is told once per start; Furumi never rewrites a
user's configuration. The current format, version 2, orders tabs as Library,
Playlists, Queue, Podcasts, Settings, Logs; unversioned files use the earlier
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`).
- **Public discovery.** The federation node runs FRID's signed podcast
overlay (`music_dht::podcast_dht`, ALPN `furumi-fd/podcast-dht/1`, cache
`federation/podcast-dht.sqlite3`) next to the similarity overlay: it
serves the overlay, bootstraps, republishes and expires records, hands
every peer capability manifest to `note_peer_capabilities`, and advertises
`podcast_dht` in its own manifest only while the overlay runs. A cache
that cannot be opened is logged and only turns discovery off. What this
device publishes is decided by `podcasts::publication`: after local share
and unsubscribe actions (at once: `Reconciler::revoke_now`), received
podcast operations and feed refreshes (debounced 1 s), every overlay
(re)start and every 5 minutes it builds one
`PodcastShareState` per subscription (merged registers, a card from the
catalog for shared podcasts only, the local privacy confirmation) and
calls the declarative `PodcastDht::sync_shares`, which publishes what is
shared and tombstones every other own card. The share decision is the
synced `podcast_share_set` register, so share-off or unsubscribe on any
trusted device revokes every device's card and a stale device cannot
republish: until `DeviceSync::podcast_registers_current` turns true (a
sync exchange applied with a v4 device of the group after the podcast
store was attached, or no reachable v4 device in the group) a pass only
unpublishes the own cards its registers no longer share
(`publication::revoke_unshared`) and never calls `sync_shares`, whose
first call would re-send the cards FRID withholds from an earlier
session. Publication runs from each device's own identity. Privacy
confirmations of feed URLs that look private are device-local
(`podcast-share-confirmations.json`, mode 0600) and bound to the exact
URLs and to the share decision (the share register's operation): a newer
decision on any device drops them. Cards leave out artwork and web links
that look private, and both for a private-looking feed. Without a
readable podcast store this device revokes all its own cards. The report
of each pass lists the private podcasts whose own card is still live
(`unrevoked`: pending, failed or federation off) for the side panel; the
overlay peer count is refreshed every 10 s between passes. Before the
node stops on quit, `Reconciler::flush` waits up to 4 s for a revocation
requested just before. Logs carry counts only.
- **Finding and subscribing.** `Federation::search_podcasts`
(`podcasts::discovery::search`) runs `PodcastDht::search` as its own task,
bounded at 15 s including the overlay's bootstrap, apart from the music
search (`Federation::search`, unchanged), and delivers
`AppEvent::FedPodcastsLoaded` for the same search sequence, so music
results never wait for the overlay. The hits keep the podcast entries of
`merge_search_results` (exact titles first) as
`podcasts::discovery::PodcastHit`s. Off the UI thread each hit's origin
URL is resolved with
`PodcastStore::resolve_podcast_id`, and the "subscribed" mark compares
that id and the card's id with the subscribed podcasts, so a feed alias
counts. A card is a publisher's claim: only `PodcastSearchHit::feed_url`
(the origin URL the id is hashed from) is ever fetched, both for the
episode preview (`discovery::preview`, nothing stored) and for
«Подписаться» (`discovery::subscribe_from_hit` → `refresh::subscribe_url`),
which refuses a hit whose id does not match its URL and stores only what
the feed says. Claimed moved-feed URLs are displayed, never fetched or
recorded as aliases. The new subscription is private. Subscribing to an
address whose feed announces (`itunes:new-feed-url`) a newer one this
device already follows reports that subscription instead of adding a
second one, and the preview (`discovery::preview_for`) resolves the
address the feed moved to, so the card reads «Уже подписан» then.
- **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
@@ -481,9 +268,6 @@ rodio source
│
v
audio analyzer ──> normalized features + scope samples
│ (PCM history, channel levels)
v
display meter ───> third-octave spectrum, L/R levels, peak hold
│
v
Rhai render(input)
@@ -502,36 +286,6 @@ script implements `render(input)` and returns declarative commands such as
clear, cell, line, rectangle, trace, and text. Scripts never receive the
ratatui frame or audio engine directly.
Protocol v3 adds what front-panel displays need: a third-octave spectrum
(31 bands, 20 Hz to 20 kHz, nominal ISO labels with exact base-2 edges that
partition the FFT bins) with per-band peak hold, left/right RMS and held
sample peaks, the RMS of the side signal (L - R) / 2 and a
stereo/mono decision made on loud windows, the analyzed sample rate, and track, queue and transport
details (format, bit depth, bitrate, track number, queue position, repeat,
shuffle, source), and the local wall-clock time. The audio thread only appends mono samples to a lock-free
ring and accumulates per-window channel levels; the FFT (an ~85 ms window
whose length follows the sample rate) and all ballistics run when the UI
takes a snapshot, under a mutex the audio thread never touches. Ballistics
are time-based (fast attack, steady fall, hold then fall): they advance with
the clock rather than per snapshot. Audio arrives in device-callback bursts,
so a snapshot that sees no new samples reuses the last measurement; only when
the PCM write counter stays still for longer than a few callbacks (pause,
stop) does the meter fall to the floor instead of freezing. In the control
role, and briefly when a track starts, the host passes an empty analysis, so
the meter reads zero. Meter values report the decoded signal on a fixed dBFS
scale and are therefore not scaled by the output volume, unlike the v2
energy features. Scripts choose their own display range within that scale.
v3 also lets a script keep state: `render` may return
`#{ commands, state }`, and the host passes `state` back with `dt` and a frame
counter on the next frame. State lives in the host's script runtime, is reset
whenever the script is recompiled or the visualizer is opened, and is subject
to the same sandbox size limits as any other value. Thin drawing comes from braille `dots` and `line`
commands in a 2 x 4 dot grid per cell (dots merge with braille already in the
cell; lines are clipped to the area first, so far endpoints cost nothing) and
from optional bold/dim/italic/underline/reversed attributes, which each
command sets for its cells rather than adding to what was drawn before.
This command boundary is intentional:
- scripts remain independent of Rust UI internals;
@@ -571,8 +325,8 @@ is not a plugin mechanism for accessing the library, network, or player
controls.
Fullscreen execution currently happens synchronously during UI drawing, with a fresh
Rhai scope for each frame; retained state is limited to the explicit `state`
value described above, and motion should still be derived from time and `dt`.
Rhai scope for each frame; scripts should derive motion from time and the
already-smoothed audio features rather than rely on retained mutable state.
The 50 ms animation timer gives a nominal 20 FPS cadence (other events can
also trigger redraws). Operation limits are not wall-clock deadlines. The
host also checks the script modification time during drawing, so slow script
@@ -623,6 +377,25 @@ timeouts are bounded; incompatible profiles are rejected. This direct
peer-selection layer can later be replaced by DHT routing without changing
local storage or ranking.
## Podcasts
Podcasts are a separate local source beside the music library. A dedicated
SQLite store holds subscriptions and episode metadata keyed by
`(podcast, RSS GUID)`. A feed refresh upserts episodes and never deletes
ones that dropped out of the feed. Refreshes use conditional HTTP requests
and run in the background with bounded concurrency. The store is
independent of `furumi-library`, so podcast data cannot affect the music
catalog, its migrations or federation publishing.
Episodes enter the queue as ordinary `TrackItem`s whose `file_path` is the
remote audio URL and whose ids sit in a reserved negative range. Playback
opens an HTTP range-request reader on the blocking pool. The reader buffers a
bounded window ahead of the decoder, and rodio uses coarse, byte-based
seeking for it, so a seek far into an episode fetches only the bytes it
needs. A sequence number discards a stream that finishes connecting after a
newer playback request. Episodes have no content id: listening history,
likes, playlists and cross-device handoff skip them.
## Persistence boundaries
Furumi stores different kinds of state according to their lifetime:
@@ -630,18 +403,16 @@ 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 |
| Podcast share confirmations | JSON next to `podcasts.sqlite3` | Device-local consent to publish feed URLs that look private |
| Public podcast cards (own and replicas) | SQLite (`federation/podcast-dht.sqlite3`) | Discovery overlay cache; replaceable |
| 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 |
| Podcast subscriptions and episode metadata | SQLite (`podcasts.sqlite3`) | Durable local podcast catalog; refetchable from feeds |
| Podcast covers | Filesystem cache | Replaceable artwork |
| Audio and artwork cache | Filesystem cache | Reusable fetched data |
| 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, the podcast store's personal state
and the device operation log are durable.
Caches are replaceable. The local library and device operation log are durable.
This distinction lets maintenance and recovery code discard derived network
data without risking the user's collection.
@@ -670,14 +441,11 @@ 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 publication of shared podcasts and subscribing from public
cards; the podcast model,
identity, merge rules, SQLite store and discovery overlay come from FRID;
- `devices.rs` owns trusted-device replication and playback coordination;
- `app/` owns state transitions and runtime orchestration;
- `ui/` renders the TUI;
- `podcasts/` owns RSS parsing, the podcast store, feed fetching and the
HTTP range-request stream reader;
- `media.rs` integrates platform media controls;
- `visualizer.rs` hosts programmable Rhai visualizations.
+9 -177
View File
@@ -9,183 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- "VFD panel" visualization: a vacuum-fluorescent hi-fi front panel with
thin segment digits, a stereo peak level meter (bars or needle VU meters),
a 25-band analyzer with three levels of glow, a nonlinear scale and peak
hold, a peak frequency readout with hold, tone fans, a signal path
schematic with a travelling pulse (source, format, volume, sample rate,
stereo or mono, and the output level in dBFS), the transport word in segment letters,
mode lamps (REPEAT, SHUFFLE, source, LOUDNESS, MUTE, SEARCH, OVER), a tape
counter and a dot-matrix title line (Latin and Cyrillic). Effects: a
power-on segment test, a counter "search" on track changes, static on
track and source changes, a source block that relights with its new
value, colours that fade into unlit phosphor on pause, a night dimmer, a
slow filament "breath", slightly uneven digit brightness and a standby
clock when nothing is loaded. Display modes "full", "spectrum",
"minimal" and "deck" switch with Shift+M or can be pinned with
`DISPLAY_MODE()` at the top of the script, like the palettes ("classic",
"green", "amber", "ice"); a mode that does not fit the window steps down.
A switch looks like hardware: the time and L/R meters stay lit, the other
sections go dark and light up one by one, and a row of mode lamps shows
the choice for a moment. The analyzer's display range follows the
loudness of the music (the measurements stay absolute), and deck mode has
AMS, NR, AUTO REVERSE and REW/FF lamps with a counter that races during a
seek. Bars settle with a little inertia, the analyzer's
tint runs from warm amber in the bass to light yellow in the treble, peak
marks fall with a little gravity after their hold, and the transport word
uses 14-segment letters with faint unlit segments.
- `Shift+M` in the fullscreen visualizer switches its display mode; the
choice is saved in `visualizations.toml`.
- Visualization protocol v3: a real third-octave spectrum (31 bands) with
peak hold, left/right RMS and held peaks, the side-signal level
(`level_side`, the floor for mono) and `channel_mode` ("stereo" or
"mono"), the sample rate, and track,
queue and transport details in the script input; `render` may return
`#{ commands, state }` to keep state between frames (with `dt` and
`frame`); new braille `dots` and `line` commands for thin drawing; bold,
dim, italic, underline and reversed attributes on any command. v2 scripts
keep working unchanged. Scripts also get the local time as
`clock_hour`, `clock_minute`, `clock_second` and `clock_millis`, and the
display mode as `mode`, a counter each script takes modulo its own number
of modes.
### Fixed
- The visualization clock shows local time instead of UTC.
## [0.5.0] - 2026-11-20
### Added
- Sharing a podcast in the federation (`Shift+F` in the `podcasts` context,
off by default): the synced share decision makes each device publish a
signed public card of the podcast in the FRID discovery overlay
(`podcast_dht`, advertised in the capability manifest); share-off and
unsubscribe revoke it with a tombstone. Feed URLs that look private are
published only after a confirmation on that device. The list and side
panel show whether a podcast is shared and whether this device publishes
it.
- Turning sharing off or unsubscribing revokes this device's card at once,
and quitting waits up to 4 s for that (also for a change still being
saved); the side panel says when a private podcast's card is not revoked
yet (pending or failed) or may not be (federation off: a card of an
earlier session cannot be ruled out). A privacy confirmation covers one
share decision: turning sharing off (or on again after that) on any
trusted device asks again, and until then this device holds the podcast
back and revokes the card it published for the earlier decision. A
confirmation whose decision could not be saved is dropped. Cards leave out
artwork and web links with an access key in the query or a member-feed
host (random-looking path parts of public CDNs are fine), and both for a
feed whose URL looks private.
- A device with other devices in its group publishes its cards only after
it synced with one of them since it started (or once none is left that
syncs podcasts), so a device that was closed while sharing was turned off
elsewhere does not send that card again; until then it only revokes, and
the side panel says it waits for a sync.
- Search (`/`, with the federation running) also finds public podcast cards.
They are listed after the music results under "Podcasts · public cards" with
a `[podcast]` marker, the number of publishing devices, and "✓ subscribed"
when this device is subscribed (also through a feed alias). The podcast
search runs apart from the federated music search (bounded at 15 s), so
music results never wait for it; local-only search is unchanged.
- Enter on a podcast hit opens its card: title, author, categories,
description, episode count, newest episode, publishing devices, feed URL
and unverified moved-feed claims. `e` reads the seasons and episodes from
the feed without subscribing. «Подписаться» (Enter) subscribes privately
with the card's origin feed URL, so the podcast id is the card's, and
reads everything from the feed itself; «Уже подписан» opens the podcast
in the Podcasts tab instead, also when this device follows the feed under
the newer address it moved to (known once the feed was read), and
subscribing to such an older address adds no second subscription.
- 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
- `furumi-tui` 0.5.0.
- Use published `music-dht` 0.6.0 and `furumi-library` 0.3.0 from crates.io;
remove the temporary FRID fork patches.
- Self-update and release download links use the canonical
`Furumi-stack/furumi-tui` GitHub repository.
- Queue items are keyed by their source: podcast episodes by episode id,
library rows as `lib:<id>` instead of `local:<id>`. 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.
- Keymap files without `version` keep their tab shortcuts: `GoToTab` indices
are remapped in memory (3 → 4 Settings, 4 → 5 Logs, and 5 → 3 Podcasts
from the prototype podcast build) with a non-blocking startup notice. The
file is not rewritten; `version = 2` files are used as written. A `version`
key placed inside a `[[keymaps]]` entry is reported as an error instead of
being ignored.
- The tab bar shortens titles, then shows only numbers (keeping the active
tab's full title) on terminals too narrow for all six tabs.
- A keymap notice no longer hides a "logging disabled" startup warning; both
are shown.
- The help window names the target tab for number-key shortcuts
("Go to Settings") instead of "Go to tab N".
### Fixed
- Isolate log privacy and identity-lock lifecycle tests from process-wide
state and descriptor inheritance in parallel tests.
- 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.
- A Podcasts tab (key `6`) for RSS subscriptions: add a feed by URL, browse
the episode catalog with title search and date sorting, and play an episode
through the regular player and queue.
- Episodes stream over HTTP with range-request seeking; only feed metadata
is stored locally, in `podcasts.sqlite3`, with covers in the cache
directory.
- Background refresh of stale feeds (conditional requests, GUID
deduplication) and a `RefreshPodcasts` action (`Shift+R` in the
`podcasts` key context).
## [0.2.8] - 2026-09-02
Generated
+17 -19
View File
@@ -1143,7 +1143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c54e03a951783e8b327515db3f2a2fd0e3bed362a96b066f341ce66ed49b4ead"
dependencies = [
"data-encoding",
"syn 1.0.109",
"syn 3.0.4",
]
[[package]]
@@ -1269,7 +1269,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -1455,7 +1455,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -1698,9 +1698,9 @@ dependencies = [
[[package]]
name = "furumi-library"
version = "0.3.0"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e7eb785cc6ab5b59db8ef74ffa05d798afde45fbd86da08aecf356432a52d10"
checksum = "f78466b6345cb90d7bb37db5d9dd67476ab6c2c3b2202876d24f448c314a9a08"
dependencies = [
"anyhow",
"blake3",
@@ -1715,16 +1715,16 @@ dependencies = [
[[package]]
name = "furumi_tui"
version = "0.5.0"
version = "0.3.5"
dependencies = [
"anyhow",
"base64",
"blake3",
"chrono",
"core-foundation 0.10.1",
"crokey",
"crossterm",
"directories",
"encoding_rs",
"flate2",
"fs2",
"furumi-library",
@@ -1734,6 +1734,7 @@ dependencies = [
"lofty",
"music-dht",
"object",
"quick-xml",
"ratatui",
"reqwest 0.12.28",
"rhai",
@@ -3240,18 +3241,16 @@ dependencies = [
[[package]]
name = "music-dht"
version = "0.6.0"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ce81f361952228654c28a46e41555f2c3d1520bf8c6c76562e7a53f70dd38e1"
checksum = "4c3f75d49d3a742a6777a1c4cc53f397399dfc9ddb70034c57aeb44d75d2ee4f"
dependencies = [
"async-trait",
"blake3",
"data-encoding",
"encoding_rs",
"federation-net",
"futures",
"postcard",
"quick-xml",
"rand 0.9.5",
"rusqlite",
"serde",
@@ -3260,7 +3259,6 @@ dependencies = [
"tokio",
"tracing",
"unicode-normalization",
"url",
]
[[package]]
@@ -3658,7 +3656,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3743,7 +3741,7 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8"
dependencies = [
"proc-macro-crate 1.3.1",
"proc-macro-crate 3.5.0",
"proc-macro2",
"quote",
"syn 2.0.119",
@@ -4541,7 +4539,7 @@ dependencies = [
"once_cell",
"socket2 0.6.5",
"tracing",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -5057,7 +5055,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -5115,7 +5113,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -5929,7 +5927,7 @@ dependencies = [
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.4",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -7033,7 +7031,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.48.0",
"windows-sys 0.61.2",
]
[[package]]
+8 -6
View File
@@ -1,6 +1,6 @@
[package]
name = "furumi_tui"
version = "0.5.0"
version = "0.3.5"
edition = "2024"
rust-version = "1.97"
description = "A federated P2P player for personal music libraries"
@@ -23,19 +23,21 @@ object = { version = "0.36", default-features = false, features = ["read", "std"
anyhow = "1.0.102"
blake3 = "1"
# Local wall-clock time for visualizations (the clock and its hour/minute).
chrono = { version = "0.4.45", default-features = false, features = ["clock"] }
crokey = "1.4.0"
crossterm = { version = "0.29.0", features = ["event-stream"] }
directories = "6.0.0"
# Podcast feeds: non-UTF-8 XML declarations (windows-1251, koi8-r, ...).
encoding_rs = "0.8.35"
futures-util = "0.3.32"
furumi-library = "0.3.0"
furumi-library = "0.2.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 = { version = "0.6.0", features = ["podcast-feed"] }
ratatui = { version = "0.30.1", features = ["unstable-rendered-line-info"] }
music-dht = "0.5.0"
# Podcast RSS/Atom parsing.
quick-xml = "0.41.0"
ratatui = "0.30.1"
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
rhai = { version = "1", features = ["sync"] }
rodio = { version = "0.22.2", default-features = false, features = ["playback", "mp3", "flac", "vorbis", "wav", "symphonia-aac", "symphonia-isomp4", "symphonia-alac"] }
+32 -242
View File
@@ -67,10 +67,7 @@ context-aware bindings, and user-defined rebinding through TOML. Built-in
audio visualizations, OS media controls, gapless queue playback, and local
library management are included. Visualizations are runtime-loadable Rhai
scripts executed in a resource-limited sandbox, so they can be added or edited
without rebuilding the player. The bundled "VFD panel" preset draws a hi-fi
front panel from a real third-octave spectrum and stereo level meter; scripts
can use the same spectrum, levels, track details, thin braille lines and
per-frame state.
without rebuilding the player.
Optional similarity search calculates versioned embeddings for local tracks
in the background and keeps them in SQLite. It works offline; after a separate
@@ -82,6 +79,33 @@ The first selectable model is downloaded on demand and is licensed separately
by MTG under CC BY-NC-SA 4.0 (a proprietary license is also available from
MTG); Furumi itself remains WTFPL.
## Podcasts
The **Podcasts** tab (key `6`) subscribes to podcast RSS feeds and plays
episodes through the same player, queue and media keys as your music.
- Choose **Add podcast** and paste the feed URL, for example
`https://podster.fm/rss.xml?pid=778`. Furumi stores the feed's metadata
only: titles, notes, dates and durations. Audio files are never
downloaded in bulk.
- Press `Enter` on a podcast to open its episode list. `/` searches episode
titles as you type, `v` switches between newest-first and oldest-first,
and `Shift+R` checks for new episodes.
- `Enter` on an episode plays it right away, ahead of the rest of your
queue. `a` / `Shift+A` queue it like a track. The episode streams over HTTP
and seeks with range requests, so jumping into a long episode does not
download everything before that point.
- Subscriptions are checked for new episodes in the background. A feed is
fetched again only after 30 minutes, and the request is conditional, so
an unchanged feed costs almost nothing. Episodes are matched by their RSS
GUID, so each one is listed once.
- `Shift+D` on a podcast in the list unsubscribes from it.
Subscriptions live in Furumi's data directory (`podcasts.sqlite3`). Covers are
kept in the cache directory. Podcasts are local to each device: they are not
yet synchronized between trusted devices or shared through federation, and
playback position is not remembered.
## Install
### macOS
@@ -101,7 +125,7 @@ furumi
### Linux, Windows, and other platforms
Download a prebuilt archive from the
[GitHub releases](https://github.com/Furumi-stack/furumi-tui/releases), or
[GitHub releases](https://github.com/house-of-vanity/furumi_tui/releases), or
build Furumi from source with Rust 1.97 or newer:
The repository selects the stable Rust toolchain. With rustup, run
@@ -152,235 +176,6 @@ The **Additional settings** window also contains the music save directory and
visualization controls. Use Up/Down (or j/k) to navigate, Enter to select, and
Esc to return to Settings. Long lists scroll with the selection.
### Tabs and key bindings
The number keys open the tabs in the order they are shown, and Tab /
Shift+Tab cycle through them:
| Key | Tab |
| --- | --- |
| `1` | Library |
| `2` | Playlists |
| `3` | Queue |
| `4` | Podcasts |
| `5` | Settings |
| `6` | Logs |
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,
`~/Library/Application Support/furumi/keymap.toml` on macOS); the bundled
`src/config/default_keymap.toml` documents the format. Start the file with
`version = 2`, the current format: its `GoToTab` indices are used exactly as
written (`0` Library … `5` Logs). `version` must come before the first
`[[keymaps]]` entry; written after one, TOML would attach it to that entry, so
Furumi rejects such a file with an error instead of guessing. Files without `version` were written for the
older tab order (Settings and Logs on `4`/`5`, Podcasts on `6` in the
prototype podcast build); Furumi adapts their tab shortcuts in memory so they
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.
- `Shift+F` shares the selected (or opened) podcast in the federation, or
makes it private again. The list marks shared podcasts with `shared`, and
the side panel says whether this device publishes the card (see
"Sharing a podcast" below).
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 (`Shift+F`, below).
- **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.
Sharing a podcast (public discovery):
- `Shift+F` turns "share in the federation" on for one podcast. The choice is
synchronized to your trusted devices like a subscription; each of your
devices that runs the federation then publishes a small public card of the
podcast under its own device key: the feed URL, title, author,
description, artwork and web links, the number of episodes and the date of
the newest one. The card never contains your progress, history, play
counts, device names or anything from podcasts you did not share. Artwork
and web links that look like they carry an access key (a token, key or
signature in the query, a member-feed host) are left out, and so are both
for a feed whose own URL looks private. Random-looking parts of their
path alone do not count: public podcast hosts name artwork that way.
- If the feed URL looks private (a token, key or signature in its query, a
member-feed host such as Patreon, a long random-looking part), Furumi asks
first and lists why (in Russian). Anyone who finds the card can read the
feed at that URL. The confirmation is stored only on this device
(`podcast-share-confirmations.json` next to `podcasts.sqlite3`) and covers
exactly the URLs shown and this one decision to share: another device of
yours asks again before it publishes, and so does this one if the feed
moves or if sharing is turned off (or turned on again after that) on any
of your devices. Until then the side panel says the publication is held
back, and a card this device published for the earlier decision is
revoked; press `Shift+F` there to confirm (or `x` in the dialog to stop
sharing). A confirmation whose decision could not be saved is dropped.
- `Shift+F` on a shared podcast, or unsubscribing, makes it private again on
every device in your group: each device replaces its card with a signed
removal record. This device sends its removal right away, and quitting
waits for it (up to 4 seconds, also when you quit before the change is
saved) and logs a warning when a card may still be live. While the
removal is not sent yet (or could not be sent), the side panel says the
public card is not revoked yet instead of just "private". With the
federation off this device cannot tell whether a card of an earlier
session is still live, so for a podcast that was shared the panel says
the card may not be revoked yet; the removal is sent the next time the
federation runs. Until then, and for a copy that missed the removal, the
card can still be found until it expires (at most 12 hours). Removing your card never touches other
people's subscriptions. An older device that comes back with the earlier
"share" decision does not publish it again: after starting, a device with
other devices in its group publishes nothing until it has synced with one
of them (the side panel says "waiting for a sync with your other
devices"); it still revokes at once what its own decisions no longer
share. Devices of your group that still run Furumi 0.3 (they do not sync
podcasts) and devices without an address are not waited for; remove a
device you no longer use under Connected Devices so this one stops
waiting for it.
- A device that left your device group, or was removed from it, no longer
receives your share decisions: it keeps publishing the podcasts it shares
until you turn sharing off (or unsubscribe) on that device itself.
- The card is signed with this device's federation key, which is also the
key of its public music library, so a shared podcast can be linked to
that library and to the device's network address.
- Publishing needs the federation to be running; the side panel shows
"not published" with the reason otherwise.
Finding public podcasts:
- The search (`/` outside the Podcasts tab, with the federation
running) also looks for public podcast cards. They are listed after the
music results under "Podcasts · public cards", each marked `[podcast]`,
with the number of devices that publish the card (devices, not
subscribers; anyone can create devices, so it is not a sign of trust) or
"✓ subscribed" when this device already follows the podcast, also when
you subscribed under an older address of the same feed that moved to the
card's address. When you follow the feed under a newer address than the
card's, the list cannot tell yet; the card can once its episodes are read
(`e`), and «Подписаться» never adds a second subscription for it. Your
query words go to the discovery peers, as they do for music search. The
podcast search runs apart from the music search (music results never wait
for it) and gives up after 15 seconds. With the federation off, search
stays local and shows no podcasts.
- Enter opens the card (in Russian): what its publishers claim (title,
author, categories, description, number of episodes and date of the
newest one), the feed URL, and addresses some publishers claim the feed
moved to, shown as unverified and never used. `e` reads the feed itself
and lists its seasons and episodes without subscribing; this is a request
to the podcast's own server, not to the publisher.
- «Подписаться» (Enter) subscribes with the card's feed URL, the address
the podcast's id is derived from, so the podcast is the same one the
publisher shares. Furumi then reads the feed from its own server and
stores only what the feed says; the publisher does not need to stay
online, and nothing from the card is kept. The new subscription is
private like any other: it is not shared until you press `Shift+F`, and
your progress is your own. «Уже подписан» means this device is already
subscribed (under the card's address, an older one, or — once the feed
was read — the newer address the feed moved to); Enter then opens the
podcast in the Podcasts tab.
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
@@ -461,23 +256,18 @@ owner_timeout_ms = 120000
All clients must support the new coordination envelope for eventual single
output ownership. frid's `PLAYBACK_PROTOCOL.md` describes the protocol, adapter
contract and rollout. This release uses the published FRID crates
`music-dht 0.6.0` and `furumi-library 0.3.0` from crates.io.
contract and rollout. Local Cargo patches are only for development; publish
frid and bump the client dependency versions before releasing these changes.
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 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
handoff, and a stalled peer. 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
+8 -54
View File
@@ -32,7 +32,6 @@ pub enum Action {
ToggleShuffle,
CycleRepeat,
ToggleVisualizer,
CycleVisualizerMode,
ToggleLike,
ToggleTrackSelection,
OpenTrackInfo,
@@ -58,27 +57,8 @@ 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,
/// Podcasts: turn "share in the federation" on or off for the selected
/// or opened podcast (asks first when its feed URL looks private).
TogglePodcastShare,
/// Podcasts: check the opened (or every) subscription for new episodes.
RefreshPodcasts,
}
/// Help-window sections, in display order.
@@ -88,18 +68,16 @@ pub enum Category {
Queue,
Library,
Navigation,
Podcasts,
Search,
System,
}
impl Category {
pub const ALL: [Category; 7] = [
pub const ALL: [Category; 6] = [
Category::Playback,
Category::Queue,
Category::Library,
Category::Navigation,
Category::Podcasts,
Category::Search,
Category::System,
];
@@ -110,7 +88,6 @@ impl Category {
Category::Queue => "Queue & playlists",
Category::Library => "Library",
Category::Navigation => "Navigation",
Category::Podcasts => "Podcasts",
Category::Search => "Search & commands",
Category::System => "System",
}
@@ -130,7 +107,6 @@ impl Action {
| Action::ToggleShuffle
| Action::CycleRepeat
| Action::ToggleVisualizer
| Action::CycleVisualizerMode
| Action::OpenConnectedDevices
| Action::OpenListenHistory => Category::Playback,
Action::QueueAddNext
@@ -164,17 +140,8 @@ impl Action {
Action::EditSelected
| 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
| Action::TogglePodcastShare => Category::Podcasts,
| Action::OpenLibraryFilters
| Action::RefreshPodcasts => Category::Library,
Action::OpenSearch | Action::OpenCommandLine => Category::Search,
Action::ToggleHelp | Action::Quit => Category::System,
}
@@ -206,10 +173,7 @@ impl Action {
Action::Quit => "Quit".into(),
Action::NextTab => "Next tab".into(),
Action::PrevTab => "Previous tab".into(),
Action::GoToTab(i) => match crate::app::state::Tab::from_index(*i) {
Some(tab) => format!("Go to {}", tab.title()),
None => format!("Go to tab {}", i + 1),
},
Action::GoToTab(i) => format!("Go to tab {}", i + 1),
Action::MoveUp => "Move up".into(),
Action::MoveDown => "Move down".into(),
Action::MoveLeft => "Move left".into(),
@@ -230,7 +194,6 @@ impl Action {
Action::ToggleShuffle => "Toggle shuffle".into(),
Action::CycleRepeat => "Cycle repeat mode".into(),
Action::ToggleVisualizer => "Toggle fullscreen visualizer".into(),
Action::CycleVisualizerMode => "Next visualizer display mode".into(),
Action::ToggleLike => "Like / unlike".into(),
Action::ToggleTrackSelection => "Track line selection".into(),
Action::OpenTrackInfo => "Track info".into(),
@@ -252,19 +215,10 @@ impl Action {
Action::CycleSourceMode => "Cycle source mode: Local / My / Global".into(),
Action::OpenLibraryFilters => "Library filters…".into(),
Action::OpenCommandLine => "Command line (:help for commands)".into(),
Action::OpenSearch => "Search artists, releases, tracks, public podcasts".into(),
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(),
Action::TogglePodcastShare => "Share podcast in the federation on/off".into(),
Action::RefreshPodcasts => "Podcasts: check for new episodes".into(),
}
}
}
-44
View File
@@ -109,9 +109,7 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) {
state.search.results = None;
state.search.fed_tracks.clear();
state.search.fed_artists.clear();
state.search.fed_podcasts.clear();
state.search.fed_loading = false;
state.search.fed_podcasts_loading = false;
return;
}
let library = Arc::clone(&runtime.library);
@@ -134,9 +132,7 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) {
// node is up); its results render as a separate, marked section.
state.search.fed_tracks.clear();
state.search.fed_artists.clear();
state.search.fed_podcasts.clear();
state.search.fed_loading = false;
state.search.fed_podcasts_loading = false;
if runtime.federation.settings().enabled {
state.search.fed_loading = true;
let fed = Arc::clone(&runtime.federation);
@@ -151,47 +147,9 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) {
let result = fed.search(&query).await.map_err(|err| format!("{err:#}"));
let _ = tx.send(AppEvent::FedSearchLoaded { seq, result });
});
schedule_podcast_search(state, runtime, seq);
}
}
/// Public podcast cards for the live search `seq`, as their own task and
/// event: the overlay search (bounded, bootstrap included) never holds back
/// the music results.
fn schedule_podcast_search(state: &mut AppState, runtime: &Runtime, seq: u64) {
state.search.fed_podcasts_loading = true;
let fed = Arc::clone(&runtime.federation);
let query = state.search.query.clone();
let tx = runtime.event_tx.clone();
let latest = Arc::clone(&runtime.search_seq);
let podcast_store = runtime
.podcasts
.as_ref()
.map(|podcasts| Arc::clone(&podcasts.services.store));
tokio::spawn(async move {
tokio::time::sleep(SEARCH_DEBOUNCE).await;
if latest.load(Ordering::SeqCst) != seq {
return;
}
let mut hits = fed.search_podcasts(&query).await;
// Which public podcasts this device already knows (feed aliases
// included), for the "subscribed" mark.
if let Some(store) = podcast_store
&& !hits.is_empty()
{
hits = tokio::task::spawn_blocking(move || {
if let Err(err) = crate::podcasts::discovery::resolve_local_ids(&store, &mut hits) {
tracing::warn!("podcast search: feed aliases not resolved: {err:#}");
}
hits
})
.await
.unwrap_or_default();
}
let _ = tx.send(AppEvent::FedPodcastsLoaded { seq, hits });
});
}
pub(super) fn schedule_similarity_search(
state: &mut AppState,
runtime: &Runtime,
@@ -213,9 +171,7 @@ pub(super) fn schedule_similarity_search(
state.search.results = None;
state.search.fed_tracks.clear();
state.search.fed_artists.clear();
state.search.fed_podcasts.clear();
state.search.fed_loading = false;
state.search.fed_podcasts_loading = false;
state.active_tab = crate::app::state::Tab::Global;
if let Some(crate::app::state::GlobalView::Search { cursor }) = state.global.stack.last_mut() {
*cursor = 0;
+17 -84
View File
@@ -130,21 +130,13 @@ pub enum AppEvent {
tracks: Vec<TrackItem>,
},
/// A status snapshot for the Federation tab.
/// Boxed to keep `AppEvent` small; the status is large and rare.
FederationStatus(Box<crate::federation::FedStatus>),
FederationStatus(crate::federation::FedStatus),
/// Federated live-search results (artists a card can be opened for,
/// plus matching tracks).
FedSearchLoaded {
seq: u64,
result: Result<crate::federation::FedSearchResults, String>,
},
/// Public podcast cards for the same live search (`seq`), delivered on
/// their own so the music results never wait for the overlay. Already
/// resolved against the podcast store.
FedPodcastsLoaded {
seq: u64,
hits: Vec<crate::podcasts::discovery::PodcastHit>,
},
/// A federated artist card finished assembling.
FedArtistLoaded {
name: String,
@@ -180,8 +172,7 @@ pub enum AppEvent {
FedTrackInfoLoaded {
placeholder_id: i64,
item_id: String,
/// Boxed: `TrackItem` is large since it can carry a podcast episode.
result: Result<Box<TrackItem>, String>,
result: Result<TrackItem, String>,
},
/// This peer's connection ticket, requested from the Federation tab.
FedTicket(Result<String, String>),
@@ -215,84 +206,26 @@ pub enum AppEvent {
JamInvite(Result<String, String>),
/// Result of joining a Jam capability.
JamJoined(Result<String, String>),
/// 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<music_dht::podcast::ProgressRecord>,
changed: bool,
},
/// The podcast store opened (with the feed client), or why it did not.
PodcastStoreOpened(Result<crate::podcasts::refresh::Services, String>),
/// Subscribed podcasts reloaded from the podcast store.
PodcastsLoaded(Result<Vec<crate::app::state::PodcastRow>, String>),
PodcastSeasonsLoaded {
podcast_id: music_dht::podcast::PodcastId,
result: Result<Vec<furumi_library::podcasts::SeasonSummary>, String>,
},
/// One page of episodes; `seq` drops results of older requests.
/// Podcast subscriptions (re)loaded from the podcast store.
PodcastsLoaded(Result<Vec<crate::podcasts::Podcast>, String>),
/// The stored episode catalog of one podcast.
PodcastEpisodesLoaded {
seq: u64,
offset: usize,
result: Result<Vec<furumi_library::podcasts::EpisodeListItem>, String>,
podcast_id: i64,
result: Result<Vec<crate::podcasts::Episode>, String>,
},
PodcastSubscribed(Result<crate::podcasts::refresh::SubscribeReport, String>),
/// One feed check finished.
/// A new subscription was stored (its id) or rejected.
PodcastSubscribed(Result<i64, String>),
/// One feed refresh finished.
PodcastRefreshed {
podcast_id: music_dht::podcast::PodcastId,
result: Result<crate::podcasts::refresh::RefreshReport, String>,
/// Asked by the user (status bar), not the background scheduler.
manual: bool,
podcast_id: i64,
result: Result<crate::podcasts::RefreshSummary, String>,
/// Background sweep: report errors in the log, not the status bar.
quiet: bool,
},
PodcastUnsubscribed {
podcast_id: music_dht::podcast::PodcastId,
title: String,
podcast_id: i64,
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<music_dht::podcast::PodcastEpisodeRef>,
start_ms: i64,
duration_ms: Option<i64>,
/// 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<Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>, String>),
/// What sharing a podcast would publish (`Effect::PodcastPrepareShare`).
PodcastSharePrepared {
podcast_id: music_dht::podcast::PodcastId,
title: String,
result: Result<crate::podcasts::publication::ShareCheck, String>,
},
/// The share decision was written (`Effect::PodcastSetShared`).
PodcastShareSet {
podcast_id: music_dht::podcast::PodcastId,
title: String,
shared: bool,
result: Result<(), String>,
},
/// The publication reconciler finished a pass.
PodcastPublication(crate::podcasts::publication::PublicationReport),
/// A public card's feed was read for a preview (`Effect::PodcastPreview`).
PodcastPreviewLoaded {
podcast_id: music_dht::podcast::PodcastId,
result: Result<Box<crate::podcasts::discovery::FeedPreview>, String>,
},
/// Periodic timer: refresh subscriptions whose feed went stale.
PodcastAutoRefreshDue,
}
+213 -564
View File
File diff suppressed because it is too large. Load diff
-138
View File
@@ -1,138 +0,0 @@
//! 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<PodcastRow>) -> 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<EpisodeListItem>) -> 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<SeasonSummary> {
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),
)
}
+402 -1299
View File
File diff suppressed because it is too large. Load diff
+38 -77
View File
@@ -115,29 +115,6 @@ 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 @ Popup::PodcastCard { .. } => {
if let Some(effect) =
crate::app::update::podcast_discovery::card_key(state, popup, key.code)
{
super::perform_effect(state, runtime, effect);
}
}
popup @ Popup::ConfirmPodcastShare { .. } => {
if let Some(effect) =
crate::app::update::podcasts::share_confirmation_key(state, popup, key.code)
{
super::perform_effect(state, runtime, effect);
}
}
Popup::SimilarityPrivacyConsent { enable_federation } => match key.code {
KeyCode::Enter | KeyCode::Char('y') => {
state.similarity.settings.federation_consent = true;
@@ -581,30 +558,13 @@ fn handle_fed_input(
) {
match key.code {
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);
if field == FedInputField::PodcastFilter {
crate::app::update::podcasts_set_filter(state, String::new());
}
}
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
@@ -706,17 +666,29 @@ fn handle_fed_input(
);
}
}
FedInputField::PodcastFeedUrl => {
if value.is_empty() {
state.status_message = Some("podcast feed URL is empty".into());
} else {
super::perform_effect(
state,
runtime,
crate::app::update::Effect::PodcastSubscribe(value),
);
}
}
// The filter is already applied live; Enter just keeps it.
FedInputField::PodcastFilter => {
crate::app::update::podcasts_set_filter(state, value);
}
}
}
_ => {
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);
if field == FedInputField::PodcastFilter {
crate::app::update::podcasts_set_filter(state, input.to_string());
}
state.popup = Some(Popup::FedInput { field, input });
}
}
}
@@ -835,15 +807,15 @@ 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, runtime: &mut Runtime, pasted: &str) {
pub fn handle_paste(state: &mut AppState, pasted: &str) {
let cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect();
let mut search = None;
let mut filter = None;
match &mut state.popup {
Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned),
Some(Popup::FedInput { field, input }) => {
input.insert_str(&cleaned);
if *field == FedInputField::PodcastSearch {
search = Some(input.to_string());
if *field == FedInputField::PodcastFilter {
filter = Some(input.to_string());
}
}
Some(Popup::Edit { fields, focus, .. }) => {
@@ -853,10 +825,8 @@ pub fn handle_paste(state: &mut AppState, runtime: &mut Runtime, pasted: &str) {
}
_ => {}
}
if let Some(search) = search
&& let Some(effect) = crate::app::update::podcasts::set_search(state, search)
{
super::perform_effect(state, runtime, effect);
if let Some(filter) = filter {
crate::app::update::podcasts_set_filter(state, filter);
}
}
@@ -1030,13 +1000,22 @@ fn save_edit(
fn handle_confirm_delete(
state: &mut AppState,
runtime: &Runtime,
runtime: &mut Runtime,
target: DeleteTarget,
label: String,
key: KeyEvent,
) {
match key.code {
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {}
KeyCode::Enter | KeyCode::Char('y') if matches!(target, DeleteTarget::Podcast(_)) => {
if let DeleteTarget::Podcast(id) = target {
super::perform_effect(
state,
runtime,
crate::app::update::Effect::PodcastUnsubscribe(id),
);
}
}
KeyCode::Enter | KeyCode::Char('y') => {
let library = Arc::clone(&runtime.library);
let result = match target {
@@ -1050,6 +1029,7 @@ fn handle_confirm_delete(
library.delete_playlist(id)
}
},
DeleteTarget::Podcast(_) => unreachable!("handled by the guarded arm above"),
};
match result {
Ok(()) => {
@@ -1079,21 +1059,6 @@ 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)))
@@ -1155,10 +1120,6 @@ 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(),
@@ -1188,7 +1149,7 @@ fn handle_track_info(
cursor,
scroll,
});
} else if track.is_podcast() || track.id < 0 || track.file_path.is_empty() {
} else if 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,
+128 -254
View File
@@ -338,7 +338,6 @@ mod tests {
file_size_bytes: None,
play_count: 0,
fed: None,
podcast: None,
}],
};
let mut state = AppState::default();
@@ -696,6 +695,8 @@ pub enum DeleteTarget {
Release(i64),
Artist(i64),
Playlist(i64),
/// Unsubscribe: drops the feed's stored metadata and cached cover.
Podcast(i64),
}
/// One text field of an edit form.
@@ -763,37 +764,6 @@ 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,
},
/// Sharing a podcast whose feed URL looks private: the user confirms
/// publishing exactly `urls`. Enter/y publishes, Esc/n cancels; when the
/// podcast is already shared (publication held back on this device),
/// `x` turns sharing off.
ConfirmPodcastShare {
podcast_id: music_dht::podcast::PodcastId,
title: String,
urls: crate::podcasts::publication::ConfirmedUrls,
/// Russian descriptions of the privacy warnings.
warnings: Vec<String>,
already_shared: bool,
},
/// A public podcast card from the search results: its claims, an
/// optional episode preview read from the feed, and "Подписаться" (or
/// "Уже подписан" when this device is subscribed under any of its ids).
PodcastCard {
hit: Box<crate::podcasts::discovery::PodcastHit>,
/// `None` until the user asks for the episodes (`e`).
preview: Option<Loadable<crate::podcasts::discovery::FeedPreview>>,
/// First body line shown.
scroll: u16,
/// Largest useful `scroll` at the last draw (the wrapped body
/// height minus the visible rows); scroll keys stop there.
/// `u16::MAX` until the card was drawn.
max_scroll: std::cell::Cell<u16>,
},
/// Enabling network similarity reveals an embedding query to peers.
SimilarityPrivacyConsent { enable_federation: bool },
/// Derived data is safe to recreate but potentially expensive.
@@ -931,10 +901,9 @@ 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,
/// Live episode-title filter of the opened podcast.
PodcastFilter,
}
impl FedInputField {
@@ -950,7 +919,7 @@ impl FedInputField {
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",
FedInputField::PodcastFilter => "Search episodes by title",
}
}
@@ -984,10 +953,10 @@ impl FedInputField {
"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."
"Paste the podcast's RSS feed URL. Only the episode list is stored; audio streams when you play an episode."
}
FedInputField::PodcastSearch => {
"Words from episode titles or descriptions; the list updates as you type. Enter keeps the search, Esc clears it."
FedInputField::PodcastFilter => {
"Type words from the episode title; the list updates as you type. Enter keeps the filter, Esc clears it."
}
}
}
@@ -1426,7 +1395,7 @@ impl SimilaritySearchHit {
Self::Local { track, .. } => track
.content_id
.clone()
.unwrap_or_else(|| track.key().to_string()),
.unwrap_or_else(|| format!("local:{}", track.id)),
Self::Federated { track, .. } => track
.content_id
.clone()
@@ -1446,14 +1415,7 @@ pub struct SearchState {
/// Artists a federated card can be opened for — from artist records and
/// from the artist names of matching tracks.
pub fed_artists: Vec<crate::federation::FedArtistHit>,
/// Public podcast cards (type `podcast`), listed after the federated
/// tracks; Enter opens the card.
pub fed_podcasts: Vec<crate::podcasts::discovery::PodcastHit>,
/// The federated music search is running.
pub fed_loading: bool,
/// The podcast search in the discovery overlay is running (separately,
/// so the music results never wait for it).
pub fed_podcasts_loading: bool,
/// Present only for a track-seeded search; text-search refreshes must not
/// replace this page with a title query.
pub similarity_source: Option<i64>,
@@ -1466,24 +1428,6 @@ pub struct SearchState {
}
impl SearchState {
/// Rows the cursor can reach in a text search: local artists, releases
/// and tracks, then federated artists, tracks and podcasts.
pub fn total_len(&self) -> usize {
self.results.as_ref().map_or(0, SearchResults::len)
+ self.fed_artists.len()
+ self.fed_tracks.len()
+ self.fed_podcasts.len()
}
/// Something federated is shown (or still loading).
pub fn has_federated(&self) -> bool {
!self.fed_tracks.is_empty()
|| !self.fed_artists.is_empty()
|| !self.fed_podcasts.is_empty()
|| self.fed_loading
|| self.fed_podcasts_loading
}
pub fn similarity_len(&self) -> usize {
usize::from(self.similarity_source_track.is_some()) + self.similarity_tracks.len()
}
@@ -1536,22 +1480,21 @@ pub enum Tab {
Global,
Playlists,
Queue,
Podcasts,
Federation,
Logs,
Podcasts,
}
impl Tab {
/// Display, Tab/Shift+Tab and `GoToTab` index order (`1`..`6`). Changing
/// it changes what `GoToTab(n)` means in users' keymap files: bump
/// `config::keymap::KEYMAP_VERSION` and remap older files in memory.
/// Display and `GoToTab` index order. Podcasts is appended so the
/// existing number keys (and user keymaps) keep their meaning.
pub const ALL: [Tab; 6] = [
Tab::Global,
Tab::Playlists,
Tab::Queue,
Tab::Podcasts,
Tab::Federation,
Tab::Logs,
Tab::Podcasts,
];
pub fn title(self) -> &'static str {
@@ -1559,21 +1502,9 @@ impl Tab {
Tab::Global => "Library",
Tab::Playlists => "Playlists",
Tab::Queue => "Queue",
Tab::Podcasts => "Podcasts",
Tab::Federation => "Settings",
Tab::Logs => "Logs",
}
}
/// Abbreviated title for the tab bar on narrow terminals.
pub fn short_title(self) -> &'static str {
match self {
Tab::Global => "Lib",
Tab::Playlists => "Lists",
Tab::Queue => "Queue",
Tab::Podcasts => "Pods",
Tab::Federation => "Set",
Tab::Logs => "Logs",
Tab::Podcasts => "Podcasts",
}
}
@@ -1598,9 +1529,9 @@ impl Tab {
Tab::Global => KeyContext::Library,
Tab::Playlists => KeyContext::Playlists,
Tab::Queue => KeyContext::Queue,
Tab::Podcasts => KeyContext::Podcasts,
Tab::Federation => KeyContext::Federation,
Tab::Logs => KeyContext::Logs,
Tab::Podcasts => KeyContext::Podcasts,
}
}
}
@@ -1734,142 +1665,82 @@ impl DevicePlaybackState {
}
}
// ---------------------------------------------------------------------------
// Podcasts tab
// ---------------------------------------------------------------------------
/// Podcasts tab: subscriptions list, or the episode catalog of one opened
/// podcast. Only RSS metadata lives here; audio streams on demand.
#[derive(Debug, Default)]
pub struct PodcastsTab {
/// Subscriptions; `None` until the first load is requested.
pub list: Option<Loadable<Vec<crate::podcasts::Podcast>>>,
/// Cursor over `podcast_list_rows` (action rows first, then podcasts).
pub selected: usize,
pub opened: Option<OpenedPodcast>,
/// Episodes of the opened podcast, keyed by its id so a late result for
/// a podcast that is no longer open is ignored.
pub episodes: Option<(i64, Loadable<Vec<crate::podcasts::Episode>>)>,
/// Case-insensitive episode title filter; every word must match.
pub filter: String,
pub sort: EpisodeSort,
/// Feeds with a refresh in flight (spinner + dedup of requests).
pub refreshing: HashSet<i64>,
pub subscribing: bool,
/// Wall-clock seconds of the last automatic refresh sweep.
pub last_auto_refresh: Option<i64>,
/// Newly subscribed podcast to select once the reloaded list arrives.
pub pending_select: Option<i64>,
}
/// 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.
/// "Add podcast" and "Check for new episodes" rows above the podcasts.
pub const PODCAST_ACTION_ROWS: usize = 2;
/// State of the podcast store (`podcasts.sqlite3`).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum PodcastStoreStatus {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OpenedPodcast {
pub id: i64,
/// Index into `visible_episode_indices`.
pub cursor: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EpisodeSort {
#[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),
NewestFirst,
OldestFirst,
}
/// 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
impl EpisodeSort {
pub fn toggle(self) -> Self {
match self {
EpisodeSort::NewestFirst => EpisodeSort::OldestFirst,
EpisodeSort::OldestFirst => EpisodeSort::NewestFirst,
}
}
pub fn title(&self) -> &str {
&self.summary.meta.title
pub fn label(self) -> &'static str {
match self {
EpisodeSort::NewestFirst => "newest first",
EpisodeSort::OldestFirst => "oldest first",
}
}
}
/// One row of the subscriptions list.
/// One row of the podcast subscriptions list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PodcastListRow {
Add,
RefreshAll,
/// Index into the loaded podcasts.
/// Index into the loaded podcast list.
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<usize>,
/// The episode group shown; `None` while the season list is shown.
pub season: Option<furumi_library::podcasts::SeasonFilter>,
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<furumi_library::podcasts::EpisodeListItem>,
/// 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<String>,
/// Episode to keep the cursor on when the first page arrives.
pub focus: Option<music_dht::podcast::EpisodeId>,
}
/// 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<Loadable<Vec<PodcastRow>>>,
/// Cursor over [`PodcastsTab::list_rows`] (action rows first).
pub selected: usize,
pub opened: Option<OpenedPodcast>,
/// Season groups of the opened podcast.
pub seasons: Option<(
music_dht::podcast::PodcastId,
Loadable<Vec<furumi_library::podcasts::SeasonSummary>>,
)>,
pub episodes: Option<EpisodeList>,
/// 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<music_dht::podcast::PodcastId>,
pub subscribing: bool,
/// Podcast to select once the reloaded list arrives.
pub pending_select: Option<music_dht::podcast::PodcastId>,
/// Last episode request sequence handed out.
pub episode_seq: u64,
/// This device's public discovery publications (last reconciler pass).
pub publication: crate::podcasts::publication::PublicationReport,
/// A share check or share decision is being written.
pub share_busy: HashSet<music_dht::podcast::PodcastId>,
}
impl PodcastsTab {
pub fn podcasts(&self) -> &[PodcastRow] {
pub fn podcasts(&self) -> &[crate::podcasts::Podcast] {
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)
}
/// This device is subscribed to the podcast of a public card: under the
/// card's id or under the id its origin URL resolves to here (a feed
/// alias of a podcast subscribed earlier).
pub fn subscribed_to(&self, hit: &crate::podcasts::discovery::PodcastHit) -> bool {
hit.ids().into_iter().any(|id| self.podcast(id).is_some())
pub fn podcast(&self, id: i64) -> Option<&crate::podcasts::Podcast> {
self.podcasts().iter().find(|podcast| podcast.id == id)
}
pub fn list_rows(&self) -> Vec<PodcastListRow> {
@@ -1878,10 +1749,10 @@ impl PodcastsTab {
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);
/// The podcast under the list cursor (list view) or the opened one.
pub fn current_podcast(&self) -> Option<&crate::podcasts::Podcast> {
if let Some(opened) = self.opened {
return self.podcast(opened.id);
}
match self.list_rows().get(self.selected)? {
PodcastListRow::Podcast(index) => self.podcasts().get(*index),
@@ -1889,51 +1760,72 @@ impl PodcastsTab {
}
}
/// 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),
/// Episodes of the opened podcast, when loaded.
pub fn opened_episodes(&self) -> Option<&[crate::podcasts::Episode]> {
let opened = self.opened?;
match &self.episodes {
Some((id, Loadable::Ready(episodes))) if *id == opened.id => Some(episodes),
_ => None,
}
}
pub fn season_rows(&self) -> Vec<SeasonRow> {
let mut rows = vec![SeasonRow::All];
rows.extend((0..self.opened_seasons().map_or(0, <[_]>::len)).map(SeasonRow::Group));
rows
/// Indices into `opened_episodes()` after the title filter and sort.
pub fn visible_episode_indices(&self) -> Vec<usize> {
let Some(episodes) = self.opened_episodes() else {
return Vec::new();
};
visible_episode_indices(episodes, &self.filter, self.sort)
}
/// 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())
pub fn selected_episode(&self) -> Option<&crate::podcasts::Episode> {
let opened = self.opened?;
let episodes = self.opened_episodes()?;
let visible = visible_episode_indices(episodes, &self.filter, self.sort);
visible
.get(opened.cursor.min(visible.len().saturating_sub(1)))
.and_then(|&index| episodes.get(index))
}
}
/// 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
/// Filter by title (every whitespace-separated term, case-insensitively)
/// and order by publication date; undated episodes always go last.
pub fn visible_episode_indices(
episodes: &[crate::podcasts::Episode],
filter: &str,
sort: EpisodeSort,
) -> Vec<usize> {
let terms: Vec<String> = filter.split_whitespace().map(str::to_lowercase).collect();
let mut indices: Vec<usize> = episodes
.iter()
.enumerate()
.filter(|(_, episode)| {
if terms.is_empty() {
return true;
}
let title = episode.title.to_lowercase();
terms.iter().all(|term| title.contains(term.as_str()))
})
}
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<TrackItem> {
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))
}
.map(|(index, _)| index)
.collect();
indices.sort_by(|&a, &b| {
let (a_ep, b_ep) = (&episodes[a], &episodes[b]);
match (a_ep.published_at, b_ep.published_at) {
(Some(x), Some(y)) => match sort {
EpisodeSort::NewestFirst => y.cmp(&x),
EpisodeSort::OldestFirst => x.cmp(&y),
},
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
// Stable sort keeps the store order (newest id first) for ties;
// reverse it for oldest-first so ties follow the same direction.
.then_with(|| match sort {
EpisodeSort::NewestFirst => a.cmp(&b),
EpisodeSort::OldestFirst => b.cmp(&a),
})
});
indices
}
/// Single source of truth for the UI. Mutated only by `update()` and the
@@ -2453,13 +2345,7 @@ 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<String> {
if track.is_podcast() {
return None;
}
track
.content_id
.as_deref()
@@ -2473,24 +2359,12 @@ pub fn track_content_id(track: &TrackItem) -> Option<String> {
})
}
/// 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:<row id>` for library
/// rows, `tmp:<id>` 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);
}
track.key().to_string()
format!("local:{}", track.id)
}
+255 -185
View File
@@ -20,12 +20,8 @@ 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]` (a podcast episode resumes
/// from its saved position).
/// (Re)start playback of `queue[queue_pos]`.
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.
@@ -110,58 +106,13 @@ 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.
/// Load the episode catalog of one podcast (and refresh it when stale).
PodcastLoadEpisodes(i64),
/// Fetch an RSS feed and store it as a new subscription.
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,
},
/// Check what sharing a podcast would publish (privacy warnings) before
/// turning sharing on or confirming a held-back publication.
PodcastPrepareShare {
podcast_id: music_dht::podcast::PodcastId,
title: String,
},
/// Turn sharing on or off (synced decision). `confirm` records, on this
/// device only, that the user confirmed publishing these feed URLs
/// despite privacy warnings.
PodcastSetShared {
podcast_id: music_dht::podcast::PodcastId,
title: String,
shared: bool,
confirm: Option<crate::podcasts::publication::ConfirmedUrls>,
},
/// Reconcile this device's public podcast cards now.
PodcastPublicationSync,
/// Subscribe to the podcast of a public card with its origin feed URL
/// (`podcasts::discovery::subscribe_from_hit`); reported as
/// `AppEvent::PodcastSubscribed`.
PodcastSubscribeFromCard(Box<music_dht::podcast_dht::PodcastSearchHit>),
/// Read a public card's feed for an episode preview (nothing stored).
PodcastPreview {
podcast_id: music_dht::podcast::PodcastId,
feed_url: music_dht::podcast::FeedUrl,
},
/// Check these subscriptions for new episodes.
PodcastRefresh(Vec<i64>),
PodcastUnsubscribe(i64),
}
pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
@@ -171,14 +122,6 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
.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,
@@ -347,6 +290,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
state.logs.selected_seq = None;
state.logs.follow = true;
}
Tab::Podcasts => podcasts_toggle_sort(state),
_ => {}
}
None
@@ -375,6 +319,15 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
state.cmdline.begin_history_navigation();
None
}
Action::OpenSearch
if state.active_tab == Tab::Podcasts && state.podcasts.opened.is_some() =>
{
state.popup = Some(super::state::Popup::FedInput {
field: super::state::FedInputField::PodcastFilter,
input: crate::app::input::LineEdit::new(state.podcasts.filter.clone()),
});
None
}
Action::OpenSearch => {
// The command line opens pre-filled with "/": typing continues
// the live search, exactly as if `:` then `/` were pressed.
@@ -389,18 +342,6 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
}
None
}
Action::CycleVisualizerMode => {
if state.visualizer.active {
if let Err(err) = state.visualizer.cycle_mode() {
// the fullscreen visualizer hides the status line
tracing::warn!(error = %format!("{err:#}"), "saving the visualizer mode failed");
state.status_message = Some(format!("visualization settings: {err:#}"));
}
} else {
state.status_message = Some("open the visualizer first (Shift+L)".into());
}
None
}
Action::Select => select_current(state),
Action::Back if state.visualizer.active => {
state.visualizer.close();
@@ -428,14 +369,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
let mut local_tracks: Vec<TrackItem> = Vec::new();
let mut fed_tracks: Vec<crate::federation::FedTrack> = 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;
}
@@ -448,11 +382,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
}
}
if local_tracks.is_empty() && fed_tracks.is_empty() {
state.status_message = Some(if episodes > 0 {
PODCAST_NOT_MUSIC.into()
} else {
"no track selected".into()
});
state.status_message = Some("no track selected".into());
None
} else {
let should_like = local_tracks.iter().any(|track| !state.track_liked(track))
@@ -498,9 +428,6 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
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()),
}
@@ -508,17 +435,16 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
}
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
.get_or_insert_with(|| "no track selected".into());
None if state.active_tab == Tab::Podcasts => {
state.status_message =
Some("podcast episodes cannot be added to playlists".into());
}
None => state.status_message = Some("no track selected".into()),
}
None
}
@@ -537,8 +463,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
}
}
Action::NewPlaylist => {
let for_target =
without_podcast_episodes(state, selected_playlist_target(state, false));
let for_target = selected_playlist_target(state, false);
state.popup = Some(super::state::Popup::NewPlaylist {
for_target,
input: crate::app::input::LineEdit::default(),
@@ -566,27 +491,16 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
None
}
}
Action::EditSelected if state.active_tab == Tab::Podcasts => {
state.status_message = Some("podcast metadata comes from the RSS feed".into());
None
}
Action::EditSelected => {
open_edit_popup(state);
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
| Action::TogglePodcastShare => {
state.status_message = Some(format!(
"open the Podcasts tab ({}) for this command",
Tab::Podcasts.index() + 1
));
None
}
Action::RefreshPodcasts => podcasts_refresh(state),
}
}
@@ -601,9 +515,7 @@ fn current_track_for_info(state: &AppState) -> Option<TrackItem> {
queued_at_position
.filter(|queued| {
super::state::track_key(queued) == current_key
// Podcast episodes share the sentinel id; only their key
// identifies them.
|| (!current.is_podcast() && !queued.is_podcast() && queued.id == current.id)
|| queued.id == current.id
|| current.is_fed_pending()
})
.or_else(|| {
@@ -656,36 +568,6 @@ 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<super::state::PlaylistAddTarget>,
) -> Option<super::state::PlaylistAddTarget> {
use super::state::PlaylistAddTarget;
match target {
Some(PlaylistAddTarget::Local(tracks)) => {
let total = tracks.len();
let music: Vec<TrackItem> = 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,
@@ -695,14 +577,22 @@ fn selected_playlist_target(
return Some(super::state::PlaylistAddTarget::Fed(fed));
}
let local = selected_tracks(state);
// Playlists hold library rows; streamed podcast episodes have none.
let local: Vec<TrackItem> = selected_tracks(state)
.into_iter()
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
.collect();
if !local.is_empty() {
return Some(super::state::PlaylistAddTarget::Local(local));
}
if state.active_tab == Tab::Podcasts {
return None;
}
include_current
.then(|| state.player.current.clone())
.flatten()
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
.map(|track| super::state::PlaylistAddTarget::Local(vec![track]))
}
@@ -773,10 +663,6 @@ 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;
}
@@ -839,6 +725,9 @@ fn delete_selected(state: &mut AppState) -> Option<Effect> {
if state.active_tab == Tab::Queue {
return remove_selected_from_queue(state);
}
if state.active_tab == Tab::Podcasts {
return podcasts_confirm_unsubscribe(state);
}
if state.active_tab == Tab::Playlists {
match state.playlists.opened {
None => {
@@ -1164,7 +1053,7 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
state.queue_tab.cursor,
state.player.queue.len(),
)),
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
@@ -1218,11 +1107,14 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
state.queue_tab.cursor,
state.player.queue.clone(),
)),
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
pub fn selected_tracks(state: &AppState) -> Vec<TrackItem> {
if state.active_tab == Tab::Podcasts {
return selected_episode_track(state).into_iter().collect();
}
if state.active_tab == Tab::Global
&& state.search.similarity_source.is_some()
&& let Some(GlobalView::Search { cursor }) = state.global.stack.last()
@@ -1519,8 +1411,7 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
.map(|track| (*track).clone())
}
Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(),
Tab::Podcasts => state.podcasts.selected_episode_track(),
Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
@@ -1539,14 +1430,8 @@ pub fn playlist_tracks(state: &AppState, id: i64) -> Option<Vec<&TrackItem>> {
}
}
/// 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 {
track.is_podcast()
|| state.global.filters.source_mode.includes_network()
|| !track.is_fed_pending()
state.global.filters.source_mode.includes_network() || !track.is_fed_pending()
}
/// A *release* under the cursor (artist-view tile/row or a search release).
@@ -1582,9 +1467,6 @@ fn selected_release_id(state: &AppState) -> Option<i64> {
/// an async fetch effect.
fn queue_add(state: &mut AppState, next: bool) -> Option<Effect> {
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();
@@ -1933,6 +1815,10 @@ fn page_step(state: &AppState) -> isize {
}
fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
if state.active_tab == Tab::Podcasts {
podcasts_move(state, dy);
return;
}
if state.active_tab == Tab::Federation {
if dy != 0 {
let last = settings_rows(state).len() as isize - 1;
@@ -2089,7 +1975,9 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
let total = if state.search.similarity_source.is_some() {
state.search.similarity_len()
} else {
state.search.total_len()
state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()
} as isize;
if total == 0 {
return;
@@ -2213,7 +2101,9 @@ fn current_view_len(state: &AppState) -> usize {
if state.search.similarity_source.is_some() {
state.search.similarity_len()
} else {
state.search.total_len()
state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()
}
}
Some(GlobalView::FedArtist { .. }) => fed_card_len(state),
@@ -2323,6 +2213,10 @@ fn jump_selection(state: &mut AppState, first: bool) {
state.settings_cursor = if first { 0 } else { last };
return;
}
if state.active_tab == Tab::Podcasts {
podcasts_move(state, if first { isize::MIN } else { isize::MAX });
return;
}
if state.active_tab == Tab::Logs {
if first {
let level = super::state::LOG_LEVELS[state.logs.level_index];
@@ -2413,6 +2307,9 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
if state.active_tab == Tab::Federation {
return federation_select(state);
}
if state.active_tab == Tab::Podcasts {
return podcasts_select(state);
}
// Queue: jump playback to the track under the cursor. Earlier tracks
// stay in the queue as "played"; picking one of them just moves the
// playing position back.
@@ -2649,10 +2546,6 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
state.active_tab = Tab::Global;
Some(Effect::FedOpenArtist(name))
}
Outcome::OpenPodcastCard(index) => {
podcast_discovery::open_card(state, index);
None
}
Outcome::OpenFedRelease { name, card, index } => {
let start =
if card.releases.get(index).is_some_and(|release| {
@@ -2894,7 +2787,6 @@ enum Outcome {
start: usize,
},
OpenFedArtist(String),
OpenPodcastCard(usize),
OpenFedRelease {
name: String,
card: crate::federation::FedArtistCard,
@@ -2905,8 +2797,7 @@ enum Outcome {
}
/// Enter inside the federated section of the search results: artists open
/// their card, tracks queue up like a local track list, podcasts open their
/// public card.
/// their card, tracks queue up like a local track list.
fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
let artists = &state.search.fed_artists;
if fed_index < artists.len() {
@@ -2914,12 +2805,7 @@ fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
}
let start = fed_index - artists.len();
if start >= state.search.fed_tracks.len() {
let podcast = start - state.search.fed_tracks.len();
return if podcast < state.search.fed_podcasts.len() {
Outcome::OpenPodcastCard(podcast)
} else {
Outcome::Nothing
};
return Outcome::Nothing;
}
Outcome::Play {
tracks: state
@@ -3362,6 +3248,7 @@ fn go_back(state: &mut AppState) {
Tab::Playlists => {
state.playlists.opened = None;
}
Tab::Podcasts => podcasts_close(state),
Tab::Global => {
// Esc on a view opened by Shift-J from another tab goes back to
// that tab, not down the Global stack.
@@ -3416,17 +3303,200 @@ fn reset_tab(state: &mut AppState, tab: Tab) {
state.logs.selected_seq = None;
}
Tab::Queue => {}
Tab::Podcasts => podcasts::close(state),
Tab::Podcasts => podcasts_close(state),
}
}
// ---------------------------------------------------------------------------
// Podcasts tab
// ---------------------------------------------------------------------------
/// How long a feed counts as fresh: opening it or the periodic sweep only
/// re-fetches feeds checked longer ago than this.
pub const PODCAST_STALE_SECS: i64 = 30 * 60;
fn podcasts_move(state: &mut AppState, dy: isize) {
let tab = &mut state.podcasts;
let (cursor, len) = match tab.opened {
Some(opened) => (opened.cursor, tab.visible_episode_indices().len()),
None => (tab.selected, tab.list_rows().len()),
};
if len == 0 {
return;
}
let target = (cursor as isize)
.saturating_add(dy)
.clamp(0, len as isize - 1) as usize;
match &mut tab.opened {
Some(opened) => opened.cursor = target,
None => tab.selected = target,
}
}
fn podcasts_toggle_sort(state: &mut AppState) {
let tab = &mut state.podcasts;
tab.sort = tab.sort.toggle();
if let Some(opened) = &mut tab.opened {
opened.cursor = 0;
}
state.status_message = Some(format!("episodes: {}", state.podcasts.sort.label()));
}
/// Esc: episode catalog → subscriptions list. The filter belongs to the
/// catalog it was typed in, so it is dropped as well.
fn podcasts_close(state: &mut AppState) {
let tab = &mut state.podcasts;
if let Some(opened) = tab.opened.take()
&& let Some(index) = tab.podcasts().iter().position(|p| p.id == opened.id)
{
tab.selected = index + super::state::PODCAST_ACTION_ROWS;
}
tab.filter.clear();
}
/// Called whenever the filter text changes: keep the cursor on the first
/// match so the list does not appear empty below a stale cursor.
pub fn podcasts_set_filter(state: &mut AppState, filter: String) {
// Caret movement and Enter on an unchanged filter keep the cursor.
if state.podcasts.filter == filter {
return;
}
state.podcasts.filter = filter;
if let Some(opened) = &mut state.podcasts.opened {
opened.cursor = 0;
}
}
fn podcasts_select(state: &mut AppState) -> Option<Effect> {
use super::state::PodcastListRow;
if state.podcasts.opened.is_some() {
return play_selected_episode(state);
}
match state
.podcasts
.list_rows()
.get(state.podcasts.selected)
.copied()?
{
PodcastListRow::Add => {
if state.podcasts.subscribing {
state.status_message =
Some("podcasts: a subscription is already being added".into());
return None;
}
state.popup = Some(super::state::Popup::FedInput {
field: super::state::FedInputField::PodcastFeedUrl,
input: crate::app::input::LineEdit::default(),
});
None
}
PodcastListRow::RefreshAll => podcasts_refresh(state),
PodcastListRow::Podcast(index) => {
let id = state.podcasts.podcasts().get(index)?.id;
state.podcasts.opened = Some(super::state::OpenedPodcast { id, cursor: 0 });
state.podcasts.filter.clear();
if !matches!(&state.podcasts.episodes, Some((loaded, Loadable::Ready(_))) if *loaded == id)
{
state.podcasts.episodes = Some((id, Loadable::Loading));
}
Some(Effect::PodcastLoadEpisodes(id))
}
}
}
/// Shift-R: the opened podcast, or every subscription from the list.
fn podcasts_refresh(state: &mut AppState) -> Option<Effect> {
if state.active_tab != Tab::Podcasts {
return None;
}
let ids: Vec<i64> = match state.podcasts.opened {
Some(opened) => vec![opened.id],
None => state.podcasts.podcasts().iter().map(|p| p.id).collect(),
};
let ids: Vec<i64> = ids
.into_iter()
.filter(|id| !state.podcasts.refreshing.contains(id))
.collect();
if ids.is_empty() {
state.status_message = Some(if state.podcasts.podcasts().is_empty() {
"podcasts: no subscriptions yet".into()
} else {
"podcasts: already checking for new episodes".into()
});
return None;
}
state.podcasts.refreshing.extend(ids.iter().copied());
state.status_message = Some(format!(
"podcasts: checking {} feed(s) for new episodes…",
ids.len()
));
Some(Effect::PodcastRefresh(ids))
}
fn podcasts_confirm_unsubscribe(state: &mut AppState) -> Option<Effect> {
if state.podcasts.opened.is_some() {
state.status_message =
Some("podcasts: go back to the podcast list (esc) to unsubscribe".into());
return None;
}
let Some(podcast) = state.podcasts.current_podcast() else {
state.status_message = Some("podcasts: select a podcast to unsubscribe".into());
return None;
};
state.popup = Some(super::state::Popup::ConfirmDelete {
target: super::state::DeleteTarget::Podcast(podcast.id),
label: format!("podcast \"{}\"", podcast.title),
});
None
}
/// The queueable track for the episode under the cursor.
fn selected_episode_track(state: &AppState) -> Option<TrackItem> {
let opened = state.podcasts.opened?;
let podcast = state.podcasts.podcast(opened.id)?;
let episode = state.podcasts.selected_episode()?;
Some(crate::podcasts::episode_track(podcast, episode))
}
/// Enter on an episode: play it right now without discarding the queue. It
/// is inserted after the current track and becomes the current track.
fn play_selected_episode(state: &mut AppState) -> Option<Effect> {
let Some(track) = selected_episode_track(state) else {
state.status_message = Some(match state.podcasts.opened_episodes() {
Some(_) if !state.podcasts.filter.is_empty() => "no episode matches the filter".into(),
Some(_) => "this podcast has no episodes".into(),
None => "episodes are still loading".into(),
});
return None;
};
Some(play_track_now(state, track))
}
pub fn play_track_now(state: &mut AppState, track: TrackItem) -> Effect {
let player = &mut state.player;
let insert_at = if player.current.is_some() {
(player.queue_pos + 1).min(player.queue.len())
} else {
player.queue_pos.min(player.queue.len())
};
player.queue.insert(insert_at, track);
if let Some(end) = player.play_next_end.as_mut()
&& *end >= insert_at
{
*end += 1;
}
player.queue_pos = insert_at;
player.prefetched_pos = None;
normalize_play_next_block(player);
state.track_selection.clear();
Effect::PlayCurrent
}
fn not_yet(state: &mut AppState, what: &str) {
state.status_message = Some(format!("{what}: coming in a later milestone"));
}
pub(crate) mod podcast_discovery;
pub(crate) mod podcasts;
#[cfg(test)]
#[path = "update_tests.rs"]
mod tests;
-135
View File
@@ -1,135 +0,0 @@
//! Public podcast cards opened from the search results.
//!
//! The card shows what its publishers claim. "Подписаться" subscribes with
//! the card's origin feed URL only (`Effect::PodcastSubscribeFromCard`); the
//! subscription is private and its metadata comes from the feed. When this
//! device is already subscribed (under the card's id or a feed alias), the
//! action reads «Уже подписан» and Enter opens the podcast instead.
use crossterm::event::KeyCode;
use music_dht::podcast::PodcastId;
use super::Effect;
use super::podcasts::{display_title, reveal_podcast, store_ready};
use crate::app::state::{AppState, Loadable, Popup};
use crate::podcasts::discovery::{FeedPreview, PodcastHit};
/// Lines a page key scrolls the card by.
const PAGE: u16 = 10;
/// Enter on a podcast hit of the search results.
pub fn open_card(state: &mut AppState, index: usize) {
let Some(hit) = state.search.fed_podcasts.get(index) else {
return;
};
state.popup = Some(Popup::PodcastCard {
hit: Box::new(hit.clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
}
/// A key in [`Popup::PodcastCard`] (the popup was taken out of the state):
/// Enter/s subscribes (or opens the subscribed podcast), `e` reads the
/// episodes from the feed, arrows and page keys scroll, Esc/q closes.
pub fn card_key(state: &mut AppState, popup: Popup, key: KeyCode) -> Option<Effect> {
let Popup::PodcastCard {
hit,
mut preview,
mut scroll,
max_scroll,
} = popup
else {
state.popup = Some(popup);
return None;
};
let mut effect = None;
match key {
KeyCode::Esc | KeyCode::Char('q') => return None,
KeyCode::Enter | KeyCode::Char('s') => {
if let Some(id) = subscribed_id(state, &hit, preview.as_ref()) {
// «Уже подписан»: show the subscription instead.
return reveal_podcast(state, id, hit.title());
}
effect = subscribe(state, &hit);
}
KeyCode::Char('e') => {
if !matches!(preview, Some(Loadable::Loading | Loadable::Ready(_))) {
preview = Some(Loadable::Loading);
effect = Some(Effect::PodcastPreview {
podcast_id: hit.hit.podcast_id.clone(),
feed_url: hit.hit.feed_url.clone(),
});
}
}
KeyCode::Up | KeyCode::Char('k') => scroll = scroll.saturating_sub(1),
KeyCode::Down | KeyCode::Char('j') => scroll = scroll.saturating_add(1),
KeyCode::PageUp => scroll = scroll.saturating_sub(PAGE),
KeyCode::PageDown => scroll = scroll.saturating_add(PAGE),
KeyCode::Home | KeyCode::Char('g') => scroll = 0,
_ => {}
}
// Never past the end the last draw measured, so scrolling back moves
// the view at once.
scroll = scroll.min(max_scroll.get());
state.popup = Some(Popup::PodcastCard {
hit,
preview,
scroll,
max_scroll,
});
effect
}
/// The id under which this device is subscribed to the card's podcast: the
/// card's id, the id its origin URL resolves to here (a feed alias), or —
/// once the feed was read — the podcast followed under the newer address
/// the feed moved to.
pub fn subscribed_id(
state: &AppState,
hit: &PodcastHit,
preview: Option<&Loadable<FeedPreview>>,
) -> Option<PodcastId> {
let moved = match preview {
Some(Loadable::Ready(preview)) => preview.subscribed_as.as_ref(),
_ => None,
};
hit.ids()
.into_iter()
.chain(moved)
.find(|id| state.podcasts.podcast(id).is_some())
.cloned()
}
fn subscribe(state: &mut AppState, hit: &PodcastHit) -> Option<Effect> {
if !store_ready(state) {
return None;
}
if state.podcasts.subscribing {
state.status_message = Some("podcasts: a subscription is already being added".into());
return None;
}
state.podcasts.subscribing = true;
state.status_message = Some(format!(
"podcasts: reading the feed of {}…",
display_title(hit.title())
));
Some(Effect::PodcastSubscribeFromCard(Box::new(hit.hit.clone())))
}
/// The preview of an open card arrived (ignored when another card is open).
pub fn preview_loaded(
state: &mut AppState,
podcast_id: PodcastId,
result: Result<Box<FeedPreview>, String>,
) {
if let Some(Popup::PodcastCard { hit, preview, .. }) = &mut state.popup
&& hit.hit.podcast_id == podcast_id
{
*preview = Some(match result {
Ok(feed) => Loadable::Ready(*feed),
Err(err) => Loadable::Failed(err),
});
}
}
File diff suppressed because it is too large. Load diff
+232 -1464
View File
File diff suppressed because it is too large. Load diff
+5 -69
View File
@@ -1,27 +1,15 @@
# Default keybindings for furumi.
#
# To customize, copy entries into keymap.toml in the platform config dir
# (~/.config/furumi/keymap.toml on Linux,
# ~/Library/Application Support/furumi/keymap.toml on macOS). A user binding
# replaces the default binding with the same key sequence and context.
#
# version: keymap format version; put `version = 2` at the top of your file,
# before the first [[keymaps]] entry (after one, TOML attaches it to that
# entry and the file is rejected). Version 2 numbers tabs in this order:
# GoToTab = 0 Library, 1 Playlists, 2 Queue, 3 Podcasts, 4 Settings, 5 Logs.
# A file without `version` is treated as the older layout and its GoToTab
# indices are adapted in memory (3 -> 4 Settings, 4 -> 5 Logs, and 5 -> 3
# Podcasts from the prototype podcast build); the file itself is never
# rewritten. With `version = 2` indices are used exactly as written.
# To customize, copy entries into <config dir>/furumi/keymap.toml
# (~/.config/furumi/keymap.toml on Linux/macOS). A user binding replaces the
# default binding with the same key sequence and context.
#
# key_sequence: space-separated chords, e.g. "g g" or "ctrl-a x".
# Modifiers: ctrl-, alt-, shift-, cmd-. Uppercase letters: "shift-g".
# command: an Action name, optionally with parameters:
# command = { SeekForward = { seconds = 30 } }
# context: optional view filter — global (default), library, search,
# playlists, queue, podcasts, settings, logs.
version = 2
# playlists, queue, settings, logs, podcasts.
[[keymaps]]
key_sequence = "q"
@@ -223,10 +211,6 @@ command = "CycleRepeat"
key_sequence = "shift-l"
command = "ToggleVisualizer"
[[keymaps]]
key_sequence = "shift-m"
command = "CycleVisualizerMode"
[[keymaps]]
key_sequence = "shift-h"
command = "OpenListenHistory"
@@ -276,55 +260,7 @@ command = "OpenCommandLine"
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"
[[keymaps]]
key_sequence = "shift-f"
command = "TogglePodcastShare"
command = "RefreshPodcasts"
context = "podcasts"
+6 -341
View File
@@ -9,14 +9,6 @@ use crate::app::action::Action;
const DEFAULT_KEYMAP: &str = include_str!("default_keymap.toml");
/// Current keymap file format, declared as a top-level `version = 2`.
///
/// Version 2 introduced the tab order Library, Playlists, Queue, Podcasts,
/// Settings, Logs. Files without a version (or with `version = 1`) were
/// written for the older order and have their `GoToTab` indices remapped in
/// memory; the user's file itself is never rewritten.
pub const KEYMAP_VERSION: u32 = 2;
/// Input context a binding applies to. `Global` bindings work everywhere;
/// view-specific bindings shadow global ones for the same key sequence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
@@ -28,10 +20,10 @@ pub enum KeyContext {
Search,
Playlists,
Queue,
Podcasts,
#[serde(rename = "settings", alias = "federation")]
Federation,
Logs,
Podcasts,
}
impl KeyContext {
@@ -42,9 +34,9 @@ impl KeyContext {
KeyContext::Search => "search",
KeyContext::Playlists => "playlists",
KeyContext::Queue => "queue",
KeyContext::Podcasts => "podcasts",
KeyContext::Federation => "settings",
KeyContext::Logs => "logs",
KeyContext::Podcasts => "podcasts",
}
}
}
@@ -62,18 +54,10 @@ struct RawBinding {
command: Action,
#[serde(default)]
context: KeyContext,
/// Never valid here: a `version = N` line written after a `[[keymaps]]`
/// header lands inside that entry. Caught so the file is not silently
/// treated as legacy (and its tab indices remapped) despite the version.
#[serde(default)]
version: Option<toml::Value>,
}
#[derive(Debug, Default, Deserialize)]
struct KeymapFile {
/// Format version; `None` means a legacy (pre-version-2) file.
#[serde(default)]
version: Option<u32>,
#[serde(default)]
keymaps: Vec<RawBinding>,
}
@@ -105,15 +89,9 @@ impl Keymap {
{
match fs::read_to_string(&path)
.map_err(anyhow::Error::from)
.and_then(|text| parse_user_keymap(&text))
.and_then(|text| parse_bindings(&text))
{
Ok(user) => {
if let Some(notice) = user.notice {
tracing::info!(path = %path.display(), "{notice}");
warning = Some(format!("{}: {notice}", path.display()));
}
merge(&mut bindings, user.bindings);
}
Ok(user) => merge(&mut bindings, user),
Err(err) => {
warning = Some(format!(
"{} ignored: {err:#}; using default keybindings",
@@ -130,16 +108,6 @@ impl Keymap {
(keymap, warning)
}
/// The embedded defaults only, without reading the user's file.
#[cfg(test)]
pub(crate) fn defaults() -> Self {
Self {
bindings: parse_bindings(DEFAULT_KEYMAP).expect("embedded default keymap must parse"),
pending: Vec::new(),
format: KeyCombinationFormat::default(),
}
}
/// Feed one key combination; returns an action, a pending-chord state, or
/// nothing. Esc clears a pending chord instead of resolving.
pub fn resolve(&mut self, key: KeyCombination, context: KeyContext) -> KeyResolution {
@@ -309,76 +277,10 @@ pub fn user_keymap_path() -> Option<PathBuf> {
}
fn parse_bindings(text: &str) -> Result<Vec<Binding>> {
parse_file(text).map(|(_, bindings)| bindings)
}
/// A user keymap after version handling, plus a one-time, non-blocking
/// notice for the status bar and log when its meaning was adapted.
struct UserKeymap {
bindings: Vec<Binding>,
notice: Option<String>,
}
fn parse_user_keymap(text: &str) -> Result<UserKeymap> {
let (version, mut bindings) = parse_file(text)?;
let notice = match version {
None | Some(1) => {
let remapped = remap_legacy_tab_indices(&mut bindings);
(remapped > 0).then(|| {
format!(
"{remapped} tab shortcut(s) adapted to the new tab order \
(4 Podcasts, 5 Settings, 6 Logs); \
add `version = {KEYMAP_VERSION}` to use indices as written"
)
})
}
Some(KEYMAP_VERSION) => None,
Some(0) => bail!("unsupported keymap version 0"),
Some(newer) => Some(format!(
"keymap version {newer} is newer than this build supports \
({KEYMAP_VERSION}); bindings are used as written"
)),
};
Ok(UserKeymap { bindings, notice })
}
/// Pre-version-2 `GoToTab` index → current index. Index 5 only existed in the
/// prototype podcast build, where it opened Podcasts.
fn legacy_tab_index(index: usize) -> usize {
match index {
3 => 4, // Settings
4 => 5, // Logs
5 => 3, // Podcasts (prototype)
other => other,
}
}
/// Rewrites legacy `GoToTab` indices in memory; returns how many changed.
fn remap_legacy_tab_indices(bindings: &mut [Binding]) -> usize {
let mut remapped = 0;
for binding in bindings {
if let Action::GoToTab(index) = &mut binding.action {
let current = legacy_tab_index(*index);
if current != *index {
*index = current;
remapped += 1;
}
}
}
remapped
}
fn parse_file(text: &str) -> Result<(Option<u32>, Vec<Binding>)> {
let file: KeymapFile = toml::from_str(text).context("invalid TOML")?;
let bindings = file
.keymaps
file.keymaps
.into_iter()
.map(|raw| {
if raw.version.is_some() {
bail!(
"`version` must be a top-level key placed before the first [[keymaps]] entry"
);
}
let keys = parse_sequence(&raw.key_sequence)
.with_context(|| format!("bad key_sequence {:?}", raw.key_sequence))?;
Ok(Binding {
@@ -387,8 +289,7 @@ fn parse_file(text: &str) -> Result<(Option<u32>, Vec<Binding>)> {
context: raw.context,
})
})
.collect::<Result<_>>()?;
Ok((file.version, bindings))
.collect()
}
fn parse_sequence(s: &str) -> Result<Vec<KeyCombination>> {
@@ -584,11 +485,6 @@ mod tests {
km.resolve(shift_l, KeyContext::Library),
KeyResolution::Action(Action::ToggleVisualizer)
);
let shift_m = KeyCombination::new(KeyCode::Char('M'), KeyModifiers::SHIFT);
assert_eq!(
km.resolve(shift_m, KeyContext::Library),
KeyResolution::Action(Action::CycleVisualizerMode)
);
}
#[test]
@@ -703,237 +599,6 @@ mod tests {
);
}
#[test]
fn default_keymap_declares_current_version() {
let (version, _) = parse_file(DEFAULT_KEYMAP).unwrap();
assert_eq!(version, Some(KEYMAP_VERSION));
assert!(parse_user_keymap(DEFAULT_KEYMAP).unwrap().notice.is_none());
}
#[test]
fn default_digit_keys_follow_tab_order() {
let mut km = keymap_from(DEFAULT_KEYMAP);
for (index, key) in ['1', '2', '3', '4', '5', '6'].into_iter().enumerate() {
let combo = KeyCombination::new(KeyCode::Char(key), KeyModifiers::NONE);
assert_eq!(
km.resolve(combo, KeyContext::Library),
KeyResolution::Action(Action::GoToTab(index))
);
}
}
fn go_to_tab_indices(bindings: &[Binding]) -> Vec<usize> {
bindings
.iter()
.filter_map(|b| match b.action {
Action::GoToTab(index) => Some(index),
_ => None,
})
.collect()
}
const LEGACY_TABS: &str = r#"
[[keymaps]]
key_sequence = "1"
command = { GoToTab = 0 }
[[keymaps]]
key_sequence = "f2"
command = { GoToTab = 2 }
[[keymaps]]
key_sequence = "f4"
command = { GoToTab = 3 }
[[keymaps]]
key_sequence = "f5"
command = { GoToTab = 4 }
[[keymaps]]
key_sequence = "f6"
command = { GoToTab = 5 }
context = "logs"
[[keymaps]]
key_sequence = "x"
command = "ToggleHelp"
"#;
#[test]
fn legacy_keymap_tab_indices_are_remapped_in_memory() {
let user = parse_user_keymap(LEGACY_TABS).unwrap();
// Settings 3 -> 4, Logs 4 -> 5, prototype Podcasts 5 -> 3.
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 4, 5, 3]);
assert_eq!(user.bindings[4].context, KeyContext::Logs);
assert_eq!(user.bindings[5].action, Action::ToggleHelp);
let notice = user.notice.expect("remap must be reported");
assert!(notice.starts_with("3 tab shortcut(s)"), "{notice}");
assert!(notice.contains("version = 2"), "{notice}");
}
#[test]
fn explicit_version_one_is_treated_as_legacy() {
let user = parse_user_keymap(&format!("version = 1\n{LEGACY_TABS}")).unwrap();
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 4, 5, 3]);
assert!(user.notice.is_some());
}
#[test]
fn legacy_keymap_without_changed_tab_indices_has_no_notice() {
let user = parse_user_keymap(
r#"
[[keymaps]]
key_sequence = "1"
command = { GoToTab = 0 }
[[keymaps]]
key_sequence = "q"
command = "Back"
"#,
)
.unwrap();
assert_eq!(go_to_tab_indices(&user.bindings), [0]);
assert!(user.notice.is_none());
}
#[test]
fn versioned_keymap_is_taken_literally() {
let user = parse_user_keymap(&format!("version = 2\n{LEGACY_TABS}")).unwrap();
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 3, 4, 5]);
assert!(user.notice.is_none());
}
#[test]
fn newer_keymap_version_is_used_as_written_with_a_notice() {
let user = parse_user_keymap(&format!("version = 3\n{LEGACY_TABS}")).unwrap();
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 3, 4, 5]);
assert!(user.notice.unwrap().contains("newer"));
}
#[test]
fn version_inside_a_keymaps_entry_is_rejected() {
// Appended at the end, TOML puts `version` into the last entry; the
// file must not silently fall back to the legacy remap.
let err = parse_user_keymap(
"[[keymaps]]\nkey_sequence = \"4\"\ncommand = { GoToTab = 3 }\nversion = 2\n",
)
.err()
.expect("misplaced version must be rejected");
assert!(
format!("{err:#}").contains("before the first [[keymaps]]"),
"{err:#}"
);
}
#[test]
fn unknown_keys_in_keymaps_entries_are_still_ignored() {
let user = parse_user_keymap(
"version = 2\n[[keymaps]]\nkey_sequence = \"4\"\ncommand = { GoToTab = 3 }\nnote = \"mine\"\n",
)
.unwrap();
assert_eq!(go_to_tab_indices(&user.bindings), [3]);
}
#[test]
fn keymap_version_zero_is_rejected() {
assert!(parse_user_keymap("version = 0").is_err());
}
#[test]
fn legacy_user_digit_override_keeps_its_old_target() {
let mut bindings = parse_bindings(DEFAULT_KEYMAP).unwrap();
// An old file that swapped "4" and "5" (Settings / Logs).
let user = parse_user_keymap(
r#"
[[keymaps]]
key_sequence = "4"
command = { GoToTab = 4 }
[[keymaps]]
key_sequence = "5"
command = { GoToTab = 3 }
"#,
)
.unwrap();
merge(&mut bindings, user.bindings);
let mut km = Keymap {
bindings,
pending: Vec::new(),
format: KeyCombinationFormat::default(),
};
let four = KeyCombination::new(KeyCode::Char('4'), KeyModifiers::NONE);
let five = KeyCombination::new(KeyCode::Char('5'), KeyModifiers::NONE);
let six = KeyCombination::new(KeyCode::Char('6'), KeyModifiers::NONE);
// Still Logs and Settings, now at their new indices.
assert_eq!(
km.resolve(four, KeyContext::Library),
KeyResolution::Action(Action::GoToTab(5))
);
assert_eq!(
km.resolve(five, KeyContext::Library),
KeyResolution::Action(Action::GoToTab(4))
);
// The new default "6" is untouched by the user's file.
assert_eq!(
km.resolve(six, KeyContext::Library),
KeyResolution::Action(Action::GoToTab(5))
);
}
#[test]
fn podcasts_context_parses() {
let bindings = parse_bindings(
r#"
[[keymaps]]
key_sequence = "z"
command = "ToggleHelp"
context = "podcasts"
"#,
)
.unwrap();
assert_eq!(bindings[0].context, KeyContext::Podcasts);
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),
(shift('F'), Action::TogglePodcastShare),
];
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);
+557 -1149
View File
File diff suppressed because it is too large. Load diff
@@ -1 +0,0 @@
{"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","trackLine truncated
+2 -6
View File
@@ -25,7 +25,6 @@ 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();
@@ -56,16 +55,13 @@ async fn localhost_web_peer() {
vector: BTreeMap::new(),
ops: vec![],
snapshot: SyncSnapshot::default(),
playback: sync.local_playback_snapshot_for(PROTOCOL_VERSION),
playback: sync.local_playback_snapshot(),
};
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(
&music_dht::device_sync::read_line(&mut stream).await.unwrap(),
)
.unwrap();
serde_json::from_slice(&read_line(&mut stream).await.unwrap()).unwrap();
let WireMessage::SyncResponse {
accepted: true,
playback: Some(snapshot),
File diff suppressed because it is too large. Load diff
+55 -515
View File
@@ -30,7 +30,6 @@ 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);
@@ -55,18 +54,8 @@ 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,
VersionReport::SelfReported(Some(profile_b.protocol_version)),
)
.unwrap();
b.apply_device_profile(
&profile_a,
true,
VersionReport::SelfReported(Some(profile_a.protocol_version)),
)
.unwrap();
a.apply_device_profile(&profile_b, true).unwrap();
b.apply_device_profile(&profile_a, true).unwrap();
a.claim_playback(&profile_a.device_id).unwrap();
a.playback_tick(true, true).unwrap();
let mut state = empty_playback_state();
@@ -174,12 +163,7 @@ 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,
VersionReport::SelfReported(Some(silent_profile.protocol_version)),
)
.unwrap();
a.apply_device_profile(&silent_profile, true).unwrap();
lock(&a.conn)
.execute(
"UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'",
@@ -234,121 +218,6 @@ 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![],
@@ -360,7 +229,6 @@ fn empty_playback_state() -> PlaybackStateWire {
volume: 80,
shuffle: false,
repeat: PlaybackRepeat::Off,
playback_rate: None,
}
}
@@ -414,7 +282,7 @@ fn coordination_ignores_legacy_commands_and_wrong_snapshot_sender() {
coordination: None,
});
let gossip = sync
.local_playback_snapshot_for(PROTOCOL_VERSION)
.local_playback_snapshot()
.unwrap()
.coordination
.unwrap();
@@ -474,7 +342,6 @@ 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
@@ -482,29 +349,6 @@ fn test_sync() -> DeviceSync {
#[test]
fn device_identity_has_one_coordinator_across_installation_paths() {
// Other tests spawn processes in parallel. A child can briefly inherit
// this test's locked file before exec closes it, delaying release after
// drop(first). Keep the lock lifecycle in its own test process.
const ISOLATED: &str = "FURUMI_TEST_ISOLATED_IDENTITY_LOCK";
if std::env::var_os(ISOLATED).is_none() {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"devices::tests::device_identity_has_one_coordinator_across_installation_paths",
"--nocapture",
])
.env(ISOLATED, "1")
.output()
.unwrap();
assert!(
output.status.success(),
"isolated identity lock test failed:\n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
return;
}
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("sync.sqlite3");
let first = acquire_identity_lock(&path).unwrap();
@@ -580,25 +424,12 @@ fn test_fed_track(content_id: &str) -> crate::federation::FedTrack {
}
#[test]
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")
);
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);
}
}
#[test]
@@ -621,35 +452,6 @@ 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 {
@@ -676,7 +478,6 @@ 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);
@@ -764,7 +565,6 @@ fn playback_command_is_targeted_and_deduplicated() {
volume: 42,
shuffle: false,
repeat: PlaybackRepeat::Off,
playback_rate: None,
},
seek: false,
};
@@ -821,7 +621,6 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
volume: 42,
shuffle: false,
repeat: PlaybackRepeat::Off,
playback_rate: None,
},
seek: false,
};
@@ -830,7 +629,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", PROTOCOL_VERSION).unwrap();
let target_ops = sync.ops_for_peer("dev_target").unwrap();
assert_eq!(
target_ops
.iter()
@@ -839,7 +638,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
1
);
assert!(
sync.ops_for_peer("dev_unknown", PROTOCOL_VERSION)
sync.ops_for_peer("dev_unknown")
.unwrap()
.iter()
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
@@ -854,7 +653,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
)
.unwrap();
assert!(
sync.ops_for_peer("dev_target", PROTOCOL_VERSION)
sync.ops_for_peer("dev_target")
.unwrap()
.iter()
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
@@ -893,7 +692,6 @@ fn newer_device_trust_reactivates_revoked_device() {
updated_at_ms: 20,
},
false,
VersionReport::Relayed(PROTOCOL_VERSION),
)
.unwrap();
assert!(!device_revoked(&sync, device_id));
@@ -1139,311 +937,53 @@ fn stale_synced_playlist_item_metadata_repairs_pending_fed_track() {
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(),
#[test]
fn podcast_episode_survives_the_playback_wire_but_local_paths_do_not() {
let podcast = crate::podcasts::Podcast {
id: 1,
feed_url: "https://example.test/feed.xml".into(),
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::<rusqlite::Result<Vec<_>>>()
.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<u16> {
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<u16>) -> 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<i64>| {
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();
cover_path: Some("/home/user/.cache/cover.jpg".into()),
episode_count: 1,
latest_published_at: None,
last_checked_at: None,
last_error: None,
};
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::<SyncOpPayload>(&payload).unwrap())
.collect::<Vec<_>>()
let episode = crate::podcasts::Episode {
id: 42,
podcast_id: 1,
guid: "g".into(),
title: "Episode".into(),
description: None,
published_at: None,
duration_seconds: Some(60.0),
audio_url: "https://cdn.example.test/42.mp3".into(),
mime_type: Some("audio/mpeg".into()),
byte_len: None,
link: None,
image_url: None,
};
// 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));
let track = crate::podcasts::episode_track(&podcast, &episode);
let round_trip = PlaybackTrack::from_track(&track).to_track_item();
assert_eq!(round_trip.id, track.id);
assert_eq!(round_trip.file_path, episode.audio_url);
assert_eq!(
crate::app::state::track_key(&round_trip),
crate::app::state::track_key(&track)
);
// Local files never leak their path; a URL without an episode id is
// not trusted as a stream either.
let mut local = track.clone();
local.id = 7;
local.file_path = "/home/user/Music/a.flac".into();
assert!(PlaybackTrack::from_track(&local).file_path.is_empty());
local.file_path = "https://cdn.example.test/other.mp3".into();
let wire = PlaybackTrack::from_track(&local);
assert_eq!(wire.file_path, "https://cdn.example.test/other.mp3");
assert!(wire.to_track_item().file_path.is_empty());
}
-576
View File
@@ -1,576 +0,0 @@
//! 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<i64>,
#[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<String>,
#[serde(default)]
pub featured_artist_names: Vec<String>,
pub year: Option<i32>,
pub duration_seconds: Option<i64>,
pub content_id: String,
pub release_title: Option<String>,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlaybackTrack {
pub id: i64,
pub title: String,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
pub duration_seconds: f64,
#[serde(default)]
pub artist_names: Vec<String>,
#[serde(default)]
pub featured_artist_names: Vec<String>,
pub release_id: i64,
pub release_title: String,
pub release_year: Option<i32>,
#[serde(default)]
pub file_path: String,
pub content_id: Option<String>,
pub audio_format: Option<String>,
pub audio_bitrate: Option<i32>,
pub audio_sample_rate: Option<i32>,
pub audio_bit_depth: Option<i32>,
pub file_size_bytes: Option<i64>,
#[serde(default)]
pub play_count: i64,
#[serde(default)]
pub fed: Option<SyncedFedTrack>,
}
#[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<PlaybackTrack>,
#[serde(default)]
pub queue_pos: usize,
pub playing: bool,
pub paused: bool,
#[serde(default)]
pub idle_since_ms: Option<i64>,
pub position_secs: f64,
#[serde(default)]
pub volume: u8,
pub shuffle: bool,
pub repeat: PlaybackRepeat,
}
pub type PlaybackSnapshot = music_dht::playback::Snapshot<PlaybackStateWire>;
pub type PlaybackCommand = music_dht::playback::Command<PlaybackStateWire>;
#[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<SyncedFedTrack>,
},
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<SyncedFedTrack>,
},
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<CommandStamp>,
},
ListenRecorded {
event: ListenEvent,
},
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SyncSnapshot {
#[serde(default)]
pub likes: Vec<SnapshotLike>,
#[serde(default)]
pub unlikes: Vec<SnapshotLikeTombstone>,
#[serde(default)]
pub playlists: Vec<SnapshotPlaylist>,
#[serde(default)]
pub deleted_playlists: Vec<SnapshotPlaylistTombstone>,
#[serde(default)]
pub removed_playlist_items: Vec<SnapshotPlaylistItemTombstone>,
}
#[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<SyncedFedTrack>,
}
#[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<SnapshotPlaylistItem>,
}
#[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<SyncedFedTrack>,
}
#[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<String>,
#[serde(default)]
group_active_devices: usize,
#[serde(default)]
devices: Vec<DeviceProfileWire>,
vector: BTreeMap<String, i64>,
ops: Vec<SyncOpWire>,
snapshot: SyncSnapshot,
#[serde(default)]
playback: Option<PlaybackSnapshot>,
},
PairResponse {
accepted: bool,
#[serde(default)]
pending: bool,
#[serde(default)]
error: Option<String>,
#[serde(default)]
group_id: Option<String>,
#[serde(default)]
profile: Option<DeviceProfileWire>,
#[serde(default)]
devices: Vec<DeviceProfileWire>,
#[serde(default)]
vector: BTreeMap<String, i64>,
#[serde(default)]
ops: Vec<SyncOpWire>,
#[serde(default)]
snapshot: SyncSnapshot,
#[serde(default)]
playback: Option<PlaybackSnapshot>,
},
Hello {
group_id: String,
profile: DeviceProfileWire,
devices: Vec<DeviceProfileWire>,
vector: BTreeMap<String, i64>,
ops: Vec<SyncOpWire>,
snapshot: SyncSnapshot,
#[serde(default)]
playback: Option<PlaybackSnapshot>,
},
SyncResponse {
accepted: bool,
#[serde(default)]
error: Option<String>,
#[serde(default)]
devices: Vec<DeviceProfileWire>,
#[serde(default)]
vector: BTreeMap<String, i64>,
#[serde(default)]
ops: Vec<SyncOpWire>,
#[serde(default)]
snapshot: SyncSnapshot,
#[serde(default)]
playback: Option<PlaybackSnapshot>,
},
}
}
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<String> {
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::<legacy::WireMessage>(&new_json).unwrap(),
old
);
assert_eq!(serde_json::from_str::<WireMessage>(&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::<legacy::WireMessage>(hello.clone()).is_err());
assert!(serde_json::from_value::<WireMessage>(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::<legacy::InviteWire>(&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 { .. }
));
}
+13 -59
View File
@@ -10,7 +10,6 @@ use music_dht::capabilities::{
CAPABILITIES_PROTOCOL_VERSION, CapabilityManifest, CapabilityMessage, read_message,
write_message,
};
use music_dht::podcast_dht::PodcastDht;
use music_dht::{ByteStream, EndpointId, MusicDhtService, StreamAcceptor};
const PROBE_INTERVAL: Duration = Duration::from_secs(30);
@@ -31,10 +30,8 @@ pub struct NewerProtocol {
}
impl ProtocolVersions {
/// `podcast_dht`: whether this node runs the podcast-discovery overlay
/// (it is advertised only then).
pub fn snapshot(observed: &ObservedVersions, podcast_dht: bool) -> Self {
let local = local_manifest(podcast_dht).protocols;
pub fn snapshot(observed: &ObservedVersions) -> Self {
let local = local_manifest().protocols;
let observed_versions = lock(&observed.versions).clone();
let newer = observed_versions
.iter()
@@ -62,35 +59,27 @@ pub struct ObservedVersions {
peers: Mutex<BTreeMap<String, String>>,
}
/// This node's manifest. The podcast-discovery overlay is opt-in in FRID:
/// it is advertised only when this node serves it (`podcast_dht`), so
/// peers never dial an overlay that failed to open here.
fn local_manifest(podcast_dht: bool) -> CapabilityManifest {
let manifest = CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION"))
.with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION);
if podcast_dht {
manifest.with_podcast_dht()
} else {
manifest
}
fn local_manifest() -> CapabilityManifest {
CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION"))
.with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION)
}
pub async fn serve(mut acceptor: StreamAcceptor, podcast_dht: bool) {
pub async fn serve(mut acceptor: StreamAcceptor) {
while let Some(stream) = acceptor.accept().await {
tokio::spawn(async move {
if let Err(error) = serve_one(stream, podcast_dht).await {
if let Err(error) = serve_one(stream).await {
tracing::debug!("capability stream failed: {error:#}");
}
});
}
}
async fn serve_one(mut stream: ByteStream, podcast_dht: bool) -> Result<()> {
async fn serve_one(mut stream: ByteStream) -> Result<()> {
let response = match read_message(&mut stream).await? {
CapabilityMessage::Get {
version: CAPABILITIES_PROTOCOL_VERSION,
} => CapabilityMessage::Manifest {
manifest: local_manifest(podcast_dht),
manifest: local_manifest(),
},
CapabilityMessage::Get { version } => CapabilityMessage::Error {
message: format!("unsupported capability protocol {version}"),
@@ -105,14 +94,7 @@ async fn serve_one(mut stream: ByteStream, podcast_dht: bool) -> Result<()> {
Ok(())
}
/// Polls the manifests of known peers. With the podcast-discovery overlay
/// running, every manifest is handed to `PodcastDht::note_peer_capabilities`,
/// which stops dialing peers that do not serve the overlay.
pub async fn probe_loop(
service: Arc<MusicDhtService>,
observed: Arc<ObservedVersions>,
podcast_dht: Option<Arc<PodcastDht>>,
) {
pub async fn probe_loop(service: Arc<MusicDhtService>, observed: Arc<ObservedVersions>) {
let mut interval = tokio::time::interval(PROBE_INTERVAL);
loop {
interval.tick().await;
@@ -127,8 +109,7 @@ pub async fn probe_loop(
)
.collect::<std::collections::BTreeSet<_>>();
for peer in peers {
if let Err(error) = probe_peer(&service, peer, &observed, podcast_dht.as_deref()).await
{
if let Err(error) = probe_peer(&service, peer, &observed).await {
tracing::trace!(%peer, "peer capability probe unavailable: {error:#}");
}
}
@@ -139,7 +120,6 @@ async fn probe_peer(
service: &MusicDhtService,
peer: EndpointId,
observed: &ObservedVersions,
podcast_dht: Option<&PodcastDht>,
) -> Result<()> {
let mut stream = service.open_stream(peer, CAPABILITIES_ALPN).await?;
write_message(
@@ -157,11 +137,6 @@ async fn probe_peer(
anyhow::bail!("peer did not return a capability manifest");
};
manifest.validate()?;
if let Some(podcast_dht) = podcast_dht
&& let Err(error) = podcast_dht.note_peer_capabilities(peer, &manifest).await
{
tracing::debug!(%peer, "podcast DHT capability note failed: {error:#}");
}
{
let mut versions = lock(&observed.versions);
for (id, version) in manifest.protocols {
@@ -187,7 +162,7 @@ mod tests {
#[test]
fn local_manifest_lists_every_player_protocol() {
let manifest = local_manifest(true);
let manifest = local_manifest();
for id in [
"federation_net",
"ticket",
@@ -199,31 +174,10 @@ mod tests {
"similarity_dht",
"device_sync",
"jam",
"podcast_dht",
] {
assert!(manifest.protocols.contains_key(id), "missing {id}");
}
manifest.validate().unwrap();
assert!(manifest.supports_podcast_dht());
}
#[test]
fn podcast_discovery_is_advertised_only_when_it_runs() {
let manifest = local_manifest(false);
assert!(!manifest.supports_podcast_dht());
assert!(!manifest.protocols.contains_key("podcast_dht"));
manifest.validate().unwrap();
let observed = ObservedVersions::default();
assert!(
!ProtocolVersions::snapshot(&observed, false)
.local
.contains_key("podcast_dht")
);
assert!(
ProtocolVersions::snapshot(&observed, true)
.local
.contains_key("podcast_dht")
);
}
#[test]
@@ -231,7 +185,7 @@ mod tests {
let observed = ObservedVersions::default();
lock(&observed.versions).insert("music_dht".to_string(), 99);
lock(&observed.versions).insert("jam".to_string(), crate::jam::PROTOCOL_VERSION);
let snapshot = ProtocolVersions::snapshot(&observed, true);
let snapshot = ProtocolVersions::snapshot(&observed);
assert_eq!(snapshot.newer.len(), 1);
assert_eq!(snapshot.newer[0].id, "music_dht");
}
+18 -116
View File
@@ -25,7 +25,6 @@ use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht};
use music_dht::similarity_dht::SimilarityDht;
use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN;
use music_dht::{
@@ -45,10 +44,6 @@ pub use capabilities::ProtocolVersions;
pub use catalog::{CATALOG_ALPN, FedAppearsOn, FedArtistCard, FedCardTrack, FedRelease};
pub use similarity::SIMILARITY_ALPN;
/// Cache of the public podcast-discovery overlay (`music_dht::podcast_dht`),
/// next to the similarity overlay's cache in the federation data directory.
pub const PODCAST_DHT_CACHE_FILE: &str = "podcast-dht.sqlite3";
/// How often the published library is re-synchronized with the local index.
const SYNC_INTERVAL: Duration = Duration::from_secs(60);
/// How often the application checks whether the shared transport needs a
@@ -417,9 +412,6 @@ pub struct NetworkLibrarySource {
struct Running {
service: Arc<MusicDhtService>,
similarity_dht: Arc<SimilarityDht>,
/// Public podcast discovery; `None` when its cache could not be opened
/// (discovery is then off and the overlay is not advertised).
podcast_dht: Option<Arc<PodcastDht>>,
network_name: String,
network_id: NetworkId,
tasks: Vec<tokio::task::JoinHandle<()>>,
@@ -446,9 +438,6 @@ pub struct Federation {
last_error: std::sync::Mutex<Option<String>>,
transport_stats: Arc<TransportStats>,
observed_protocols: Arc<capabilities::ObservedVersions>,
/// The running podcast-discovery overlay, for the podcast publication
/// reconciler (`podcasts::publication`); `None` while federation is off.
podcast_dht: tokio::sync::watch::Sender<Option<Arc<PodcastDht>>>,
}
#[derive(Debug, Clone)]
@@ -583,17 +572,9 @@ impl Federation {
last_error: std::sync::Mutex::new(initial_error),
transport_stats: Arc::new(TransportStats::default()),
observed_protocols: Arc::new(capabilities::ObservedVersions::default()),
podcast_dht: tokio::sync::watch::Sender::new(None),
})
}
/// Follows the podcast-discovery overlay of the running node: `Some`
/// while federation runs with the overlay, `None` otherwise. Every
/// (re)start publishes a new value.
pub fn podcast_dht(&self) -> tokio::sync::watch::Receiver<Option<Arc<PodcastDht>>> {
self.podcast_dht.subscribe()
}
pub fn settings(&self) -> FedSettings {
lock(&self.settings).clone()
}
@@ -749,7 +730,6 @@ impl Federation {
} else if recovery_only {
return Ok(false);
}
self.podcast_dht.send_replace(None);
stop_running(guard.take()).await;
} else if recovery_only {
tracing::warn!(network = %network_name, "retrying stopped federation service");
@@ -776,8 +756,6 @@ impl Federation {
.stream_protocol(crate::jam::JAM_ALPN)
// Signed LSH summaries form their own upgrade-safe DHT overlay.
.schema_independent_stream_protocol(SIMILARITY_DHT_ALPN)
// Public cards of podcasts the user chose to share.
.schema_independent_stream_protocol(PODCAST_DHT_ALPN)
// Informational application/protocol versions.
.schema_independent_stream_protocol(capabilities::CAPABILITIES_ALPN)
.build()
@@ -810,18 +788,6 @@ impl Federation {
Arc::clone(&self.similarity),
Arc::clone(&self.library),
));
// Public podcast discovery: serving, bootstrap, republishing and
// expiry run here; what to publish is decided by the podcast
// publication reconciler from the user's share decisions.
let mut podcast_dht_tasks = Vec::new();
let podcast_dht =
open_podcast_dht(&service, &self.data_dir)
.await
.map(|(dht, acceptor)| {
podcast_dht_tasks.push(tokio::spawn(Arc::clone(&dht).serve(acceptor)));
podcast_dht_tasks.push(tokio::spawn(Arc::clone(&dht).maintenance()));
dht
});
// Drain DHT events into the log; the channel is bounded.
let event_task = tokio::spawn(async move {
@@ -895,41 +861,33 @@ impl Federation {
let capabilities_acceptor = service
.stream_acceptor(capabilities::CAPABILITIES_ALPN)
.map_err(|err| anyhow::anyhow!("failed to take capabilities acceptor: {err}"))?;
let capabilities_serve_task = tokio::spawn(capabilities::serve(
capabilities_acceptor,
podcast_dht.is_some(),
));
let capabilities_serve_task = tokio::spawn(capabilities::serve(capabilities_acceptor));
let capabilities_probe_task = tokio::spawn(capabilities::probe_loop(
Arc::clone(&service),
Arc::clone(&self.observed_protocols),
podcast_dht.clone(),
));
let mut tasks = podcast_dht_tasks;
tasks.extend([
event_task,
sync_task,
audio_task,
catalog_task,
similarity_task,
device_sync_task,
device_tick_task,
jam_serve_task,
jam_poll_task,
capabilities_serve_task,
capabilities_probe_task,
similarity_dht_serve_task,
similarity_dht_maintenance_task,
similarity_dht_sync_task,
]);
self.podcast_dht.send_replace(podcast_dht.clone());
*guard = Some(Running {
service,
similarity_dht,
podcast_dht,
network_name,
network_id,
tasks,
tasks: vec![
event_task,
sync_task,
audio_task,
catalog_task,
similarity_task,
device_sync_task,
device_tick_task,
jam_serve_task,
jam_poll_task,
capabilities_serve_task,
capabilities_probe_task,
similarity_dht_serve_task,
similarity_dht_maintenance_task,
similarity_dht_sync_task,
],
});
self.set_error(None);
Ok(true)
@@ -937,7 +895,6 @@ impl Federation {
async fn stop(&self) {
let mut guard = self.running.lock().await;
self.podcast_dht.send_replace(None);
stop_running(guard.take()).await;
}
@@ -1131,12 +1088,7 @@ impl Federation {
recovery_count: self.recovery_count.load(Ordering::Relaxed),
last_sync: lock(&self.last_sync).clone(),
last_error: lock(&self.last_error).clone(),
protocols: ProtocolVersions::snapshot(
&self.observed_protocols,
guard
.as_ref()
.is_some_and(|running| running.podcast_dht.is_some()),
),
protocols: ProtocolVersions::snapshot(&self.observed_protocols),
..FedStatus::default()
};
if let Some(running) = guard.as_ref() {
@@ -1262,10 +1214,6 @@ impl Federation {
/// Searches the federated network: matching tracks plus the artists a
/// card can be assembled for (from artist records and from the artist
/// names of matching tracks/releases).
///
/// Public podcast cards are searched separately
/// ([`Federation::search_podcasts`]), so the music results never wait
/// for the discovery overlay.
pub async fn search(&self, query: &str) -> Result<FedSearchResults> {
let service = self.service().await?;
let normalized = music_dht::normalize_name(query);
@@ -1361,24 +1309,6 @@ impl Federation {
Ok(FedSearchResults { artists, tracks })
}
/// Public podcast cards matching `query` in the discovery overlay
/// (explicit type `podcast`), bounded by
/// [`crate::podcasts::discovery::PODCAST_SEARCH_TIMEOUT`] including the
/// overlay's bootstrap. Empty while the overlay is not running, on an
/// error or on timeout.
pub async fn search_podcasts(
&self,
query: &str,
) -> Vec<crate::podcasts::discovery::PodcastHit> {
let podcast_dht = self.podcast_dht.borrow().clone();
crate::podcasts::discovery::search(
podcast_dht.as_deref(),
query,
crate::podcasts::discovery::PODCAST_SEARCH_TIMEOUT,
)
.await
}
/// Bounded fan-out to known peers using the exact model/profile
/// fingerprint carried with the query. No DHT records are written.
pub async fn search_similar(
@@ -2403,7 +2333,6 @@ fn metadata_preview_track(
.or(base.file_size_bytes),
play_count: base.play_count,
fed: Some(fed.clone()),
podcast: None,
}
}
@@ -2874,31 +2803,6 @@ async fn similarity_route_sync_loop(
}
}
/// Opens the podcast-discovery overlay and takes its stream acceptor. A
/// failure (unreadable or newer cache, …) is logged and only disables public
/// podcast discovery: the acceptor is dropped so the overlay's ALPN refuses
/// streams, and the overlay is not advertised.
async fn open_podcast_dht(
service: &Arc<MusicDhtService>,
data_dir: &Path,
) -> Option<(Arc<PodcastDht>, music_dht::StreamAcceptor)> {
let acceptor = match service.stream_acceptor(PODCAST_DHT_ALPN) {
Ok(acceptor) => acceptor,
Err(err) => {
tracing::warn!("podcast discovery is off: cannot take its acceptor: {err}");
return None;
}
};
match PodcastDht::open(Arc::clone(service), data_dir.join(PODCAST_DHT_CACHE_FILE)).await {
Ok(dht) => Some((dht, acceptor)),
Err(err) => {
tracing::warn!("podcast discovery is off: cannot open its cache: {err}");
drop(acceptor);
None
}
}
}
async fn stop_running(running: Option<Running>) {
let Some(running) = running else { return };
for task in &running.tasks {
@@ -3044,7 +2948,6 @@ pub fn pending_track(fed: &FedTrack) -> TrackItem {
file_size_bytes: None,
play_count: 0,
fed: Some(fed.clone()),
podcast: None,
}
}
@@ -3117,7 +3020,6 @@ 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,
}
}
-32
View File
@@ -135,35 +135,3 @@ fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() {
);
}
}
/// The podcast-discovery overlay opens next to the other overlay caches; a
/// cache that cannot be opened only turns discovery off.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn podcast_discovery_opens_or_turns_itself_off() {
async fn service(dir: &Path) -> Arc<MusicDhtService> {
let config = MusicDhtConfig::builder()
.data_dir(dir)
.network_id(NetworkId::from_name("furumi-tui-podcast-dht-open-test"))
.schema_independent_stream_protocol(PODCAST_DHT_ALPN)
.build()
.unwrap();
let (service, mut events) = MusicDhtService::start(config).await.unwrap();
tokio::spawn(async move { while events.recv().await.is_some() {} });
Arc::new(service)
}
let ok_dir = tempfile::tempdir().unwrap();
let ok = service(ok_dir.path()).await;
let opened = open_podcast_dht(&ok, ok_dir.path()).await;
assert!(opened.is_some());
assert!(ok_dir.path().join(PODCAST_DHT_CACHE_FILE).is_file());
drop(opened);
ok.shutdown().await.unwrap();
// A directory where the cache file belongs cannot be opened as SQLite.
let broken_dir = tempfile::tempdir().unwrap();
std::fs::create_dir(broken_dir.path().join(PODCAST_DHT_CACHE_FILE)).unwrap();
let broken = service(broken_dir.path()).await;
assert!(open_podcast_dht(&broken, broken_dir.path()).await.is_none());
broken.shutdown().await.unwrap();
}
+11 -51
View File
@@ -225,16 +225,10 @@ 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(PlaybackSnapshot {
state: music_dht::device_sync::redact_for_jam(snapshot.state),
..snapshot
});
state.host.playback = Some(snapshot);
}
}
@@ -380,15 +374,7 @@ async fn serve_one(mut stream: ByteStream, manager: Arc<JamManager>) -> 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());
// 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");
}
let _ = manager.event_tx.send(AppEvent::JamCommand(command.command));
}
}
while state.host.seen_order.len() > 4096 {
@@ -670,14 +656,8 @@ 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![crate::devices::PlaybackTrack::podcast_episode(
episode.clone(),
)],
queue: Vec::new(),
queue_pos: 0,
playing: false,
paused: false,
@@ -686,7 +666,6 @@ mod tests {
volume: 73,
shuffle: false,
repeat: crate::devices::PlaybackRepeat::Off,
playback_rate: None,
};
host.publish_host_playback(PlaybackSnapshot {
coordination: None,
@@ -711,27 +690,12 @@ 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,
})
@@ -746,17 +710,13 @@ mod tests {
})
.await
.unwrap();
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());
assert!(matches!(
command,
PlaybackCommand::SetState {
state: crate::devices::PlaybackStateWire { paused: true, .. },
seek: false
}
));
server.abort();
host_drain.abort();
+1 -3
View File
@@ -66,9 +66,7 @@ fn main() -> Result<()> {
// after local guards have been dropped and the terminal has been restored.
let _stderr_capture = capture_stderr();
let (keymap, keymap_warning) = config::keymap::Keymap::load();
// The keymap message may be an informational notice; keep both so a
// logging failure is not hidden behind it.
let startup_warning = app::join_status_messages(keymap_warning, startup_warning);
let startup_warning = keymap_warning.or(startup_warning);
// The app (tokio + TUI) runs on a worker thread; the main thread stays
// dedicated to the OS media-key event loop — on macOS the system only
+15 -265
View File
@@ -1,12 +1,10 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicI16, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::time::Duration;
use rodio::source::SeekError;
use rodio::{ChannelCount, Sample, SampleRate, Source};
use super::spectrum::{Meter, PCM_SAMPLES, StereoReading};
const LEVEL_SCALE: f32 = 1_000_000.0;
const SCOPE_SAMPLES: usize = 256;
const SCOPE_SAMPLE_SCALE: f32 = i16::MAX as f32;
@@ -22,26 +20,6 @@ pub struct AudioAnalysisSnapshot {
pub treble: f64,
pub beat: f64,
pub scope: Vec<f64>,
/// Third-octave band levels (see `spectrum::BAND_CENTERS_HZ`), 0.0..1.0
/// over `spectrum::FLOOR_DB`..0 dBFS, with analyzer ballistics.
pub spectrum: Vec<f64>,
/// Per-band peak-hold markers on the same scale as `spectrum`.
pub spectrum_peaks: Vec<f64>,
/// Left/right RMS levels and held sample peaks on the same dB scale.
/// Mono sources report the same value for both channels.
pub level_l: f64,
pub level_r: f64,
pub peak_l: f64,
pub peak_r: f64,
/// RMS of the side signal (L - R) / 2 on the same scale: the floor for
/// mono sources and for stereo files that carry the same signal twice.
pub level_side: f64,
/// Whether the channels carry different signals: decided on loud windows
/// only (side within 30 dB of the louder channel), `None` until then.
pub stereo: Option<bool>,
/// Sample rate of the analyzed stream in Hz (0 when nothing has played
/// since the player last stopped).
pub sample_rate: u32,
}
#[derive(Debug)]
@@ -54,20 +32,6 @@ pub struct AnalyzerShared {
beat: AtomicU32,
scope_write: AtomicUsize,
scope: Box<[AtomicI16]>,
/// Full-rate mono history for the spectrum; written by the audio thread.
pcm_write: AtomicUsize,
pcm: Box<[AtomicI16]>,
sample_rate: AtomicU32,
/// Per-window channel levels (linear, `LEVEL_SCALE`). Peaks accumulate
/// with `fetch_max` until the meter consumes them.
rms_l: AtomicU32,
rms_r: AtomicU32,
rms_side: AtomicU32,
peak_l: AtomicU32,
peak_r: AtomicU32,
/// Ballistics and FFT state. Only snapshot readers (the UI side) lock it;
/// the audio thread never does.
meter: Mutex<Meter>,
}
impl Default for AnalyzerShared {
@@ -80,27 +44,14 @@ impl Default for AnalyzerShared {
treble: AtomicU32::new(0),
beat: AtomicU32::new(0),
scope_write: AtomicUsize::new(0),
scope: atomic_ring(SCOPE_SAMPLES),
pcm_write: AtomicUsize::new(0),
pcm: atomic_ring(PCM_SAMPLES),
sample_rate: AtomicU32::new(0),
rms_l: AtomicU32::new(0),
rms_r: AtomicU32::new(0),
rms_side: AtomicU32::new(0),
peak_l: AtomicU32::new(0),
peak_r: AtomicU32::new(0),
meter: Mutex::new(Meter::default()),
scope: (0..SCOPE_SAMPLES)
.map(|_| AtomicI16::new(0))
.collect::<Vec<_>>()
.into_boxed_slice(),
}
}
}
fn atomic_ring(len: usize) -> Box<[AtomicI16]> {
(0..len)
.map(|_| AtomicI16::new(0))
.collect::<Vec<_>>()
.into_boxed_slice()
}
impl AnalyzerShared {
pub fn clear(&self) {
self.energy.store(0, Ordering::Relaxed);
@@ -112,37 +63,10 @@ impl AnalyzerShared {
for sample in self.scope.iter() {
sample.store(0, Ordering::Relaxed);
}
self.pcm_write.store(0, Ordering::Relaxed);
for sample in self.pcm.iter() {
sample.store(0, Ordering::Relaxed);
}
for level in [
&self.rms_l,
&self.rms_r,
&self.rms_side,
&self.peak_l,
&self.peak_r,
] {
level.store(0, Ordering::Relaxed);
}
self.sequence.store(0, Ordering::Relaxed);
}
/// Like [`Self::clear`], and also forgets the sample rate: nothing is
/// loaded any more.
pub fn reset(&self) {
self.clear();
self.sample_rate.store(0, Ordering::Relaxed);
}
pub fn snapshot(&self) -> AudioAnalysisSnapshot {
self.snapshot_at(Instant::now())
}
/// Like [`Self::snapshot`] with an explicit clock, so meter ballistics are
/// deterministic in tests. Ballistics are time-based: how often callers
/// read does not change how fast bars rise or fall.
pub fn snapshot_at(&self, now: Instant) -> AudioAnalysisSnapshot {
let write = self.scope_write.load(Ordering::Relaxed);
let len = self.scope.len().max(1);
let scope = (0..self.scope.len())
@@ -152,25 +76,6 @@ impl AnalyzerShared {
})
.collect();
let sample_rate = self.sample_rate.load(Ordering::Relaxed);
let pcm_write = self.pcm_write.load(Ordering::Relaxed);
let stereo = StereoReading {
rms_l: load_norm(&self.rms_l) as f32,
rms_r: load_norm(&self.rms_r) as f32,
rms_side: load_norm(&self.rms_side) as f32,
peak_l: f64::from(self.peak_l.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
peak_r: f64::from(self.peak_r.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
};
let reading = {
let mut meter = self
.meter
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
meter.update(now, pcm_write, sample_rate, stereo, |out| {
self.copy_pcm(pcm_write, out)
})
};
AudioAnalysisSnapshot {
sequence: self.sequence.load(Ordering::Relaxed),
energy: load_norm(&self.energy),
@@ -179,31 +84,6 @@ impl AnalyzerShared {
treble: load_norm(&self.treble),
beat: load_norm(&self.beat),
scope,
spectrum: reading.spectrum,
spectrum_peaks: reading.spectrum_peaks,
level_l: reading.level_l,
level_r: reading.level_r,
peak_l: reading.peak_l,
peak_r: reading.peak_r,
level_side: reading.level_side,
stereo: reading.stereo,
sample_rate,
}
}
/// Copies the newest `out.len()` mono samples, oldest first. Slots not
/// written since the last clear read as silence.
fn copy_pcm(&self, write: usize, out: &mut [f32]) {
let len = self.pcm.len();
let count = out.len().min(len);
let start = write.wrapping_sub(count);
for (offset, slot) in out.iter_mut().enumerate() {
*slot = if offset < count && write >= count - offset {
let index = start.wrapping_add(offset) % len;
f32::from(self.pcm[index].load(Ordering::Relaxed)) / SCOPE_SAMPLE_SCALE
} else {
0.0
};
}
}
@@ -216,22 +96,6 @@ impl AnalyzerShared {
self.sequence.fetch_add(1, Ordering::Relaxed);
}
fn push_pcm(&self, sample: f32) {
let index = self.pcm_write.fetch_add(1, Ordering::Relaxed) % self.pcm.len();
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
self.pcm[index].store(value, Ordering::Relaxed);
}
fn store_channels(&self, sample_rate: f32, rms: [f32; 3], peak: [f32; 2]) {
self.sample_rate
.store(sample_rate.round() as u32, Ordering::Relaxed);
store_norm(&self.rms_l, rms[0]);
store_norm(&self.rms_r, rms[1]);
store_norm(&self.rms_side, rms[2]);
self.peak_l.fetch_max(norm_bits(peak[0]), Ordering::Relaxed);
self.peak_r.fetch_max(norm_bits(peak[1]), Ordering::Relaxed);
}
fn push_scope(&self, sample: f32) {
let index = self.scope_write.fetch_add(1, Ordering::Relaxed) % self.scope.len();
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
@@ -317,12 +181,6 @@ struct AnalyzerState {
mid_alpha: f32,
channel_index: usize,
frame_sum: f32,
/// Left/right of the current frame (mono feeds both).
frame_lr: [f32; 2],
channel_sq: [f32; 2],
/// Sum of squared side samples (L - R) / 2 in the current window.
side_sq: f32,
channel_peak: [f32; 2],
frame_count: usize,
window_frames: usize,
scope_counter: usize,
@@ -350,10 +208,6 @@ impl AnalyzerState {
mid_alpha: 0.0,
channel_index: 0,
frame_sum: 0.0,
frame_lr: [0.0; 2],
channel_sq: [0.0; 2],
side_sq: 0.0,
channel_peak: [0.0; 2],
frame_count: 0,
window_frames: 0,
scope_counter: 0,
@@ -398,10 +252,6 @@ impl AnalyzerState {
fn reset_filters(&mut self) {
self.channel_index = 0;
self.frame_sum = 0.0;
self.frame_lr = [0.0; 2];
self.channel_sq = [0.0; 2];
self.side_sq = 0.0;
self.channel_peak = [0.0; 2];
self.frame_count = 0;
self.scope_counter = 0;
self.low_bass = 0.0;
@@ -425,23 +275,10 @@ impl AnalyzerState {
0.0
};
self.frame_sum += sample;
// First channel is left, second right; extra channels only feed mono.
if self.channel_index < 2 {
self.frame_lr[self.channel_index] = sample;
}
self.channel_index += 1;
if self.channel_index < self.channels {
return;
}
if self.channels == 1 {
self.frame_lr[1] = self.frame_lr[0];
}
for (channel, value) in self.frame_lr.iter().enumerate() {
self.channel_sq[channel] += value * value;
self.channel_peak[channel] = self.channel_peak[channel].max(value.abs());
}
let side = (self.frame_lr[0] - self.frame_lr[1]) * 0.5;
self.side_sq += side * side;
let mono = self.frame_sum / self.channels as f32;
self.channel_index = 0;
@@ -462,7 +299,6 @@ impl AnalyzerState {
self.mid_sq += mid * mid;
self.treble_sq += treble * treble;
self.frame_count += 1;
shared.push_pcm(sample);
self.scope_counter += 1;
if self.scope_counter >= self.scope_stride {
@@ -503,23 +339,10 @@ impl AnalyzerState {
self.smooth_beat,
);
shared.store_channels(
self.sample_rate,
[
(self.channel_sq[0] / frames).sqrt(),
(self.channel_sq[1] / frames).sqrt(),
(self.side_sq / frames).sqrt(),
],
self.channel_peak,
);
self.full_sq = 0.0;
self.bass_sq = 0.0;
self.mid_sq = 0.0;
self.treble_sq = 0.0;
self.channel_sq = [0.0; 2];
self.side_sq = 0.0;
self.channel_peak = [0.0; 2];
self.frame_count = 0;
}
}
@@ -538,11 +361,10 @@ fn smooth_level(previous: f32, next: f32, attack: f32, release: f32) -> f32 {
}
fn store_norm(target: &AtomicU32, value: f32) {
target.store(norm_bits(value), Ordering::Relaxed);
}
fn norm_bits(value: f32) -> u32 {
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32
target.store(
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32,
Ordering::Relaxed,
);
}
fn load_norm(source: &AtomicU32) -> f64 {
@@ -562,24 +384,19 @@ mod tests {
impl TestSource {
fn sine(frames: usize, channels: u16, sample_rate: u32, frequency: f32) -> Self {
let gains = vec![1.0; usize::from(channels.max(1))];
Self::sine_gains(frames, sample_rate, frequency, &gains)
}
/// A sine with a gain per channel (one entry per channel).
fn sine_gains(frames: usize, sample_rate: u32, frequency: f32, gains: &[f32]) -> Self {
let mut samples = Vec::with_capacity(frames * gains.len());
let channels_count = channels.max(1);
let mut samples = Vec::with_capacity(frames * usize::from(channels_count));
for frame in 0..frames {
let time = frame as f32 / sample_rate as f32;
let sample = (std::f32::consts::TAU * frequency * time).sin() * 0.5;
for gain in gains {
samples.push((sample * gain) as Sample);
for _ in 0..channels_count {
samples.push(sample as Sample);
}
}
Self {
samples,
cursor: 0,
channels: ChannelCount::new(u16::try_from(gains.len()).unwrap()).unwrap(),
channels: ChannelCount::new(channels_count).unwrap(),
sample_rate: SampleRate::new(sample_rate).unwrap(),
}
}
@@ -635,71 +452,4 @@ mod tests {
assert!(snapshot.bass > 0.0);
assert!(snapshot.scope.iter().any(|sample| sample.abs() > 0.001));
}
#[test]
fn analyzed_source_feeds_the_spectrum_and_channel_meters() {
let shared = Arc::new(AnalyzerShared::default());
let source = TestSource::sine(48_000, 2, 48_000, 1_000.0);
let analyzed = AnalyzedSource::new(source, Arc::clone(&shared));
for _ in analyzed {}
let snapshot = shared.snapshot();
assert_eq!(snapshot.sample_rate, 48_000);
let bands = crate::player::BAND_CENTERS_HZ;
let at_1k = bands.iter().position(|hz| *hz == 1_000.0).unwrap();
let loudest = snapshot
.spectrum
.iter()
.enumerate()
.max_by(|left, right| left.1.total_cmp(right.1))
.map(|(index, _)| index);
assert_eq!(loudest, Some(at_1k));
assert!(snapshot.level_l > 0.5 && (snapshot.level_l - snapshot.level_r).abs() < 1e-6);
// the same signal in both channels has no side component
assert_eq!(snapshot.level_side, 0.0);
assert_eq!(snapshot.stereo, Some(false));
assert!(snapshot.peak_l >= snapshot.level_l);
}
#[test]
fn analyzed_source_measures_the_side_signal() {
let measure = |gains: &[f32]| {
let shared = Arc::new(AnalyzerShared::default());
let source = TestSource::sine_gains(48_000, 48_000, 1_000.0, gains);
for _ in AnalyzedSource::new(source, Arc::clone(&shared)) {}
shared.snapshot()
};
// R = -L: the side signal (L - R) / 2 equals L
let opposite = measure(&[1.0, -1.0]);
assert!(opposite.level_side > 0.5);
assert!((opposite.level_side - opposite.level_l).abs() < 1e-3);
assert_eq!(opposite.stereo, Some(true));
// mono duplicates L into R
let mono = measure(&[1.0]);
assert_eq!(mono.level_side, 0.0);
assert_eq!(mono.stereo, Some(false));
// a third channel only feeds the mono sum
let surround = measure(&[1.0, 0.0, 1.0]);
assert!(surround.level_side > 0.0 && surround.level_side < surround.level_l);
}
#[test]
fn pcm_history_reads_newest_samples_and_silence_before_the_first() {
let shared = AnalyzerShared::default();
for value in [0.25_f32, 0.5, -0.5] {
shared.push_pcm(value);
}
let mut out = [1.0_f32; 5];
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
let rounded: Vec<f32> = out
.iter()
.map(|value| (value * 4.0).round() / 4.0)
.collect();
assert_eq!(rounded, [0.0, 0.0, 0.25, 0.5, -0.5]);
shared.clear();
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
assert!(out.iter().all(|value| *value == 0.0));
}
}
+31 -260
View File
@@ -6,7 +6,6 @@
mod analyzer;
mod opus;
mod spectrum;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
@@ -16,7 +15,6 @@ use std::time::Duration;
use rodio::{Decoder, DeviceSinkBuilder, Player, Source, stream::MixerDeviceSink};
pub use analyzer::AudioAnalysisSnapshot;
pub use spectrum::{BAND_CENTERS_HZ, FLOOR_DB};
pub trait TrackReadSeek: std::io::Read + std::io::Seek + Send + Sync {}
@@ -43,14 +41,6 @@ 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 {
@@ -61,17 +51,11 @@ 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.
/// Play a source that was already probed and decoded off this thread
/// (remote podcast streams, whose probing waits on the network).
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).
@@ -113,13 +97,6 @@ pub struct Controller {
tx: Sender<Command>,
pub shared: Arc<Shared>,
playback_allowed: Arc<AtomicBool>,
/// 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<AtomicU64>,
/// 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<AtomicBool>,
}
impl Controller {
@@ -131,52 +108,7 @@ 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<u64>, volume: f32) {
self.supersede();
self.start_paused.store(false, Ordering::SeqCst);
let _ = self.tx.send(Command::Play {
reader,
byte_len,
@@ -187,8 +119,6 @@ impl Controller {
}
pub fn play_stream(&self, reader: TrackReader, mime_type: Option<String>, volume: f32) {
self.supersede();
self.start_paused.store(false, Ordering::SeqCst);
let _ = self.tx.send(Command::Play {
reader,
byte_len: None,
@@ -198,22 +128,25 @@ impl Controller {
});
}
/// Play an already decoded source, built with `decode_source_with`
/// outside the audio thread.
pub fn play_source(&self, source: DecodedSource, volume: f32) {
let _ = self.tx.send(Command::PlaySource { source, volume });
}
pub fn enqueue(&self, reader: TrackReader, byte_len: Option<u64>) {
let _ = self.tx.send(Command::Enqueue { reader, byte_len });
}
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);
}
@@ -232,73 +165,31 @@ 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,
Generations {
current: thread_generation,
start_paused: thread_start_paused,
},
on_event,
)
})
.spawn(move || run(rx, thread_shared, thread_allowed, 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<AtomicU64>,
start_paused: Arc<AtomicBool>,
}
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<Command>,
shared: Arc<Shared>,
playback_allowed: Arc<AtomicBool>,
generations: Generations,
on_event: impl Fn(PlayerEvent),
) {
let generation = &generations.current;
let mut output: Option<Output> = None;
let mut playing = Playing {
track_loaded: false,
base: Duration::ZERO,
generation: generation.load(Ordering::SeqCst),
};
let mut track_loaded = false;
let mut last_len = 0usize;
loop {
@@ -315,60 +206,26 @@ fn run(
{
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,
&generations.start_paused,
&mut output,
&mut playing,
&on_event,
);
if let Some(started_under) = started_under {
playing.generation = started_under;
}
handle(command, &shared, &mut output, &mut track_loaded, &on_event);
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(position.as_millis() as u64, Ordering::Relaxed);
.store(out.player.get_pos().as_millis() as u64, Ordering::Relaxed);
shared
.paused
.store(out.player.is_paused(), Ordering::Relaxed);
let len = out.player.len();
if playing.track_loaded && len < last_len {
// The next (gapless) source starts at zero.
playing.base = Duration::ZERO;
if track_loaded && len < last_len {
if len == 0 {
playing.track_loaded = false;
track_loaded = false;
}
// 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 });
}
for _ in len..last_len {
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
}
}
last_len = len;
@@ -382,9 +239,8 @@ fn run(
fn handle(
command: Command,
shared: &Arc<Shared>,
start_paused: &AtomicBool,
output: &mut Option<Output>,
playing: &mut Playing,
track_loaded: &mut bool,
on_event: &impl Fn(PlayerEvent),
) {
match command {
@@ -399,31 +255,16 @@ fn handle(
return;
};
match decode_source(reader, byte_len, mime_type.as_deref(), seekable) {
Ok(source) => start_source(out, source, volume, false, shared, on_event),
Ok(source) => start_source(out, source, volume, shared, track_loaded, 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::PlaySource { source, volume } => {
if let Some(out) = ensure_output(output, on_event) {
start_source(out, source, volume, shared, track_loaded, on_event);
}
}
Command::Enqueue { reader, byte_len } => {
let Some(out) = output.as_ref() else {
@@ -455,26 +296,16 @@ fn handle(
if let Some(out) = output.take() {
out.player.stop();
}
shared.analysis.reset();
playing.track_loaded = false;
playing.base = Duration::ZERO;
shared.analysis.clear();
*track_loaded = false;
}
Command::Seek(position) => {
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"),
if let Some(out) = output
&& let Err(err) = out.player.try_seek(position)
{
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 {
@@ -513,8 +344,8 @@ fn start_source(
out: &Output,
source: DecodedSource,
volume: f32,
paused: bool,
shared: &Shared,
track_loaded: &mut bool,
on_event: &impl Fn(PlayerEvent),
) {
shared.analysis.clear();
@@ -524,11 +355,8 @@ fn start_source(
source,
Arc::clone(&shared.analysis),
));
if paused {
out.player.pause();
} else {
out.player.play();
}
out.player.play();
*track_loaded = true;
on_event(PlayerEvent::Started);
}
@@ -580,60 +408,3 @@ pub(crate) fn decode_source_with(
.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<Command>) {
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());
}
}
-650
View File
@@ -1,650 +0,0 @@
//! Display meter for visualizations: a third-octave spectrum and stereo levels
//! with analyzer ballistics (fast attack, steady fall, peak hold).
//!
//! The audio thread only writes samples and per-window channel levels into
//! `AnalyzerShared`; the FFT and all smoothing run here, on the reader side,
//! when a snapshot is taken. Ballistics are time-based: rises and falls
//! advance with the clock, not with the number of snapshots.
use std::sync::Arc;
use std::time::Instant;
use rustfft::num_complex::Complex;
use rustfft::{Fft, FftPlanner};
/// Mono samples kept for the spectrum: enough for an ~85 ms window at
/// 192 kHz. The FFT uses the newest `fft_len(rate)` of them.
pub const PCM_SAMPLES: usize = 16_384;
/// Bottom of the display scale: values are normalized from `FLOOR_DB`..0 dBFS
/// to 0.0..1.0, so a full-scale sine reads 1.0.
pub const FLOOR_DB: f32 = -60.0;
/// Nominal ISO third-octave band centres (Hz), 20 Hz .. 20 kHz. These are
/// labels; band edges come from the exact base-2 centres (see `exact_center`)
/// so neighbouring bands meet without gaps or overlaps.
pub const BAND_CENTERS_HZ: [f32; 31] = [
20.0, 25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0, 400.0,
500.0, 630.0, 800.0, 1_000.0, 1_250.0, 1_600.0, 2_000.0, 2_500.0, 3_150.0, 4_000.0, 5_000.0,
6_300.0, 8_000.0, 10_000.0, 12_500.0, 16_000.0, 20_000.0,
];
const BANDS: usize = BAND_CENTERS_HZ.len();
/// Index of the 1 kHz band.
const KHZ_BAND: i32 = 17;
const MIN_FFT: usize = 4096;
const BAND_ATTACK_SECS: f32 = 0.025;
const BAND_FALL_DB_PER_SEC: f32 = 36.0;
const PEAK_HOLD_SECS: f32 = 1.2;
const PEAK_FALL_DB_PER_SEC: f32 = 18.0;
const LEVEL_ATTACK_SECS: f32 = 0.05;
const LEVEL_RELEASE_SECS: f32 = 0.3;
/// A window counts as stereo when its side signal (L - R) / 2 is no more
/// than this far below the louder channel: lossy coding of a mono recording
/// leaves a side signal far below that, real stereo mixes sit well above it.
const STEREO_SIDE_DB: f32 = -30.0;
/// Only windows this far above the floor decide; quieter ones keep the last
/// decision, since their side signal is lost in the floor.
const STEREO_DECIDE_ABOVE_FLOOR_DB: f32 = 30.0;
/// Longest step applied at once; a stalled reader must not fast-forward a
/// whole second of decay in one frame.
const MAX_STEP_SECS: f32 = 0.25;
/// Audio arrives in device-callback bursts (rodio uses ~46 ms buffers), so a
/// snapshot may see no new samples while playback runs. Only a longer stall
/// (pause, stop) counts as silence.
const STALL_SECS: f32 = 0.15;
/// FFT length for a sample rate: ~85 ms of audio, a power of two.
fn fft_len(sample_rate: u32) -> usize {
(sample_rate as usize * MIN_FFT / 48_000)
.next_power_of_two()
.clamp(MIN_FFT, PCM_SAMPLES)
}
/// Exact base-2 third-octave centre of band `index` (1 kHz at index 17).
fn exact_center(index: usize) -> f32 {
1_000.0 * 2f32.powf((index as i32 - KHZ_BAND) as f32 / 3.0)
}
/// Linear channel levels of the latest analysis window.
#[derive(Debug, Clone, Copy, Default)]
pub struct StereoReading {
pub rms_l: f32,
pub rms_r: f32,
/// RMS of the side signal (L - R) / 2.
pub rms_side: f32,
/// Highest sample peaks since the previous reading.
pub peak_l: f32,
pub peak_r: f32,
}
#[derive(Debug, Clone, Default)]
pub struct MeterReading {
pub spectrum: Vec<f64>,
pub spectrum_peaks: Vec<f64>,
pub level_l: f64,
pub level_r: f64,
pub peak_l: f64,
pub peak_r: f64,
pub level_side: f64,
/// Whether the two channels carry different signals (see
/// `STEREO_SIDE_DB`); `None` until a loud enough window was measured.
pub stereo: Option<bool>,
}
struct Hold {
db: f32,
age: f32,
}
impl Hold {
fn new() -> Self {
Self {
db: FLOOR_DB,
age: 0.0,
}
}
fn update(&mut self, value_db: f32, dt: f32) {
if value_db >= self.db {
self.db = value_db;
self.age = 0.0;
return;
}
self.age += dt;
if self.age > PEAK_HOLD_SECS {
self.db = (self.db - PEAK_FALL_DB_PER_SEC * dt).max(value_db);
}
}
}
/// FFT plan, window and band bin ranges for one sample rate.
struct Analysis {
sample_rate: u32,
fft: Arc<dyn Fft<f32>>,
window: Vec<f32>,
/// Sum of squared window weights, for power normalization.
window_power: f32,
pcm: Vec<f32>,
buffer: Vec<Complex<f32>>,
scratch: Vec<Complex<f32>>,
/// Inclusive bin range per band; `None` above Nyquist.
bins: [Option<(usize, usize)>; BANDS],
}
impl Analysis {
fn new(sample_rate: u32) -> Self {
let len = fft_len(sample_rate);
let fft = FftPlanner::<f32>::new().plan_fft_forward(len);
let scratch = vec![Complex::default(); fft.get_inplace_scratch_len()];
let window: Vec<f32> = (0..len)
.map(|index| {
let phase = std::f32::consts::TAU * index as f32 / len as f32;
0.5 - 0.5 * phase.cos()
})
.collect();
let window_power = window.iter().map(|weight| weight * weight).sum();
Self {
sample_rate,
fft,
window,
window_power,
pcm: vec![0.0; len],
buffer: vec![Complex::default(); len],
scratch,
bins: band_bins(sample_rate, len),
}
}
/// Band power in dBFS (a full-scale sine reads 0 dB in its band). The
/// caller has filled `pcm`.
fn band_targets(&mut self) -> [f32; BANDS] {
for ((slot, sample), weight) in self.buffer.iter_mut().zip(&self.pcm).zip(&self.window) {
*slot = Complex::new(sample * weight, 0.0);
}
self.fft
.process_with_scratch(&mut self.buffer, &mut self.scratch);
// One-sided power per bin, normalized so the bins of a sine sum to
// its mean square (A^2 / 2); doubled below so the sine reads 0 dB.
let scale = 2.0 / (self.buffer.len() as f32 * self.window_power);
let mut out = [FLOOR_DB; BANDS];
for (slot, range) in out.iter_mut().zip(self.bins) {
if let Some((first, last)) = range {
let power: f32 = self.buffer[first..=last]
.iter()
.map(|bin| bin.norm_sqr() * scale)
.sum();
*slot = to_db_power(2.0 * power);
}
}
out
}
}
/// Partitions the FFT bins between the bands: each bin belongs to exactly one
/// band, from the first bin at or above the band's lower edge up to the bin
/// before the next band starts. A band narrower than one bin (deep bass at
/// short windows) reads the bin that contains its centre, which it then shares
/// with its neighbour.
fn band_bins(sample_rate: u32, len: usize) -> [Option<(usize, usize)>; BANDS] {
let bin_hz = sample_rate as f32 / len as f32;
let last_bin = len / 2 - 1;
let nyquist = sample_rate as f32 / 2.0;
let edge = 2f32.powf(1.0 / 6.0);
let first_bin = |index: usize| ((exact_center(index) / edge / bin_hz).ceil() as usize).max(1);
std::array::from_fn(|index| {
let low = exact_center(index) / edge;
if low >= nyquist {
return None;
}
let first = first_bin(index);
let last = if index + 1 < BANDS {
first_bin(index + 1).saturating_sub(1)
} else {
((exact_center(index) * edge / bin_hz).floor() as usize).max(first)
}
.min(last_bin);
if first <= last {
Some((first, last))
} else {
let center = ((exact_center(index) / bin_hz).round() as usize).clamp(1, last_bin);
Some((center, center))
}
})
}
pub struct Meter {
analysis: Option<Analysis>,
bands_db: [f32; BANDS],
band_peaks: [Hold; BANDS],
/// Left, right and side RMS levels.
level_db: [f32; 3],
level_peaks: [Hold; 2],
last_update: Option<Instant>,
last_write: usize,
/// When the PCM counter last moved, and what that audio measured; reused
/// between device callbacks until `STALL_SECS` pass.
last_fresh: Option<Instant>,
held_targets: [f32; BANDS],
held_rms: [f32; 3],
/// Last stereo decision; kept through silence, pauses and stalls.
stereo: Option<bool>,
}
impl std::fmt::Debug for Meter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Meter")
.field("bands_db", &self.bands_db)
.field("level_db", &self.level_db)
.finish_non_exhaustive()
}
}
impl Default for Meter {
fn default() -> Self {
Self {
analysis: None,
bands_db: [FLOOR_DB; BANDS],
band_peaks: std::array::from_fn(|_| Hold::new()),
level_db: [FLOOR_DB; 3],
level_peaks: std::array::from_fn(|_| Hold::new()),
last_update: None,
last_write: 0,
last_fresh: None,
held_targets: [FLOOR_DB; BANDS],
held_rms: [0.0; 3],
stereo: None,
}
}
}
impl Meter {
/// Advances the meter to `now`. `write` is the analyzer's PCM write
/// counter: while it keeps moving the newest audio is measured; once it
/// has not moved for `STALL_SECS` (paused, stopped) the meter is fed
/// silence and falls instead of freezing. `fill_pcm` copies the newest
/// `out.len()` mono samples, oldest first.
pub fn update(
&mut self,
now: Instant,
write: usize,
sample_rate: u32,
stereo: StereoReading,
fill_pcm: impl FnOnce(&mut [f32]),
) -> MeterReading {
let dt = self
.last_update
.map(|last| now.saturating_duration_since(last).as_secs_f32())
.unwrap_or(MAX_STEP_SECS)
.min(MAX_STEP_SECS);
self.last_update = Some(now);
let fresh = write != self.last_write;
self.last_write = write;
let (targets, rms, peaks) = if fresh && sample_rate > 0 {
if self
.analysis
.as_ref()
.is_none_or(|analysis| analysis.sample_rate != sample_rate)
{
self.analysis = Some(Analysis::new(sample_rate));
}
let analysis = self.analysis.as_mut().expect("analysis was just set");
fill_pcm(&mut analysis.pcm);
self.held_targets = analysis.band_targets();
self.held_rms = [stereo.rms_l, stereo.rms_r, stereo.rms_side];
// decided on the raw window, before ballistics: smoothed levels
// converge on the floor together and would read as stereo
let loud = stereo.rms_l.max(stereo.rms_r);
if to_db(loud) > FLOOR_DB + STEREO_DECIDE_ABOVE_FLOOR_DB {
self.stereo = Some(to_db(stereo.rms_side) > to_db(loud) + STEREO_SIDE_DB);
}
self.last_fresh = Some(now);
(
self.held_targets,
self.held_rms,
[stereo.peak_l, stereo.peak_r],
)
} else if self
.last_fresh
.is_some_and(|last| now.saturating_duration_since(last).as_secs_f32() <= STALL_SECS)
{
(self.held_targets, self.held_rms, [0.0; 2])
} else {
([FLOOR_DB; BANDS], [0.0; 3], [0.0; 2])
};
for ((current, target), peak) in self
.bands_db
.iter_mut()
.zip(targets)
.zip(self.band_peaks.iter_mut())
{
*current = follow_band(*current, target, dt);
peak.update(*current, dt);
}
for (level, value) in self.level_db.iter_mut().zip(rms) {
*level = follow_level(*level, to_db(value), dt);
}
for (hold, value) in self.level_peaks.iter_mut().zip(peaks) {
hold.update(to_db(value), dt);
}
MeterReading {
spectrum: self.bands_db.iter().map(|db| normalize(*db)).collect(),
spectrum_peaks: self
.band_peaks
.iter()
.map(|hold| normalize(hold.db))
.collect(),
level_l: normalize(self.level_db[0]),
level_r: normalize(self.level_db[1]),
peak_l: normalize(self.level_peaks[0].db),
peak_r: normalize(self.level_peaks[1].db),
level_side: normalize(self.level_db[2]),
stereo: self.stereo,
}
}
}
fn follow_band(current: f32, target: f32, dt: f32) -> f32 {
if target > current {
current + (target - current) * (1.0 - (-dt / BAND_ATTACK_SECS).exp())
} else {
(current - BAND_FALL_DB_PER_SEC * dt).max(target)
}
}
fn follow_level(current: f32, target: f32, dt: f32) -> f32 {
let tau = if target > current {
LEVEL_ATTACK_SECS
} else {
LEVEL_RELEASE_SECS
};
current + (target - current) * (1.0 - (-dt / tau).exp())
}
fn to_db(amplitude: f32) -> f32 {
if amplitude <= 0.0 || !amplitude.is_finite() {
FLOOR_DB
} else {
(20.0 * amplitude.log10()).max(FLOOR_DB)
}
}
fn to_db_power(power: f32) -> f32 {
if power <= 0.0 || !power.is_finite() {
FLOOR_DB
} else {
(10.0 * power.log10()).max(FLOOR_DB)
}
}
fn normalize(db: f32) -> f64 {
f64::from(((db - FLOOR_DB) / -FLOOR_DB).clamp(0.0, 1.0))
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
fn sine(frequency: f32, amplitude: f32, sample_rate: u32, len: usize) -> Vec<f32> {
(0..len)
.map(|index| {
let time = index as f32 / sample_rate as f32;
amplitude * (std::f32::consts::TAU * frequency * time).sin()
})
.collect()
}
fn band(center: f32) -> usize {
BAND_CENTERS_HZ
.iter()
.position(|value| *value == center)
.unwrap()
}
/// Feeds a steady tone for `steps` snapshots 50 ms apart.
fn settle(meter: &mut Meter, frequency: f32, sample_rate: u32, steps: u64) -> MeterReading {
let samples = sine(frequency, 0.5, sample_rate, PCM_SAMPLES);
let start = Instant::now();
let mut reading = MeterReading::default();
for step in 1..=steps {
reading = meter.update(
start + Duration::from_millis(50 * step),
step as usize,
sample_rate,
StereoReading::default(),
|out| out.copy_from_slice(&samples[..out.len()]),
);
}
reading
}
fn band_powers_db(frequency: f32, sample_rate: u32) -> [f32; BANDS] {
let mut analysis = Analysis::new(sample_rate);
let len = analysis.pcm.len();
analysis
.pcm
.copy_from_slice(&sine(frequency, 1.0, sample_rate, len));
analysis.band_targets()
}
#[test]
fn a_sine_lights_its_own_third_octave_band() {
let reading = settle(&mut Meter::default(), 1_000.0, 48_000, 10);
let at_1k = reading.spectrum[band(1_000.0)];
// -6 dBFS on a 60 dB scale reads 0.9.
assert!((at_1k - 0.9).abs() < 0.03, "1 kHz band read {at_1k}");
assert!(reading.spectrum[band(250.0)] < 0.2);
assert!(reading.spectrum[band(4_000.0)] < 0.2);
assert!(reading.spectrum_peaks[band(1_000.0)] >= at_1k);
}
#[test]
fn bands_cover_the_axis_without_gaps_or_double_counting() {
for sample_rate in [44_100, 48_000, 96_000] {
// Up to just below the top band's upper edge (~22.4 kHz) or
// Nyquist; 1/24-octave steps also land right on band edges.
let top = (sample_rate as f32 / 2.0 * 0.85).min(exact_center(BANDS - 1) * 1.1);
let mut frequency = 80.0_f32;
while frequency < top {
let powers = band_powers_db(frequency, sample_rate);
let loudest = powers.iter().copied().fold(FLOOR_DB, f32::max);
let total: f32 = powers
.iter()
.filter(|db| **db > FLOOR_DB)
.map(|db| 10f32.powf(db / 10.0))
.sum();
assert!(
(-3.5..=0.5).contains(&loudest),
"{frequency} Hz at {sample_rate}: loudest band {loudest} dB"
);
let total_db = 10.0 * total.log10();
assert!(
total_db.abs() < 0.5,
"{frequency} Hz at {sample_rate}: bands sum to {total_db} dB"
);
frequency *= 2f32.powf(1.0 / 24.0);
}
}
}
#[test]
fn deep_bass_reads_at_every_sample_rate() {
for sample_rate in [44_100, 48_000, 96_000, 192_000] {
let powers = band_powers_db(31.5, sample_rate);
assert!(
powers[band(31.5)] > -3.0,
"31.5 Hz at {sample_rate}: {} dB",
powers[band(31.5)]
);
assert!(powers[band(1_000.0)] < -40.0);
}
// Bands above Nyquist stay at the floor.
assert_eq!(band_powers_db(1_000.0, 32_000)[band(20_000.0)], FLOOR_DB);
}
#[test]
fn callback_gaps_keep_the_meter_steady_but_a_stall_falls() {
let mut meter = Meter::default();
let samples = sine(1_000.0, 0.5, 48_000, PCM_SAMPLES);
let tone = StereoReading {
rms_l: 0.35,
rms_r: 0.35,
rms_side: 0.0,
peak_l: 0.5,
peak_r: 0.5,
};
let start = Instant::now();
let mut write = 0;
let mut steady = MeterReading::default();
for step in 1..=40u64 {
// Every third snapshot lands between two device callbacks.
if step % 3 != 0 {
write += 1;
}
let reading = meter.update(
start + Duration::from_millis(50 * step),
write,
48_000,
tone,
|out| out.copy_from_slice(&samples[..out.len()]),
);
if step > 20 {
assert!(
(reading.level_l - steady.level_l).abs() < 0.02 || step == 21,
"level jumped at step {step}"
);
assert!(
(reading.spectrum[band(1_000.0)] - 0.9).abs() < 0.03,
"band jumped at step {step}"
);
}
steady = reading;
}
// The counter stops for longer than a callback gap: paused.
let index = band(1_000.0);
let paused_at = start + Duration::from_millis(2_000);
let first = meter.update(paused_at, write, 48_000, tone, |_| {});
let later = meter.update(
paused_at + Duration::from_millis(500),
write,
48_000,
tone,
|_| {},
);
assert!(later.spectrum[index] < first.spectrum[index]);
assert!(later.level_l < first.level_l);
// The peak marker holds for 1.2 s before it falls too.
assert!((later.spectrum_peaks[index] - 0.9).abs() < 0.03);
let mut reading = later;
for step in 1..=20u64 {
reading = meter.update(
paused_at + Duration::from_millis(500 + 250 * step),
write,
48_000,
tone,
|_| {},
);
}
assert_eq!(reading.spectrum[index], 0.0);
assert!(reading.spectrum_peaks[index] < 0.5);
}
#[test]
fn channel_levels_follow_rms_and_hold_peaks() {
let mut meter = Meter::default();
let start = Instant::now();
let loud_left = StereoReading {
rms_l: 0.5,
rms_r: 0.05,
rms_side: 0.2,
peak_l: 0.9,
peak_r: 0.1,
};
let mut reading = MeterReading::default();
for step in 1..=20 {
reading = meter.update(
start + Duration::from_millis(50 * step),
step as usize,
48_000,
loud_left,
|out| out.fill(0.0),
);
}
assert!(reading.level_l > reading.level_r);
assert!((reading.level_l - normalize(to_db(0.5))).abs() < 0.02);
assert!((reading.peak_l - normalize(to_db(0.9))).abs() < 0.01);
assert!(reading.peak_l >= reading.level_l);
assert!((reading.level_side - normalize(to_db(0.2))).abs() < 0.02);
assert_eq!(reading.stereo, Some(true));
}
#[test]
fn stereo_is_decided_on_loud_windows_and_kept_through_silence() {
let mut meter = Meter::default();
let start = Instant::now();
let mut read = |step: u64, stereo: StereoReading| {
meter.update(
start + Duration::from_millis(50 * step),
step as usize,
48_000,
stereo,
|out| out.fill(0.0),
)
};
let quiet = StereoReading {
rms_l: 0.0005,
rms_r: 0.0001,
rms_side: 0.0002,
..StereoReading::default()
};
assert_eq!(read(1, quiet).stereo, None);
let near_mono = StereoReading {
rms_l: 0.3,
rms_r: 0.3,
rms_side: 0.003,
..StereoReading::default()
};
assert_eq!(read(2, near_mono).stereo, Some(false));
// silence and then a stall keep the decision
assert_eq!(read(3, StereoReading::default()).stereo, Some(false));
let wide = StereoReading {
rms_side: 0.1,
..near_mono
};
assert_eq!(read(4, wide).stereo, Some(true));
let later = meter.update(
start + Duration::from_secs(5),
4,
48_000,
StereoReading::default(),
|out| out.fill(0.0),
);
assert_eq!(later.stereo, Some(true));
}
#[test]
fn silence_and_unknown_rate_read_the_floor() {
let mut meter = Meter::default();
let reading = meter.update(Instant::now(), 1, 0, StereoReading::default(), |_| {});
assert!(reading.spectrum.iter().all(|value| *value == 0.0));
assert_eq!(reading.level_l, 0.0);
assert_eq!(reading.level_side, 0.0);
assert_eq!(reading.spectrum.len(), BAND_CENTERS_HZ.len());
}
#[test]
fn fft_length_tracks_the_sample_rate() {
assert_eq!(fft_len(44_100), 4096);
assert_eq!(fft_len(48_000), 4096);
assert_eq!(fft_len(96_000), 8192);
assert_eq!(fft_len(192_000), 16_384);
assert_eq!(fft_len(0), 4096);
}
}
-257
View File
@@ -1,257 +0,0 @@
//! Public podcast cards found by federated search: their order next to the
//! music results, whether this device is already subscribed, an episode
//! preview read from the feed itself, and subscribing from a card.
//!
//! A card is a publisher's claim. Only its origin feed URL
//! ([`PodcastSearchHit::feed_url`]) is bound to the podcast id, so it is the
//! only URL this module ever fetches or subscribes with; the claimed moved
//! URLs are shown, never used. Nothing from the card is stored: subscribing
//! reads the feed from the podcast's own host like any other subscription,
//! and the new subscription is private.
use anyhow::{Result, bail};
use furumi_library::podcasts::PodcastStore;
use music_dht::LibraryItem;
use music_dht::podcast::{FeedUrl, PodcastId};
use music_dht::podcast_dht::{
FederatedSearchHit, PodcastDht, PodcastSearchHit, merge_search_results,
};
use super::fetch::{FeedBody, Fetcher, Validators};
use super::refresh::{self, Services, SubscribeReport};
/// Podcast hits asked from the discovery overlay per search.
pub const PODCAST_SEARCH_LIMIT: usize = 20;
/// Upper bound of one podcast search, the overlay's bootstrap included (the
/// overlay bounds only its lookups). The music lookup deadline.
pub const PODCAST_SEARCH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);
/// Episodes kept in a preview (newest first).
pub const PREVIEW_EPISODES: usize = 200;
/// One public podcast in the search results.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PodcastHit {
pub hit: PodcastSearchHit,
/// The id this device gives the hit's origin URL
/// (`PodcastStore::resolve_podcast_id`): the card's id, unless the URL
/// is a known alias of a podcast this device already has.
pub local_id: PodcastId,
}
impl PodcastHit {
/// A hit not yet resolved against the local store.
pub fn new(hit: PodcastSearchHit) -> Self {
let local_id = hit.podcast_id.clone();
Self { hit, local_id }
}
/// Ids under which this device may know the podcast.
pub fn ids(&self) -> [&PodcastId; 2] {
[&self.local_id, &self.hit.podcast_id]
}
pub fn title(&self) -> &str {
&self.hit.card.title
}
}
/// The podcast hits of a federated search, in the order of
/// [`merge_search_results`] (exact title matches first). The TUI keeps its
/// own sections for the music results and searches podcasts separately;
/// the order of the podcast entries does not depend on the music items
/// (the merge is a stable exact-title partition), so [`search`] passes none.
pub fn podcast_hits(
query: &str,
music: Vec<LibraryItem>,
podcasts: Vec<PodcastSearchHit>,
) -> Vec<PodcastHit> {
merge_search_results(query, music, podcasts)
.into_iter()
.filter_map(|hit| match hit {
FederatedSearchHit::Podcast(hit) => Some(PodcastHit::new(hit)),
FederatedSearchHit::Music(_) => None,
})
.collect()
}
/// Searches the discovery overlay for public podcast cards, independently
/// of the music search. Bounded by `timeout` (the overlay bootstraps first
/// when it knows no peers, which its own lookup deadline does not cover).
/// No overlay, an error or a timeout give no hits; the warning names
/// neither the query nor any URL.
pub async fn search(
dht: Option<&PodcastDht>,
query: &str,
timeout: std::time::Duration,
) -> Vec<PodcastHit> {
let Some(dht) = dht else {
return Vec::new();
};
match tokio::time::timeout(timeout, dht.search(query, PODCAST_SEARCH_LIMIT)).await {
Ok(Ok(hits)) => podcast_hits(query, Vec::new(), hits),
Ok(Err(err)) => {
tracing::warn!("podcast discovery search failed: {err}");
Vec::new()
}
Err(_) => {
tracing::warn!(
timeout_s = timeout.as_secs(),
"podcast discovery search timed out"
);
Vec::new()
}
}
}
/// Resolves each hit's origin URL against the local store (feed aliases
/// included). Blocking.
pub fn resolve_local_ids(store: &PodcastStore, hits: &mut [PodcastHit]) -> Result<()> {
for hit in hits {
hit.local_id = store.resolve_podcast_id(&hit.hit.feed_url)?;
}
Ok(())
}
/// Subscribes to the podcast of a public card with its origin URL, never a
/// claimed one. The feed is read from its own host (following its own
/// redirects and `itunes:new-feed-url`), the subscription is private and
/// none of the card's metadata is stored.
pub async fn subscribe_from_hit(
services: &Services,
hit: &PodcastSearchHit,
now_ms: i64,
) -> Result<SubscribeReport> {
if PodcastId::from_feed_url(&hit.feed_url) != hit.podcast_id {
bail!("the card's feed URL does not match its podcast id");
}
refresh::subscribe_url(services, hit.feed_url.clone(), now_ms).await
}
/// One episode of a preview.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreviewEpisode {
pub title: String,
pub season: Option<u32>,
pub episode_number: Option<u32>,
pub published_at_ms: Option<i64>,
pub duration_ms: Option<i64>,
}
/// What a podcast's own feed says, read without subscribing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeedPreview {
/// Channel title in the feed (may differ from the card's claim).
pub title: String,
pub episode_count: usize,
/// Season number (`None`: episodes without one) and episode count,
/// seasons in ascending order, unnumbered last.
pub seasons: Vec<(Option<u32>, usize)>,
/// Newest first, at most [`PREVIEW_EPISODES`].
pub episodes: Vec<PreviewEpisode>,
/// The feed answered from, or announced, another address.
pub moved_to: Option<FeedUrl>,
/// This device is subscribed to the podcast under `moved_to` (it
/// followed the feed's newer address, the card names the older one):
/// the card shows «Уже подписан». Set by [`preview_for`].
pub subscribed_as: Option<PodcastId>,
}
/// Reads the feed at `url` (a card's origin URL) for a preview. Nothing is
/// stored and no fetch state is recorded.
pub async fn preview(fetcher: &Fetcher, url: &FeedUrl) -> Result<FeedPreview> {
let response = fetcher.fetch_feed(url, Validators::default()).await?;
let FeedBody::Modified { body, .. } = response.body else {
bail!("the server answered 304 Not Modified to an unconditional request");
};
let feed = refresh::parse(body, &response.final_url).await?;
let announced = feed
.new_feed_url
.as_deref()
.and_then(|url| FeedUrl::parse(url).ok());
let moved_to = response
.moved_permanently_to
.or(announced)
.filter(|moved| moved != url);
let mut seasons: std::collections::BTreeMap<(bool, u32), usize> = Default::default();
for episode in &feed.episodes {
let key = match episode.season {
Some(season) => (false, season),
None => (true, 0),
};
*seasons.entry(key).or_default() += 1;
}
let mut episodes: Vec<PreviewEpisode> = feed
.episodes
.iter()
.map(|episode| PreviewEpisode {
title: episode.title.clone(),
season: episode.season,
episode_number: episode.episode_number,
published_at_ms: episode.published_at_ms,
duration_ms: episode.duration_ms,
})
.collect();
episodes.sort_by_key(|episode| std::cmp::Reverse(episode.published_at_ms));
episodes.truncate(PREVIEW_EPISODES);
Ok(FeedPreview {
title: feed.title.clone(),
episode_count: feed.episodes.len(),
seasons: seasons
.into_iter()
.map(|((unnumbered, season), count)| ((!unnumbered).then_some(season), count))
.collect(),
episodes,
moved_to,
subscribed_as: None,
})
}
/// [`preview`], plus whether this device already follows the podcast under
/// the newer address the feed moved to (`subscribed_as`). The store is only
/// read.
pub async fn preview_for(services: &Services, url: &FeedUrl) -> Result<FeedPreview> {
let mut preview = preview(&services.fetcher, url).await?;
if let Some(moved) = preview.moved_to.clone() {
let store = std::sync::Arc::clone(&services.store);
preview.subscribed_as =
tokio::task::spawn_blocking(move || refresh::subscribed_podcast(&store, &moved))
.await??;
}
Ok(preview)
}
/// A search hit for `feed_url` as one publisher would announce it (tests of
/// the search UI and of subscribing from a card).
#[cfg(test)]
pub(crate) fn test_hit(feed_url: &str, title: &str) -> PodcastSearchHit {
let feed_url = FeedUrl::parse(feed_url).unwrap();
let podcast_id = PodcastId::from_feed_url(&feed_url);
let publisher = music_dht::SecretKey::from_bytes(&[7; 32]).public();
PodcastSearchHit {
podcast_id: podcast_id.clone(),
feed_url: feed_url.clone(),
card: music_dht::podcast_dht::PodcastCard {
version: 1,
network_id: music_dht::NetworkId::from_name("furumi-tui-discovery-test"),
publisher,
podcast_id,
feed_url,
id_feed_url: None,
title: title.to_string(),
author: Some("Ferris Crab".to_string()),
description: Some("Weekly news about Rust.".to_string()),
image_url: None,
link: None,
language: Some("en".to_string()),
categories: vec!["Technology".to_string()],
episode_count: Some(12),
latest_episode_at_ms: Some(1_718_445_600_000),
revision: 1,
issued_at_ms: 1,
expires_at_ms: 2,
},
publishers: vec![publisher],
claimed_feed_urls: Vec::new(),
}
}
-500
View File
@@ -1,500 +0,0 @@
//! Public podcast cards: their order in the search results, the "already
//! subscribed" mark, the feed preview, subscribing from a card, and an
//! end-to-end run over two loopback discovery nodes with a local feed
//! server (no internet, temp directories only).
use std::sync::Arc;
use std::time::Duration;
use furumi_library::podcasts::PodcastListFilter;
use music_dht::NetworkId;
use music_dht::podcast::{FeedUrl, PodcastId};
use music_dht::podcast_dht::{ClaimedFeedUrl, FederatedSearchHit, SearchHitKind};
use super::discovery::{
PODCAST_SEARCH_TIMEOUT, PodcastHit, podcast_hits, preview, preview_for, resolve_local_ids,
search, subscribe_from_hit, test_hit,
};
use super::publication::{Publication, check_share, reconcile, set_shared};
use super::publication_tests::{Node, wait_for};
use super::refresh::{refresh, subscribe};
use super::test_server::Route;
use super::tests::{NOW, fixture, rss};
#[test]
fn podcast_hits_put_exact_titles_first_and_keep_only_podcasts() {
let other = test_hit("https://feeds.example.test/weekly.xml", "Rust Radio Weekly");
let exact = test_hit("https://feeds.example.test/radio.xml", "Rust Radio");
let hits = podcast_hits("rust radio", Vec::new(), vec![other.clone(), exact.clone()]);
let ids: Vec<&PodcastId> = hits.iter().map(|hit| &hit.hit.podcast_id).collect();
assert_eq!(ids, [&exact.podcast_id, &other.podcast_id]);
// Unresolved hits use the card's id; the type marker is "podcast".
assert!(hits.iter().all(|hit| hit.local_id == hit.hit.podcast_id));
assert_eq!(hits[0].hit.kind(), SearchHitKind::Podcast);
assert_eq!(
FederatedSearchHit::Podcast(exact).kind().as_str(),
"podcast"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn podcast_search_is_bounded_and_empty_without_the_overlay() {
// Federation off (no overlay): no hits, nothing to wait for.
assert!(
search(None, "rust radio", PODCAST_SEARCH_TIMEOUT)
.await
.is_empty()
);
// An overlay that knows no overlay peers bootstraps from its music-DHT
// contacts before it searches, outside its own lookup deadline. With a
// contact that no longer answers, the search still ends at the bound.
let temp = tempfile::tempdir().unwrap();
let network = NetworkId::from_name("furumi-tui-podcast-search-bound-test");
let a = Node::start(&temp.path().join("a"), network).await;
let gone = Node::start(&temp.path().join("gone"), network).await;
a.service
.connect(gone.service.ticket().await.unwrap())
.await
.unwrap();
wait_for("a music DHT contact", async || {
(!a.service.known_peers().is_empty()).then_some(())
})
.await;
gone.stop().await;
assert_eq!(a.dht.known_peers(), 0);
let bound = Duration::from_millis(500);
let started = std::time::Instant::now();
let hits = search(Some(&a.dht), "rust radio", bound).await;
let elapsed = started.elapsed();
assert!(hits.is_empty());
assert!(elapsed < bound + Duration::from_secs(1), "took {elapsed:?}");
a.stop().await;
}
#[tokio::test]
async fn subscribing_from_a_card_matches_subscribing_by_url() {
let by_url = fixture();
let from_card = fixture();
by_url.server.set(
"/feed.xml",
Route::new(
rss(&[("a", "Pilot"), ("b", "Second")]),
"application/rss+xml",
),
);
let url = by_url.server.url("/feed.xml");
let typed = subscribe(&by_url.services, &url, NOW).await.unwrap();
let hit = test_hit(&url, "A card title that is only a claim");
let report = subscribe_from_hit(&from_card.services, &hit, NOW)
.await
.unwrap();
assert_eq!(report.podcast_id, typed.podcast_id);
assert_eq!(report.podcast_id, hit.podcast_id);
assert_eq!(report.new_episodes, 2);
// Metadata comes from the feed, never from the card.
assert_eq!(report.title, "Test Show");
// A private subscription: no share decision was written.
assert_eq!(from_card.log.kinds(), ["podcast_subscription_set"]);
let store = &from_card.services.store;
assert_eq!(store.share(&report.podcast_id).unwrap(), None);
let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap();
assert_eq!(rows.len(), 1);
assert!(!rows[0].shared);
assert_eq!(rows[0].meta.title, "Test Show");
// Subscribing again from the card changes nothing.
let again = subscribe_from_hit(&from_card.services, &hit, NOW + 1)
.await
.unwrap();
assert!(again.already_subscribed);
assert_eq!(from_card.log.kinds().len(), 1);
}
#[tokio::test]
async fn subscribing_from_a_card_never_fetches_a_claimed_url() {
let f = fixture();
// The origin URL moved permanently; a publisher also claims a URL of its
// own choosing.
f.server.set(
"/origin.xml",
Route::redirect(301, &f.server.url("/moved.xml")),
);
f.server.set(
"/moved.xml",
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
);
f.server.set(
"/claimed.xml",
Route::new(rss(&[("x", "Imposter")]), "application/rss+xml"),
);
let origin = FeedUrl::parse(&f.server.url("/origin.xml")).unwrap();
let moved = FeedUrl::parse(&f.server.url("/moved.xml")).unwrap();
let claimed = FeedUrl::parse(&f.server.url("/claimed.xml")).unwrap();
let mut hit = test_hit(origin.as_str(), "Rust Radio");
hit.claimed_feed_urls = vec![ClaimedFeedUrl {
feed_url: claimed.clone(),
publishers: hit.publishers.clone(),
}];
hit.card.feed_url = claimed.clone();
hit.card.id_feed_url = Some(origin.clone());
let report = subscribe_from_hit(&f.services, &hit, NOW).await.unwrap();
assert_eq!(report.podcast_id, PodcastId::from_feed_url(&origin));
assert!(f.server.requests("/claimed.xml").is_empty());
let aliases = f.services.store.feed_aliases(&report.podcast_id).unwrap();
assert!(
aliases.contains(&moved),
"the feed's own redirect is an alias"
);
assert!(
!aliases.contains(&claimed),
"a claim never becomes an alias"
);
// A card whose id is not the hash of its origin URL is refused before
// anything is fetched or stored.
let other = fixture();
let mut forged = test_hit(&f.server.url("/moved.xml"), "Rust Radio");
forged.podcast_id = hit.podcast_id.clone();
let requests = f.server.requests("/moved.xml").len();
assert!(
subscribe_from_hit(&other.services, &forged, NOW)
.await
.is_err()
);
assert_eq!(f.server.requests("/moved.xml").len(), requests);
assert!(other.log.kinds().is_empty());
}
#[tokio::test]
async fn hits_resolve_to_the_podcast_a_feed_alias_belongs_to() {
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 subscribed = subscribe(&f.services, &f.server.url("/old.xml"), NOW)
.await
.unwrap();
// Somebody else subscribed with the new address and shares that card.
let mut hits = vec![
PodcastHit::new(test_hit(&f.server.url("/new.xml"), "Test Show")),
PodcastHit::new(test_hit(
"https://feeds.example.test/unknown.xml",
"Unknown",
)),
];
assert_ne!(hits[0].hit.podcast_id, subscribed.podcast_id);
resolve_local_ids(&f.services.store, &mut hits).unwrap();
assert_eq!(hits[0].local_id, subscribed.podcast_id);
assert!(hits[0].ids().contains(&&subscribed.podcast_id));
assert_eq!(hits[1].local_id, hits[1].hit.podcast_id);
}
#[tokio::test]
async fn a_card_for_an_older_address_finds_the_subscription_under_the_newer_one() {
use furumi_library::podcasts::PodcastListFilter;
// The user follows the feed under its newer address; a long-standing
// publisher's card names the older one (the card's origin).
let f = fixture();
let feed = |announce: Option<&str>| {
let announce = announce
.map(|url| format!("<itunes:new-feed-url>{url}</itunes:new-feed-url>"))
.unwrap_or_default();
format!(
"<?xml version=\"1.0\"?><rss version=\"2.0\" \
xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\"><channel>\
<title>Test Show</title>{announce}{}</channel></rss>",
item("a", "Pilot", 1, "Sat, 01 Jun 2024 10:00:00 GMT"),
)
};
let subscribed_rows = || {
f.services
.store
.list_podcasts(PodcastListFilter::Subscribed)
.unwrap()
.len()
};
// 1. The old address redirects permanently to the new one.
f.server
.set("/old.xml", Route::redirect(301, &f.server.url("/new.xml")));
f.server
.set("/new.xml", Route::new(feed(None), "application/rss+xml"));
let followed = subscribe(&f.services, &f.server.url("/new.xml"), NOW)
.await
.unwrap();
let card = test_hit(&f.server.url("/old.xml"), "Test Show");
let url = card.feed_url.clone();
let preview = preview_for(&f.services, &url).await.unwrap();
assert_eq!(preview.subscribed_as.as_ref(), Some(&followed.podcast_id));
let report = subscribe_from_hit(&f.services, &card, NOW).await.unwrap();
assert!(report.already_subscribed);
assert_eq!(report.podcast_id, followed.podcast_id);
assert_eq!(subscribed_rows(), 1);
// 2. The old address only announces the new one (itunes:new-feed-url,
// no redirect): no second subscription for the same podcast.
let new_url = f.server.url("/new2.xml");
f.server
.set("/new2.xml", Route::new(feed(None), "application/rss+xml"));
f.server.set(
"/old2.xml",
Route::new(feed(Some(&new_url)), "application/rss+xml"),
);
let followed = subscribe(&f.services, &new_url, NOW).await.unwrap();
let kinds = f.log.kinds().len();
let card = test_hit(&f.server.url("/old2.xml"), "Test Show");
let url = card.feed_url.clone();
let preview = preview_for(&f.services, &url).await.unwrap();
assert_eq!(preview.moved_to, Some(FeedUrl::parse(&new_url).unwrap()));
assert_eq!(preview.subscribed_as.as_ref(), Some(&followed.podcast_id));
let report = subscribe_from_hit(&f.services, &card, NOW).await.unwrap();
assert!(report.already_subscribed);
assert_eq!(report.podcast_id, followed.podcast_id);
assert_eq!(subscribed_rows(), 2, "one row per podcast");
assert_eq!(f.log.kinds().len(), kinds, "nothing was written");
// A card whose feed moved to an address this device does not follow
// is not marked.
f.server.set(
"/elsewhere.xml",
Route::new(
feed(Some("https://feeds.example.test/unknown.xml")),
"application/rss+xml",
),
);
let url = FeedUrl::parse(&f.server.url("/elsewhere.xml")).unwrap();
let preview = preview_for(&f.services, &url).await.unwrap();
assert!(preview.moved_to.is_some());
assert_eq!(preview.subscribed_as, None);
}
#[tokio::test]
async fn preview_reads_seasons_and_episodes_without_storing_anything() {
let f = fixture();
let new_url = "https://feeds.example.test/new-home.xml";
let feed = format!(
"<?xml version=\"1.0\"?><rss version=\"2.0\" \
xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\"><channel>\
<title>Feed Title</title><itunes:new-feed-url>{new_url}</itunes:new-feed-url>\
{}{}{}{}</channel></rss>",
item("a", "One", 1, "Sat, 01 Jun 2024 10:00:00 GMT"),
item("b", "Two", 1, "Sat, 08 Jun 2024 10:00:00 GMT"),
item("c", "Three", 2, "Sat, 15 Jun 2024 10:00:00 GMT"),
"<item><title>Bonus</title><guid>d</guid>\
<pubDate>Sat, 22 Jun 2024 10:00:00 GMT</pubDate>\
<enclosure url=\"/d.mp3\" type=\"audio/mpeg\"/></item>",
);
f.server
.set("/feed.xml", Route::new(feed, "application/rss+xml"));
let url = FeedUrl::parse(&f.server.url("/feed.xml")).unwrap();
let preview = preview(&f.services.fetcher, &url).await.unwrap();
assert_eq!(preview.title, "Feed Title");
assert_eq!(preview.episode_count, 4);
assert_eq!(preview.seasons, [(Some(1), 2), (Some(2), 1), (None, 1)]);
let titles: Vec<&str> = preview.episodes.iter().map(|e| e.title.as_str()).collect();
assert_eq!(titles, ["Bonus", "Three", "Two", "One"]);
assert_eq!(preview.episodes[1].season, Some(2));
assert_eq!(preview.episodes[1].duration_ms, Some(45 * 60 * 1000));
assert_eq!(
preview.moved_to,
Some(FeedUrl::parse(new_url).unwrap()),
"the feed announced a new address"
);
// Nothing was stored: no podcast, no fetch state, no operation.
let store = &f.services.store;
assert!(
store
.list_podcasts(PodcastListFilter::All)
.unwrap()
.is_empty()
);
assert!(f.log.kinds().is_empty());
}
fn item(guid: &str, title: &str, season: u32, date: &str) -> String {
format!(
"<item><title>{title}</title><guid>{guid}</guid><pubDate>{date}</pubDate>\
<itunes:season>{season}</itunes:season><itunes:duration>45:00</itunes:duration>\
<enclosure url=\"/{guid}.mp3\" type=\"audio/mpeg\"/></item>"
)
}
// ---------------------------------------------------------------------------
// Two loopback nodes and a local feed server
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_stranger_finds_a_shared_card_subscribes_and_refreshes_from_the_feed() {
tokio::time::timeout(Duration::from_secs(240), stranger_subscribes())
.await
.unwrap();
}
async fn stranger_subscribes() {
let temp = tempfile::tempdir().unwrap();
let network = NetworkId::from_name("furumi-tui-podcast-discovery-test");
// A shares a podcast. B is another user's TUI outside A's
// trusted-device group: no device sync between them, only the overlay.
let a = Node::start(&temp.path().join("a"), network).await;
let b = Node::start(&temp.path().join("b"), network).await;
b.service
.connect(a.service.ticket().await.unwrap())
.await
.unwrap();
wait_for("music DHT contacts", async || {
(!a.service.known_peers().is_empty() && !b.service.known_peers().is_empty()).then_some(())
})
.await;
for node in [&a, &b] {
node.dht.bootstrap().await.unwrap();
}
wait_for("overlay contacts", async || {
(a.dht.known_peers() >= 1 && b.dht.known_peers() >= 1).then_some(())
})
.await;
// The podcast's own host (a local HTTP server).
let user_a = fixture();
let user_b = fixture();
let feed = &user_a.server;
feed.set(
"/show.xml",
Route::new(
rss(&[("a", "Pilot"), ("b", "Second")]),
"application/rss+xml",
),
);
let feed_url = FeedUrl::parse(&feed.url("/show.xml")).unwrap();
let private_url = feed.url("/diary.xml");
feed.set(
"/diary.xml",
Route::new(rss(&[("p", "Private entry")]), "application/rss+xml"),
);
// A subscribes to two podcasts and shares one. The local address looks
// private, so A confirms exactly these URLs first (the dialog).
let shared = subscribe(&user_a.services, feed_url.as_str(), NOW)
.await
.unwrap();
let private = subscribe(&user_a.services, &private_url, NOW)
.await
.unwrap();
let store_a = Arc::clone(&user_a.services.store);
let confirmations = Arc::clone(&user_a.services.confirmations);
let check = check_share(&store_a, &confirmations, &shared.podcast_id).unwrap();
set_shared(
&store_a,
&confirmations,
&shared.podcast_id,
true,
check.needs_confirmation().then_some(check.urls),
|| {
user_a
.services
.log
.record(&mut |stamps| store_a.set_shared(stamps, &shared.podcast_id, true))
},
)
.unwrap();
let report = reconcile(
&a.dht,
Some(Arc::clone(&store_a)),
Arc::clone(&confirmations),
)
.await;
assert_eq!(report.error, None);
assert_eq!(
report.podcasts.get(&shared.podcast_id),
Some(&Publication::Published)
);
// B searches like the search view does: overlay search, merged order,
// resolved against B's own store.
let mut hits = wait_for("the shared card", async || {
let found = search(Some(&b.dht), "test show", PODCAST_SEARCH_TIMEOUT).await;
(!found.is_empty()).then_some(found)
})
.await;
assert_eq!(hits.len(), 1, "the private subscription is not found");
resolve_local_ids(&user_b.services.store, &mut hits).unwrap();
let hit = hits.remove(0);
assert_eq!(hit.hit.kind(), SearchHitKind::Podcast);
assert_eq!(hit.hit.podcast_id, shared.podcast_id);
assert_eq!(hit.hit.feed_url, feed_url);
assert_eq!(hit.hit.publishers, vec![a.service.endpoint_id()]);
assert_eq!(hit.local_id, shared.podcast_id);
assert!(
b.dht
.lookup_podcast(&private.podcast_id)
.await
.unwrap()
.is_none()
);
// "Подписаться": B reads the feed from its host, not from A.
let before = feed.requests("/show.xml").len();
let subscribed = subscribe_from_hit(&user_b.services, &hit.hit, NOW)
.await
.unwrap();
assert_eq!(subscribed.podcast_id, shared.podcast_id, "same podcast id");
assert_eq!(subscribed.new_episodes, 2);
assert_eq!(feed.requests("/show.xml").len(), before + 1);
let store_b = &user_b.services.store;
assert_eq!(store_b.share(&subscribed.podcast_id).unwrap(), None);
assert_eq!(user_b.log.kinds(), ["podcast_subscription_set"]);
assert!(
b.dht.published().await.unwrap().is_empty(),
"B publishes nothing"
);
// B refreshes on its own from the feed.
feed.set(
"/show.xml",
Route::new(
rss(&[("a", "Pilot"), ("b", "Second"), ("c", "Third")]),
"application/rss+xml",
),
);
let refreshed = refresh(&user_b.services, &subscribed.podcast_id, &feed_url, NOW + 1)
.await
.unwrap();
assert_eq!(refreshed.new_episodes, 1);
// A stops sharing: the card disappears for B, B's subscription stays.
user_a
.services
.log
.record(&mut |stamps| store_a.set_shared(stamps, &shared.podcast_id, false))
.unwrap();
let report = reconcile(&a.dht, Some(Arc::clone(&store_a)), confirmations).await;
assert_eq!(report.podcasts.get(&shared.podcast_id), None);
assert!(
b.dht
.lookup_podcast(&shared.podcast_id)
.await
.unwrap()
.is_none()
);
assert!(
search(Some(&b.dht), "test show", PODCAST_SEARCH_TIMEOUT)
.await
.is_empty()
);
let rows = store_b
.list_podcasts(PodcastListFilter::Subscribed)
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].podcast_id, shared.podcast_id);
assert!(!rows[0].shared);
let refreshed = refresh(&user_b.services, &subscribed.podcast_id, &feed_url, NOW + 2)
.await
.unwrap();
assert_eq!(refreshed.new_episodes, 0);
a.stop().await;
b.stop().await;
}
+1576
View File
File diff suppressed because it is too large. Load diff
+119 -301
View File
@@ -1,103 +1,32 @@
//! 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.
//! Async HTTP for podcast feeds and cover images.
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::header::{ACCEPT, ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED};
use reqwest::{StatusCode, Url};
use super::feed::{ParsedFeed, parse_feed_with_base};
/// 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";
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.
pub enum FeedFetch {
NotModified,
/// A new document with its validators.
Modified {
body: Vec<u8>,
Fetched {
feed: ParsedFeed,
etag: Option<String>,
last_modified: Option<String>,
},
}
/// 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<FeedUrl>,
/// 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<Duration>,
}
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,
@@ -107,268 +36,107 @@ impl Fetcher {
pub fn new(covers_dir: PathBuf) -> Result<Fetcher> {
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)
.connect_timeout(Duration::from_secs(15))
.timeout(Duration::from_secs(60))
.redirect(reqwest::redirect::Policy::limited(10))
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
.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.
/// Conditional GET of a feed. Parsing runs on the blocking pool.
pub async fn fetch_feed(
&self,
url: &FeedUrl,
validators: Validators<'_>,
) -> Result<FeedResponse> {
within_deadline("feed", self.fetch_feed_unbounded(url, validators)).await
}
async fn fetch_feed_unbounded(
&self,
url: &FeedUrl,
validators: Validators<'_>,
) -> Result<FeedResponse> {
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());
url: &str,
etag: Option<&str>,
last_modified: Option<&str>,
) -> Result<FeedFetch> {
let mut request = self.client.get(url).header(ACCEPT, FEED_ACCEPT);
if let Some(etag) = etag {
request = request.header(IF_NONE_MATCH, etag);
}
if let Some(last_modified) = last_modified {
request = request.header(IF_MODIFIED_SINCE, last_modified);
}
let response = request
.send()
.await
.with_context(|| format!("fetching feed {url}"))?;
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,
});
return Ok(FeedFetch::NotModified);
}
if !status.is_success() {
return Err(status_error(status, response.headers()).into());
bail!("feed request failed: HTTP {status}");
}
let etag = header_string(response.headers(), ETAG);
let last_modified = header_string(response.headers(), LAST_MODIFIED);
let header = |name| {
response
.headers()
.get(name)
.and_then(|value: &reqwest::header::HeaderValue| value.to_str().ok())
.map(str::to_string)
};
let etag = header(ETAG);
let last_modified = header(LAST_MODIFIED);
// Relative enclosure URLs resolve against where the feed really is
// (after redirects).
let base = response.url().to_string();
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,
let feed = tokio::task::spawn_blocking(move || parse_feed_with_base(&body, Some(&base)))
.await
.map_err(|err| anyhow!("feed parser task failed: {err}"))??;
Ok(FeedFetch::Fetched {
feed,
etag,
last_modified,
})
}
/// Downloads a cover image into the covers directory; returns its path.
pub async fn fetch_cover(&self, podcast_id: &PodcastId, image_url: &str) -> Result<PathBuf> {
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")?;
/// Downloads a cover image into the covers directory and returns its path.
pub async fn fetch_cover(&self, podcast_id: i64, image_url: &str) -> Result<PathBuf> {
let response = self
.client
.get(image_url)
.send()
.await
.with_context(|| format!("fetching cover {image_url}"))?;
let status = response.status();
if !status.is_success() {
bail!("cover request failed: HTTP {status}");
}
let body = read_capped(response, MAX_COVER_BYTES, "cover image").await?;
let format = image::guess_format(&body).context("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 hash = blake3::hash(image_url.as_bytes()).to_hex();
let name = format!("{podcast_id}-{}.{extension}", &hash.as_str()[..16]);
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<Vec<u8>> {
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<Followed> {
let mut current = Url::parse(url).with_context(|| format!("invalid URL {url:?}"))?;
check_scheme(&current)?;
// 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(&current)))?;
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<T>(what: &str, fetch: impl Future<Output = Result<T>>) -> Result<T> {
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<Url>,
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<String> {
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::<u64>().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<Vec<u8>> {
if let Some(len) = response.content_length()
&& len > cap as u64
{
bail!("the {what} is too large ({len} bytes, limit {cap})");
bail!("{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}"))?
.with_context(|| format!("reading {what}"))?
{
if body.len() + chunk.len() > cap {
bail!("the {what} is too large (over {cap} bytes)");
bail!("{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<PathBuf> {
static COUNTER: AtomicU64 = AtomicU64::new(0);
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
@@ -387,3 +155,53 @@ fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result<PathBuf> {
result?;
Ok(target)
}
/// Whether `text` starts with `<scheme>://`. Only a prefix counts: a URL
/// embedded in the query (`a.test/rss?src=https://b.test`) is not a scheme.
fn has_scheme(text: &str) -> bool {
let Some((scheme, rest)) = text.split_once(':') else {
return false;
};
let mut chars = scheme.chars();
chars.next().is_some_and(|c| c.is_ascii_alphabetic())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
&& rest.starts_with("//")
}
/// Normalizes user input into a feed URL: trims, maps `feed://`,
/// `podcast://`, `pcast://` and `itpc://` to `https://`, defaults to
/// `https://` without a scheme and accepts only http(s) URLs with a host.
pub fn normalize_feed_url(input: &str) -> Result<String> {
let trimmed = input.trim();
if trimmed.is_empty() {
bail!("feed URL is empty");
}
if trimmed.chars().any(char::is_whitespace) {
bail!("feed URL must not contain whitespace");
}
let lower = trimmed.to_ascii_lowercase();
let mut candidate = trimmed.to_string();
for scheme in ["feed:", "podcast:", "pcast:", "itpc:"] {
if lower.starts_with(scheme) {
let rest = &trimmed[scheme.len()..];
let rest_lower = rest.to_ascii_lowercase();
candidate = if rest_lower.starts_with("http://") || rest_lower.starts_with("https://") {
rest.to_string()
} else {
format!("https://{}", rest.trim_start_matches('/'))
};
break;
}
}
if !has_scheme(&candidate) {
candidate = format!("https://{candidate}");
}
let url = Url::parse(&candidate).with_context(|| format!("invalid feed URL {trimmed:?}"))?;
if !matches!(url.scheme(), "http" | "https") {
bail!("unsupported feed URL scheme {:?}", url.scheme());
}
if url.host_str().is_none_or(str::is_empty) {
bail!("feed URL has no host");
}
Ok(url.to_string())
}
+314 -215
View File
@@ -1,48 +1,22 @@
//! Podcast runtime of the TUI: feed HTTP, refresh scheduling and the glue
//! between the shared podcast store and the device-sync log.
//! Podcast RSS/Atom subscriptions.
//!
//! 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:
//! Furumi stores only subscription and episode *metadata* (SQLite, see
//! [`store`]); audio stays on the publisher's host and is streamed on demand
//! through [`stream`] when an episode is played. Adding a subscription never
//! downloads episodes.
//!
//! * [`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;
//! * [`publication`] publishes the podcasts the user chose to share to the
//! public discovery overlay (`music_dht::podcast_dht`) and revokes them;
//! * [`discovery`] handles public cards found by search: their order, the
//! "already subscribed" mark, an episode preview read from the feed and
//! subscribing from a card.
//!
//! Only RSS metadata is stored. Audio stays on the publisher's server.
//! * [`feed`] parses RSS 2.0 / RSS 1.0 / Atom documents.
//! * [`fetch`] downloads feeds (conditional GET) and cover images.
//! * [`store`] persists podcasts and episodes, deduplicated by GUID.
//! * [`stream`] plays a remote episode through a bounded read-ahead buffer.
//! * [`subscribe`] / [`refresh`] orchestrate the above without blocking
//! Tokio workers.
pub mod discovery;
pub mod feed;
pub mod fetch;
pub mod playback;
pub mod progress;
pub mod publication;
pub mod refresh;
pub mod store;
pub mod stream;
#[cfg(test)]
mod discovery_tests;
#[cfg(test)]
mod playback_tests;
#[cfg(test)]
mod publication_tests;
#[cfg(test)]
mod stream_tests;
#[cfg(test)]
mod test_server;
#[cfg(test)]
@@ -51,190 +25,74 @@ 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,
};
use anyhow::{Result, anyhow, bail};
pub use fetch::Fetcher;
use refresh::Services;
use crate::library::models::{ArtistRef, TrackItem};
/// 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<Vec<PodcastLocalOp>>,
) -> Result<()>;
pub use fetch::{FeedFetch, Fetcher, normalize_feed_url};
pub use store::Store;
// Convenience re-exports used by the module's tests.
#[cfg(test)]
pub use feed::{ParsedEpisode, ParsedFeed, parse_feed, parse_feed_with_base};
#[cfg(test)]
pub use stream::HttpStream;
/// Track ids of podcast episodes are `-(EPISODE_TRACK_ID_BASE + episode.id)`
/// for episode ids in `1..EPISODE_TRACK_ID_BASE`, i.e. the narrow band
/// `-(2^41 - 1)..=-(2^40 + 1)`, far away from library rows (positive) and
/// other synthetic ids.
pub const EPISODE_TRACK_ID_BASE: i64 = 1 << 40;
#[derive(Debug, Clone, PartialEq)]
pub struct Podcast {
pub id: i64,
pub feed_url: String,
pub title: String,
pub author: Option<String>,
/// Plain text (HTML stripped, entities decoded, whitespace collapsed),
/// at most 4000 characters.
pub description: Option<String>,
pub link: Option<String>,
pub image_url: Option<String>,
/// Absolute path of the locally cached cover image, if downloaded.
pub cover_path: Option<String>,
pub episode_count: usize,
/// Unix seconds, max over episodes.
pub latest_published_at: Option<i64>,
/// Unix seconds of the last refresh attempt.
pub last_checked_at: Option<i64>,
/// Error of the last refresh attempt; `None` if it succeeded.
pub last_error: Option<String>,
}
/// 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<PathBuf> {
let dirs = crate::config::project_dirs()
.ok_or_else(|| anyhow!("no home directory for podcast data"))?;
Ok(dirs.data_dir().join(PODCAST_DB_FILE))
#[derive(Debug, Clone, PartialEq)]
pub struct Episode {
/// SQLite rowid, stable across refreshes.
pub id: i64,
pub podcast_id: i64,
pub guid: String,
pub title: String,
/// Plain text, at most 4000 characters.
pub description: Option<String>,
/// Unix seconds.
pub published_at: Option<i64>,
pub duration_seconds: Option<f64>,
pub audio_url: String,
pub mime_type: Option<String>,
pub byte_len: Option<u64>,
pub link: Option<String>,
pub image_url: Option<String>,
}
/// Replaceable cache of downloaded podcast covers.
pub fn covers_dir() -> Result<PathBuf> {
let dirs = crate::config::project_dirs()
.ok_or_else(|| anyhow!("no cache directory for podcast covers"))?;
Ok(dirs.cache_dir().join("podcasts").join("covers"))
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RefreshSummary {
pub new_episodes: usize,
pub updated_episodes: usize,
pub not_modified: bool,
}
/// 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<dyn OpLog>) -> Result<Services> {
// 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:#}"),
}
let confirmations =
publication::ShareConfirmations::open(publication::confirmations_path(db_path));
Ok(Services {
store: Arc::new(store),
fetcher: Arc::new(Fetcher::new(covers_dir)?),
log,
confirmations: Arc::new(confirmations),
})
}
/// 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<usize> {
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::<PodcastStoreError>() {
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<T, F>(work: F) -> Result<T>
/// Runs blocking store/filesystem work off the Tokio workers.
async fn blocking<T, F>(work: F) -> Result<T>
where
T: Send + 'static,
F: FnOnce() -> Result<T> + Send + 'static,
@@ -243,3 +101,244 @@ where
.await
.map_err(|err| anyhow!("background task failed: {err}"))?
}
/// Subscribes to a feed: normalizes the URL, rejects duplicates, fetches and
/// parses the feed (nothing is stored if it is not a feed), stores the
/// podcast with all episode metadata, then downloads the cover (best effort).
pub async fn subscribe(
store: Arc<Store>,
fetcher: &Fetcher,
input_url: &str,
now: i64,
) -> Result<i64> {
let feed_url = normalize_feed_url(input_url)?;
let existing = {
let store = Arc::clone(&store);
let feed_url = feed_url.clone();
blocking(move || store.podcast_by_feed_url(&feed_url)).await?
};
if let Some(podcast) = existing {
bail!("already subscribed to {}", podcast.title);
}
let (feed, etag, last_modified) = match fetcher.fetch_feed(&feed_url, None, None).await? {
FeedFetch::Fetched {
feed,
etag,
last_modified,
} => (feed, etag, last_modified),
FeedFetch::NotModified => {
bail!("server answered 304 Not Modified to an unconditional request")
}
};
let image_url = feed.image_url.clone();
let podcast_id = {
let store = Arc::clone(&store);
blocking(move || {
let id = store.add_podcast(&feed_url, &feed, now)?;
store.set_http_validators(id, etag.as_deref(), last_modified.as_deref())?;
Ok(id)
})
.await?
};
if let Some(image_url) = image_url {
update_cover(&store, fetcher, podcast_id, &image_url).await;
}
Ok(podcast_id)
}
/// Refreshes one subscription with a conditional request. Failures are
/// recorded on the podcast (`last_error`) and returned.
pub async fn refresh(
store: Arc<Store>,
fetcher: &Fetcher,
podcast_id: i64,
now: i64,
) -> Result<RefreshSummary> {
let (podcast, (etag, last_modified)) = {
let store = Arc::clone(&store);
blocking(move || {
let podcast = store
.podcast(podcast_id)?
.ok_or_else(|| anyhow!("podcast {podcast_id} not found"))?;
Ok((podcast, store.http_validators(podcast_id)?))
})
.await?
};
let fetched = fetcher
.fetch_feed(&podcast.feed_url, etag.as_deref(), last_modified.as_deref())
.await;
let (summary, image_url) = match fetched {
Err(err) => {
let message = format!("{err:#}");
let store = Arc::clone(&store);
if let Err(record_err) =
blocking(move || store.record_check(podcast_id, now, Some(&message))).await
{
tracing::warn!("recording podcast refresh error failed: {record_err:#}");
}
return Err(err);
}
Ok(FeedFetch::NotModified) => {
let store = Arc::clone(&store);
blocking(move || store.record_check(podcast_id, now, None)).await?;
let summary = RefreshSummary {
not_modified: true,
..RefreshSummary::default()
};
(summary, podcast.image_url)
}
Ok(FeedFetch::Fetched {
feed,
etag,
last_modified,
}) => {
let image_url = feed.image_url.clone();
let store = Arc::clone(&store);
let applied = blocking(move || {
let summary = store.apply_feed(podcast_id, &feed, now);
match &summary {
Ok(_) => store.set_http_validators(
podcast_id,
etag.as_deref(),
last_modified.as_deref(),
)?,
Err(err) => {
store.record_check(podcast_id, now, Some(&format!("{err:#}")))?;
}
}
summary
})
.await?;
(applied, image_url)
}
};
if let Some(image_url) = image_url {
refresh_cover(&store, fetcher, podcast_id, &image_url).await;
}
Ok(summary)
}
/// Re-downloads the cover when the feed image changed or the cached file is
/// gone. Best effort: failures are only logged.
async fn refresh_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
let source = {
let store = Arc::clone(store);
blocking(move || {
let (url, path) = store.cover_source(podcast_id)?;
let missing = path
.as_deref()
.is_none_or(|path| !Path::new(path).is_file());
Ok((url, missing))
})
.await
};
match source {
Ok((url, missing)) if url.as_deref() == Some(image_url) && !missing => {}
Ok(_) => update_cover(store, fetcher, podcast_id, image_url).await,
Err(err) => tracing::warn!("reading podcast cover state failed: {err:#}"),
}
}
/// Downloads `image_url` as the podcast cover, records it and deletes a
/// superseded cover file. Best effort.
async fn update_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
let path = match fetcher.fetch_cover(podcast_id, image_url).await {
Ok(path) => path,
Err(err) => {
tracing::warn!("downloading podcast cover {image_url} failed: {err:#}");
return;
}
};
let store = Arc::clone(store);
let image_url = image_url.to_string();
let result = blocking(move || {
let new_path = path_string(&path);
if store.podcast(podcast_id)?.is_none() {
// Unsubscribed while the cover was downloading.
let _ = std::fs::remove_file(&path);
return Ok(());
}
let (_, old_path) = store.cover_source(podcast_id)?;
store.set_cover(podcast_id, Some(&image_url), Some(&new_path))?;
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 podcast cover failed: {err:#}");
}
}
fn path_string(path: &Path) -> String {
let absolute: PathBuf = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
absolute.to_string_lossy().into_owned()
}
/// Whether a track path is a remote (http/https) URL rather than a file.
pub fn is_remote_url(value: &str) -> bool {
let value = value.as_bytes();
let has_prefix = |prefix: &[u8]| {
value.len() >= prefix.len() && value[..prefix.len()].eq_ignore_ascii_case(prefix)
};
has_prefix(b"http://") || has_prefix(b"https://")
}
/// The player-facing track for an episode. `file_path` is the remote audio
/// URL; see [`is_remote_url`] and [`stream::open`].
pub fn episode_track(podcast: &Podcast, episode: &Episode) -> TrackItem {
TrackItem {
id: -(EPISODE_TRACK_ID_BASE + episode.id),
title: episode.title.clone(),
track_number: None,
disc_number: None,
duration_seconds: episode.duration_seconds.unwrap_or(0.0),
artists: vec![ArtistRef {
id: -1,
name: podcast.title.clone(),
}],
featured_artists: Vec::new(),
release_id: -1,
release_title: podcast.title.clone(),
release_year: episode.published_at.map(feed::year_from_unix),
file_path: episode.audio_url.clone(),
content_id: None,
cover_path: podcast.cover_path.clone(),
audio_format: episode.mime_type.as_deref().and_then(audio_format),
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: episode
.byte_len
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
play_count: 0,
fed: None,
}
}
/// Inverse of the id mapping in [`episode_track`]. Only the band reserved
/// for episodes maps back, so hash-derived negative ids (for example device
/// sync placeholders spread over the whole negative range) are almost never
/// mistaken for episodes; callers should still confirm with a store lookup.
pub fn episode_id_from_track_id(track_id: i64) -> Option<i64> {
let episode_id = track_id.checked_neg()?.checked_sub(EPISODE_TRACK_ID_BASE)?;
(episode_id > 0 && episode_id < EPISODE_TRACK_ID_BASE).then_some(episode_id)
}
/// Display format from a MIME type: `audio/mpeg` -> `MP3`, otherwise the
/// upper-cased subtype without an `x-` prefix.
fn audio_format(mime: &str) -> Option<String> {
let (kind, subtype) = mime.split(';').next()?.trim().split_once('/')?;
let subtype = subtype.strip_prefix("x-").unwrap_or(subtype);
if subtype.is_empty() {
return None;
}
let audio = kind.eq_ignore_ascii_case("audio");
Some(match subtype.to_ascii_lowercase().as_str() {
"mpeg" | "mp3" | "mpeg3" | "mpg" if audio => "MP3".to_string(),
"mp4" | "m4a" if audio => "M4A".to_string(),
"vorbis" => "OGG".to_string(),
_ => subtype.to_ascii_uppercase(),
})
}
-291
View File
@@ -1,291 +0,0 @@
//! 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<i64>,
/// `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<i64>,
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<PreparedEpisode> {
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<Services>,
queued: PodcastEpisodeRef,
start: StartAt,
now_ms: i64,
cancel: CancelToken,
) -> Result<PreparedEpisode> {
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::<StreamStatusError>()
.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::<Cancelled>().is_some() {
return Cancelled.to_string();
}
if let Some(cannot) = err.downcast_ref::<CannotResume>() {
return cannot.to_string();
}
format!("{err:#}")
}
-836
View File
@@ -1,836 +0,0 @@
//! 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<ProgressEvent>,
}
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::<usize>().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::<CannotResume>(),
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<u8>;
}
impl PipeReplace for Vec<u8> {
fn pipe_replace(self, from: &str, to: &str) -> Vec<u8> {
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<u8> {
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::<super::stream::Cancelled>().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()
);
}
-488
View File
@@ -1,488 +0,0 @@
//! 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<PodcastEpisodeRef>,
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<Sender<ProgressCommand>>,
thread: Option<JoinHandle<()>>,
}
impl ProgressWriter {
pub fn spawn(
services: Services,
on_event: impl Fn(ProgressEvent) + Send + 'static,
) -> Result<ProgressWriter> {
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<ProgressSession>,
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<ProgressCommand>,
store: &Arc<PodcastStore>,
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<Active>,
on_event: &'a F,
}
/// Runs a store action through the sync log and returns its value.
fn record<T>(
log: &dyn OpLog,
mut action: impl FnMut(&mut dyn StampSource) -> Result<(T, Vec<PodcastLocalOp>)>,
) -> Result<T> {
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<F: Fn(ProgressEvent)> 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<ProgressWrite>,
) -> 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(())
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-430
View File
@@ -1,430 +0,0 @@
//! 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<PodcastStore>,
pub fetcher: Arc<Fetcher>,
pub log: Arc<dyn OpLog>,
/// Device-local privacy confirmations for sharing (never synced).
pub confirmations: Arc<super::publication::ShareConfirmations>,
}
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<FeedUrl>,
}
/// 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<Duration>) -> 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<SubscribeReport> {
let url = FeedUrl::from_user_input(input).map_err(|err| anyhow!("{err}"))?;
subscribe_url(services, url, now_ms).await
}
/// Subscribes to `url` exactly as given (see [`subscribe`]).
///
/// Subscribing from a public card passes the card's origin URL here
/// (`podcasts::discovery::subscribe_from_hit`): the id then matches the
/// card's `podcast_id` unless this device already knows the URL as an alias
/// of another podcast, and everything stored comes from the feed itself.
pub async fn subscribe_url(
services: &Services,
url: FeedUrl,
now_ms: i64,
) -> Result<SubscribeReport> {
if let Some(report) = already_subscribed(services, &url).await? {
return Ok(report);
}
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);
// An older address of a feed this device follows under the newer one it
// announces (`itunes:new-feed-url`, no redirect): the same podcast, not
// a second subscription. Nothing is written.
if let Some(announced) = feed
.new_feed_url
.as_deref()
.and_then(|announced| FeedUrl::parse(announced).ok())
.filter(|announced| *announced != url && *announced != landed)
&& let Some(report) = already_subscribed(services, &announced).await?
{
return Ok(report);
}
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)
}
/// The subscription `url` belongs to on this device (directly or as a
/// known feed alias), as an "already subscribed" report.
async fn already_subscribed(services: &Services, url: &FeedUrl) -> Result<Option<SubscribeReport>> {
let store = Arc::clone(&services.store);
let lookup = url.clone();
blocking(move || {
let Some(podcast_id) = subscribed_podcast(&store, &lookup)? else {
return Ok(None);
};
let title = store
.podcast(&podcast_id)?
.map(|summary| summary.meta.title);
Ok(Some(SubscribeReport {
podcast_id,
title: title.unwrap_or_default(),
already_subscribed: true,
new_episodes: 0,
}))
})
.await
}
/// The podcast `url` resolves to on this device (feed aliases included),
/// when it is subscribed. Blocking.
pub(crate) fn subscribed_podcast(store: &PodcastStore, url: &FeedUrl) -> Result<Option<PodcastId>> {
let id = store.resolve_podcast_id(url)?;
let subscribed = store
.subscription(&id)?
.is_some_and(|record| record.subscribed);
Ok(subscribed.then_some(id))
}
fn knows(store: &PodcastStore, url: &FeedUrl) -> Result<bool> {
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<RefreshReport> {
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::<HttpStatusError>()
.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<RefreshReport> {
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.
pub(crate) async fn parse(body: Vec<u8>, base: &FeedUrl) -> Result<ParsedFeed> {
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:#}");
}
}
+504
View File
@@ -0,0 +1,504 @@
//! SQLite persistence for podcast subscriptions and episode metadata.
//!
//! Only metadata is stored; audio stays on the publisher's host. Episodes are
//! unique per `(podcast_id, guid)` and their row ids stay stable across
//! refreshes, so they can be referenced from playback state. Both tables use
//! `AUTOINCREMENT`, so ids of removed podcasts and episodes are never handed
//! out again (a stale reference or an in-flight refresh of a removed podcast
//! can never hit a different subscription).
use std::path::Path;
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use anyhow::{Context, Result, anyhow, bail};
use rusqlite::{Connection, OptionalExtension, Row, params};
use super::feed::{ParsedEpisode, ParsedFeed};
use super::{Episode, Podcast, RefreshSummary};
/// Schema migrations; `PRAGMA user_version` records how many have run.
/// Every statement is idempotent so a partially applied step can be re-run.
const MIGRATIONS: &[&str] = &[
// v1: subscriptions and episode metadata.
"
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);
",
];
const PODCAST_COLUMNS: &str = "
p.id, p.feed_url, p.title, p.author, p.description, p.link, p.image_url,
p.cover_path, p.last_checked_at, p.last_error,
(SELECT COUNT(*) FROM podcast_episodes e WHERE e.podcast_id = p.id),
(SELECT MAX(e.published_at) FROM podcast_episodes e WHERE e.podcast_id = p.id)";
const EPISODE_COLUMNS: &str = "
id, podcast_id, guid, title, description, published_at, duration_seconds,
audio_url, mime_type, byte_len, link, image_url";
pub struct Store {
conn: Mutex<Connection>,
}
impl Store {
/// Opens (creating if needed) the podcast database at `path`.
pub fn open(path: &Path) -> Result<Store> {
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
std::fs::create_dir_all(parent)
.with_context(|| format!("creating {}", parent.display()))?;
}
let conn = Connection::open(path)
.with_context(|| format!("opening podcast database {}", path.display()))?;
Self::init(conn, true)
}
#[cfg(test)]
pub fn open_in_memory() -> Result<Store> {
Self::init(Connection::open_in_memory()?, false)
}
fn init(conn: Connection, wal: bool) -> Result<Store> {
conn.busy_timeout(Duration::from_secs(5))?;
conn.pragma_update(None, "foreign_keys", true)?;
if wal {
// Returns the resulting mode as a row, hence query instead of execute.
let _: String = conn.query_row("PRAGMA journal_mode=WAL", [], |row| row.get(0))?;
}
migrate(&conn)?;
Ok(Store {
conn: Mutex::new(conn),
})
}
fn conn(&self) -> MutexGuard<'_, Connection> {
self.conn.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn list_podcasts(&self) -> Result<Vec<Podcast>> {
let conn = self.conn();
let mut stmt = conn.prepare(&format!(
"SELECT {PODCAST_COLUMNS} FROM podcasts p ORDER BY p.title COLLATE NOCASE, p.id"
))?;
let podcasts = stmt
.query_map([], podcast_from_row)?
.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(podcasts)
}
pub fn podcast(&self, id: i64) -> Result<Option<Podcast>> {
let conn = self.conn();
Ok(conn
.query_row(
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.id = ?1"),
[id],
podcast_from_row,
)
.optional()?)
}
pub fn podcast_by_feed_url(&self, feed_url: &str) -> Result<Option<Podcast>> {
let conn = self.conn();
Ok(conn
.query_row(
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.feed_url = ?1"),
[feed_url],
podcast_from_row,
)
.optional()?)
}
/// Inserts a subscription and all of its episodes in one transaction.
pub fn add_podcast(&self, feed_url: &str, feed: &ParsedFeed, now: i64) -> Result<i64> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let exists: bool = tx.query_row(
"SELECT EXISTS (SELECT 1 FROM podcasts WHERE feed_url = ?1)",
[feed_url],
|row| row.get(0),
)?;
if exists {
bail!("already subscribed");
}
tx.execute(
"INSERT INTO podcasts
(feed_url, title, author, description, link, image_url, added_at, last_checked_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?7)",
params![
feed_url,
feed.title,
feed.author,
feed.description,
feed.link,
feed.image_url,
now
],
)
.map_err(|err| match err {
rusqlite::Error::SqliteFailure(code, _)
if code.extended_code == rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE =>
{
anyhow!("already subscribed")
}
other => other.into(),
})?;
let podcast_id = tx.last_insert_rowid();
upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
tx.commit()?;
Ok(podcast_id)
}
/// Applies a freshly fetched feed: updates channel metadata and upserts
/// episodes by guid. Episodes no longer in the feed are kept.
pub fn apply_feed(
&self,
podcast_id: i64,
feed: &ParsedFeed,
now: i64,
) -> Result<RefreshSummary> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let updated = tx.execute(
"UPDATE podcasts
SET title = ?2, author = ?3, description = ?4, link = ?5, image_url = ?6,
last_checked_at = ?7, last_error = NULL
WHERE id = ?1",
params![
podcast_id,
feed.title,
feed.author,
feed.description,
feed.link,
feed.image_url,
now
],
)?;
if updated == 0 {
bail!("podcast {podcast_id} not found");
}
let summary = upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
tx.commit()?;
Ok(summary)
}
pub fn record_check(&self, podcast_id: i64, now: i64, error: Option<&str>) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET last_checked_at = ?2, last_error = ?3 WHERE id = ?1",
params![podcast_id, now, error],
)?;
Ok(())
}
pub fn http_validators(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
Ok(self
.conn()
.query_row(
"SELECT etag, last_modified FROM podcasts WHERE id = ?1",
[podcast_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()?
.unwrap_or((None, None)))
}
pub fn set_http_validators(
&self,
podcast_id: i64,
etag: Option<&str>,
last_modified: Option<&str>,
) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET etag = ?2, last_modified = ?3 WHERE id = ?1",
params![podcast_id, etag, last_modified],
)?;
Ok(())
}
/// Records the cached cover file and the image URL it was downloaded from.
pub fn set_cover(
&self,
podcast_id: i64,
cover_url: Option<&str>,
cover_path: Option<&str>,
) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET cover_url = ?2, cover_path = ?3 WHERE id = ?1",
params![podcast_id, cover_url, cover_path],
)?;
Ok(())
}
/// `(cover_url, cover_path)`: the image URL the cached cover came from
/// and the cached file.
pub fn cover_source(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
Ok(self
.conn()
.query_row(
"SELECT cover_url, cover_path FROM podcasts WHERE id = ?1",
[podcast_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()?
.unwrap_or((None, None)))
}
/// Episodes of a podcast, newest first (undated episodes last).
pub fn episodes(&self, podcast_id: i64) -> Result<Vec<Episode>> {
let conn = self.conn();
let mut stmt = conn.prepare(&format!(
"SELECT {EPISODE_COLUMNS} FROM podcast_episodes
WHERE podcast_id = ?1
ORDER BY published_at IS NULL, published_at DESC, id DESC"
))?;
let episodes = stmt
.query_map([podcast_id], episode_from_row)?
.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(episodes)
}
#[cfg(test)]
pub fn episode(&self, episode_id: i64) -> Result<Option<Episode>> {
Ok(self
.conn()
.query_row(
&format!("SELECT {EPISODE_COLUMNS} FROM podcast_episodes WHERE id = ?1"),
[episode_id],
episode_from_row,
)
.optional()?)
}
/// Deletes a subscription and its episodes; returns the cached cover path
/// so the caller can delete the file.
pub fn remove_podcast(&self, podcast_id: i64) -> Result<Option<String>> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let cover_path: Option<Option<String>> = tx
.query_row(
"SELECT cover_path FROM podcasts WHERE id = ?1",
[podcast_id],
|row| row.get(0),
)
.optional()?;
// Explicit delete keeps this correct even on a connection where the
// foreign-key cascade is somehow disabled.
tx.execute(
"DELETE FROM podcast_episodes WHERE podcast_id = ?1",
[podcast_id],
)?;
tx.execute("DELETE FROM podcasts WHERE id = ?1", [podcast_id])?;
tx.commit()?;
Ok(cover_path.flatten())
}
#[cfg(test)]
pub(super) fn schema_version(&self) -> Result<i64> {
Ok(self
.conn()
.query_row("PRAGMA user_version", [], |row| row.get(0))?)
}
}
fn migrate(conn: &Connection) -> Result<()> {
let version: i64 = conn.query_row("PRAGMA user_version", [], |row| row.get(0))?;
let target = MIGRATIONS.len() as i64;
if version > target {
bail!("podcast database schema v{version} is newer than this Furumi (v{target})");
}
for (index, sql) in MIGRATIONS.iter().enumerate().skip(version as usize) {
let next = index as i64 + 1;
conn.execute_batch(&format!(
"BEGIN IMMEDIATE; {sql}; PRAGMA user_version = {next}; COMMIT;"
))
.inspect_err(|_| {
let _ = conn.execute_batch("ROLLBACK");
})
.with_context(|| format!("migrating podcast database to v{next}"))?;
}
Ok(())
}
fn podcast_from_row(row: &Row<'_>) -> rusqlite::Result<Podcast> {
let episode_count: i64 = row.get(10)?;
Ok(Podcast {
id: row.get(0)?,
feed_url: row.get(1)?,
title: row.get(2)?,
author: row.get(3)?,
description: row.get(4)?,
link: row.get(5)?,
image_url: row.get(6)?,
cover_path: row.get(7)?,
last_checked_at: row.get(8)?,
last_error: row.get(9)?,
episode_count: usize::try_from(episode_count).unwrap_or(0),
latest_published_at: row.get(11)?,
})
}
fn episode_from_row(row: &Row<'_>) -> rusqlite::Result<Episode> {
let byte_len: Option<i64> = row.get(9)?;
Ok(Episode {
id: row.get(0)?,
podcast_id: row.get(1)?,
guid: row.get(2)?,
title: row.get(3)?,
description: row.get(4)?,
published_at: row.get(5)?,
duration_seconds: row.get(6)?,
audio_url: row.get(7)?,
mime_type: row.get(8)?,
byte_len: byte_len.and_then(|len| u64::try_from(len).ok()),
link: row.get(10)?,
image_url: row.get(11)?,
})
}
/// The mutable columns of an episode row, for change detection.
#[derive(PartialEq)]
struct EpisodeFields {
title: String,
description: Option<String>,
published_at: Option<i64>,
duration_seconds: Option<f64>,
audio_url: String,
mime_type: Option<String>,
byte_len: Option<i64>,
link: Option<String>,
image_url: Option<String>,
}
impl EpisodeFields {
fn from_parsed(episode: &ParsedEpisode) -> EpisodeFields {
EpisodeFields {
title: episode.title.clone(),
description: episode.description.clone(),
published_at: episode.published_at,
duration_seconds: episode.duration_seconds,
audio_url: episode.audio_url.clone(),
mime_type: episode.mime_type.clone(),
byte_len: episode
.byte_len
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
link: episode.link.clone(),
image_url: episode.image_url.clone(),
}
}
}
fn upsert_episodes(
tx: &rusqlite::Transaction<'_>,
podcast_id: i64,
episodes: &[ParsedEpisode],
now: i64,
) -> Result<RefreshSummary> {
let mut summary = RefreshSummary::default();
let mut select = tx.prepare_cached(
"SELECT id, title, description, published_at, duration_seconds, audio_url,
mime_type, byte_len, link, image_url
FROM podcast_episodes WHERE podcast_id = ?1 AND guid = ?2",
)?;
let mut insert = tx.prepare_cached(
"INSERT INTO podcast_episodes
(podcast_id, guid, title, description, published_at, duration_seconds,
audio_url, mime_type, byte_len, link, image_url, first_seen_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
)?;
let mut update = tx.prepare_cached(
"UPDATE podcast_episodes
SET title = ?2, description = ?3, published_at = ?4, duration_seconds = ?5,
audio_url = ?6, mime_type = ?7, byte_len = ?8, link = ?9, image_url = ?10
WHERE id = ?1",
)?;
for episode in episodes {
let fields = EpisodeFields::from_parsed(episode);
let existing = select
.query_row(params![podcast_id, episode.guid], |row| {
Ok((
row.get::<_, i64>(0)?,
EpisodeFields {
title: row.get(1)?,
description: row.get(2)?,
published_at: row.get(3)?,
duration_seconds: row.get(4)?,
audio_url: row.get(5)?,
mime_type: row.get(6)?,
byte_len: row.get(7)?,
link: row.get(8)?,
image_url: row.get(9)?,
},
))
})
.optional()?;
match existing {
None => {
insert.execute(params![
podcast_id,
episode.guid,
fields.title,
fields.description,
fields.published_at,
fields.duration_seconds,
fields.audio_url,
fields.mime_type,
fields.byte_len,
fields.link,
fields.image_url,
now
])?;
summary.new_episodes += 1;
}
Some((id, stored)) if stored != fields => {
update.execute(params![
id,
fields.title,
fields.description,
fields.published_at,
fields.duration_seconds,
fields.audio_url,
fields.mime_type,
fields.byte_len,
fields.link,
fields.image_url
])?;
summary.updated_episodes += 1;
}
Some(_) => {}
}
}
Ok(summary)
}
+20 -197
View File
@@ -1,9 +1,5 @@
//! 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:
@@ -41,18 +37,6 @@
//! 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
@@ -65,11 +49,9 @@ use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail};
use reqwest::StatusCode;
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;
@@ -103,89 +85,9 @@ const STARVED_BACKOFF: Duration = Duration::from_secs(1);
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<Arc<dyn Fn() -> 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<Shared>);
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<u64>,
pub mime_type: Option<String>,
pub seekable: bool,
@@ -199,7 +101,6 @@ pub struct HttpStream {
struct Shared {
state: Mutex<State>,
cancel: CancelToken,
/// Signalled when data, EOF, an error or downloader exit is available to
/// the reader.
data_ready: Condvar,
@@ -222,11 +123,6 @@ struct State {
/// Last position the reader consumed from the window.
reader_pos: u64,
total: Option<u64>,
/// `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,
@@ -288,55 +184,24 @@ impl Shared {
}
}
/// [`open_with`] without cancellation.
#[cfg(test)]
pub fn open(url: &str) -> Result<OpenedStream> {
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<OpenedStream> {
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);
pub fn open(url: &str) -> Result<OpenedStream> {
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)"
))
.redirect(reqwest::redirect::Policy::limited(10))
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
.build()
.context("building the streaming HTTP client")?;
let response = client
.get(parsed)
.get(url)
.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}"))?;
.with_context(|| format!("requesting {url}"))?;
let opened = classify(&response, 0).with_context(|| format!("requesting {url}"))?;
let mime_type = response
.headers()
.get(CONTENT_TYPE)
@@ -356,8 +221,6 @@ pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
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,
@@ -370,22 +233,19 @@ pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
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(),
url: url.to_string(),
};
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,
@@ -428,32 +288,9 @@ fn classify(response: &Response, requested: u64) -> Result<Classified> {
ranges: false,
});
}
Err(StreamStatusError(status.as_u16()).into())
bail!("HTTP {status}");
}
/// 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 <start>-<end>/<total|*>` into `(start, total)`.
fn parse_content_range(value: &str) -> Option<(u64, Option<u64>)> {
let value = value.trim();
@@ -482,7 +319,7 @@ enum Failure {
struct Downloader {
shared: Arc<Shared>,
client: Client,
url: Url,
url: String,
}
/// Marks the downloader as exited however `run` returns (including a panic).
@@ -541,11 +378,7 @@ impl Downloader {
let Some(body) = response.as_mut() else {
continue;
};
let read = body.read(&mut buf);
if self.shared.cancel.is_cancelled() {
return;
}
match read {
match body.read(&mut buf) {
Ok(0) => {
response = None;
let total = self.shared.lock().total;
@@ -587,7 +420,7 @@ impl Downloader {
) -> bool {
let mut state = self.shared.lock();
loop {
if state.closed || self.shared.cancel.is_cancelled() {
if state.closed {
return false;
}
if state.generation != *generation {
@@ -615,9 +448,8 @@ impl Downloader {
state = self
.shared
.space_ready
.wait_timeout(state, CANCEL_POLL)
.unwrap_or_else(PoisonError::into_inner)
.0;
.wait(state)
.unwrap_or_else(PoisonError::into_inner);
}
}
@@ -625,13 +457,13 @@ impl Downloader {
/// `Ok(None)` means the server has nothing at that offset.
fn reopen(&self, offset: u64) -> std::result::Result<Option<(Response, u64)>, Failure> {
let ranges = self.shared.lock().ranges;
let mut request = self.client.get(self.url.clone());
let mut request = self.client.get(&self.url);
if ranges {
request = request.header(RANGE, format!("bytes={offset}-"));
}
let response = request
.send()
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err.without_url()))))?;
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err))))?;
let status = response.status();
if ranges && status == StatusCode::RANGE_NOT_SATISFIABLE {
return Ok(None);
@@ -649,9 +481,8 @@ impl Downloader {
// The server stopped honouring Range: read from 0 and discard.
state.ranges = false;
}
if state.total.is_none() && classified.total.is_some() {
if state.total.is_none() {
state.total = classified.total;
state.declared_total = true;
}
Ok(Some((response, classified.start)))
}
@@ -701,7 +532,7 @@ impl Downloader {
// 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() {
if state.closed {
return false;
}
if state.generation != generation {
@@ -719,7 +550,7 @@ impl Downloader {
state = self
.shared
.space_ready
.wait_timeout(state, (limit - elapsed).min(CANCEL_POLL))
.wait_timeout(state, limit - elapsed)
.unwrap_or_else(PoisonError::into_inner)
.0;
}
@@ -773,11 +604,6 @@ impl Read for HttpStream {
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
}
}
@@ -793,9 +619,6 @@ fn read_locked<'a>(
// 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));
}
@@ -869,7 +692,7 @@ fn read_locked<'a>(
}
state = shared
.data_ready
.wait_timeout(state, (STARVE_TIMEOUT - waited).min(CANCEL_POLL))
.wait_timeout(state, STARVE_TIMEOUT - waited)
.unwrap_or_else(PoisonError::into_inner)
.0;
continue;
-405
View File
@@ -1,405 +0,0 @@
//! 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<u8> {
let mut out = Vec::new();
reader.read_to_end(&mut out).unwrap();
out
}
fn read_n(reader: &mut impl Read, len: usize) -> Vec<u8> {
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<T: Send + Sync + std::io::Read + std::io::Seek>() {}
check::<stream::HttpStream>();
}
/// 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::<stream::Cancelled>()),
"{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");
}
+43 -147
View File
@@ -1,8 +1,5 @@
//! 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.
//! One request per connection (`Connection: close`).
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
@@ -16,20 +13,14 @@ pub(super) struct Route {
pub body: Arc<Vec<u8>>,
pub content_type: String,
pub etag: Option<String>,
pub last_modified: Option<String>,
/// `Location` of a redirect status.
pub location: Option<String>,
pub retry_after: Option<String>,
/// Honour `Range` requests with 206 responses.
pub ranges: bool,
/// Send `Content-Length` (and the total in `Content-Range`).
/// Send `Content-Length` (otherwise the body is close-delimited).
pub send_length: bool,
/// Cut the body after this many bytes (absolute file offset)...
/// Close the connection once the body reaches this absolute file offset,
/// on the first `fail_times` responses (simulates a flaky network).
pub cut_after: Option<usize>,
/// ...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<Duration>,
}
impl Route {
@@ -39,47 +30,10 @@ impl Route {
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")
}
}
@@ -93,26 +47,29 @@ impl Route {
self
}
pub fn last_modified(mut self, value: &str) -> Route {
self.last_modified = Some(value.to_string());
pub fn no_ranges(mut self) -> Route {
self.ranges = false;
self
}
pub fn retry_after(mut self, value: &str) -> Route {
self.retry_after = Some(value.to_string());
pub fn no_length(mut self) -> Route {
self.send_length = false;
self
}
pub fn cut(mut self, after: usize, times: usize) -> Route {
self.cut_after = Some(after);
self.fail_times = times;
self
}
}
/// Headers of one received request (names lowercased).
pub(super) type RequestHeaders = HashMap<String, String>;
#[derive(Default)]
struct ServerState {
routes: HashMap<String, Route>,
requests: HashMap<String, Vec<RequestHeaders>>,
/// Cut responses served so far, per path.
cuts: HashMap<String, usize>,
/// Per path: the `Range` header of every request (None if absent).
requests: HashMap<String, Vec<Option<String>>>,
failures: HashMap<String, usize>,
}
pub(super) struct TestServer {
@@ -144,19 +101,10 @@ impl TestServer {
self.lock().routes.insert(path.to_string(), route);
}
/// Headers of every request received for `path`, in order.
pub fn requests(&self, path: &str) -> Vec<RequestHeaders> {
pub fn requests(&self, path: &str) -> Vec<Option<String>> {
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<Option<String>> {
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)
}
@@ -177,7 +125,7 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
.nth(1)
.unwrap_or("/")
.to_string();
let mut headers = RequestHeaders::new();
let mut headers = HashMap::new();
loop {
let mut line = String::new();
match reader.read_line(&mut line) {
@@ -192,19 +140,20 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_string());
}
}
let range_header = headers.get("range").cloned();
let (route, cut) = {
let mut state = state.lock().unwrap_or_else(PoisonError::into_inner);
state
.requests
.entry(path.clone())
.or_default()
.push(headers.clone());
.push(range_header.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;
let failures = state.failures.entry(path.clone()).or_default();
if *failures < route.fail_times {
*failures += 1;
route.cut_after
} else {
None
@@ -215,38 +164,30 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
(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) {
if let (Some(etag), Some(sent)) = (&route.etag, headers.get("if-none-match"))
&& etag == sent
{
let _ = write!(
out,
"HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n"
"HTTP/1.1 304 Not Modified\r\nETag: {etag}\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 start = 0usize;
let mut end = 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(range) = range_header
.as_deref()
.and_then(|r| r.strip_prefix("bytes="))
&& let Some((from, to)) = range.split_once('-')
&& let Ok(from) = from.trim().parse::<usize>()
{
@@ -257,53 +198,38 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
);
return;
}
status = 206;
start = from;
end = to
let to = to
.trim()
.parse::<usize>()
.map_or(total, |to| (to + 1).min(total));
let size = if route.send_length {
total.to_string()
} else {
"*".to_string()
};
status = 206;
start = from;
end = to;
extra.push_str(&format!(
"Content-Range: bytes {start}-{}/{size}\r\n",
"Content-Range: bytes {start}-{}/{total}\r\n",
end - 1
));
}
if route.ranges {
extra.push_str("Accept-Ranges: bytes\r\n");
}
if let Some(etag) = &route.etag {
extra.push_str(&format!("ETag: {etag}\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",
500 => "Internal Server Error",
_ => "Status",
};
if write!(
out,
"HTTP/1.1 {status} {reason}\r\nContent-Type: {}\r\n{extra}Connection: close\r\n\r\n",
route.content_type,
route.content_type
)
.is_err()
{
@@ -332,33 +258,3 @@ pub(super) fn noise(len: usize, seed: u64) -> Vec<u8> {
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<u8> {
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
}
+1577 -951
View File
File diff suppressed because it is too large. Load diff
-10
View File
@@ -1,11 +1,6 @@
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<String> {
if track.is_podcast() {
return None;
}
track
.fed
.as_ref()
@@ -19,9 +14,6 @@ pub fn track_can_share(track: &TrackItem) -> bool {
}
pub fn track_share_link(track: &TrackItem) -> Option<String> {
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))
@@ -162,7 +154,6 @@ mod tests {
file_size_bytes: None,
play_count: 0,
fed: None,
podcast: None,
};
assert_eq!(
@@ -240,7 +231,6 @@ 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");
+17 -63
View File
@@ -947,7 +947,12 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
let empty_results = SearchResults::default();
let results = match &search.results {
Some(results) => results,
None if state.search.has_federated() => &empty_results,
None if !state.search.fed_tracks.is_empty()
|| !state.search.fed_artists.is_empty()
|| state.search.fed_loading =>
{
&empty_results
}
None => {
let hint = if search.query.is_empty() {
"type to search artists, releases and tracks"
@@ -962,7 +967,11 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
return centered_line(frame, inner, line);
}
};
if results.is_empty() && !state.search.has_federated() {
if results.is_empty()
&& state.search.fed_tracks.is_empty()
&& state.search.fed_artists.is_empty()
&& !state.search.fed_loading
{
return centered_line(frame, inner, Line::styled("nothing found", theme::dim()));
}
@@ -1028,12 +1037,15 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
}
}
// Federated section: artists whose card can be assembled, then tracks
// (both marked with the owning peers), then public podcast cards.
if state.search.has_federated() {
// (both marked with the owning peers).
if !state.search.fed_tracks.is_empty()
|| !state.search.fed_artists.is_empty()
|| state.search.fed_loading
{
if !rows.is_empty() {
rows.push((Line::default(), None, None));
}
let header = if state.search.fed_loading || state.search.fed_podcasts_loading {
let header = if state.search.fed_loading {
"Federation · searching…"
} else {
"Federation"
@@ -1088,23 +1100,6 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
));
index += 1;
}
// Podcasts carry an explicit type marker: they are not music and
// Enter opens their public card, never plays anything.
if !state.search.fed_podcasts.is_empty() {
rows.push((
Line::styled("Podcasts · public cards", theme::header_for(state)),
None,
None,
));
}
for hit in &state.search.fed_podcasts {
rows.push((
podcast_hit_line(state, hit),
Some(podcast_hit_meta(state, hit)),
Some(index),
));
index += 1;
}
}
// Absolute cursor indices covered by an active Shift-V range in the
@@ -1150,47 +1145,6 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
}
}
/// One public podcast in the search results: the `podcast` type marker,
/// the title and the claimed author.
fn podcast_hit_line(
state: &AppState,
hit: &crate::podcasts::discovery::PodcastHit,
) -> Line<'static> {
let card = &hit.hit.card;
let mut detail = String::from(" ");
if let Some(author) = &card.author {
detail.push_str(author);
detail.push_str(" · ");
}
detail.push_str("open the card");
Line::from(vec![
Span::styled(
format!("[{}] ", hit.hit.kind().as_str()),
theme::accent_for(state),
),
Span::raw(card.title.clone()),
Span::styled(detail, theme::dim()),
])
}
/// Right column of a podcast hit: subscribed here, or how many devices
/// publish the card (not a subscriber count).
fn podcast_hit_meta(state: &AppState, hit: &crate::podcasts::discovery::PodcastHit) -> String {
if state.podcasts.subscribed_to(hit) {
return "✓ subscribed".to_string();
}
let mut meta = match hit.hit.card.episode_count {
Some(count) => format!("{count} ep · "),
None => String::new(),
};
let publishers = hit.hit.publishers.len();
meta.push_str(&format!(
"{publishers} publisher{}",
if publishers == 1 { "" } else { "s" }
));
meta
}
fn draw_similarity_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
let search = &state.search;
let status = if search.fed_loading {
+17 -546
View File
@@ -4,7 +4,6 @@ mod federation;
pub(crate) mod global;
mod logs;
mod playlists;
mod podcast_card;
mod podcasts;
mod popup;
#[cfg(test)]
@@ -52,510 +51,6 @@ mod tests {
assert_eq!(label, "●");
assert_eq!(style, client_style);
}
fn render(state: &AppState, width: u16, height: u16) -> String {
let keymap = Keymap::defaults();
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
terminal.draw(|frame| draw(frame, state, &keymap)).unwrap();
let buffer = terminal.backend().buffer();
(0..height)
.map(|y| {
(0..width)
.map(|x| buffer[(x, y)].symbol())
.collect::<String>()
})
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn tab_bar_numbers_tabs_in_the_new_order() {
let screen = render(&AppState::default(), 120, 10);
let bar = screen.lines().next().unwrap();
let positions: Vec<usize> = [
"1 Library",
"2 Playlists",
"3 Queue",
"4 Podcasts",
"5 Settings",
"6 Logs",
]
.iter()
.map(|title| {
bar.find(title)
.unwrap_or_else(|| panic!("{title} in {bar:?}"))
})
.collect();
assert!(positions.is_sorted(), "{bar:?}");
}
/// Each tab's number appears once, in order, and the active tab's label
/// is fully on screen.
fn assert_tab_bar_fits(width: u16, active: Tab, active_label: &str) {
let mut state = AppState::default();
state.active_tab = active;
let screen = render(&state, width, 10);
let bar = screen.lines().next().unwrap();
let digits: String = bar.chars().filter(char::is_ascii_digit).collect();
assert_eq!(digits, "123456", "width {width}: {bar:?}");
assert!(
bar.contains(active_label),
"width {width}: {active_label} missing in {bar:?}"
);
}
#[test]
fn tab_bar_keeps_every_tab_visible_on_narrow_terminals() {
for width in [80, 77, 76, 70, 65, 60, 48, 40, 30] {
assert_tab_bar_fits(width, Tab::Logs, "6 Logs");
assert_tab_bar_fits(width, Tab::Global, "1 Lib");
}
// Full titles while they fit.
assert_tab_bar_fits(80, Tab::Global, "5 Settings");
assert_tab_bar_fits(65, Tab::Global, "5 Settings");
// Numbers only, but the active tab keeps its full title.
assert_tab_bar_fits(30, Tab::Federation, "5 Settings");
assert_tab_bar_fits(30, Tab::Playlists, "2 Playlists");
}
#[test]
fn tab_bar_highlights_active_tab_when_compact() {
let mut state = AppState::default();
state.active_tab = Tab::Logs;
let keymap = Keymap::defaults();
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(60, 10)).unwrap();
terminal.draw(|frame| draw(frame, &state, &keymap)).unwrap();
let buffer = terminal.backend().buffer();
let bar: String = (0..60).map(|x| buffer[(x, 0)].symbol()).collect();
let x = bar.find("6 Logs").expect("active tab on screen") as u16;
let highlight = theme::tab_active_for(&state);
let cell = buffer[(x, 0)].style();
assert_eq!((cell.fg, cell.bg), (highlight.fg, highlight.bg));
}
use crate::app::podcast_fixtures;
#[test]
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("opening the podcast database"), "{screen}");
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 ["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_share_state_is_shown_in_the_list_and_details() {
use crate::podcasts::publication::{Discovery, Publication};
let mut state =
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 3, 0)]);
state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS;
let screen = render(&state, 110, 30);
assert!(screen.contains("private (not shared"), "{screen}");
assert!(!screen.contains("shared · 3 ep"), "{screen}");
let mut row = podcast_fixtures::row("Test Show", 0, 3, 0);
row.summary.shared = true;
let mut state = podcast_fixtures::ready_state(vec![row]);
state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS;
state.podcasts.publication.discovery = Discovery::Running { peers: 2 };
state
.podcasts
.publication
.podcasts
.insert(podcast_fixtures::podcast_id(), Publication::Published);
let screen = render(&state, 110, 30);
assert!(screen.contains("shared · 3 ep"), "{screen}");
assert!(screen.contains("shared · published from"), "{screen}");
state.podcasts.publication.podcasts.insert(
podcast_fixtures::podcast_id(),
Publication::HeldBack(Vec::new()),
);
let screen = render(&state, 110, 30);
assert!(screen.contains("held back"), "{screen}");
// Restarted with other devices in the group: not published before
// a sync with one of them.
state.podcasts.publication.podcasts.insert(
podcast_fixtures::podcast_id(),
Publication::WaitingForDevices,
);
let screen = render(&state, 110, 30);
assert!(screen.contains("waiting for a sync"), "{screen}");
assert!(!screen.contains("published from"), "{screen}");
state.podcasts.publication.discovery = Discovery::Off("federation is off".into());
let screen = render(&state, 110, 30);
assert!(screen.contains("not published:"), "{screen}");
// Sharing turned off here: until a pass revoked the card, the panel
// does not claim the podcast is simply private.
state.podcasts.publication.discovery = Discovery::Running { peers: 2 };
state
.podcasts
.publication
.podcasts
.insert(podcast_fixtures::podcast_id(), Publication::Published);
state
.podcasts
.publication
.note_revocation(&podcast_fixtures::podcast_id(), true);
let screen = render(&state, 110, 30);
assert!(screen.contains("private · revoking the"), "{screen}");
if let Some(crate::app::state::Loadable::Ready(rows)) = &mut state.podcasts.list {
rows[0].summary.shared = false;
}
let screen = render(&state, 110, 30);
assert!(screen.contains("private · revoking the"), "{screen}");
assert!(!screen.contains("private (not shared"), "{screen}");
state.podcasts.publication.discovery = Discovery::Off("federation is off".into());
let screen = render(&state, 110, 30);
assert!(screen.contains("may not be revoked yet:"), "{screen}");
assert!(screen.contains("federation is off"), "{screen}");
state.podcasts.publication.unrevoked.clear();
let screen = render(&state, 110, 30);
assert!(screen.contains("private (not shared"), "{screen}");
}
#[test]
fn the_podcast_card_scrolls_exactly_to_its_last_line() {
use crate::app::state::Popup;
use crate::app::update::podcast_discovery::card_key;
use crossterm::event::KeyCode;
let mut state = search_with_podcasts();
let mut hit = state.search.fed_podcasts[1].clone();
// Long words word-wrap into far more rows than their character
// count suggests (one 50-character word per 90-column row).
let word = "x".repeat(50);
hit.hit.card.description = Some(vec![word.as_str(); 30].join(" "));
state.popup = Some(Popup::PodcastCard {
hit: Box::new(hit),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let last_line = "e — прочитать ленту";
let screen = render(&state, 110, 30);
assert!(!screen.contains(last_line), "below the fold:\n{screen}");
// Paging far past the end stops at the last line ...
for _ in 0..20 {
let popup = state.popup.take().unwrap();
assert_eq!(card_key(&mut state, popup, KeyCode::PageDown), None);
}
let screen = render(&state, 110, 30);
assert!(screen.contains(last_line), "{screen}");
let Some(Popup::PodcastCard {
scroll, max_scroll, ..
}) = &state.popup
else {
panic!("the card is open");
};
assert_eq!(*scroll, max_scroll.get());
let end = *scroll;
// ... and one step back moves the view at once.
let popup = state.popup.take().unwrap();
card_key(&mut state, popup, KeyCode::Up);
assert!(matches!(
&state.popup,
Some(Popup::PodcastCard { scroll, .. }) if *scroll == end - 1
));
}
/// Search view with a federated artist and two public podcast cards;
/// the first podcast is subscribed on this device.
fn search_with_podcasts() -> AppState {
use crate::podcasts::discovery::{PodcastHit, test_hit};
let mut state =
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 3, 0)]);
state.active_tab = Tab::Global;
state.search.query = "show".into();
state.search.results = Some(Default::default());
state.search.fed_artists = vec![crate::federation::FedArtistHit {
name: "Show Band".into(),
peers: 2,
}];
let mut other = test_hit("https://feeds.example.test/other.xml", "Other Show");
other.card.author = None;
state.search.fed_podcasts = vec![
PodcastHit::new(test_hit("https://example.test/feed.xml", "Test Show")),
PodcastHit::new(other),
];
state.global.stack.push(GlobalView::Search { cursor: 1 });
state
}
#[test]
fn search_marks_podcast_hits_apart_from_music() {
let screen = render(&search_with_podcasts(), 120, 20);
assert!(screen.contains("Federation"), "{screen}");
assert!(
screen.contains("Show Band artist · open the card"),
"{screen}"
);
assert!(screen.contains("Podcasts · public cards"), "{screen}");
assert!(
screen.contains("[podcast] Test Show Ferris Crab · open the card"),
"{screen}"
);
assert!(
screen.contains("[podcast] Other Show open the card"),
"{screen}"
);
assert!(screen.contains("✓ subscribed"), "{screen}");
assert!(screen.contains("12 ep · 1 publisher"), "{screen}");
// The podcasts come after the music rows.
let artist = screen.find("Show Band").unwrap();
let podcast = screen.find("[podcast]").unwrap();
assert!(artist < podcast, "{screen}");
}
#[test]
fn podcast_card_offers_subscribe_or_shows_already_subscribed() {
use crate::app::state::{Loadable, Popup};
use crate::podcasts::discovery::{FeedPreview, PreviewEpisode};
let mut state = search_with_podcasts();
let mut hit = state.search.fed_podcasts[1].clone();
hit.hit.claimed_feed_urls = vec![music_dht::podcast_dht::ClaimedFeedUrl {
feed_url: music_dht::podcast::FeedUrl::parse("https://elsewhere.test/claimed.xml")
.unwrap(),
publishers: hit.hit.publishers.clone(),
}];
state.popup = Some(Popup::PodcastCard {
hit: Box::new(hit.clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(screen.contains("[podcast] Other Show"), "{screen}");
assert!(screen.contains("они не проверены"), "{screen}");
assert!(screen.contains("Категории: Technology"), "{screen}");
assert!(
screen.contains("Выпусков: 12 · последний 2024-06-15"),
"{screen}"
);
assert!(
screen.contains("Опубликовали: 1 устройство (не число подписчиков"),
"{screen}"
);
assert!(
screen.contains("Лента: https://feeds.example.test/other.xml"),
"{screen}"
);
assert!(
screen.contains("(не проверен, не используется): https://elsewhere.test/claimed.xml"),
"{screen}"
);
assert!(screen.contains("[ Подписаться ]"), "{screen}");
assert!(
screen.contains("e — прочитать ленту без подписки"),
"{screen}"
);
assert!(screen.contains("enter подписаться"), "{screen}");
assert!(!screen.contains("Уже подписан"), "{screen}");
state.popup = Some(Popup::PodcastCard {
hit: Box::new(hit),
preview: Some(Loadable::Ready(FeedPreview {
title: "Other Show (feed)".into(),
episode_count: 3,
seasons: vec![(Some(1), 2), (None, 1)],
episodes: vec![PreviewEpisode {
title: "Pilot".into(),
season: Some(1),
episode_number: Some(1),
published_at_ms: Some(1_718_445_600_000),
duration_ms: Some(2_700_000),
}],
moved_to: None,
subscribed_as: None,
})),
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(
screen.contains("Название в ленте: Other Show (feed)"),
"{screen}"
);
assert!(
screen.contains("В ленте: 3 выпуска · сезон 1: 2, без сезона: 1"),
"{screen}"
);
assert!(
screen.contains("• Pilot S1E1 · 2024-06-15 · 45:00"),
"{screen}"
);
assert!(screen.contains("… и ещё 2"), "{screen}");
// The subscribed podcast: «Уже подписан», Enter opens it.
state.popup = Some(Popup::PodcastCard {
hit: Box::new(state.search.fed_podcasts[0].clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(screen.contains("✓ Уже подписан"), "{screen}");
assert!(screen.contains("enter открыть"), "{screen}");
assert!(!screen.contains("[ Подписаться ]"), "{screen}");
}
#[test]
fn podcast_share_confirmation_lists_the_warnings_in_russian() {
let mut state =
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 1, 0)]);
state.popup = Some(crate::app::state::Popup::ConfirmPodcastShare {
podcast_id: podcast_fixtures::podcast_id(),
title: "Test Show".into(),
urls: crate::podcasts::publication::ConfirmedUrls {
feed_url: music_dht::podcast::FeedUrl::parse(
"https://example.test/feed.xml?token=abc",
)
.unwrap(),
id_feed_url: None,
},
warnings: vec!["в адресе ленты есть параметр «token», похожий на ключ доступа".into()],
already_shared: false,
});
let screen = render(&state, 100, 30);
assert!(
screen.contains("Поделиться подкастом «Test Show»"),
"{screen}"
);
assert!(screen.contains("Адрес похож на личный"), "{screen}");
assert!(screen.contains("параметр «token»"), "{screen}");
assert!(screen.contains("feed.xml?token=abc"), "{screen}");
assert!(
screen.contains("enter/y опубликовать · esc/n отмена"),
"{screen}"
);
assert!(!screen.contains("x не делиться"), "{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();
state.help_visible = true;
let screen = render(&state, 160, 80);
for title in ["Go to Podcasts", "Go to Settings", "Go to Logs"] {
assert!(screen.contains(title), "{title} missing:\n{screen}");
}
assert!(!screen.contains("Go to tab"), "{screen}");
}
}
pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
@@ -579,9 +74,9 @@ pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
Tab::Global => global::draw(frame, main_area, state),
Tab::Playlists => playlists::draw(frame, main_area, state),
Tab::Queue => draw_queue(frame, main_area, state),
Tab::Podcasts => podcasts::draw(frame, main_area, state),
Tab::Federation => federation::draw(frame, main_area, state),
Tab::Logs => logs::draw(frame, main_area, state),
Tab::Podcasts => podcasts::draw(frame, main_area, state),
}
draw_status(frame, status_area, state);
@@ -647,48 +142,24 @@ fn is_waiting_message(message: &str) -> bool {
}
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
frame.render_widget(tab_bar(state, area.width), area);
}
/// The widest tab bar that fits `width`. Narrow terminals drop the outer
/// padding, then abbreviate titles, then show only numbers (keeping the
/// active tab's full title), so all six shortcuts and the highlight stay
/// visible instead of the last tabs being cut off.
fn tab_bar(state: &AppState, width: u16) -> Tabs<'static> {
use unicode_width::UnicodeWidthStr as _;
let active = state.active_tab;
let build = |label: &dyn Fn(Tab) -> String, padded: bool| {
let tabs = Tabs::new(Tab::ALL.iter().map(|&tab| label(tab)))
.select(active.index())
.style(theme::dim())
.highlight_style(theme::tab_active_for(state))
.divider("");
if padded { tabs } else { tabs.padding("", "") }
};
let full = |tab: Tab| format!(" {} {} ", tab.index() + 1, tab.title());
let short = |tab: Tab| format!(" {} {} ", tab.index() + 1, tab.short_title());
let numbers_with_active = |tab: Tab| {
if tab == active {
full(tab)
} else {
// Titles carry their own spacing. When all of them do not fit, inactive
// tabs collapse to their number so the active one is never cut off.
let full: usize = Tab::ALL.iter().map(|tab| tab.title().len() + 4).sum();
let compact = full > usize::from(area.width);
let titles = Tab::ALL.iter().map(|tab| {
if compact && *tab != state.active_tab {
format!(" {} ", tab.index() + 1)
} else {
format!(" {} {} ", tab.index() + 1, tab.title())
}
};
let numbers = |tab: Tab| format!(" {} ", tab.index() + 1);
let candidates: [(&dyn Fn(Tab) -> String, bool); 4] = [
(&full, true),
(&full, false),
(&short, false),
(&numbers_with_active, false),
];
for (label, padded) in candidates {
let tabs = build(label, padded);
if tabs.width() <= usize::from(width) {
return tabs;
}
}
build(&numbers, false)
});
let tabs = Tabs::new(titles)
.select(state.active_tab.index())
.style(theme::dim())
.highlight_style(theme::tab_active_for(state))
.padding("", "")
.divider("");
frame.render_widget(tabs, area);
}
/// One track row used by every track list: ♥ marker for liked tracks, the
-284
View File
@@ -1,284 +0,0 @@
//! The public podcast card opened from the search results. In Russian, like
//! the share confirmation: «Подписаться» / «Уже подписан» are the actions
//! the requirements name.
//!
//! Everything but the origin feed URL is a publisher's claim and is shown as
//! such; the episode preview comes from the feed itself.
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Layout};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, Paragraph, Wrap};
use super::podcasts::{format_date, format_duration_ms};
use super::popup::centered;
use super::theme;
use crate::app::state::{AppState, Loadable};
use crate::podcasts::discovery::{FeedPreview, PodcastHit};
pub(super) fn draw(
frame: &mut Frame,
state: &AppState,
hit: &PodcastHit,
preview: Option<&Loadable<FeedPreview>>,
scroll: u16,
max_scroll: &std::cell::Cell<u16>,
) {
let subscribed =
crate::app::update::podcast_discovery::subscribed_id(state, hit, preview).is_some();
let subscribing = state.podcasts.subscribing;
let lines = card_lines(state, hit, preview, subscribed, subscribing);
let width = 92.min(frame.area().width.saturating_sub(4)).max(30);
let inner_width = width.saturating_sub(2).max(1);
let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false });
// The height of the word-wrapped text, as it is rendered.
let body_height = u16::try_from(paragraph.line_count(inner_width)).unwrap_or(u16::MAX);
let height = body_height
.saturating_add(4)
.min(frame.area().height.saturating_sub(2));
let area = centered(frame.area(), width, height);
let block = Block::bordered()
.title(" Подкаст · публичная карточка ")
.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 [body, footer_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
let last = body_height.saturating_sub(body.height);
max_scroll.set(last);
frame.render_widget(paragraph.scroll((scroll.min(last), 0)), body);
let footer = if subscribed {
"enter открыть · e выпуски · ↑↓ прокрутка · esc закрыть"
} else {
"enter подписаться · e выпуски · ↑↓ прокрутка · esc закрыть"
};
frame.render_widget(
Paragraph::new(Line::styled(footer, theme::dim())).alignment(Alignment::Center),
footer_area,
);
}
fn card_lines(
state: &AppState,
hit: &PodcastHit,
preview: Option<&Loadable<FeedPreview>>,
subscribed: bool,
subscribing: bool,
) -> Vec<Line<'static>> {
let card = &hit.hit.card;
let bold = Style::new().add_modifier(Modifier::BOLD);
let mut lines = vec![
Line::from(vec![
Span::styled("[podcast] ", theme::accent_for(state)),
Span::styled(card.title.clone(), bold),
]),
Line::styled(
"Сведения ниже указали издатели карточки; они не проверены.",
theme::dim(),
),
Line::raw(""),
];
if let Some(author) = &card.author {
lines.push(field("Автор", author.clone()));
}
if !card.categories.is_empty() {
lines.push(field("Категории", card.categories.join(", ")));
}
if let Some(language) = &card.language {
lines.push(field("Язык", language.clone()));
}
let mut episodes = match card.episode_count {
Some(count) => format!("{count}"),
None => "не указано".to_string(),
};
if let Some(latest) = card.latest_episode_at_ms {
episodes.push_str(&format!(
" · последний {}",
format_date(latest.div_euclid(1000))
));
}
lines.push(field("Выпусков", episodes));
let publishers = hit.hit.publishers.len();
lines.push(field(
"Опубликовали",
format!(
"{publishers} {} (не число подписчиков и не знак доверия)",
plural_ru(publishers, "устройство", "устройства", "устройств")
),
));
lines.push(field("Лента", hit.hit.feed_url.as_str().to_string()));
for claim in &hit.hit.claimed_feed_urls {
lines.push(Line::styled(
format!(
"Заявленный новый адрес (не проверен, не используется): {}",
claim.feed_url.as_str()
),
theme::dim(),
));
}
if let Some(link) = &card.link {
lines.push(field("Сайт", link.clone()));
}
if let Some(description) = &card.description {
lines.push(Line::raw(""));
lines.extend(
description
.lines()
.map(|line| Line::raw(line.replace('\t', " "))),
);
}
lines.push(Line::raw(""));
if subscribed {
lines.push(Line::from(vec![
Span::styled(
"✓ Уже подписан",
Style::new().fg(Color::Green).add_modifier(Modifier::BOLD),
),
Span::styled(
" · enter откроет подкаст во вкладке Podcasts",
theme::dim(),
),
]));
} else if subscribing {
lines.push(Line::styled(
"Подписка… читаем ленту",
theme::accent_for(state),
));
} else {
lines.push(Line::from(vec![
Span::styled(
"[ Подписаться ]",
theme::accent_for(state).add_modifier(Modifier::BOLD),
),
Span::styled(
" лента будет прочитана с её сервера; подписка личная и не публикуется",
theme::dim(),
),
]));
}
lines.push(Line::raw(""));
lines.push(Line::styled(
"Сезоны и выпуски (из RSS-ленты)",
theme::header_for(state),
));
match preview {
None => lines.push(Line::styled(
"e — прочитать ленту без подписки (запрос к серверу ленты)",
theme::dim(),
)),
Some(Loadable::Loading) => {
lines.push(Line::styled("читаем ленту…", theme::accent_for(state)))
}
Some(Loadable::Failed(err)) => lines.push(Line::styled(
format!("не удалось прочитать ленту: {err}"),
Style::new().fg(Color::Red),
)),
Some(Loadable::Ready(feed)) => preview_lines(&mut lines, card.title.as_str(), feed),
}
lines
}
fn preview_lines(lines: &mut Vec<Line<'static>>, card_title: &str, feed: &FeedPreview) {
if feed.title != card_title {
lines.push(field("Название в ленте", feed.title.clone()));
}
if let Some(moved) = &feed.moved_to {
lines.push(field("Лента переехала", moved.as_str().to_string()));
}
let seasons: Vec<String> = feed
.seasons
.iter()
.map(|(season, count)| match season {
Some(season) => format!("сезон {season}: {count}"),
None => format!("без сезона: {count}"),
})
.collect();
lines.push(field(
"В ленте",
format!(
"{} {}{}",
feed.episode_count,
plural_ru(feed.episode_count, "выпуск", "выпуска", "выпусков"),
if seasons.is_empty() {
String::new()
} else {
format!(" · {}", seasons.join(", "))
}
),
));
for episode in &feed.episodes {
let mut meta = Vec::new();
match (episode.season, episode.episode_number) {
(Some(season), Some(number)) => meta.push(format!("S{season}E{number}")),
(Some(season), None) => meta.push(format!("S{season}")),
(None, Some(number)) => meta.push(format!("#{number}")),
(None, None) => {}
}
if let Some(published) = episode.published_at_ms {
meta.push(format_date(published.div_euclid(1000)));
}
if let Some(duration) = episode.duration_ms {
meta.push(format_duration_ms(duration));
}
lines.push(Line::from(vec![
Span::raw(format!("• {}", episode.title)),
Span::styled(
if meta.is_empty() {
String::new()
} else {
format!(" {}", meta.join(" · "))
},
theme::dim(),
),
]));
}
if feed.episodes.len() < feed.episode_count {
lines.push(Line::styled(
format!("… и ещё {}", feed.episode_count - feed.episodes.len()),
theme::dim(),
));
}
}
fn field(label: &str, value: String) -> Line<'static> {
Line::from(vec![
Span::styled(format!("{label}: "), theme::dim()),
Span::raw(value),
])
}
/// Russian plural form for `n`.
fn plural_ru(n: usize, one: &'static str, few: &'static str, many: &'static str) -> &'static str {
let (tens, units) = (n % 100, n % 10);
if (11..=14).contains(&tens) {
many
} else if units == 1 {
one
} else if (2..=4).contains(&units) {
few
} else {
many
}
}
#[cfg(test)]
mod tests {
use super::plural_ru;
#[test]
fn russian_plurals() {
let forms = |n| plural_ru(n, "устройство", "устройства", "устройств");
assert_eq!(forms(1), "устройство");
assert_eq!(forms(3), "устройства");
assert_eq!(forms(5), "устройств");
assert_eq!(forms(11), "устройств");
assert_eq!(forms(21), "устройство");
assert_eq!(forms(0), "устройств");
}
}
+200 -535
View File
@@ -1,9 +1,6 @@
//! 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`.
//! Podcasts tab: subscriptions with their cover, and the episode catalog of
//! one podcast with title search, date sort and the selected episode's notes.
use furumi_library::podcasts::{EpisodeListItem, SeasonSummary};
use music_dht::podcast::ProgressStatus;
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
@@ -13,55 +10,18 @@ use unicode_width::UnicodeWidthStr;
use super::{loading_line, theme};
use crate::app::state::{
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow, PodcastRow,
PodcastStoreStatus, SeasonRow,
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow,
};
use crate::app::update::podcasts::{played_label, sort_label};
use crate::podcasts::publication::{Discovery, Publication};
use crate::podcasts::{Episode, Podcast};
/// Width below which the side panel with the cover is dropped.
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80;
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 72;
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 {
match state.podcasts.opened {
Some(opened) => draw_episodes(frame, area, state, opened.id, opened.cursor),
None => draw_list(frame, area, state),
Some(opened) if opened.season.is_none() => draw_seasons(frame, area, state),
Some(_) => draw_episodes(frame, area, state),
}
}
@@ -91,48 +51,6 @@ 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<String>,
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());
@@ -145,47 +63,60 @@ fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) {
}
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);
let first = tab
.selected
.saturating_sub(visible / 2)
.min(rows.len().saturating_sub(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 area = Rect {
x: list_area.x,
y: list_area.y + (index - first) as u16,
width: list_area.width,
height: 1,
};
let (left, right) = list_row_lines(state, *row, list_area.width);
frame.render_widget(Paragraph::new(left), area);
if let Some(right) = right {
frame.render_widget(Paragraph::new(right).alignment(Alignment::Right), area);
}
if index == tab.selected {
frame
.buffer_mut()
.set_style(area, theme::tab_active_for(state));
}
}
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;
if tab.podcasts().is_empty() {
let hint_y = list_area.y + rows.len() as u16 + 1;
if hint_y < list_area.y + list_area.height {
frame.render_widget(
Paragraph::new(Line::styled(
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.",
theme::dim(),
))
.wrap(Wrap { trim: true }),
Rect {
y: hint_y,
height: list_area.y + list_area.height - hint_y,
..list_area
},
);
}
}
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<String>) {
fn list_row_lines(
state: &AppState,
row: PodcastListRow,
width: u16,
) -> (Line<'static>, Option<Line<'static>>) {
let tab = &state.podcasts;
match row {
PodcastListRow::Add => {
@@ -197,14 +128,15 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>
(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 {
let busy = !tab.refreshing.is_empty();
let label = if busy {
format!(
"{} Checking {} feed(s) for new episodes…",
state.spinner(),
tab.refreshing.len()
)
} else {
"↻ Check all podcasts for new episodes".to_string()
};
(Line::styled(label, theme::accent_for(state)), None)
}
@@ -212,31 +144,26 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>
let Some(podcast) = tab.podcasts().get(index) else {
return (Line::raw(""), None);
};
let marker = if tab.refreshing.contains(podcast.id()) {
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() {
} else if podcast.last_error.is_some() {
Span::styled("! ", error_style())
} else {
Span::raw(" ")
};
let summary = &podcast.summary;
let mut meta = if summary.unplayed_count > 0 {
format!(
"{} unplayed · {} ep",
summary.unplayed_count, summary.episode_count
)
} else {
format!("{} ep", summary.episode_count)
};
if summary.shared {
meta = format!("shared · {meta}");
let mut meta = format!("{} ep", podcast.episode_count);
if let Some(latest) = podcast.latest_published_at {
meta.push_str(&format!(" · {}", format_date(latest)));
}
let title_width = usize::from(width).saturating_sub(2 + meta.width() + 2);
meta.push(' ');
// Marker (2), the title, one gap column, then the meta.
let title_width =
usize::from(width).saturating_sub(2 + UnicodeWidthStr::width(meta.as_str()) + 1);
let left = Line::from(vec![
marker,
Span::raw(fit_width(podcast.title(), title_width)),
Span::raw(fit_width(&podcast.title, title_width)),
]);
(left, Some(meta))
(left, Some(Line::styled(meta, theme::dim())))
}
}
}
@@ -251,7 +178,7 @@ fn split_side_panel(area: Rect) -> (Rect, Option<Rect>) {
(main, Some(side))
}
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&PodcastRow>) {
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&Podcast>) {
let block = Block::default()
.borders(Borders::LEFT)
.border_style(theme::border_for(state));
@@ -284,109 +211,42 @@ fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Opt
if text_area.height == 0 {
return;
}
let summary = &podcast.summary;
let mut lines = vec![Line::styled(
podcast.title().to_string(),
podcast.title.clone(),
Style::new().add_modifier(Modifier::BOLD),
)];
if let Some(author) = &summary.meta.author {
if let Some(author) = &podcast.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(share_status_line(state, podcast));
lines.push(fetch_status_line(state, podcast));
if let Some(description) = &summary.meta.description {
lines.push(Line::styled(
format!("{} episodes", podcast.episode_count),
theme::dim(),
));
lines.extend(check_status_line(state, podcast));
if let Some(description) = &podcast.description {
lines.push(Line::raw(""));
lines.push(Line::raw(description.clone()));
}
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area);
}
/// Public discovery state of one podcast: private, or shared and whether
/// this device publishes its card.
pub(crate) fn share_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> {
let tab = &state.podcasts;
if tab.share_busy.contains(podcast.id()) {
return loading_line(state, "saving the share setting…");
fn check_status_line(state: &AppState, podcast: &Podcast) -> Option<Line<'static>> {
if state.podcasts.refreshing.contains(&podcast.id) {
return Some(loading_line(state, "checking for new episodes…"));
}
let report = &tab.publication;
// A card from this device that is still live although the podcast is
// private now (revocation pending, failed, or federation off).
let unrevoked = report.unrevoked.contains(podcast.id());
if !podcast.summary.shared || (unrevoked && !report.podcasts.contains_key(podcast.id())) {
if !unrevoked {
return Line::styled("private (not shared in the federation)", theme::dim());
}
if let Discovery::Off(_) = report.discovery {
return Line::styled(
"private · public card may not be revoked yet: federation is off",
Style::new().fg(Color::Yellow),
);
}
return loading_line(
state,
"private · revoking the public card from this device…",
);
if let Some(error) = &podcast.last_error {
return Some(Line::styled(
format!("last check failed: {error}"),
error_style(),
));
}
let (text, style) = match (&report.discovery, report.podcasts.get(podcast.id())) {
(Discovery::Off(reason), _) => (format!("shared · not published: {reason}"), theme::dim()),
(_, Some(Publication::HeldBack(_))) => (
"shared · held back on this device: the feed URL looks private, \
press Shift+F to confirm"
.to_string(),
Style::new().fg(Color::Yellow),
),
(_, Some(Publication::Failed(error))) => {
(format!("shared · not published: {error}"), error_style())
}
(Discovery::Running { peers: 0 }, Some(Publication::Published)) => (
"shared · published from this device (no discovery peers yet)".to_string(),
theme::accent_for(state),
),
(_, Some(Publication::Published)) => (
"shared · published from this device".to_string(),
theme::accent_for(state),
),
(_, Some(Publication::WaitingForDevices)) => (
"shared · not published yet: waiting for a sync with your other devices".to_string(),
theme::dim(),
),
(Discovery::Starting, _) | (_, Some(Publication::Pending) | None) => {
return loading_line(state, "shared · publishing…");
}
};
Line::styled(text, style)
podcast
.last_checked_at
.map(|at| Line::styled(format!("checked {}", format_date(at)), theme::dim()))
}
/// 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| {
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &Podcast) {
let art = podcast.cover_path.as_ref().and_then(|path| {
state.art.get(&crate::art::cache_key(
path,
ART_HEADER_WIDTH,
@@ -402,220 +262,97 @@ fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &Podcast
}
}
// ---------------------------------------------------------------------------
// Seasons
fn draw_seasons(frame: &mut Frame, area: Rect, state: &AppState) {
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor: usize) {
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))) => {
let podcast = tab.podcast(id);
let title = podcast.map_or("…".to_string(), |podcast| podcast.title.clone());
let mut header = format!(" Podcasts ▸ {title} · {} ", tab.sort.label());
if !tab.filter.is_empty() {
header.push_str(&format!("· search: \"{}\" ", tab.filter));
}
let inner = bordered(frame, area, state, header);
let episodes = match &tab.episodes {
Some((loaded, Loadable::Ready(episodes))) if *loaded == id => episodes,
Some((loaded, Loadable::Failed(error))) if *loaded == id => {
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…")),
},
_ => return centered_line(frame, inner, loading_line(state, "loading episodes…")),
};
let visible_indices = tab.visible_episode_indices();
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,
);
}
}
// Episode notes below the list when there is room for them.
let notes_height = if main.height >= 16 { 6 } else { 0 };
let [list_area, notes_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(notes_height)]).areas(main);
let [summary_area, list_area] =
Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(list_area);
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")
let summary = if tab.filter.is_empty() {
format!("{} episodes", episodes.len())
} 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;
format!(
"{} of {} episodes match \"{}\"",
visible_indices.len(),
episodes.len(),
tab.filter
)
};
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 mut summary_spans = vec![Span::styled(summary, theme::dim())];
if tab.refreshing.contains(&id) {
summary_spans.push(Span::raw(" "));
summary_spans.extend(loading_line(state, "checking for new episodes…").spans);
}
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…"));
};
frame.render_widget(Paragraph::new(Line::from(summary_spans)), summary_area);
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"
if visible_indices.is_empty() {
let message = if episodes.is_empty() {
"this feed has no playable episodes"
} else {
"no episode matches the search or filter"
"no episode title matches the search"
};
return centered_line(frame, list_area, Line::styled(message, theme::dim()));
}
let cursor = opened.episode_cursor.min(list.items.len() - 1);
let cursor = cursor.min(visible_indices.len() - 1);
let playing_id = state
.player
.current
.as_ref()
.and_then(|track| crate::podcasts::episode_id_from_track_id(track.id));
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) {
let first = cursor
.saturating_sub(visible / 2)
.min(visible_indices.len().saturating_sub(visible));
for (row_index, &episode_index) in visible_indices.iter().enumerate().skip(first).take(visible)
{
let episode = &episodes[episode_index];
let area = Rect {
x: list_area.x,
y: list_area.y + (row_index - first) as u16,
width: list_area.width,
height: 1,
};
draw_episode_row(
frame,
row_area(list_area, index - first),
area,
state,
item,
index == cursor,
episode,
playing_id == Some(episode.id),
row_index == cursor,
);
}
if notes_height > 0
&& let Some(item) = list.items.get(cursor)
&& let Some(episode) = visible_indices
.get(cursor)
.and_then(|&index| episodes.get(index))
{
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()),
draw_notes(frame, notes_area, state, episode);
}
}
@@ -623,98 +360,65 @@ fn draw_episode_row(
frame: &mut Frame,
area: Rect,
state: &AppState,
item: &EpisodeListItem,
episode: &Episode,
playing: bool,
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(),
.published_at
.map(format_date)
.unwrap_or_else(|| " ".to_string());
let marker = if playing {
Span::styled("▶ ", theme::accent_for(state))
} else {
Span::raw(" ")
};
let duration = episode
.duration_seconds
.map(format_duration)
.unwrap_or_default();
let title_width = usize::from(area.width)
.saturating_sub(2 + 10 + 2 + UnicodeWidthStr::width(duration.as_str()) + 2);
let line = Line::from(vec![
Span::styled(format!("{marker} "), marker_style),
marker,
Span::styled(date, theme::dim()),
Span::raw(" "),
Span::styled(number, theme::dim()),
Span::styled(fit_width(&title, title_width), title_style),
Span::raw(fit_width(&episode.title, title_width)),
]);
draw_row(
frame,
area,
state,
line,
(!right.is_empty()).then_some(right),
selected,
);
frame.render_widget(Paragraph::new(line), area);
if !duration.is_empty() {
frame.render_widget(
Paragraph::new(Line::styled(format!("{duration} "), theme::dim()))
.alignment(Alignment::Right),
area,
);
}
if selected {
frame
.buffer_mut()
.set_style(area, theme::tab_active_for(state));
}
}
/// 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<i64> {
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) {
fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, episode: &Episode) {
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
let text = 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 {
fn fit_width(text: &str, width: usize) -> String {
if UnicodeWidthStr::width(text) <= width {
return text.to_string();
}
@@ -735,8 +439,8 @@ pub(crate) fn fit_width(text: &str, width: usize) -> String {
out
}
pub(crate) fn format_duration_ms(ms: i64) -> String {
let total = (ms.max(0) + 500) / 1000;
fn format_duration(seconds: f64) -> String {
let total = seconds.max(0.0).round() as u64;
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
if hours > 0 {
format!("{hours}:{minutes:02}:{secs:02}")
@@ -745,17 +449,9 @@ pub(crate) fn format_duration_ms(ms: i64) -> String {
}
}
/// `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) {
/// `YYYY-MM-DD` (UTC) for unix seconds, without a date library: Howard
/// Hinnant's days-to-civil algorithm.
fn format_date(unix_seconds: i64) -> String {
let days = unix_seconds.div_euclid(86_400);
let z = days + 719_468;
let era = z.div_euclid(146_097);
@@ -766,38 +462,7 @@ fn civil(unix_seconds: i64) -> (i64, i64, i64) {
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")
}
format!("{year:04}-{month:02}-{day:02}")
}
#[cfg(test)]
@@ -805,18 +470,18 @@ mod tests {
use super::*;
#[test]
fn formats_dates_durations_and_relative_times() {
fn formats_dates_in_utc() {
assert_eq!(format_date(0), "1970-01-01");
assert_eq!(format_date(951_782_400), "2000-02-29");
assert_eq!(format_date(1_718_409_600), "2024-06-15");
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 formats_durations() {
assert_eq!(format_duration(59.6), "1:00");
assert_eq!(format_duration(754.0), "12:34");
assert_eq!(format_duration(3_723.0), "1:02:03");
}
#[test]
+20 -142
View File
@@ -24,23 +24,9 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
error,
..
}) => 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::ConfirmDelete { target, label }) => {
draw_confirm_delete(frame, state, *target, label)
}
Some(Popup::PodcastCard {
hit,
preview,
scroll,
max_scroll,
}) => super::podcast_card::draw(frame, state, hit, preview.as_ref(), *scroll, max_scroll),
Some(Popup::ConfirmPodcastShare {
title,
urls,
warnings,
already_shared,
..
}) => draw_confirm_podcast_share(frame, state, title, urls, warnings, *already_shared),
Some(Popup::SimilarityPrivacyConsent { .. }) => {
draw_similarity_privacy_consent(frame, state)
}
@@ -59,7 +45,7 @@ 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 }) => {
let keys = if *field == crate::app::state::FedInputField::PodcastSearch {
let keys = if *field == crate::app::state::FedInputField::PodcastFilter {
"enter: keep · esc: clear"
} else {
"enter: apply · esc: cancel"
@@ -1262,11 +1248,23 @@ fn draw_edit(
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer);
}
fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
fn draw_confirm_delete(
frame: &mut Frame,
state: &AppState,
target: crate::app::state::DeleteTarget,
label: &str,
) {
// Unsubscribing only drops the feed's stored metadata, not music.
let (title, verb, key) = match target {
crate::app::state::DeleteTarget::Podcast(_) => {
(" Unsubscribe? ", "Unsubscribe from", "unsubscribe")
}
_ => (" Delete? ", "Delete", "delete"),
};
let width = 64.min(frame.area().width.saturating_sub(4)).max(30);
let area = centered(frame.area(), width, 7);
let block = Block::bordered()
.title(" Delete? ")
.title(title)
.title_style(theme::header_for(state))
.border_style(theme::strong_border_for(state));
let inner = block.inner(area);
@@ -1275,55 +1273,14 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
let [body, footer] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
frame.render_widget(
Paragraph::new(format!("Delete {label}?"))
Paragraph::new(format!("{verb} {label}?"))
.wrap(Wrap { trim: false })
.alignment(Alignment::Center),
body,
);
frame.render_widget(
Paragraph::new(Line::styled("enter/y delete · esc/n cancel", theme::dim()))
.alignment(Alignment::Center),
footer,
);
}
/// 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",
format!("enter/y {key} · esc/n cancel"),
theme::dim(),
))
.alignment(Alignment::Center),
@@ -1331,85 +1288,6 @@ fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title:
);
}
/// Sharing a podcast whose feed URL looks private. In Russian: the warning
/// is about the user's own secret, so it must not be missed.
fn draw_confirm_podcast_share(
frame: &mut Frame,
state: &AppState,
title: &str,
urls: &crate::podcasts::publication::ConfirmedUrls,
warnings: &[String],
already_shared: bool,
) {
let width = 78.min(frame.area().width.saturating_sub(4)).max(30);
let mut lines = vec![
Line::styled(
format!("Поделиться подкастом «{title}» в федерации?"),
ratatui::style::Style::new().add_modifier(ratatui::style::Modifier::BOLD),
),
Line::raw(""),
Line::raw(
"Карточка с адресом RSS-ленты станет видна всем участникам федерации, \
и любой из них сможет читать ленту по этому адресу.",
),
Line::raw(""),
Line::styled(
"Адрес похож на личный:",
ratatui::style::Style::new().fg(ratatui::style::Color::Yellow),
),
];
lines.extend(warnings.iter().map(|warning| {
Line::styled(
format!("• {warning}"),
ratatui::style::Style::new().fg(ratatui::style::Color::Yellow),
)
}));
lines.push(Line::raw(""));
lines.push(Line::styled(
format!("Лента: {}", urls.feed_url.as_str()),
theme::dim(),
));
if let Some(origin) = &urls.id_feed_url {
lines.push(Line::styled(
format!("Исходный адрес: {}", origin.as_str()),
theme::dim(),
));
}
lines.push(Line::raw(""));
lines.push(Line::styled(
"Публикацию можно отозвать, но копии, которые уже получили другие, \
отозвать нельзя. Подтверждение хранится только на этом устройстве.",
theme::dim(),
));
let footer = if already_shared {
"enter/y опубликовать · x не делиться · esc/n отмена"
} else {
"enter/y опубликовать · esc/n отмена"
};
let inner_width = width.saturating_sub(2).max(1);
let body = Paragraph::new(lines).wrap(Wrap { trim: false });
// The height of the word-wrapped text, as it is rendered.
let body_height = u16::try_from(body.line_count(inner_width)).unwrap_or(u16::MAX);
let height = body_height
.saturating_add(4)
.min(frame.area().height.saturating_sub(2));
let area = centered(frame.area(), width, height);
let block = Block::bordered()
.title(" Поделиться в федерации ")
.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 [body_area, footer_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
frame.render_widget(body, body_area);
frame.render_widget(
Paragraph::new(Line::styled(footer, theme::dim())).alignment(Alignment::Center),
footer_area,
);
}
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);
@@ -1828,7 +1706,7 @@ fn draw_name_entry(
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer);
}
pub(super) fn centered(area: Rect, width: u16, height: u16) -> Rect {
fn centered(area: Rect, width: u16, height: u16) -> Rect {
let [rect] = Layout::horizontal([Constraint::Length(width.min(area.width))])
.flex(Flex::Center)
.areas(area);
-1
View File
@@ -24,7 +24,6 @@ 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;
+1 -1
View File
@@ -13,7 +13,7 @@ use semver::Version;
use serde::Deserialize;
use sha2::{Digest, Sha256};
const REPOSITORY: &str = "https://api.github.com/repos/Furumi-stack/furumi-tui";
const REPOSITORY: &str = "https://api.github.com/repos/house-of-vanity/furumi_tui";
const MAX_ARCHIVE: u64 = 512 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
File diff suppressed because it is too large. Load diff
+25 -980
View File
File diff suppressed because it is too large. Load diff