Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
038f0979af | ||
|
|
81f422b244 | ||
|
|
7659313dc2 | ||
|
|
44d0bb5abe | ||
|
|
59d6a1fe24 | ||
|
|
409188b9a9 | ||
|
|
ce5697ce1d | ||
|
|
adfe809a40 | ||
|
|
d6962eba47 | ||
|
|
43e0696531 | ||
|
|
405f8ae6d3 | ||
|
|
3ddf7995e8 | ||
|
|
17ed8bfcd4 | ||
|
|
fd6827126f | ||
|
|
744d56e06b | ||
|
|
7add66920d | ||
|
|
1137670229 | ||
|
|
a51f5552e3 | ||
|
|
d38144fe45 | ||
|
|
160237b5a5 | ||
|
|
e038f282c2 | ||
|
|
25a9f2b0ec | ||
|
|
6d225531a5 | ||
|
|
8f4e58d8af | ||
|
|
8c90035fa2 | ||
|
|
c453b0a52f | ||
|
|
ea7cdc4b7f | ||
|
|
c7e6a3672d | ||
|
|
e0624ab7f4 | ||
|
|
2806e679ac | ||
|
|
d03a3300f1 | ||
|
|
42ecc310fc | ||
|
|
96e615bf1d | ||
|
|
34c096a36b | ||
|
|
268ea915da | ||
|
|
e0da2ddf2b | ||
|
|
fe373e4169 | ||
|
|
42f22f0fea | ||
|
|
2989747162 | ||
|
|
8d87fe3959 | ||
|
|
70df945ada | ||
|
|
a79f0a0e95 | ||
|
|
c6d7848ba4 | ||
|
|
a92e12f322 | ||
|
|
79372e00fc | ||
|
|
4c3edbcd12 | ||
|
|
e7713269f2 | ||
|
|
253583b139 | ||
|
|
54db2978e0 | ||
|
|
9321e82335 | ||
|
|
3567f5f395 | ||
|
|
7583afaeba | ||
|
|
043caf1aaf | ||
|
|
99c4d05dec | ||
|
|
07dcd0587c | ||
|
|
bc04587252 | ||
|
|
595c886303 | ||
|
|
78bf2cf583 | ||
|
|
df7ad3c8b7 | ||
|
|
df171208eb | ||
|
|
e78f1627da | ||
|
|
292f17fee1 | ||
|
|
3e46f21804 | ||
|
|
7f0ca2984f | ||
|
|
0ad3844d37 | ||
|
|
2253ec7d91 | ||
|
|
8dd3ededf6 | ||
|
|
81fdcf13ae | ||
|
|
a6d502e319 | ||
|
|
3298204f2e | ||
|
|
9155bb9e00 | ||
|
|
b07bcd4ddd | ||
|
|
966cf4b437 | ||
|
|
532d6ee1e9 | ||
|
|
5a193d839f | ||
|
|
7d6af59e97 | ||
|
|
32c976beb8 | ||
|
|
2607314da3 | ||
|
|
f901836929 |
No files matched your search
@@ -98,7 +98,12 @@ jobs:
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
gh release create "$RELEASE_TAG" artifacts/*/* \
|
||||
# Hash archive bytes; manifest entries use the release asset basenames.
|
||||
for archive in artifacts/*/*; do
|
||||
(cd "$(dirname "$archive")" && sha256sum "$(basename "$archive")")
|
||||
done > SHA256SUMS
|
||||
|
||||
gh release create "$RELEASE_TAG" artifacts/*/* SHA256SUMS \
|
||||
--title "furumi ${RELEASE_TAG}" \
|
||||
--notes "Release ${RELEASE_TAG}" \
|
||||
--verify-tag
|
||||
@@ -61,8 +61,30 @@ 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.
|
||||
coordination. Its wire types are the shared `music_dht::device_sync`
|
||||
definitions; do not add local copies (golden tests in
|
||||
`devices/wire_compat_tests.rs` keep the v3 JSON).
|
||||
- `src/ui/` renders `AppState`; rendering code must not start background work
|
||||
or mutate persistence.
|
||||
- `src/media.rs` owns OS media controls.
|
||||
@@ -155,7 +177,25 @@ When changing it:
|
||||
- keep snapshots able to repair peers that missed older operations;
|
||||
- treat membership and revocation as replicated durable state;
|
||||
- keep playback commands targeted and deduplicated;
|
||||
- test merge behavior from at least two operation orders.
|
||||
- test merge behavior from at least two operation orders;
|
||||
- never store this build's protocol version for another device: use the
|
||||
device's own report (`profile_protocol_version`) and only raise a stored
|
||||
version from relayed profiles (`relayed_protocol_version`);
|
||||
- gate everything a peer cannot parse by the version it reported: podcast
|
||||
operations, podcast snapshot sections and playback state with podcast items
|
||||
go only to v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`,
|
||||
`playback_allowed_for_peer`), a podcast queue is never handed to an older
|
||||
device and its playback commands are ignored while the queue has episodes;
|
||||
never strip episodes from a queue to make it fit an old peer; an older
|
||||
peer still gets the playback coordination, with the queue redacted
|
||||
(`redact_for_jam`), so its engine does not fail over, and the
|
||||
"Private item" music entries it echoes back in its own states are left
|
||||
out (`drop_echoed_private_items`), never adopted;
|
||||
- apply podcast operations and snapshot sections only through
|
||||
`PodcastStore` (`apply_remote_op`, `apply_snapshot`); never call the
|
||||
podcast store while holding the sync-log connection;
|
||||
- keep Jam snapshots redacted (`redact_for_jam`) and map guest placeholders
|
||||
back with `restore_jam_private_items`.
|
||||
|
||||
Do not replace eventual reconciliation with assumptions about a continuously
|
||||
connected leader.
|
||||
@@ -186,6 +226,11 @@ 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
|
||||
@@ -216,7 +261,9 @@ Add tests at the closest stable boundary:
|
||||
- pure state transitions in `app/update_tests.rs`;
|
||||
- SQLite behavior and migrations in `library/tests.rs`;
|
||||
- operation merge, tombstone, membership, and playback sync behavior in
|
||||
`devices/tests.rs`;
|
||||
`devices/tests.rs`; podcast sync between device stores and simulated v3
|
||||
peers in `devices/podcast_sync_tests.rs`; wire shapes in
|
||||
`devices/wire_compat_tests.rs`;
|
||||
- search/ranking/federation conversion in `federation/tests.rs`;
|
||||
- protocol-specific tests beside `federation/audio.rs` or
|
||||
`federation/catalog.rs` when appropriate.
|
||||
|
||||
+280
-4
@@ -66,12 +66,27 @@ This layer connects devices owned or trusted by the same user. It carries
|
||||
personal state such as:
|
||||
|
||||
- likes and playlist operations;
|
||||
- podcast subscriptions, episode progress and listening sessions;
|
||||
- device membership and revocation;
|
||||
- acknowledgements and synchronization progress;
|
||||
- playback state, commands, and handoff information.
|
||||
|
||||
Pairing establishes the trust relationship. After that, changes are replicated
|
||||
through an append-only operation log and applied to materialized local tables.
|
||||
The wire types are the shared `music_dht::device_sync` definitions (protocol
|
||||
v4); golden tests keep the v3 JSON of furumi-tui 0.3.x byte for byte.
|
||||
|
||||
Each device stores the protocol version that the other devices report
|
||||
themselves (their hello and pairing profiles, the sender's own entry of a
|
||||
`devices` list, their own profile announcements). A relayed profile only
|
||||
raises a stored version, because older relays list every device with their
|
||||
own constant; this build never stores its own version for another device.
|
||||
Everything a peer cannot parse is gated by that version: podcast operations,
|
||||
podcast snapshot sections and playback state that contains podcast items
|
||||
reach only v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`,
|
||||
`playback_allowed_for_peer`). An older client would reject a whole message
|
||||
over one unknown operation kind, or silently drop an episode it does not
|
||||
understand.
|
||||
|
||||
### Music federation
|
||||
|
||||
@@ -97,8 +112,12 @@ audio. Other TUI peers use a dedicated Jam ALPN to submit the same portable
|
||||
playback commands used by connected-device control and receive the host's
|
||||
playback snapshot. They receive queue metadata, not audio.
|
||||
|
||||
Jam never exchanges trusted membership, likes, playlists, or listening
|
||||
history. Volume remains local. Commands carry unique IDs and are retried until
|
||||
Jam never exchanges trusted membership, likes, playlists, listening history
|
||||
or podcast state. The host's podcast episodes are private: the published
|
||||
snapshot replaces them with opaque placeholders (`redact_for_jam`), guest
|
||||
commands that add episodes are dropped, and a guest's placeholders are mapped
|
||||
back to the host's episodes (`restore_jam_private_items`); a change that
|
||||
drops, duplicates or seeks in them is refused. Volume remains local. Commands carry unique IDs and are retried until
|
||||
the host acknowledges them, while inactive participants expire from the
|
||||
runtime session. Regenerating the capability or restarting the host invalidates
|
||||
the previous link.
|
||||
@@ -123,6 +142,14 @@ The DHT is the distributed index. Nodes publish compact searchable
|
||||
descriptions of their local library and query the network without contacting a
|
||||
central search service.
|
||||
|
||||
Opening a local artist or release card in a network-enabled library view
|
||||
enriches it asynchronously. DHT matches identify likely catalog owners, while
|
||||
a bounded set of known peers can also answer directly when the index is
|
||||
incomplete. Card results are replaceable snapshots: reopening a card or
|
||||
periodic maintenance retries discovery, with at most one request per card in
|
||||
flight. A failed refresh keeps the last usable snapshot, and never prevents
|
||||
browsing the local tracks.
|
||||
|
||||
Similarity discovery uses a separate schema-independent DHT overlay. A node
|
||||
derives a deterministic 256-bit routing signature from every local embedding,
|
||||
groups them into fixed two-level LSH buckets, and publishes compact summaries
|
||||
@@ -181,6 +208,12 @@ The synchronization model combines:
|
||||
- per-peer acknowledgements to determine when old tombstones can be compacted;
|
||||
- snapshots to repair a peer that missed older operations.
|
||||
|
||||
Vectors only grow, so operations hidden from a peer while it ran an older
|
||||
protocol are never sent again after it upgrades. Snapshots are the recovery
|
||||
path for that case; the podcast sections carry the subscription, share and
|
||||
progress registers and the most recent listening sessions, bounded by the
|
||||
shared FRID limits and by the line budget of each message.
|
||||
|
||||
This is eventual consistency scoped to a trusted device group. A temporarily
|
||||
offline laptop can modify playlists, another device can continue playback, and
|
||||
both can later converge without a permanently available coordinator.
|
||||
@@ -190,6 +223,13 @@ must propagate and converge, not an ephemeral server-side session flag.
|
||||
|
||||
## Playback across devices
|
||||
|
||||
A persisted device identity has exactly one live coordinator. The TUI holds
|
||||
an OS file lock beside its device-sync database for the entire runtime; two
|
||||
installations using the same user data directory cannot open it concurrently.
|
||||
Network polling is independent per trusted peer, with one in-flight exchange
|
||||
per peer and a bounded deadline. A stalled peer must not delay the next poll
|
||||
of a healthy output. Poll tasks are cancelled when federation shuts down.
|
||||
|
||||
Playback has one logical state but remains physically local to the device
|
||||
producing audio.
|
||||
|
||||
@@ -197,7 +237,25 @@ The synchronized state describes the queue, current item, position, pause
|
||||
state, volume, shuffle/repeat mode, and the active playback owner. Commands are
|
||||
targeted and deduplicated. Handoff transfers intent and position; the receiving
|
||||
device resolves the track against its own library or federation sources before
|
||||
starting its audio engine.
|
||||
starting its audio engine. Each queue item names its source: a podcast episode
|
||||
travels as `PlaybackSource::PodcastEpisode` with the reference needed to play
|
||||
it (the receiving device streams it from the publisher at the handed-off
|
||||
position, with or without a subscription). A queue with episodes is not handed
|
||||
to a device below protocol v4, and playback commands from such a device are
|
||||
ignored while the local queue has episodes. Such a device still gets the
|
||||
playback snapshot with its coordination envelope, but with every episode
|
||||
replaced by an opaque "Private item" (`redact_for_jam`): an engine renews the
|
||||
owner's liveness only from the owner's own announcements, so an old device
|
||||
that heard nothing would take over by automatic failover after
|
||||
`owner_timeout_ms` and replace the queue. This departs from FRID's
|
||||
`PLAYBACK_PROTOCOL.md` rule 1 ("otherwise send `None`") and is reported to
|
||||
FRID as a contract gap. An old device that mirrors such a queue drops the
|
||||
`source` its types do not know, so each placeholder becomes a music item
|
||||
titled "Private item"; when it later takes over (Play on it, a paused-startup
|
||||
claim, failover while this device is gone) or sends a command, it announces
|
||||
those. States from devices below v4 are therefore cleaned on receipt
|
||||
(`drop_echoed_private_items`: bare music items with that title and no content
|
||||
id, federated source, artist, release or duration), with a status message.
|
||||
|
||||
Active-device leases and idle timing prevent stale snapshots from immediately
|
||||
taking control after a device reconnects. This provides practical coordination
|
||||
@@ -242,6 +300,176 @@ 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
|
||||
@@ -286,6 +514,17 @@ created or edited while Furumi is running without rebuilding or restarting the
|
||||
application. Bundled scripts use the same path and contract as user scripts,
|
||||
so built-in and custom visualizations exercise the same runtime.
|
||||
|
||||
The v2 primitives are sufficient for small perspective wireframes without a
|
||||
native 3D engine. The bundled morphing cube transforms eight vertices in Rhai
|
||||
and batches its twelve edges into trace/vertical-line commands. Perspective,
|
||||
audio-driven deformation, and depth cues stay in the user-editable script;
|
||||
Rust rasterizes the resulting 2D commands. This deliberately avoids per-cell
|
||||
script command maps and does not provide filled meshes or a depth buffer.
|
||||
The richer cube preset adds bounded particle, ring, inner-cage, and echo
|
||||
layers. Echo poses are reconstructed from earlier times with current audio
|
||||
features, rather than retaining previous frames; particles have deterministic
|
||||
lifetimes. Small terminals omit expensive layers and reduce particle counts.
|
||||
|
||||
The Rhai engine is configured as a sandboxed computation environment. Module
|
||||
loading through `import` and `export` is disabled, no filesystem or network API
|
||||
is exposed to scripts, and execution is bounded by limits on operations, call
|
||||
@@ -298,6 +537,34 @@ intended role: transforming current audio features into drawing commands. It
|
||||
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; 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
|
||||
storage or expensive custom scripts can still delay input. Benchmark script
|
||||
evaluation and buffer drawing separately from real terminal throughput before
|
||||
increasing scene complexity or the refresh rate.
|
||||
|
||||
The embedded status surface uses the same Rhai engine, input map, command
|
||||
decoder, and clipped buffer rasterizer with a one-row viewport. It has an
|
||||
independent AST cache and a fixed user-editable script in
|
||||
`visualizations/status/braille.rhai`, installed if absent. Exact copies of the
|
||||
previous bundled default can be atomically upgraded; edited files are preserved. It is not a
|
||||
fullscreen preset. During local playback the visual timer samples the audio
|
||||
analyzer without increasing trusted-device publication frequency. At most
|
||||
one blocking render job is in flight; excess ticks are dropped. File access,
|
||||
compilation, evaluation, and buffer preparation happen on that worker.
|
||||
An `AppEvent` delivers a prepared row to `AppState`; UI drawing only copies
|
||||
cells. Its audio input comes from the decoded-source analyzer before output
|
||||
volume scaling; the volume field remains informational. Pause suspends jobs
|
||||
and retains the last frame, even when the row needs resizing. Generation
|
||||
checks discard results invalidated by track changes, resize, pause/resume,
|
||||
or stop. Script errors are confined to the row and retried so
|
||||
editing the file repairs the display. Status scripts have a smaller operation
|
||||
budget, bounded source/output sizes, and cannot draw outside their viewport.
|
||||
|
||||
## Music-similarity indexing
|
||||
|
||||
Similarity search is an optional local capability and is disabled by default.
|
||||
@@ -330,6 +597,9 @@ 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 |
|
||||
@@ -337,7 +607,8 @@ Furumi stores different kinds of state according to their lifetime:
|
||||
| Settings, keymap, identity | Platform config/data dirs | Node configuration |
|
||||
| Queue and playback snapshots | Application/device sync state | Continuity and handoff |
|
||||
|
||||
Caches are replaceable. The local library and device operation log are durable.
|
||||
Caches are replaceable. The local library, the podcast store's personal state
|
||||
and the device operation log are durable.
|
||||
This distinction lets maintenance and recovery code discard derived network
|
||||
data without risking the user's collection.
|
||||
|
||||
@@ -366,6 +637,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;
|
||||
|
||||
+141
-2
@@ -7,6 +7,142 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [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.
|
||||
|
||||
## [0.2.8] - 2026-09-02
|
||||
|
||||
### Added
|
||||
|
||||
- Optional offline music-similarity search for local tracks, backed by
|
||||
@@ -27,7 +163,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Changed
|
||||
|
||||
- Similarity wire types, bounds, validation, and stream framing now come from
|
||||
the shared `music-dht 0.4.0` API so native, web, and future clients can
|
||||
the shared `music-dht 0.4` API so native, web, and future clients can
|
||||
interoperate without sharing an embedding implementation.
|
||||
- Existing SQLite embeddings are backfilled once with compact 256-bit routing
|
||||
signatures; new embeddings store them immediately without changing exact
|
||||
@@ -41,6 +177,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- Federation now recovers automatically after sleep, prolonged idle, or a
|
||||
degraded rendezvous transport while preserving local playback and state.
|
||||
- Current-track information (`Shift+I`) now uses the enriched queue entry, so
|
||||
it shows the same complete metadata and similarity action as `I` on that
|
||||
track in the queue.
|
||||
@@ -74,5 +212,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Music-directory validation and migration now reject overlapping changes,
|
||||
resolve canonical paths, and produce Windows-portable managed filenames.
|
||||
|
||||
[Unreleased]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.5...HEAD
|
||||
[Unreleased]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.8...HEAD
|
||||
[0.2.8]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.7...v0.2.8
|
||||
[0.2.5]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.4...v0.2.5
|
||||
Generated
+438
-247
File diff suppressed because it is too large.
Load diff
+15
-5
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumi_tui"
|
||||
version = "0.2.7"
|
||||
version = "0.5.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.97"
|
||||
description = "A federated P2P player for personal music libraries"
|
||||
@@ -11,20 +11,30 @@ name = "furumi"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||
semver = "1"
|
||||
self-replace = "1.5"
|
||||
tempfile = "3"
|
||||
fs2 = "0.4"
|
||||
flate2 = "1"
|
||||
tar = "0.4"
|
||||
zip = { version = "4", default-features = false, features = ["deflate"] }
|
||||
object = { version = "0.36", default-features = false, features = ["read", "std"] }
|
||||
|
||||
anyhow = "1.0.102"
|
||||
blake3 = "1"
|
||||
crokey = "1.4.0"
|
||||
crossterm = { version = "0.29.0", features = ["event-stream"] }
|
||||
directories = "6.0.0"
|
||||
futures-util = "0.3.32"
|
||||
furumi-library = "0.1.0"
|
||||
furumi-library = "0.3.0"
|
||||
image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] }
|
||||
lofty = "0.22"
|
||||
# P2P federation: library index in a shared DHT + audio streaming between
|
||||
# peers (same protocol as furumi-fd).
|
||||
music-dht = "0.4.0"
|
||||
ratatui = "0.30.1"
|
||||
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream"] }
|
||||
music-dht = { version = "0.6.0", features = ["podcast-feed"] }
|
||||
ratatui = { version = "0.30.1", features = ["unstable-rendered-line-info"] }
|
||||
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"] }
|
||||
rusty-opus = "0.9.1"
|
||||
|
||||
@@ -98,9 +98,12 @@ furumi
|
||||
### Linux, Windows, and other platforms
|
||||
|
||||
Download a prebuilt archive from the
|
||||
[GitHub releases](https://github.com/house-of-vanity/furumi_tui/releases), or
|
||||
[GitHub releases](https://github.com/Furumi-stack/furumi-tui/releases), or
|
||||
build Furumi from source with Rust 1.97 or newer:
|
||||
|
||||
The repository selects the stable Rust toolchain. With rustup, run
|
||||
`rustup update stable` to install or update it before building.
|
||||
|
||||
```bash
|
||||
cargo build --release --locked
|
||||
./target/release/furumi
|
||||
@@ -124,6 +127,257 @@ Import a music directory from Furumi's command line:
|
||||
Federation, trusted-device pairing, and key bindings are configured directly
|
||||
inside the player.
|
||||
|
||||
### Manual updates
|
||||
|
||||
In **Settings → Additional settings → Updates**, select **Check for updates**, then **Install update**
|
||||
when a newer stable GitHub release is available. Downloads run in the background.
|
||||
After installation, restart `furumi` to use the new version; playback is not
|
||||
restarted automatically. Wait for an active update operation to finish before
|
||||
quitting.
|
||||
|
||||
Updates replace the running executable in its installation directory, which
|
||||
must be writable by your user. Release archives must include a matching entry
|
||||
in the release's `SHA256SUMS` asset. Older releases without it cannot be installed
|
||||
through this feature. The updater checks SHA-256 and the executable's format
|
||||
and architecture before replacing it. Checksums provide integrity checking,
|
||||
not publisher signatures. Settings and the local library are preserved.
|
||||
|
||||
There are no automatic startup checks. Only the existing release asset naming
|
||||
scheme is supported; missing or incompatible platform builds are rejected.
|
||||
|
||||
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
|
||||
@@ -173,6 +427,60 @@ cargo check --all-targets
|
||||
cargo test --all-targets
|
||||
```
|
||||
|
||||
## Playback coordination
|
||||
|
||||
Connected Devices uses the shared `music_dht::playback` engine from frid.
|
||||
Newly started players take an idle/paused output after discovery and become
|
||||
controllers when another device is playing. Missing output reports trigger
|
||||
automatic failover; concurrent claims converge to one owner. The web gateway
|
||||
uses a passive server profile and reports actual browser activity.
|
||||
|
||||
Only one TUI process may use a device identity. Installed and locally built
|
||||
binaries use the same user data directory: close the installed player before
|
||||
starting a development build. An OS file lock prevents duplicate coordinators
|
||||
and is released automatically on exit or a crash. Older builds do not take
|
||||
this lock, so close those explicitly when upgrading.
|
||||
|
||||
Connected-device polling runs independently per peer, so an offline or stalled
|
||||
device does not postpone updates to live players.
|
||||
|
||||
The existing device menu handles manual transfers. Advanced policy can be set
|
||||
in `settings.toml` without changing the UI:
|
||||
|
||||
```toml
|
||||
[playback]
|
||||
claim_on_startup = true
|
||||
automatic_failover = true
|
||||
take_paused_on_startup = true
|
||||
discovery_ms = 3000
|
||||
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.
|
||||
|
||||
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
|
||||
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
|
||||
PostgreSQL and does not cover web database migrations. Both tests use temporary
|
||||
identities and data; no running player or user library is used.
|
||||
|
||||
## License
|
||||
|
||||
Furumi is released under the
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "1.97.0"
|
||||
channel = "stable"
|
||||
profile = "minimal"
|
||||
components = ["clippy", "rust-src", "rustfmt"]
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Build and run the real TUI and web protocol adapters against each other.
|
||||
|
||||
Usage: python scripts/test_device_interop.py [path/to/furumusic]
|
||||
No user databases, accounts, audio outputs, or external servers are used.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
|
||||
def test_binary(repo):
|
||||
result = subprocess.run(
|
||||
["cargo", "test", "--locked", "--no-run", "--message-format=json"],
|
||||
cwd=repo, text=True, encoding="utf-8", stdout=subprocess.PIPE,
|
||||
)
|
||||
result.check_returncode()
|
||||
binaries = []
|
||||
for line in result.stdout.splitlines():
|
||||
event = json.loads(line)
|
||||
if event.get("reason") == "compiler-artifact" and event.get("profile", {}).get("test") and event.get("executable"):
|
||||
binaries.append(event["executable"])
|
||||
if len(binaries) != 1:
|
||||
raise RuntimeError(f"Expected one player test binary in {repo}, found {binaries}")
|
||||
return binaries[0]
|
||||
|
||||
|
||||
def main():
|
||||
tui = Path(__file__).resolve().parents[1]
|
||||
web = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else tui.parent / "furumusic"
|
||||
tui_bin, web_bin = test_binary(tui), test_binary(web)
|
||||
with tempfile.TemporaryDirectory(prefix="furumi-interop-") as directory:
|
||||
env = dict(os.environ, FURUMI_INTEROP_DIR=directory)
|
||||
processes = []
|
||||
try:
|
||||
for binary, test in [(web_bin, "federation::devices::interop_tests::localhost_tui_peer"),
|
||||
(tui_bin, "devices::interop_tests::localhost_web_peer")]:
|
||||
listing = subprocess.check_output([binary, "--list"], text=True, encoding="utf-8")
|
||||
if f"{test}: test" not in listing:
|
||||
raise RuntimeError(f"Required test {test} is missing from {binary}")
|
||||
processes.append(subprocess.Popen(
|
||||
[binary, "--ignored", "--exact", test, "--nocapture"], env=env,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
text=True, encoding="utf-8",
|
||||
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||||
))
|
||||
codes = []
|
||||
for process in processes:
|
||||
output, _ = process.communicate(timeout=45)
|
||||
print(output, end="")
|
||||
codes.append(process.returncode)
|
||||
if any(codes):
|
||||
raise RuntimeError(f"Cross-player test failed: exit codes {codes}")
|
||||
finally:
|
||||
for process in processes:
|
||||
if process.poll() is None:
|
||||
process.kill()
|
||||
process.wait()
|
||||
print("PASS: TUI <-> WEB state, bidirectional handoff, pause/seek, duplicate fencing")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+50
-3
@@ -57,6 +57,27 @@ 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,
|
||||
}
|
||||
|
||||
/// Help-window sections, in display order.
|
||||
@@ -66,16 +87,18 @@ pub enum Category {
|
||||
Queue,
|
||||
Library,
|
||||
Navigation,
|
||||
Podcasts,
|
||||
Search,
|
||||
System,
|
||||
}
|
||||
|
||||
impl Category {
|
||||
pub const ALL: [Category; 6] = [
|
||||
pub const ALL: [Category; 7] = [
|
||||
Category::Playback,
|
||||
Category::Queue,
|
||||
Category::Library,
|
||||
Category::Navigation,
|
||||
Category::Podcasts,
|
||||
Category::Search,
|
||||
Category::System,
|
||||
];
|
||||
@@ -86,6 +109,7 @@ impl Category {
|
||||
Category::Queue => "Queue & playlists",
|
||||
Category::Library => "Library",
|
||||
Category::Navigation => "Navigation",
|
||||
Category::Podcasts => "Podcasts",
|
||||
Category::Search => "Search & commands",
|
||||
Category::System => "System",
|
||||
}
|
||||
@@ -139,6 +163,16 @@ impl Action {
|
||||
| Action::DeleteSelected
|
||||
| Action::CycleSourceMode
|
||||
| Action::OpenLibraryFilters => Category::Library,
|
||||
Action::AddPodcast
|
||||
| Action::SearchEpisodes
|
||||
| Action::RefreshPodcast
|
||||
| Action::RefreshAllPodcasts
|
||||
| Action::CycleEpisodeSort
|
||||
| Action::CycleEpisodeFilter
|
||||
| Action::ToggleEpisodePlayed
|
||||
| Action::UnsubscribePodcast
|
||||
| Action::RestartEpisode
|
||||
| Action::TogglePodcastShare => Category::Podcasts,
|
||||
Action::OpenSearch | Action::OpenCommandLine => Category::Search,
|
||||
Action::ToggleHelp | Action::Quit => Category::System,
|
||||
}
|
||||
@@ -170,7 +204,10 @@ impl Action {
|
||||
Action::Quit => "Quit".into(),
|
||||
Action::NextTab => "Next tab".into(),
|
||||
Action::PrevTab => "Previous tab".into(),
|
||||
Action::GoToTab(i) => format!("Go to tab {}", i + 1),
|
||||
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::MoveUp => "Move up".into(),
|
||||
Action::MoveDown => "Move down".into(),
|
||||
Action::MoveLeft => "Move left".into(),
|
||||
@@ -212,9 +249,19 @@ 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".into(),
|
||||
Action::OpenSearch => "Search artists, releases, tracks, public podcasts".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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,9 @@ 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);
|
||||
@@ -132,7 +134,9 @@ 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);
|
||||
@@ -147,9 +151,47 @@ 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,
|
||||
@@ -171,7 +213,9 @@ 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;
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
//! Scheduling for local cards enriched by replaceable federation data.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::state::{AppState, GlobalView, Loadable, Tab};
|
||||
|
||||
const REFRESH_INTERVAL: Duration = Duration::from_secs(30);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub(super) enum Card {
|
||||
Artist(i64),
|
||||
Release(i64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct RefreshState {
|
||||
visible: Option<Card>,
|
||||
/// None means a request is in flight; Some is its completion time.
|
||||
requests: HashMap<Card, Option<Instant>>,
|
||||
}
|
||||
|
||||
impl RefreshState {
|
||||
pub(super) fn is_loading(&self, card: Card) -> bool {
|
||||
matches!(self.requests.get(&card), Some(None))
|
||||
}
|
||||
|
||||
fn begin(&mut self, visible: Option<Card>, now: Instant) -> bool {
|
||||
let reopened = self.visible != visible;
|
||||
self.visible = visible;
|
||||
let Some(card) = visible else { return false };
|
||||
match self.requests.get(&card) {
|
||||
Some(None) => return false,
|
||||
Some(Some(completed))
|
||||
if !reopened && now.duration_since(*completed) < REFRESH_INTERVAL =>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.requests.insert(card, None);
|
||||
true
|
||||
}
|
||||
|
||||
pub(super) fn complete(&mut self, card: Card, now: Instant) {
|
||||
self.requests.insert(card, Some(now));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) enum Request {
|
||||
Artist {
|
||||
id: i64,
|
||||
name: String,
|
||||
},
|
||||
Release {
|
||||
id: i64,
|
||||
title: String,
|
||||
artists: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Called on navigation and maintenance ticks. A cached success or failure
|
||||
/// must not suppress discovery for the rest of the session.
|
||||
pub(super) fn prepare(state: &mut AppState, now: Instant) -> Option<Request> {
|
||||
let visible = if state.federation.settings.enabled
|
||||
&& state.global.filters.source_mode.includes_network()
|
||||
&& state.active_tab == Tab::Global
|
||||
{
|
||||
match state.global.stack.last() {
|
||||
Some(GlobalView::Artist { id, .. }) => Some(Card::Artist(*id)),
|
||||
Some(GlobalView::Release { id, .. }) => Some(Card::Release(*id)),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let request = match visible {
|
||||
Some(Card::Artist(id)) => match state.artist_views.get(&id) {
|
||||
Some(Loadable::Ready(detail)) => Some(Request::Artist {
|
||||
id,
|
||||
name: detail.name.clone(),
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
Some(Card::Release(id)) => match state.release_views.get(&id) {
|
||||
Some(Loadable::Ready(detail)) => {
|
||||
let mut artists = Vec::new();
|
||||
for artist in &detail.artists {
|
||||
if !artists.iter().any(|name: &String| {
|
||||
music_dht::normalize_name(name) == music_dht::normalize_name(&artist.name)
|
||||
}) {
|
||||
artists.push(artist.name.clone());
|
||||
}
|
||||
}
|
||||
(!artists.is_empty()).then(|| Request::Release {
|
||||
id,
|
||||
title: detail.title.clone(),
|
||||
artists,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
if !state
|
||||
.card_enrichment
|
||||
.begin(visible.filter(|_| request.is_some()), now)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
match &request {
|
||||
Some(Request::Artist { id, .. }) => mark_loading(&mut state.artist_fed_views, *id),
|
||||
Some(Request::Release { id, .. }) => mark_loading(&mut state.release_fed_views, *id),
|
||||
None => {}
|
||||
}
|
||||
request
|
||||
}
|
||||
|
||||
fn mark_loading<T>(views: &mut HashMap<i64, Loadable<T>>, id: i64) {
|
||||
if !matches!(views.get(&id), Some(Loadable::Ready(_))) {
|
||||
views.insert(id, Loadable::Loading);
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep the last usable snapshot if a refresh fails while peers are offline.
|
||||
pub(super) fn store_result<T>(
|
||||
views: &mut HashMap<i64, Loadable<T>>,
|
||||
id: i64,
|
||||
result: Result<T, String>,
|
||||
) {
|
||||
match result {
|
||||
Ok(value) => {
|
||||
views.insert(id, Loadable::Ready(value));
|
||||
}
|
||||
Err(message) if !matches!(views.get(&id), Some(Loadable::Ready(_))) => {
|
||||
views.insert(id, Loadable::Failed(message));
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
+105
-3
@@ -10,7 +10,11 @@ use crate::library::models::{
|
||||
/// the playback engine, imports). Tasks never touch AppState directly.
|
||||
#[derive(Debug)]
|
||||
pub enum AppEvent {
|
||||
UpdateChecked(Result<Option<crate::updater::Update>, String>),
|
||||
UpdateProgress(String),
|
||||
UpdateInstalled(Result<(), String>),
|
||||
StatusMessage(String),
|
||||
StatusVisualizationReady(crate::visualizer::status::Ready),
|
||||
/// A page of the artists list arrived (or failed).
|
||||
ArtistsLoaded(Result<ArtistsPage, String>),
|
||||
/// A full reload after a library change: replaces the loaded artist
|
||||
@@ -27,6 +31,11 @@ pub enum AppEvent {
|
||||
id: i64,
|
||||
result: Result<ReleaseDetail, String>,
|
||||
},
|
||||
/// Federated tracks that complete an already-open local release view.
|
||||
ReleaseFederationLoaded {
|
||||
id: i64,
|
||||
result: Result<Option<crate::federation::FedRelease>, String>,
|
||||
},
|
||||
/// Live search results; `seq` drops responses that are already stale.
|
||||
SearchLoaded {
|
||||
seq: u64,
|
||||
@@ -121,13 +130,21 @@ pub enum AppEvent {
|
||||
tracks: Vec<TrackItem>,
|
||||
},
|
||||
/// A status snapshot for the Federation tab.
|
||||
FederationStatus(crate::federation::FedStatus),
|
||||
/// Boxed to keep `AppEvent` small; the status is large and rare.
|
||||
FederationStatus(Box<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,
|
||||
@@ -163,7 +180,8 @@ pub enum AppEvent {
|
||||
FedTrackInfoLoaded {
|
||||
placeholder_id: i64,
|
||||
item_id: String,
|
||||
result: Result<TrackItem, String>,
|
||||
/// Boxed: `TrackItem` is large since it can carry a podcast episode.
|
||||
result: Result<Box<TrackItem>, String>,
|
||||
},
|
||||
/// This peer's connection ticket, requested from the Federation tab.
|
||||
FedTicket(Result<String, String>),
|
||||
@@ -182,7 +200,11 @@ pub enum AppEvent {
|
||||
/// Trusted device playback state, delivered by personal-device sync.
|
||||
DevicePlayback(crate::devices::PlaybackSnapshot),
|
||||
/// Playback command addressed to this device.
|
||||
PlaybackCommand(crate::devices::PlaybackCommand),
|
||||
PlaybackCommand {
|
||||
command: crate::devices::PlaybackCommand,
|
||||
authority: music_dht::playback::CommandStamp,
|
||||
origin: String,
|
||||
},
|
||||
/// Current lifecycle/status of the federation Jam.
|
||||
JamStatus(crate::jam::JamStatus),
|
||||
/// Host playback snapshot received by a Jam participant.
|
||||
@@ -193,4 +215,84 @@ 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.
|
||||
PodcastEpisodesLoaded {
|
||||
seq: u64,
|
||||
offset: usize,
|
||||
result: Result<Vec<furumi_library::podcasts::EpisodeListItem>, String>,
|
||||
},
|
||||
PodcastSubscribed(Result<crate::podcasts::refresh::SubscribeReport, String>),
|
||||
/// One feed check 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,
|
||||
},
|
||||
PodcastUnsubscribed {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
title: String,
|
||||
result: Result<(), String>,
|
||||
},
|
||||
/// Result of the progress writer (saves, supersedes, mark played).
|
||||
PodcastProgress(crate::podcasts::progress::ProgressEvent),
|
||||
/// An episode stream is playing; `generation` is the player source
|
||||
/// generation of the start that requested it.
|
||||
PodcastEpisodeStarted {
|
||||
generation: u64,
|
||||
episode: Box<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>,
|
||||
},
|
||||
}
|
||||
+1003
-235
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,138 @@
|
||||
//! Podcast state fixtures shared by update and rendering tests.
|
||||
|
||||
use furumi_library::podcasts::{
|
||||
EpisodeListItem, EpisodeQuery, FetchState, PodcastSummary, SeasonSummary,
|
||||
};
|
||||
use music_dht::podcast::{
|
||||
EpisodeId, EpisodeMeta, EpisodeType, FeedUrl, PodcastId, PodcastMeta, ProgressRecord,
|
||||
ProgressStatus,
|
||||
};
|
||||
|
||||
use crate::app::state::{
|
||||
AppState, EpisodeList, Loadable, OpenedPodcast, PodcastRow, PodcastStoreStatus, Tab,
|
||||
};
|
||||
|
||||
pub fn podcast_id() -> PodcastId {
|
||||
PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/feed.xml").unwrap())
|
||||
}
|
||||
|
||||
pub fn row(title: &str, unplayed: u64, episodes: u64, seasons: u64) -> PodcastRow {
|
||||
PodcastRow {
|
||||
summary: PodcastSummary {
|
||||
podcast_id: podcast_id(),
|
||||
meta: PodcastMeta {
|
||||
feed_url: FeedUrl::parse("https://example.test/feed.xml").unwrap(),
|
||||
title: title.into(),
|
||||
author: Some("Host".into()),
|
||||
description: Some("A show about tests.".into()),
|
||||
link: None,
|
||||
image_url: None,
|
||||
},
|
||||
subscribed: true,
|
||||
shared: false,
|
||||
episode_count: episodes,
|
||||
unplayed_count: unplayed,
|
||||
in_progress_count: 0,
|
||||
latest_published_at_ms: None,
|
||||
season_count: seasons,
|
||||
},
|
||||
fetch: FetchState::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn episode(n: u32, title: &str, status: Option<(ProgressStatus, i64)>) -> EpisodeListItem {
|
||||
let podcast = podcast_id();
|
||||
let episode_id = EpisodeId::from_guid(&podcast, &format!("ep-{n}")).unwrap();
|
||||
EpisodeListItem {
|
||||
episode: EpisodeMeta {
|
||||
episode_id: episode_id.clone(),
|
||||
podcast_id: podcast.clone(),
|
||||
guid: Some(format!("ep-{n}")),
|
||||
title: title.into(),
|
||||
description: Some(format!("Notes for {title}.")),
|
||||
season: Some(1),
|
||||
episode_number: Some(n),
|
||||
episode_type: EpisodeType::Full,
|
||||
published_at_ms: Some(1_718_409_600_000),
|
||||
duration_ms: Some(2_700_000),
|
||||
audio_url: format!("https://example.test/{n}.mp3"),
|
||||
mime: Some("audio/mpeg".into()),
|
||||
byte_len: None,
|
||||
image_url: None,
|
||||
link: None,
|
||||
},
|
||||
progress: status.map(|(status, position_ms)| ProgressRecord {
|
||||
episode_id,
|
||||
podcast_id: podcast,
|
||||
position_ms,
|
||||
status,
|
||||
generation: 1,
|
||||
session_id: "s".into(),
|
||||
hlc_ms: 1,
|
||||
device_id: "dev".into(),
|
||||
op_id: "dev:1".into(),
|
||||
}),
|
||||
completed_count: 0,
|
||||
in_feed: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ready_state(rows: Vec<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),
|
||||
)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
+84
-4
@@ -115,6 +115,29 @@ 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;
|
||||
@@ -557,10 +580,31 @@ fn handle_fed_input(
|
||||
key: KeyEvent,
|
||||
) {
|
||||
match key.code {
|
||||
KeyCode::Esc => {}
|
||||
KeyCode::Esc => {
|
||||
// Esc in the episode search clears it.
|
||||
if field == FedInputField::PodcastSearch
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, String::new())
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let value = input.trim().to_string();
|
||||
match field {
|
||||
FedInputField::PodcastFeedUrl => {
|
||||
if let Some(effect) =
|
||||
crate::app::update::podcasts::submit_feed_url(state, &value)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
// The search is applied while typing; Enter keeps it.
|
||||
FedInputField::PodcastSearch => {
|
||||
let search = input.to_string();
|
||||
if let Some(effect) = crate::app::update::podcasts::set_search(state, search) {
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
FedInputField::NetworkId => {
|
||||
state.federation.settings.network_id = value;
|
||||
// An empty id turns federation off rather than leaving a
|
||||
@@ -666,7 +710,13 @@ fn handle_fed_input(
|
||||
}
|
||||
_ => {
|
||||
input.handle_key(key);
|
||||
let search = (field == FedInputField::PodcastSearch).then(|| input.to_string());
|
||||
state.popup = Some(Popup::FedInput { field, input });
|
||||
if let Some(search) = search
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, search)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -785,11 +835,17 @@ fn handle_device_leave(state: &mut AppState, runtime: &mut Runtime, key: KeyEven
|
||||
}
|
||||
|
||||
/// Pasted text goes into the focused text field when one is open.
|
||||
pub fn handle_paste(state: &mut AppState, pasted: &str) {
|
||||
pub fn handle_paste(state: &mut AppState, runtime: &mut Runtime, pasted: &str) {
|
||||
let cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect();
|
||||
let mut search = None;
|
||||
match &mut state.popup {
|
||||
Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned),
|
||||
Some(Popup::FedInput { input, .. }) => input.insert_str(&cleaned),
|
||||
Some(Popup::FedInput { field, input }) => {
|
||||
input.insert_str(&cleaned);
|
||||
if *field == FedInputField::PodcastSearch {
|
||||
search = Some(input.to_string());
|
||||
}
|
||||
}
|
||||
Some(Popup::Edit { fields, focus, .. }) => {
|
||||
if let Some(field) = fields.get_mut(*focus) {
|
||||
field.value.insert_str(&cleaned);
|
||||
@@ -797,6 +853,11 @@ pub fn handle_paste(state: &mut AppState, pasted: &str) {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(search) = search
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, search)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1018,6 +1079,21 @@ fn handle_track_info(
|
||||
let len = tracks.len();
|
||||
match key.code {
|
||||
KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q') => {}
|
||||
// An episode's "artist" is its podcast title: never look it up as a
|
||||
// federated artist (that would send a private title to the
|
||||
// network); open the podcast instead.
|
||||
KeyCode::Char('a')
|
||||
if tracks
|
||||
.get(cursor.min(len.saturating_sub(1)))
|
||||
.is_some_and(TrackItem::is_podcast) =>
|
||||
{
|
||||
let track = tracks[cursor.min(len.saturating_sub(1))].clone();
|
||||
if let Some(effect) =
|
||||
crate::app::update::podcasts::reveal_episode_podcast(state, &track)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('a') => {
|
||||
let artists = tracks
|
||||
.get(cursor.min(len.saturating_sub(1)))
|
||||
@@ -1079,6 +1155,10 @@ fn handle_track_info(
|
||||
KeyCode::Char('c') => {
|
||||
if let Some(track) = tracks.get(cursor.min(len.saturating_sub(1))) {
|
||||
match crate::share::track_share_link(track) {
|
||||
None if track.is_podcast() => {
|
||||
state.status_message =
|
||||
Some("podcast episodes have no track share link".into())
|
||||
}
|
||||
Some(link) => {
|
||||
state.popup = Some(Popup::FedCopyText {
|
||||
title: "Track share link".into(),
|
||||
@@ -1108,7 +1188,7 @@ fn handle_track_info(
|
||||
cursor,
|
||||
scroll,
|
||||
});
|
||||
} else if track.id < 0 || track.file_path.is_empty() {
|
||||
} else if track.is_podcast() || track.id < 0 || track.file_path.is_empty() {
|
||||
state.status_message = Some("similarity search starts from a local track".into());
|
||||
state.popup = Some(Popup::TrackInfo {
|
||||
tracks,
|
||||
|
||||
+502
-25
@@ -303,6 +303,80 @@ mod tests {
|
||||
assert_eq!(fed_release_display_order(&releases), vec![2, 0, 3, 1, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_tracks_adds_remote_rows_without_duplicating_local_content() {
|
||||
let local_content =
|
||||
"b3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string();
|
||||
let detail = ReleaseDetail {
|
||||
id: 7,
|
||||
title: "Album".to_string(),
|
||||
release_type: "album".to_string(),
|
||||
year: Some(2024),
|
||||
cover_path: None,
|
||||
artists: vec![crate::library::models::ArtistRef {
|
||||
id: 3,
|
||||
name: "Artist".to_string(),
|
||||
}],
|
||||
tracks: vec![TrackItem {
|
||||
id: 1,
|
||||
title: "Local".to_string(),
|
||||
track_number: Some(1),
|
||||
disc_number: None,
|
||||
duration_seconds: 120.0,
|
||||
artists: Vec::new(),
|
||||
featured_artists: Vec::new(),
|
||||
release_id: 7,
|
||||
release_title: "Album".to_string(),
|
||||
release_year: Some(2024),
|
||||
file_path: "/music/local.mp3".to_string(),
|
||||
content_id: Some(local_content.clone()),
|
||||
cover_path: None,
|
||||
audio_format: Some("mp3".to_string()),
|
||||
audio_bitrate: Some(320),
|
||||
audio_sample_rate: None,
|
||||
audio_bit_depth: None,
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
}],
|
||||
};
|
||||
let mut state = AppState::default();
|
||||
state.release_fed_views.insert(
|
||||
detail.id,
|
||||
Loadable::Ready(crate::federation::FedRelease {
|
||||
title: "Album".to_string(),
|
||||
tracks: vec![
|
||||
crate::federation::FedCardTrack {
|
||||
title: "Local".to_string(),
|
||||
track_number: Some(1),
|
||||
content_id: Some(local_content),
|
||||
sources: vec![("peer".to_string(), "local-item".to_string())],
|
||||
..Default::default()
|
||||
},
|
||||
crate::federation::FedCardTrack {
|
||||
title: "Remote".to_string(),
|
||||
track_number: Some(3),
|
||||
sources: vec![("peer".to_string(), "remote-item".to_string())],
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
|
||||
let tracks = release_tracks(&state, detail.id, &detail);
|
||||
|
||||
assert_eq!(
|
||||
tracks
|
||||
.iter()
|
||||
.map(|track| track.title.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
vec!["Local", "Remote"]
|
||||
);
|
||||
assert!(tracks[1].is_fed_pending());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_merged_releases_marks_partially_local_federated_release() {
|
||||
let local_id = "b3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
@@ -689,6 +763,37 @@ 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.
|
||||
@@ -826,6 +931,10 @@ pub enum FedInputField {
|
||||
DeviceName,
|
||||
ConnectInvite,
|
||||
JamInvite,
|
||||
/// Podcasts: RSS/Atom feed URL of a new subscription.
|
||||
PodcastFeedUrl,
|
||||
/// Podcasts: live search in the opened episode list.
|
||||
PodcastSearch,
|
||||
}
|
||||
|
||||
impl FedInputField {
|
||||
@@ -840,6 +949,8 @@ impl FedInputField {
|
||||
FedInputField::DeviceName => "Device name",
|
||||
FedInputField::ConnectInvite => "Connect device (paste frid://i invite)",
|
||||
FedInputField::JamInvite => "Join Jam (paste frid://j invite)",
|
||||
FedInputField::PodcastFeedUrl => "Add podcast (RSS feed URL)",
|
||||
FedInputField::PodcastSearch => "Search episodes",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -872,6 +983,12 @@ impl FedInputField {
|
||||
FedInputField::JamInvite => {
|
||||
"Paste a frid://j capability to control the host player for this Jam only."
|
||||
}
|
||||
FedInputField::PodcastFeedUrl => {
|
||||
"Paste the podcast's RSS or Atom feed URL. Only episode information is stored; audio streams from the publisher when you play. New subscriptions are private."
|
||||
}
|
||||
FedInputField::PodcastSearch => {
|
||||
"Words from episode titles or descriptions; the list updates as you type. Enter keeps the search, Esc clears it."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -926,6 +1043,9 @@ impl FedRow {
|
||||
/// the config directory.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SettingsRow {
|
||||
AdditionalSettings,
|
||||
CheckUpdate,
|
||||
InstallUpdate,
|
||||
MusicDirectory,
|
||||
Similarity(SimilarityRow),
|
||||
Federation(FedRow),
|
||||
@@ -1064,20 +1184,13 @@ pub fn device_status_order(state: &AppState) -> Vec<usize> {
|
||||
indices
|
||||
}
|
||||
|
||||
pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
|
||||
let mut rows = vec![SettingsRow::MusicDirectory];
|
||||
rows.extend(SimilarityRow::ALL.into_iter().map(SettingsRow::Similarity));
|
||||
rows.extend(FedRow::ALL.into_iter().map(SettingsRow::Federation));
|
||||
rows.push(SettingsRow::DeviceName);
|
||||
rows.push(SettingsRow::DeviceInvite);
|
||||
rows.push(SettingsRow::DeviceConnect);
|
||||
rows.push(SettingsRow::DeviceSyncNow);
|
||||
rows.push(SettingsRow::DeviceLeaveGroup);
|
||||
rows.extend(
|
||||
device_status_order(state)
|
||||
.into_iter()
|
||||
.map(SettingsRow::Device),
|
||||
);
|
||||
/// Secondary settings share actions but have independent navigation.
|
||||
pub fn additional_settings_rows(state: &AppState) -> Vec<SettingsRow> {
|
||||
let mut rows = vec![
|
||||
SettingsRow::MusicDirectory,
|
||||
SettingsRow::CheckUpdate,
|
||||
SettingsRow::InstallUpdate,
|
||||
];
|
||||
rows.push(SettingsRow::VisualizationClock);
|
||||
rows.extend(
|
||||
state
|
||||
@@ -1091,7 +1204,23 @@ pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
|
||||
if !state.visualizer.scripts.is_empty() {
|
||||
rows.push(SettingsRow::VisualizationEdit);
|
||||
}
|
||||
rows.push(SettingsRow::StatusDetails);
|
||||
rows
|
||||
}
|
||||
|
||||
pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
|
||||
let mut rows = vec![SettingsRow::AdditionalSettings, SettingsRow::StatusDetails];
|
||||
rows.extend(FedRow::ALL.into_iter().map(SettingsRow::Federation));
|
||||
rows.push(SettingsRow::DeviceName);
|
||||
rows.push(SettingsRow::DeviceInvite);
|
||||
rows.push(SettingsRow::DeviceConnect);
|
||||
rows.push(SettingsRow::DeviceSyncNow);
|
||||
rows.push(SettingsRow::DeviceLeaveGroup);
|
||||
rows.extend(
|
||||
device_status_order(state)
|
||||
.into_iter()
|
||||
.map(SettingsRow::Device),
|
||||
);
|
||||
rows.extend(SimilarityRow::ALL.into_iter().map(SettingsRow::Similarity));
|
||||
rows
|
||||
}
|
||||
|
||||
@@ -1297,7 +1426,7 @@ impl SimilaritySearchHit {
|
||||
Self::Local { track, .. } => track
|
||||
.content_id
|
||||
.clone()
|
||||
.unwrap_or_else(|| format!("local:{}", track.id)),
|
||||
.unwrap_or_else(|| track.key().to_string()),
|
||||
Self::Federated { track, .. } => track
|
||||
.content_id
|
||||
.clone()
|
||||
@@ -1317,7 +1446,14 @@ 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>,
|
||||
@@ -1330,6 +1466,24 @@ 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()
|
||||
}
|
||||
@@ -1382,15 +1536,20 @@ pub enum Tab {
|
||||
Global,
|
||||
Playlists,
|
||||
Queue,
|
||||
Podcasts,
|
||||
Federation,
|
||||
Logs,
|
||||
}
|
||||
|
||||
impl Tab {
|
||||
pub const ALL: [Tab; 5] = [
|
||||
/// 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.
|
||||
pub const ALL: [Tab; 6] = [
|
||||
Tab::Global,
|
||||
Tab::Playlists,
|
||||
Tab::Queue,
|
||||
Tab::Podcasts,
|
||||
Tab::Federation,
|
||||
Tab::Logs,
|
||||
];
|
||||
@@ -1400,11 +1559,24 @@ 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",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index(self) -> usize {
|
||||
Self::ALL.iter().position(|t| *t == self).unwrap()
|
||||
}
|
||||
@@ -1426,6 +1598,7 @@ 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,
|
||||
}
|
||||
@@ -1529,6 +1702,7 @@ impl DevicePlaybackRole {
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DevicePlaybackState {
|
||||
pub config: music_dht::playback::Config,
|
||||
pub role: DevicePlaybackRole,
|
||||
pub self_device_id: String,
|
||||
pub self_device_name: String,
|
||||
@@ -1539,9 +1713,6 @@ pub struct DevicePlaybackState {
|
||||
pub remote: BTreeMap<String, crate::devices::PlaybackSnapshot>,
|
||||
pub last_remote_snapshot: Option<crate::devices::PlaybackSnapshot>,
|
||||
pub jam_host: bool,
|
||||
/// A freshly started TUI owns playback by protocol. The first active
|
||||
/// snapshot discovered during startup is imported and handed off here.
|
||||
pub startup_takeover_pending: bool,
|
||||
}
|
||||
|
||||
impl DevicePlaybackState {
|
||||
@@ -1550,10 +1721,6 @@ impl DevicePlaybackState {
|
||||
|| (self.role == DevicePlaybackRole::Jam && !self.jam_host)
|
||||
}
|
||||
|
||||
pub fn is_personal_control(&self) -> bool {
|
||||
self.role == DevicePlaybackRole::Control
|
||||
}
|
||||
|
||||
pub fn is_audio_owner(&self) -> bool {
|
||||
self.role == DevicePlaybackRole::Active
|
||||
|| (self.role == DevicePlaybackRole::Jam && self.jam_host)
|
||||
@@ -1567,10 +1734,213 @@ impl DevicePlaybackState {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Podcasts tab
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Episodes loaded per page; the next page loads when the cursor nears the end.
|
||||
pub const PODCAST_EPISODE_PAGE: usize = 500;
|
||||
/// "Add podcast" and "Check all" rows above the subscriptions.
|
||||
pub const PODCAST_ACTION_ROWS: usize = 2;
|
||||
|
||||
/// State of the podcast store (`podcasts.sqlite3`).
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub enum PodcastStoreStatus {
|
||||
#[default]
|
||||
Opening,
|
||||
Ready,
|
||||
/// The store could not be opened (newer schema, foreign file, I/O). The
|
||||
/// file was left unchanged; only the Podcasts tab is unavailable.
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
/// A subscribed podcast with its local fetch state.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PodcastRow {
|
||||
pub summary: furumi_library::podcasts::PodcastSummary,
|
||||
pub fetch: furumi_library::podcasts::FetchState,
|
||||
}
|
||||
|
||||
impl PodcastRow {
|
||||
pub fn id(&self) -> &music_dht::podcast::PodcastId {
|
||||
&self.summary.podcast_id
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &str {
|
||||
&self.summary.meta.title
|
||||
}
|
||||
}
|
||||
|
||||
/// One row of the subscriptions list.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PodcastListRow {
|
||||
Add,
|
||||
RefreshAll,
|
||||
/// Index into the loaded podcasts.
|
||||
Podcast(usize),
|
||||
}
|
||||
|
||||
/// One row of a podcast's season list.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SeasonRow {
|
||||
All,
|
||||
/// Index into the loaded seasons.
|
||||
Group(usize),
|
||||
}
|
||||
|
||||
/// The podcast opened from the list: its season list (when the feed has
|
||||
/// seasons) and the episode list of the chosen group.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct OpenedPodcast {
|
||||
pub podcast_id: music_dht::podcast::PodcastId,
|
||||
/// Season list cursor; `None` when the podcast has no seasons and its
|
||||
/// episodes open directly.
|
||||
pub season_cursor: Option<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] {
|
||||
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 list_rows(&self) -> Vec<PodcastListRow> {
|
||||
let mut rows = vec![PodcastListRow::Add, PodcastListRow::RefreshAll];
|
||||
rows.extend((0..self.podcasts().len()).map(PodcastListRow::Podcast));
|
||||
rows
|
||||
}
|
||||
|
||||
/// The opened podcast, or the one under the list cursor.
|
||||
pub fn current_podcast(&self) -> Option<&PodcastRow> {
|
||||
if let Some(opened) = &self.opened {
|
||||
return self.podcast(&opened.podcast_id);
|
||||
}
|
||||
match self.list_rows().get(self.selected)? {
|
||||
PodcastListRow::Podcast(index) => self.podcasts().get(*index),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Season groups of the opened podcast, when loaded.
|
||||
pub fn opened_seasons(&self) -> Option<&[furumi_library::podcasts::SeasonSummary]> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
match &self.seasons {
|
||||
Some((id, Loadable::Ready(seasons))) if *id == opened.podcast_id => Some(seasons),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn season_rows(&self) -> Vec<SeasonRow> {
|
||||
let mut rows = vec![SeasonRow::All];
|
||||
rows.extend((0..self.opened_seasons().map_or(0, <[_]>::len)).map(SeasonRow::Group));
|
||||
rows
|
||||
}
|
||||
|
||||
/// Whether the episode list (rather than the season list) is shown.
|
||||
pub fn showing_episodes(&self) -> bool {
|
||||
self.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.season.is_some())
|
||||
}
|
||||
|
||||
/// Episodes of the opened group, when they belong to it.
|
||||
pub fn opened_episodes(&self) -> Option<&EpisodeList> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
let season = opened.season?;
|
||||
self.episodes.as_ref().filter(|list| {
|
||||
list.query.podcast_id == opened.podcast_id && list.query.season == season
|
||||
})
|
||||
}
|
||||
|
||||
pub fn selected_episode(&self) -> Option<&furumi_library::podcasts::EpisodeListItem> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
let list = self.opened_episodes()?;
|
||||
list.items.get(opened.episode_cursor)
|
||||
}
|
||||
|
||||
/// The selected episode as a queue item (explicit podcast source).
|
||||
pub fn selected_episode_track(&self) -> Option<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))
|
||||
}
|
||||
}
|
||||
|
||||
/// Single source of truth for the UI. Mutated only by `update()` and the
|
||||
/// event handlers in the main loop; views render from `&AppState`.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AppState {
|
||||
pub updater: crate::updater::State,
|
||||
pub active_tab: Tab,
|
||||
pub should_quit: bool,
|
||||
pub shutting_down: bool,
|
||||
@@ -1582,6 +1952,9 @@ pub struct AppState {
|
||||
pub status_message: Option<String>,
|
||||
pub spinner_frame: usize,
|
||||
pub settings_cursor: usize,
|
||||
/// Kept open underneath child dialogs and asynchronous confirmations.
|
||||
pub additional_settings_open: bool,
|
||||
pub additional_settings_cursor: usize,
|
||||
/// Root for music permanently downloaded from federation peers.
|
||||
pub music_dir: std::path::PathBuf,
|
||||
pub music_dir_changing: bool,
|
||||
@@ -1589,11 +1962,15 @@ pub struct AppState {
|
||||
pub device_playback: DevicePlaybackState,
|
||||
pub jam: crate::jam::JamStatus,
|
||||
pub visualizer: crate::visualizer::VisualizerState,
|
||||
pub status_visualizer: crate::visualizer::status::State,
|
||||
pub global: GlobalTab,
|
||||
pub artist_views: HashMap<i64, Loadable<ArtistDetail>>,
|
||||
/// Federated card data that enriches a local artist page in-place.
|
||||
pub artist_fed_views: HashMap<i64, Loadable<crate::federation::FedArtistCard>>,
|
||||
pub release_views: HashMap<i64, Loadable<ReleaseDetail>>,
|
||||
/// Federated tracks that enrich an already-open local release view.
|
||||
pub release_fed_views: HashMap<i64, Loadable<crate::federation::FedRelease>>,
|
||||
pub(super) card_enrichment: super::enrichment::RefreshState,
|
||||
pub playlists: PlaylistsTab,
|
||||
pub playlist_views: HashMap<i64, Loadable<PlaylistDetail>>,
|
||||
/// Liked local-library content ids, for the ♥ markers everywhere tracks are shown.
|
||||
@@ -1611,6 +1988,7 @@ pub struct AppState {
|
||||
pub queue_tab: QueueTab,
|
||||
pub federation: FederationTab,
|
||||
pub similarity: SimilarityTab,
|
||||
pub podcasts: PodcastsTab,
|
||||
/// The one federated artist card being viewed (name + loading state);
|
||||
/// opening another card replaces it.
|
||||
pub fed_artist_view: Option<(String, Loadable<crate::federation::FedArtistCard>)>,
|
||||
@@ -1631,6 +2009,18 @@ pub struct AppState {
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn artist_federation_loading(&self, id: i64) -> bool {
|
||||
self.card_enrichment
|
||||
.is_loading(super::enrichment::Card::Artist(id))
|
||||
|| matches!(self.artist_fed_views.get(&id), Some(Loadable::Loading))
|
||||
}
|
||||
|
||||
pub fn release_federation_loading(&self, id: i64) -> bool {
|
||||
self.card_enrichment
|
||||
.is_loading(super::enrichment::Card::Release(id))
|
||||
|| matches!(self.release_fed_views.get(&id), Some(Loadable::Loading))
|
||||
}
|
||||
|
||||
pub fn advance_spinner(&mut self) {
|
||||
self.spinner_frame = self.spinner_frame.wrapping_add(1);
|
||||
}
|
||||
@@ -1711,6 +2101,75 @@ pub fn artist_fed_card(state: &AppState, id: i64) -> Option<&crate::federation::
|
||||
}
|
||||
}
|
||||
|
||||
/// The tracklist rendered and played by an opened release. When federation
|
||||
/// has supplied a matching release, local rows remain authoritative and only
|
||||
/// tracks that are not already present locally are appended.
|
||||
pub fn release_tracks(state: &AppState, id: i64, detail: &ReleaseDetail) -> Vec<TrackItem> {
|
||||
let mut tracks = detail.tracks.clone();
|
||||
if !state.global.filters.source_mode.includes_network() {
|
||||
return tracks;
|
||||
}
|
||||
let Some(Loadable::Ready(fed_release)) = state.release_fed_views.get(&id) else {
|
||||
return tracks;
|
||||
};
|
||||
for fed in &fed_release.tracks {
|
||||
let Some((owner, item_id)) = fed.sources.first().cloned() else {
|
||||
continue;
|
||||
};
|
||||
let fed_content_id = fed
|
||||
.content_id
|
||||
.as_deref()
|
||||
.and_then(music_dht::normalize_content_id);
|
||||
let already_local = tracks.iter().any(|local| {
|
||||
let same_content = fed_content_id.is_some()
|
||||
&& track_content_id(local).as_deref() == fed_content_id.as_deref();
|
||||
let same_position = local.track_number == fed.track_number
|
||||
|| local.track_number.is_none()
|
||||
|| fed.track_number.is_none();
|
||||
same_content
|
||||
|| (music_dht::normalize_name(&local.title)
|
||||
== music_dht::normalize_name(&fed.title)
|
||||
&& same_position)
|
||||
});
|
||||
if already_local {
|
||||
continue;
|
||||
}
|
||||
let names = if fed.artists.is_empty() {
|
||||
detail
|
||||
.artists
|
||||
.iter()
|
||||
.map(|artist| artist.name.clone())
|
||||
.collect()
|
||||
} else {
|
||||
fed.artists.clone()
|
||||
};
|
||||
let featured_artists = fed.featured_artists.clone();
|
||||
tracks.push(crate::federation::pending_track(
|
||||
&crate::federation::FedTrack {
|
||||
item_id,
|
||||
owner,
|
||||
own: false,
|
||||
title: fed.title.clone(),
|
||||
artist_names: names,
|
||||
featured_artist_names: featured_artists,
|
||||
year: fed_release.year.or(detail.year),
|
||||
duration_seconds: fed.duration_seconds.map(|seconds| seconds.round() as i64),
|
||||
content_id: fed.content_id.clone(),
|
||||
release_title: Some(detail.title.clone()),
|
||||
track_number: fed.track_number,
|
||||
disc_number: fed.disc_number,
|
||||
},
|
||||
));
|
||||
}
|
||||
tracks.sort_by_key(|track| {
|
||||
(
|
||||
track.disc_number.unwrap_or(1),
|
||||
track.track_number.unwrap_or(i32::MAX),
|
||||
)
|
||||
});
|
||||
tracks
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ArtistCursorAnchor {
|
||||
TopTrack(i64),
|
||||
@@ -1994,7 +2453,13 @@ fn fed_release_slot_availability(
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio content id of a music track (library or federated). Podcast
|
||||
/// episodes have none: they are never content-addressed, resolved by hash
|
||||
/// or fetched from peers.
|
||||
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||
if track.is_podcast() {
|
||||
return None;
|
||||
}
|
||||
track
|
||||
.content_id
|
||||
.as_deref()
|
||||
@@ -2008,12 +2473,24 @@ 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);
|
||||
}
|
||||
format!("local:{}", track.id)
|
||||
track.key().to_string()
|
||||
}
|
||||
+285
-41
@@ -18,8 +18,14 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit";
|
||||
/// owns the Runtime (audio controller, API client). Keeps update() pure.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Effect {
|
||||
/// (Re)start playback of `queue[queue_pos]`.
|
||||
CheckUpdate,
|
||||
InstallUpdate(crate::updater::Update),
|
||||
/// (Re)start playback of `queue[queue_pos]` (a podcast episode resumes
|
||||
/// from its saved position).
|
||||
PlayCurrent,
|
||||
/// Play `queue[queue_pos]`, a podcast episode, from the beginning as an
|
||||
/// explicit "start over".
|
||||
PlayCurrentFromStart,
|
||||
TogglePause,
|
||||
StopPlayback,
|
||||
/// Seek relative to the current position, in seconds.
|
||||
@@ -104,6 +110,58 @@ pub enum Effect {
|
||||
},
|
||||
/// Load qualified listening history without blocking the UI thread.
|
||||
LoadListenHistory,
|
||||
/// Reload the subscribed podcasts with their fetch state.
|
||||
PodcastsReload,
|
||||
/// Load the season groups of a podcast.
|
||||
PodcastLoadSeasons(music_dht::podcast::PodcastId),
|
||||
/// Load one page of episodes; `seq` drops stale results.
|
||||
PodcastLoadEpisodes {
|
||||
seq: u64,
|
||||
query: furumi_library::podcasts::EpisodeQuery,
|
||||
},
|
||||
/// Fetch a feed the user typed and subscribe to it.
|
||||
PodcastSubscribe(String),
|
||||
/// Check feeds for new episodes (`manual`: asked by the user).
|
||||
PodcastRefresh {
|
||||
targets: Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>,
|
||||
manual: bool,
|
||||
},
|
||||
/// Unsubscribe; progress and history are kept.
|
||||
PodcastUnsubscribe {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
title: String,
|
||||
},
|
||||
/// Mark an episode played or unplayed (an explicit progress action).
|
||||
PodcastSetPlayed {
|
||||
episode_id: music_dht::podcast::EpisodeId,
|
||||
played: bool,
|
||||
},
|
||||
/// 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,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
@@ -113,6 +171,30 @@ 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,
|
||||
Action::PlayPause
|
||||
| Action::NextTrack
|
||||
| Action::PrevTrack
|
||||
| Action::SeekForward { .. }
|
||||
| Action::SeekBackward { .. }
|
||||
| Action::VolumeUp
|
||||
| Action::VolumeDown
|
||||
| Action::ToggleShuffle
|
||||
| Action::CycleRepeat
|
||||
)
|
||||
{
|
||||
return update_additional_settings(state, action);
|
||||
}
|
||||
match action {
|
||||
// While the help window is open, quit/back just close it.
|
||||
Action::Quit | Action::Back if state.help_visible => {
|
||||
@@ -334,7 +416,14 @@ 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;
|
||||
}
|
||||
@@ -347,7 +436,11 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
}
|
||||
}
|
||||
if local_tracks.is_empty() && fed_tracks.is_empty() {
|
||||
state.status_message = Some("no track selected".into());
|
||||
state.status_message = Some(if episodes > 0 {
|
||||
PODCAST_NOT_MUSIC.into()
|
||||
} else {
|
||||
"no track selected".into()
|
||||
});
|
||||
None
|
||||
} else {
|
||||
let should_like = local_tracks.iter().any(|track| !state.track_liked(track))
|
||||
@@ -393,6 +486,9 @@ 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()),
|
||||
}
|
||||
@@ -400,12 +496,17 @@ 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 = Some("no track selected".into()),
|
||||
None => {
|
||||
state
|
||||
.status_message
|
||||
.get_or_insert_with(|| "no track selected".into());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -424,7 +525,8 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
}
|
||||
}
|
||||
Action::NewPlaylist => {
|
||||
let for_target = selected_playlist_target(state, false);
|
||||
let for_target =
|
||||
without_podcast_episodes(state, selected_playlist_target(state, false));
|
||||
state.popup = Some(super::state::Popup::NewPlaylist {
|
||||
for_target,
|
||||
input: crate::app::input::LineEdit::default(),
|
||||
@@ -457,6 +559,22 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +589,9 @@ fn current_track_for_info(state: &AppState) -> Option<TrackItem> {
|
||||
queued_at_position
|
||||
.filter(|queued| {
|
||||
super::state::track_key(queued) == current_key
|
||||
|| queued.id == current.id
|
||||
// Podcast episodes share the sentinel id; only their key
|
||||
// identifies them.
|
||||
|| (!current.is_podcast() && !queued.is_podcast() && queued.id == current.id)
|
||||
|| current.is_fed_pending()
|
||||
})
|
||||
.or_else(|| {
|
||||
@@ -524,6 +644,36 @@ fn open_track_info(
|
||||
}
|
||||
}
|
||||
|
||||
/// Status text when a music-only action meets a podcast episode.
|
||||
pub const PODCAST_NOT_MUSIC: &str =
|
||||
"podcast episodes are not music: likes, playlists and library actions do not apply";
|
||||
|
||||
/// Playlists hold music only: drops podcast episodes from a local target
|
||||
/// (and says so when nothing is left).
|
||||
fn without_podcast_episodes(
|
||||
state: &mut AppState,
|
||||
target: Option<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,
|
||||
@@ -611,6 +761,10 @@ fn open_edit_popup(state: &mut AppState) {
|
||||
return;
|
||||
}
|
||||
if let Some(track) = selected_track(state).or_else(|| state.player.current.clone()) {
|
||||
if track.is_podcast() {
|
||||
state.status_message = Some("podcast details come from the RSS feed".into());
|
||||
return;
|
||||
}
|
||||
state.popup = Some(track_edit_popup(&track));
|
||||
return;
|
||||
}
|
||||
@@ -947,11 +1101,10 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
|
||||
_ => None,
|
||||
},
|
||||
GlobalView::Release { id, cursor } => match state.release_views.get(id)? {
|
||||
Loadable::Ready(detail) => Some((
|
||||
TrackSelectionScope::Release(*id),
|
||||
*cursor,
|
||||
detail.tracks.len(),
|
||||
)),
|
||||
Loadable::Ready(detail) => {
|
||||
let tracks = crate::app::state::release_tracks(state, *id, detail);
|
||||
Some((TrackSelectionScope::Release(*id), *cursor, tracks.len()))
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
GlobalView::Search { cursor } => {
|
||||
@@ -999,11 +1152,11 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
|
||||
state.queue_tab.cursor,
|
||||
state.player.queue.len(),
|
||||
)),
|
||||
Tab::Federation | Tab::Logs => None,
|
||||
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, Vec<&TrackItem>)> {
|
||||
fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, Vec<TrackItem>)> {
|
||||
match state.active_tab {
|
||||
Tab::Global => match state.global.stack.last()? {
|
||||
GlobalView::Artist { id, cursor } => match state.artist_views.get(id)? {
|
||||
@@ -1014,14 +1167,14 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
|
||||
Some((
|
||||
TrackSelectionScope::ArtistTop(*id),
|
||||
*cursor,
|
||||
detail.top_tracks.iter().collect(),
|
||||
detail.top_tracks.clone(),
|
||||
))
|
||||
} else {
|
||||
let featured = cursor.checked_sub(tracks + releases)?;
|
||||
(featured < detail.featured_tracks.len()).then_some((
|
||||
TrackSelectionScope::ArtistFeatured(*id),
|
||||
featured,
|
||||
detail.featured_tracks.iter().collect(),
|
||||
detail.featured_tracks.clone(),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1031,7 +1184,7 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
|
||||
Loadable::Ready(detail) => Some((
|
||||
TrackSelectionScope::Release(*id),
|
||||
*cursor,
|
||||
detail.tracks.iter().collect(),
|
||||
crate::app::state::release_tracks(state, *id, detail),
|
||||
)),
|
||||
_ => None,
|
||||
},
|
||||
@@ -1042,15 +1195,18 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
|
||||
Some((
|
||||
TrackSelectionScope::Playlist(opened.id),
|
||||
opened.cursor,
|
||||
playlist_tracks(state, opened.id)?,
|
||||
playlist_tracks(state, opened.id)?
|
||||
.into_iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
Tab::Queue => Some((
|
||||
TrackSelectionScope::Queue,
|
||||
state.queue_tab.cursor,
|
||||
state.player.queue.iter().collect(),
|
||||
state.player.queue.clone(),
|
||||
)),
|
||||
Tab::Federation | Tab::Logs => None,
|
||||
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1086,7 +1242,7 @@ pub fn selected_tracks(state: &AppState) -> Vec<TrackItem> {
|
||||
.unwrap_or_else(|| vec![cursor.min(tracks.len().saturating_sub(1))]);
|
||||
indices
|
||||
.into_iter()
|
||||
.filter_map(|index| tracks.get(index).map(|track| (*track).clone()))
|
||||
.filter_map(|index| tracks.get(index).cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -1297,7 +1453,9 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
|
||||
_ => None,
|
||||
},
|
||||
GlobalView::Release { id, cursor } => match state.release_views.get(id)? {
|
||||
Loadable::Ready(detail) => detail.tracks.get(*cursor).cloned(),
|
||||
Loadable::Ready(detail) => crate::app::state::release_tracks(state, *id, detail)
|
||||
.get(*cursor)
|
||||
.cloned(),
|
||||
_ => None,
|
||||
},
|
||||
GlobalView::Search { cursor } => {
|
||||
@@ -1349,6 +1507,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,
|
||||
}
|
||||
}
|
||||
@@ -1368,8 +1527,14 @@ 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 {
|
||||
state.global.filters.source_mode.includes_network() || !track.is_fed_pending()
|
||||
track.is_podcast()
|
||||
|| state.global.filters.source_mode.includes_network()
|
||||
|| !track.is_fed_pending()
|
||||
}
|
||||
|
||||
/// A *release* under the cursor (artist-view tile/row or a search release).
|
||||
@@ -1405,6 +1570,9 @@ 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();
|
||||
@@ -1438,11 +1606,12 @@ fn open_release_for_track(state: &mut AppState, track: &TrackItem) {
|
||||
}
|
||||
let release_id = track.release_id;
|
||||
let cursor = match state.release_views.get(&release_id) {
|
||||
Some(Loadable::Ready(detail)) => detail
|
||||
.tracks
|
||||
.iter()
|
||||
.position(|t| t.id == track.id)
|
||||
.unwrap_or(0),
|
||||
Some(Loadable::Ready(detail)) => {
|
||||
crate::app::state::release_tracks(state, release_id, detail)
|
||||
.iter()
|
||||
.position(|t| t.id == track.id)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
_ => {
|
||||
state.pending_release_focus = Some((release_id, track.id));
|
||||
0
|
||||
@@ -1896,7 +2065,7 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
|
||||
let Some(Loadable::Ready(detail)) = state.release_views.get(&id) else {
|
||||
return;
|
||||
};
|
||||
let total = detail.tracks.len() as isize;
|
||||
let total = crate::app::state::release_tracks(state, id, detail).len() as isize;
|
||||
if total == 0 {
|
||||
return;
|
||||
}
|
||||
@@ -1908,9 +2077,7 @@ 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.results.as_ref().map_or(0, |r| r.len())
|
||||
+ state.search.fed_artists.len()
|
||||
+ state.search.fed_tracks.len()
|
||||
state.search.total_len()
|
||||
} as isize;
|
||||
if total == 0 {
|
||||
return;
|
||||
@@ -2027,16 +2194,14 @@ fn current_view_len(state: &AppState) -> usize {
|
||||
_ => 0,
|
||||
},
|
||||
Some(GlobalView::Release { id, .. }) => match state.release_views.get(id) {
|
||||
Some(Loadable::Ready(d)) => d.tracks.len(),
|
||||
Some(Loadable::Ready(d)) => crate::app::state::release_tracks(state, *id, d).len(),
|
||||
_ => 0,
|
||||
},
|
||||
Some(GlobalView::Search { .. }) => {
|
||||
if state.search.similarity_source.is_some() {
|
||||
state.search.similarity_len()
|
||||
} else {
|
||||
state.search.results.as_ref().map_or(0, |r| r.len())
|
||||
+ state.search.fed_artists.len()
|
||||
+ state.search.fed_tracks.len()
|
||||
state.search.total_len()
|
||||
}
|
||||
}
|
||||
Some(GlobalView::FedArtist { .. }) => fed_card_len(state),
|
||||
@@ -2321,10 +2486,17 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
|
||||
_ => Outcome::Nothing,
|
||||
},
|
||||
Some(GlobalView::Release { id, cursor }) => match state.release_views.get(&id) {
|
||||
Some(Loadable::Ready(detail)) if !detail.tracks.is_empty() => Outcome::Play {
|
||||
tracks: detail.tracks.clone(),
|
||||
start: cursor.min(detail.tracks.len() - 1),
|
||||
},
|
||||
Some(Loadable::Ready(detail)) => {
|
||||
let tracks = crate::app::state::release_tracks(state, id, detail);
|
||||
if tracks.is_empty() {
|
||||
Outcome::Nothing
|
||||
} else {
|
||||
Outcome::Play {
|
||||
start: cursor.min(tracks.len() - 1),
|
||||
tracks,
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Outcome::Nothing,
|
||||
},
|
||||
Some(GlobalView::Search { cursor }) => {
|
||||
@@ -2465,6 +2637,10 @@ 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| {
|
||||
@@ -2706,6 +2882,7 @@ enum Outcome {
|
||||
start: usize,
|
||||
},
|
||||
OpenFedArtist(String),
|
||||
OpenPodcastCard(usize),
|
||||
OpenFedRelease {
|
||||
name: String,
|
||||
card: crate::federation::FedArtistCard,
|
||||
@@ -2716,7 +2893,8 @@ enum Outcome {
|
||||
}
|
||||
|
||||
/// Enter inside the federated section of the search results: artists open
|
||||
/// their card, tracks queue up like a local track list.
|
||||
/// their card, tracks queue up like a local track list, podcasts open their
|
||||
/// public card.
|
||||
fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
|
||||
let artists = &state.search.fed_artists;
|
||||
if fed_index < artists.len() {
|
||||
@@ -2724,7 +2902,12 @@ fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
|
||||
}
|
||||
let start = fed_index - artists.len();
|
||||
if start >= state.search.fed_tracks.len() {
|
||||
return Outcome::Nothing;
|
||||
let podcast = start - state.search.fed_tracks.len();
|
||||
return if podcast < state.search.fed_podcasts.len() {
|
||||
Outcome::OpenPodcastCard(podcast)
|
||||
} else {
|
||||
Outcome::Nothing
|
||||
};
|
||||
}
|
||||
Outcome::Play {
|
||||
tracks: state
|
||||
@@ -2812,9 +2995,66 @@ fn fed_card_featured_artist_names(track: &crate::federation::FedCardTrack) -> Ve
|
||||
|
||||
/// Enter on Settings: toggle switches, open text inputs, run
|
||||
/// one-shot operations. The heavy lifting happens in perform_effect().
|
||||
fn update_additional_settings(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
if state.help_visible {
|
||||
if matches!(action, Action::Back | Action::Quit | Action::ToggleHelp) {
|
||||
state.help_visible = false;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
let rows = super::state::additional_settings_rows(state);
|
||||
let last = rows.len().saturating_sub(1);
|
||||
let cursor = state.additional_settings_cursor.min(last);
|
||||
state.additional_settings_cursor = match action {
|
||||
Action::Back | Action::Quit => {
|
||||
state.additional_settings_open = false;
|
||||
return None;
|
||||
}
|
||||
Action::MoveUp | Action::PrevTab => cursor.saturating_sub(1),
|
||||
Action::MoveDown | Action::NextTab => (cursor + 1).min(last),
|
||||
Action::PageUp => cursor.saturating_sub(5),
|
||||
Action::PageDown => (cursor + 5).min(last),
|
||||
Action::SelectFirst => 0,
|
||||
Action::SelectLast => last,
|
||||
Action::Select => return select_settings_row(state, *rows.get(cursor)?),
|
||||
Action::ToggleHelp => {
|
||||
state.help_visible = true;
|
||||
cursor
|
||||
}
|
||||
_ => cursor,
|
||||
};
|
||||
None
|
||||
}
|
||||
|
||||
fn federation_select(state: &mut AppState) -> Option<Effect> {
|
||||
select_settings_row(state, *settings_rows(state).get(state.settings_cursor)?)
|
||||
}
|
||||
|
||||
fn select_settings_row(state: &mut AppState, row: super::state::SettingsRow) -> Option<Effect> {
|
||||
use super::state::{FedInputField, FedRow, Popup, SettingsRow, SimilarityRow};
|
||||
match settings_rows(state).get(state.settings_cursor).copied()? {
|
||||
match row {
|
||||
SettingsRow::AdditionalSettings => {
|
||||
state.additional_settings_open = true;
|
||||
None
|
||||
}
|
||||
SettingsRow::CheckUpdate => {
|
||||
if state.updater.busy || state.updater.installed {
|
||||
return None;
|
||||
}
|
||||
state.updater.busy = true;
|
||||
state.updater.available = None;
|
||||
state.updater.message = "Checking GitHub Releases...".into();
|
||||
Some(Effect::CheckUpdate)
|
||||
}
|
||||
SettingsRow::InstallUpdate => {
|
||||
if state.updater.busy || state.updater.installed {
|
||||
return None;
|
||||
}
|
||||
let update = state.updater.available.clone()?;
|
||||
state.updater.busy = true;
|
||||
state.updater.message = "Downloading update...".into();
|
||||
Some(Effect::InstallUpdate(update))
|
||||
}
|
||||
SettingsRow::MusicDirectory => {
|
||||
if state.music_dir_changing {
|
||||
state.status_message = Some("music directory change is already running".into());
|
||||
@@ -3164,6 +3404,7 @@ fn reset_tab(state: &mut AppState, tab: Tab) {
|
||||
state.logs.selected_seq = None;
|
||||
}
|
||||
Tab::Queue => {}
|
||||
Tab::Podcasts => podcasts::close(state),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3171,6 +3412,9 @@ 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;
|
||||
@@ -0,0 +1,135 @@
|
||||
//! 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
File diff suppressed because it is too large.
Load diff
@@ -1,15 +1,27 @@
|
||||
# Default keybindings for furumi.
|
||||
#
|
||||
# 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.
|
||||
# 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.
|
||||
#
|
||||
# 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, logs.
|
||||
# playlists, queue, podcasts, settings, logs.
|
||||
|
||||
version = 2
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "q"
|
||||
@@ -51,6 +63,10 @@ command = { GoToTab = 3 }
|
||||
key_sequence = "5"
|
||||
command = { GoToTab = 4 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "6"
|
||||
command = { GoToTab = 5 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "y"
|
||||
command = "DownloadSelected"
|
||||
@@ -255,3 +271,56 @@ command = "OpenCommandLine"
|
||||
[[keymaps]]
|
||||
key_sequence = "/"
|
||||
command = "OpenSearch"
|
||||
|
||||
# Podcasts tab. Enter opens a podcast, its seasons and episodes, and plays
|
||||
# an episode (continuing where you stopped); Esc goes back. a / shift-a queue
|
||||
# the episode; Shift-D also unsubscribes here.
|
||||
[[keymaps]]
|
||||
key_sequence = "o"
|
||||
command = "AddPodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "/"
|
||||
command = "SearchEpisodes"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-r"
|
||||
command = "RefreshPodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "ctrl-r"
|
||||
command = "RefreshAllPodcasts"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-s"
|
||||
command = "CycleEpisodeSort"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f"
|
||||
command = "CycleEpisodeFilter"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "m"
|
||||
command = "ToggleEpisodePlayed"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-u"
|
||||
command = "UnsubscribePodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "b"
|
||||
command = "RestartEpisode"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-f"
|
||||
command = "TogglePodcastShare"
|
||||
context = "podcasts"
|
||||
+336
-4
@@ -9,6 +9,14 @@ 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)]
|
||||
@@ -20,6 +28,7 @@ pub enum KeyContext {
|
||||
Search,
|
||||
Playlists,
|
||||
Queue,
|
||||
Podcasts,
|
||||
#[serde(rename = "settings", alias = "federation")]
|
||||
Federation,
|
||||
Logs,
|
||||
@@ -33,6 +42,7 @@ impl KeyContext {
|
||||
KeyContext::Search => "search",
|
||||
KeyContext::Playlists => "playlists",
|
||||
KeyContext::Queue => "queue",
|
||||
KeyContext::Podcasts => "podcasts",
|
||||
KeyContext::Federation => "settings",
|
||||
KeyContext::Logs => "logs",
|
||||
}
|
||||
@@ -52,10 +62,18 @@ 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>,
|
||||
}
|
||||
@@ -87,9 +105,15 @@ impl Keymap {
|
||||
{
|
||||
match fs::read_to_string(&path)
|
||||
.map_err(anyhow::Error::from)
|
||||
.and_then(|text| parse_bindings(&text))
|
||||
.and_then(|text| parse_user_keymap(&text))
|
||||
{
|
||||
Ok(user) => merge(&mut bindings, user),
|
||||
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);
|
||||
}
|
||||
Err(err) => {
|
||||
warning = Some(format!(
|
||||
"{} ignored: {err:#}; using default keybindings",
|
||||
@@ -106,6 +130,16 @@ 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 {
|
||||
@@ -275,10 +309,76 @@ 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")?;
|
||||
file.keymaps
|
||||
let bindings = 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 {
|
||||
@@ -287,7 +387,8 @@ fn parse_bindings(text: &str) -> Result<Vec<Binding>> {
|
||||
context: raw.context,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
.collect::<Result<_>>()?;
|
||||
Ok((file.version, bindings))
|
||||
}
|
||||
|
||||
fn parse_sequence(s: &str) -> Result<Vec<KeyCombination>> {
|
||||
@@ -597,6 +698,237 @@ 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);
|
||||
|
||||
@@ -50,6 +50,8 @@ impl Default for SimilaritySettings {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AppSettings {
|
||||
#[serde(default)]
|
||||
pub playback: music_dht::playback::Config,
|
||||
#[serde(default = "default_volume")]
|
||||
pub volume: u8,
|
||||
#[serde(default)]
|
||||
@@ -64,6 +66,7 @@ pub struct AppSettings {
|
||||
impl Default for AppSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
playback: music_dht::playback::Config::default(),
|
||||
volume: default_volume(),
|
||||
library: LibraryFilters::default(),
|
||||
music_dir: default_music_dir(),
|
||||
|
||||
+1371
-577
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1 @@
|
||||
{"type":"hello","group_id":"grp_1","profile":{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},"devices":[{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},{"device_id":"web_b","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5}],"vector":{"tui_a":12,"web_b":4},"ops":[{"op_id":"tui_a:1","origin_device_id":"tui_a","seq":1,"hlc_ms":1700000000001,"payload":{"kind":"track_like_set","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","liked":true,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}}},{"op_id":"tui_a:2","origin_device_id":"tui_a","seq":2,"hlc_ms":1700000000002,"payload":{"kind":"playlist_created","playlist_id":"pl","title":"Mix"}},{"op_id":"tui_a:3","origin_device_id":"tui_a","seq":3,"hlc_ms":1700000000003,"payload":{"kind":"playlist_renamed","playlist_id":"pl","title":"Mix 2"}},{"op_id":"tui_a:4","origin_device_id":"tui_a","seq":4,"hlc_ms":1700000000004,"payload":{"kind":"playlist_track_added","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","position":0,"fed":null}},{"op_id":"tui_a:5","origin_device_id":"tui_a","seq":5,"hlc_ms":1700000000005,"payload":{"kind":"playlist_track_removed","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111"}},{"op_id":"tui_a:6","origin_device_id":"tui_a","seq":6,"hlc_ms":1700000000006,"payload":{"kind":"playlist_deleted","playlist_id":"pl"}},{"op_id":"tui_a:7","origin_device_id":"tui_a","seq":7,"hlc_ms":1700000000007,"payload":{"kind":"device_profile_set","name":"TUI","client_version":"0.3.4","endpoint_ticket":"t","endpoint_id":"e"}},{"op_id":"tui_a:8","origin_device_id":"tui_a","seq":8,"hlc_ms":1700000000008,"payload":{"kind":"device_trusted","target_device_id":"web_b"}},{"op_id":"tui_a:9","origin_device_id":"tui_a","seq":9,"hlc_ms":1700000000009,"payload":{"kind":"device_revoked","target_device_id":"old","target_max_seq_seen":3}},{"op_id":"tui_a:10","origin_device_id":"tui_a","seq":10,"hlc_ms":1700000000010,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"set_state","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","track_number":null,"disc_number":null,"duration_seconds":0.0,"artist_names":[],"featured_artist_names":[],"release_id":0,"release_title":"","release_year":null,"file_path":"","content_id":null,"audio_format":null,"audio_bitrate":null,"audio_sample_rate":null,"audio_bit_depth":null,"file_size_bytes":null,"play_count":0,"fed":null}],"queue_pos":1,"playing":true,"paused":false,"idle_since_ms":null,"position_secs":42.5,"volume":73,"shuffle":true,"repeat":"all"},"seek":true},"authority":{"version":1,"claim":{"counter":3,"issuer":"tui_a","owner":"tui_a"},"sequence":9}}},{"op_id":"tui_a:11","origin_device_id":"tui_a","seq":11,"hlc_ms":1700000000011,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"active_changed","active_device_id":"web_b","active_device_name":"WEB","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","trackLine truncated
|
||||
@@ -0,0 +1,137 @@
|
||||
//! Cross-binary wire test. Run with scripts/test_device_interop.py; the other
|
||||
//! endpoint is compiled from furumusic's real protocol types and player hub.
|
||||
use super::*;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "run scripts/test_device_interop.py to start both player test binaries"]
|
||||
async fn localhost_web_peer() {
|
||||
tokio::time::timeout(Duration::from_secs(30), async {
|
||||
let dir = PathBuf::from(std::env::var("FURUMI_INTEROP_DIR").expect("interop runner"));
|
||||
let address = loop {
|
||||
if let Ok(address) = std::fs::read_to_string(dir.join("web-address")) {
|
||||
break address;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
};
|
||||
let mut stream = tokio::net::TcpStream::connect(address.trim())
|
||||
.await
|
||||
.unwrap();
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
init_schema(&conn).unwrap();
|
||||
let sync = DeviceSync {
|
||||
_identity_lock: None,
|
||||
conn: Arc::new(std::sync::Mutex::new(conn)),
|
||||
library: Arc::new(Library::open(&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();
|
||||
sync.set_event_tx(tx);
|
||||
sync.claim_playback(&id.device_id).unwrap();
|
||||
sync.playback_tick(true, true).unwrap();
|
||||
let mut state: PlaybackStateWire = serde_json::from_value(serde_json::json!({
|
||||
"queue": [{ "id": 7, "title": "Interop track", "duration_seconds": 123.5,
|
||||
"release_id": 1, "release_title": "Interop album", "artist_names": ["Interop artist"],
|
||||
"content_id": "b3:0000000000000000000000000000000000000000000000000000000000000000" }],
|
||||
"queue_pos": 0, "playing": true, "paused": false,
|
||||
"position_secs": 42.5, "volume": 73, "shuffle": true, "repeat": "all"
|
||||
}))
|
||||
.unwrap();
|
||||
for phase in 0..3 {
|
||||
sync.publish_playback(PlaybackSnapshot {
|
||||
device_id: id.device_id.clone(),
|
||||
device_name: "TUI interop".into(),
|
||||
active: true,
|
||||
updated_at_ms: now_ms(),
|
||||
state: state.clone(),
|
||||
coordination: None,
|
||||
});
|
||||
let hello = WireMessage::Hello {
|
||||
group_id: id.group_id.clone(),
|
||||
profile: sync.own_profile("").unwrap(),
|
||||
devices: vec![],
|
||||
vector: BTreeMap::new(),
|
||||
ops: vec![],
|
||||
snapshot: SyncSnapshot::default(),
|
||||
playback: sync.local_playback_snapshot_for(PROTOCOL_VERSION),
|
||||
};
|
||||
let mut bytes = serde_json::to_vec(&hello).unwrap();
|
||||
bytes.push(b'\n');
|
||||
stream.write_all(&bytes).await.unwrap();
|
||||
let response: WireMessage =
|
||||
serde_json::from_slice(
|
||||
&music_dht::device_sync::read_line(&mut stream).await.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let WireMessage::SyncResponse {
|
||||
accepted: true,
|
||||
playback: Some(snapshot),
|
||||
ops,
|
||||
..
|
||||
} = response
|
||||
else {
|
||||
panic!("expected web response")
|
||||
};
|
||||
let web_id = snapshot.device_id.clone();
|
||||
sync.apply_playback_snapshot(&web_id, snapshot).unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
let op = &ops[0];
|
||||
// Production command adapter including durable fencing/deduplication.
|
||||
sync.apply_op(op).unwrap();
|
||||
sync.apply_op(op).unwrap();
|
||||
let mut commands = Vec::new();
|
||||
while let Ok(event) = events.try_recv() {
|
||||
if let AppEvent::PlaybackCommand {
|
||||
command,
|
||||
authority,
|
||||
origin,
|
||||
} = event
|
||||
{
|
||||
assert!(sync.playback_command_is_current(&origin, &authority));
|
||||
commands.push(command);
|
||||
}
|
||||
}
|
||||
assert_eq!(commands.len(), 1, "one event even after duplicate delivery");
|
||||
match commands.pop().unwrap() {
|
||||
PlaybackCommand::ActiveChanged {
|
||||
active_device_id,
|
||||
state: next,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(
|
||||
active_device_id,
|
||||
if phase == 0 {
|
||||
web_id.clone()
|
||||
} else {
|
||||
id.device_id.clone()
|
||||
}
|
||||
);
|
||||
assert_eq!(next.position_secs, 42.5);
|
||||
assert_eq!(next.queue, state.queue);
|
||||
state = next;
|
||||
}
|
||||
PlaybackCommand::SetState { state: next, seek } => {
|
||||
assert_eq!(phase, 2);
|
||||
assert!(seek);
|
||||
assert!(next.paused);
|
||||
assert_eq!(next.position_secs, 87.0);
|
||||
state = next;
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
sync.playback_tick(true, false).unwrap(),
|
||||
Some(if phase == 0 {
|
||||
web_id
|
||||
} else {
|
||||
id.device_id.clone()
|
||||
})
|
||||
);
|
||||
}
|
||||
stream.write_all(b"ok\n").await.unwrap();
|
||||
})
|
||||
.await
|
||||
.expect("web/TUI exchange timed out");
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
+906
-15
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,576 @@
|
||||
//! Golden wire-compatibility tests: the shared `music_dht::device_sync`
|
||||
//! types this client now uses must serialize v3 content byte-for-byte like
|
||||
//! the private copies of furumi-tui 0.3.x (kept verbatim in `legacy`
|
||||
//! below), so 0.3.x clients and furumi-web 0.12.x keep interoperating.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Private wire types of furumi-tui 0.3.x (`src/devices.rs` before sync
|
||||
/// v4), copied verbatim apart from visibility, `PartialEq` and doc comments.
|
||||
#[allow(dead_code)]
|
||||
pub(super) mod legacy {
|
||||
use music_dht::device_sync::ListenEvent;
|
||||
use music_dht::playback::CommandStamp;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct InviteWire {
|
||||
pub v: u16,
|
||||
#[serde(rename = "t")]
|
||||
pub ticket: String,
|
||||
#[serde(rename = "d")]
|
||||
pub device_id: String,
|
||||
#[serde(rename = "i")]
|
||||
pub invite_id: String,
|
||||
#[serde(rename = "s")]
|
||||
pub secret: String,
|
||||
#[serde(rename = "e")]
|
||||
pub expires_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DeviceProfileWire {
|
||||
pub device_id: String,
|
||||
pub name: String,
|
||||
pub client_version: String,
|
||||
pub protocol_version: u16,
|
||||
pub endpoint_id: String,
|
||||
pub endpoint_ticket: String,
|
||||
#[serde(default)]
|
||||
pub revoked: bool,
|
||||
#[serde(default)]
|
||||
pub revoke_cutoff_seq: Option<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 { .. }
|
||||
));
|
||||
}
|
||||
@@ -10,6 +10,7 @@ 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);
|
||||
@@ -30,8 +31,10 @@ pub struct NewerProtocol {
|
||||
}
|
||||
|
||||
impl ProtocolVersions {
|
||||
pub fn snapshot(observed: &ObservedVersions) -> Self {
|
||||
let local = local_manifest().protocols;
|
||||
/// `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;
|
||||
let observed_versions = lock(&observed.versions).clone();
|
||||
let newer = observed_versions
|
||||
.iter()
|
||||
@@ -59,27 +62,35 @@ pub struct ObservedVersions {
|
||||
peers: Mutex<BTreeMap<String, String>>,
|
||||
}
|
||||
|
||||
fn local_manifest() -> CapabilityManifest {
|
||||
CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION"))
|
||||
.with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION)
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn serve(mut acceptor: StreamAcceptor) {
|
||||
pub async fn serve(mut acceptor: StreamAcceptor, podcast_dht: bool) {
|
||||
while let Some(stream) = acceptor.accept().await {
|
||||
tokio::spawn(async move {
|
||||
if let Err(error) = serve_one(stream).await {
|
||||
if let Err(error) = serve_one(stream, podcast_dht).await {
|
||||
tracing::debug!("capability stream failed: {error:#}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_one(mut stream: ByteStream) -> Result<()> {
|
||||
async fn serve_one(mut stream: ByteStream, podcast_dht: bool) -> Result<()> {
|
||||
let response = match read_message(&mut stream).await? {
|
||||
CapabilityMessage::Get {
|
||||
version: CAPABILITIES_PROTOCOL_VERSION,
|
||||
} => CapabilityMessage::Manifest {
|
||||
manifest: local_manifest(),
|
||||
manifest: local_manifest(podcast_dht),
|
||||
},
|
||||
CapabilityMessage::Get { version } => CapabilityMessage::Error {
|
||||
message: format!("unsupported capability protocol {version}"),
|
||||
@@ -94,7 +105,14 @@ async fn serve_one(mut stream: ByteStream) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn probe_loop(service: Arc<MusicDhtService>, observed: Arc<ObservedVersions>) {
|
||||
/// 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>>,
|
||||
) {
|
||||
let mut interval = tokio::time::interval(PROBE_INTERVAL);
|
||||
loop {
|
||||
interval.tick().await;
|
||||
@@ -109,7 +127,8 @@ pub async fn probe_loop(service: Arc<MusicDhtService>, observed: Arc<ObservedVer
|
||||
)
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
for peer in peers {
|
||||
if let Err(error) = probe_peer(&service, peer, &observed).await {
|
||||
if let Err(error) = probe_peer(&service, peer, &observed, podcast_dht.as_deref()).await
|
||||
{
|
||||
tracing::trace!(%peer, "peer capability probe unavailable: {error:#}");
|
||||
}
|
||||
}
|
||||
@@ -120,6 +139,7 @@ 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(
|
||||
@@ -137,6 +157,11 @@ 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 {
|
||||
@@ -162,7 +187,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn local_manifest_lists_every_player_protocol() {
|
||||
let manifest = local_manifest();
|
||||
let manifest = local_manifest(true);
|
||||
for id in [
|
||||
"federation_net",
|
||||
"ticket",
|
||||
@@ -174,10 +199,31 @@ 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]
|
||||
@@ -185,7 +231,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);
|
||||
let snapshot = ProtocolVersions::snapshot(&observed, true);
|
||||
assert_eq!(snapshot.newer.len(), 1);
|
||||
assert_eq!(snapshot.newer[0].id, "music_dht");
|
||||
}
|
||||
|
||||
+294
-44
@@ -21,10 +21,11 @@ use std::collections::{HashMap, VecDeque};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicI64, Ordering};
|
||||
use std::time::Duration;
|
||||
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::{
|
||||
@@ -44,8 +45,17 @@ 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
|
||||
/// full restart so all application-owned protocol acceptors are recreated.
|
||||
const SUPERVISOR_INTERVAL: Duration = Duration::from_secs(15);
|
||||
/// Prevents repeated restarts during a prolonged external network outage.
|
||||
const RECOVERY_COOLDOWN: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
/// How many times a share-link content lookup is retried before the label
|
||||
/// fallback kicks in.
|
||||
@@ -370,6 +380,12 @@ pub struct FedStatus {
|
||||
pub endpoint_id: String,
|
||||
pub dht_node_id: String,
|
||||
pub connected_peers: Vec<String>,
|
||||
pub network_health: String,
|
||||
pub rendezvous_failures: u32,
|
||||
pub peer_dial_failures: u32,
|
||||
pub rendezvous_restarts: u64,
|
||||
pub recovery_count: u64,
|
||||
pub last_rendezvous_error: Option<String>,
|
||||
pub known_contacts: usize,
|
||||
pub stored_dht_records: Option<usize>,
|
||||
pub stored_dht_bytes: Option<u64>,
|
||||
@@ -401,6 +417,9 @@ 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<()>>,
|
||||
@@ -419,10 +438,17 @@ pub struct Federation {
|
||||
metadata_cache: std::sync::Mutex<std::collections::HashMap<String, CachedTrackMetadata>>,
|
||||
settings: std::sync::Mutex<FedSettings>,
|
||||
running: tokio::sync::Mutex<Option<Running>>,
|
||||
supervisor_task: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
|
||||
supervisor_shutdown: tokio::sync::Notify,
|
||||
shutting_down: AtomicBool,
|
||||
recovery_count: AtomicU64,
|
||||
last_sync: std::sync::Mutex<Option<String>>,
|
||||
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)]
|
||||
@@ -549,17 +575,41 @@ impl Federation {
|
||||
metadata_cache: std::sync::Mutex::new(Default::default()),
|
||||
settings: std::sync::Mutex::new(load_settings()),
|
||||
running: tokio::sync::Mutex::new(None),
|
||||
supervisor_task: std::sync::Mutex::new(None),
|
||||
supervisor_shutdown: tokio::sync::Notify::new(),
|
||||
shutting_down: AtomicBool::new(false),
|
||||
recovery_count: AtomicU64::new(0),
|
||||
last_sync: std::sync::Mutex::new(None),
|
||||
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()
|
||||
}
|
||||
|
||||
/// Starts the application-level federation supervisor once.
|
||||
pub fn start_supervisor(self: &Arc<Self>) {
|
||||
let mut task = lock(&self.supervisor_task);
|
||||
if task.is_some() || self.shutting_down.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
let federation = Arc::clone(self);
|
||||
*task = Some(tokio::spawn(async move {
|
||||
federation.supervisor_loop().await;
|
||||
}));
|
||||
}
|
||||
|
||||
pub fn media_dir(&self) -> PathBuf {
|
||||
lock(&self.media_dir).clone()
|
||||
}
|
||||
@@ -665,12 +715,44 @@ impl Federation {
|
||||
network_id: NetworkId,
|
||||
network_name: String,
|
||||
) -> Result<()> {
|
||||
self.start_with_network_id_mode(network_id, network_name, false)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
async fn start_with_network_id_mode(
|
||||
self: &Arc<Self>,
|
||||
network_id: NetworkId,
|
||||
network_name: String,
|
||||
recovery_only: bool,
|
||||
) -> Result<bool> {
|
||||
let mut guard = self.running.lock().await;
|
||||
if recovery_only {
|
||||
let current = self.settings();
|
||||
if !current.enabled
|
||||
|| current.network_id.trim() != network_name
|
||||
|| NetworkId::from_name(current.network_id.trim()) != network_id
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
if let Some(running) = guard.as_ref() {
|
||||
if running.network_id == network_id {
|
||||
return Ok(());
|
||||
if !recovery_only || !running.service.network_health().restart_recommended {
|
||||
return Ok(false);
|
||||
}
|
||||
tracing::warn!(
|
||||
network = %network_name,
|
||||
health = %running.service.network_health().state,
|
||||
"restarting degraded federation service"
|
||||
);
|
||||
} 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");
|
||||
}
|
||||
std::fs::create_dir_all(&self.data_dir)
|
||||
.with_context(|| format!("creating {}", self.data_dir.display()))?;
|
||||
@@ -694,6 +776,8 @@ 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()
|
||||
@@ -726,6 +810,18 @@ 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 {
|
||||
@@ -799,47 +895,104 @@ 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));
|
||||
let capabilities_serve_task = tokio::spawn(capabilities::serve(
|
||||
capabilities_acceptor,
|
||||
podcast_dht.is_some(),
|
||||
));
|
||||
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: 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,
|
||||
],
|
||||
tasks,
|
||||
});
|
||||
self.set_error(None);
|
||||
Ok(())
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn stop(&self) {
|
||||
let mut guard = self.running.lock().await;
|
||||
self.podcast_dht.send_replace(None);
|
||||
stop_running(guard.take()).await;
|
||||
}
|
||||
|
||||
pub async fn shutdown(&self) {
|
||||
self.shutting_down.store(true, Ordering::SeqCst);
|
||||
self.supervisor_shutdown.notify_one();
|
||||
let supervisor = lock(&self.supervisor_task).take();
|
||||
if let Some(supervisor) = supervisor {
|
||||
let _ = supervisor.await;
|
||||
}
|
||||
self.stop().await;
|
||||
}
|
||||
|
||||
async fn supervisor_loop(self: Arc<Self>) {
|
||||
let mut interval = tokio::time::interval(SUPERVISOR_INTERVAL);
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
interval.tick().await;
|
||||
let mut last_attempt = None;
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = self.supervisor_shutdown.notified() => return,
|
||||
_ = interval.tick() => {}
|
||||
}
|
||||
if self.shutting_down.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
if last_attempt.is_some_and(|attempt: Instant| attempt.elapsed() < RECOVERY_COOLDOWN) {
|
||||
continue;
|
||||
}
|
||||
match self.recover_if_needed().await {
|
||||
Ok(false) => {}
|
||||
Ok(true) => {
|
||||
last_attempt = Some(Instant::now());
|
||||
self.recovery_count.fetch_add(1, Ordering::Relaxed);
|
||||
self.spawn_sync_soon().await;
|
||||
}
|
||||
Err(err) => {
|
||||
last_attempt = Some(Instant::now());
|
||||
tracing::error!(error = %err, "federation recovery failed");
|
||||
self.set_error(Some(format!("network recovery failed: {err}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn recover_if_needed(self: &Arc<Self>) -> Result<bool> {
|
||||
let settings = self.settings();
|
||||
if !settings.enabled || settings.network_id.trim().is_empty() {
|
||||
return Ok(false);
|
||||
}
|
||||
let network_name = settings.network_id.trim().to_string();
|
||||
self.start_with_network_id_mode(NetworkId::from_name(&network_name), network_name, true)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn service(&self) -> Result<Arc<MusicDhtService>> {
|
||||
self.running
|
||||
.lock()
|
||||
@@ -975,13 +1128,20 @@ impl Federation {
|
||||
let guard = self.running.lock().await;
|
||||
let mut status = FedStatus {
|
||||
network: settings.network_id,
|
||||
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),
|
||||
protocols: ProtocolVersions::snapshot(
|
||||
&self.observed_protocols,
|
||||
guard
|
||||
.as_ref()
|
||||
.is_some_and(|running| running.podcast_dht.is_some()),
|
||||
),
|
||||
..FedStatus::default()
|
||||
};
|
||||
if let Some(running) = guard.as_ref() {
|
||||
let service = &running.service;
|
||||
let health = service.network_health();
|
||||
status.running = true;
|
||||
status.network = running.network_name.clone();
|
||||
status.endpoint_id = service.endpoint_id().to_string();
|
||||
@@ -991,6 +1151,11 @@ impl Federation {
|
||||
.iter()
|
||||
.map(|p| p.to_string())
|
||||
.collect();
|
||||
status.network_health = health.state.to_string();
|
||||
status.rendezvous_failures = health.consecutive_rendezvous_failures;
|
||||
status.peer_dial_failures = health.consecutive_peer_dial_failures;
|
||||
status.rendezvous_restarts = health.rendezvous_restarts;
|
||||
status.last_rendezvous_error = health.last_rendezvous_error;
|
||||
status.known_contacts = service.known_peers().len();
|
||||
status.stored_dht_records = service.dht_record_count().await.ok();
|
||||
status.stored_dht_bytes =
|
||||
@@ -1097,6 +1262,10 @@ 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);
|
||||
@@ -1192,6 +1361,24 @@ 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(
|
||||
@@ -1369,31 +1556,38 @@ impl Federation {
|
||||
let own = service.endpoint_id();
|
||||
let own_hex = own.to_string();
|
||||
let normalized = music_dht::normalize_name(name);
|
||||
let outcome = service
|
||||
.search_network(name)
|
||||
.await
|
||||
.map_err(|err| anyhow::anyhow!("federated search failed: {err}"))?;
|
||||
// A partial/unavailable DHT index must not prevent direct catalog
|
||||
// requests to peers we already know. Keep discovery bounded so an
|
||||
// open card cannot remain in Loading indefinitely.
|
||||
let network_results = match tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
service.search_network(name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(outcome)) => outcome.network_results,
|
||||
Ok(Err(error)) => {
|
||||
tracing::debug!(artist = %name, %error, "artist discovery failed; trying known peers");
|
||||
Vec::new()
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::debug!(artist = %name, "artist discovery timed out; trying known peers");
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
let cached_metadata = self.cached_metadata_snapshot();
|
||||
let has_cached_artist = cached_metadata
|
||||
.iter()
|
||||
.any(|cached| cached_has_artist(cached, &normalized));
|
||||
let owners: std::collections::HashSet<EndpointId> = outcome
|
||||
.network_results
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|
||||
|| item
|
||||
.artist_names
|
||||
.iter()
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
|| item
|
||||
.featured_artist_names
|
||||
.iter()
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
})
|
||||
.map(|item| item.owner)
|
||||
.filter(|owner| *owner != own)
|
||||
.collect();
|
||||
let owners = artist_catalog_owners(
|
||||
&network_results,
|
||||
&normalized,
|
||||
own,
|
||||
service
|
||||
.connected_peers()
|
||||
.into_iter()
|
||||
.chain(service.known_peers().into_iter().map(|peer| peer.peer_id)),
|
||||
);
|
||||
|
||||
let local_library = Arc::clone(&self.library);
|
||||
let local_name = name.to_string();
|
||||
@@ -1448,7 +1642,7 @@ impl Federation {
|
||||
}
|
||||
}
|
||||
let mut card = catalog::merge_catalogs(name, catalogs);
|
||||
add_dht_appearance_hits(&mut card, &outcome.network_results, &normalized, name);
|
||||
add_dht_appearance_hits(&mut card, &network_results, &normalized, name);
|
||||
add_cached_appearance_hits(&mut card, &cached_metadata, &normalized, name);
|
||||
anyhow::ensure!(
|
||||
!card.releases.is_empty() || !card.appears_on.is_empty(),
|
||||
@@ -2209,6 +2403,7 @@ fn metadata_preview_track(
|
||||
.or(base.file_size_bytes),
|
||||
play_count: base.play_count,
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2289,6 +2484,34 @@ fn cached_has_artist(cached: &CachedTrackMetadata, normalized_artist: &str) -> b
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized_artist)
|
||||
}
|
||||
|
||||
/// Prefer advertised owners, then ask a bounded set of known peers directly.
|
||||
/// Catalog lookup is useful even before those peers' DHT records arrive.
|
||||
fn artist_catalog_owners(
|
||||
items: &[LibraryItem],
|
||||
normalized: &str,
|
||||
own: EndpointId,
|
||||
known: impl IntoIterator<Item = EndpointId>,
|
||||
) -> Vec<EndpointId> {
|
||||
const MAX_CATALOG_PEERS: usize = 32;
|
||||
let advertised = items
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|
||||
|| item
|
||||
.artist_names
|
||||
.iter()
|
||||
.chain(&item.featured_artist_names)
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
})
|
||||
.map(|item| item.owner);
|
||||
let mut seen = std::collections::HashSet::from([own]);
|
||||
advertised
|
||||
.chain(known)
|
||||
.filter(|owner| seen.insert(*owner))
|
||||
.take(MAX_CATALOG_PEERS)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn rank_fed_search_results(
|
||||
artists: &mut [FedArtistHit],
|
||||
tracks: &mut [FedTrack],
|
||||
@@ -2651,6 +2874,31 @@ 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 {
|
||||
@@ -2796,6 +3044,7 @@ pub fn pending_track(fed: &FedTrack) -> TrackItem {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2868,6 +3117,7 @@ fn ephemeral_track(
|
||||
// Keep the federation reference: the cached track can still be
|
||||
// liked, re-downloaded and marked as federated in the lists.
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,3 +93,77 @@ fn federation_search_ranks_exact_names_first() {
|
||||
assert_eq!(artists[0].name, "Ежемесячные");
|
||||
assert_eq!(tracks[0].title, "Ежемесячные");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_catalog_discovery_uses_known_peers_without_dht_hits() {
|
||||
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
|
||||
let peer = test_owner();
|
||||
assert_eq!(
|
||||
artist_catalog_owners(&[], "artist", own, [own, peer, peer]),
|
||||
vec![peer]
|
||||
);
|
||||
// Unrelated records must not send the card request to their owners.
|
||||
assert!(artist_catalog_owners(&[dht_track(&["Other"], &[])], "artist", own, []).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() {
|
||||
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
|
||||
let peer = test_owner();
|
||||
let known: Vec<_> = (10..60)
|
||||
.map(|seed| music_dht::SecretKey::from_bytes(&[seed; 32]).public())
|
||||
.collect();
|
||||
for item in [
|
||||
dht_track(&["Artist"], &[]),
|
||||
dht_track(&["Host"], &["Artist"]),
|
||||
] {
|
||||
let owners = artist_catalog_owners(
|
||||
&[item],
|
||||
"artist",
|
||||
own,
|
||||
known.iter().copied().chain([own, peer]),
|
||||
);
|
||||
assert_eq!(owners.len(), 32);
|
||||
assert_eq!(owners[0], peer);
|
||||
assert!(!owners.contains(&own));
|
||||
assert_eq!(
|
||||
owners
|
||||
.iter()
|
||||
.collect::<std::collections::HashSet<_>>()
|
||||
.len(),
|
||||
owners.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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();
|
||||
}
|
||||
+52
-11
@@ -225,10 +225,16 @@ impl JamManager {
|
||||
JamStatus::default()
|
||||
}
|
||||
|
||||
/// Publishes the host's playback to participants. Podcast episodes are
|
||||
/// private: participants get opaque placeholders (`redact_for_jam`),
|
||||
/// never their titles, feeds or URLs.
|
||||
pub fn publish_host_playback(&self, snapshot: PlaybackSnapshot) {
|
||||
let mut state = lock(&self.state);
|
||||
if state.host.invite.is_some() {
|
||||
state.host.playback = Some(snapshot);
|
||||
state.host.playback = Some(PlaybackSnapshot {
|
||||
state: music_dht::device_sync::redact_for_jam(snapshot.state),
|
||||
..snapshot
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,7 +380,15 @@ async fn serve_one(mut stream: ByteStream, manager: Arc<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());
|
||||
let _ = manager.event_tx.send(AppEvent::JamCommand(command.command));
|
||||
// Participants cannot add podcast episodes to the
|
||||
// host queue; placeholders are mapped back by the
|
||||
// host (`restore_jam_private_items`).
|
||||
if music_dht::device_sync::jam_command_allowed(&command.command) {
|
||||
let _ =
|
||||
manager.event_tx.send(AppEvent::JamCommand(command.command));
|
||||
} else {
|
||||
tracing::info!("ignored a Jam command that adds podcast episodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
while state.host.seen_order.len() > 4096 {
|
||||
@@ -656,8 +670,14 @@ mod tests {
|
||||
.join(&invite, "dev_guest".into(), "Guest".into())
|
||||
.unwrap();
|
||||
|
||||
// The host plays a private podcast episode.
|
||||
let episode = crate::app::podcast_fixtures::episode_track(1)
|
||||
.podcast
|
||||
.unwrap();
|
||||
let playback_state = crate::devices::PlaybackStateWire {
|
||||
queue: Vec::new(),
|
||||
queue: vec![crate::devices::PlaybackTrack::podcast_episode(
|
||||
episode.clone(),
|
||||
)],
|
||||
queue_pos: 0,
|
||||
playing: false,
|
||||
paused: false,
|
||||
@@ -666,8 +686,10 @@ mod tests {
|
||||
volume: 73,
|
||||
shuffle: false,
|
||||
repeat: crate::devices::PlaybackRepeat::Off,
|
||||
playback_rate: None,
|
||||
};
|
||||
host.publish_host_playback(PlaybackSnapshot {
|
||||
coordination: None,
|
||||
device_id: "dev_host".into(),
|
||||
device_name: "Host".into(),
|
||||
active: true,
|
||||
@@ -689,12 +711,27 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(playback.device_id, "dev_host");
|
||||
assert_eq!(playback.state.volume, 73);
|
||||
// Guests get an opaque placeholder: no title, feed or URL.
|
||||
assert!(playback.state.queue[0].is_private_placeholder());
|
||||
let seen = serde_json::to_string(&playback).unwrap();
|
||||
assert!(!seen.contains(&episode.audio_url) && !seen.contains(&episode.episode_title));
|
||||
|
||||
// A guest cannot add an episode to the host's queue; its ordinary
|
||||
// command (with the placeholder) still arrives.
|
||||
guest
|
||||
.submit_command(PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire {
|
||||
queue: vec![crate::devices::PlaybackTrack::podcast_episode(episode)],
|
||||
..playback_state.clone()
|
||||
},
|
||||
seek: false,
|
||||
})
|
||||
.unwrap();
|
||||
guest
|
||||
.submit_command(PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire {
|
||||
paused: true,
|
||||
..playback_state
|
||||
..playback.state
|
||||
},
|
||||
seek: false,
|
||||
})
|
||||
@@ -709,13 +746,17 @@ mod tests {
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
command,
|
||||
PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire { paused: true, .. },
|
||||
seek: false
|
||||
}
|
||||
));
|
||||
let PlaybackCommand::SetState {
|
||||
state: received,
|
||||
seek: false,
|
||||
} = command
|
||||
else {
|
||||
panic!("expected the guest's set_state");
|
||||
};
|
||||
// The command that adds an episode was sent first; had it been
|
||||
// forwarded, it would have arrived first.
|
||||
assert!(received.paused);
|
||||
assert!(received.queue[0].is_private_placeholder());
|
||||
|
||||
server.abort();
|
||||
host_drain.abort();
|
||||
|
||||
+112
-17
@@ -7,11 +7,13 @@ mod jam;
|
||||
mod library;
|
||||
mod media;
|
||||
mod player;
|
||||
mod podcasts;
|
||||
mod share;
|
||||
mod similarity;
|
||||
mod status;
|
||||
mod streaming;
|
||||
mod ui;
|
||||
mod updater;
|
||||
mod visualizer;
|
||||
|
||||
use std::io;
|
||||
@@ -60,9 +62,13 @@ fn main() -> Result<()> {
|
||||
if let Err(err) = config::logging::init() {
|
||||
startup_warning = Some(format!("logging disabled: {err:#}"));
|
||||
}
|
||||
capture_stderr();
|
||||
// Restore stderr before main returns: Rust prints a returned error only
|
||||
// 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();
|
||||
let startup_warning = keymap_warning.or(startup_warning);
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
@@ -131,25 +137,46 @@ fn run_app(
|
||||
/// every line into tracing — it lands in the Logs tab and the log file
|
||||
/// instead of the screen.
|
||||
#[cfg(unix)]
|
||||
fn capture_stderr() {
|
||||
struct StderrCapture {
|
||||
original: std::os::fd::RawFd,
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl Drop for StderrCapture {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: original is our owned duplicate, kept open for this scope.
|
||||
unsafe {
|
||||
libc::dup2(self.original, libc::STDERR_FILENO);
|
||||
libc::close(self.original);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn capture_stderr() -> Option<StderrCapture> {
|
||||
use std::io::BufRead as _;
|
||||
use std::os::fd::FromRawFd as _;
|
||||
|
||||
let mut fds = [0i32; 2];
|
||||
// SAFETY: plain pipe/dup2 syscalls on freshly created fds.
|
||||
unsafe {
|
||||
let original = libc::dup(libc::STDERR_FILENO);
|
||||
if original == -1 {
|
||||
return None;
|
||||
}
|
||||
let capture = StderrCapture { original };
|
||||
if libc::pipe(fds.as_mut_ptr()) != 0 {
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
let [read_fd, write_fd] = fds;
|
||||
if libc::dup2(write_fd, libc::STDERR_FILENO) == -1 {
|
||||
libc::close(read_fd);
|
||||
libc::close(write_fd);
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
libc::close(write_fd);
|
||||
let reader = std::fs::File::from_raw_fd(read_fd);
|
||||
std::thread::Builder::new()
|
||||
let reader_thread = std::thread::Builder::new()
|
||||
.name("stderr".to_string())
|
||||
.spawn(move || {
|
||||
for line in std::io::BufReader::new(reader).lines() {
|
||||
@@ -158,33 +185,65 @@ fn capture_stderr() {
|
||||
tracing::warn!(target: "stderr", "{line}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
});
|
||||
if reader_thread.is_err() {
|
||||
return None;
|
||||
}
|
||||
Some(capture)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn capture_stderr() {
|
||||
struct StderrCapture {
|
||||
original: windows_sys::Win32::Foundation::HANDLE,
|
||||
writer: windows_sys::Win32::Foundation::HANDLE,
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Drop for StderrCapture {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: original is borrowed from the process; writer is owned by
|
||||
// this guard. Restoring the original also preserves shell redirection.
|
||||
unsafe {
|
||||
windows_sys::Win32::System::Console::SetStdHandle(
|
||||
windows_sys::Win32::System::Console::STD_ERROR_HANDLE,
|
||||
self.original,
|
||||
);
|
||||
windows_sys::Win32::Foundation::CloseHandle(self.writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn capture_stderr() -> Option<StderrCapture> {
|
||||
use std::io::BufRead as _;
|
||||
use std::os::windows::io::FromRawHandle as _;
|
||||
|
||||
use windows_sys::Win32::Foundation::CloseHandle;
|
||||
use windows_sys::Win32::System::Console::{STD_ERROR_HANDLE, SetStdHandle};
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
|
||||
use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE, SetStdHandle};
|
||||
use windows_sys::Win32::System::Pipes::CreatePipe;
|
||||
|
||||
unsafe {
|
||||
let original = GetStdHandle(STD_ERROR_HANDLE);
|
||||
if original.is_null() || original == INVALID_HANDLE_VALUE {
|
||||
return None;
|
||||
}
|
||||
let mut read = core::ptr::null_mut();
|
||||
let mut write = core::ptr::null_mut();
|
||||
if CreatePipe(&mut read, &mut write, core::ptr::null(), 0) == 0 {
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
if SetStdHandle(STD_ERROR_HANDLE, write) == 0 {
|
||||
CloseHandle(read);
|
||||
CloseHandle(write);
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
let capture = StderrCapture {
|
||||
original,
|
||||
writer: write,
|
||||
};
|
||||
let reader = std::fs::File::from_raw_handle(read);
|
||||
std::thread::Builder::new()
|
||||
let reader_thread = std::thread::Builder::new()
|
||||
.name("stderr".to_string())
|
||||
.spawn(move || {
|
||||
for line in std::io::BufReader::new(reader).lines() {
|
||||
@@ -193,13 +252,49 @@ fn capture_stderr() {
|
||||
tracing::warn!(target: "stderr", "{line}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
});
|
||||
if reader_thread.is_err() {
|
||||
return None;
|
||||
}
|
||||
Some(capture)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn capture_stderr() {}
|
||||
fn capture_stderr() -> Option<()> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod stderr_tests {
|
||||
#[test]
|
||||
fn error_output_is_restored_after_capture() {
|
||||
const CHILD: &str = "FURUMI_STDERR_CAPTURE_TEST";
|
||||
if std::env::var_os(CHILD).is_some() {
|
||||
{
|
||||
let _capture = super::capture_stderr().expect("capture stderr");
|
||||
}
|
||||
eprintln!("furumi-test: visible error after terminal shutdown");
|
||||
return;
|
||||
}
|
||||
// Run in another process: redirecting global stderr inside a parallel
|
||||
// test suite would interfere with unrelated tests and panic reporting.
|
||||
let output = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args([
|
||||
"--exact",
|
||||
"stderr_tests::error_output_is_restored_after_capture",
|
||||
"--nocapture",
|
||||
])
|
||||
.env(CHILD, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
assert!(
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
.contains("furumi-test: visible error after terminal shutdown")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Kitty keyboard protocol, where supported, disambiguates Esc from alt-keys
|
||||
/// and modifier combos. The flags are popped on exit and on panic — leaving
|
||||
|
||||
+354
-47
@@ -41,6 +41,14 @@ pub enum PlayerEvent {
|
||||
has_next: bool,
|
||||
},
|
||||
Failed(String),
|
||||
/// A seek was handled for the source started under `generation`
|
||||
/// ([`Controller::source_generation`]); `result` says whether the source
|
||||
/// actually moved to `position`.
|
||||
Seeked {
|
||||
generation: u64,
|
||||
position: Duration,
|
||||
result: Result<(), String>,
|
||||
},
|
||||
}
|
||||
|
||||
enum Command {
|
||||
@@ -51,6 +59,18 @@ enum Command {
|
||||
seekable: bool,
|
||||
volume: f32,
|
||||
},
|
||||
/// Play a source that was already opened, probed and positioned off
|
||||
/// this thread (remote podcast streams, whose probing and seeking wait
|
||||
/// on the network). Dropped unless `generation` is still current. It
|
||||
/// starts paused if the latest pause/resume says so
|
||||
/// ([`Controller::expect_source`]), not as it was when requested.
|
||||
PlaySource {
|
||||
source: DecodedSource,
|
||||
volume: f32,
|
||||
/// Where `source` was positioned before it was handed over.
|
||||
start: Duration,
|
||||
generation: u64,
|
||||
},
|
||||
/// Append the next track behind the current one without interrupting
|
||||
/// playback — rodio switches sources back to back (gapless-ish).
|
||||
Enqueue {
|
||||
@@ -90,10 +110,71 @@ impl Shared {
|
||||
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 {
|
||||
/// Revoke the output at the audio thread boundary, including work queued
|
||||
/// by background decoders before an ownership transfer.
|
||||
pub fn set_playback_allowed(&self, allowed: bool) {
|
||||
if self.playback_allowed.swap(allowed, Ordering::AcqRel) && !allowed {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
@@ -104,6 +185,8 @@ 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,
|
||||
@@ -118,14 +201,17 @@ impl Controller {
|
||||
}
|
||||
|
||||
pub fn pause(&self) {
|
||||
self.start_paused.store(true, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Pause);
|
||||
}
|
||||
|
||||
pub fn resume(&self) {
|
||||
self.start_paused.store(false, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Resume);
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
self.supersede();
|
||||
let _ = self.tx.send(Command::Stop);
|
||||
}
|
||||
|
||||
@@ -142,11 +228,40 @@ pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Controller {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let shared = Arc::new(Shared::default());
|
||||
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, on_event))
|
||||
.spawn(move || {
|
||||
run(
|
||||
rx,
|
||||
thread_shared,
|
||||
thread_allowed,
|
||||
Generations {
|
||||
current: thread_generation,
|
||||
start_paused: thread_start_paused,
|
||||
},
|
||||
on_event,
|
||||
)
|
||||
})
|
||||
.expect("spawning the audio thread cannot fail");
|
||||
Controller { tx, shared }
|
||||
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 {
|
||||
@@ -155,34 +270,103 @@ struct Output {
|
||||
player: Player,
|
||||
}
|
||||
|
||||
fn run(rx: Receiver<Command>, shared: Arc<Shared>, on_event: impl Fn(PlayerEvent)) {
|
||||
/// 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 track_loaded = false;
|
||||
let mut playing = Playing {
|
||||
track_loaded: false,
|
||||
base: Duration::ZERO,
|
||||
generation: generation.load(Ordering::SeqCst),
|
||||
};
|
||||
let mut last_len = 0usize;
|
||||
|
||||
loop {
|
||||
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||
Ok(command) => {
|
||||
if !playback_allowed.load(Ordering::Acquire)
|
||||
&& matches!(
|
||||
command,
|
||||
Command::Play { .. }
|
||||
| Command::PlaySource { .. }
|
||||
| Command::Enqueue { .. }
|
||||
| Command::Resume
|
||||
)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if let Command::PlaySource {
|
||||
generation: requested,
|
||||
..
|
||||
} = &command
|
||||
&& *requested != generation.load(Ordering::SeqCst)
|
||||
{
|
||||
// Superseded while it was being prepared.
|
||||
continue;
|
||||
}
|
||||
let started_under = match &command {
|
||||
Command::PlaySource {
|
||||
generation: requested,
|
||||
..
|
||||
} => Some(*requested),
|
||||
Command::Play { .. } | Command::Stop => Some(generation.load(Ordering::SeqCst)),
|
||||
_ => None,
|
||||
};
|
||||
// Commands change the source queue legitimately; resync the
|
||||
// length so the next tick doesn't read it as a track ending.
|
||||
handle(command, &shared, &mut output, &mut track_loaded, &on_event);
|
||||
handle(
|
||||
command,
|
||||
&shared,
|
||||
&generations.start_paused,
|
||||
&mut output,
|
||||
&mut playing,
|
||||
&on_event,
|
||||
);
|
||||
if let Some(started_under) = started_under {
|
||||
playing.generation = started_under;
|
||||
}
|
||||
last_len = output.as_ref().map_or(0, |out| out.player.len());
|
||||
}
|
||||
Err(RecvTimeoutError::Timeout) => {
|
||||
if let Some(out) = &output {
|
||||
let position = out.player.get_pos() + playing.base;
|
||||
shared
|
||||
.position_ms
|
||||
.store(out.player.get_pos().as_millis() as u64, Ordering::Relaxed);
|
||||
.store(position.as_millis() as u64, Ordering::Relaxed);
|
||||
shared
|
||||
.paused
|
||||
.store(out.player.is_paused(), Ordering::Relaxed);
|
||||
let len = out.player.len();
|
||||
if track_loaded && len < last_len {
|
||||
if playing.track_loaded && len < last_len {
|
||||
// The next (gapless) source starts at zero.
|
||||
playing.base = Duration::ZERO;
|
||||
if len == 0 {
|
||||
track_loaded = false;
|
||||
playing.track_loaded = false;
|
||||
}
|
||||
for _ in len..last_len {
|
||||
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
||||
// A source that ended because it was superseded
|
||||
// (its stream cancelled) did not finish.
|
||||
if generation.load(Ordering::SeqCst) == playing.generation {
|
||||
for _ in len..last_len {
|
||||
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
last_len = len;
|
||||
@@ -196,8 +380,9 @@ fn run(rx: Receiver<Command>, shared: Arc<Shared>, on_event: impl Fn(PlayerEvent
|
||||
fn handle(
|
||||
command: Command,
|
||||
shared: &Arc<Shared>,
|
||||
start_paused: &AtomicBool,
|
||||
output: &mut Option<Output>,
|
||||
track_loaded: &mut bool,
|
||||
playing: &mut Playing,
|
||||
on_event: &impl Fn(PlayerEvent),
|
||||
) {
|
||||
match command {
|
||||
@@ -208,42 +393,35 @@ fn handle(
|
||||
seekable,
|
||||
volume,
|
||||
} => {
|
||||
// The device is opened lazily on first playback so the app works
|
||||
// on machines with no audio output until you actually press play.
|
||||
if output.is_none() {
|
||||
match DeviceSinkBuilder::open_default_sink() {
|
||||
Ok(device) => {
|
||||
let player = Player::connect_new(device.mixer());
|
||||
*output = Some(Output {
|
||||
_device: device,
|
||||
player,
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
on_event(PlayerEvent::Failed(format!("no audio device: {err}")));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
let out = output.as_ref().expect("output opened above");
|
||||
|
||||
let Some(out) = ensure_output(output, on_event) else {
|
||||
return;
|
||||
};
|
||||
match decode_source(reader, byte_len, mime_type.as_deref(), seekable) {
|
||||
Ok(source) => {
|
||||
shared.analysis.clear();
|
||||
out.player.stop();
|
||||
out.player.set_volume(volume);
|
||||
out.player.append(analyzer::AnalyzedSource::new(
|
||||
source,
|
||||
Arc::clone(&shared.analysis),
|
||||
));
|
||||
out.player.play();
|
||||
*track_loaded = true;
|
||||
on_event(PlayerEvent::Started);
|
||||
}
|
||||
Ok(source) => start_source(out, source, volume, false, shared, on_event),
|
||||
Err(err) => {
|
||||
on_event(PlayerEvent::Failed(format!("cannot decode track: {err}")));
|
||||
return;
|
||||
}
|
||||
}
|
||||
playing.track_loaded = true;
|
||||
playing.base = Duration::ZERO;
|
||||
}
|
||||
Command::PlaySource {
|
||||
source,
|
||||
volume,
|
||||
start,
|
||||
generation: _,
|
||||
} => {
|
||||
let Some(out) = ensure_output(output, on_event) else {
|
||||
return;
|
||||
};
|
||||
let paused = start_paused.load(Ordering::SeqCst);
|
||||
start_source(out, source, volume, paused, shared, on_event);
|
||||
playing.track_loaded = true;
|
||||
playing.base = start;
|
||||
shared
|
||||
.position_ms
|
||||
.store(start.as_millis() as u64, Ordering::Relaxed);
|
||||
}
|
||||
Command::Enqueue { reader, byte_len } => {
|
||||
let Some(out) = output.as_ref() else {
|
||||
@@ -276,15 +454,25 @@ fn handle(
|
||||
out.player.stop();
|
||||
}
|
||||
shared.analysis.clear();
|
||||
*track_loaded = false;
|
||||
playing.track_loaded = false;
|
||||
playing.base = Duration::ZERO;
|
||||
}
|
||||
Command::Seek(position) => {
|
||||
if let Some(out) = output
|
||||
&& let Err(err) = out.player.try_seek(position)
|
||||
{
|
||||
tracing::warn!(%err, "seek failed");
|
||||
let result = match output {
|
||||
Some(out) => out.player.try_seek(position).map_err(|err| err.to_string()),
|
||||
None => Err("nothing is playing".to_string()),
|
||||
};
|
||||
match &result {
|
||||
// rodio now reports absolute positions.
|
||||
Ok(()) => playing.base = Duration::ZERO,
|
||||
Err(err) => tracing::warn!(%err, "seek failed"),
|
||||
}
|
||||
shared.analysis.clear();
|
||||
on_event(PlayerEvent::Seeked {
|
||||
generation: playing.generation,
|
||||
position,
|
||||
result,
|
||||
});
|
||||
}
|
||||
Command::SetVolume(volume) => {
|
||||
if let Some(out) = output {
|
||||
@@ -294,13 +482,74 @@ fn handle(
|
||||
}
|
||||
}
|
||||
|
||||
/// The device is opened lazily on first playback so the app works on
|
||||
/// machines with no audio output until you actually press play.
|
||||
fn ensure_output<'a>(
|
||||
output: &'a mut Option<Output>,
|
||||
on_event: &impl Fn(PlayerEvent),
|
||||
) -> Option<&'a Output> {
|
||||
if output.is_none() {
|
||||
match DeviceSinkBuilder::open_default_sink() {
|
||||
Ok(device) => {
|
||||
let player = Player::connect_new(device.mixer());
|
||||
*output = Some(Output {
|
||||
_device: device,
|
||||
player,
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
on_event(PlayerEvent::Failed(format!("no audio device: {err}")));
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
output.as_ref()
|
||||
}
|
||||
|
||||
/// Replace whatever is playing with `source`.
|
||||
fn start_source(
|
||||
out: &Output,
|
||||
source: DecodedSource,
|
||||
volume: f32,
|
||||
paused: bool,
|
||||
shared: &Shared,
|
||||
on_event: &impl Fn(PlayerEvent),
|
||||
) {
|
||||
shared.analysis.clear();
|
||||
out.player.stop();
|
||||
out.player.set_volume(volume);
|
||||
out.player.append(analyzer::AnalyzedSource::new(
|
||||
source,
|
||||
Arc::clone(&shared.analysis),
|
||||
));
|
||||
if paused {
|
||||
out.player.pause();
|
||||
} else {
|
||||
out.player.play();
|
||||
}
|
||||
on_event(PlayerEvent::Started);
|
||||
}
|
||||
|
||||
pub(crate) type DecodedSource = Box<dyn Source + Send>;
|
||||
|
||||
pub(crate) fn decode_source(
|
||||
reader: TrackReader,
|
||||
byte_len: Option<u64>,
|
||||
mime_type: Option<&str>,
|
||||
seekable: bool,
|
||||
) -> Result<DecodedSource, String> {
|
||||
decode_source_with(reader, byte_len, mime_type, seekable, false)
|
||||
}
|
||||
|
||||
/// `decode_source` with optional coarse (byte-estimate) seeking, which
|
||||
/// remote streams need: an accurate MP3 seek decodes every frame before the
|
||||
/// target, i.e. downloads it.
|
||||
pub(crate) fn decode_source_with(
|
||||
mut reader: TrackReader,
|
||||
byte_len: Option<u64>,
|
||||
mime_type: Option<&str>,
|
||||
seekable: bool,
|
||||
coarse_seek: bool,
|
||||
) -> Result<DecodedSource, String> {
|
||||
let mime_is_opus = mime_type.is_some_and(|mime| {
|
||||
let mime = mime.to_ascii_lowercase();
|
||||
@@ -316,6 +565,7 @@ pub(crate) fn decode_source(
|
||||
let mut builder = Decoder::builder()
|
||||
.with_data(reader)
|
||||
.with_seekable(seekable)
|
||||
.with_coarse_seek(coarse_seek)
|
||||
.with_gapless(true);
|
||||
if let Some(len) = byte_len {
|
||||
builder = builder.with_byte_len(len);
|
||||
@@ -328,3 +578,60 @@ pub(crate) fn decode_source(
|
||||
.map(|decoder| Box::new(decoder) as DecodedSource)
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A controller without the audio thread; the commands it sends are
|
||||
/// returned instead (no audio device in tests).
|
||||
fn detached() -> (Controller, Receiver<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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
//! 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(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
//! 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;
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
//! HTTP for podcast feeds and cover images.
|
||||
//!
|
||||
//! Redirects are followed by hand so the caller learns where the feed
|
||||
//! really lives: the address reached by the leading permanent (301/308)
|
||||
//! hops is where the feed moved, and the store records it as the new fetch
|
||||
//! URL (the old one becomes an alias). Temporary hops after it (a CDN or a
|
||||
//! tracking prefix) do not move the feed further. Conditional requests
|
||||
//! (`If-None-Match` / `If-Modified-Since`), response size caps, timeouts and
|
||||
//! the user agent are fixed here; parsing and storage happen elsewhere.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use music_dht::podcast::{FeedUrl, PodcastId};
|
||||
use reqwest::header::{
|
||||
ACCEPT, ETAG, HeaderMap, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, RETRY_AFTER,
|
||||
};
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
/// Hard cap on a feed document (large catalogs are a few MiB).
|
||||
pub const MAX_FEED_BYTES: usize = 32 * 1024 * 1024;
|
||||
/// Hard cap on a cover image.
|
||||
pub const MAX_COVER_BYTES: usize = 10 * 1024 * 1024;
|
||||
/// Redirect hops followed before giving up.
|
||||
pub const MAX_REDIRECTS: usize = 10;
|
||||
/// TCP + TLS connection setup.
|
||||
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
/// Whole request, including the body, per hop.
|
||||
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
/// A whole feed or cover fetch, all redirect hops and the body included, so
|
||||
/// a slow server holds one of the few refresh slots for at most this long.
|
||||
#[cfg(not(test))]
|
||||
pub const FETCH_DEADLINE: Duration = Duration::from_secs(120);
|
||||
#[cfg(test)]
|
||||
pub const FETCH_DEADLINE: Duration = Duration::from_secs(3);
|
||||
/// Sent with every podcast request.
|
||||
pub const USER_AGENT: &str = concat!("furumi/", env!("CARGO_PKG_VERSION"), " (podcast reader)");
|
||||
|
||||
const FEED_ACCEPT: &str = "application/rss+xml, application/atom+xml, application/xml;q=0.9, \
|
||||
text/xml;q=0.9, */*;q=0.5";
|
||||
|
||||
/// HTTP validators of the last successful fetch.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct Validators<'a> {
|
||||
pub etag: Option<&'a str>,
|
||||
pub last_modified: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// What a feed request returned.
|
||||
#[derive(Debug)]
|
||||
pub enum FeedBody {
|
||||
/// `304 Not Modified` to a conditional request.
|
||||
NotModified,
|
||||
/// A new document with its validators.
|
||||
Modified {
|
||||
body: Vec<u8>,
|
||||
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,
|
||||
}
|
||||
|
||||
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)
|
||||
.build()
|
||||
.context("building the podcast HTTP client")?;
|
||||
Ok(Fetcher { client, covers_dir })
|
||||
}
|
||||
|
||||
/// GET of a feed. `validators` make it conditional; they are dropped
|
||||
/// after a permanent redirect because they belong to the old URL.
|
||||
pub async fn fetch_feed(
|
||||
&self,
|
||||
url: &FeedUrl,
|
||||
validators: Validators<'_>,
|
||||
) -> Result<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());
|
||||
let status = response.status();
|
||||
if status == StatusCode::NOT_MODIFIED {
|
||||
if !conditional {
|
||||
bail!("the server answered 304 Not Modified to an unconditional request");
|
||||
}
|
||||
return Ok(FeedResponse {
|
||||
body: FeedBody::NotModified,
|
||||
final_url,
|
||||
moved_permanently_to,
|
||||
redirects,
|
||||
});
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(status_error(status, response.headers()).into());
|
||||
}
|
||||
let etag = header_string(response.headers(), ETAG);
|
||||
let last_modified = header_string(response.headers(), LAST_MODIFIED);
|
||||
let body = read_capped(response, MAX_FEED_BYTES, "feed").await?;
|
||||
Ok(FeedResponse {
|
||||
body: FeedBody::Modified {
|
||||
body,
|
||||
etag,
|
||||
last_modified,
|
||||
},
|
||||
final_url,
|
||||
moved_permanently_to,
|
||||
redirects,
|
||||
})
|
||||
}
|
||||
|
||||
/// Downloads a cover image into the covers directory; returns its path.
|
||||
pub async fn fetch_cover(&self, podcast_id: &PodcastId, image_url: &str) -> Result<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")?;
|
||||
let extension = format.extensions_str().first().copied().unwrap_or("img");
|
||||
let name = cover_file_name(podcast_id, image_url, extension);
|
||||
let dir = self.covers_dir.clone();
|
||||
tokio::task::spawn_blocking(move || write_atomically(&dir, &name, &body))
|
||||
.await
|
||||
.map_err(|err| anyhow!("cover writer task failed: {err}"))?
|
||||
}
|
||||
|
||||
async fn download_cover(&self, image_url: &str) -> Result<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(¤t)?;
|
||||
// Still in the leading run of permanent hops.
|
||||
let mut permanent_run = true;
|
||||
let mut moved_permanently_to = None;
|
||||
let mut send_validators = true;
|
||||
let mut hops = 0;
|
||||
loop {
|
||||
let mut request = self.client.get(current.clone());
|
||||
if let Some(accept) = accept {
|
||||
request = request.header(ACCEPT, accept);
|
||||
}
|
||||
if send_validators {
|
||||
if let Some(etag) = validators.etag {
|
||||
request = request.header(IF_NONE_MATCH, etag);
|
||||
}
|
||||
if let Some(last_modified) = validators.last_modified {
|
||||
request = request.header(IF_MODIFIED_SINCE, last_modified);
|
||||
}
|
||||
}
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
// reqwest errors print the full URL, query tokens included.
|
||||
.map_err(reqwest::Error::without_url)
|
||||
.with_context(|| format!("requesting {}", redact(¤t)))?;
|
||||
let status = response.status();
|
||||
if !status.is_redirection() || status == StatusCode::NOT_MODIFIED {
|
||||
let conditional = send_validators
|
||||
&& (validators.etag.is_some() || validators.last_modified.is_some());
|
||||
return Ok(Followed {
|
||||
response,
|
||||
url: current,
|
||||
moved_permanently_to,
|
||||
redirects: hops,
|
||||
conditional,
|
||||
});
|
||||
}
|
||||
let location = response
|
||||
.headers()
|
||||
.get(LOCATION)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.ok_or_else(|| anyhow!("HTTP {status} redirect without a Location header"))?;
|
||||
let next = current
|
||||
.join(location.trim())
|
||||
.with_context(|| format!("invalid redirect location {location:?}"))?;
|
||||
check_scheme(&next)?;
|
||||
hops += 1;
|
||||
if hops > MAX_REDIRECTS {
|
||||
bail!("too many redirects (more than {MAX_REDIRECTS})");
|
||||
}
|
||||
let permanent = matches!(
|
||||
status,
|
||||
StatusCode::MOVED_PERMANENTLY | StatusCode::PERMANENT_REDIRECT
|
||||
);
|
||||
permanent_run &= permanent;
|
||||
if permanent_run {
|
||||
moved_permanently_to = Some(next.clone());
|
||||
}
|
||||
if permanent {
|
||||
send_validators = false;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fails `fetch` once [`FETCH_DEADLINE`] has passed.
|
||||
async fn within_deadline<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})");
|
||||
}
|
||||
let mut body = Vec::new();
|
||||
while let Some(chunk) = response
|
||||
.chunk()
|
||||
.await
|
||||
.map_err(reqwest::Error::without_url)
|
||||
.with_context(|| format!("reading the {what}"))?
|
||||
{
|
||||
if body.len() + chunk.len() > cap {
|
||||
bail!("the {what} is too large (over {cap} bytes)");
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Stable per podcast and image URL, so a changed image gets a new file and
|
||||
/// the previous one can be removed.
|
||||
fn cover_file_name(podcast_id: &PodcastId, image_url: &str, extension: &str) -> String {
|
||||
let podcast = blake3::hash(podcast_id.as_str().as_bytes()).to_hex();
|
||||
let image = blake3::hash(image_url.as_bytes()).to_hex();
|
||||
format!("{}-{}.{extension}", &podcast[..16], &image[..16])
|
||||
}
|
||||
|
||||
fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result<PathBuf> {
|
||||
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
|
||||
let target = dir.join(name);
|
||||
let temp = dir.join(format!(
|
||||
".{name}.{}-{}.tmp",
|
||||
std::process::id(),
|
||||
COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
let result = std::fs::write(&temp, bytes)
|
||||
.and_then(|()| std::fs::rename(&temp, &target))
|
||||
.with_context(|| format!("writing {}", target.display()));
|
||||
if result.is_err() {
|
||||
let _ = std::fs::remove_file(&temp);
|
||||
}
|
||||
result?;
|
||||
Ok(target)
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
//! Podcast runtime of the TUI: feed HTTP, refresh scheduling and the glue
|
||||
//! between the shared podcast store and the device-sync log.
|
||||
//!
|
||||
//! The data model, identity rules, merge rules and the SQLite store are not
|
||||
//! defined here: they come from FRID (`music_dht::podcast` and
|
||||
//! `furumi_library::podcasts::PodcastStore`, file `podcasts.sqlite3`). This
|
||||
//! module only does what the contract leaves to clients:
|
||||
//!
|
||||
//! * [`fetch`] downloads feeds (conditional GET, redirects reported so
|
||||
//! permanent moves become feed aliases, size and time limits) and covers;
|
||||
//! * [`refresh`] subscribes and refreshes feeds: it parses them with the
|
||||
//! shared `music_dht::podcast::feed` parser, ingests them with
|
||||
//! `PodcastStore::apply_feed` and keeps the local fetch state (validators,
|
||||
//! errors, back-off);
|
||||
//! * [`OpLog`] hands store actions the stamps of the device-sync log and
|
||||
//! appends the operations they return;
|
||||
//! * [`stream`] plays an episode straight from the publisher through a
|
||||
//! bounded HTTP Range reader, and [`playback`] resolves the current audio
|
||||
//! URL and resume position, opens and positions it;
|
||||
//! * [`progress`] writes resume positions, completions and listening
|
||||
//! sessions in order on its own thread;
|
||||
//! * [`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.
|
||||
|
||||
pub mod discovery;
|
||||
pub mod fetch;
|
||||
pub mod playback;
|
||||
pub mod progress;
|
||||
pub mod publication;
|
||||
pub mod refresh;
|
||||
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)]
|
||||
mod tests;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use furumi_library::podcasts::{
|
||||
LEGACY_PROTOTYPE_SCHEMA_VERSION, PODCAST_DB_FILE, PodcastListFilter, PodcastLocalOp,
|
||||
PodcastStore, PodcastStoreError, StampSource,
|
||||
};
|
||||
|
||||
pub use fetch::Fetcher;
|
||||
use refresh::Services;
|
||||
|
||||
/// Appends podcast operations to the device-sync log.
|
||||
///
|
||||
/// [`OpLog::record`] gives `action` a [`StampSource`] that reserves stamps
|
||||
/// in the log. The action runs a `PodcastStore` local action and returns the
|
||||
/// operations it emitted; they are appended with exactly those stamps.
|
||||
pub trait OpLog: Send + Sync {
|
||||
fn record(
|
||||
&self,
|
||||
action: &mut dyn FnMut(&mut dyn StampSource) -> Result<Vec<PodcastLocalOp>>,
|
||||
) -> Result<()>;
|
||||
}
|
||||
|
||||
/// The podcast store file. It is the file the prototype podcast build
|
||||
/// used, so an existing prototype database is migrated in place.
|
||||
pub fn db_path() -> Result<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))
|
||||
}
|
||||
|
||||
/// 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"))
|
||||
}
|
||||
|
||||
/// 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>
|
||||
where
|
||||
T: Send + 'static,
|
||||
F: FnOnce() -> Result<T> + Send + 'static,
|
||||
{
|
||||
tokio::task::spawn_blocking(work)
|
||||
.await
|
||||
.map_err(|err| anyhow!("background task failed: {err}"))?
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
//! Preparing an episode for playback: resolve its current audio URL and
|
||||
//! resume position from the store, open the HTTP stream, decode it and
|
||||
//! position it, all off the UI and audio threads.
|
||||
//!
|
||||
//! The queue item ([`PodcastEpisodeRef`]) carries the URL that was current
|
||||
//! when the episode was queued (or received); the store is asked again at
|
||||
//! play time, so a feed refresh that changed the episode's audio URL is
|
||||
//! picked up. When the publisher answers a stale URL with a client error,
|
||||
//! the feed is refreshed once and the episode retried with the address the
|
||||
//! feed now lists for the same episode id.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use furumi_library::podcasts::PodcastStore;
|
||||
use music_dht::podcast::{PodcastEpisodeRef, reaches_completion};
|
||||
use rodio::Source as _;
|
||||
|
||||
use super::blocking;
|
||||
use super::refresh::{self, Services};
|
||||
use super::stream::{self, CancelToken, Cancelled, StreamEnd, StreamProbe, StreamStatusError};
|
||||
use crate::player::{DecodedSource, decode_source_with};
|
||||
|
||||
/// Where playback of an episode starts.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StartAt {
|
||||
/// Ordinary Play: the saved resume position (the beginning for a new
|
||||
/// or completed episode).
|
||||
Resume,
|
||||
/// An exact position (handoff, restarting the same item).
|
||||
Position { ms: i64 },
|
||||
/// "Start over": from the beginning, recorded as an explicit restart.
|
||||
Restart,
|
||||
}
|
||||
|
||||
/// An episode ready to hand to the audio thread.
|
||||
pub struct PreparedEpisode {
|
||||
/// The episode as the store knows it now (current URL and duration), or
|
||||
/// the queued reference when the store does not know it.
|
||||
pub episode: PodcastEpisodeRef,
|
||||
pub source: DecodedSource,
|
||||
/// Position the source was moved to.
|
||||
pub start_ms: i64,
|
||||
/// Duration from the decoder, else the one the feed announced.
|
||||
pub duration_ms: Option<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:#}")
|
||||
}
|
||||
@@ -0,0 +1,836 @@
|
||||
//! Episode playback preparation and durable progress, against a local HTTP
|
||||
//! server and a temp-dir store (no internet, no audio device).
|
||||
|
||||
use std::sync::PoisonError;
|
||||
use std::sync::mpsc::{Receiver, RecvTimeoutError};
|
||||
use std::time::Duration;
|
||||
|
||||
use furumi_library::podcasts::{EpisodeQuery, PodcastStore};
|
||||
use music_dht::podcast::{
|
||||
EpisodeId, PodcastEpisodeRef, PodcastId, ProgressStatus, reaches_completion,
|
||||
};
|
||||
|
||||
use super::playback::{self, CannotResume, StartAt};
|
||||
use super::progress::{ProgressCommand, ProgressEvent, ProgressWriter};
|
||||
use super::refresh::{Services, refresh, subscribe};
|
||||
use super::stream::{CancelToken, StreamEnd};
|
||||
use super::test_server::{Route, WAV_RATE, wav};
|
||||
use super::tests::{Fixture, NOW, fixture, rss};
|
||||
|
||||
const MINUTE_MS: i64 = 60_000;
|
||||
|
||||
/// Subscribes the fixture to a feed with episodes `a` and `b` and returns
|
||||
/// the podcast and the reference of episode `a`.
|
||||
async fn subscribed(f: &Fixture) -> (PodcastId, PodcastEpisodeRef) {
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW)
|
||||
.await
|
||||
.unwrap();
|
||||
let episode = episode_ref(&f.services.store, &report.podcast_id, "Pilot");
|
||||
(report.podcast_id, episode)
|
||||
}
|
||||
|
||||
fn episode_ref(store: &PodcastStore, podcast_id: &PodcastId, title: &str) -> PodcastEpisodeRef {
|
||||
let item = store
|
||||
.episodes(&EpisodeQuery::new(podcast_id.clone()))
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|item| item.episode.title == title)
|
||||
.expect("episode in the catalog");
|
||||
store
|
||||
.episode_ref(&item.episode.episode_id)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A progress writer whose events can be awaited.
|
||||
struct Writer {
|
||||
writer: ProgressWriter,
|
||||
events: Receiver<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()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,488 @@
|
||||
//! Durable episode progress: one background thread writes every progress
|
||||
//! action of this device to the podcast store and the sync log, in order.
|
||||
//!
|
||||
//! The rules come from the FRID contract (`music_dht::podcast::state`,
|
||||
//! `PodcastStore` progress actions); this module only decides *when* to
|
||||
//! call them:
|
||||
//!
|
||||
//! * a playback start is an explicit action (`start_session`, or `restart`
|
||||
//! for "start over") that takes a new progress generation;
|
||||
//! * periodic checkpoints, pause and the final save keep the session's
|
||||
//! generation (`checkpoint`), so a late checkpoint of an older session
|
||||
//! always loses against a newer explicit action;
|
||||
//! * a seek is explicit (`seek`) and becomes the session's generation;
|
||||
//! * reaching the end (`reaches_completion` or the end of the stream) is
|
||||
//! `complete_session`;
|
||||
//! * when the store reports a newer generation (`Superseded`), the session
|
||||
//! stops writing checkpoints until the user's next explicit action;
|
||||
//! * the listening session itself (`ListenSession`) is recorded at pause,
|
||||
//! completion and end; the store only keeps sessions that pass the shared
|
||||
//! history threshold and only when they grew.
|
||||
//!
|
||||
//! "Mark played" / "Mark unplayed" go through the same thread so they are
|
||||
//! ordered with the checkpoints of a playing episode.
|
||||
//!
|
||||
//! Writes never happen on the UI or audio thread, and a failed write is
|
||||
//! reported and logged without stopping playback.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use furumi_library::podcasts::{
|
||||
CheckpointOutcome, PodcastLocalOp, PodcastStore, ProgressSession, ProgressWrite, StampSource,
|
||||
};
|
||||
use music_dht::podcast::{EpisodeId, ListenSession, PodcastEpisodeRef, ProgressRecord};
|
||||
|
||||
use super::OpLog;
|
||||
use super::refresh::Services;
|
||||
|
||||
/// How often a playing episode saves its position.
|
||||
pub const CHECKPOINT_INTERVAL: Duration = Duration::from_secs(20);
|
||||
|
||||
/// What the UI asks the writer to do. `playback` identifies one start of
|
||||
/// one episode on this device; commands for a playback that is no longer
|
||||
/// the active one are ignored (they can only arrive late).
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ProgressCommand {
|
||||
/// Playback actually started at `position_ms`.
|
||||
Begin {
|
||||
playback: u64,
|
||||
episode: Box<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
@@ -0,0 +1,430 @@
|
||||
//! 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:#}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,971 @@
|
||||
//! Seekable playback of a remote episode without downloading it first.
|
||||
//!
|
||||
//! Audio is never stored: the reader keeps a bounded in-memory window and
|
||||
//! the episode stays on the publisher's server (it is not imported, hashed
|
||||
//! or offered to peers).
|
||||
//!
|
||||
//! [`open`] issues one `Range: bytes=0-` request and hands the response to a
|
||||
//! dedicated downloader thread. The thread fills a bounded in-memory window
|
||||
//! around the reader position:
|
||||
//!
|
||||
//! * up to [`READ_AHEAD`] bytes ahead of the reader (then it pauses),
|
||||
//! * up to [`KEEP_BEHIND`] bytes behind it (cheap small backward seeks),
|
||||
//! * plus the first [`HEAD_KEEP`] bytes of the file, kept permanently because
|
||||
//! decoders re-read the container header while probing.
|
||||
//!
|
||||
//! The window buffer is allocated once at its maximum size and never grows,
|
||||
//! so memory stays bounded (and no large reallocation happens under the lock
|
||||
//! the audio callback reads through).
|
||||
//!
|
||||
//! A seek inside the window is free. A read outside it restarts the
|
||||
//! downloader at the new position with a fresh `Range` request; a generation
|
||||
//! counter makes sure bytes from the superseded response are dropped. Servers
|
||||
//! that ignore `Range` are still supported: forward seeks read and discard,
|
||||
//! backward seeks restart from the beginning.
|
||||
//!
|
||||
//! Network errors: the downloader never gives up on a transient error by
|
||||
//! itself. It resumes from its current offset with capped exponential backoff
|
||||
//! (shortened while the reader is waiting for data), so a short outage is
|
||||
//! bridged by the buffered window. The decision to fail belongs to the
|
||||
//! reader: a `read` that is blocked on missing data and sees no download
|
||||
//! progress for [`STARVE_TIMEOUT`] returns `io::ErrorKind::TimedOut`; the next
|
||||
//! `read` starts a fresh attempt. Client errors (HTTP 4xx other than 408/429,
|
||||
//! e.g. an expired signed URL) are reported to the reader immediately.
|
||||
//!
|
||||
//! Timeouts: `reqwest::blocking` applies `ClientBuilder::timeout` to waiting
|
||||
//! for the response head and then *separately to every `Response::read`
|
||||
//! call* (verified in reqwest 0.12.28: `blocking::Response::read` wraps each
|
||||
//! read in `wait::timeout(.., self.timeout)`, and the blocking client does not
|
||||
//! forward that value to the async client's total timeout). So
|
||||
//! `timeout(STALL_TIMEOUT)` is a per-read stall timeout, not a cap on the whole
|
||||
//! body, and long episodes are fine. The request-level
|
||||
//! `RequestBuilder::timeout` (which *is* a total timeout) is never used.
|
||||
//!
|
||||
//! Cancellation: a stream belongs to one playback start ([`CancelToken`]).
|
||||
//! Once that start is superseded (another item, stop, restart), every read,
|
||||
//! including one blocked waiting for data inside the audio callback, fails
|
||||
//! within [`CANCEL_POLL`], and the downloader thread stops. So a stalled
|
||||
//! stream never holds the audio thread past the user's next choice, and a
|
||||
//! superseded resume (which may be reading and discarding up to its offset)
|
||||
//! stops downloading.
|
||||
//!
|
||||
//! End of playback: [`StreamProbe::end`] tells whether the reader saw the
|
||||
//! file end the way the server described it or stopped on an error, so an
|
||||
//! episode whose connection was lost is not taken as played to the end.
|
||||
//!
|
||||
//! Decoder note for the player integration: symphonia's MP3 demuxer in
|
||||
//! accurate seek mode never seeks by byte offset (a backward seek re-parses
|
||||
//! from the first frame), which would re-download the episode. Build rodio
|
||||
//! decoders for these streams with `.with_coarse_seek(true)` (and
|
||||
//! `.with_byte_len(..)`), so a seek maps to a single range request.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{self, Read, Seek, SeekFrom};
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use reqwest::blocking::{Client, Response};
|
||||
use reqwest::header::{CONTENT_RANGE, CONTENT_TYPE, RANGE};
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
use super::fetch::redact;
|
||||
|
||||
/// Maximum bytes buffered ahead of the reader position.
|
||||
pub const READ_AHEAD: u64 = 8 * 1024 * 1024;
|
||||
/// Bytes kept behind the reader position for small backward seeks.
|
||||
pub const KEEP_BEHIND: u64 = 1024 * 1024;
|
||||
/// Leading bytes of the file kept for the lifetime of the stream.
|
||||
pub const HEAD_KEEP: usize = 256 * 1024;
|
||||
/// Size of a single network read.
|
||||
const CHUNK: usize = 64 * 1024;
|
||||
/// Largest the window can get: the read-ahead may overshoot by less than one
|
||||
/// chunk before the downloader pauses.
|
||||
const WINDOW_CAP: usize = READ_AHEAD as usize + KEEP_BEHIND as usize + CHUNK;
|
||||
/// A forward gap up to this size is bridged by waiting for the downloader
|
||||
/// instead of issuing a new range request.
|
||||
const NEAR_GAP: u64 = 512 * 1024;
|
||||
/// A restart this close ahead of the current response offset keeps the
|
||||
/// response and discards bytes instead of reconnecting.
|
||||
const DISCARD_LIMIT: u64 = 256 * 1024;
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
/// No progress for this long on one read (or the response head) is an error.
|
||||
const STALL_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
/// First retry delay; doubles per consecutive failure.
|
||||
const RETRY_BASE: Duration = Duration::from_millis(250);
|
||||
/// Longest delay between attempts while the reader still has buffered data.
|
||||
const MAX_BACKOFF: Duration = Duration::from_secs(10);
|
||||
/// Longest delay between attempts while the reader is waiting for data.
|
||||
const STARVED_BACKOFF: Duration = Duration::from_secs(1);
|
||||
/// A `read` blocked on missing data fails after this long without any
|
||||
/// download progress. Shorter in tests to keep failure tests fast.
|
||||
#[cfg(not(test))]
|
||||
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
#[cfg(test)]
|
||||
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(4);
|
||||
/// How often a blocked read or an idle downloader checks its
|
||||
/// [`CancelToken`].
|
||||
pub const CANCEL_POLL: Duration = Duration::from_millis(100);
|
||||
|
||||
/// Tells a stream that the playback start it belongs to was superseded.
|
||||
#[derive(Clone)]
|
||||
pub struct CancelToken(Option<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,
|
||||
}
|
||||
|
||||
/// `Read + Seek` view of a remote file; see the module docs.
|
||||
pub struct HttpStream {
|
||||
pos: u64,
|
||||
shared: Arc<Shared>,
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
state: Mutex<State>,
|
||||
cancel: CancelToken,
|
||||
/// Signalled when data, EOF, an error or downloader exit is available to
|
||||
/// the reader.
|
||||
data_ready: Condvar,
|
||||
/// Signalled when the reader moved, requested a restart, started waiting
|
||||
/// or went away.
|
||||
space_ready: Condvar,
|
||||
}
|
||||
|
||||
struct State {
|
||||
/// Bumped on every restart; downloader output for older generations is
|
||||
/// discarded.
|
||||
generation: u64,
|
||||
/// Start offset requested for the current generation.
|
||||
target: u64,
|
||||
/// File offset of `window[0]`.
|
||||
window_start: u64,
|
||||
window: VecDeque<u8>,
|
||||
/// Bytes `0..head.len()` of the file.
|
||||
head: Vec<u8>,
|
||||
/// 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,
|
||||
/// The current generation was abandoned (fatal error, or the reader gave
|
||||
/// up waiting); the downloader idles until the reader restarts it.
|
||||
failed: bool,
|
||||
/// Fatal error to report to the reader once.
|
||||
error: Option<(io::ErrorKind, String)>,
|
||||
/// Most recent transient failure, for the reader's timeout message.
|
||||
last_error: Option<String>,
|
||||
/// Bytes received from the network for the current generation
|
||||
/// (including discarded ones); the reader's stall detection watches it.
|
||||
progress: u64,
|
||||
/// The reader is blocked waiting for data.
|
||||
reader_waiting: bool,
|
||||
/// The reader went away.
|
||||
closed: bool,
|
||||
/// The downloader thread exited.
|
||||
exited: bool,
|
||||
peak_buffered: usize,
|
||||
peak_capacity: usize,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn window_end(&self) -> u64 {
|
||||
self.window_start + self.window.len() as u64
|
||||
}
|
||||
|
||||
fn buffered(&self) -> usize {
|
||||
self.window.len() + self.head.len()
|
||||
}
|
||||
|
||||
fn restart_at(&mut self, pos: u64) {
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
self.target = pos;
|
||||
self.window_start = pos;
|
||||
// Keeps the allocation: the window never needs more than WINDOW_CAP.
|
||||
self.window.clear();
|
||||
self.done = false;
|
||||
self.failed = false;
|
||||
self.error = None;
|
||||
self.last_error = None;
|
||||
self.progress = 0;
|
||||
}
|
||||
|
||||
fn trim_behind(&mut self) {
|
||||
let keep_from = self.reader_pos.saturating_sub(KEEP_BEHIND);
|
||||
if keep_from > self.window_start {
|
||||
let drop = (keep_from - self.window_start).min(self.window.len() as u64) as usize;
|
||||
self.window.drain(..drop);
|
||||
self.window_start += drop as u64;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Shared {
|
||||
fn lock(&self) -> MutexGuard<'_, State> {
|
||||
self.state.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// [`open_with`] without cancellation.
|
||||
#[cfg(test)]
|
||||
pub fn open(url: &str) -> Result<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);
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
let client = Client::builder()
|
||||
.connect_timeout(CONNECT_TIMEOUT)
|
||||
// Per-read stall timeout, see the module docs.
|
||||
.timeout(STALL_TIMEOUT)
|
||||
.redirect(reqwest::redirect::Policy::custom(|attempt| {
|
||||
if attempt.previous().len() >= 10 {
|
||||
attempt.error("too many redirects")
|
||||
} else if matches!(attempt.url().scheme(), "http" | "https") {
|
||||
attempt.follow()
|
||||
} else {
|
||||
attempt.stop()
|
||||
}
|
||||
}))
|
||||
.user_agent(concat!(
|
||||
"furumi/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (podcast reader)"
|
||||
))
|
||||
.build()
|
||||
.context("building the streaming HTTP client")?;
|
||||
let response = client
|
||||
.get(parsed)
|
||||
.header(RANGE, "bytes=0-")
|
||||
.send()
|
||||
.map_err(|err| anyhow!(err.without_url()))
|
||||
.with_context(|| format!("requesting {shown}"))?;
|
||||
if cancel.is_cancelled() {
|
||||
return Err(Cancelled.into());
|
||||
}
|
||||
let opened = classify(&response, 0).with_context(|| format!("requesting {shown}"))?;
|
||||
let mime_type = response
|
||||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(|value| value.split(';').next())
|
||||
.map(|value| value.trim().to_ascii_lowercase())
|
||||
.filter(|value| !value.is_empty());
|
||||
|
||||
let window = VecDeque::with_capacity(WINDOW_CAP);
|
||||
let peak_capacity = window.capacity();
|
||||
let shared = Arc::new(Shared {
|
||||
state: Mutex::new(State {
|
||||
generation: 0,
|
||||
target: 0,
|
||||
window_start: 0,
|
||||
window,
|
||||
head: Vec::with_capacity(HEAD_KEEP),
|
||||
reader_pos: 0,
|
||||
total: opened.total,
|
||||
declared_total: opened.total.is_some(),
|
||||
read_failed: false,
|
||||
ranges: opened.ranges,
|
||||
done: false,
|
||||
failed: false,
|
||||
error: None,
|
||||
last_error: None,
|
||||
progress: 0,
|
||||
reader_waiting: false,
|
||||
closed: false,
|
||||
exited: false,
|
||||
peak_buffered: 0,
|
||||
peak_capacity,
|
||||
}),
|
||||
cancel,
|
||||
data_ready: Condvar::new(),
|
||||
space_ready: Condvar::new(),
|
||||
});
|
||||
// Requests continue at the final URL of the redirect chain.
|
||||
let downloader = Downloader {
|
||||
shared: Arc::clone(&shared),
|
||||
client,
|
||||
url: response.url().clone(),
|
||||
};
|
||||
std::thread::Builder::new()
|
||||
.name("podcast-stream".to_string())
|
||||
.spawn(move || downloader.run(response))
|
||||
.context("spawning the podcast stream thread")?;
|
||||
Ok(OpenedStream {
|
||||
probe: StreamProbe(Arc::clone(&shared)),
|
||||
reader: HttpStream { pos: 0, shared },
|
||||
byte_len: opened.total,
|
||||
mime_type,
|
||||
seekable: opened.total.is_some(),
|
||||
})
|
||||
}
|
||||
|
||||
struct Classified {
|
||||
/// Offset of the first body byte.
|
||||
start: u64,
|
||||
total: Option<u64>,
|
||||
ranges: bool,
|
||||
}
|
||||
|
||||
/// Interprets a response to `Range: bytes=<requested>-` (or a plain GET).
|
||||
fn classify(response: &Response, requested: u64) -> Result<Classified> {
|
||||
let status = response.status();
|
||||
if status == StatusCode::PARTIAL_CONTENT {
|
||||
let range = response
|
||||
.headers()
|
||||
.get(CONTENT_RANGE)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(parse_content_range);
|
||||
let Some((start, total)) = range else {
|
||||
bail!("HTTP 206 without a usable Content-Range");
|
||||
};
|
||||
if start != requested {
|
||||
bail!("server returned range starting at {start}, expected {requested}");
|
||||
}
|
||||
return Ok(Classified {
|
||||
start,
|
||||
total,
|
||||
ranges: true,
|
||||
});
|
||||
}
|
||||
if status.is_success() {
|
||||
return Ok(Classified {
|
||||
start: 0,
|
||||
total: response.content_length(),
|
||||
ranges: false,
|
||||
});
|
||||
}
|
||||
Err(StreamStatusError(status.as_u16()).into())
|
||||
}
|
||||
|
||||
/// The audio server answered with an error status.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StreamStatusError(pub u16);
|
||||
|
||||
impl StreamStatusError {
|
||||
/// The URL itself is wrong or expired (4xx other than 408/429): the
|
||||
/// feed may know a newer address for the episode.
|
||||
pub fn url_is_stale(self) -> bool {
|
||||
(400..500).contains(&self.0) && self.0 != 408 && self.0 != 429
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StreamStatusError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match StatusCode::from_u16(self.0) {
|
||||
Ok(status) => write!(f, "HTTP {status}"),
|
||||
Err(_) => write!(f, "HTTP {}", self.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for StreamStatusError {}
|
||||
|
||||
/// Parses `bytes <start>-<end>/<total|*>` into `(start, total)`.
|
||||
fn parse_content_range(value: &str) -> Option<(u64, Option<u64>)> {
|
||||
let value = value.trim();
|
||||
let rest = value
|
||||
.get(..6)
|
||||
.filter(|unit| unit.eq_ignore_ascii_case("bytes "))
|
||||
.map(|_| &value[6..])?;
|
||||
let (range, total) = rest.split_once('/')?;
|
||||
let (start, _end) = range.trim().split_once('-')?;
|
||||
let start = start.trim().parse().ok()?;
|
||||
let total = match total.trim() {
|
||||
"*" => None,
|
||||
total => Some(total.parse().ok()?),
|
||||
};
|
||||
Some((start, total))
|
||||
}
|
||||
|
||||
/// Why a (re)connection attempt failed.
|
||||
enum Failure {
|
||||
/// Worth retrying (network error, 5xx, 408/429, odd range answer).
|
||||
Transient(String),
|
||||
/// Retrying will not help (4xx such as 403/404/410).
|
||||
Fatal(String),
|
||||
}
|
||||
|
||||
struct Downloader {
|
||||
shared: Arc<Shared>,
|
||||
client: Client,
|
||||
url: Url,
|
||||
}
|
||||
|
||||
/// Marks the downloader as exited however `run` returns (including a panic).
|
||||
struct ExitFlag(Arc<Shared>);
|
||||
|
||||
impl Drop for ExitFlag {
|
||||
fn drop(&mut self) {
|
||||
// Set under the lock so a reader that checked `exited` and is about
|
||||
// to wait cannot miss the notification.
|
||||
let mut state = self.0.lock();
|
||||
state.exited = true;
|
||||
drop(state);
|
||||
self.0.data_ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
impl Downloader {
|
||||
fn run(self, first: Response) {
|
||||
let _exit = ExitFlag(Arc::clone(&self.shared));
|
||||
let mut generation = 0u64;
|
||||
let mut response = Some(first);
|
||||
// File offset of the next byte `response` yields, or where the next
|
||||
// request should start when there is no response.
|
||||
let mut offset = 0u64;
|
||||
// Consecutive failures of the current generation (drives backoff).
|
||||
let mut failures = 0u32;
|
||||
let mut buf = vec![0u8; CHUNK];
|
||||
loop {
|
||||
if !self.wait_for_work(&mut generation, &mut response, &mut offset, &mut failures) {
|
||||
return;
|
||||
}
|
||||
if response.is_none() {
|
||||
match self.reopen(offset) {
|
||||
Ok(Some((next, start))) => {
|
||||
response = Some(next);
|
||||
offset = start;
|
||||
}
|
||||
Ok(None) => {
|
||||
// Nothing left at this offset (HTTP 416).
|
||||
self.mark_done(generation, offset, false);
|
||||
continue;
|
||||
}
|
||||
Err(Failure::Fatal(message)) => {
|
||||
failures = 0;
|
||||
self.fail(generation, message);
|
||||
continue;
|
||||
}
|
||||
Err(Failure::Transient(message)) => {
|
||||
if !self.retry(generation, &mut failures, &message) {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some(body) = response.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
let read = body.read(&mut buf);
|
||||
if self.shared.cancel.is_cancelled() {
|
||||
return;
|
||||
}
|
||||
match read {
|
||||
Ok(0) => {
|
||||
response = None;
|
||||
let total = self.shared.lock().total;
|
||||
if total.is_some_and(|total| offset < total) {
|
||||
// Connection closed early: resume from `offset`.
|
||||
let message = format!("connection closed at byte {offset}");
|
||||
if !self.retry(generation, &mut failures, &message) {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
self.mark_done(generation, offset, true);
|
||||
}
|
||||
Ok(read) => {
|
||||
failures = 0;
|
||||
let start = offset;
|
||||
offset += read as u64;
|
||||
self.publish(generation, start, &buf[..read]);
|
||||
}
|
||||
Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
|
||||
Err(err) => {
|
||||
response = None;
|
||||
if !self.retry(generation, &mut failures, &err.to_string()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Blocks until there is something to download, adopting restarts
|
||||
/// requested by the reader. Returns `false` when the reader is gone.
|
||||
fn wait_for_work(
|
||||
&self,
|
||||
generation: &mut u64,
|
||||
response: &mut Option<Response>,
|
||||
offset: &mut u64,
|
||||
failures: &mut u32,
|
||||
) -> bool {
|
||||
let mut state = self.shared.lock();
|
||||
loop {
|
||||
if state.closed || self.shared.cancel.is_cancelled() {
|
||||
return false;
|
||||
}
|
||||
if state.generation != *generation {
|
||||
*generation = state.generation;
|
||||
// A new generation gets a fresh backoff.
|
||||
*failures = 0;
|
||||
// Keep the open response when the new target is a little
|
||||
// ahead of it (or anywhere ahead if ranges are unsupported):
|
||||
// discarding is cheaper than reconnecting.
|
||||
let keep = response.is_some()
|
||||
&& state.target >= *offset
|
||||
&& (!state.ranges || state.target - *offset <= DISCARD_LIMIT);
|
||||
if !keep {
|
||||
*response = None;
|
||||
*offset = state.target;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let paused = state.done
|
||||
|| state.failed
|
||||
|| state.window_end() >= state.reader_pos.saturating_add(READ_AHEAD);
|
||||
if !paused {
|
||||
return true;
|
||||
}
|
||||
state = self
|
||||
.shared
|
||||
.space_ready
|
||||
.wait_timeout(state, CANCEL_POLL)
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Requests the file from `offset` (from 0 when ranges are unsupported).
|
||||
/// `Ok(None)` means the server has nothing at that offset.
|
||||
fn reopen(&self, offset: u64) -> std::result::Result<Option<(Response, u64)>, Failure> {
|
||||
let ranges = self.shared.lock().ranges;
|
||||
let mut request = self.client.get(self.url.clone());
|
||||
if ranges {
|
||||
request = request.header(RANGE, format!("bytes={offset}-"));
|
||||
}
|
||||
let response = request
|
||||
.send()
|
||||
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err.without_url()))))?;
|
||||
let status = response.status();
|
||||
if ranges && status == StatusCode::RANGE_NOT_SATISFIABLE {
|
||||
return Ok(None);
|
||||
}
|
||||
if status.is_client_error()
|
||||
&& status != StatusCode::REQUEST_TIMEOUT
|
||||
&& status != StatusCode::TOO_MANY_REQUESTS
|
||||
{
|
||||
return Err(Failure::Fatal(format!("HTTP {status}")));
|
||||
}
|
||||
let classified = classify(&response, if ranges { offset } else { 0 })
|
||||
.map_err(|err| Failure::Transient(format!("{err:#}")))?;
|
||||
let mut state = self.shared.lock();
|
||||
if !classified.ranges && state.ranges {
|
||||
// The server stopped honouring Range: read from 0 and discard.
|
||||
state.ranges = false;
|
||||
}
|
||||
if state.total.is_none() && classified.total.is_some() {
|
||||
state.total = classified.total;
|
||||
state.declared_total = true;
|
||||
}
|
||||
Ok(Some((response, classified.start)))
|
||||
}
|
||||
|
||||
fn mark_done(&self, generation: u64, offset: u64, learn_total: bool) {
|
||||
let mut state = self.shared.lock();
|
||||
if state.generation != generation {
|
||||
return;
|
||||
}
|
||||
state.done = true;
|
||||
if learn_total && state.total.is_none() {
|
||||
state.total = Some(offset.max(state.window_end()));
|
||||
}
|
||||
self.shared.data_ready.notify_all();
|
||||
}
|
||||
|
||||
/// Abandons the current generation with an error for the reader.
|
||||
fn fail(&self, generation: u64, message: String) {
|
||||
let mut state = self.shared.lock();
|
||||
if state.generation != generation {
|
||||
return;
|
||||
}
|
||||
tracing::warn!("podcast stream failed: {message}");
|
||||
state.failed = true;
|
||||
state.error = Some((io::ErrorKind::Other, message));
|
||||
self.shared.data_ready.notify_all();
|
||||
}
|
||||
|
||||
/// Records a transient failure and backs off before the next attempt.
|
||||
/// Returns `false` when the thread should exit.
|
||||
fn retry(&self, generation: u64, failures: &mut u32, message: &str) -> bool {
|
||||
let mut state = self.shared.lock();
|
||||
if state.closed {
|
||||
return false;
|
||||
}
|
||||
if state.generation != generation {
|
||||
// The reader moved on; `wait_for_work` adopts the new target.
|
||||
return true;
|
||||
}
|
||||
*failures = failures.saturating_add(1);
|
||||
state.last_error = Some(message.to_string());
|
||||
tracing::debug!("podcast stream attempt failed ({failures} in a row): {message}");
|
||||
let delay = RETRY_BASE
|
||||
.saturating_mul(1 << (*failures - 1).min(6))
|
||||
.min(MAX_BACKOFF);
|
||||
let started = Instant::now();
|
||||
// Wakes early when the reader goes away, seeks elsewhere or starts
|
||||
// waiting for data (which shortens the delay).
|
||||
loop {
|
||||
if state.closed || self.shared.cancel.is_cancelled() {
|
||||
return false;
|
||||
}
|
||||
if state.generation != generation {
|
||||
return true;
|
||||
}
|
||||
let limit = if state.reader_waiting {
|
||||
delay.min(STARVED_BACKOFF)
|
||||
} else {
|
||||
delay
|
||||
};
|
||||
let elapsed = started.elapsed();
|
||||
if elapsed >= limit {
|
||||
return true;
|
||||
}
|
||||
state = self
|
||||
.shared
|
||||
.space_ready
|
||||
.wait_timeout(state, (limit - elapsed).min(CANCEL_POLL))
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn publish(&self, generation: u64, start: u64, bytes: &[u8]) {
|
||||
let mut state = self.shared.lock();
|
||||
if state.generation != generation {
|
||||
return;
|
||||
}
|
||||
state.progress = state.progress.wrapping_add(bytes.len() as u64);
|
||||
state.last_error = None;
|
||||
let end = start + bytes.len() as u64;
|
||||
// Permanent copy of the file head for format probing.
|
||||
let head_len = state.head.len() as u64;
|
||||
if start <= head_len && end > head_len && head_len < HEAD_KEEP as u64 {
|
||||
let from = (head_len - start) as usize;
|
||||
let to = (HEAD_KEEP as u64).min(end) - start;
|
||||
state.head.extend_from_slice(&bytes[from..to as usize]);
|
||||
}
|
||||
// Only bytes at or after the window end are new; earlier ones are
|
||||
// being skipped (Range-less seek or resume) or are already buffered.
|
||||
let want = state.window_end();
|
||||
if end > want {
|
||||
if start > want {
|
||||
// A gap should not happen; resynchronise rather than corrupt.
|
||||
state.window.clear();
|
||||
state.window_start = start;
|
||||
state.window.extend(bytes);
|
||||
} else {
|
||||
state.window.extend(&bytes[(want - start) as usize..]);
|
||||
}
|
||||
state.trim_behind();
|
||||
}
|
||||
state.peak_buffered = state.peak_buffered.max(state.buffered());
|
||||
state.peak_capacity = state.peak_capacity.max(state.window.capacity());
|
||||
self.shared.data_ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
fn downloader_stopped() -> io::Error {
|
||||
io::Error::other("podcast stream downloader stopped")
|
||||
}
|
||||
|
||||
impl Read for HttpStream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if buf.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
let shared = &*self.shared;
|
||||
let state = shared.lock();
|
||||
let (mut state, result) = read_locked(shared, &mut self.pos, state, buf);
|
||||
state.reader_waiting = false;
|
||||
match &result {
|
||||
Ok(0) => {}
|
||||
Ok(_) => state.read_failed = false,
|
||||
Err(_) => state.read_failed = true,
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/// The body of [`HttpStream::read`]; hands the guard back so the caller can
|
||||
/// clear `reader_waiting` on every exit path.
|
||||
fn read_locked<'a>(
|
||||
shared: &Shared,
|
||||
pos: &mut u64,
|
||||
mut state: MutexGuard<'a, State>,
|
||||
buf: &mut [u8],
|
||||
) -> (MutexGuard<'a, State>, io::Result<usize>) {
|
||||
// When this read started waiting, and the download progress then.
|
||||
let mut starved: Option<(Instant, u64)> = None;
|
||||
loop {
|
||||
if shared.cancel.is_cancelled() {
|
||||
return (state, Err(cancelled_error()));
|
||||
}
|
||||
if state.total.is_some_and(|total| *pos >= total) {
|
||||
return (state, Ok(0));
|
||||
}
|
||||
if *pos < state.head.len() as u64 {
|
||||
let from = *pos as usize;
|
||||
let len = buf.len().min(state.head.len() - from);
|
||||
buf[..len].copy_from_slice(&state.head[from..from + len]);
|
||||
*pos += len as u64;
|
||||
return (state, Ok(len));
|
||||
}
|
||||
let window_end = state.window_end();
|
||||
if *pos >= state.window_start && *pos < window_end {
|
||||
let from = (*pos - state.window_start) as usize;
|
||||
let len = buf.len().min(state.window.len() - from);
|
||||
let (front, back) = state.window.as_slices();
|
||||
copy_from_ring(front, back, from, &mut buf[..len]);
|
||||
*pos += len as u64;
|
||||
state.reader_pos = *pos;
|
||||
// Free what fell out of the keep-behind range even when the
|
||||
// downloader is idle (paused or done).
|
||||
state.trim_behind();
|
||||
shared.space_ready.notify_all();
|
||||
return (state, Ok(len));
|
||||
}
|
||||
// Not buffered. Wait for the current generation if it is heading
|
||||
// here, otherwise restart the downloader at the reader position.
|
||||
let reachable = *pos >= state.window_start
|
||||
&& (!state.ranges || *pos <= window_end.saturating_add(NEAR_GAP));
|
||||
if reachable && !state.failed {
|
||||
if state.done {
|
||||
return (state, Ok(0));
|
||||
}
|
||||
if state.exited {
|
||||
return (state, Err(downloader_stopped()));
|
||||
}
|
||||
if state.reader_pos != *pos {
|
||||
state.reader_pos = *pos;
|
||||
state.trim_behind();
|
||||
shared.space_ready.notify_all();
|
||||
}
|
||||
let now = Instant::now();
|
||||
let since = match starved {
|
||||
Some((since, seen)) if seen == state.progress => since,
|
||||
_ => {
|
||||
starved = Some((now, state.progress));
|
||||
now
|
||||
}
|
||||
};
|
||||
let waited = now.saturating_duration_since(since);
|
||||
if waited >= STARVE_TIMEOUT {
|
||||
// Give up on this generation; the next read retries.
|
||||
state.failed = true;
|
||||
state.error = None;
|
||||
let cause = state
|
||||
.last_error
|
||||
.take()
|
||||
.unwrap_or_else(|| "no data received".to_string());
|
||||
let error = io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
format!(
|
||||
"podcast stream stalled for {}s: {cause}",
|
||||
STARVE_TIMEOUT.as_secs()
|
||||
),
|
||||
);
|
||||
return (state, Err(error));
|
||||
}
|
||||
if !state.reader_waiting {
|
||||
// Lets a downloader in backoff retry sooner.
|
||||
state.reader_waiting = true;
|
||||
shared.space_ready.notify_all();
|
||||
}
|
||||
state = shared
|
||||
.data_ready
|
||||
.wait_timeout(state, (STARVE_TIMEOUT - waited).min(CANCEL_POLL))
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
continue;
|
||||
}
|
||||
if let Some((kind, message)) = state.error.take() {
|
||||
// Report once; the next read retries from scratch.
|
||||
return (state, Err(io::Error::new(kind, message)));
|
||||
}
|
||||
if state.exited {
|
||||
return (state, Err(downloader_stopped()));
|
||||
}
|
||||
state.restart_at(*pos);
|
||||
state.reader_pos = *pos;
|
||||
starved = None;
|
||||
shared.space_ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_from_ring(front: &[u8], back: &[u8], from: usize, out: &mut [u8]) {
|
||||
let mut written = 0;
|
||||
if from < front.len() {
|
||||
let len = out.len().min(front.len() - from);
|
||||
out[..len].copy_from_slice(&front[from..from + len]);
|
||||
written = len;
|
||||
}
|
||||
if written < out.len() {
|
||||
let back_from = from + written - front.len();
|
||||
let len = out.len() - written;
|
||||
out[written..].copy_from_slice(&back[back_from..back_from + len]);
|
||||
}
|
||||
}
|
||||
|
||||
impl Seek for HttpStream {
|
||||
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
|
||||
let target = match position {
|
||||
SeekFrom::Start(offset) => i128::from(offset),
|
||||
SeekFrom::Current(delta) => i128::from(self.pos) + i128::from(delta),
|
||||
SeekFrom::End(delta) => {
|
||||
let Some(total) = self.shared.lock().total else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Unsupported,
|
||||
"stream length is unknown",
|
||||
));
|
||||
};
|
||||
i128::from(total) + i128::from(delta)
|
||||
}
|
||||
};
|
||||
let target = u64::try_from(target).map_err(|_| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"invalid seek to a negative or overflowing position",
|
||||
)
|
||||
})?;
|
||||
// Lazy: the downloader is only redirected when a read needs data.
|
||||
self.pos = target;
|
||||
Ok(target)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for HttpStream {
|
||||
fn drop(&mut self) {
|
||||
let mut state = self.shared.lock();
|
||||
state.closed = true;
|
||||
state.window = VecDeque::new();
|
||||
state.head = Vec::new();
|
||||
drop(state);
|
||||
self.shared.space_ready.notify_all();
|
||||
self.shared.data_ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl HttpStream {
|
||||
/// Bytes currently held in memory (window + head).
|
||||
pub(super) fn buffered_bytes(&self) -> usize {
|
||||
self.shared.lock().buffered()
|
||||
}
|
||||
|
||||
/// Highest number of bytes ever held in memory at once.
|
||||
pub(super) fn peak_buffered_bytes(&self) -> usize {
|
||||
self.shared.lock().peak_buffered
|
||||
}
|
||||
|
||||
/// Largest allocation (window + head capacity) ever held.
|
||||
pub(super) fn peak_allocated_bytes(&self) -> usize {
|
||||
let state = self.shared.lock();
|
||||
state.peak_capacity + state.head.capacity()
|
||||
}
|
||||
|
||||
/// Flag the downloader thread sets when it exits.
|
||||
pub(super) fn exit_probe(&self) -> impl Fn() -> bool + Send + 'static {
|
||||
let shared = Arc::clone(&self.shared);
|
||||
move || shared.lock().exited
|
||||
}
|
||||
}
|
||||
|
||||
/// Upper bound of [`HttpStream`] memory use, for tests.
|
||||
#[cfg(test)]
|
||||
pub(super) const MAX_BUFFERED: usize = WINDOW_CAP + HEAD_KEEP;
|
||||
@@ -0,0 +1,405 @@
|
||||
//! Tests of the HTTP Range stream reader against the local test server:
|
||||
//! sequential reads, seeks inside and outside the window, servers without
|
||||
//! Range or Content-Length, transient cuts, persistent failures, bounded
|
||||
//! memory and thread shutdown.
|
||||
|
||||
use super::stream::{self, MAX_BUFFERED};
|
||||
use super::test_server::{Route, TestServer, noise};
|
||||
use std::io::{ErrorKind, Read, Seek, SeekFrom};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MIB: usize = 1024 * 1024;
|
||||
|
||||
fn read_to_end(reader: &mut impl Read) -> Vec<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");
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
//! Minimal HTTP/1.1 server on 127.0.0.1 for podcast tests (no internet).
|
||||
//! One request per connection (`Connection: close`). Besides feeds it serves
|
||||
//! audio like a publisher: `Range` requests (206 / 416) unless disabled,
|
||||
//! optionally without `Content-Length`, and connections cut after a number
|
||||
//! of bytes to simulate outages.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Route {
|
||||
pub status: u16,
|
||||
pub body: Arc<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`).
|
||||
pub send_length: bool,
|
||||
/// Cut the body after this many bytes (absolute file offset)...
|
||||
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 {
|
||||
pub fn new(body: impl Into<Vec<u8>>, content_type: &str) -> Route {
|
||||
Route {
|
||||
status: 200,
|
||||
body: Arc::new(body.into()),
|
||||
content_type: content_type.to_string(),
|
||||
etag: None,
|
||||
last_modified: None,
|
||||
location: None,
|
||||
retry_after: None,
|
||||
ranges: true,
|
||||
send_length: true,
|
||||
cut_after: None,
|
||||
fail_times: 0,
|
||||
stall: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Answer only after `delay`.
|
||||
pub fn stall(mut self, delay: Duration) -> Route {
|
||||
self.stall = Some(delay);
|
||||
self
|
||||
}
|
||||
|
||||
/// A server that ignores `Range` and always answers 200 with the file.
|
||||
pub fn no_ranges(mut self) -> Route {
|
||||
self.ranges = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// No `Content-Length`: the client cannot know the file size.
|
||||
pub fn no_length(mut self) -> Route {
|
||||
self.send_length = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// The first `times` responses stop after byte `after`.
|
||||
pub fn cut(mut self, after: usize, times: usize) -> Route {
|
||||
self.cut_after = Some(after);
|
||||
self.fail_times = times;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn redirect(status: u16, location: &str) -> Route {
|
||||
Route {
|
||||
status,
|
||||
location: Some(location.to_string()),
|
||||
..Route::new(Vec::new(), "text/plain")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(mut self, status: u16) -> Route {
|
||||
self.status = status;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn etag(mut self, etag: &str) -> Route {
|
||||
self.etag = Some(etag.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn last_modified(mut self, value: &str) -> Route {
|
||||
self.last_modified = Some(value.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn retry_after(mut self, value: &str) -> Route {
|
||||
self.retry_after = Some(value.to_string());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Headers of one received request (names lowercased).
|
||||
pub(super) type RequestHeaders = HashMap<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>,
|
||||
}
|
||||
|
||||
pub(super) struct TestServer {
|
||||
base: String,
|
||||
state: Arc<Mutex<ServerState>>,
|
||||
}
|
||||
|
||||
impl TestServer {
|
||||
pub fn start() -> TestServer {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
|
||||
let base = format!("http://{}", listener.local_addr().expect("local addr"));
|
||||
let state = Arc::new(Mutex::new(ServerState::default()));
|
||||
let accept_state = Arc::clone(&state);
|
||||
std::thread::spawn(move || {
|
||||
for stream in listener.incoming() {
|
||||
let Ok(stream) = stream else { continue };
|
||||
let state = Arc::clone(&accept_state);
|
||||
std::thread::spawn(move || handle(stream, &state));
|
||||
}
|
||||
});
|
||||
TestServer { base, state }
|
||||
}
|
||||
|
||||
pub fn url(&self, path: &str) -> String {
|
||||
format!("{}{path}", self.base)
|
||||
}
|
||||
|
||||
pub fn set(&self, path: &str, route: Route) {
|
||||
self.lock().routes.insert(path.to_string(), route);
|
||||
}
|
||||
|
||||
/// Headers of every request received for `path`, in order.
|
||||
pub fn requests(&self, path: &str) -> Vec<RequestHeaders> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
|
||||
let mut reader = BufReader::new(match stream.try_clone() {
|
||||
Ok(stream) => stream,
|
||||
Err(_) => return,
|
||||
});
|
||||
let mut request_line = String::new();
|
||||
if reader.read_line(&mut request_line).is_err() {
|
||||
return;
|
||||
}
|
||||
let path = request_line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap_or("/")
|
||||
.to_string();
|
||||
let mut headers = RequestHeaders::new();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
match reader.read_line(&mut line) {
|
||||
Ok(0) | Err(_) => return,
|
||||
Ok(_) => {}
|
||||
}
|
||||
let line = line.trim_end();
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some((name, value)) = line.split_once(':') {
|
||||
headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_string());
|
||||
}
|
||||
}
|
||||
let (route, cut) = {
|
||||
let mut state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state
|
||||
.requests
|
||||
.entry(path.clone())
|
||||
.or_default()
|
||||
.push(headers.clone());
|
||||
let route = state.routes.get(&path).cloned();
|
||||
let cut = match &route {
|
||||
Some(route) if route.cut_after.is_some() => {
|
||||
let served = state.cuts.entry(path.clone()).or_default();
|
||||
if *served < route.fail_times {
|
||||
*served += 1;
|
||||
route.cut_after
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
(route, cut)
|
||||
};
|
||||
let mut out = stream;
|
||||
if let Some(delay) = route.as_ref().and_then(|route| route.stall) {
|
||||
std::thread::sleep(delay);
|
||||
}
|
||||
let Some(route) = route else {
|
||||
let _ = out
|
||||
.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
|
||||
return;
|
||||
};
|
||||
let etag_matches = route
|
||||
.etag
|
||||
.as_ref()
|
||||
.is_some_and(|etag| headers.get("if-none-match") == Some(etag));
|
||||
let date_matches = route
|
||||
.last_modified
|
||||
.as_ref()
|
||||
.is_some_and(|date| headers.get("if-modified-since") == Some(date));
|
||||
if route.status == 200 && (etag_matches || date_matches) {
|
||||
let _ = write!(
|
||||
out,
|
||||
"HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n"
|
||||
);
|
||||
return;
|
||||
}
|
||||
let total = route.body.len();
|
||||
let mut status = route.status;
|
||||
let (mut start, mut end) = (0usize, total);
|
||||
let mut extra = String::new();
|
||||
if route.status == 200
|
||||
&& route.ranges
|
||||
&& let Some(range) = headers
|
||||
.get("range")
|
||||
.and_then(|range| range.strip_prefix("bytes="))
|
||||
&& let Some((from, to)) = range.split_once('-')
|
||||
&& let Ok(from) = from.trim().parse::<usize>()
|
||||
{
|
||||
if from >= total {
|
||||
let _ = write!(
|
||||
out,
|
||||
"HTTP/1.1 416 Range Not Satisfiable\r\nContent-Range: bytes */{total}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
||||
);
|
||||
return;
|
||||
}
|
||||
status = 206;
|
||||
start = from;
|
||||
end = to
|
||||
.trim()
|
||||
.parse::<usize>()
|
||||
.map_or(total, |to| (to + 1).min(total));
|
||||
let size = if route.send_length {
|
||||
total.to_string()
|
||||
} else {
|
||||
"*".to_string()
|
||||
};
|
||||
extra.push_str(&format!(
|
||||
"Content-Range: bytes {start}-{}/{size}\r\n",
|
||||
end - 1
|
||||
));
|
||||
}
|
||||
if route.ranges {
|
||||
extra.push_str("Accept-Ranges: bytes\r\n");
|
||||
}
|
||||
if route.send_length {
|
||||
extra.push_str(&format!("Content-Length: {}\r\n", end - start));
|
||||
}
|
||||
for (name, value) in [
|
||||
("ETag", &route.etag),
|
||||
("Last-Modified", &route.last_modified),
|
||||
("Location", &route.location),
|
||||
("Retry-After", &route.retry_after),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
extra.push_str(&format!("{name}: {value}\r\n"));
|
||||
}
|
||||
}
|
||||
let reason = match status {
|
||||
200 => "OK",
|
||||
206 => "Partial Content",
|
||||
301 => "Moved Permanently",
|
||||
302 => "Found",
|
||||
307 => "Temporary Redirect",
|
||||
308 => "Permanent Redirect",
|
||||
404 => "Not Found",
|
||||
503 => "Service Unavailable",
|
||||
_ => "Status",
|
||||
};
|
||||
if write!(
|
||||
out,
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: {}\r\n{extra}Connection: close\r\n\r\n",
|
||||
route.content_type,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
let limit = cut.map_or(end, |cut| cut.clamp(start, end));
|
||||
for chunk in route.body[start..limit].chunks(64 * 1024) {
|
||||
if out.write_all(chunk).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let _ = out.flush();
|
||||
let _ = out.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
|
||||
/// Deterministic pseudo-random bytes.
|
||||
pub(super) fn noise(len: usize, seed: u64) -> Vec<u8> {
|
||||
let mut state = seed | 1;
|
||||
let mut out = Vec::with_capacity(len);
|
||||
while out.len() < len {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
out.extend_from_slice(&state.to_le_bytes());
|
||||
}
|
||||
out.truncate(len);
|
||||
out
|
||||
}
|
||||
|
||||
/// Sample rate of [`wav`].
|
||||
pub(super) const WAV_RATE: u32 = 8_000;
|
||||
|
||||
/// A mono 16-bit PCM WAV file of `seconds` (8 kHz). Every sample of second
|
||||
/// `n` has the value `n * 40` (up to 13 minutes), so a decoded sample tells where playback
|
||||
/// is.
|
||||
pub(super) fn wav(seconds: u32) -> Vec<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
|
||||
}
|
||||
@@ -0,0 +1,994 @@
|
||||
//! Store opening, subscribing and refreshing against a local HTTP server.
|
||||
//! No internet, no real user directories: every test uses a temp dir.
|
||||
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Result;
|
||||
use furumi_library::podcasts::{
|
||||
EpisodeQuery, OpStamp, PodcastListFilter, PodcastLocalOp, PodcastStore, StampSource,
|
||||
};
|
||||
use music_dht::device_sync::{SyncOpPayload, SyncOpWire};
|
||||
use music_dht::podcast::{FeedUrl, PodcastId};
|
||||
|
||||
use super::OpLog;
|
||||
use super::fetch::{FETCH_DEADLINE, MAX_COVER_BYTES, MAX_FEED_BYTES, MAX_REDIRECTS, Validators};
|
||||
use super::refresh::{
|
||||
FAILURE_BACKOFF_BASE, FAILURE_BACKOFF_MAX, REFRESH_INTERVAL, Services,
|
||||
next_check_after_failure, next_check_after_success, refresh, subscribe,
|
||||
};
|
||||
use super::test_server::{Route, TestServer};
|
||||
|
||||
pub(super) const NOW: i64 = 1_700_000_000_000;
|
||||
|
||||
/// In-memory sync log: hands out stamps and keeps the appended operations.
|
||||
#[derive(Default)]
|
||||
pub(super) struct MemoryLog {
|
||||
ops: Mutex<Vec<SyncOpWire>>,
|
||||
}
|
||||
|
||||
struct MemoryStamps {
|
||||
next_seq: i64,
|
||||
reserved: Vec<i64>,
|
||||
}
|
||||
|
||||
impl StampSource for MemoryStamps {
|
||||
fn device_id(&self) -> &str {
|
||||
"test-device"
|
||||
}
|
||||
|
||||
fn next_stamp(&mut self) -> Result<OpStamp> {
|
||||
let seq = self.next_seq + self.reserved.len() as i64;
|
||||
self.reserved.push(seq);
|
||||
Ok(OpStamp::new(
|
||||
format!("test-device:{seq}"),
|
||||
"test-device",
|
||||
NOW + seq,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl OpLog for MemoryLog {
|
||||
fn record(
|
||||
&self,
|
||||
action: &mut dyn FnMut(&mut dyn StampSource) -> Result<Vec<PodcastLocalOp>>,
|
||||
) -> Result<()> {
|
||||
let mut ops = self.ops.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let mut stamps = MemoryStamps {
|
||||
next_seq: ops.len() as i64 + 1,
|
||||
reserved: Vec::new(),
|
||||
};
|
||||
for (op, seq) in action(&mut stamps)?.into_iter().zip(stamps.reserved) {
|
||||
ops.push(op.into_wire(seq));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl MemoryLog {
|
||||
pub(super) fn kinds(&self) -> Vec<String> {
|
||||
self.ops
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.iter()
|
||||
.map(|op| {
|
||||
serde_json::to_value(&op.payload).unwrap()["kind"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct Fixture {
|
||||
_dir: tempfile::TempDir,
|
||||
pub(super) services: Services,
|
||||
pub(super) log: Arc<MemoryLog>,
|
||||
pub(super) server: TestServer,
|
||||
}
|
||||
|
||||
pub(super) fn fixture() -> Fixture {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let log = Arc::new(MemoryLog::default());
|
||||
let services = super::open_services(
|
||||
&dir.path().join("podcasts.sqlite3"),
|
||||
dir.path().join("covers"),
|
||||
log.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
Fixture {
|
||||
_dir: dir,
|
||||
services,
|
||||
log,
|
||||
server: TestServer::start(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn rss(items: &[(&str, &str)]) -> Vec<u8> {
|
||||
let items: String = items
|
||||
.iter()
|
||||
.map(|(guid, title)| {
|
||||
format!(
|
||||
"<item><title>{title}</title><guid>{guid}</guid>\
|
||||
<pubDate>Sat, 15 Jun 2024 10:00:00 GMT</pubDate>\
|
||||
<itunes:duration>45:00</itunes:duration>\
|
||||
<enclosure url=\"/audio/{guid}.mp3\" type=\"audio/mpeg\" length=\"100\"/></item>"
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
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><itunes:author>Host</itunes:author>{items}</channel></rss>"
|
||||
)
|
||||
.into_bytes()
|
||||
}
|
||||
|
||||
fn episode_count(store: &PodcastStore, id: &PodcastId) -> usize {
|
||||
store
|
||||
.episodes(&EpisodeQuery::new(id.clone()))
|
||||
.unwrap()
|
||||
.len()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_then_conditional_refresh_uses_validators() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]),
|
||||
"application/rss+xml",
|
||||
)
|
||||
.etag("\"v1\"")
|
||||
.last_modified("Sat, 15 Jun 2024 10:00:00 GMT"),
|
||||
);
|
||||
let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!report.already_subscribed);
|
||||
assert_eq!(report.new_episodes, 2);
|
||||
assert_eq!(report.title, "Test Show");
|
||||
assert_eq!(f.log.kinds(), ["podcast_subscription_set"]);
|
||||
let store = &f.services.store;
|
||||
let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap();
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert!(!rows[0].shared, "a new subscription is private");
|
||||
let state = store.fetch_state(&report.podcast_id).unwrap();
|
||||
assert_eq!(state.etag.as_deref(), Some("\"v1\""));
|
||||
assert_eq!(state.next_check_at_ms, Some(next_check_after_success(NOW)));
|
||||
let first = &f.server.requests("/feed.xml")[0];
|
||||
assert!(!first.contains_key("if-none-match"), "{first:?}");
|
||||
assert!(
|
||||
first
|
||||
.get("user-agent")
|
||||
.is_some_and(|agent| agent.starts_with("furumi/")),
|
||||
"{first:?}"
|
||||
);
|
||||
|
||||
// Unchanged: 304, nothing re-parsed, validators sent.
|
||||
let url = rows[0].meta.feed_url.clone();
|
||||
let checked = refresh(&f.services, &report.podcast_id, &url, NOW + 1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(checked.not_modified);
|
||||
let second = &f.server.requests("/feed.xml")[1];
|
||||
assert_eq!(
|
||||
second.get("if-none-match").map(String::as_str),
|
||||
Some("\"v1\"")
|
||||
);
|
||||
assert_eq!(
|
||||
second.get("if-modified-since").map(String::as_str),
|
||||
Some("Sat, 15 Jun 2024 10:00:00 GMT")
|
||||
);
|
||||
|
||||
// Changed: a new episode arrives, the known ones are not duplicated.
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot (remastered)"), ("b", "Second"), ("c", "Third")]),
|
||||
"application/rss+xml",
|
||||
)
|
||||
.etag("\"v2\""),
|
||||
);
|
||||
let changed = refresh(&f.services, &report.podcast_id, &url, NOW + 2)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!changed.not_modified);
|
||||
assert_eq!(changed.new_episodes, 1);
|
||||
assert_eq!(changed.updated_episodes, 1);
|
||||
assert_eq!(episode_count(store, &report.podcast_id), 3);
|
||||
// A refresh never writes personal state: no new operation.
|
||||
assert_eq!(f.log.kinds(), ["podcast_subscription_set"]);
|
||||
|
||||
// Subscribing again is recognised without another fetch.
|
||||
let requests = f.server.requests("/feed.xml").len();
|
||||
let again = subscribe(&f.services, &f.server.url("/feed.xml"), NOW + 3)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(again.already_subscribed);
|
||||
assert_eq!(again.podcast_id, report.podcast_id);
|
||||
assert_eq!(f.server.requests("/feed.xml").len(), requests);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn permanent_redirect_becomes_an_alias_and_the_new_fetch_url() {
|
||||
let f = fixture();
|
||||
f.server
|
||||
.set("/old.xml", Route::redirect(301, &f.server.url("/new.xml")));
|
||||
f.server.set(
|
||||
"/new.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
let old = FeedUrl::parse(&f.server.url("/old.xml")).unwrap();
|
||||
let new = FeedUrl::parse(&f.server.url("/new.xml")).unwrap();
|
||||
let report = subscribe(&f.services, old.as_str(), NOW).await.unwrap();
|
||||
// The id comes from the URL of the first subscription.
|
||||
assert_eq!(report.podcast_id, PodcastId::from_feed_url(&old));
|
||||
assert_eq!(
|
||||
f.log.kinds(),
|
||||
["podcast_subscription_set", "podcast_feed_aliases_added"]
|
||||
);
|
||||
let store = &f.services.store;
|
||||
assert!(
|
||||
store
|
||||
.feed_aliases(&report.podcast_id)
|
||||
.unwrap()
|
||||
.contains(&new)
|
||||
);
|
||||
let summary = store.podcast(&report.podcast_id).unwrap().unwrap();
|
||||
assert_eq!(summary.meta.feed_url, new, "fetched from the new URL now");
|
||||
assert_eq!(store.resolve_podcast_id(&new).unwrap(), report.podcast_id);
|
||||
|
||||
// The next refresh goes straight to the new URL; no new alias op.
|
||||
let checked = refresh(&f.services, &report.podcast_id, &new, NOW + 1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(checked.moved_to.is_none());
|
||||
assert_eq!(f.server.requests("/old.xml").len(), 1);
|
||||
assert_eq!(f.log.kinds().len(), 2);
|
||||
|
||||
// Subscribing through the new URL finds the same podcast.
|
||||
let again = subscribe(&f.services, new.as_str(), NOW + 2).await.unwrap();
|
||||
assert!(again.already_subscribed);
|
||||
assert_eq!(again.podcast_id, report.podcast_id);
|
||||
}
|
||||
|
||||
/// A feed that moved (301) to a host that answers through a temporary
|
||||
/// redirect (302, a CDN or tracker) moves to the permanent target only; the
|
||||
/// following checks start there and are conditional again.
|
||||
#[tokio::test]
|
||||
async fn a_permanent_move_behind_a_temporary_redirect_still_moves_the_feed() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/old.xml",
|
||||
Route::redirect(301, &f.server.url("/moved.xml")),
|
||||
);
|
||||
f.server
|
||||
.set("/moved.xml", Route::redirect(302, "/cdn/feed.xml"));
|
||||
f.server.set(
|
||||
"/cdn/feed.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml").etag("\"v1\""),
|
||||
);
|
||||
let old = FeedUrl::parse(&f.server.url("/old.xml")).unwrap();
|
||||
let moved = FeedUrl::parse(&f.server.url("/moved.xml")).unwrap();
|
||||
let report = subscribe(&f.services, old.as_str(), NOW).await.unwrap();
|
||||
let store = &f.services.store;
|
||||
let summary = store.podcast(&report.podcast_id).unwrap().unwrap();
|
||||
assert_eq!(
|
||||
summary.meta.feed_url, moved,
|
||||
"the permanent target, not the CDN"
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.feed_aliases(&report.podcast_id)
|
||||
.unwrap()
|
||||
.contains(&moved)
|
||||
);
|
||||
// Relative enclosures resolve against the document's own address.
|
||||
let episodes = store
|
||||
.episodes(&EpisodeQuery::new(report.podcast_id.clone()))
|
||||
.unwrap();
|
||||
assert_eq!(episodes[0].episode.audio_url, f.server.url("/audio/a.mp3"));
|
||||
|
||||
// The next check starts at the new address and keeps its validators
|
||||
// through the temporary hop.
|
||||
let checked = refresh(&f.services, &report.podcast_id, &moved, NOW + 1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(checked.not_modified);
|
||||
assert_eq!(f.server.requests("/old.xml").len(), 1);
|
||||
let cdn = f.server.requests("/cdn/feed.xml");
|
||||
assert_eq!(cdn.len(), 2);
|
||||
assert_eq!(
|
||||
cdn[1].get("if-none-match").map(String::as_str),
|
||||
Some("\"v1\"")
|
||||
);
|
||||
|
||||
// A temporary hop first never moves the feed, whatever follows it.
|
||||
f.server.set(
|
||||
"/tracked.xml",
|
||||
Route::redirect(302, &f.server.url("/old.xml")),
|
||||
);
|
||||
let tracked = FeedUrl::parse(&f.server.url("/tracked.xml")).unwrap();
|
||||
let other = subscribe(&f.services, tracked.as_str(), NOW + 2)
|
||||
.await
|
||||
.unwrap();
|
||||
let summary = store.podcast(&other.podcast_id).unwrap().unwrap();
|
||||
assert_eq!(summary.meta.feed_url, tracked);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn temporary_redirect_keeps_the_feed_url() {
|
||||
let f = fixture();
|
||||
f.server
|
||||
.set("/tracked.xml", Route::redirect(302, "/real.xml"));
|
||||
f.server.set(
|
||||
"/real.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
let tracked = FeedUrl::parse(&f.server.url("/tracked.xml")).unwrap();
|
||||
let report = subscribe(&f.services, tracked.as_str(), NOW).await.unwrap();
|
||||
assert_eq!(f.log.kinds(), ["podcast_subscription_set"]);
|
||||
let summary = f
|
||||
.services
|
||||
.store
|
||||
.podcast(&report.podcast_id)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(summary.meta.feed_url, tracked);
|
||||
// Episode URLs resolve against where the document was served from.
|
||||
let episodes = f
|
||||
.services
|
||||
.store
|
||||
.episodes(&EpisodeQuery::new(report.podcast_id.clone()))
|
||||
.unwrap();
|
||||
assert_eq!(episodes[0].episode.audio_url, f.server.url("/audio/a.mp3"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn malformed_feed_and_server_errors_back_off_and_keep_the_catalog() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW)
|
||||
.await
|
||||
.unwrap();
|
||||
let id = report.podcast_id;
|
||||
let url = FeedUrl::parse(&f.server.url("/feed.xml")).unwrap();
|
||||
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new("<html><body>Moved to our app!</body></html>", "text/html"),
|
||||
);
|
||||
let err = refresh(&f.services, &id, &url, NOW + 1).await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("unusable feed"), "{err:#}");
|
||||
let store = &f.services.store;
|
||||
let state = store.fetch_state(&id).unwrap();
|
||||
assert_eq!(state.consecutive_failures, 1);
|
||||
assert!(
|
||||
state
|
||||
.last_error
|
||||
.as_deref()
|
||||
.unwrap()
|
||||
.contains("unusable feed")
|
||||
);
|
||||
assert_eq!(
|
||||
state.next_check_at_ms,
|
||||
Some(next_check_after_failure(NOW + 1, 1, None))
|
||||
);
|
||||
assert_eq!(episode_count(store, &id), 2, "an error keeps the catalog");
|
||||
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new("busy", "text/plain")
|
||||
.status(503)
|
||||
.retry_after("7200"),
|
||||
);
|
||||
let err = refresh(&f.services, &id, &url, NOW + 2).await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("HTTP 503"), "{err:#}");
|
||||
let state = store.fetch_state(&id).unwrap();
|
||||
assert_eq!(state.consecutive_failures, 2);
|
||||
assert_eq!(state.next_check_at_ms, Some(NOW + 2 + 7_200_000));
|
||||
assert_eq!(episode_count(store, &id), 2);
|
||||
// A failing feed is not due before its back-off expires.
|
||||
assert!(
|
||||
store
|
||||
.podcasts_due_for_refresh(NOW + 3, 10)
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
assert_eq!(
|
||||
store
|
||||
.podcasts_due_for_refresh(NOW + 2 + 7_200_000, 10)
|
||||
.unwrap()
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
|
||||
// Recovery clears the error.
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
refresh(&f.services, &id, &url, NOW + 3).await.unwrap();
|
||||
let state = store.fetch_state(&id).unwrap();
|
||||
assert_eq!(state.consecutive_failures, 0);
|
||||
assert!(state.last_error.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribing_to_a_non_feed_stores_nothing() {
|
||||
let f = fixture();
|
||||
f.server
|
||||
.set("/page", Route::new("<html>hello</html>", "text/html"));
|
||||
let err = subscribe(&f.services, &f.server.url("/page"), NOW)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("unusable feed"), "{err:#}");
|
||||
let err = subscribe(&f.services, &f.server.url("/missing"), NOW)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("HTTP 404"), "{err:#}");
|
||||
let err = subscribe(&f.services, "ftp://example.test/feed", NOW)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(!format!("{err:#}").is_empty());
|
||||
assert!(
|
||||
f.services
|
||||
.store
|
||||
.list_podcasts(PodcastListFilter::All)
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
assert!(f.log.kinds().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unsubscribe_then_resubscribe_keeps_the_podcast_id() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
let first = subscribe(&f.services, &f.server.url("/feed.xml"), NOW)
|
||||
.await
|
||||
.unwrap();
|
||||
let store = Arc::clone(&f.services.store);
|
||||
let id = first.podcast_id.clone();
|
||||
f.log
|
||||
.record(&mut |stamps| store.unsubscribe(stamps, &id))
|
||||
.unwrap();
|
||||
assert!(
|
||||
store
|
||||
.list_podcasts(PodcastListFilter::Subscribed)
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
let again = subscribe(&f.services, &f.server.url("/feed.xml"), NOW + 1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!again.already_subscribed);
|
||||
assert_eq!(again.podcast_id, first.podcast_id);
|
||||
assert_eq!(
|
||||
f.log.kinds(),
|
||||
[
|
||||
"podcast_subscription_set",
|
||||
"podcast_subscription_set",
|
||||
"podcast_share_set",
|
||||
"podcast_subscription_set"
|
||||
]
|
||||
);
|
||||
let ops = f.log.ops.lock().unwrap();
|
||||
assert!(matches!(
|
||||
ops.last().map(|op| &op.payload),
|
||||
Some(SyncOpPayload::PodcastSubscriptionSet {
|
||||
subscribed: true,
|
||||
..
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn back_off_doubles_and_honours_retry_after() {
|
||||
let base = FAILURE_BACKOFF_BASE.as_millis() as i64;
|
||||
assert_eq!(next_check_after_failure(0, 1, None), base);
|
||||
assert_eq!(next_check_after_failure(0, 2, None), 2 * base);
|
||||
assert_eq!(next_check_after_failure(0, 4, None), 8 * base);
|
||||
let max = FAILURE_BACKOFF_MAX.as_millis() as i64;
|
||||
assert_eq!(next_check_after_failure(0, 40, None), max);
|
||||
assert_eq!(
|
||||
next_check_after_failure(0, 1, Some(Duration::from_secs(3_600))),
|
||||
3_600_000
|
||||
);
|
||||
assert_eq!(
|
||||
next_check_after_failure(0, 1, Some(Duration::from_secs(10 * 86_400))),
|
||||
max
|
||||
);
|
||||
assert_eq!(
|
||||
next_check_after_success(0),
|
||||
REFRESH_INTERVAL.as_millis() as i64
|
||||
);
|
||||
}
|
||||
|
||||
/// The prototype podcast build's schema (user_version 1), verbatim.
|
||||
const PROTOTYPE_SCHEMA: &str = "
|
||||
CREATE TABLE IF NOT EXISTS podcasts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
feed_url TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL,
|
||||
author TEXT,
|
||||
description TEXT,
|
||||
link TEXT,
|
||||
image_url TEXT,
|
||||
cover_url TEXT,
|
||||
cover_path TEXT,
|
||||
etag TEXT,
|
||||
last_modified TEXT,
|
||||
added_at INTEGER NOT NULL,
|
||||
last_checked_at INTEGER,
|
||||
last_error TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS podcast_episodes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
podcast_id INTEGER NOT NULL REFERENCES podcasts(id) ON DELETE CASCADE,
|
||||
guid TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
published_at INTEGER,
|
||||
duration_seconds REAL,
|
||||
audio_url TEXT NOT NULL,
|
||||
mime_type TEXT,
|
||||
byte_len INTEGER,
|
||||
link TEXT,
|
||||
image_url TEXT,
|
||||
first_seen_at INTEGER NOT NULL,
|
||||
UNIQUE (podcast_id, guid)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS podcast_episodes_by_date
|
||||
ON podcast_episodes (podcast_id, published_at DESC, id DESC);
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn prototype_database_is_migrated_in_place() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE);
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
conn.execute_batch(PROTOTYPE_SCHEMA).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcasts (feed_url, title, etag, added_at, last_checked_at)
|
||||
VALUES ('https://example.test/feed.xml', 'Old Show', '\"e1\"', 1700000000, 1700000100)",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcast_episodes
|
||||
(podcast_id, guid, title, published_at, duration_seconds, audio_url, first_seen_at)
|
||||
VALUES (1, 'ep-1', 'Old Pilot', 1700000000, 1800.0,
|
||||
'https://example.test/1.mp3', 1700000000)",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
conn.pragma_update(None, "user_version", 1).unwrap();
|
||||
}
|
||||
let services = super::open_services(
|
||||
&path,
|
||||
dir.path().join("covers"),
|
||||
Arc::new(MemoryLog::default()),
|
||||
)
|
||||
.unwrap();
|
||||
let store = &services.store;
|
||||
assert_eq!(
|
||||
store.schema_version().unwrap(),
|
||||
furumi_library::podcasts::PODCAST_SCHEMA_VERSION
|
||||
);
|
||||
let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap();
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert_eq!(rows[0].meta.title, "Old Show");
|
||||
assert_eq!(rows[0].episode_count, 1);
|
||||
let expected =
|
||||
PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/feed.xml").unwrap());
|
||||
assert_eq!(rows[0].podcast_id, expected);
|
||||
// The prototype's validators are dropped: the first check parses the
|
||||
// feed (`imported_podcasts_get_their_seasons_on_the_first_check`).
|
||||
let fetch = store.fetch_state(&expected).unwrap();
|
||||
assert_eq!(fetch.etag, None);
|
||||
assert_eq!(fetch.last_success_at_ms, Some(1_700_000_100_000));
|
||||
assert_eq!(fetch.next_check_at_ms, None, "due at once");
|
||||
drop(services);
|
||||
|
||||
// The original rows are kept, renamed.
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
let legacy: i64 = conn
|
||||
.query_row("SELECT COUNT(*) FROM legacy_podcast_episodes", [], |row| {
|
||||
row.get(0)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(legacy, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_or_newer_databases_are_refused_unchanged() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let log: Arc<dyn OpLog> = Arc::new(MemoryLog::default());
|
||||
|
||||
let foreign = dir.path().join("foreign.sqlite3");
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&foreign).unwrap();
|
||||
conn.execute_batch("CREATE TABLE tracks (id INTEGER PRIMARY KEY, title TEXT);")
|
||||
.unwrap();
|
||||
}
|
||||
let newer = dir.path().join("newer.sqlite3");
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&newer).unwrap();
|
||||
conn.pragma_update(None, "user_version", 99).unwrap();
|
||||
}
|
||||
let garbage = dir.path().join("garbage.sqlite3");
|
||||
std::fs::write(
|
||||
&garbage,
|
||||
b"this is not a database at all, just some text bytes",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for (path, expected) in [
|
||||
(&foreign, "not a Furumi podcast database"),
|
||||
(&newer, "newer than this Furumi supports"),
|
||||
(&garbage, ""),
|
||||
] {
|
||||
let before = std::fs::read(path).unwrap();
|
||||
let Err(err) = super::open_services(path, dir.path().join("covers"), Arc::clone(&log))
|
||||
else {
|
||||
panic!("{} opened", path.display());
|
||||
};
|
||||
let message = super::describe_open_error(&err);
|
||||
assert!(message.contains(expected), "{message}");
|
||||
assert_eq!(std::fs::read(path).unwrap(), before, "{}", path.display());
|
||||
}
|
||||
}
|
||||
|
||||
/// A prototype database knew no seasons or episode numbers. Its stored
|
||||
/// `ETag` would make the first check "304 Not Modified" forever for a feed
|
||||
/// that no longer changes, so the seasons would never appear.
|
||||
#[tokio::test]
|
||||
async fn imported_podcasts_get_their_seasons_on_the_first_check() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let server = TestServer::start();
|
||||
let feed = "<?xml version=\"1.0\"?><rss version=\"2.0\" \
|
||||
xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\"><channel>\
|
||||
<title>Old Show</title><item><title>Old Pilot</title><guid>ep-1</guid>\
|
||||
<pubDate>Sat, 15 Jun 2024 10:00:00 GMT</pubDate>\
|
||||
<itunes:season>1</itunes:season><itunes:episode>1</itunes:episode>\
|
||||
<enclosure url=\"/audio/1.mp3\" type=\"audio/mpeg\" length=\"100\"/></item>\
|
||||
</channel></rss>";
|
||||
// The server answers the prototype's validator with 304.
|
||||
server.set(
|
||||
"/feed.xml",
|
||||
Route::new(feed, "application/rss+xml").etag("\"e1\""),
|
||||
);
|
||||
let feed_url = server.url("/feed.xml");
|
||||
let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE);
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
conn.execute_batch(PROTOTYPE_SCHEMA).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcasts (feed_url, title, etag, last_modified, added_at, last_checked_at)
|
||||
VALUES (?1, 'Old Show', '\"e1\"', 'Sat, 15 Jun 2024 10:00:00 GMT',
|
||||
1700000000, 1700000100)",
|
||||
[&feed_url],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcast_episodes
|
||||
(podcast_id, guid, title, published_at, duration_seconds, audio_url, first_seen_at)
|
||||
VALUES (1, 'ep-1', 'Old Pilot', 1718445600, 1800.0, ?1, 1700000000)",
|
||||
[server.url("/audio/1.mp3")],
|
||||
)
|
||||
.unwrap();
|
||||
conn.pragma_update(None, "user_version", 1).unwrap();
|
||||
}
|
||||
let services = super::open_services(
|
||||
&path,
|
||||
dir.path().join("covers"),
|
||||
Arc::new(MemoryLog::default()),
|
||||
)
|
||||
.unwrap();
|
||||
let store = Arc::clone(&services.store);
|
||||
let id = PodcastId::from_feed_url(&FeedUrl::parse(&feed_url).unwrap());
|
||||
assert!(
|
||||
store
|
||||
.seasons(&id)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|s| s.season.is_none()),
|
||||
"the prototype had no seasons"
|
||||
);
|
||||
let due = store.podcasts_due_for_refresh(NOW, 16).unwrap();
|
||||
assert_eq!(due.len(), 1);
|
||||
let report = refresh(&services, &id, &due[0].1, NOW).await.unwrap();
|
||||
assert!(!report.not_modified);
|
||||
let requests = server.requests("/feed.xml");
|
||||
assert_eq!(requests.len(), 1);
|
||||
assert!(!requests[0].contains_key("if-none-match"));
|
||||
assert!(!requests[0].contains_key("if-modified-since"));
|
||||
let seasons = store.seasons(&id).unwrap();
|
||||
assert_eq!(
|
||||
seasons.iter().map(|s| s.season).collect::<Vec<_>>(),
|
||||
[Some(1)]
|
||||
);
|
||||
// From then on the checks are conditional again.
|
||||
let report = refresh(&services, &id, &due[0].1, NOW + 1).await.unwrap();
|
||||
assert!(report.not_modified);
|
||||
drop(services);
|
||||
|
||||
// A store that was already migrated keeps its validators on reopen.
|
||||
let services = super::open_services(
|
||||
&path,
|
||||
dir.path().join("covers"),
|
||||
Arc::new(MemoryLog::default()),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
services.store.fetch_state(&id).unwrap().etag.as_deref(),
|
||||
Some("\"e1\"")
|
||||
);
|
||||
}
|
||||
|
||||
/// The import commits inside `PodcastStore::open` and the validators are
|
||||
/// dropped afterwards. A start that stopped in between (killed, or a failed
|
||||
/// write) finishes the job at the next start, and the imported back-off is
|
||||
/// kept.
|
||||
#[test]
|
||||
fn dropping_prototype_validators_resumes_after_an_interrupted_start() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join(furumi_library::podcasts::PODCAST_DB_FILE);
|
||||
{
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
conn.execute_batch(PROTOTYPE_SCHEMA).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcasts (feed_url, title, etag, added_at, last_checked_at)
|
||||
VALUES ('https://example.test/ok.xml', 'Fine Show', '\"e1\"', 1700000000, 1700000100)",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO podcasts
|
||||
(feed_url, title, last_modified, added_at, last_checked_at, last_error)
|
||||
VALUES ('https://example.test/broken.xml', 'Broken Show',
|
||||
'Sat, 15 Jun 2024 10:00:00 GMT', 1700000000, 1700000200, 'HTTP 500')",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
conn.pragma_update(None, "user_version", 1).unwrap();
|
||||
}
|
||||
let ok = PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/ok.xml").unwrap());
|
||||
let broken =
|
||||
PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/broken.xml").unwrap());
|
||||
|
||||
// The first start: the marker is written, then the import commits, and
|
||||
// the process stops before the validators are dropped.
|
||||
let marker = super::validators_marker(&path);
|
||||
assert!(super::is_prototype_database(&path));
|
||||
std::fs::write(&marker, b"").unwrap();
|
||||
drop(PodcastStore::open(&path).unwrap());
|
||||
assert!(!super::is_prototype_database(&path), "migrated");
|
||||
|
||||
// The next start finishes the job.
|
||||
let services = super::open_services(
|
||||
&path,
|
||||
dir.path().join("covers"),
|
||||
Arc::new(MemoryLog::default()),
|
||||
)
|
||||
.unwrap();
|
||||
let store = &services.store;
|
||||
let fetch = store.fetch_state(&ok).unwrap();
|
||||
assert_eq!(fetch.etag, None);
|
||||
assert_eq!(fetch.last_error, None);
|
||||
let fetch = store.fetch_state(&broken).unwrap();
|
||||
assert_eq!(fetch.last_modified, None);
|
||||
assert_eq!(fetch.last_error.as_deref(), Some("HTTP 500"));
|
||||
assert_eq!(
|
||||
fetch.consecutive_failures, 1,
|
||||
"the imported back-off is kept"
|
||||
);
|
||||
assert_eq!(fetch.checked_at_ms, Some(1_700_000_200_000));
|
||||
assert!(!marker.exists(), "done once");
|
||||
|
||||
// Validators of later checks are kept on every start after that.
|
||||
store
|
||||
.record_fetch_success(&ok, NOW, Some("\"e2\""), None, None)
|
||||
.unwrap();
|
||||
drop(services);
|
||||
let services = super::open_services(
|
||||
&path,
|
||||
dir.path().join("covers"),
|
||||
Arc::new(MemoryLog::default()),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
services.store.fetch_state(&ok).unwrap().etag.as_deref(),
|
||||
Some("\"e2\"")
|
||||
);
|
||||
}
|
||||
|
||||
fn feed_url(f: &Fixture, path: &str) -> FeedUrl {
|
||||
FeedUrl::parse(&f.server.url(path)).unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn feeds_over_the_size_limit_are_refused_with_or_without_a_length() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
let url = feed_url(&f, "/feed.xml");
|
||||
let id = subscribe(&f.services, url.as_str(), NOW)
|
||||
.await
|
||||
.unwrap()
|
||||
.podcast_id;
|
||||
let mut huge = rss(&[("a", "Pilot"), ("b", "Second")]);
|
||||
huge.resize(MAX_FEED_BYTES + 1, b' ');
|
||||
let store = &f.services.store;
|
||||
for (attempt, route) in [
|
||||
Route::new(huge.clone(), "application/rss+xml"),
|
||||
Route::new(huge, "application/rss+xml").no_length(),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let failures = attempt as u32 + 1;
|
||||
f.server.set("/feed.xml", route);
|
||||
let now = NOW + i64::from(failures);
|
||||
let err = refresh(&f.services, &id, &url, now).await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("too large"), "{err:#}");
|
||||
let state = store.fetch_state(&id).unwrap();
|
||||
assert_eq!(state.consecutive_failures, failures);
|
||||
assert!(state.last_error.unwrap().contains("too large"));
|
||||
assert_eq!(
|
||||
state.next_check_at_ms,
|
||||
Some(next_check_after_failure(now, failures, None))
|
||||
);
|
||||
assert_eq!(episode_count(store, &id), 1, "the catalog is kept");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn redirect_chains_are_bounded_and_lead_only_to_http() {
|
||||
let f = fixture();
|
||||
let fetch = |path: &'static str| {
|
||||
let url = feed_url(&f, path);
|
||||
let fetcher = std::sync::Arc::clone(&f.services.fetcher);
|
||||
async move { fetcher.fetch_feed(&url, Validators::default()).await }
|
||||
};
|
||||
for n in 0..=MAX_REDIRECTS {
|
||||
f.server.set(
|
||||
&format!("/r{n}.xml"),
|
||||
Route::redirect(302, &format!("/r{}.xml", n + 1)),
|
||||
);
|
||||
}
|
||||
f.server.set(
|
||||
&format!("/r{}.xml", MAX_REDIRECTS + 1),
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
// MAX_REDIRECTS hops are followed, one more is refused.
|
||||
let followed = fetch("/r1.xml").await.unwrap();
|
||||
assert_eq!(followed.redirects, MAX_REDIRECTS);
|
||||
let err = fetch("/r0.xml").await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("too many redirects"), "{err:#}");
|
||||
// A loop ends the same way.
|
||||
f.server.set("/a.xml", Route::redirect(301, "/b.xml"));
|
||||
f.server.set("/b.xml", Route::redirect(301, "/a.xml"));
|
||||
let err = fetch("/a.xml").await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("too many redirects"), "{err:#}");
|
||||
assert_eq!(f.server.requests("/a.xml").len(), MAX_REDIRECTS / 2 + 1);
|
||||
// Only http(s) is followed.
|
||||
f.server.set(
|
||||
"/ftp.xml",
|
||||
Route::redirect(302, "ftp://example.test/feed.xml"),
|
||||
);
|
||||
let err = fetch("/ftp.xml").await.unwrap_err();
|
||||
assert!(
|
||||
format!("{err:#}").contains("unsupported URL scheme"),
|
||||
"{err:#}"
|
||||
);
|
||||
// Subscribing through any of them stores nothing.
|
||||
for path in ["/r0.xml", "/a.xml", "/ftp.xml"] {
|
||||
assert!(
|
||||
subscribe(&f.services, &f.server.url(path), NOW)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
f.services
|
||||
.store
|
||||
.list_podcasts(PodcastListFilter::All)
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_hanging_server_fails_the_check_at_the_deadline() {
|
||||
let f = fixture();
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"),
|
||||
);
|
||||
let url = feed_url(&f, "/feed.xml");
|
||||
let id = subscribe(&f.services, url.as_str(), NOW)
|
||||
.await
|
||||
.unwrap()
|
||||
.podcast_id;
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(rss(&[("a", "Pilot")]), "application/rss+xml")
|
||||
.stall(FETCH_DEADLINE + Duration::from_secs(5)),
|
||||
);
|
||||
let started = std::time::Instant::now();
|
||||
let err = refresh(&f.services, &id, &url, NOW + 1).await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("took longer than"), "{err:#}");
|
||||
assert!(started.elapsed() < FETCH_DEADLINE + Duration::from_secs(2));
|
||||
let state = f.services.store.fetch_state(&id).unwrap();
|
||||
assert_eq!(state.consecutive_failures, 1);
|
||||
assert_eq!(episode_count(&f.services.store, &id), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn covers_must_be_images_within_the_limit_and_are_written_whole() {
|
||||
let f = fixture();
|
||||
let png = {
|
||||
let mut out = std::io::Cursor::new(Vec::new());
|
||||
image::RgbImage::new(2, 2)
|
||||
.write_to(&mut out, image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
out.into_inner()
|
||||
};
|
||||
f.server
|
||||
.set("/cover.png", Route::new(png.clone(), "image/png"));
|
||||
f.server.set(
|
||||
"/page.png",
|
||||
Route::new("<html>not an image</html>", "image/png"),
|
||||
);
|
||||
f.server.set(
|
||||
"/huge.png",
|
||||
Route::new(vec![0u8; MAX_COVER_BYTES + 1], "image/png"),
|
||||
);
|
||||
let id = PodcastId::from_feed_url(&feed_url(&f, "/feed.xml"));
|
||||
let fetcher = &f.services.fetcher;
|
||||
let path = fetcher
|
||||
.fetch_cover(&id, &f.server.url("/cover.png"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), png);
|
||||
assert_eq!(path.extension().unwrap(), "png");
|
||||
let err = fetcher
|
||||
.fetch_cover(&id, &f.server.url("/page.png"))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
format!("{err:#}").contains("not a supported image"),
|
||||
"{err:#}"
|
||||
);
|
||||
let err = fetcher
|
||||
.fetch_cover(&id, &f.server.url("/huge.png"))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("too large"), "{err:#}");
|
||||
// Only the one finished file: nothing partial, no temporary files.
|
||||
let files: Vec<_> = std::fs::read_dir(path.parent().unwrap())
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.collect();
|
||||
assert_eq!(files, [path]);
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
use crate::library::models::TrackItem;
|
||||
|
||||
/// Content id of a music track. Podcast episodes have none: they are not
|
||||
/// content-addressed and never shared as `frid://` track links.
|
||||
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||
if track.is_podcast() {
|
||||
return None;
|
||||
}
|
||||
track
|
||||
.fed
|
||||
.as_ref()
|
||||
@@ -14,6 +19,9 @@ pub fn track_can_share(track: &TrackItem) -> bool {
|
||||
}
|
||||
|
||||
pub fn track_share_link(track: &TrackItem) -> Option<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))
|
||||
@@ -154,6 +162,7 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
@@ -231,6 +240,7 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
let link = track_share_link(&track).expect("link");
|
||||
let parsed = parse_frid_link(&link).expect("parses back");
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
//! Secondary settings window; child dialogs are rendered above it.
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
text::Line,
|
||||
widgets::{Block, Clear, List, ListItem, ListState, Paragraph, Wrap},
|
||||
};
|
||||
|
||||
use super::theme;
|
||||
use crate::app::state::{AppState, SettingsRow, additional_settings_rows};
|
||||
|
||||
pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
let screen = frame.area();
|
||||
let width = screen.width.saturating_sub(2).min(90);
|
||||
let height = screen.height.saturating_sub(2).min(26);
|
||||
let area = Rect::new(
|
||||
screen.x + (screen.width - width) / 2,
|
||||
screen.y + (screen.height - height) / 2,
|
||||
width,
|
||||
height,
|
||||
);
|
||||
let block = Block::bordered()
|
||||
.title(" Additional settings ")
|
||||
.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 [list_area, status_area, footer] = Layout::vertical([
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(inner);
|
||||
|
||||
let rows = additional_settings_rows(state);
|
||||
let mut items = Vec::new();
|
||||
let mut selected = 0;
|
||||
for (index, row) in rows.iter().enumerate() {
|
||||
let section = match row {
|
||||
SettingsRow::MusicDirectory => Some("Library"),
|
||||
SettingsRow::CheckUpdate => Some("Updates"),
|
||||
SettingsRow::VisualizationClock => Some("Visualizations"),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(section) = section {
|
||||
items.push(ListItem::new(Line::styled(
|
||||
section,
|
||||
theme::header_for(state),
|
||||
)));
|
||||
}
|
||||
if index
|
||||
== state
|
||||
.additional_settings_cursor
|
||||
.min(rows.len().saturating_sub(1))
|
||||
{
|
||||
selected = items.len();
|
||||
}
|
||||
let (label, value, enabled) = match row {
|
||||
SettingsRow::MusicDirectory => (
|
||||
"Music save directory".into(),
|
||||
if state.music_dir_changing {
|
||||
"checking/changing...".into()
|
||||
} else {
|
||||
state.music_dir.to_string_lossy().into_owned()
|
||||
},
|
||||
!state.music_dir_changing,
|
||||
),
|
||||
SettingsRow::CheckUpdate => (
|
||||
"Check for updates".into(),
|
||||
format!("v{}", env!("CARGO_PKG_VERSION")),
|
||||
!state.updater.busy && !state.updater.installed,
|
||||
),
|
||||
SettingsRow::InstallUpdate => (
|
||||
"Install update".into(),
|
||||
state
|
||||
.updater
|
||||
.available
|
||||
.as_ref()
|
||||
.map(|u| format!("v{}", u.version))
|
||||
.unwrap_or_else(|| "check for updates first".into()),
|
||||
state.updater.available.is_some()
|
||||
&& !state.updater.busy
|
||||
&& !state.updater.installed,
|
||||
),
|
||||
SettingsRow::VisualizationClock => (
|
||||
"Show clock".into(),
|
||||
if state.visualizer.config.show_clock {
|
||||
"on"
|
||||
} else {
|
||||
"off"
|
||||
}
|
||||
.into(),
|
||||
true,
|
||||
),
|
||||
SettingsRow::VisualizationScript(index) => {
|
||||
let script = &state.visualizer.scripts[*index];
|
||||
let mark = if state.visualizer.selected_script_index() == Some(*index) {
|
||||
"* "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
(
|
||||
format!("{mark}{}", script.name),
|
||||
script
|
||||
.path
|
||||
.file_name()
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
.unwrap_or_default(),
|
||||
true,
|
||||
)
|
||||
}
|
||||
SettingsRow::VisualizationNew => {
|
||||
("+ New visualization script".into(), "enter".into(), true)
|
||||
}
|
||||
SettingsRow::VisualizationEdit => {
|
||||
("Edit selected visualization".into(), "enter".into(), true)
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
let item = ListItem::new(format!("{label}: {value}"));
|
||||
items.push(if enabled {
|
||||
item
|
||||
} else {
|
||||
item.style(theme::dim())
|
||||
});
|
||||
}
|
||||
frame.render_stateful_widget(
|
||||
List::new(items)
|
||||
.highlight_symbol("> ")
|
||||
.highlight_style(theme::selection_for(state)),
|
||||
list_area,
|
||||
&mut ListState::default().with_selected(Some(selected)),
|
||||
);
|
||||
let message = state
|
||||
.status_message
|
||||
.as_deref()
|
||||
.filter(|message| !message.is_empty())
|
||||
.unwrap_or(&state.updater.message);
|
||||
frame.render_widget(
|
||||
Paragraph::new(message)
|
||||
.wrap(Wrap { trim: true })
|
||||
.style(theme::dim()),
|
||||
status_area,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new("Up/Down: navigate | Enter: select | Esc: back").style(theme::dim()),
|
||||
footer,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn window_renders_controls_and_scrolls_to_last_row_in_small_terminal() {
|
||||
for (width, height) in [(80, 30), (45, 14)] {
|
||||
let mut terminal =
|
||||
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
|
||||
let mut state = AppState::default();
|
||||
state.updater.message = "No newer stable release".into();
|
||||
terminal.draw(|frame| draw(frame, &state)).unwrap();
|
||||
let text: String = terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.iter()
|
||||
.map(|c| c.symbol())
|
||||
.collect();
|
||||
assert!(text.contains("Additional settings"));
|
||||
assert!(text.contains("Music save directory"));
|
||||
if height >= 30 {
|
||||
assert!(text.contains("Check for updates"));
|
||||
assert!(text.contains("Install update"));
|
||||
assert!(text.contains("Visualizations"));
|
||||
}
|
||||
state.additional_settings_cursor = additional_settings_rows(&state).len() - 1;
|
||||
terminal.draw(|frame| draw(frame, &state)).unwrap();
|
||||
let text: String = terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.iter()
|
||||
.map(|c| c.symbol())
|
||||
.collect();
|
||||
assert!(text.contains("New visualization script"));
|
||||
assert!(text.contains("No newer stable release"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+99
-121
@@ -33,7 +33,7 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
}
|
||||
|
||||
let rows_height =
|
||||
(settings_rows(state).len() + 10 + device_presence_sections(state).len()) as u16;
|
||||
(settings_rows(state).len() + 6 + device_presence_sections(state).len()) as u16;
|
||||
let [rows_area, _, status_area] = Layout::vertical([
|
||||
Constraint::Length(rows_height.min(inner.height)),
|
||||
Constraint::Length(1),
|
||||
@@ -69,7 +69,6 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let mut y = area.y;
|
||||
let mut cursor = 0usize;
|
||||
|
||||
draw_section(frame, area, state, &mut y, "Library");
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
@@ -77,56 +76,22 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"Music save directory",
|
||||
if state.music_dir_changing {
|
||||
format!("{} checking/changing…", state.spinner())
|
||||
} else {
|
||||
state.music_dir.to_string_lossy().into_owned()
|
||||
},
|
||||
"Additional settings",
|
||||
"enter".into(),
|
||||
);
|
||||
cursor += 1;
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"Full status details",
|
||||
"enter".to_string(),
|
||||
);
|
||||
|
||||
y = y.saturating_add(1);
|
||||
|
||||
draw_section(frame, area, state, &mut y, "Similarity Search");
|
||||
let similarity = &state.similarity.settings;
|
||||
for row in SimilarityRow::ALL {
|
||||
let (label, value) = match row {
|
||||
SimilarityRow::Toggle => ("Similarity search", on_off(similarity.enabled).to_string()),
|
||||
SimilarityRow::Model => (
|
||||
"Embedding model",
|
||||
crate::similarity::model_by_id(&similarity.model)
|
||||
.map(|model| format!("{} · {}", model.id, model.license))
|
||||
.unwrap_or_else(|| similarity.model.clone()),
|
||||
),
|
||||
SimilarityRow::Profile => (
|
||||
"Preprocessing profile",
|
||||
format!("{} (enter for details)", similarity.profile),
|
||||
),
|
||||
SimilarityRow::MinimumScore => (
|
||||
"Minimum similarity",
|
||||
format!("{:.2}", similarity.minimum_score),
|
||||
),
|
||||
SimilarityRow::MaxTracksPerArtist => (
|
||||
"Tracks per artist",
|
||||
similarity.max_tracks_per_artist.to_string(),
|
||||
),
|
||||
SimilarityRow::Workers => ("Background workers", similarity.workers.to_string()),
|
||||
SimilarityRow::Clear => ("Clear all stored embeddings", "↵".to_string()),
|
||||
};
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
label,
|
||||
value,
|
||||
);
|
||||
cursor += 1;
|
||||
}
|
||||
|
||||
cursor += 1;
|
||||
y = y.saturating_add(1);
|
||||
|
||||
draw_section(frame, area, state, &mut y, "Federation");
|
||||
@@ -314,39 +279,32 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
}
|
||||
|
||||
y = y.saturating_add(1);
|
||||
draw_section(frame, area, state, &mut y, "Visualizations");
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"Show clock",
|
||||
if state.visualizer.config.show_clock {
|
||||
"[x]".to_string()
|
||||
} else {
|
||||
"[ ]".to_string()
|
||||
},
|
||||
);
|
||||
cursor += 1;
|
||||
|
||||
for (index, script) in state.visualizer.scripts.iter().enumerate() {
|
||||
let selected_script = state
|
||||
.visualizer
|
||||
.selected_script_index()
|
||||
.is_some_and(|selected| selected == index);
|
||||
let label = if selected_script {
|
||||
format!("* {}", script.name)
|
||||
} else {
|
||||
format!(" {}", script.name)
|
||||
draw_section(frame, area, state, &mut y, "Similarity Search");
|
||||
let similarity = &state.similarity.settings;
|
||||
for row in SimilarityRow::ALL {
|
||||
let (label, value) = match row {
|
||||
SimilarityRow::Toggle => ("Similarity search", on_off(similarity.enabled).to_string()),
|
||||
SimilarityRow::Model => (
|
||||
"Embedding model",
|
||||
crate::similarity::model_by_id(&similarity.model)
|
||||
.map(|model| format!("{} · {}", model.id, model.license))
|
||||
.unwrap_or_else(|| similarity.model.clone()),
|
||||
),
|
||||
SimilarityRow::Profile => (
|
||||
"Preprocessing profile",
|
||||
format!("{} (enter for details)", similarity.profile),
|
||||
),
|
||||
SimilarityRow::MinimumScore => (
|
||||
"Minimum similarity",
|
||||
format!("{:.2}", similarity.minimum_score),
|
||||
),
|
||||
SimilarityRow::MaxTracksPerArtist => (
|
||||
"Tracks per artist",
|
||||
similarity.max_tracks_per_artist.to_string(),
|
||||
),
|
||||
SimilarityRow::Workers => ("Background workers", similarity.workers.to_string()),
|
||||
SimilarityRow::Clear => ("Clear all stored embeddings", "↵".to_string()),
|
||||
};
|
||||
let value = script
|
||||
.path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
@@ -354,49 +312,11 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
&label,
|
||||
label,
|
||||
value,
|
||||
);
|
||||
cursor += 1;
|
||||
}
|
||||
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"+ New visualization script",
|
||||
"↵".to_string(),
|
||||
);
|
||||
cursor += 1;
|
||||
|
||||
if state.visualizer.selected_script().is_some() {
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"Edit selected visualization",
|
||||
"↵".to_string(),
|
||||
);
|
||||
cursor += 1;
|
||||
}
|
||||
|
||||
y = y.saturating_add(1);
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
&mut y,
|
||||
cursor,
|
||||
state.settings_cursor,
|
||||
"Full status details",
|
||||
"enter".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
fn protocol_label(id: &str) -> &str {
|
||||
@@ -961,7 +881,10 @@ fn node_summary_lines(state: &AppState) -> Vec<Line<'static>> {
|
||||
]
|
||||
}
|
||||
Some(status) => vec![
|
||||
summary_line("Node", format!("running on {}", status.network)),
|
||||
summary_line(
|
||||
"Node",
|
||||
format!("running on {} · {}", status.network, status.network_health),
|
||||
),
|
||||
summary_line(
|
||||
"Peers",
|
||||
format!(
|
||||
@@ -1215,6 +1138,17 @@ fn status_detail_status_lines(state: &AppState, status_cursor: usize) -> Vec<Lin
|
||||
status.known_contacts
|
||||
),
|
||||
));
|
||||
lines.push(status_line(
|
||||
"Discovery",
|
||||
format!(
|
||||
"{} · {} DHT / {} dial failures · {} rebuilds · {} full recoveries",
|
||||
status.network_health,
|
||||
status.rendezvous_failures,
|
||||
status.peer_dial_failures,
|
||||
status.rendezvous_restarts,
|
||||
status.recovery_count
|
||||
),
|
||||
));
|
||||
if !status.connected_peers.is_empty() {
|
||||
let mut peers: Vec<String> = status
|
||||
.connected_peers
|
||||
@@ -1260,6 +1194,9 @@ fn status_detail_status_lines(state: &AppState, status_cursor: usize) -> Vec<Lin
|
||||
if let Some(error) = &status.last_error {
|
||||
lines.push(status_line("Error", first_line(error)));
|
||||
}
|
||||
if let Some(error) = &status.last_rendezvous_error {
|
||||
lines.push(status_line("Discovery error", first_line(error)));
|
||||
}
|
||||
push_transport_summary_status(&mut lines, state, status);
|
||||
}
|
||||
}
|
||||
@@ -1561,3 +1498,44 @@ fn relative_time_label(value_ms: Option<i64>, now_ms: i64) -> String {
|
||||
format!("{}d ago", seconds / 60 / 60 / 24)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod update_ui_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn update_controls_and_existing_sections_render_in_both_layouts() {
|
||||
for width in [80, 160] {
|
||||
let mut terminal =
|
||||
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, 55)).unwrap();
|
||||
let mut state = AppState::default();
|
||||
state.updater.message = "No newer stable release".into();
|
||||
terminal
|
||||
.draw(|frame| draw(frame, frame.area(), &state))
|
||||
.unwrap();
|
||||
let text: String = terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.iter()
|
||||
.map(|cell| cell.symbol())
|
||||
.collect();
|
||||
let mut previous = 0;
|
||||
for expected in [
|
||||
"Additional settings",
|
||||
"Full status details",
|
||||
"Federation",
|
||||
"Connected Devices",
|
||||
"Similarity Search",
|
||||
] {
|
||||
assert!(
|
||||
text.contains(expected),
|
||||
"missing {expected} at width {width}"
|
||||
);
|
||||
let position = text.find(expected).unwrap();
|
||||
assert!(position >= previous, "incorrect order for {expected}");
|
||||
previous = position;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+111
-25
@@ -537,6 +537,17 @@ fn draw_artist(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor:
|
||||
about.push_str(&format!(" · appears on {}", detail.featured_tracks.len()));
|
||||
}
|
||||
let federation_line = match state.artist_fed_views.get(&id) {
|
||||
_ if state.federation.settings.enabled
|
||||
&& !state.global.filters.source_mode.includes_network() =>
|
||||
{
|
||||
Some(Line::styled(
|
||||
"federation: hidden by Local filter",
|
||||
theme::dim(),
|
||||
))
|
||||
}
|
||||
_ if state.artist_federation_loading(id) => {
|
||||
Some(super::loading_line(state, "searching peers…"))
|
||||
}
|
||||
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
|
||||
Some(Loadable::Ready(card)) => {
|
||||
let tracks: usize = card
|
||||
@@ -837,29 +848,58 @@ fn draw_release(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor
|
||||
|
||||
let artists: Vec<&str> = detail.artists.iter().map(|a| a.name.as_str()).collect();
|
||||
let year = detail.year.map(|y| format!(" · {y}")).unwrap_or_default();
|
||||
let info = vec![
|
||||
let tracks = crate::app::state::release_tracks(state, id, detail);
|
||||
let federation_line = match state.release_fed_views.get(&id) {
|
||||
_ if state.federation.settings.enabled
|
||||
&& !state.global.filters.source_mode.includes_network() =>
|
||||
{
|
||||
Some(Line::styled(
|
||||
"federation: hidden by Local filter",
|
||||
theme::dim(),
|
||||
))
|
||||
}
|
||||
_ if state.release_federation_loading(id) => {
|
||||
Some(super::loading_line(state, "searching peers…"))
|
||||
}
|
||||
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
|
||||
Some(Loadable::Ready(release)) if !release.tracks.is_empty() => Some(Line::styled(
|
||||
format!("federation: +{} tracks", release.tracks.len()),
|
||||
theme::dim(),
|
||||
)),
|
||||
Some(Loadable::Ready(_)) => Some(Line::styled(
|
||||
"federation: no additional tracks",
|
||||
theme::dim(),
|
||||
)),
|
||||
Some(Loadable::Failed(_)) => Some(Line::styled("federation: unavailable", theme::dim())),
|
||||
_ if state.federation.settings.enabled
|
||||
&& state.global.filters.source_mode.includes_network() =>
|
||||
{
|
||||
Some(Line::styled("federation: pending", theme::dim()))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let mut info = vec![
|
||||
Line::default(),
|
||||
Line::styled(detail.title.clone(), theme::header_for(state)),
|
||||
Line::raw(artists.join(", ")),
|
||||
Line::default(),
|
||||
Line::styled(
|
||||
format!(
|
||||
"{}{year} · {} tracks",
|
||||
detail.release_type,
|
||||
detail.tracks.len()
|
||||
),
|
||||
format!("{}{year} · {} tracks", detail.release_type, tracks.len()),
|
||||
theme::dim(),
|
||||
),
|
||||
];
|
||||
if let Some(line) = federation_line {
|
||||
info.push(line);
|
||||
}
|
||||
frame.render_widget(Paragraph::new(info), info_area);
|
||||
|
||||
// Track list with centered scrolling.
|
||||
let visible = usize::from(tracks_area.height.max(1));
|
||||
let total = detail.tracks.len();
|
||||
let total = tracks.len();
|
||||
let first = cursor
|
||||
.saturating_sub(visible / 2)
|
||||
.min(total.saturating_sub(visible));
|
||||
for (offset, track) in detail.tracks.iter().enumerate().skip(first).take(visible) {
|
||||
for (offset, track) in tracks.iter().enumerate().skip(first).take(visible) {
|
||||
let rect = Rect {
|
||||
x: tracks_area.x,
|
||||
y: tracks_area.y + (offset - first) as u16,
|
||||
@@ -907,12 +947,7 @@ 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.fed_tracks.is_empty()
|
||||
|| !state.search.fed_artists.is_empty()
|
||||
|| state.search.fed_loading =>
|
||||
{
|
||||
&empty_results
|
||||
}
|
||||
None if state.search.has_federated() => &empty_results,
|
||||
None => {
|
||||
let hint = if search.query.is_empty() {
|
||||
"type to search artists, releases and tracks"
|
||||
@@ -927,11 +962,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
|
||||
return centered_line(frame, inner, line);
|
||||
}
|
||||
};
|
||||
if results.is_empty()
|
||||
&& state.search.fed_tracks.is_empty()
|
||||
&& state.search.fed_artists.is_empty()
|
||||
&& !state.search.fed_loading
|
||||
{
|
||||
if results.is_empty() && !state.search.has_federated() {
|
||||
return centered_line(frame, inner, Line::styled("nothing found", theme::dim()));
|
||||
}
|
||||
|
||||
@@ -997,15 +1028,12 @@ 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).
|
||||
if !state.search.fed_tracks.is_empty()
|
||||
|| !state.search.fed_artists.is_empty()
|
||||
|| state.search.fed_loading
|
||||
{
|
||||
// (both marked with the owning peers), then public podcast cards.
|
||||
if state.search.has_federated() {
|
||||
if !rows.is_empty() {
|
||||
rows.push((Line::default(), None, None));
|
||||
}
|
||||
let header = if state.search.fed_loading {
|
||||
let header = if state.search.fed_loading || state.search.fed_podcasts_loading {
|
||||
"Federation · searching…"
|
||||
} else {
|
||||
"Federation"
|
||||
@@ -1060,6 +1088,23 @@ 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
|
||||
@@ -1105,6 +1150,47 @@ 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 {
|
||||
|
||||
+652
-26
@@ -1,9 +1,14 @@
|
||||
mod additional_settings;
|
||||
pub mod art;
|
||||
mod federation;
|
||||
mod global;
|
||||
pub(crate) mod global;
|
||||
mod logs;
|
||||
mod playlists;
|
||||
mod podcast_card;
|
||||
mod podcasts;
|
||||
mod popup;
|
||||
#[cfg(test)]
|
||||
mod status_tests;
|
||||
pub mod theme;
|
||||
|
||||
use ratatui::Frame;
|
||||
@@ -47,6 +52,510 @@ 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) {
|
||||
@@ -70,11 +579,16 @@ 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),
|
||||
}
|
||||
draw_status(frame, status_area, state);
|
||||
|
||||
if state.additional_settings_open {
|
||||
additional_settings::draw(frame, state);
|
||||
}
|
||||
|
||||
if state.help_visible {
|
||||
draw_help(frame, keymap, state);
|
||||
}
|
||||
@@ -133,15 +647,48 @@ fn is_waiting_message(message: &str) -> bool {
|
||||
}
|
||||
|
||||
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let titles = Tab::ALL
|
||||
.iter()
|
||||
.map(|tab| format!(" {} {} ", tab.index() + 1, tab.title()));
|
||||
let tabs = Tabs::new(titles)
|
||||
.select(state.active_tab.index())
|
||||
.style(theme::dim())
|
||||
.highlight_style(theme::tab_active_for(state))
|
||||
.divider("");
|
||||
frame.render_widget(tabs, area);
|
||||
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 {
|
||||
format!(" {} ", tab.index() + 1)
|
||||
}
|
||||
};
|
||||
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)
|
||||
}
|
||||
|
||||
/// One track row used by every track list: ♥ marker for liked tracks, the
|
||||
@@ -329,7 +876,7 @@ fn format_secs(secs: f64) -> String {
|
||||
|
||||
/// Playback time, progress bar, queue position, volume and mode flags.
|
||||
/// Wider consoles get a longer bar and full flags; narrow ones drop pieces.
|
||||
fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
|
||||
fn player_right_line(state: &AppState, width: u16) -> (Line<'static>, u16) {
|
||||
let player = &state.player;
|
||||
let mut spans: Vec<Span<'static>> = Vec::new();
|
||||
if let Some(track) = &player.current
|
||||
@@ -359,6 +906,7 @@ fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
|
||||
));
|
||||
}
|
||||
}
|
||||
let volume_offset = spans.iter().map(Span::width).sum::<usize>() + 2;
|
||||
if width >= 80 {
|
||||
let volume_cells = usize::from(player.volume / 10);
|
||||
spans.extend([
|
||||
@@ -392,29 +940,99 @@ fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
|
||||
format!(" {} ", state.device_playback.role.label()),
|
||||
theme::role_pill(state.device_playback.role),
|
||||
));
|
||||
spans.push(Span::styled(
|
||||
format!(" · online {}", state.device_playback.online_devices.max(1)),
|
||||
theme::dim(),
|
||||
));
|
||||
if width >= 160 {
|
||||
spans.push(Span::styled(
|
||||
format!(" · online {}", state.device_playback.online_devices.max(1)),
|
||||
theme::dim(),
|
||||
));
|
||||
}
|
||||
}
|
||||
// Keep a gap between the flags and the username block to the right.
|
||||
spans.push(Span::raw(" "));
|
||||
Line::from(spans)
|
||||
(
|
||||
Line::from(spans),
|
||||
volume_offset.min(usize::from(u16::MAX)) as u16,
|
||||
)
|
||||
}
|
||||
|
||||
/// Reserve the source-mode badge first so playback details cannot clip it.
|
||||
fn player_row_areas(row: Rect, state: &AppState) -> [Rect; 3] {
|
||||
let badge_width = (format!(" federation: {} ", state.global.filters.source_mode.label()).len()
|
||||
as u16)
|
||||
.min(row.width);
|
||||
let content = Rect::new(
|
||||
row.x,
|
||||
row.y,
|
||||
row.width.saturating_sub(badge_width),
|
||||
row.height,
|
||||
);
|
||||
let badge = Rect::new(content.right(), row.y, badge_width, row.height);
|
||||
let (line, _) = player_right_line(state, content.width);
|
||||
let right_width = line.width().min(usize::from(content.width)) as u16;
|
||||
let [title, right] = Layout::horizontal([
|
||||
Constraint::Min(8.min(content.width)),
|
||||
Constraint::Length(right_width),
|
||||
])
|
||||
.areas(content);
|
||||
[title, right, badge]
|
||||
}
|
||||
|
||||
/// Shared geometry for rendering and background visualization requests.
|
||||
fn footer_areas(area: Rect, state: &AppState) -> [Rect; 3] {
|
||||
let [player_row, message_row] =
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
|
||||
let [_, right, badge] = player_row_areas(player_row, state);
|
||||
let (_, volume_offset) = player_right_line(state, area.width.saturating_sub(badge.width));
|
||||
let version_width = (env!("CARGO_PKG_VERSION").len() as u16 + 1).min(message_row.width);
|
||||
let version = Rect::new(
|
||||
message_row.x + message_row.width.saturating_sub(version_width) / 2,
|
||||
message_row.y,
|
||||
version_width,
|
||||
message_row.height,
|
||||
);
|
||||
let message = Rect::new(
|
||||
message_row.x,
|
||||
message_row.y,
|
||||
version.x.saturating_sub(message_row.x).saturating_sub(1),
|
||||
message_row.height,
|
||||
);
|
||||
// On wide terminals align exactly with "vol". On narrow ones keep the
|
||||
// centered version readable, shrinking the histogram into the remainder.
|
||||
let start = right
|
||||
.x
|
||||
.saturating_add(volume_offset)
|
||||
.max(version.right().saturating_add(1))
|
||||
.min(message_row.right());
|
||||
let visual = Rect::new(
|
||||
start,
|
||||
message_row.y,
|
||||
message_row.right().saturating_sub(start),
|
||||
message_row.height,
|
||||
);
|
||||
[message, version, visual]
|
||||
}
|
||||
|
||||
pub(crate) fn status_visualizer_area(screen: Rect, state: &AppState) -> Rect {
|
||||
let [_, _, status] = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(2),
|
||||
])
|
||||
.areas(screen);
|
||||
footer_areas(status, state)[2]
|
||||
}
|
||||
|
||||
fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let [player_row, message_row] =
|
||||
let [player_row, _] =
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
|
||||
let [message_row, version_area, visual_area] = footer_areas(area, state);
|
||||
|
||||
let player = &state.player;
|
||||
// Layout: track title left, time/progress/flags on the right. The
|
||||
// right block is built first and gets a fixed width; the title
|
||||
// truncates into whatever is left.
|
||||
let center = player_right_line(state, area.width);
|
||||
let center_width = (center.width() as u16).min(area.width);
|
||||
let [title_area, right_area] =
|
||||
Layout::horizontal([Constraint::Min(8), Constraint::Length(center_width)])
|
||||
.areas(player_row);
|
||||
let [title_area, right_area, mode_area] = player_row_areas(player_row, state);
|
||||
let (center, _) = player_right_line(state, area.width.saturating_sub(mode_area.width));
|
||||
|
||||
let mut spans = Vec::new();
|
||||
match &player.current {
|
||||
@@ -439,6 +1057,16 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
}
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), title_area);
|
||||
frame.render_widget(Paragraph::new(center), right_area);
|
||||
let mode = state.global.filters.source_mode;
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!(" federation: {} ", mode.label()))
|
||||
.style(theme::source_mode_pill(mode)),
|
||||
mode_area,
|
||||
);
|
||||
draw_version(frame, version_area);
|
||||
if crate::visualizer::status::visible(state) {
|
||||
state.status_visualizer.draw(frame, visual_area);
|
||||
}
|
||||
|
||||
if state.cmdline.active {
|
||||
// Vim-style command line takes over the message row.
|
||||
@@ -448,7 +1076,6 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
usize::from(message_row.width.saturating_sub(2)),
|
||||
));
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), message_row);
|
||||
draw_version(frame, message_row);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -470,7 +1097,6 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
},
|
||||
};
|
||||
frame.render_widget(Paragraph::new(message), message_row);
|
||||
draw_version(frame, message_row);
|
||||
|
||||
if let Some(pending) = &state.pending_keys {
|
||||
let pending = Paragraph::new(Line::styled(
|
||||
@@ -489,7 +1115,7 @@ fn active_artist_peer_search(state: &AppState) -> Option<String> {
|
||||
let Some(GlobalView::Artist { id, .. }) = state.global.stack.last() else {
|
||||
return None;
|
||||
};
|
||||
if !matches!(state.artist_fed_views.get(id), Some(Loadable::Loading)) {
|
||||
if !state.artist_federation_loading(*id) {
|
||||
return None;
|
||||
}
|
||||
let name = match state.artist_views.get(id) {
|
||||
@@ -502,7 +1128,7 @@ fn active_artist_peer_search(state: &AppState) -> Option<String> {
|
||||
fn draw_version(frame: &mut Frame, area: Rect) {
|
||||
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(version, theme::dim())).alignment(Alignment::Right),
|
||||
Paragraph::new(Line::styled(version, theme::dim())).alignment(Alignment::Center),
|
||||
area,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
//! 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), "устройств");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,828 @@
|
||||
//! Podcasts tab: subscriptions with their check status, a podcast's
|
||||
//! seasons, and its episodes with saved position, played state, duration
|
||||
//! and date. Rendering only reads `AppState`.
|
||||
|
||||
use furumi_library::podcasts::{EpisodeListItem, SeasonSummary};
|
||||
use music_dht::podcast::ProgressStatus;
|
||||
use ratatui::Frame;
|
||||
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::{loading_line, theme};
|
||||
use crate::app::state::{
|
||||
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow, PodcastRow,
|
||||
PodcastStoreStatus, SeasonRow,
|
||||
};
|
||||
use crate::app::update::podcasts::{played_label, sort_label};
|
||||
use crate::podcasts::publication::{Discovery, Publication};
|
||||
|
||||
/// Width below which the side panel with the cover is dropped.
|
||||
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80;
|
||||
const SIDE_PANEL_WIDTH: u16 = ART_HEADER_WIDTH + 2;
|
||||
|
||||
pub(crate) const EMPTY_HINT: &str =
|
||||
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.";
|
||||
pub(crate) const STORE_FAILED_NOTE: &str = "The podcast database was left unchanged. \
|
||||
Music, playlists and sync keep working.";
|
||||
|
||||
pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
match &tab.store {
|
||||
PodcastStoreStatus::Opening => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
return centered_line(
|
||||
frame,
|
||||
inner,
|
||||
loading_line(state, "opening the podcast database…"),
|
||||
);
|
||||
}
|
||||
PodcastStoreStatus::Failed(err) => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
let lines = vec![
|
||||
Line::styled("Podcasts are unavailable", theme::header_for(state)),
|
||||
Line::default(),
|
||||
Line::styled(err.clone(), error_style()),
|
||||
Line::default(),
|
||||
Line::styled(STORE_FAILED_NOTE, theme::dim()),
|
||||
];
|
||||
frame.render_widget(
|
||||
Paragraph::new(lines)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
return;
|
||||
}
|
||||
PodcastStoreStatus::Ready => {}
|
||||
}
|
||||
match &tab.opened {
|
||||
None => draw_list(frame, area, state),
|
||||
Some(opened) if opened.season.is_none() => draw_seasons(frame, area, state),
|
||||
Some(_) => draw_episodes(frame, area, state),
|
||||
}
|
||||
}
|
||||
|
||||
fn bordered(frame: &mut Frame, area: Rect, state: &AppState, title: String) -> Rect {
|
||||
let block = Block::bordered()
|
||||
.title(title)
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
inner
|
||||
}
|
||||
|
||||
fn centered_line(frame: &mut Frame, area: Rect, line: Line) {
|
||||
if area.height == 0 {
|
||||
return;
|
||||
}
|
||||
let middle = Rect {
|
||||
y: area.y + area.height / 2,
|
||||
height: 1,
|
||||
..area
|
||||
};
|
||||
frame.render_widget(Paragraph::new(line).alignment(Alignment::Center), middle);
|
||||
}
|
||||
|
||||
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());
|
||||
match &tab.list {
|
||||
Some(Loadable::Failed(error)) => {
|
||||
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
None | Some(Loadable::Loading) => {
|
||||
return centered_line(frame, inner, loading_line(state, "loading podcasts…"));
|
||||
}
|
||||
Some(Loadable::Ready(_)) => {}
|
||||
}
|
||||
let (list_area, side_area) = split_side_panel(inner);
|
||||
if let Some(side) = side_area {
|
||||
draw_side_panel(frame, side, state, tab.current_podcast());
|
||||
}
|
||||
let rows = tab.list_rows();
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(tab.selected, rows.len(), visible);
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (left, right) = list_row(state, *row, list_area.width);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
state,
|
||||
left,
|
||||
right,
|
||||
index == tab.selected,
|
||||
);
|
||||
}
|
||||
let used = rows.len().saturating_sub(first).min(visible) as u16;
|
||||
let below = Rect {
|
||||
y: list_area.y + used + 1,
|
||||
height: list_area.height.saturating_sub(used + 1),
|
||||
..list_area
|
||||
};
|
||||
if below.height == 0 {
|
||||
return;
|
||||
}
|
||||
let hint = if tab.podcasts().is_empty() {
|
||||
Line::styled(EMPTY_HINT, theme::dim())
|
||||
} else if side_area.is_none()
|
||||
&& let Some(row) = tab.current_podcast()
|
||||
{
|
||||
// Narrow terminal: the selected podcast's status below the list.
|
||||
fetch_status_line(state, row)
|
||||
} else {
|
||||
Line::default()
|
||||
};
|
||||
frame.render_widget(Paragraph::new(hint).wrap(Wrap { trim: true }), below);
|
||||
}
|
||||
|
||||
fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>, Option<String>) {
|
||||
let tab = &state.podcasts;
|
||||
match row {
|
||||
PodcastListRow::Add => {
|
||||
let label = if tab.subscribing {
|
||||
format!("{} Adding podcast…", state.spinner())
|
||||
} else {
|
||||
"+ Add podcast (RSS feed URL)…".to_string()
|
||||
};
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
PodcastListRow::RefreshAll => {
|
||||
let label = if tab.refreshing.is_empty() {
|
||||
"↻ Check all podcasts for new episodes".to_string()
|
||||
} else {
|
||||
format!(
|
||||
"{} Checking {} feed(s) for new episodes…",
|
||||
state.spinner(),
|
||||
tab.refreshing.len()
|
||||
)
|
||||
};
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
PodcastListRow::Podcast(index) => {
|
||||
let Some(podcast) = tab.podcasts().get(index) else {
|
||||
return (Line::raw(""), None);
|
||||
};
|
||||
let marker = if tab.refreshing.contains(podcast.id()) {
|
||||
Span::styled(format!("{} ", state.spinner()), theme::accent_for(state))
|
||||
} else if podcast.fetch.last_error.is_some() {
|
||||
Span::styled("! ", error_style())
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
let summary = &podcast.summary;
|
||||
let 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 title_width = usize::from(width).saturating_sub(2 + meta.width() + 2);
|
||||
let left = Line::from(vec![
|
||||
marker,
|
||||
Span::raw(fit_width(podcast.title(), title_width)),
|
||||
]);
|
||||
(left, Some(meta))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Main area plus an optional right-hand panel for the cover and details.
|
||||
fn split_side_panel(area: Rect) -> (Rect, Option<Rect>) {
|
||||
if area.width < SIDE_PANEL_MIN_TOTAL_WIDTH {
|
||||
return (area, None);
|
||||
}
|
||||
let [main, side] =
|
||||
Layout::horizontal([Constraint::Min(0), Constraint::Length(SIDE_PANEL_WIDTH)]).areas(area);
|
||||
(main, Some(side))
|
||||
}
|
||||
|
||||
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&PodcastRow>) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::LEFT)
|
||||
.border_style(theme::border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
let Some(podcast) = podcast else {
|
||||
return;
|
||||
};
|
||||
let inner = Rect {
|
||||
x: inner.x + 1,
|
||||
width: inner.width.saturating_sub(1),
|
||||
..inner
|
||||
};
|
||||
let art_height = ART_HEADER_HEIGHT.min(inner.height);
|
||||
draw_cover(
|
||||
frame,
|
||||
Rect {
|
||||
height: art_height,
|
||||
width: ART_HEADER_WIDTH.min(inner.width),
|
||||
..inner
|
||||
},
|
||||
state,
|
||||
podcast,
|
||||
);
|
||||
let text_area = Rect {
|
||||
y: inner.y + art_height + 1,
|
||||
height: inner.height.saturating_sub(art_height + 1),
|
||||
..inner
|
||||
};
|
||||
if text_area.height == 0 {
|
||||
return;
|
||||
}
|
||||
let summary = &podcast.summary;
|
||||
let mut lines = vec![Line::styled(
|
||||
podcast.title().to_string(),
|
||||
Style::new().add_modifier(Modifier::BOLD),
|
||||
)];
|
||||
if let Some(author) = &summary.meta.author {
|
||||
lines.push(Line::styled(author.clone(), theme::dim()));
|
||||
}
|
||||
let mut counts = format!("{} episodes", summary.episode_count);
|
||||
if summary.season_count > 0 {
|
||||
counts.push_str(&format!(" · {} seasons", summary.season_count));
|
||||
}
|
||||
lines.push(Line::styled(counts, theme::dim()));
|
||||
lines.push(share_status_line(state, podcast));
|
||||
lines.push(fetch_status_line(state, podcast));
|
||||
if let Some(description) = &summary.meta.description {
|
||||
lines.push(Line::raw(""));
|
||||
lines.push(Line::raw(description.clone()));
|
||||
}
|
||||
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area);
|
||||
}
|
||||
|
||||
/// 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…");
|
||||
}
|
||||
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…",
|
||||
);
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
/// Refresh status of one podcast: checking, the last error, or when it was
|
||||
/// last checked.
|
||||
fn fetch_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> {
|
||||
if state.podcasts.refreshing.contains(podcast.id()) {
|
||||
return loading_line(state, "checking for new episodes…");
|
||||
}
|
||||
let fetch = &podcast.fetch;
|
||||
if let Some(error) = &fetch.last_error {
|
||||
let mut text = format!("last check failed: {error}");
|
||||
if fetch.consecutive_failures > 1 {
|
||||
text.push_str(&format!(" ({} times)", fetch.consecutive_failures));
|
||||
}
|
||||
if let Some(next) = fetch.next_check_at_ms {
|
||||
text.push_str(&format!("; next try {}", relative_time(next)));
|
||||
}
|
||||
return Line::styled(text, error_style());
|
||||
}
|
||||
match fetch.last_success_at_ms {
|
||||
Some(at) => Line::styled(format!("checked {}", relative_time(at)), theme::dim()),
|
||||
None => Line::styled("not checked yet", theme::dim()),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &PodcastRow) {
|
||||
let art = podcast.fetch.cover_path.as_ref().and_then(|path| {
|
||||
state.art.get(&crate::art::cache_key(
|
||||
path,
|
||||
ART_HEADER_WIDTH,
|
||||
ART_HEADER_HEIGHT,
|
||||
))
|
||||
});
|
||||
match art {
|
||||
Some(ArtState::Ready(image)) => {
|
||||
frame.render_widget(Paragraph::new(super::art::to_text(image)), area);
|
||||
}
|
||||
Some(ArtState::Loading) => centered_line(frame, area, Line::styled("…", theme::dim())),
|
||||
_ => centered_line(frame, area, Line::styled("🎙", theme::dim())),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Seasons
|
||||
|
||||
fn draw_seasons(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let podcast = tab.current_podcast();
|
||||
let title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let seasons = match &tab.seasons {
|
||||
Some((_, Loadable::Failed(error))) => {
|
||||
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
_ => match tab.opened_seasons() {
|
||||
Some(seasons) => seasons,
|
||||
None => return centered_line(frame, inner, loading_line(state, "loading seasons…")),
|
||||
},
|
||||
};
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let cursor = tab
|
||||
.opened
|
||||
.as_ref()
|
||||
.and_then(|opened| opened.season_cursor)
|
||||
.unwrap_or(0);
|
||||
let rows = tab.season_rows();
|
||||
let visible = usize::from(main.height.max(1));
|
||||
let first = first_visible(cursor, rows.len(), visible);
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (label, meta) = match row {
|
||||
SeasonRow::All => {
|
||||
let (episodes, unplayed) = seasons.iter().fold((0, 0), |(e, u), season| {
|
||||
(e + season.episode_count, u + season.unplayed_count)
|
||||
});
|
||||
("All episodes".to_string(), counts(episodes, unplayed))
|
||||
}
|
||||
SeasonRow::Group(index) => {
|
||||
let Some(season) = seasons.get(*index) else {
|
||||
continue;
|
||||
};
|
||||
(season_title(season), season_meta(season))
|
||||
}
|
||||
};
|
||||
let width = usize::from(main.width).saturating_sub(meta.width() + 4);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(main, index - first),
|
||||
state,
|
||||
Line::raw(format!(" {}", fit_width(&label, width))),
|
||||
Some(meta),
|
||||
index == cursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn season_title(season: &SeasonSummary) -> String {
|
||||
match season.season {
|
||||
Some(number) => format!("Season {number}"),
|
||||
None => "Without a season".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn counts(episodes: u64, unplayed: u64) -> String {
|
||||
if unplayed > 0 {
|
||||
format!("{unplayed} unplayed · {episodes} ep")
|
||||
} else {
|
||||
format!("{episodes} ep")
|
||||
}
|
||||
}
|
||||
|
||||
fn season_meta(season: &SeasonSummary) -> String {
|
||||
let mut meta = counts(season.episode_count, season.unplayed_count);
|
||||
let years = (
|
||||
season.first_published_at_ms.map(year_of),
|
||||
season.last_published_at_ms.map(year_of),
|
||||
);
|
||||
match years {
|
||||
(Some(first), Some(last)) if first != last => meta.push_str(&format!(" · {first}–{last}")),
|
||||
(Some(year), _) | (None, Some(year)) => meta.push_str(&format!(" · {year}")),
|
||||
(None, None) => {}
|
||||
}
|
||||
meta
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episodes
|
||||
|
||||
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let Some(opened) = &tab.opened else {
|
||||
return;
|
||||
};
|
||||
let podcast = tab.current_podcast();
|
||||
let mut title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
if opened.season_cursor.is_some() {
|
||||
let season = match opened.season {
|
||||
Some(furumi_library::podcasts::SeasonFilter::Season(number)) => {
|
||||
format!("Season {number}")
|
||||
}
|
||||
Some(furumi_library::podcasts::SeasonFilter::NoSeason) => {
|
||||
"Without a season".to_string()
|
||||
}
|
||||
_ => "All episodes".to_string(),
|
||||
};
|
||||
title = format!("{title} ▸ {season}");
|
||||
}
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let Some(list) = tab.opened_episodes() else {
|
||||
return centered_line(frame, inner, loading_line(state, "loading episodes…"));
|
||||
};
|
||||
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let notes_height = if main.height >= 18 { 6 } else { 0 };
|
||||
let [summary_area, list_area, notes_area] = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(notes_height),
|
||||
])
|
||||
.areas(main);
|
||||
|
||||
let mut summary = vec![Span::styled(
|
||||
episode_summary(state, list.items.len(), list.complete),
|
||||
theme::dim(),
|
||||
)];
|
||||
if list.loading || list.loading_more {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "loading…").spans);
|
||||
} else if podcast.is_some_and(|row| tab.refreshing.contains(row.id())) {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "checking for new episodes…").spans);
|
||||
}
|
||||
frame.render_widget(Paragraph::new(Line::from(summary)), summary_area);
|
||||
|
||||
if let Some(error) = &list.error {
|
||||
return centered_line(frame, list_area, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
if list.items.is_empty() {
|
||||
if list.loading {
|
||||
return;
|
||||
}
|
||||
let message = if tab.search.trim().is_empty()
|
||||
&& tab.played == furumi_library::podcasts::PlayedFilter::Any
|
||||
{
|
||||
"this feed has no playable episodes yet"
|
||||
} else {
|
||||
"no episode matches the search or filter"
|
||||
};
|
||||
return centered_line(frame, list_area, Line::styled(message, theme::dim()));
|
||||
}
|
||||
|
||||
let cursor = opened.episode_cursor.min(list.items.len() - 1);
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(cursor, list.items.len(), visible);
|
||||
for (index, item) in list.items.iter().enumerate().skip(first).take(visible) {
|
||||
draw_episode_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
state,
|
||||
item,
|
||||
index == cursor,
|
||||
);
|
||||
}
|
||||
if notes_height > 0
|
||||
&& let Some(item) = list.items.get(cursor)
|
||||
{
|
||||
draw_notes(frame, notes_area, state, item);
|
||||
}
|
||||
}
|
||||
|
||||
fn episode_summary(state: &AppState, loaded: usize, complete: bool) -> String {
|
||||
let tab = &state.podcasts;
|
||||
let count = if complete {
|
||||
format!("{loaded} episodes")
|
||||
} else {
|
||||
format!("{loaded}+ episodes")
|
||||
};
|
||||
let mut parts = vec![
|
||||
count,
|
||||
played_label(tab.played).to_string(),
|
||||
sort_label(tab.sort).to_string(),
|
||||
];
|
||||
let search = tab.search.trim();
|
||||
if !search.is_empty() {
|
||||
parts.push(format!("search: \"{search}\""));
|
||||
}
|
||||
parts.join(" · ")
|
||||
}
|
||||
|
||||
/// Marker and right-hand text for an episode's listening state.
|
||||
pub(crate) fn episode_state(item: &EpisodeListItem) -> (&'static str, String) {
|
||||
let duration = item.episode.duration_ms.filter(|ms| *ms > 0);
|
||||
match item.status() {
|
||||
ProgressStatus::Completed => (
|
||||
"✓",
|
||||
match duration {
|
||||
Some(ms) => format!("played · {}", format_duration_ms(ms)),
|
||||
None => "played".to_string(),
|
||||
},
|
||||
),
|
||||
ProgressStatus::InProgress => {
|
||||
let position = format_duration_ms(item.resume_position_ms());
|
||||
(
|
||||
"◐",
|
||||
match duration {
|
||||
Some(ms) => format!("{position} / {}", format_duration_ms(ms)),
|
||||
None => format!("at {position}"),
|
||||
},
|
||||
)
|
||||
}
|
||||
ProgressStatus::NotStarted => ("●", duration.map(format_duration_ms).unwrap_or_default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_episode_row(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &AppState,
|
||||
item: &EpisodeListItem,
|
||||
selected: bool,
|
||||
) {
|
||||
let episode = &item.episode;
|
||||
let date = episode
|
||||
.published_at_ms
|
||||
.map(|ms| format_date(ms.div_euclid(1000)))
|
||||
.unwrap_or_else(|| " ".repeat(10));
|
||||
let (mut marker, mut right) = episode_state(item);
|
||||
let mut marker_style = match item.status() {
|
||||
ProgressStatus::NotStarted => theme::accent_for(state),
|
||||
_ => theme::dim(),
|
||||
};
|
||||
// The episode playing on this device shows its live position.
|
||||
if let Some(position_ms) = playing_position_ms(state, &episode.episode_id) {
|
||||
marker = "▶";
|
||||
marker_style = theme::accent_for(state);
|
||||
let position = format_duration_ms(position_ms);
|
||||
right = match episode.duration_ms.filter(|ms| *ms > 0) {
|
||||
Some(ms) => format!("{position} / {}", format_duration_ms(ms)),
|
||||
None => position,
|
||||
};
|
||||
}
|
||||
let number = match (episode.season, episode.episode_number) {
|
||||
(Some(season), Some(number)) => format!("S{season}E{number} "),
|
||||
(None, Some(number)) => format!("#{number} "),
|
||||
_ => String::new(),
|
||||
};
|
||||
let mut title = episode.title.clone();
|
||||
if !item.in_feed {
|
||||
title.push_str(" (no longer in the feed)");
|
||||
}
|
||||
let title_width =
|
||||
usize::from(area.width).saturating_sub(2 + 10 + 2 + number.width() + right.width() + 2);
|
||||
let title_style = match item.status() {
|
||||
ProgressStatus::Completed => theme::dim(),
|
||||
_ => Style::default(),
|
||||
};
|
||||
let line = Line::from(vec![
|
||||
Span::styled(format!("{marker} "), marker_style),
|
||||
Span::styled(date, theme::dim()),
|
||||
Span::raw(" "),
|
||||
Span::styled(number, theme::dim()),
|
||||
Span::styled(fit_width(&title, title_width), title_style),
|
||||
]);
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
line,
|
||||
(!right.is_empty()).then_some(right),
|
||||
selected,
|
||||
);
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::TOP)
|
||||
.border_style(theme::border_for(state))
|
||||
.title(Span::styled(" Episode notes ", theme::dim()));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
let mut text = item
|
||||
.episode
|
||||
.description
|
||||
.clone()
|
||||
.unwrap_or_else(|| "No description in the feed.".to_string());
|
||||
if item.completed_count > 1 {
|
||||
text = format!("Listened {} times. {text}", item.completed_count);
|
||||
}
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::raw(text)).wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting
|
||||
|
||||
/// Truncate to a display width, adding an ellipsis when cut.
|
||||
pub(crate) fn fit_width(text: &str, width: usize) -> String {
|
||||
if UnicodeWidthStr::width(text) <= width {
|
||||
return text.to_string();
|
||||
}
|
||||
if width == 0 {
|
||||
return String::new();
|
||||
}
|
||||
let mut out = String::new();
|
||||
let mut used = 0;
|
||||
for ch in text.chars() {
|
||||
let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
|
||||
if used + w + 1 > width {
|
||||
break;
|
||||
}
|
||||
out.push(ch);
|
||||
used += w;
|
||||
}
|
||||
out.push('…');
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn format_duration_ms(ms: i64) -> String {
|
||||
let total = (ms.max(0) + 500) / 1000;
|
||||
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
|
||||
if hours > 0 {
|
||||
format!("{hours}:{minutes:02}:{secs:02}")
|
||||
} else {
|
||||
format!("{minutes}:{secs:02}")
|
||||
}
|
||||
}
|
||||
|
||||
/// `YYYY-MM-DD` (UTC) for unix seconds: Howard Hinnant's days-to-civil.
|
||||
pub(crate) fn format_date(unix_seconds: i64) -> String {
|
||||
let (year, month, day) = civil(unix_seconds);
|
||||
format!("{year:04}-{month:02}-{day:02}")
|
||||
}
|
||||
|
||||
fn year_of(unix_ms: i64) -> i64 {
|
||||
civil(unix_ms.div_euclid(1000)).0
|
||||
}
|
||||
|
||||
fn civil(unix_seconds: i64) -> (i64, i64, i64) {
|
||||
let days = unix_seconds.div_euclid(86_400);
|
||||
let z = days + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z.rem_euclid(146_097);
|
||||
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let day = doy - (153 * mp + 2) / 5 + 1;
|
||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let year = yoe + era * 400 + i64::from(month <= 2);
|
||||
(year, month, day)
|
||||
}
|
||||
|
||||
/// "5 min ago", "in 2 h", or a date for anything further away.
|
||||
fn relative_time(at_ms: i64) -> String {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as i64)
|
||||
.unwrap_or(0);
|
||||
relative_to(at_ms, now)
|
||||
}
|
||||
|
||||
pub(crate) fn relative_to(at_ms: i64, now_ms: i64) -> String {
|
||||
let delta = (now_ms - at_ms) / 1000;
|
||||
let (amount, future) = (delta.unsigned_abs(), delta < 0);
|
||||
let text = match amount {
|
||||
0..60 => {
|
||||
return if future {
|
||||
"soon".into()
|
||||
} else {
|
||||
"just now".into()
|
||||
};
|
||||
}
|
||||
60..3_600 => format!("{} min", amount / 60),
|
||||
3_600..86_400 => format!("{} h", amount / 3_600),
|
||||
_ => return format_date(at_ms.div_euclid(1000)),
|
||||
};
|
||||
if future {
|
||||
format!("in {text}")
|
||||
} else {
|
||||
format!("{text} ago")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn formats_dates_durations_and_relative_times() {
|
||||
assert_eq!(format_date(0), "1970-01-01");
|
||||
assert_eq!(format_date(951_782_400), "2000-02-29");
|
||||
assert_eq!(format_date(-86_400), "1969-12-31");
|
||||
assert_eq!(format_duration_ms(59_600), "1:00");
|
||||
assert_eq!(format_duration_ms(754_000), "12:34");
|
||||
assert_eq!(format_duration_ms(3_723_000), "1:02:03");
|
||||
let now = 1_700_000_000_000;
|
||||
assert_eq!(relative_to(now - 30_000, now), "just now");
|
||||
assert_eq!(relative_to(now - 5 * 60_000, now), "5 min ago");
|
||||
assert_eq!(relative_to(now + 2 * 3_600_000, now), "in 2 h");
|
||||
assert_eq!(relative_to(now - 3 * 86_400_000, now), "2023-11-11");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fits_display_width_with_ellipsis() {
|
||||
assert_eq!(fit_width("Выпуск 650", 20), "Выпуск 650");
|
||||
assert_eq!(fit_width("Выпуск 650", 5), "Выпу…");
|
||||
assert_eq!(fit_width("abc", 0), "");
|
||||
}
|
||||
}
|
||||
+148
-4
@@ -25,6 +25,22 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
..
|
||||
}) => draw_edit(frame, state, title, fields, *focus, error.as_deref()),
|
||||
Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label),
|
||||
Some(Popup::ConfirmPodcastUnsubscribe { title, .. }) => {
|
||||
draw_confirm_podcast_unsubscribe(frame, state, title)
|
||||
}
|
||||
Some(Popup::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)
|
||||
}
|
||||
@@ -43,7 +59,12 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
}) => draw_track_artists(frame, state, tracks, *cursor, *selected),
|
||||
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
|
||||
Some(Popup::FedInput { field, input }) => {
|
||||
draw_fed_input(frame, state, field.title(), field.help(), input)
|
||||
let keys = if *field == crate::app::state::FedInputField::PodcastSearch {
|
||||
"enter: keep · esc: clear"
|
||||
} else {
|
||||
"enter: apply · esc: cancel"
|
||||
};
|
||||
draw_fed_input(frame, state, field.title(), field.help(), keys, input)
|
||||
}
|
||||
Some(Popup::FedText { title, text }) => draw_fed_text(frame, state, title, text),
|
||||
Some(Popup::FedCopyText {
|
||||
@@ -891,6 +912,7 @@ fn draw_fed_input(
|
||||
state: &AppState,
|
||||
title: &str,
|
||||
help: &str,
|
||||
keys: &str,
|
||||
input: &crate::app::input::LineEdit,
|
||||
) {
|
||||
let area = centered(frame.area(), 72, 8);
|
||||
@@ -916,8 +938,7 @@ fn draw_fed_input(
|
||||
let spans = super::line_edit_spans(input, usize::from(entry_area.width.saturating_sub(1)));
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), entry_area);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled("enter: apply · esc: cancel", theme::dim()))
|
||||
.alignment(Alignment::Center),
|
||||
Paragraph::new(Line::styled(keys.to_string(), theme::dim())).alignment(Alignment::Center),
|
||||
hint_area,
|
||||
);
|
||||
}
|
||||
@@ -1266,6 +1287,129 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
|
||||
);
|
||||
}
|
||||
|
||||
/// Text of the unsubscribe confirmation: unsubscribing keeps progress and
|
||||
/// listening history, so subscribing again continues where the user left off.
|
||||
pub(crate) const PODCAST_UNSUBSCRIBE_NOTE: &str = "Listening progress and history are kept: \
|
||||
subscribe again later to continue where you left off.";
|
||||
|
||||
fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title: &str) {
|
||||
let width = 66.min(frame.area().width.saturating_sub(4)).max(30);
|
||||
let area = centered(frame.area(), width, 9);
|
||||
let block = Block::bordered()
|
||||
.title(" Unsubscribe? ")
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::strong_border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(block, area);
|
||||
|
||||
let [question, note, footer] = Layout::vertical([
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(inner);
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("Unsubscribe from \"{title}\"?"))
|
||||
.wrap(Wrap { trim: false })
|
||||
.alignment(Alignment::Center),
|
||||
question,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(PODCAST_UNSUBSCRIBE_NOTE, theme::dim()))
|
||||
.wrap(Wrap { trim: true })
|
||||
.alignment(Alignment::Center),
|
||||
note,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(
|
||||
"enter/y unsubscribe · esc/n cancel",
|
||||
theme::dim(),
|
||||
))
|
||||
.alignment(Alignment::Center),
|
||||
footer,
|
||||
);
|
||||
}
|
||||
|
||||
/// 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);
|
||||
@@ -1684,7 +1828,7 @@ fn draw_name_entry(
|
||||
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer);
|
||||
}
|
||||
|
||||
fn centered(area: Rect, width: u16, height: u16) -> Rect {
|
||||
pub(super) fn centered(area: Rect, width: u16, height: u16) -> Rect {
|
||||
let [rect] = Layout::horizontal([Constraint::Length(width.min(area.width))])
|
||||
.flex(Flex::Center)
|
||||
.areas(area);
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
use super::*;
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
|
||||
fn playing_state() -> AppState {
|
||||
let mut state = AppState::default();
|
||||
state.player.current = Some(crate::library::models::TrackItem {
|
||||
id: 1,
|
||||
title: "Песня 界".into(),
|
||||
track_number: None,
|
||||
disc_number: None,
|
||||
duration_seconds: 240.0,
|
||||
artists: vec![],
|
||||
featured_artists: vec![],
|
||||
release_id: 1,
|
||||
release_title: "Release".into(),
|
||||
release_year: None,
|
||||
cover_path: None,
|
||||
file_path: "/unused/test.flac".into(),
|
||||
content_id: None,
|
||||
audio_format: None,
|
||||
audio_bitrate: None,
|
||||
audio_sample_rate: None,
|
||||
audio_bit_depth: None,
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
});
|
||||
state.player.playing = true;
|
||||
state.player.position_secs = 12.0;
|
||||
state
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footer_centers_version_and_aligns_visualization_under_volume() {
|
||||
let mut state = playing_state();
|
||||
for width in [160, 200] {
|
||||
for shuffle in [false, true] {
|
||||
state.player.shuffle = shuffle;
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| draw_status(frame, frame.area(), &state))
|
||||
.unwrap();
|
||||
let buffer = terminal.backend().buffer();
|
||||
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
|
||||
// Count display cells, not UTF-8 bytes: the title is non-ASCII.
|
||||
let volume_x = (0..width)
|
||||
.find(|x| {
|
||||
buffer[(*x, 0)].symbol() == "v"
|
||||
&& *x + 2 < width
|
||||
&& buffer[(*x + 1, 0)].symbol() == "o"
|
||||
&& buffer[(*x + 2, 0)].symbol() == "l"
|
||||
})
|
||||
.unwrap();
|
||||
let areas = footer_areas(Rect::new(0, 0, width, 2), &state);
|
||||
assert_eq!(areas[2].x, volume_x);
|
||||
assert_eq!(areas[2].y, 1);
|
||||
assert_eq!(areas[2].right(), width);
|
||||
let version_x = (width - version.len() as u16) / 2;
|
||||
for (offset, ch) in version.chars().enumerate() {
|
||||
assert_eq!(
|
||||
buffer[(version_x + offset as u16, 1)].symbol(),
|
||||
ch.to_string()
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
status_visualizer_area(Rect::new(0, 0, width, 24), &state).x,
|
||||
volume_x
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footer_regions_do_not_overlap_on_narrow_terminals_or_command_input() {
|
||||
let mut state = playing_state();
|
||||
state.status_message = Some("long status message ".repeat(20));
|
||||
for width in [1, 5, 8, 20, 40, 80, 120, 200] {
|
||||
let areas = footer_areas(Rect::new(0, 0, width, 2), &state);
|
||||
assert!(areas[0].right() <= areas[1].x);
|
||||
assert!(areas[1].right() <= areas[2].x);
|
||||
assert!(areas[2].right() <= width);
|
||||
for command in [false, true] {
|
||||
state.cmdline.active = command;
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| draw_status(frame, frame.area(), &state))
|
||||
.unwrap();
|
||||
// Neither messages nor the command prompt may paint the graph.
|
||||
for x in areas[2].x..areas[2].right() {
|
||||
assert_eq!(terminal.backend().buffer()[(x, 1)].symbol(), " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_animation_requires_local_unpaused_playback() {
|
||||
let mut state = playing_state();
|
||||
assert!(crate::visualizer::status::playing(&state));
|
||||
state.player.paused = true;
|
||||
assert!(!crate::visualizer::status::playing(&state));
|
||||
assert!(crate::visualizer::status::visible(&state));
|
||||
state.player.paused = false;
|
||||
state.player.playing = false;
|
||||
assert!(!crate::visualizer::status::playing(&state));
|
||||
assert!(!crate::visualizer::status::visible(&state));
|
||||
state.player.playing = true;
|
||||
state.device_playback.role = crate::app::state::DevicePlaybackRole::Control;
|
||||
assert!(!crate::visualizer::status::playing(&state));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn federation_mode_badge_stays_visible_at_the_right_edge() {
|
||||
use crate::config::settings::LibrarySourceMode;
|
||||
use ratatui::style::Color;
|
||||
let mut state = playing_state();
|
||||
for (mode, background) in [
|
||||
(LibrarySourceMode::Local, Color::Yellow),
|
||||
(LibrarySourceMode::My, Color::Cyan),
|
||||
(LibrarySourceMode::Global, Color::Green),
|
||||
] {
|
||||
state.global.filters.source_mode = mode;
|
||||
for width in [20, 40, 80, 160, 200] {
|
||||
for playing in [false, true] {
|
||||
state.player.playing = playing;
|
||||
state.cmdline.active = !playing;
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| draw_status(frame, frame.area(), &state))
|
||||
.unwrap();
|
||||
let label = format!(" federation: {} ", mode.label());
|
||||
let start = width - label.len() as u16;
|
||||
for (offset, ch) in label.chars().enumerate() {
|
||||
let cell = &terminal.backend().buffer()[(start + offset as u16, 0)];
|
||||
assert_eq!(cell.symbol(), ch.to_string());
|
||||
assert_eq!(cell.bg, background);
|
||||
assert_eq!(cell.fg, Color::Black);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,19 @@ pub fn role_pill(role: DevicePlaybackRole) -> Style {
|
||||
.add_modifier(Modifier::BOLD)
|
||||
}
|
||||
|
||||
pub fn source_mode_pill(mode: crate::config::settings::LibrarySourceMode) -> Style {
|
||||
use crate::config::settings::LibrarySourceMode;
|
||||
let bg = match mode {
|
||||
LibrarySourceMode::Local => Color::Yellow,
|
||||
LibrarySourceMode::My => Color::Cyan,
|
||||
LibrarySourceMode::Global => Color::Green,
|
||||
};
|
||||
Style::new()
|
||||
.fg(Color::Black)
|
||||
.bg(bg)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
}
|
||||
|
||||
fn accent_color_for(state: &AppState) -> Color {
|
||||
match state.device_playback.role {
|
||||
DevicePlaybackRole::Jam => JAM_ACCENT,
|
||||
|
||||
+432
@@ -0,0 +1,432 @@
|
||||
//! Manual portable-binary updates. All network/disk work runs off the UI thread.
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
path::Path,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use object::Object;
|
||||
use reqwest::blocking::Client;
|
||||
use semver::Version;
|
||||
use serde::Deserialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const REPOSITORY: &str = "https://api.github.com/repos/Furumi-stack/furumi-tui";
|
||||
const MAX_ARCHIVE: u64 = 512 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Update {
|
||||
pub version: String,
|
||||
asset: Asset,
|
||||
checksums: Asset,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
struct Asset {
|
||||
name: String,
|
||||
browser_download_url: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Release {
|
||||
tag_name: String,
|
||||
draft: bool,
|
||||
prerelease: bool,
|
||||
assets: Vec<Asset>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct State {
|
||||
pub busy: bool,
|
||||
pub installed: bool,
|
||||
pub available: Option<Update>,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
fn client() -> Result<Client> {
|
||||
// Reuse the ring backend already used by federation; respect an existing provider.
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
Ok(Client::builder()
|
||||
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
|
||||
.https_only(true)
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.timeout(Duration::from_secs(300))
|
||||
.build()?)
|
||||
}
|
||||
|
||||
fn read_limited(mut reader: impl Read, limit: u64) -> Result<Vec<u8>> {
|
||||
let mut data = Vec::new();
|
||||
reader.by_ref().take(limit + 1).read_to_end(&mut data)?;
|
||||
ensure!(data.len() as u64 <= limit, "download exceeds size limit");
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn select_release(release: Release, current: &str, os: &str, arch: &str) -> Result<Option<Update>> {
|
||||
let version = release
|
||||
.tag_name
|
||||
.strip_prefix('v')
|
||||
.unwrap_or(&release.tag_name);
|
||||
let next = Version::parse(version).context("invalid release version")?;
|
||||
if release.draft
|
||||
|| release.prerelease
|
||||
|| !next.pre.is_empty()
|
||||
|| next <= Version::parse(current)?
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
let platform = match os {
|
||||
"linux" => "linux",
|
||||
"macos" => "macos",
|
||||
"windows" => "windows",
|
||||
_ => bail!("self-update is unsupported on {os}"),
|
||||
};
|
||||
let extension = if os == "windows" { "zip" } else { "tar.gz" };
|
||||
let name = format!("furumi-{platform}-{arch}-{version}.{extension}");
|
||||
let find = |name: &str| -> Result<Asset> {
|
||||
let matches: Vec<_> = release
|
||||
.assets
|
||||
.iter()
|
||||
.filter(|asset| asset.name == name)
|
||||
.collect();
|
||||
ensure!(matches.len() == 1, "release has no unique {name} asset");
|
||||
Ok(matches[0].clone())
|
||||
};
|
||||
Ok(Some(Update {
|
||||
version: version.to_owned(),
|
||||
asset: find(&name)?,
|
||||
checksums: find("SHA256SUMS")?,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn check() -> Result<Option<Update>> {
|
||||
let response = client()?
|
||||
.get(format!("{REPOSITORY}/releases/latest"))
|
||||
.timeout(Duration::from_secs(20))
|
||||
.send()?;
|
||||
if response.status() == reqwest::StatusCode::NOT_FOUND {
|
||||
return Ok(None);
|
||||
}
|
||||
let release = serde_json::from_slice(&read_limited(
|
||||
response.error_for_status()?,
|
||||
2 * 1024 * 1024,
|
||||
)?)?;
|
||||
select_release(
|
||||
release,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
std::env::consts::OS,
|
||||
std::env::consts::ARCH,
|
||||
)
|
||||
}
|
||||
|
||||
fn checksum(text: &str, name: &str) -> Result<String> {
|
||||
let mut found = None;
|
||||
for line in text.lines() {
|
||||
let Some((hash, filename)) = line.split_once(' ') else {
|
||||
continue;
|
||||
};
|
||||
if filename.trim_start().trim_start_matches('*') != name {
|
||||
continue;
|
||||
}
|
||||
ensure!(found.is_none(), "duplicate checksum for {name}");
|
||||
ensure!(
|
||||
hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit()),
|
||||
"invalid SHA-256 for {name}"
|
||||
);
|
||||
found = Some(hash.to_ascii_lowercase());
|
||||
}
|
||||
found.context("release does not contain a checksum for the selected archive")
|
||||
}
|
||||
|
||||
fn extract(archive: &Path, name: &str, output: &mut File) -> Result<()> {
|
||||
let binary = if cfg!(windows) {
|
||||
"furumi.exe"
|
||||
} else {
|
||||
"furumi"
|
||||
};
|
||||
let root = name
|
||||
.strip_suffix(".tar.gz")
|
||||
.or_else(|| name.strip_suffix(".zip"))
|
||||
.context("unsupported archive")?;
|
||||
// Existing release archives omit the version in their inner directory.
|
||||
let root = root.rsplit_once('-').context("invalid archive name")?.0;
|
||||
let expected = format!("{root}/{binary}");
|
||||
let mut count = 0;
|
||||
if name.ends_with(".zip") {
|
||||
let mut archive = zip::ZipArchive::new(File::open(archive)?)?;
|
||||
for index in 0..archive.len() {
|
||||
let mut entry = archive.by_index(index)?;
|
||||
if entry.name() != expected {
|
||||
continue;
|
||||
}
|
||||
ensure!(
|
||||
entry.is_file() && !entry.is_symlink(),
|
||||
"binary is not a regular file"
|
||||
);
|
||||
count += 1;
|
||||
ensure!(count == 1, "duplicate binary in archive");
|
||||
output.write_all(&read_limited(&mut entry, MAX_ARCHIVE)?)?;
|
||||
}
|
||||
} else {
|
||||
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(File::open(archive)?));
|
||||
for entry in archive.entries()? {
|
||||
let mut entry = entry?;
|
||||
if entry.path()?.as_ref() != Path::new(&expected) {
|
||||
continue;
|
||||
}
|
||||
ensure!(
|
||||
entry.header().entry_type().is_file(),
|
||||
"binary is not a regular file"
|
||||
);
|
||||
count += 1;
|
||||
ensure!(count == 1, "duplicate binary in archive");
|
||||
output.write_all(&read_limited(&mut entry, MAX_ARCHIVE)?)?;
|
||||
}
|
||||
}
|
||||
ensure!(count == 1, "archive does not contain {expected}");
|
||||
output.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_binary(current: &[u8], candidate: &[u8]) -> Result<()> {
|
||||
let current = object::File::parse(current).context("cannot inspect installed binary")?;
|
||||
let candidate = object::File::parse(candidate).context("invalid downloaded binary")?;
|
||||
ensure!(
|
||||
candidate.kind() == object::ObjectKind::Executable
|
||||
|| candidate.kind() == object::ObjectKind::Dynamic,
|
||||
"download is not an executable"
|
||||
);
|
||||
ensure!(
|
||||
candidate.format() == current.format()
|
||||
&& candidate.architecture() == current.architecture()
|
||||
&& candidate.is_64() == current.is_64()
|
||||
&& candidate.is_little_endian() == current.is_little_endian(),
|
||||
"downloaded binary has incompatible platform or architecture"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn install(update: &Update, mut progress: impl FnMut(String)) -> Result<()> {
|
||||
let exe = std::env::current_exe()?.canonicalize()?;
|
||||
let parent = exe.parent().context("executable has no parent directory")?;
|
||||
let lock = File::options()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.open(parent.join(".furumi-update.lock"))
|
||||
.context("cannot write to installation directory")?;
|
||||
fs2::FileExt::try_lock_exclusive(&lock).context("another furumi instance is updating")?;
|
||||
// Keep staging on the destination filesystem and never overwrite a running image.
|
||||
let staging = tempfile::Builder::new()
|
||||
.prefix(".furumi-update-")
|
||||
.tempdir_in(parent)
|
||||
.context("cannot create update staging directory")?;
|
||||
let client = client()?;
|
||||
let sums = read_limited(
|
||||
client
|
||||
.get(&update.checksums.browser_download_url)
|
||||
.send()?
|
||||
.error_for_status()?,
|
||||
1024 * 1024,
|
||||
)?;
|
||||
let expected = checksum(std::str::from_utf8(&sums)?, &update.asset.name)?;
|
||||
let mut response = client
|
||||
.get(&update.asset.browser_download_url)
|
||||
.send()?
|
||||
.error_for_status()?;
|
||||
let total = response.content_length();
|
||||
ensure!(
|
||||
total.is_none_or(|size| size <= MAX_ARCHIVE),
|
||||
"archive exceeds size limit"
|
||||
);
|
||||
let archive = staging.path().join("download");
|
||||
let mut file = File::create(&archive)?;
|
||||
let mut hash = Sha256::new();
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
let mut downloaded = 0u64;
|
||||
let mut reported = u64::MAX;
|
||||
loop {
|
||||
let count = response.read(&mut buffer)?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
downloaded += count as u64;
|
||||
ensure!(downloaded <= MAX_ARCHIVE, "archive exceeds size limit");
|
||||
file.write_all(&buffer[..count])?;
|
||||
hash.update(&buffer[..count]);
|
||||
let mb = downloaded / (1024 * 1024);
|
||||
if mb != reported {
|
||||
progress(format!("Downloading: {mb} MiB"));
|
||||
reported = mb;
|
||||
}
|
||||
}
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
ensure!(
|
||||
format!("{:x}", hash.finalize()) == expected,
|
||||
"SHA-256 mismatch; update was not installed"
|
||||
);
|
||||
progress("Verifying binary...".into());
|
||||
let binary = staging.path().join(if cfg!(windows) {
|
||||
"furumi.exe"
|
||||
} else {
|
||||
"furumi"
|
||||
});
|
||||
let mut output = File::create(&binary)?;
|
||||
extract(&archive, &update.asset.name, &mut output)?;
|
||||
drop(output);
|
||||
validate_binary(&std::fs::read(&exe)?, &std::fs::read(&binary)?)?;
|
||||
std::fs::set_permissions(&binary, std::fs::metadata(&exe)?.permissions())?;
|
||||
progress("Installing...".into());
|
||||
self_replace::self_replace(&binary).context("could not replace executable")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn https_client_can_be_constructed() {
|
||||
client().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_only_expected_binary_from_release_archives() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let binary = if cfg!(windows) {
|
||||
"furumi.exe"
|
||||
} else {
|
||||
"furumi"
|
||||
};
|
||||
let expected = format!("furumi-windows-x86_64/{binary}");
|
||||
let payload = b"test executable";
|
||||
let zip_path = temp.path().join("test.zip");
|
||||
let mut zip = zip::ZipWriter::new(File::create(&zip_path).unwrap());
|
||||
zip.start_file("README.md", zip::write::SimpleFileOptions::default())
|
||||
.unwrap();
|
||||
zip.write_all(b"readme").unwrap();
|
||||
zip.start_file(&expected, zip::write::SimpleFileOptions::default())
|
||||
.unwrap();
|
||||
zip.write_all(payload).unwrap();
|
||||
zip.finish().unwrap();
|
||||
let output = temp.path().join("output");
|
||||
extract(
|
||||
&zip_path,
|
||||
"furumi-windows-x86_64-0.1.6.zip",
|
||||
&mut File::create(&output).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read(&output).unwrap(), payload);
|
||||
assert!(
|
||||
extract(
|
||||
&zip_path,
|
||||
"furumi-linux-x86_64-0.1.6.zip",
|
||||
&mut File::create(&output).unwrap()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
|
||||
let tar_path = temp.path().join("test.tar.gz");
|
||||
let gzip = flate2::write::GzEncoder::new(
|
||||
File::create(&tar_path).unwrap(),
|
||||
flate2::Compression::default(),
|
||||
);
|
||||
let mut tar = tar::Builder::new(gzip);
|
||||
let mut header = tar::Header::new_gnu();
|
||||
header.set_size(payload.len() as u64);
|
||||
header.set_mode(0o755);
|
||||
header.set_cksum();
|
||||
tar.append_data(&mut header, &expected, payload.as_slice())
|
||||
.unwrap();
|
||||
tar.into_inner().unwrap().finish().unwrap();
|
||||
extract(
|
||||
&tar_path,
|
||||
"furumi-windows-x86_64-0.1.6.tar.gz",
|
||||
&mut File::create(&output).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read(&output).unwrap(), payload);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binary_validation_rejects_wrong_architecture() {
|
||||
let current = std::fs::read(std::env::current_exe().unwrap()).unwrap();
|
||||
let mut other = current.clone();
|
||||
// Change only the machine field, preserving an otherwise valid executable.
|
||||
if current.starts_with(b"MZ") {
|
||||
let pe = u32::from_le_bytes(current[0x3c..0x40].try_into().unwrap()) as usize;
|
||||
let machine: u16 = if current[pe + 4..pe + 6] == [0x64, 0x86] {
|
||||
0xaa64
|
||||
} else {
|
||||
0x8664
|
||||
};
|
||||
other[pe + 4..pe + 6].copy_from_slice(&machine.to_le_bytes());
|
||||
} else if current.starts_with(b"\x7fELF") {
|
||||
let machine: u16 = if current[18..20] == [62, 0] { 183 } else { 62 };
|
||||
other[18..20].copy_from_slice(&machine.to_le_bytes());
|
||||
} else if current.starts_with(&[0xcf, 0xfa, 0xed, 0xfe]) {
|
||||
let cpu: u32 = if current[4] == 7 {
|
||||
0x0100000c
|
||||
} else {
|
||||
0x01000007
|
||||
};
|
||||
other[4..8].copy_from_slice(&cpu.to_le_bytes());
|
||||
} else {
|
||||
panic!("unsupported test executable format");
|
||||
}
|
||||
assert!(validate_binary(¤t, &other).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn checksums_require_exact_unique_valid_entry() {
|
||||
let hash = "ab".repeat(32);
|
||||
assert_eq!(
|
||||
checksum(&format!("{hash} app.zip\n"), "app.zip").unwrap(),
|
||||
hash
|
||||
);
|
||||
assert!(checksum(&format!("{hash} other.zip"), "app.zip").is_err());
|
||||
assert!(checksum("broken app.zip", "app.zip").is_err());
|
||||
assert!(checksum(&format!("{hash} app.zip\n{hash} app.zip"), "app.zip").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn release_selection_respects_semver_and_assets() {
|
||||
let release = |version: &str| Release {
|
||||
tag_name: version.into(),
|
||||
draft: false,
|
||||
prerelease: false,
|
||||
assets: vec![
|
||||
Asset {
|
||||
name: "furumi-linux-x86_64-0.1.10.tar.gz".into(),
|
||||
browser_download_url: String::new(),
|
||||
},
|
||||
Asset {
|
||||
name: "SHA256SUMS".into(),
|
||||
browser_download_url: String::new(),
|
||||
},
|
||||
],
|
||||
};
|
||||
assert!(
|
||||
select_release(release("v0.1.10"), "0.1.9", "linux", "x86_64")
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
assert!(
|
||||
select_release(release("v0.1.8"), "0.1.9", "linux", "x86_64")
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
select_release(release("v0.2.0-beta.1"), "0.1.9", "linux", "x86_64")
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert!(select_release(release("v0.1.10"), "0.1.9", "linux", "aarch64").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn binary_validation_rejects_corrupt_download() {
|
||||
let current = std::fs::read(std::env::current_exe().unwrap()).unwrap();
|
||||
validate_binary(¤t, ¤t).unwrap();
|
||||
assert!(validate_binary(¤t, b"not a binary").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
// name: Morphing cube
|
||||
// protocol: furumi-visualizer-v2
|
||||
// bundle-version: 6
|
||||
//
|
||||
// Audio-reactive neon wireframe, echo contours, core, shock rings, sparks
|
||||
// and a bounded particle field. No native 3D API or retained state required.
|
||||
// Echoes sample earlier rotation times with CURRENT audio; they are analytic
|
||||
// trails, not stored frames. Particle lifetimes use time, rings also expand
|
||||
// as the smoothed beat envelope decays. No beat history is available in v2.
|
||||
//
|
||||
// Fixed budgets: 8 mesh vertices, at most 48 particles + 24 short tails,
|
||||
// 20 background stars, 3 rings, 2 echoes. Edges/rings use bulk trace commands.
|
||||
// Smaller terminals omit echoes/rings and reduce particle counts.
|
||||
|
||||
fn clamp(v, lo, hi) {
|
||||
if v < lo { lo } else if v > hi { hi } else { v }
|
||||
}
|
||||
|
||||
fn min(a, b) { if a < b { a } else { b } }
|
||||
|
||||
fn rgb(r, g, b) {
|
||||
to_int(clamp(r, 0.0, 255.0)) * 65536
|
||||
+ to_int(clamp(g, 0.0, 255.0)) * 256
|
||||
+ to_int(clamp(b, 0.0, 255.0))
|
||||
}
|
||||
|
||||
// trace runs left-to-right. Swap endpoints for reversed edges and use
|
||||
// vline for vertical ones (including an edge projected to a single cell).
|
||||
fn edge(ax, ay, bx, by, fg, bg) {
|
||||
if ax > bx {
|
||||
let tx = ax; ax = bx; bx = tx;
|
||||
let ty = ay; ay = by; by = ty;
|
||||
}
|
||||
if ax == bx {
|
||||
return #{ op: "vline", x: ax, y: min(ay, by),
|
||||
h: abs(to_float(by - ay)).to_int() + 1, ch: "|", fg: fg, bg: bg };
|
||||
}
|
||||
let dx = bx - ax;
|
||||
let dy = by - ay;
|
||||
let ch = if abs(to_float(dy)) * 3.0 < to_float(dx) { "-" }
|
||||
else if abs(to_float(dy)) > to_float(dx) { "|" }
|
||||
else if dy < 0 { "/" } else { "\\" };
|
||||
let ys = [];
|
||||
let slope = to_float(dy) / to_float(dx);
|
||||
for x in 0..=dx {
|
||||
ys.push(to_int(to_float(ay) + to_float(x) * slope + 0.5));
|
||||
}
|
||||
#{ op: "trace", x: ax, ys: ys, ch: ch, line_ch: ch,
|
||||
fg: fg, line_fg: fg, bg: bg }
|
||||
}
|
||||
|
||||
// Lift quieter passages without a hard gain/clipping threshold.
|
||||
fn response(level) { 1.0 - pow(1.0 - clamp(level, 0.0, 1.0), 1.6) }
|
||||
|
||||
fn palette(t, heat, light) {
|
||||
rgb((95.0 + 125.0 * heat + 35.0 * sin(t * 0.43)) * light,
|
||||
(185.0 - 95.0 * heat + 55.0 * sin(t * 0.31 + 1.3)) * light,
|
||||
255.0 * light)
|
||||
}
|
||||
|
||||
// A single projected mesh, fitted uniformly: never clamp individual corners.
|
||||
fn mesh(t, energy, bass, mid, treble, beat, width, height) {
|
||||
let pulse = 0.70 + energy * 0.45 + beat * 0.85;
|
||||
let sx = pulse * (1.0 + bass * 0.55);
|
||||
let sy = pulse * (1.0 - bass * 0.42 + mid * 0.15);
|
||||
let sz = pulse * (1.0 + treble * 0.22);
|
||||
// A bounded beat kick, not time * instantaneous_energy (which jumps).
|
||||
let ax = t * 0.47 + 0.35 + beat * 0.22 * sin(t * 1.3);
|
||||
let ay = t * 0.63 + 0.55;
|
||||
let az = t * 0.23 + beat * 0.16 * cos(t * 0.7);
|
||||
let cx = cos(ax); let snx = sin(ax);
|
||||
let cy = cos(ay); let sny = sin(ay);
|
||||
let cz = cos(az); let snz = sin(az);
|
||||
let twist = mid * 0.95 * sin(t * 0.83);
|
||||
let ct = cos(twist); let st = sin(twist);
|
||||
let vertices = [
|
||||
[-1.0, -1.0, -1.0], [1.0, -1.0, -1.0],
|
||||
[1.0, 1.0, -1.0], [-1.0, 1.0, -1.0],
|
||||
[-1.0, -1.0, 1.0], [1.0, -1.0, 1.0],
|
||||
[1.0, 1.0, 1.0], [-1.0, 1.0, 1.0]
|
||||
];
|
||||
let projected = [];
|
||||
let extent = 0.01;
|
||||
for i in 0..8 {
|
||||
let v = vertices[i];
|
||||
let pull = 1.0 + treble * 0.38 * sin(t * 2.3 + to_float(i) * 2.4)
|
||||
+ beat * 0.14 * cos(to_float(i) * 1.7 + t);
|
||||
let x = v[0] * sx * pull;
|
||||
let y = v[1] * sy * pull;
|
||||
let z = v[2] * sz * pull;
|
||||
let xt = x * ct + z * st * v[1];
|
||||
let zt = z * ct - x * st * v[1];
|
||||
let y1 = y * cx - zt * snx;
|
||||
let z1 = y * snx + zt * cx;
|
||||
let x2 = xt * cy + z1 * sny;
|
||||
let z2 = z1 * cy - xt * sny;
|
||||
let x3 = x2 * cz - y1 * snz;
|
||||
let y3 = x2 * snz + y1 * cz;
|
||||
// Even the largest deformed vertex stays < 6.4 from the origin.
|
||||
let perspective = 10.0 / (10.0 + z2);
|
||||
let px = x3 * perspective;
|
||||
let py = y3 * perspective;
|
||||
if abs(px) > extent { extent = abs(px); }
|
||||
if abs(py) > extent { extent = abs(py); }
|
||||
projected.push([px, py, z2]);
|
||||
}
|
||||
// CELL_ASPECT is cell height / width; adjust for a different font.
|
||||
let CELL_ASPECT = 2.0;
|
||||
let span = min(to_float(width - 4) / CELL_ASPECT, to_float(height - 4));
|
||||
let scale = span * min(0.23, 0.46 / extent);
|
||||
let center_x = to_float(width - 1) * 0.5;
|
||||
let center_y = to_float(height - 1) * 0.5;
|
||||
let points = [];
|
||||
for p in projected {
|
||||
points.push([to_int(center_x + p[0] * scale * CELL_ASPECT + 0.5),
|
||||
to_int(center_y + p[1] * scale + 0.5), p[2]]);
|
||||
}
|
||||
points
|
||||
}
|
||||
|
||||
// Method calls mutate the command array in place; don't copy it into helpers.
|
||||
fn dot(x, y, ch, fg, bg, width, height) {
|
||||
let px = to_int(x + 0.5); let py = to_int(y + 0.5);
|
||||
if x >= 0.0 && y >= 1.0 && px < width && py < height - 1 {
|
||||
this.push(#{ op: "cell", x: px, y: py, ch: ch, fg: fg, bg: bg });
|
||||
}
|
||||
}
|
||||
|
||||
fn render(input) {
|
||||
let bg = 0x020610;
|
||||
let cmds = [#{ op: "clear", bg: bg }];
|
||||
if input.width < 12 || input.height < 8 { return cmds; }
|
||||
let w = input.width; let h = input.height;
|
||||
let t = input.time;
|
||||
let energy = response(input.energy);
|
||||
let bass = response(input.bass);
|
||||
let mid = response(input.mid);
|
||||
let treble = response(input.treble);
|
||||
let beat = response(input.beat);
|
||||
let drive = clamp(energy * 0.4 + treble * 0.3 + beat * 0.8, 0.0, 1.0);
|
||||
let small = w < 40 || h < 16;
|
||||
let center_x = to_float(w - 1) * 0.5;
|
||||
let center_y = to_float(h - 1) * 0.5;
|
||||
let span = min(to_float(w - 4) * 0.5, to_float(h - 4));
|
||||
let points = mesh(t, energy, bass, mid, treble, beat, w, h);
|
||||
let edges = [[0,1], [1,2], [2,3], [3,0], [4,5], [5,6],
|
||||
[6,7], [7,4], [0,4], [1,5], [2,6], [3,7]];
|
||||
|
||||
// Sparse drifting starfield; deterministic, independent of audio hardware.
|
||||
let stars = if small { 8 } else { 20 };
|
||||
for i in 0..stars {
|
||||
let phase = to_float(i);
|
||||
let x = (phase * 37.71 + t * (0.3 + to_float(i % 3) * 0.2)) % to_float(w - 1);
|
||||
let y = 1.0 + (phase * 13.37) % to_float(h - 3);
|
||||
let light = 0.12 + treble * 0.16 * (0.5 + 0.5 * sin(t * 2.0 + phase));
|
||||
cmds.dot(x, y, ".", palette(t, 0.0, light), bg, w, h);
|
||||
}
|
||||
|
||||
// Three elliptical shock fronts. On a transient they brighten and
|
||||
// spread; release of the beat envelope carries them farther outward.
|
||||
if !small && drive > 0.04 {
|
||||
for ring in 0..3 {
|
||||
let phase = (t * 0.45 + to_float(ring) / 3.0) % 1.0;
|
||||
let radius = 0.22 + phase * 0.55 + (1.0 - beat) * 0.18;
|
||||
let rx = span * radius;
|
||||
let ry = span * radius * (0.22 + mid * 0.10);
|
||||
let left = to_int(center_x - rx);
|
||||
let right = to_int(center_x + rx);
|
||||
let top = []; let bottom = [];
|
||||
for x in left..=right {
|
||||
let nx = clamp((to_float(x) - center_x) / rx, -1.0, 1.0);
|
||||
let dy = sqrt(1.0 - nx * nx) * ry;
|
||||
top.push(to_int(center_y - dy + 0.5));
|
||||
bottom.push(to_int(center_y + dy + 0.5));
|
||||
}
|
||||
let fg = palette(t + to_float(ring), beat, (1.0 - phase) * (0.12 + beat * 0.45 + bass * 0.12));
|
||||
for ys in [top, bottom] {
|
||||
cmds.push(#{ op: "trace", x: left, ys: ys, ch: ".", line_ch: ".",
|
||||
fg: fg, line_fg: fg, bg: bg });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Particles launch from the eight corners with staggered lifetimes.
|
||||
// Speed/age depend on time; audio changes reach and brightness instead
|
||||
// of rescaling time and teleporting particles on each audio update.
|
||||
if drive > 0.04 {
|
||||
let particles = if small { 16 } else { 48 };
|
||||
for i in 0..particles {
|
||||
let fi = to_float(i);
|
||||
let age = (t * (0.27 + to_float(i % 5) * 0.025) + fi * 0.618034) % 1.0;
|
||||
let angle = fi * 2.39996 + t * 0.16;
|
||||
let dx = cos(angle); let dy = sin(angle);
|
||||
let origin = points[i % 8];
|
||||
let travel = span * (0.35 + energy * 0.55 + beat * 0.8);
|
||||
let distance = age * travel;
|
||||
let light = sin(age * 3.14159) * (0.15 + drive * 0.85);
|
||||
let fg = palette(t + fi * 0.13, beat, light);
|
||||
let ch = if beat > 0.45 && i % 3 == 0 { "*" } else if treble > 0.35 { "+" } else { "." };
|
||||
if !small && i % 2 == 0 {
|
||||
let tail = distance - span * 0.04;
|
||||
cmds.dot(to_float(origin[0]) + dx * tail * 2.0,
|
||||
to_float(origin[1]) + dy * tail, ".", palette(t, beat, light * 0.3), bg, w, h);
|
||||
}
|
||||
cmds.dot(to_float(origin[0]) + dx * distance * 2.0,
|
||||
to_float(origin[1]) + dy * distance, ch, fg, bg, w, h);
|
||||
}
|
||||
}
|
||||
|
||||
// Analytic motion echoes. These are recomputed poses, not frame history.
|
||||
if !small && drive > 0.04 {
|
||||
for echo in [2, 1] {
|
||||
let lag = to_float(echo) * (0.14 + beat * 0.10);
|
||||
let ghost = mesh(t - lag, energy, bass, mid, treble, beat, w, h);
|
||||
let fg = palette(t + to_float(echo), 1.0 - beat, (0.08 + drive * 0.16) / to_float(echo));
|
||||
for e in edges {
|
||||
let a = ghost[e[0]]; let b = ghost[e[1]];
|
||||
cmds.push(edge(a[0], a[1], b[0], b[1], fg, bg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A warm inner cage gives depth and a second pulse inside the main mesh.
|
||||
if !small {
|
||||
let inner = [];
|
||||
let size = 0.35 + beat * 0.25;
|
||||
for p in points {
|
||||
inner.push([to_int(center_x + (to_float(p[0]) - center_x) * size + 0.5),
|
||||
to_int(center_y + (to_float(p[1]) - center_y) * size + 0.5)]);
|
||||
}
|
||||
let fg = rgb(100.0 + beat * 155.0, 45.0 + energy * 75.0, 65.0 + treble * 70.0);
|
||||
for e in edges {
|
||||
let a = inner[e[0]]; let b = inner[e[1]];
|
||||
cmds.push(edge(a[0], a[1], b[0], b[1], fg, bg));
|
||||
}
|
||||
}
|
||||
|
||||
let ordered = [];
|
||||
for e in edges {
|
||||
let depth = (points[e[0]][2] + points[e[1]][2]) * 0.5;
|
||||
let at = 0;
|
||||
while at < ordered.len() && ordered[at][2] > depth { at += 1; }
|
||||
ordered.insert(at, [e[0], e[1], depth]);
|
||||
}
|
||||
for e in ordered {
|
||||
let a = points[e[0]]; let b = points[e[1]];
|
||||
let light = clamp(0.78 - e[2] * 0.14 + beat * 0.12, 0.3, 1.0);
|
||||
cmds.push(edge(a[0], a[1], b[0], b[1], palette(t, beat, light), bg));
|
||||
}
|
||||
// Sparks travel along edges; no more than twelve extra cells.
|
||||
if treble > 0.08 || beat > 0.08 {
|
||||
for i in 0..12 {
|
||||
let e = edges[i]; let a = points[e[0]]; let b = points[e[1]];
|
||||
let phase = (t * 0.8 + to_float(i) * 0.137) % 1.0;
|
||||
cmds.dot(to_float(a[0]) + to_float(b[0] - a[0]) * phase,
|
||||
to_float(a[1]) + to_float(b[1] - a[1]) * phase,
|
||||
"*", 0xffe8c0, bg, w, h);
|
||||
}
|
||||
}
|
||||
for p in points {
|
||||
if !small && beat > 0.45 {
|
||||
let glow = palette(t, beat, beat * 0.45);
|
||||
for offset in [[-1,0], [1,0], [0,-1], [0,1]] {
|
||||
cmds.dot(to_float(p[0] + offset[0]), to_float(p[1] + offset[1]), "+", glow, bg, w, h);
|
||||
}
|
||||
}
|
||||
cmds.push(#{ op: "cell", x: p[0], y: p[1], ch: "o", fg: 0xe8f6ff, bg: bg });
|
||||
}
|
||||
if input.show_clock {
|
||||
cmds.push(#{ op: "text", x: 1, y: 0, text: input.clock, fg: 0xa6cde0, bg: bg });
|
||||
}
|
||||
if input.paused {
|
||||
cmds.push(#{ op: "text", x: w - 7, y: 0, text: "paused", fg: 0xa6cde0, bg: bg });
|
||||
}
|
||||
if w >= 16 {
|
||||
cmds.push(#{ op: "text", x: 1, y: h - 1, text: "MORPHING CUBE", fg: 0x527b98, bg: bg });
|
||||
}
|
||||
cmds
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// name: Status braille histogram
|
||||
// protocol: furumi-visualizer-v2
|
||||
// surface: status
|
||||
// revision: 2
|
||||
//
|
||||
// One-row waveform histogram. Input is decoded audio BEFORE output volume.
|
||||
// The host freezes the last frame on pause, including custom scripts.
|
||||
// Edit this file to customize it; only untouched bundled defaults upgrade.
|
||||
// Each Braille character packs two independent four-dot vertical columns.
|
||||
// "default" background retains the terminal's background color.
|
||||
|
||||
fn clamp(value, low, high) {
|
||||
if value < low { low } else if value > high { high } else { value }
|
||||
}
|
||||
|
||||
// Local RMS for one slice of the recent waveform. Method syntax avoids
|
||||
// copying the complete sample array for every column.
|
||||
fn level(index, columns, reference) {
|
||||
let count = this.len();
|
||||
if count == 0 { return 0; }
|
||||
let start = index * count / columns;
|
||||
let end = (index + 1) * count / columns;
|
||||
let square = 0.0;
|
||||
let total = 0;
|
||||
for i in start..end {
|
||||
square += this[i] * this[i];
|
||||
total += 1;
|
||||
}
|
||||
let rms = if total > 0 { sqrt(square / to_float(total)) }
|
||||
else { abs(this[start]) };
|
||||
// Remove the near-silent baseline and spread useful levels over 0..4.
|
||||
// No additive bass/energy/beat floor: quiet columns really become empty.
|
||||
let normalized = clamp((rms / reference - 0.12) / 0.72, 0.0, 1.0);
|
||||
to_int(normalized * 4.0 + 0.5)
|
||||
}
|
||||
|
||||
fn render(input) {
|
||||
let commands = [#{ op: "clear", bg: "default" }];
|
||||
if input.width <= 0 || input.height <= 0 { return commands; }
|
||||
let samples = input.samples;
|
||||
let square = 0.0;
|
||||
for sample in samples { square += sample * sample; }
|
||||
let rms = if samples.len() > 0 { sqrt(square / to_float(samples.len())) } else { 0.0 };
|
||||
// Adapt headroom to the current recording, with an absolute noise floor
|
||||
// so silence/very quiet passages are never normalized into a full row.
|
||||
let reference = if rms * 1.8 > 0.10 { rms * 1.8 } else { 0.10 };
|
||||
let dots = ["⠀⢀⢠⢰⢸", "⡀⣀⣠⣰⣸", "⡄⣄⣤⣴⣼", "⡆⣆⣦⣶⣾", "⡇⣇⣧⣷⣿"];
|
||||
let columns = input.width * 2;
|
||||
for x in 0..input.width {
|
||||
let left = samples.level(x * 2, columns, reference);
|
||||
let right = samples.level(x * 2 + 1, columns, reference);
|
||||
let glyph = dots[left].sub_string(right, 1);
|
||||
commands.push(#{ op: "cell", x: x, y: 0, ch: glyph,
|
||||
fg: 0x48c9c0, bg: "default" });
|
||||
}
|
||||
commands
|
||||
}
|
||||
+353
-32
@@ -1,4 +1,4 @@
|
||||
//! Script-backed fullscreen visualizers.
|
||||
//! Script-backed fullscreen and status-bar visualizers.
|
||||
//!
|
||||
//! Rust owns the audio tap, script loading, sandboxed execution limits and
|
||||
//! terminal drawing primitives. Visual math lives in `.rhai` files: every
|
||||
@@ -11,6 +11,9 @@ use std::time::{Instant, SystemTime};
|
||||
|
||||
use anyhow::{Context as _, Result};
|
||||
use ratatui::Frame;
|
||||
use ratatui::buffer::Buffer;
|
||||
|
||||
pub mod status;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::{Color, Style};
|
||||
use ratatui::widgets::{Clear, Paragraph};
|
||||
@@ -20,12 +23,12 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::app::state::AppState;
|
||||
|
||||
const PRIMARY_SCRIPT_ID: &str = "scope_spectrum";
|
||||
const BUNDLE_VERSION: u32 = 5;
|
||||
|
||||
struct BundledScript {
|
||||
id: &'static str,
|
||||
file_name: &'static str,
|
||||
display_name: &'static str,
|
||||
version: u32,
|
||||
source: &'static str,
|
||||
}
|
||||
|
||||
@@ -34,14 +37,23 @@ const BUNDLED_SCRIPTS: &[BundledScript] = &[
|
||||
id: PRIMARY_SCRIPT_ID,
|
||||
file_name: "scope_spectrum.rhai",
|
||||
display_name: "Scope spectrum",
|
||||
version: 5,
|
||||
source: include_str!("visualizations/scope_spectrum.rhai"),
|
||||
},
|
||||
BundledScript {
|
||||
id: "pulsing_sphere",
|
||||
file_name: "pulsing_sphere.rhai",
|
||||
display_name: "Pulsing sphere",
|
||||
version: 5,
|
||||
source: include_str!("visualizations/pulsing_sphere.rhai"),
|
||||
},
|
||||
BundledScript {
|
||||
id: "morphing_cube",
|
||||
file_name: "morphing_cube.rhai",
|
||||
display_name: "Morphing cube",
|
||||
version: 6,
|
||||
source: include_str!("visualizations/morphing_cube.rhai"),
|
||||
},
|
||||
];
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -114,6 +126,9 @@ impl VisualizerState {
|
||||
}
|
||||
|
||||
pub fn load_library(&mut self) -> Result<()> {
|
||||
if let Err(err) = status::ensure_script() {
|
||||
tracing::warn!(%err, "status visualization initialization failed");
|
||||
}
|
||||
ensure_bundled_scripts()?;
|
||||
self.config = load_config().unwrap_or_else(|err| {
|
||||
tracing::warn!(%err, "visualization settings failed to load; using defaults");
|
||||
@@ -284,7 +299,10 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
}
|
||||
|
||||
fn input_map(area: Rect, state: &AppState) -> Map {
|
||||
let features = features_from_state(state);
|
||||
input_map_with_features(area, state, features_from_state(state, true))
|
||||
}
|
||||
|
||||
fn input_map_with_features(area: Rect, state: &AppState, features: AudioFeatures) -> Map {
|
||||
let mut input = Map::new();
|
||||
insert_int(&mut input, "width", i64::from(area.width));
|
||||
insert_int(&mut input, "height", i64::from(area.height));
|
||||
@@ -329,7 +347,7 @@ fn input_map(area: Rect, state: &AppState) -> Map {
|
||||
input
|
||||
}
|
||||
|
||||
fn features_from_state(state: &AppState) -> AudioFeatures {
|
||||
fn features_from_state(state: &AppState, scale_output: bool) -> AudioFeatures {
|
||||
let elapsed_secs = state
|
||||
.visualizer
|
||||
.started_at
|
||||
@@ -355,12 +373,18 @@ fn features_from_state(state: &AppState) -> AudioFeatures {
|
||||
.unwrap_or(0.17);
|
||||
let analysis = &state.player.audio_analysis;
|
||||
let volume = f64::from(state.player.volume.min(100)) / 100.0;
|
||||
let output_scale = if state.player.volume == 0 {
|
||||
let output_scale = if !scale_output {
|
||||
1.0
|
||||
} else if state.player.volume == 0 {
|
||||
0.0
|
||||
} else {
|
||||
0.35 + volume * 0.65
|
||||
};
|
||||
let active_scale = if state.player.paused { 0.12 } else { 1.0 };
|
||||
let active_scale = if scale_output && state.player.paused {
|
||||
0.12
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let scale = output_scale * active_scale;
|
||||
|
||||
AudioFeatures {
|
||||
@@ -373,7 +397,7 @@ fn features_from_state(state: &AppState) -> AudioFeatures {
|
||||
bass: (analysis.bass * scale).clamp(0.0, 1.0),
|
||||
mid: (analysis.mid * scale).clamp(0.0, 1.0),
|
||||
treble: (analysis.treble * scale).clamp(0.0, 1.0),
|
||||
beat: if state.player.paused {
|
||||
beat: if scale_output && state.player.paused {
|
||||
0.0
|
||||
} else {
|
||||
(analysis.beat * output_scale).clamp(0.0, 1.0)
|
||||
@@ -423,6 +447,10 @@ impl Default for ScriptRuntime {
|
||||
.set_max_string_size(1_000_000)
|
||||
.set_max_array_size(120_000)
|
||||
.set_max_map_size(1_000_000);
|
||||
engine.on_print(|message| tracing::debug!(message, "visualizer script"));
|
||||
engine.on_debug(|message, source, position| {
|
||||
tracing::debug!(message, ?source, ?position, "visualizer script")
|
||||
});
|
||||
engine.disable_symbol("import");
|
||||
engine.disable_symbol("export");
|
||||
engine.register_fn("sin", |value: FLOAT| value.sin());
|
||||
@@ -606,11 +634,15 @@ impl DrawCommand {
|
||||
}
|
||||
|
||||
fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
render_commands_to_buffer(frame.buffer_mut(), area, commands);
|
||||
}
|
||||
|
||||
fn render_commands_to_buffer(buffer: &mut Buffer, area: Rect, commands: &[DrawCommand]) {
|
||||
for command in commands {
|
||||
match command {
|
||||
DrawCommand::Clear { bg } => fill_rect(frame, area, " ", Style::new().bg(*bg)),
|
||||
DrawCommand::Clear { bg } => fill_rect(buffer, area, " ", Style::new().bg(*bg)),
|
||||
DrawCommand::Cell { x, y, ch, fg, bg } => {
|
||||
write_cell(frame, area, *x, *y, ch, Style::new().fg(*fg).bg(*bg));
|
||||
write_cell(buffer, area, *x, *y, ch, Style::new().fg(*fg).bg(*bg));
|
||||
}
|
||||
DrawCommand::HLine {
|
||||
x,
|
||||
@@ -621,8 +653,8 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
bg,
|
||||
} => {
|
||||
let style = Style::new().fg(*fg).bg(*bg);
|
||||
for offset in 0..*w {
|
||||
write_cell(frame, area, x.saturating_add(offset), *y, ch, style);
|
||||
for offset in 0..(*w).min(area.width.saturating_sub(*x)) {
|
||||
write_cell(buffer, area, x.saturating_add(offset), *y, ch, style);
|
||||
}
|
||||
}
|
||||
DrawCommand::VLine {
|
||||
@@ -634,8 +666,8 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
bg,
|
||||
} => {
|
||||
let style = Style::new().fg(*fg).bg(*bg);
|
||||
for offset in 0..*h {
|
||||
write_cell(frame, area, *x, y.saturating_add(offset), ch, style);
|
||||
for offset in 0..(*h).min(area.height.saturating_sub(*y)) {
|
||||
write_cell(buffer, area, *x, y.saturating_add(offset), ch, style);
|
||||
}
|
||||
}
|
||||
DrawCommand::Rect {
|
||||
@@ -648,10 +680,10 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
bg,
|
||||
} => {
|
||||
let style = Style::new().fg(*fg).bg(*bg);
|
||||
for row in 0..*h {
|
||||
for col in 0..*w {
|
||||
for row in 0..(*h).min(area.height.saturating_sub(*y)) {
|
||||
for col in 0..(*w).min(area.width.saturating_sub(*x)) {
|
||||
write_cell(
|
||||
frame,
|
||||
buffer,
|
||||
area,
|
||||
x.saturating_add(col),
|
||||
y.saturating_add(row),
|
||||
@@ -673,7 +705,12 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
let line_style = Style::new().fg(*line_fg).bg(*bg);
|
||||
let point_style = Style::new().fg(*fg).bg(*bg);
|
||||
let mut previous_y: Option<u16> = None;
|
||||
for (index, y) in ys.iter().copied().enumerate() {
|
||||
for (index, y) in ys
|
||||
.iter()
|
||||
.copied()
|
||||
.take(usize::from(area.width.saturating_sub(*x)))
|
||||
.enumerate()
|
||||
{
|
||||
let Ok(offset) = u16::try_from(index) else {
|
||||
break;
|
||||
};
|
||||
@@ -681,21 +718,25 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
if let Some(previous) = previous_y {
|
||||
let from = previous.min(y);
|
||||
let to = previous.max(y);
|
||||
for y in from..=to {
|
||||
write_cell(frame, area, x, y, line_ch, line_style);
|
||||
for y in from..=to.min(area.height.saturating_sub(1)) {
|
||||
write_cell(buffer, area, x, y, line_ch, line_style);
|
||||
}
|
||||
}
|
||||
write_cell(frame, area, x, y, ch, point_style);
|
||||
write_cell(buffer, area, x, y, ch, point_style);
|
||||
previous_y = Some(y);
|
||||
}
|
||||
}
|
||||
DrawCommand::Text { x, y, text, fg, bg } => {
|
||||
for (offset, ch) in text.chars().enumerate() {
|
||||
for (offset, ch) in text
|
||||
.chars()
|
||||
.take(usize::from(area.width.saturating_sub(*x)))
|
||||
.enumerate()
|
||||
{
|
||||
let Ok(offset) = u16::try_from(offset) else {
|
||||
break;
|
||||
};
|
||||
write_cell(
|
||||
frame,
|
||||
buffer,
|
||||
area,
|
||||
x.saturating_add(offset),
|
||||
*y,
|
||||
@@ -708,24 +749,21 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_rect(frame: &mut Frame, area: Rect, symbol: &'static str, style: Style) {
|
||||
fn fill_rect(buffer: &mut Buffer, area: Rect, symbol: &'static str, style: Style) {
|
||||
for y in area.y..area.y + area.height {
|
||||
for x in area.x..area.x + area.width {
|
||||
if let Some(cell) = frame.buffer_mut().cell_mut((x, y)) {
|
||||
if let Some(cell) = buffer.cell_mut((x, y)) {
|
||||
cell.set_symbol(symbol).set_style(style);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_cell(frame: &mut Frame, area: Rect, x: u16, y: u16, symbol: &str, style: Style) {
|
||||
fn write_cell(buffer: &mut Buffer, area: Rect, x: u16, y: u16, symbol: &str, style: Style) {
|
||||
if x >= area.width || y >= area.height {
|
||||
return;
|
||||
}
|
||||
if let Some(cell) = frame
|
||||
.buffer_mut()
|
||||
.cell_mut((area.x.saturating_add(x), area.y.saturating_add(y)))
|
||||
{
|
||||
if let Some(cell) = buffer.cell_mut((area.x.saturating_add(x), area.y.saturating_add(y))) {
|
||||
cell.set_symbol(symbol).set_style(style);
|
||||
}
|
||||
}
|
||||
@@ -774,6 +812,9 @@ fn rgb_color(value: INT) -> Color {
|
||||
}
|
||||
|
||||
fn parse_color(text: &str) -> Option<Color> {
|
||||
if text == "default" {
|
||||
return Some(Color::Reset);
|
||||
}
|
||||
let hex = text.trim().trim_start_matches('#').trim_start_matches("0x");
|
||||
let value = u32::from_str_radix(hex, 16).ok()?;
|
||||
Some(Color::Rgb(
|
||||
@@ -850,7 +891,7 @@ fn ensure_bundled_scripts() -> Result<()> {
|
||||
|
||||
fn should_replace_bundled_script(existing: &str, script: &BundledScript) -> bool {
|
||||
let name_marker = format!("// name: {}", script.display_name);
|
||||
let version_marker = format!("// bundle-version: {BUNDLE_VERSION}");
|
||||
let version_marker = format!("// bundle-version: {}", script.version);
|
||||
existing
|
||||
.lines()
|
||||
.take(3)
|
||||
@@ -918,9 +959,9 @@ mod tests {
|
||||
insert_int(&mut input, "analysis_sequence", 1);
|
||||
input.insert(
|
||||
"samples".into(),
|
||||
(0..128)
|
||||
(0..256)
|
||||
.map(|index| {
|
||||
let sample = ((index as f64 / 128.0) * std::f64::consts::TAU).sin();
|
||||
let sample = ((index as f64 / 256.0) * std::f64::consts::TAU).sin();
|
||||
Dynamic::from_float(sample as FLOAT)
|
||||
})
|
||||
.collect::<Array>()
|
||||
@@ -991,4 +1032,284 @@ mod tests {
|
||||
scope,
|
||||
));
|
||||
}
|
||||
|
||||
fn cube_file() -> tempfile::NamedTempFile {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(
|
||||
file.path(),
|
||||
include_str!("visualizations/morphing_cube.rhai"),
|
||||
)
|
||||
.unwrap();
|
||||
file
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_update_does_not_upgrade_other_presets() {
|
||||
let cube = BUNDLED_SCRIPTS
|
||||
.iter()
|
||||
.find(|script| script.id == "morphing_cube")
|
||||
.unwrap();
|
||||
let old_cube = cube
|
||||
.source
|
||||
.replacen("// bundle-version: 6", "// bundle-version: 5", 1);
|
||||
assert!(should_replace_bundled_script(&old_cube, cube));
|
||||
for script in BUNDLED_SCRIPTS {
|
||||
assert!(!should_replace_bundled_script(script.source, script));
|
||||
}
|
||||
let custom = old_cube.replacen("// name: Morphing cube", "// name: My cube", 1);
|
||||
assert!(!should_replace_bundled_script(&custom, cube));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_music_adds_particles_echoes_and_a_larger_pulse() {
|
||||
let file = cube_file();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
let mut input = test_input();
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
insert_float(&mut input, band, 0.0);
|
||||
}
|
||||
let quiet = runtime.render(file.path(), input.clone()).unwrap();
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
insert_float(&mut input, band, 0.8);
|
||||
}
|
||||
let loud = runtime.render(file.path(), input).unwrap();
|
||||
let effects = |commands: &[DrawCommand]| {
|
||||
commands
|
||||
.iter()
|
||||
.filter(|command| matches!(command, DrawCommand::Cell { ch, .. } if ch != "o"))
|
||||
.count()
|
||||
};
|
||||
assert!(
|
||||
effects(&loud) > effects(&quiet) + 30,
|
||||
"music should visibly populate the scene"
|
||||
);
|
||||
let lines = |commands: &[DrawCommand]| {
|
||||
commands
|
||||
.iter()
|
||||
.filter(|command| {
|
||||
matches!(
|
||||
command,
|
||||
DrawCommand::Trace { .. } | DrawCommand::VLine { .. }
|
||||
)
|
||||
})
|
||||
.count()
|
||||
};
|
||||
assert_eq!(
|
||||
lines(&loud),
|
||||
lines(&quiet) + 30,
|
||||
"two echoes and three rings"
|
||||
);
|
||||
let footprint = |commands: &[DrawCommand]| {
|
||||
let vertices = cube_vertices(commands);
|
||||
let xs: Vec<_> = vertices.iter().map(|p| p.0).collect();
|
||||
let ys: Vec<_> = vertices.iter().map(|p| p.1).collect();
|
||||
u32::from(xs.iter().max().unwrap() - xs.iter().min().unwrap())
|
||||
* u32::from(ys.iter().max().unwrap() - ys.iter().min().unwrap())
|
||||
};
|
||||
assert!(
|
||||
footprint(&loud) > footprint(&quiet) * 2,
|
||||
"pulse should be substantially larger"
|
||||
);
|
||||
}
|
||||
|
||||
fn cube_vertices(commands: &[DrawCommand]) -> Vec<(u16, u16)> {
|
||||
commands
|
||||
.iter()
|
||||
.filter_map(|command| match command {
|
||||
DrawCommand::Cell { x, y, ch, .. } if ch == "o" => Some((*x, *y)),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_rotates_and_each_audio_control_deforms_geometry() {
|
||||
let file = cube_file();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
let mut input = test_input();
|
||||
insert_int(&mut input, "width", 160);
|
||||
insert_int(&mut input, "height", 60);
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
insert_float(&mut input, band, 0.0);
|
||||
}
|
||||
let quiet = cube_vertices(&runtime.render(file.path(), input.clone()).unwrap());
|
||||
assert_eq!(quiet.len(), 8);
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
let mut loud = input.clone();
|
||||
insert_float(&mut loud, band, 1.0);
|
||||
let deformed = cube_vertices(&runtime.render(file.path(), loud).unwrap());
|
||||
assert_ne!(
|
||||
quiet, deformed,
|
||||
"{band} should move vertices, not just colors"
|
||||
);
|
||||
}
|
||||
insert_float(&mut input, "time", 3.0);
|
||||
let rotated = cube_vertices(&runtime.render(file.path(), input).unwrap());
|
||||
assert_ne!(quiet, rotated, "rotation should continue in silence");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_stays_bounded_across_sizes_times_and_audio_extremes() {
|
||||
let file = cube_file();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
// Much tighter than the production two-million-operation ceiling.
|
||||
runtime.engine.set_max_operations(200_000);
|
||||
for (width, height) in [
|
||||
(1, 1),
|
||||
(11, 7),
|
||||
(12, 8),
|
||||
(24, 80),
|
||||
(80, 24),
|
||||
(160, 50),
|
||||
(320, 100),
|
||||
] {
|
||||
for level in [0.0, 0.5, 1.0] {
|
||||
for tick in 0..12 {
|
||||
let mut input = test_input();
|
||||
insert_int(&mut input, "width", width);
|
||||
insert_int(&mut input, "height", height);
|
||||
insert_float(&mut input, "time", f64::from(tick) * 1.37);
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
insert_float(&mut input, band, level);
|
||||
}
|
||||
let commands = runtime.render(file.path(), input).unwrap();
|
||||
assert!(commands.len() <= 220);
|
||||
if width < 12 || height < 8 {
|
||||
assert_eq!(commands.len(), 1);
|
||||
continue;
|
||||
}
|
||||
assert_eq!(cube_vertices(&commands).len(), 8);
|
||||
let mut edges = 0;
|
||||
for command in &commands {
|
||||
match command {
|
||||
DrawCommand::Trace { x, ys, .. } => {
|
||||
edges += 1;
|
||||
assert!(usize::from(*x) + ys.len() <= width as usize);
|
||||
assert!(ys.iter().all(|y| i64::from(*y) < height));
|
||||
}
|
||||
DrawCommand::VLine { x, y, h, .. } => {
|
||||
edges += 1;
|
||||
assert!(i64::from(*x) < width);
|
||||
assert!(i64::from(*y) + i64::from(*h) <= height);
|
||||
}
|
||||
DrawCommand::Cell { x, y, .. } => {
|
||||
assert!(i64::from(*x) < width && i64::from(*y) < height);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
assert!((12..=54).contains(&edges));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_edges_handle_reversed_vertical_and_collapsed_projections() {
|
||||
let runtime = ScriptRuntime::default();
|
||||
let ast = runtime
|
||||
.engine
|
||||
.compile(include_str!("visualizations/morphing_cube.rhai"))
|
||||
.unwrap();
|
||||
for (ax, ay, bx, by) in [
|
||||
(2_i64, 3_i64, 8_i64, 9_i64),
|
||||
(8, 9, 2, 3),
|
||||
(2, 9, 8, 3),
|
||||
(8, 3, 2, 9),
|
||||
(4, 9, 4, 3),
|
||||
(4, 3, 4, 3),
|
||||
] {
|
||||
let output = runtime
|
||||
.engine
|
||||
.call_fn::<Dynamic>(
|
||||
&mut Scope::new(),
|
||||
&ast,
|
||||
"edge",
|
||||
(ax, ay, bx, by, 0xffffff_i64, 0_i64),
|
||||
)
|
||||
.unwrap();
|
||||
match DrawCommand::from_dynamic(output).unwrap() {
|
||||
DrawCommand::Trace { x, ys, .. } => {
|
||||
assert_eq!(i64::from(x), ax.min(bx));
|
||||
assert_eq!(ys.len(), (ax - bx).unsigned_abs() as usize + 1);
|
||||
let (left_y, right_y) = if ax < bx { (ay, by) } else { (by, ay) };
|
||||
assert_eq!(i64::from(ys[0]), left_y);
|
||||
assert_eq!(i64::from(*ys.last().unwrap()), right_y);
|
||||
}
|
||||
DrawCommand::VLine { x, y, h, .. } => {
|
||||
assert_eq!(i64::from(x), ax);
|
||||
assert_eq!(i64::from(y), ay.min(by));
|
||||
assert_eq!(u64::from(h), (ay - by).unsigned_abs() + 1);
|
||||
}
|
||||
other => panic!("unexpected edge: {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicit, reproducible CPU measurement; no audio hardware or user config.
|
||||
/// TestBackend includes buffer drawing/diff, but not terminal I/O/emulation.
|
||||
#[test]
|
||||
#[ignore = "manual benchmark: cargo test --release benchmark_visualizers -- --ignored --nocapture"]
|
||||
fn benchmark_visualizers() {
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
|
||||
for script in BUNDLED_SCRIPTS {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(file.path(), script.source).unwrap();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
for (width, height) in [(80_u16, 24_u16), (160, 50), (240, 80)] {
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
|
||||
let mut script_times = Vec::new();
|
||||
let mut frame_times = Vec::new();
|
||||
let mut max_commands = 0;
|
||||
for tick in 0..230 {
|
||||
let mut input = test_input();
|
||||
insert_int(&mut input, "width", i64::from(width));
|
||||
insert_int(&mut input, "height", i64::from(height));
|
||||
let time = f64::from(tick) * 0.05;
|
||||
insert_float(&mut input, "time", time);
|
||||
for (i, band) in ["energy", "bass", "mid", "treble", "beat"]
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
insert_float(&mut input, band, (time * (i + 1) as f64).sin() * 0.5 + 0.5);
|
||||
}
|
||||
let started = Instant::now();
|
||||
let commands = runtime.render(file.path(), input).unwrap();
|
||||
let script_elapsed = started.elapsed();
|
||||
terminal
|
||||
.draw(|frame| render_commands(frame, frame.area(), &commands))
|
||||
.unwrap();
|
||||
if tick >= 30 {
|
||||
script_times.push(script_elapsed.as_secs_f64() * 1000.0);
|
||||
frame_times.push(started.elapsed().as_secs_f64() * 1000.0);
|
||||
max_commands = max_commands.max(commands.len());
|
||||
}
|
||||
}
|
||||
script_times.sort_by(f64::total_cmp);
|
||||
frame_times.sort_by(f64::total_cmp);
|
||||
println!(
|
||||
"{} {width}x{height}: script p95={:.3}ms; frame median={:.3}ms p95={:.3}ms max={:.3}ms; commands<={max_commands}",
|
||||
script.id,
|
||||
script_times[189],
|
||||
frame_times[99],
|
||||
frame_times[189],
|
||||
frame_times[199]
|
||||
);
|
||||
if script.id == "morphing_cube" && width == 80 {
|
||||
for row in terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.chunks(usize::from(width))
|
||||
{
|
||||
println!(
|
||||
"{}",
|
||||
row.iter().map(|cell| cell.symbol()).collect::<String>()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// name: Status braille histogram
|
||||
// protocol: furumi-visualizer-v2
|
||||
// surface: status
|
||||
//
|
||||
// Always-on, one-row visualization. Edit this file to customize it; saved
|
||||
// changes reload during playback. The host never overwrites an existing file.
|
||||
// Same render(input) and draw commands as fullscreen scripts; height is 1.
|
||||
// Audio bands and waveform amplitudes drive the bars (not a full FFT).
|
||||
// Each Braille character packs two independent four-dot vertical columns.
|
||||
// "default" background retains the terminal's background color.
|
||||
|
||||
// A method reads the waveform through `this`, avoiding an array copy for
|
||||
// every column in this continuously running visualization.
|
||||
fn level(index, columns, band, energy, beat) {
|
||||
let wave = 0.0;
|
||||
let count = this.len();
|
||||
if count > 0 {
|
||||
let start = index * count / columns;
|
||||
let end = (index + 1) * count / columns;
|
||||
let total = 0;
|
||||
for i in start..end {
|
||||
wave += abs(this[i]);
|
||||
total += 1;
|
||||
}
|
||||
if total > 0 { wave /= to_float(total); }
|
||||
else { wave = abs(this[start]); }
|
||||
}
|
||||
let value = band * 0.55 + wave * 0.8 + energy * 0.12 + beat * 0.18;
|
||||
if value <= 0.01 { 0 }
|
||||
else if value < 0.20 { 1 }
|
||||
else if value < 0.40 { 2 }
|
||||
else if value < 0.65 { 3 }
|
||||
else { 4 }
|
||||
}
|
||||
|
||||
fn render(input) {
|
||||
let commands = [#{ op: "clear", bg: "default" }];
|
||||
if input.width <= 0 || input.height <= 0 || input.paused { return commands; }
|
||||
// Lookup table indexed by left/right bar heights, from the bottom up.
|
||||
let dots = ["⠀⢀⢠⢰⢸", "⡀⣀⣠⣰⣸", "⡄⣄⣤⣴⣼", "⡆⣆⣦⣶⣾", "⡇⣇⣧⣷⣿"];
|
||||
let columns = input.width * 2;
|
||||
let samples = input.samples;
|
||||
for x in 0..input.width {
|
||||
let levels = [];
|
||||
for side in 0..2 {
|
||||
let index = x * 2 + side;
|
||||
let phase = to_float(index) / to_float(columns);
|
||||
let band = if phase < 0.33 { input.bass }
|
||||
else if phase < 0.67 { input.mid } else { input.treble };
|
||||
levels.push(samples.level(index, columns, band, input.energy, input.beat));
|
||||
}
|
||||
let glyph = dots[levels[0]].sub_string(levels[1], 1);
|
||||
commands.push(#{ op: "cell", x: x, y: 0, ch: glyph,
|
||||
fg: 0x48c9c0, bg: "default" });
|
||||
}
|
||||
commands
|
||||
}
|
||||
@@ -0,0 +1,583 @@
|
||||
//! One-row surface: blocking preparation runs off the UI thread; views copy
|
||||
//! only the prepared buffer. Uses the fullscreen engine and drawing commands.
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
style::{Color, Style},
|
||||
widgets::Paragraph,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::{
|
||||
ScriptRuntime, features_from_state, input_map_with_features, render_commands_to_buffer,
|
||||
};
|
||||
use crate::app::{event::AppEvent, state::AppState};
|
||||
|
||||
const SOURCE: &str = include_str!("../visualizations/status/braille.rhai");
|
||||
const LEGACY_SOURCE: &str = include_str!("fixtures/status_braille_v1.rhai");
|
||||
|
||||
pub fn script_path() -> anyhow::Result<PathBuf> {
|
||||
Ok(super::visualizations_dir()?
|
||||
.join("status")
|
||||
.join("braille.rhai"))
|
||||
}
|
||||
|
||||
pub(super) fn ensure_script() -> anyhow::Result<()> {
|
||||
install_script_at(&script_path()?)
|
||||
}
|
||||
|
||||
fn install_script_at(path: &Path) -> anyhow::Result<()> {
|
||||
ensure_script_at(path)?;
|
||||
// Upgrade only a byte-for-byte stock script, never a user's edited one.
|
||||
if std::fs::metadata(path)?.len() == LEGACY_SOURCE.len() as u64
|
||||
&& std::fs::read(path)? == LEGACY_SOURCE.as_bytes()
|
||||
{
|
||||
let mut replacement =
|
||||
tempfile::NamedTempFile::new_in(path.parent().unwrap_or(Path::new(".")))?;
|
||||
replacement.write_all(SOURCE.as_bytes())?;
|
||||
replacement.as_file().sync_all()?;
|
||||
if std::fs::read(path)? == LEGACY_SOURCE.as_bytes() {
|
||||
replacement.persist(path)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_script_at(path: &Path) -> anyhow::Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
match std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(path)
|
||||
{
|
||||
Ok(mut file) => file.write_all(SOURCE.as_bytes())?,
|
||||
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {}
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn visible(state: &AppState) -> bool {
|
||||
state.player.playing && state.player.current.is_some() && state.device_playback.is_audio_owner()
|
||||
}
|
||||
|
||||
pub fn playing(state: &AppState) -> bool {
|
||||
visible(state) && !state.player.paused
|
||||
}
|
||||
|
||||
fn source_input(width: u16, state: &AppState, elapsed: f64) -> rhai::Map {
|
||||
// AnalyzedSource taps decoded audio before rodio's output volume. Keep
|
||||
// those source levels intact; input.volume remains available as metadata.
|
||||
let mut features = features_from_state(state, false);
|
||||
features.elapsed_secs = elapsed;
|
||||
input_map_with_features(Rect::new(0, 0, width, 1), state, features)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct State {
|
||||
target: Option<(u16, String)>,
|
||||
generation: u64,
|
||||
frame: Option<Buffer>,
|
||||
error: Option<String>,
|
||||
paused: bool,
|
||||
}
|
||||
|
||||
impl State {
|
||||
/// Invalidates in-flight results. A resize keeps the last frame until a
|
||||
/// replacement arrives, including when playback is paused.
|
||||
pub fn set_target(&mut self, target: Option<(u16, String)>) {
|
||||
if self.target != target {
|
||||
let same_track = self
|
||||
.target
|
||||
.as_ref()
|
||||
.zip(target.as_ref())
|
||||
.is_some_and(|(old, new)| old.1 == new.1);
|
||||
self.target = target;
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
if !same_track {
|
||||
self.frame = None;
|
||||
}
|
||||
self.error = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_paused(&mut self, paused: bool) {
|
||||
if self.paused != paused {
|
||||
self.paused = paused;
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply(&mut self, ready: Ready) {
|
||||
if ready.generation != self.generation || self.target.is_none() || self.paused {
|
||||
return;
|
||||
}
|
||||
match ready.result {
|
||||
Ok(frame) => {
|
||||
self.frame = Some(frame);
|
||||
self.error = None;
|
||||
}
|
||||
Err(error) => {
|
||||
if self.error.as_ref() != Some(&error) {
|
||||
tracing::warn!(%error, "status visualization failed");
|
||||
}
|
||||
self.frame = None;
|
||||
self.error = Some(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, frame: &mut Frame, area: Rect) {
|
||||
if area.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(buffer) = &self.frame {
|
||||
if buffer.area.width == 0 {
|
||||
return;
|
||||
}
|
||||
for x in 0..area.width {
|
||||
let source_x =
|
||||
usize::from(x) * usize::from(buffer.area.width) / usize::from(area.width);
|
||||
if let Some(target) = frame.buffer_mut().cell_mut((area.x + x, area.y)) {
|
||||
*target = buffer.content[source_x].clone();
|
||||
}
|
||||
}
|
||||
} else if let Some(error) = &self.error {
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("viz: {error}")).style(Style::new().fg(Color::Red)),
|
||||
area,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ready {
|
||||
generation: u64,
|
||||
result: Result<Buffer, String>,
|
||||
}
|
||||
|
||||
/// One in-flight job at most: slow custom scripts drop ticks, never queue
|
||||
/// frames or block input. AST caches are independent of fullscreen selection.
|
||||
pub struct Renderer {
|
||||
runtime: Arc<Mutex<ScriptRuntime>>,
|
||||
started: Instant,
|
||||
pub busy: bool,
|
||||
}
|
||||
|
||||
impl Default for Renderer {
|
||||
fn default() -> Self {
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
runtime
|
||||
.engine
|
||||
.set_max_operations(200_000)
|
||||
.set_max_array_size(8192)
|
||||
.set_max_map_size(4096)
|
||||
.set_max_string_size(32_768);
|
||||
Self {
|
||||
runtime: Arc::new(Mutex::new(runtime)),
|
||||
started: Instant::now(),
|
||||
busy: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
pub fn request(&mut self, state: &AppState, tx: mpsc::UnboundedSender<AppEvent>) {
|
||||
let Some((width, _)) = &state.status_visualizer.target else {
|
||||
return;
|
||||
};
|
||||
if self.busy || !playing(state) || state.visualizer.active {
|
||||
return;
|
||||
}
|
||||
self.busy = true;
|
||||
let width = *width;
|
||||
let generation = state.status_visualizer.generation;
|
||||
let input = source_input(width, state, self.started.elapsed().as_secs_f64());
|
||||
let runtime = Arc::clone(&self.runtime);
|
||||
tokio::spawn(async move {
|
||||
let result = tokio::task::spawn_blocking(move || {
|
||||
let path = script_path().map_err(|e| e.to_string())?;
|
||||
let mut runtime = runtime
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if runtime.cached_path.is_none() {
|
||||
// Retry initialization after a transient startup error.
|
||||
install_script_at(&path).map_err(|e| e.to_string())?;
|
||||
}
|
||||
prepare(&mut runtime, &path, input, width)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|err| Err(err.to_string()));
|
||||
let _ = tx.send(AppEvent::StatusVisualizationReady(Ready {
|
||||
generation,
|
||||
result,
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare(
|
||||
runtime: &mut ScriptRuntime,
|
||||
path: &Path,
|
||||
input: rhai::Map,
|
||||
width: u16,
|
||||
) -> Result<Buffer, String> {
|
||||
if std::fs::metadata(path).map_err(|e| e.to_string())?.len() > 65_536 {
|
||||
return Err("status script exceeds 64 KiB".into());
|
||||
}
|
||||
let commands = runtime.render(path, input)?;
|
||||
if commands.len() > 4096 {
|
||||
return Err("too many status drawing commands".into());
|
||||
}
|
||||
let area = Rect::new(0, 0, width, 1);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
render_commands_to_buffer(&mut buffer, area, &commands);
|
||||
// A user-supplied cell string must never spill into neighboring UI areas.
|
||||
for cell in &mut buffer.content {
|
||||
if cell.symbol().width() != 1 || cell.symbol().chars().any(char::is_control) {
|
||||
cell.set_symbol(" ");
|
||||
}
|
||||
}
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::insert_float;
|
||||
use super::*;
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
use rhai::{Array, Dynamic};
|
||||
|
||||
fn input(width: u16, level: f64) -> rhai::Map {
|
||||
let mut input = source_input(width, &AppState::default(), 0.0);
|
||||
for band in ["energy", "bass", "mid", "treble", "beat"] {
|
||||
insert_float(&mut input, band, level);
|
||||
}
|
||||
input.insert(
|
||||
"samples".into(),
|
||||
(0..256)
|
||||
.map(|index| {
|
||||
Dynamic::from_float(level * (index as f64 * std::f64::consts::TAU / 64.0).sin())
|
||||
})
|
||||
.collect::<Array>()
|
||||
.into(),
|
||||
);
|
||||
input
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn histogram_uses_braille_and_real_levels_in_one_row() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("status/braille.rhai");
|
||||
ensure_script_at(&path).unwrap();
|
||||
let renderer = Renderer::default();
|
||||
let mut runtime = renderer.runtime.lock().unwrap();
|
||||
for width in [0, 1, 8, 60, 200] {
|
||||
let quiet = prepare(&mut runtime, &path, input(width, 0.0), width).unwrap();
|
||||
let loud = prepare(&mut runtime, &path, input(width, 1.0), width).unwrap();
|
||||
assert_eq!(loud.area, Rect::new(0, 0, width, 1));
|
||||
assert!(quiet.content.iter().all(|cell| cell.symbol() == "⠀"));
|
||||
assert!(loud.content.iter().all(|cell| {
|
||||
cell.symbol()
|
||||
.chars()
|
||||
.all(|ch| ('\u{2800}'..='\u{28ff}').contains(&ch))
|
||||
&& cell.bg == Color::Reset
|
||||
}));
|
||||
if width > 0 {
|
||||
assert_ne!(loud, quiet);
|
||||
}
|
||||
let mut paused = input(width, 1.0);
|
||||
paused.insert("paused".into(), true.into());
|
||||
assert_eq!(prepare(&mut runtime, &path, paused, width).unwrap(), loud);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_user_script_survives_and_hot_reload_recovers_from_errors() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
let path = file.path();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
for (revision, symbol) in [(1, "A"), (2, "B"), (3, "bad"), (4, "C")] {
|
||||
let source = if symbol == "bad" {
|
||||
"fn render(".into()
|
||||
} else {
|
||||
format!(
|
||||
"fn render(input) {{ [#{{ op: \"text\", x: 0, y: 0, text: \"{symbol}\" }}] }}"
|
||||
)
|
||||
};
|
||||
std::fs::write(path, &source).unwrap();
|
||||
file.as_file()
|
||||
.set_times(std::fs::FileTimes::new().set_modified(
|
||||
std::time::UNIX_EPOCH
|
||||
+ std::time::Duration::from_secs(1_700_000_000 + revision),
|
||||
))
|
||||
.unwrap();
|
||||
install_script_at(path).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(path).unwrap(), source);
|
||||
let frame = prepare(&mut runtime, path, input(8, 0.0), 8);
|
||||
if symbol == "bad" {
|
||||
assert!(frame.is_err());
|
||||
} else {
|
||||
assert_eq!(frame.unwrap().content[0].symbol(), symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_commands_are_clipped_and_cannot_spill_into_the_ui() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(
|
||||
file.path(),
|
||||
r##"fn render(input) { [
|
||||
#{ op: "rect", x: 0, y: 0, w: 65535, h: 65535, ch: "#" },
|
||||
#{ op: "text", x: 0, y: 1, text: "outside" },
|
||||
#{ op: "trace", x: 6, ys: [65535, 0, 65535], ch: "*" },
|
||||
#{ op: "cell", x: 0, y: 0, ch: "界" },
|
||||
#{ op: "cell", x: 1, y: 0, ch: "\n" },
|
||||
#{ op: "cell", x: 2, y: 0, ch: "too long" }
|
||||
] }"##,
|
||||
)
|
||||
.unwrap();
|
||||
let buffer = prepare(&mut ScriptRuntime::default(), file.path(), input(8, 0.0), 8).unwrap();
|
||||
assert!(buffer.content[..3].iter().all(|cell| cell.symbol() == " "));
|
||||
let state = State {
|
||||
frame: Some(buffer),
|
||||
..State::default()
|
||||
};
|
||||
let mut terminal = Terminal::new(TestBackend::new(20, 4)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
for cell in &mut frame.buffer_mut().content {
|
||||
cell.set_symbol("x");
|
||||
}
|
||||
state.draw(frame, Rect::new(11, 3, 8, 1));
|
||||
})
|
||||
.unwrap();
|
||||
let buffer = terminal.backend().buffer();
|
||||
for y in 0..4 {
|
||||
for x in 0..20 {
|
||||
if y != 3 || !(11..19).contains(&x) {
|
||||
assert_eq!(buffer[(x, y)].symbol(), "x");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_frames_are_discarded_after_resize_track_change_or_stop() {
|
||||
let mut state = State::default();
|
||||
state.set_target(Some((8, "track-a".into())));
|
||||
let generation = state.generation;
|
||||
state.apply(Ready {
|
||||
generation,
|
||||
result: Ok(Buffer::empty(Rect::new(0, 0, 8, 1))),
|
||||
});
|
||||
assert!(state.frame.is_some());
|
||||
for target in [
|
||||
Some((12, "track-a".into())),
|
||||
Some((12, "track-b".into())),
|
||||
None,
|
||||
] {
|
||||
state.set_target(target);
|
||||
let previous_frame = state.frame.clone();
|
||||
state.apply(Ready {
|
||||
generation,
|
||||
result: Ok(Buffer::empty(Rect::new(0, 0, 8, 1))),
|
||||
});
|
||||
assert_eq!(state.frame, previous_frame);
|
||||
}
|
||||
assert!(state.frame.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pause_freezes_the_last_frame_and_rejects_in_flight_work() {
|
||||
let mut state = State::default();
|
||||
state.set_target(Some((8, "track".into())));
|
||||
let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 1));
|
||||
for cell in &mut buffer.content {
|
||||
cell.set_symbol("⣤");
|
||||
}
|
||||
let generation = state.generation;
|
||||
state.apply(Ready {
|
||||
generation,
|
||||
result: Ok(buffer.clone()),
|
||||
});
|
||||
state.set_paused(true);
|
||||
state.apply(Ready {
|
||||
generation,
|
||||
result: Err("late error".into()),
|
||||
});
|
||||
state.apply(Ready {
|
||||
generation: state.generation,
|
||||
result: Ok(Buffer::empty(buffer.area)),
|
||||
});
|
||||
assert_eq!(state.frame.as_ref(), Some(&buffer));
|
||||
// A paused resize adapts the frozen row instead of erasing it.
|
||||
state.set_target(Some((12, "track".into())));
|
||||
let mut terminal = Terminal::new(TestBackend::new(12, 1)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| state.draw(frame, frame.area()))
|
||||
.unwrap();
|
||||
assert!(
|
||||
terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.iter()
|
||||
.all(|cell| cell.symbol() == "⣤")
|
||||
);
|
||||
let paused_generation = state.generation;
|
||||
state.set_paused(false);
|
||||
state.apply(Ready {
|
||||
generation: paused_generation,
|
||||
result: Err("stale pause".into()),
|
||||
});
|
||||
assert_eq!(state.frame.as_ref(), Some(&buffer));
|
||||
state.apply(Ready {
|
||||
generation: state.generation,
|
||||
result: Ok(Buffer::empty(Rect::new(0, 0, 12, 1))),
|
||||
});
|
||||
assert_ne!(state.frame.as_ref(), Some(&buffer));
|
||||
state.set_target(None);
|
||||
assert!(state.frame.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_audio_input_is_independent_of_output_volume_and_pause() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(file.path(), SOURCE).unwrap();
|
||||
let mut state = AppState::default();
|
||||
state.player.audio_analysis = crate::player::AudioAnalysisSnapshot {
|
||||
energy: 0.8,
|
||||
bass: 0.9,
|
||||
mid: 0.6,
|
||||
treble: 0.3,
|
||||
beat: 0.5,
|
||||
scope: (0..256).map(|i| (i as f64 * 0.13).sin() * 0.7).collect(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
let expected =
|
||||
prepare(&mut runtime, file.path(), source_input(64, &state, 1.0), 64).unwrap();
|
||||
for volume in [0, 1, 25, 80, 100] {
|
||||
for paused in [false, true] {
|
||||
state.player.volume = volume;
|
||||
state.player.paused = paused;
|
||||
let map = source_input(64, &state, 1.0);
|
||||
for (key, level) in [
|
||||
("energy", 0.8),
|
||||
("bass", 0.9),
|
||||
("mid", 0.6),
|
||||
("treble", 0.3),
|
||||
("beat", 0.5),
|
||||
] {
|
||||
assert_eq!(map[key].as_float().unwrap(), level);
|
||||
}
|
||||
assert_eq!(map["volume"].as_float().unwrap(), f64::from(volume) / 100.0);
|
||||
assert_eq!(
|
||||
prepare(&mut runtime, file.path(), map, 64).unwrap(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loud_recordings_use_multiple_heights_instead_of_a_solid_row() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
|
||||
let old = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
|
||||
assert!(old.content.iter().all(|cell| cell.symbol() == "⣿"));
|
||||
install_script_at(file.path()).unwrap();
|
||||
// A fresh runtime also models a restart installing the new default.
|
||||
let mut runtime = ScriptRuntime::default();
|
||||
let new = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
|
||||
let symbols: std::collections::HashSet<_> =
|
||||
new.content.iter().map(|cell| cell.symbol()).collect();
|
||||
assert!(symbols.len() >= 4, "{symbols:?}");
|
||||
assert!(
|
||||
new.content
|
||||
.iter()
|
||||
.filter(|cell| cell.symbol() == "⣿")
|
||||
.count()
|
||||
< 32
|
||||
);
|
||||
assert!(new.content.iter().any(|cell| cell.symbol() == "⠀"));
|
||||
// Compressed band/beat values alone must not create a filled baseline.
|
||||
let mut silence = input(64, 1.0);
|
||||
silence.insert(
|
||||
"samples".into(),
|
||||
(0..256)
|
||||
.map(|_| Dynamic::from_float(0.001))
|
||||
.collect::<Array>()
|
||||
.into(),
|
||||
);
|
||||
let silent = prepare(&mut runtime, file.path(), silence, 64).unwrap();
|
||||
assert!(silent.content.iter().all(|cell| cell.symbol() == "⠀"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_untouched_previous_default_is_upgraded() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
|
||||
install_script_at(file.path()).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
|
||||
install_script_at(file.path()).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
|
||||
for stock in [LEGACY_SOURCE, SOURCE] {
|
||||
let custom = format!("{stock}\n// user customization\n");
|
||||
std::fs::write(file.path(), &custom).unwrap();
|
||||
install_script_at(file.path()).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), custom);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_script_infinite_loop_hits_the_operation_limit() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(file.path(), "fn render(input) { loop {} [] }").unwrap();
|
||||
let renderer = Renderer::default();
|
||||
let mut runtime = renderer.runtime.lock().unwrap();
|
||||
let err = prepare(&mut runtime, file.path(), input(8, 0.0), 8).unwrap_err();
|
||||
assert!(err.contains("operations"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "manual benchmark: cargo test --release benchmark_status_visualizer -- --ignored --nocapture"]
|
||||
fn benchmark_status_visualizer() {
|
||||
let file = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(file.path(), SOURCE).unwrap();
|
||||
let renderer = Renderer::default();
|
||||
let mut runtime = renderer.runtime.lock().unwrap();
|
||||
for width in [32, 64, 128] {
|
||||
let mut times = Vec::new();
|
||||
for tick in 0..230 {
|
||||
let level = (f64::from(tick) * 0.13).sin() * 0.5 + 0.5;
|
||||
let input = input(width, level);
|
||||
let start = Instant::now();
|
||||
let frame = prepare(&mut runtime, file.path(), input, width).unwrap();
|
||||
std::hint::black_box(frame);
|
||||
if tick >= 30 {
|
||||
times.push(start.elapsed().as_secs_f64() * 1000.0);
|
||||
}
|
||||
}
|
||||
times.sort_by(f64::total_cmp);
|
||||
println!(
|
||||
"status {width}x1: median={:.3}ms p95={:.3}ms max={:.3}ms",
|
||||
times[99], times[189], times[199]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user