forked from Furumi/furumi-tui
Compare commits
70
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e60195352 | ||
|
|
5a00a1c8e8 | ||
|
|
d1c89511ed | ||
|
|
2288392825 | ||
|
|
204d17f14c | ||
|
|
73a8e94c78 | ||
|
|
61027d22ca | ||
|
|
8606f08a36 | ||
|
|
aabca327b2 | ||
|
|
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 |
No files matched your search
@@ -61,8 +61,30 @@ update it only when dependencies change.
|
|||||||
- `src/player/` owns rodio playback and audio analysis.
|
- `src/player/` owns rodio playback and audio analysis.
|
||||||
- `src/federation/` owns DHT discovery, peer catalogs, audio exchange, and
|
- `src/federation/` owns DHT discovery, peer catalogs, audio exchange, and
|
||||||
federation caches.
|
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
|
- `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
|
- `src/ui/` renders `AppState`; rendering code must not start background work
|
||||||
or mutate persistence.
|
or mutate persistence.
|
||||||
- `src/media.rs` owns OS media controls.
|
- `src/media.rs` owns OS media controls.
|
||||||
@@ -155,7 +177,25 @@ When changing it:
|
|||||||
- keep snapshots able to repair peers that missed older operations;
|
- keep snapshots able to repair peers that missed older operations;
|
||||||
- treat membership and revocation as replicated durable state;
|
- treat membership and revocation as replicated durable state;
|
||||||
- keep playback commands targeted and deduplicated;
|
- 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
|
Do not replace eventual reconciliation with assumptions about a continuously
|
||||||
connected leader.
|
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.
|
- Use semantic `Action` values rather than checking raw keys inside views.
|
||||||
- Add default bindings in `src/config/default_keymap.toml`.
|
- Add default bindings in `src/config/default_keymap.toml`.
|
||||||
- Preserve user overrides and context-specific bindings.
|
- 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
|
- Account for narrow terminals, Unicode display width, and empty/loading/error
|
||||||
states.
|
states.
|
||||||
- Restore raw mode, alternate screen, bracketed paste, and keyboard
|
- 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`;
|
- pure state transitions in `app/update_tests.rs`;
|
||||||
- SQLite behavior and migrations in `library/tests.rs`;
|
- SQLite behavior and migrations in `library/tests.rs`;
|
||||||
- operation merge, tombstone, membership, and playback sync behavior in
|
- 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`;
|
- search/ranking/federation conversion in `federation/tests.rs`;
|
||||||
- protocol-specific tests beside `federation/audio.rs` or
|
- protocol-specific tests beside `federation/audio.rs` or
|
||||||
`federation/catalog.rs` when appropriate.
|
`federation/catalog.rs` when appropriate.
|
||||||
|
|||||||
+261
-6
@@ -66,12 +66,27 @@ This layer connects devices owned or trusted by the same user. It carries
|
|||||||
personal state such as:
|
personal state such as:
|
||||||
|
|
||||||
- likes and playlist operations;
|
- likes and playlist operations;
|
||||||
|
- podcast subscriptions, episode progress and listening sessions;
|
||||||
- device membership and revocation;
|
- device membership and revocation;
|
||||||
- acknowledgements and synchronization progress;
|
- acknowledgements and synchronization progress;
|
||||||
- playback state, commands, and handoff information.
|
- playback state, commands, and handoff information.
|
||||||
|
|
||||||
Pairing establishes the trust relationship. After that, changes are replicated
|
Pairing establishes the trust relationship. After that, changes are replicated
|
||||||
through an append-only operation log and applied to materialized local tables.
|
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
|
### 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 commands used by connected-device control and receive the host's
|
||||||
playback snapshot. They receive queue metadata, not audio.
|
playback snapshot. They receive queue metadata, not audio.
|
||||||
|
|
||||||
Jam never exchanges trusted membership, likes, playlists, or listening
|
Jam never exchanges trusted membership, likes, playlists, listening history
|
||||||
history. Volume remains local. Commands carry unique IDs and are retried until
|
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
|
the host acknowledges them, while inactive participants expire from the
|
||||||
runtime session. Regenerating the capability or restarting the host invalidates
|
runtime session. Regenerating the capability or restarting the host invalidates
|
||||||
the previous link.
|
the previous link.
|
||||||
@@ -189,6 +208,12 @@ The synchronization model combines:
|
|||||||
- per-peer acknowledgements to determine when old tombstones can be compacted;
|
- per-peer acknowledgements to determine when old tombstones can be compacted;
|
||||||
- snapshots to repair a peer that missed older operations.
|
- 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
|
This is eventual consistency scoped to a trusted device group. A temporarily
|
||||||
offline laptop can modify playlists, another device can continue playback, and
|
offline laptop can modify playlists, another device can continue playback, and
|
||||||
both can later converge without a permanently available coordinator.
|
both can later converge without a permanently available coordinator.
|
||||||
@@ -212,7 +237,25 @@ The synchronized state describes the queue, current item, position, pause
|
|||||||
state, volume, shuffle/repeat mode, and the active playback owner. Commands are
|
state, volume, shuffle/repeat mode, and the active playback owner. Commands are
|
||||||
targeted and deduplicated. Handoff transfers intent and position; the receiving
|
targeted and deduplicated. Handoff transfers intent and position; the receiving
|
||||||
device resolves the track against its own library or federation sources before
|
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
|
Active-device leases and idle timing prevent stale snapshots from immediately
|
||||||
taking control after a device reconnects. This provides practical coordination
|
taking control after a device reconnects. This provides practical coordination
|
||||||
@@ -257,6 +300,176 @@ The audio engine is similarly isolated. The application owns the logical
|
|||||||
queue, while `player::Controller` owns rodio playback and receives explicit
|
queue, while `player::Controller` owns rodio playback and receives explicit
|
||||||
commands. Prefetching prepares the next source before the current item ends.
|
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
|
## Scripted visualizations
|
||||||
|
|
||||||
Visualizations are an extension boundary rather than hard-coded rendering
|
Visualizations are an extension boundary rather than hard-coded rendering
|
||||||
@@ -268,6 +481,9 @@ rodio source
|
|||||||
│
|
│
|
||||||
v
|
v
|
||||||
audio analyzer ──> normalized features + scope samples
|
audio analyzer ──> normalized features + scope samples
|
||||||
|
│ (PCM history, channel levels)
|
||||||
|
v
|
||||||
|
display meter ───> third-octave spectrum, L/R levels, peak hold
|
||||||
│
|
│
|
||||||
v
|
v
|
||||||
Rhai render(input)
|
Rhai render(input)
|
||||||
@@ -286,6 +502,36 @@ script implements `render(input)` and returns declarative commands such as
|
|||||||
clear, cell, line, rectangle, trace, and text. Scripts never receive the
|
clear, cell, line, rectangle, trace, and text. Scripts never receive the
|
||||||
ratatui frame or audio engine directly.
|
ratatui frame or audio engine directly.
|
||||||
|
|
||||||
|
Protocol v3 adds what front-panel displays need: a third-octave spectrum
|
||||||
|
(31 bands, 20 Hz to 20 kHz, nominal ISO labels with exact base-2 edges that
|
||||||
|
partition the FFT bins) with per-band peak hold, left/right RMS and held
|
||||||
|
sample peaks, the RMS of the side signal (L - R) / 2 and a
|
||||||
|
stereo/mono decision made on loud windows, the analyzed sample rate, and track, queue and transport
|
||||||
|
details (format, bit depth, bitrate, track number, queue position, repeat,
|
||||||
|
shuffle, source), and the local wall-clock time. The audio thread only appends mono samples to a lock-free
|
||||||
|
ring and accumulates per-window channel levels; the FFT (an ~85 ms window
|
||||||
|
whose length follows the sample rate) and all ballistics run when the UI
|
||||||
|
takes a snapshot, under a mutex the audio thread never touches. Ballistics
|
||||||
|
are time-based (fast attack, steady fall, hold then fall): they advance with
|
||||||
|
the clock rather than per snapshot. Audio arrives in device-callback bursts,
|
||||||
|
so a snapshot that sees no new samples reuses the last measurement; only when
|
||||||
|
the PCM write counter stays still for longer than a few callbacks (pause,
|
||||||
|
stop) does the meter fall to the floor instead of freezing. In the control
|
||||||
|
role, and briefly when a track starts, the host passes an empty analysis, so
|
||||||
|
the meter reads zero. Meter values report the decoded signal on a fixed dBFS
|
||||||
|
scale and are therefore not scaled by the output volume, unlike the v2
|
||||||
|
energy features. Scripts choose their own display range within that scale.
|
||||||
|
|
||||||
|
v3 also lets a script keep state: `render` may return
|
||||||
|
`#{ commands, state }`, and the host passes `state` back with `dt` and a frame
|
||||||
|
counter on the next frame. State lives in the host's script runtime, is reset
|
||||||
|
whenever the script is recompiled or the visualizer is opened, and is subject
|
||||||
|
to the same sandbox size limits as any other value. Thin drawing comes from braille `dots` and `line`
|
||||||
|
commands in a 2 x 4 dot grid per cell (dots merge with braille already in the
|
||||||
|
cell; lines are clipped to the area first, so far endpoints cost nothing) and
|
||||||
|
from optional bold/dim/italic/underline/reversed attributes, which each
|
||||||
|
command sets for its cells rather than adding to what was drawn before.
|
||||||
|
|
||||||
This command boundary is intentional:
|
This command boundary is intentional:
|
||||||
|
|
||||||
- scripts remain independent of Rust UI internals;
|
- scripts remain independent of Rust UI internals;
|
||||||
@@ -325,8 +571,8 @@ is not a plugin mechanism for accessing the library, network, or player
|
|||||||
controls.
|
controls.
|
||||||
|
|
||||||
Fullscreen execution currently happens synchronously during UI drawing, with a fresh
|
Fullscreen execution currently happens synchronously during UI drawing, with a fresh
|
||||||
Rhai scope for each frame; scripts should derive motion from time and the
|
Rhai scope for each frame; retained state is limited to the explicit `state`
|
||||||
already-smoothed audio features rather than rely on retained mutable state.
|
value described above, and motion should still be derived from time and `dt`.
|
||||||
The 50 ms animation timer gives a nominal 20 FPS cadence (other events can
|
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
|
also trigger redraws). Operation limits are not wall-clock deadlines. The
|
||||||
host also checks the script modification time during drawing, so slow script
|
host also checks the script modification time during drawing, so slow script
|
||||||
@@ -384,6 +630,9 @@ Furumi stores different kinds of state according to their lifetime:
|
|||||||
| State | Storage | Role |
|
| State | Storage | Role |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| Library, playlists, likes, history | SQLite | Durable local source of truth |
|
| 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 |
|
| Device operation log and replicas | SQLite | Offline synchronization |
|
||||||
| Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data |
|
| Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data |
|
||||||
| Federation catalog cache | SQLite/cache | Faster network browsing |
|
| Federation catalog cache | SQLite/cache | Faster network browsing |
|
||||||
@@ -391,7 +640,8 @@ Furumi stores different kinds of state according to their lifetime:
|
|||||||
| Settings, keymap, identity | Platform config/data dirs | Node configuration |
|
| Settings, keymap, identity | Platform config/data dirs | Node configuration |
|
||||||
| Queue and playback snapshots | Application/device sync state | Continuity and handoff |
|
| 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
|
This distinction lets maintenance and recovery code discard derived network
|
||||||
data without risking the user's collection.
|
data without risking the user's collection.
|
||||||
|
|
||||||
@@ -420,6 +670,11 @@ The source tree follows the architectural responsibilities:
|
|||||||
- `similarity.rs` owns model acquisition, preprocessing, background indexing,
|
- `similarity.rs` owns model acquisition, preprocessing, background indexing,
|
||||||
and the replaceable exact in-memory index;
|
and the replaceable exact in-memory index;
|
||||||
- `federation/` owns DHT-facing search, peer catalogs, and audio exchange;
|
- `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;
|
- `devices.rs` owns trusted-device replication and playback coordination;
|
||||||
- `app/` owns state transitions and runtime orchestration;
|
- `app/` owns state transitions and runtime orchestration;
|
||||||
- `ui/` renders the TUI;
|
- `ui/` renders the TUI;
|
||||||
|
|||||||
+180
@@ -7,6 +7,186 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- "VFD panel" visualization: a vacuum-fluorescent hi-fi front panel with
|
||||||
|
thin segment digits, a stereo peak level meter (bars or needle VU meters),
|
||||||
|
a 25-band analyzer with three levels of glow, a nonlinear scale and peak
|
||||||
|
hold, a peak frequency readout with hold, tone fans, a signal path
|
||||||
|
schematic with a travelling pulse (source, format, volume, sample rate,
|
||||||
|
stereo or mono, and the output level in dBFS), the transport word in segment letters,
|
||||||
|
mode lamps (REPEAT, SHUFFLE, source, LOUDNESS, MUTE, SEARCH, OVER), a tape
|
||||||
|
counter and a dot-matrix title line (Latin and Cyrillic). Effects: a
|
||||||
|
power-on segment test, a counter "search" on track changes, static on
|
||||||
|
track and source changes, a source block that relights with its new
|
||||||
|
value, colours that fade into unlit phosphor on pause, a night dimmer, a
|
||||||
|
slow filament "breath", slightly uneven digit brightness and a standby
|
||||||
|
clock when nothing is loaded. Display modes "full", "spectrum",
|
||||||
|
"minimal" and "deck" switch with Shift+M or can be pinned with
|
||||||
|
`DISPLAY_MODE()` at the top of the script, like the palettes ("classic",
|
||||||
|
"green", "amber", "ice"); a mode that does not fit the window steps down.
|
||||||
|
A switch looks like hardware: the time and L/R meters stay lit, the other
|
||||||
|
sections go dark and light up one by one, and a row of mode lamps shows
|
||||||
|
the choice for a moment. The analyzer's display range follows the
|
||||||
|
loudness of the music (the measurements stay absolute), and deck mode has
|
||||||
|
AMS, NR, AUTO REVERSE and REW/FF lamps with a counter that races during a
|
||||||
|
seek. Bars settle with a little inertia, the analyzer's
|
||||||
|
tint runs from warm amber in the bass to light yellow in the treble, peak
|
||||||
|
marks fall with a little gravity after their hold, and the transport word
|
||||||
|
uses 14-segment letters with faint unlit segments.
|
||||||
|
- `Shift+M` in the fullscreen visualizer switches its display mode; the
|
||||||
|
choice is saved in `visualizations.toml`.
|
||||||
|
- Visualization protocol v3: a real third-octave spectrum (31 bands) with
|
||||||
|
peak hold, left/right RMS and held peaks, the side-signal level
|
||||||
|
(`level_side`, the floor for mono) and `channel_mode` ("stereo" or
|
||||||
|
"mono"), the sample rate, and track,
|
||||||
|
queue and transport details in the script input; `render` may return
|
||||||
|
`#{ commands, state }` to keep state between frames (with `dt` and
|
||||||
|
`frame`); new braille `dots` and `line` commands for thin drawing; bold,
|
||||||
|
dim, italic, underline and reversed attributes on any command. v2 scripts
|
||||||
|
keep working unchanged. Scripts also get the local time as
|
||||||
|
`clock_hour`, `clock_minute`, `clock_second` and `clock_millis`, and the
|
||||||
|
display mode as `mode`, a counter each script takes modulo its own number
|
||||||
|
of modes.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- The visualization clock shows local time instead of UTC.
|
||||||
|
|
||||||
|
## [0.5.0] - 2026-11-20
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Sharing a podcast in the federation (`Shift+F` in the `podcasts` context,
|
||||||
|
off by default): the synced share decision makes each device publish a
|
||||||
|
signed public card of the podcast in the FRID discovery overlay
|
||||||
|
(`podcast_dht`, advertised in the capability manifest); share-off and
|
||||||
|
unsubscribe revoke it with a tombstone. Feed URLs that look private are
|
||||||
|
published only after a confirmation on that device. The list and side
|
||||||
|
panel show whether a podcast is shared and whether this device publishes
|
||||||
|
it.
|
||||||
|
- Turning sharing off or unsubscribing revokes this device's card at once,
|
||||||
|
and quitting waits up to 4 s for that (also for a change still being
|
||||||
|
saved); the side panel says when a private podcast's card is not revoked
|
||||||
|
yet (pending or failed) or may not be (federation off: a card of an
|
||||||
|
earlier session cannot be ruled out). A privacy confirmation covers one
|
||||||
|
share decision: turning sharing off (or on again after that) on any
|
||||||
|
trusted device asks again, and until then this device holds the podcast
|
||||||
|
back and revokes the card it published for the earlier decision. A
|
||||||
|
confirmation whose decision could not be saved is dropped. Cards leave out
|
||||||
|
artwork and web links with an access key in the query or a member-feed
|
||||||
|
host (random-looking path parts of public CDNs are fine), and both for a
|
||||||
|
feed whose URL looks private.
|
||||||
|
- A device with other devices in its group publishes its cards only after
|
||||||
|
it synced with one of them since it started (or once none is left that
|
||||||
|
syncs podcasts), so a device that was closed while sharing was turned off
|
||||||
|
elsewhere does not send that card again; until then it only revokes, and
|
||||||
|
the side panel says it waits for a sync.
|
||||||
|
- Search (`/`, with the federation running) also finds public podcast cards.
|
||||||
|
They are listed after the music results under "Podcasts · public cards" with
|
||||||
|
a `[podcast]` marker, the number of publishing devices, and "✓ subscribed"
|
||||||
|
when this device is subscribed (also through a feed alias). The podcast
|
||||||
|
search runs apart from the federated music search (bounded at 15 s), so
|
||||||
|
music results never wait for it; local-only search is unchanged.
|
||||||
|
- Enter on a podcast hit opens its card: title, author, categories,
|
||||||
|
description, episode count, newest episode, publishing devices, feed URL
|
||||||
|
and unverified moved-feed claims. `e` reads the seasons and episodes from
|
||||||
|
the feed without subscribing. «Подписаться» (Enter) subscribes privately
|
||||||
|
with the card's origin feed URL, so the podcast id is the card's, and
|
||||||
|
reads everything from the feed itself; «Уже подписан» opens the podcast
|
||||||
|
in the Podcasts tab instead, also when this device follows the feed under
|
||||||
|
the newer address it moved to (known once the feed was read), and
|
||||||
|
subscribing to such an older address adds no second subscription.
|
||||||
|
- A Podcasts tab on `4`: subscribe to RSS/Atom feeds by URL, browse
|
||||||
|
podcast → seasons → episodes with saved position, played state, duration and
|
||||||
|
date, search episodes, filter by listening state, change the order, mark
|
||||||
|
episodes played or unplayed, check one or all feeds, and unsubscribe (progress
|
||||||
|
and history are kept). Feeds are checked in the background with conditional
|
||||||
|
requests and back-off; permanent redirects become feed aliases.
|
||||||
|
- Podcast data uses the shared FRID store (`furumi_library::podcasts`) in
|
||||||
|
`podcasts.sqlite3`; a database of the earlier podcast test build is migrated
|
||||||
|
in place, and a newer or foreign file is refused without changes.
|
||||||
|
- Podcasts sync between trusted devices (device-sync protocol 4):
|
||||||
|
subscriptions, unsubscribing, feed addresses, episode progress ("played",
|
||||||
|
position, "start over") and listening sessions. A podcast subscribed on one
|
||||||
|
device appears on the others without its URL; each device checks the feed
|
||||||
|
itself. Podcast operations are applied to the podcast store, kept in the
|
||||||
|
sync log and relayed; snapshots carry the podcast state (bounded) so new
|
||||||
|
devices and devices that upgrade from an older version catch up. Periodic
|
||||||
|
progress saves are coalesced in the log.
|
||||||
|
- A queue with podcast episodes is handed to other devices with the episode,
|
||||||
|
its position and pause; the receiving device does not need the
|
||||||
|
subscription.
|
||||||
|
- Podcast episodes play from the Podcasts tab and the shared queue (mixed
|
||||||
|
with music): audio streams from the publisher through a bounded HTTP Range
|
||||||
|
buffer, Play continues from the saved position, `b` starts over, and the
|
||||||
|
position is saved every 20 s and on pause, seek, stop, track change and
|
||||||
|
exit. Completion and listening sessions follow the shared FRID rules. A
|
||||||
|
stale audio URL triggers one feed check for the episode's new address. A
|
||||||
|
lost connection stops playback at the saved position (the episode is not
|
||||||
|
completed and the queue does not move on). Without a file size from the
|
||||||
|
server, a closed stream counts as the end within the completion window of
|
||||||
|
the duration the audio file states, or past 80 % of a duration only the
|
||||||
|
feed announced; a superseded or stalled stream
|
||||||
|
is cancelled at the next start or stop; pause pressed while an episode
|
||||||
|
connects is kept; seeks that do not move the audio are not saved.
|
||||||
|
- Podcast key bindings in the `podcasts` context: `o`, `/`, `Shift+R`,
|
||||||
|
`Ctrl+R`, `Shift+S`, `f`, `m`, `Shift+U`, `b`.
|
||||||
|
- An optional top-level `version` in `keymap.toml`; the current format is
|
||||||
|
`version = 2`, and a `podcasts` binding context.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `furumi-tui` 0.5.0.
|
||||||
|
- Use published `music-dht` 0.6.0 and `furumi-library` 0.3.0 from crates.io;
|
||||||
|
remove the temporary FRID fork patches.
|
||||||
|
- Self-update and release download links use the canonical
|
||||||
|
`Furumi-stack/furumi-tui` GitHub repository.
|
||||||
|
- Queue items are keyed by their source: podcast episodes by episode id,
|
||||||
|
library rows as `lib:<id>` instead of `local:<id>`. Likes, playlists,
|
||||||
|
music history, similarity, share links and metadata edits skip episodes.
|
||||||
|
- Device sync speaks protocol 4 and uses the shared `music_dht::device_sync`
|
||||||
|
wire types instead of private copies; v3 messages are unchanged byte for
|
||||||
|
byte (golden tests). Devices below protocol 4 never receive podcast
|
||||||
|
operations, podcast snapshot sections or a queue with episodes: handing
|
||||||
|
such a queue to them is refused ("needs a Furumi update"), and their
|
||||||
|
playback commands are ignored while the queue has episodes. They still get
|
||||||
|
the playback coordination, with each episode shown as "Private item", so
|
||||||
|
they do not take playback over after two minutes of silence. When such a
|
||||||
|
device does take over, the "Private item" entries it echoes back as music
|
||||||
|
are left out of the queue here, with a status message.
|
||||||
|
- Jam guests see the host's podcast episodes as "Private item" (no title,
|
||||||
|
feed or address). Guest changes that add, drop or duplicate them, or seek
|
||||||
|
in them, are refused; a guest leaving a Jam drops the placeholders.
|
||||||
|
- Controlling another v4 device may queue and play episodes there; an
|
||||||
|
episode started by another device's command at position 0 resumes from its
|
||||||
|
saved position.
|
||||||
|
- Tab order is now `1` Library, `2` Playlists, `3` Queue, `4` Podcasts,
|
||||||
|
`5` Settings, `6` Logs, for the tab bar, Tab/Shift+Tab, and the default
|
||||||
|
number keys.
|
||||||
|
- Keymap files without `version` keep their tab shortcuts: `GoToTab` indices
|
||||||
|
are remapped in memory (3 → 4 Settings, 4 → 5 Logs, and 5 → 3 Podcasts
|
||||||
|
from the prototype podcast build) with a non-blocking startup notice. The
|
||||||
|
file is not rewritten; `version = 2` files are used as written. A `version`
|
||||||
|
key placed inside a `[[keymaps]]` entry is reported as an error instead of
|
||||||
|
being ignored.
|
||||||
|
- The tab bar shortens titles, then shows only numbers (keeping the active
|
||||||
|
tab's full title) on terminals too narrow for all six tabs.
|
||||||
|
- A keymap notice no longer hides a "logging disabled" startup warning; both
|
||||||
|
are shown.
|
||||||
|
- The help window names the target tab for number-key shortcuts
|
||||||
|
("Go to Settings") instead of "Go to tab N".
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Isolate log privacy and identity-lock lifecycle tests from process-wide
|
||||||
|
state and descriptor inheritance in parallel tests.
|
||||||
|
- The protocol version stored for another device came from this build's own
|
||||||
|
constant (on profile announcements and accepted pairings), not from the
|
||||||
|
device. Versions now come from the device's own reports; relayed profiles
|
||||||
|
only raise them. Invites and profile announcements carry the version, and
|
||||||
|
the first start of a new version announces it.
|
||||||
|
|
||||||
## [0.2.8] - 2026-09-02
|
## [0.2.8] - 2026-09-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
Generated
+19
-15
@@ -1143,7 +1143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "c54e03a951783e8b327515db3f2a2fd0e3bed362a96b066f341ce66ed49b4ead"
|
checksum = "c54e03a951783e8b327515db3f2a2fd0e3bed362a96b066f341ce66ed49b4ead"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"data-encoding",
|
"data-encoding",
|
||||||
"syn 3.0.4",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1269,7 +1269,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"option-ext",
|
"option-ext",
|
||||||
"redox_users",
|
"redox_users",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1455,7 +1455,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1698,9 +1698,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "furumi-library"
|
name = "furumi-library"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f78466b6345cb90d7bb37db5d9dd67476ab6c2c3b2202876d24f448c314a9a08"
|
checksum = "7e7eb785cc6ab5b59db8ef74ffa05d798afde45fbd86da08aecf356432a52d10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"blake3",
|
"blake3",
|
||||||
@@ -1715,11 +1715,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "furumi_tui"
|
name = "furumi_tui"
|
||||||
version = "0.3.5"
|
version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
"blake3",
|
"blake3",
|
||||||
|
"chrono",
|
||||||
"core-foundation 0.10.1",
|
"core-foundation 0.10.1",
|
||||||
"crokey",
|
"crokey",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
@@ -3239,16 +3240,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "music-dht"
|
name = "music-dht"
|
||||||
version = "0.5.0"
|
version = "0.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4c3f75d49d3a742a6777a1c4cc53f397399dfc9ddb70034c57aeb44d75d2ee4f"
|
checksum = "7ce81f361952228654c28a46e41555f2c3d1520bf8c6c76562e7a53f70dd38e1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"blake3",
|
"blake3",
|
||||||
"data-encoding",
|
"data-encoding",
|
||||||
|
"encoding_rs",
|
||||||
"federation-net",
|
"federation-net",
|
||||||
"futures",
|
"futures",
|
||||||
"postcard",
|
"postcard",
|
||||||
|
"quick-xml",
|
||||||
"rand 0.9.5",
|
"rand 0.9.5",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -3257,6 +3260,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"unicode-normalization",
|
"unicode-normalization",
|
||||||
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3654,7 +3658,7 @@ version = "0.50.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3739,7 +3743,7 @@ version = "0.7.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8"
|
checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro-crate 3.5.0",
|
"proc-macro-crate 1.3.1",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.119",
|
"syn 2.0.119",
|
||||||
@@ -4537,7 +4541,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"socket2 0.6.5",
|
"socket2 0.6.5",
|
||||||
"tracing",
|
"tracing",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -5053,7 +5057,7 @@ dependencies = [
|
|||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys 0.12.1",
|
"linux-raw-sys 0.12.1",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -5111,7 +5115,7 @@ dependencies = [
|
|||||||
"security-framework",
|
"security-framework",
|
||||||
"security-framework-sys",
|
"security-framework-sys",
|
||||||
"webpki-root-certs",
|
"webpki-root-certs",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -5925,7 +5929,7 @@ dependencies = [
|
|||||||
"getrandom 0.4.3",
|
"getrandom 0.4.3",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix 1.1.4",
|
"rustix 1.1.4",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -7029,7 +7033,7 @@ version = "0.1.11"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
+6
-4
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "furumi_tui"
|
name = "furumi_tui"
|
||||||
version = "0.3.5"
|
version = "0.5.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.97"
|
rust-version = "1.97"
|
||||||
description = "A federated P2P player for personal music libraries"
|
description = "A federated P2P player for personal music libraries"
|
||||||
@@ -23,17 +23,19 @@ object = { version = "0.36", default-features = false, features = ["read", "std"
|
|||||||
|
|
||||||
anyhow = "1.0.102"
|
anyhow = "1.0.102"
|
||||||
blake3 = "1"
|
blake3 = "1"
|
||||||
|
# Local wall-clock time for visualizations (the clock and its hour/minute).
|
||||||
|
chrono = { version = "0.4.45", default-features = false, features = ["clock"] }
|
||||||
crokey = "1.4.0"
|
crokey = "1.4.0"
|
||||||
crossterm = { version = "0.29.0", features = ["event-stream"] }
|
crossterm = { version = "0.29.0", features = ["event-stream"] }
|
||||||
directories = "6.0.0"
|
directories = "6.0.0"
|
||||||
futures-util = "0.3.32"
|
futures-util = "0.3.32"
|
||||||
furumi-library = "0.2.0"
|
furumi-library = "0.3.0"
|
||||||
image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] }
|
image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] }
|
||||||
lofty = "0.22"
|
lofty = "0.22"
|
||||||
# P2P federation: library index in a shared DHT + audio streaming between
|
# P2P federation: library index in a shared DHT + audio streaming between
|
||||||
# peers (same protocol as furumi-fd).
|
# peers (same protocol as furumi-fd).
|
||||||
music-dht = "0.5.0"
|
music-dht = { version = "0.6.0", features = ["podcast-feed"] }
|
||||||
ratatui = "0.30.1"
|
ratatui = { version = "0.30.1", features = ["unstable-rendered-line-info"] }
|
||||||
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
|
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
|
||||||
rhai = { version = "1", features = ["sync"] }
|
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"] }
|
rodio = { version = "0.22.2", default-features = false, features = ["playback", "mp3", "flac", "vorbis", "wav", "symphonia-aac", "symphonia-isomp4", "symphonia-alac"] }
|
||||||
|
|||||||
@@ -67,7 +67,10 @@ context-aware bindings, and user-defined rebinding through TOML. Built-in
|
|||||||
audio visualizations, OS media controls, gapless queue playback, and local
|
audio visualizations, OS media controls, gapless queue playback, and local
|
||||||
library management are included. Visualizations are runtime-loadable Rhai
|
library management are included. Visualizations are runtime-loadable Rhai
|
||||||
scripts executed in a resource-limited sandbox, so they can be added or edited
|
scripts executed in a resource-limited sandbox, so they can be added or edited
|
||||||
without rebuilding the player.
|
without rebuilding the player. The bundled "VFD panel" preset draws a hi-fi
|
||||||
|
front panel from a real third-octave spectrum and stereo level meter; scripts
|
||||||
|
can use the same spectrum, levels, track details, thin braille lines and
|
||||||
|
per-frame state.
|
||||||
|
|
||||||
Optional similarity search calculates versioned embeddings for local tracks
|
Optional similarity search calculates versioned embeddings for local tracks
|
||||||
in the background and keeps them in SQLite. It works offline; after a separate
|
in the background and keeps them in SQLite. It works offline; after a separate
|
||||||
@@ -98,7 +101,7 @@ furumi
|
|||||||
### Linux, Windows, and other platforms
|
### Linux, Windows, and other platforms
|
||||||
|
|
||||||
Download a prebuilt archive from the
|
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:
|
build Furumi from source with Rust 1.97 or newer:
|
||||||
|
|
||||||
The repository selects the stable Rust toolchain. With rustup, run
|
The repository selects the stable Rust toolchain. With rustup, run
|
||||||
@@ -149,6 +152,235 @@ 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
|
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.
|
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
|
### Now playing in tmux
|
||||||
|
|
||||||
While Furumi is running, a second invocation can print a cheap, single-line
|
While Furumi is running, a second invocation can print a cheap, single-line
|
||||||
@@ -229,18 +461,23 @@ owner_timeout_ms = 120000
|
|||||||
|
|
||||||
All clients must support the new coordination envelope for eventual single
|
All clients must support the new coordination envelope for eventual single
|
||||||
output ownership. frid's `PLAYBACK_PROTOCOL.md` describes the protocol, adapter
|
output ownership. frid's `PLAYBACK_PROTOCOL.md` describes the protocol, adapter
|
||||||
contract and rollout. Local Cargo patches are only for development; publish
|
contract and rollout. This release uses the published FRID crates
|
||||||
frid and bump the client dependency versions before releasing these changes.
|
`music-dht 0.6.0` and `furumi-library 0.3.0` from crates.io.
|
||||||
|
|
||||||
Run the local protocol checks from the TUI repository:
|
Run the local protocol checks from the TUI repository:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo test localhost_devices_exchange_state_and_handoff
|
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
|
python scripts/test_device_interop.py ../furumusic
|
||||||
```
|
```
|
||||||
|
|
||||||
The first test uses real iroh streams, isolated SQLite databases, ownership
|
The first test uses real iroh streams, isolated SQLite databases, ownership
|
||||||
handoff, and a stalled peer. The second builds both player test binaries and
|
handoff, and a stalled peer. The podcast tests sync subscriptions, progress
|
||||||
|
and "start over" between TUI device stores (over iroh streams and through
|
||||||
|
the JSON messages), check a handoff with an episode and simulate a device
|
||||||
|
sync v3 peer with the exact JSON types of furumi-tui 0.3.x. The second builds both player test binaries and
|
||||||
exchanges their actual JSON messages over `127.0.0.1`: queue metadata,
|
exchanges their actual JSON messages over `127.0.0.1`: queue metadata,
|
||||||
bidirectional handoff, pause/seek and duplicate command fencing. It exercises
|
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
|
the TUI command adapter and web player hub, but does not start a browser or
|
||||||
|
|||||||
+53
-3
@@ -32,6 +32,7 @@ pub enum Action {
|
|||||||
ToggleShuffle,
|
ToggleShuffle,
|
||||||
CycleRepeat,
|
CycleRepeat,
|
||||||
ToggleVisualizer,
|
ToggleVisualizer,
|
||||||
|
CycleVisualizerMode,
|
||||||
ToggleLike,
|
ToggleLike,
|
||||||
ToggleTrackSelection,
|
ToggleTrackSelection,
|
||||||
OpenTrackInfo,
|
OpenTrackInfo,
|
||||||
@@ -57,6 +58,27 @@ pub enum Action {
|
|||||||
OpenSearch,
|
OpenSearch,
|
||||||
EditSelected,
|
EditSelected,
|
||||||
DeleteSelected,
|
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.
|
/// Help-window sections, in display order.
|
||||||
@@ -66,16 +88,18 @@ pub enum Category {
|
|||||||
Queue,
|
Queue,
|
||||||
Library,
|
Library,
|
||||||
Navigation,
|
Navigation,
|
||||||
|
Podcasts,
|
||||||
Search,
|
Search,
|
||||||
System,
|
System,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Category {
|
impl Category {
|
||||||
pub const ALL: [Category; 6] = [
|
pub const ALL: [Category; 7] = [
|
||||||
Category::Playback,
|
Category::Playback,
|
||||||
Category::Queue,
|
Category::Queue,
|
||||||
Category::Library,
|
Category::Library,
|
||||||
Category::Navigation,
|
Category::Navigation,
|
||||||
|
Category::Podcasts,
|
||||||
Category::Search,
|
Category::Search,
|
||||||
Category::System,
|
Category::System,
|
||||||
];
|
];
|
||||||
@@ -86,6 +110,7 @@ impl Category {
|
|||||||
Category::Queue => "Queue & playlists",
|
Category::Queue => "Queue & playlists",
|
||||||
Category::Library => "Library",
|
Category::Library => "Library",
|
||||||
Category::Navigation => "Navigation",
|
Category::Navigation => "Navigation",
|
||||||
|
Category::Podcasts => "Podcasts",
|
||||||
Category::Search => "Search & commands",
|
Category::Search => "Search & commands",
|
||||||
Category::System => "System",
|
Category::System => "System",
|
||||||
}
|
}
|
||||||
@@ -105,6 +130,7 @@ impl Action {
|
|||||||
| Action::ToggleShuffle
|
| Action::ToggleShuffle
|
||||||
| Action::CycleRepeat
|
| Action::CycleRepeat
|
||||||
| Action::ToggleVisualizer
|
| Action::ToggleVisualizer
|
||||||
|
| Action::CycleVisualizerMode
|
||||||
| Action::OpenConnectedDevices
|
| Action::OpenConnectedDevices
|
||||||
| Action::OpenListenHistory => Category::Playback,
|
| Action::OpenListenHistory => Category::Playback,
|
||||||
Action::QueueAddNext
|
Action::QueueAddNext
|
||||||
@@ -139,6 +165,16 @@ impl Action {
|
|||||||
| Action::DeleteSelected
|
| Action::DeleteSelected
|
||||||
| Action::CycleSourceMode
|
| Action::CycleSourceMode
|
||||||
| Action::OpenLibraryFilters => Category::Library,
|
| 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::OpenSearch | Action::OpenCommandLine => Category::Search,
|
||||||
Action::ToggleHelp | Action::Quit => Category::System,
|
Action::ToggleHelp | Action::Quit => Category::System,
|
||||||
}
|
}
|
||||||
@@ -170,7 +206,10 @@ impl Action {
|
|||||||
Action::Quit => "Quit".into(),
|
Action::Quit => "Quit".into(),
|
||||||
Action::NextTab => "Next tab".into(),
|
Action::NextTab => "Next tab".into(),
|
||||||
Action::PrevTab => "Previous 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::MoveUp => "Move up".into(),
|
||||||
Action::MoveDown => "Move down".into(),
|
Action::MoveDown => "Move down".into(),
|
||||||
Action::MoveLeft => "Move left".into(),
|
Action::MoveLeft => "Move left".into(),
|
||||||
@@ -191,6 +230,7 @@ impl Action {
|
|||||||
Action::ToggleShuffle => "Toggle shuffle".into(),
|
Action::ToggleShuffle => "Toggle shuffle".into(),
|
||||||
Action::CycleRepeat => "Cycle repeat mode".into(),
|
Action::CycleRepeat => "Cycle repeat mode".into(),
|
||||||
Action::ToggleVisualizer => "Toggle fullscreen visualizer".into(),
|
Action::ToggleVisualizer => "Toggle fullscreen visualizer".into(),
|
||||||
|
Action::CycleVisualizerMode => "Next visualizer display mode".into(),
|
||||||
Action::ToggleLike => "Like / unlike".into(),
|
Action::ToggleLike => "Like / unlike".into(),
|
||||||
Action::ToggleTrackSelection => "Track line selection".into(),
|
Action::ToggleTrackSelection => "Track line selection".into(),
|
||||||
Action::OpenTrackInfo => "Track info".into(),
|
Action::OpenTrackInfo => "Track info".into(),
|
||||||
@@ -212,9 +252,19 @@ impl Action {
|
|||||||
Action::CycleSourceMode => "Cycle source mode: Local / My / Global".into(),
|
Action::CycleSourceMode => "Cycle source mode: Local / My / Global".into(),
|
||||||
Action::OpenLibraryFilters => "Library filters…".into(),
|
Action::OpenLibraryFilters => "Library filters…".into(),
|
||||||
Action::OpenCommandLine => "Command line (:help for commands)".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::EditSelected => "Edit the selected item".into(),
|
||||||
Action::DeleteSelected => "Delete 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.results = None;
|
||||||
state.search.fed_tracks.clear();
|
state.search.fed_tracks.clear();
|
||||||
state.search.fed_artists.clear();
|
state.search.fed_artists.clear();
|
||||||
|
state.search.fed_podcasts.clear();
|
||||||
state.search.fed_loading = false;
|
state.search.fed_loading = false;
|
||||||
|
state.search.fed_podcasts_loading = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let library = Arc::clone(&runtime.library);
|
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.
|
// node is up); its results render as a separate, marked section.
|
||||||
state.search.fed_tracks.clear();
|
state.search.fed_tracks.clear();
|
||||||
state.search.fed_artists.clear();
|
state.search.fed_artists.clear();
|
||||||
|
state.search.fed_podcasts.clear();
|
||||||
state.search.fed_loading = false;
|
state.search.fed_loading = false;
|
||||||
|
state.search.fed_podcasts_loading = false;
|
||||||
if runtime.federation.settings().enabled {
|
if runtime.federation.settings().enabled {
|
||||||
state.search.fed_loading = true;
|
state.search.fed_loading = true;
|
||||||
let fed = Arc::clone(&runtime.federation);
|
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 result = fed.search(&query).await.map_err(|err| format!("{err:#}"));
|
||||||
let _ = tx.send(AppEvent::FedSearchLoaded { seq, result });
|
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(
|
pub(super) fn schedule_similarity_search(
|
||||||
state: &mut AppState,
|
state: &mut AppState,
|
||||||
runtime: &Runtime,
|
runtime: &Runtime,
|
||||||
@@ -171,7 +213,9 @@ pub(super) fn schedule_similarity_search(
|
|||||||
state.search.results = None;
|
state.search.results = None;
|
||||||
state.search.fed_tracks.clear();
|
state.search.fed_tracks.clear();
|
||||||
state.search.fed_artists.clear();
|
state.search.fed_artists.clear();
|
||||||
|
state.search.fed_podcasts.clear();
|
||||||
state.search.fed_loading = false;
|
state.search.fed_loading = false;
|
||||||
|
state.search.fed_podcasts_loading = false;
|
||||||
state.active_tab = crate::app::state::Tab::Global;
|
state.active_tab = crate::app::state::Tab::Global;
|
||||||
if let Some(crate::app::state::GlobalView::Search { cursor }) = state.global.stack.last_mut() {
|
if let Some(crate::app::state::GlobalView::Search { cursor }) = state.global.stack.last_mut() {
|
||||||
*cursor = 0;
|
*cursor = 0;
|
||||||
|
|||||||
+91
-2
@@ -130,13 +130,21 @@ pub enum AppEvent {
|
|||||||
tracks: Vec<TrackItem>,
|
tracks: Vec<TrackItem>,
|
||||||
},
|
},
|
||||||
/// A status snapshot for the Federation tab.
|
/// 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,
|
/// Federated live-search results (artists a card can be opened for,
|
||||||
/// plus matching tracks).
|
/// plus matching tracks).
|
||||||
FedSearchLoaded {
|
FedSearchLoaded {
|
||||||
seq: u64,
|
seq: u64,
|
||||||
result: Result<crate::federation::FedSearchResults, String>,
|
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.
|
/// A federated artist card finished assembling.
|
||||||
FedArtistLoaded {
|
FedArtistLoaded {
|
||||||
name: String,
|
name: String,
|
||||||
@@ -172,7 +180,8 @@ pub enum AppEvent {
|
|||||||
FedTrackInfoLoaded {
|
FedTrackInfoLoaded {
|
||||||
placeholder_id: i64,
|
placeholder_id: i64,
|
||||||
item_id: String,
|
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.
|
/// This peer's connection ticket, requested from the Federation tab.
|
||||||
FedTicket(Result<String, String>),
|
FedTicket(Result<String, String>),
|
||||||
@@ -206,4 +215,84 @@ pub enum AppEvent {
|
|||||||
JamInvite(Result<String, String>),
|
JamInvite(Result<String, String>),
|
||||||
/// Result of joining a Jam capability.
|
/// Result of joining a Jam capability.
|
||||||
JamJoined(Result<String, String>),
|
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>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
+548
-53
@@ -4,6 +4,9 @@ pub mod command;
|
|||||||
mod enrichment;
|
mod enrichment;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) mod podcast_fixtures;
|
||||||
|
pub(crate) mod podcast_runtime;
|
||||||
pub(crate) mod popup;
|
pub(crate) mod popup;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod update;
|
pub mod update;
|
||||||
@@ -24,8 +27,10 @@ use tokio::sync::mpsc;
|
|||||||
use tokio::time::MissedTickBehavior;
|
use tokio::time::MissedTickBehavior;
|
||||||
|
|
||||||
use crate::config::keymap::{KeyResolution, Keymap};
|
use crate::config::keymap::{KeyResolution, Keymap};
|
||||||
|
use crate::devices::PlaybackTrackExt as _;
|
||||||
use crate::library::Library;
|
use crate::library::Library;
|
||||||
use crate::player;
|
use crate::player;
|
||||||
|
use crate::podcasts::playback::StartAt;
|
||||||
use crate::ui;
|
use crate::ui;
|
||||||
use event::AppEvent;
|
use event::AppEvent;
|
||||||
use state::AppState;
|
use state::AppState;
|
||||||
@@ -77,6 +82,11 @@ pub struct Runtime {
|
|||||||
pub last_media_push: Option<std::time::Instant>,
|
pub last_media_push: Option<std::time::Instant>,
|
||||||
pub status_publisher: crate::status::Publisher,
|
pub status_publisher: crate::status::Publisher,
|
||||||
pub status_visualizer: crate::visualizer::status::Renderer,
|
pub status_visualizer: crate::visualizer::status::Renderer,
|
||||||
|
/// Podcast store and feed client; `None` until opened (or if opening
|
||||||
|
/// failed, which only disables the Podcasts tab).
|
||||||
|
pub podcasts: Option<podcast_runtime::PodcastRuntime>,
|
||||||
|
/// The podcast episode this device plays, for progress saving.
|
||||||
|
pub podcast_playback: Option<podcast_runtime::EpisodePlayback>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@@ -93,6 +103,17 @@ fn now_epoch_seconds() -> i64 {
|
|||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Joins two optional startup status messages with `; `, keeping both.
|
||||||
|
pub(crate) fn join_status_messages(
|
||||||
|
first: Option<String>,
|
||||||
|
second: Option<String>,
|
||||||
|
) -> Option<String> {
|
||||||
|
match (first, second) {
|
||||||
|
(Some(left), Some(right)) => Some(format!("{left}; {right}")),
|
||||||
|
(left, right) => left.or(right),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn err_string(err: anyhow::Error) -> String {
|
fn err_string(err: anyhow::Error) -> String {
|
||||||
format!("{err:#}")
|
format!("{err:#}")
|
||||||
}
|
}
|
||||||
@@ -375,7 +396,9 @@ fn spawn_content_id_backfill(runtime: &Runtime) {
|
|||||||
if let Err(err) = federation.sync_now().await {
|
if let Err(err) = federation.sync_now().await {
|
||||||
tracing::warn!("federation sync after content id backfill failed: {err:#}");
|
tracing::warn!("federation sync after content id backfill failed: {err:#}");
|
||||||
}
|
}
|
||||||
let _ = tx.send(AppEvent::FederationStatus(federation.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(
|
||||||
|
federation.status().await,
|
||||||
|
)));
|
||||||
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -394,11 +417,7 @@ pub async fn run(
|
|||||||
tracing::info!(path = %db_path.display(), "library opened");
|
tracing::info!(path = %db_path.display(), "library opened");
|
||||||
|
|
||||||
let (settings, settings_warning) = crate::config::settings::load();
|
let (settings, settings_warning) = crate::config::settings::load();
|
||||||
let status_message = match (startup_warning, settings_warning) {
|
let status_message = join_status_messages(startup_warning, settings_warning);
|
||||||
(Some(left), Some(right)) => Some(format!("{left}; {right}")),
|
|
||||||
(Some(message), None) | (None, Some(message)) => Some(message),
|
|
||||||
(None, None) => None,
|
|
||||||
};
|
|
||||||
let mut state = AppState {
|
let mut state = AppState {
|
||||||
status_message,
|
status_message,
|
||||||
..AppState::default()
|
..AppState::default()
|
||||||
@@ -475,8 +494,11 @@ pub async fn run(
|
|||||||
last_media_push: None,
|
last_media_push: None,
|
||||||
status_publisher: crate::status::Publisher::spawn(),
|
status_publisher: crate::status::Publisher::spawn(),
|
||||||
status_visualizer: crate::visualizer::status::Renderer::default(),
|
status_visualizer: crate::visualizer::status::Renderer::default(),
|
||||||
|
podcasts: None,
|
||||||
|
podcast_playback: None,
|
||||||
};
|
};
|
||||||
spawn_content_id_backfill(&runtime);
|
spawn_content_id_backfill(&runtime);
|
||||||
|
podcast_runtime::spawn_open(&runtime);
|
||||||
if state.similarity.settings.enabled {
|
if state.similarity.settings.enabled {
|
||||||
runtime.similarity.start();
|
runtime.similarity.start();
|
||||||
}
|
}
|
||||||
@@ -488,7 +510,7 @@ pub async fn run(
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
fed.start_if_enabled().await;
|
fed.start_if_enabled().await;
|
||||||
let status = fed.status().await;
|
let status = fed.status().await;
|
||||||
let _ = tx.send(AppEvent::FederationStatus(status));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(status)));
|
||||||
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -540,6 +562,7 @@ pub async fn run(
|
|||||||
state.advance_spinner();
|
state.advance_spinner();
|
||||||
expire_quit_confirmation(&mut state);
|
expire_quit_confirmation(&mut state);
|
||||||
sync_player_shared(&mut state, &runtime);
|
sync_player_shared(&mut state, &runtime);
|
||||||
|
podcast_runtime::tick(&state, &mut runtime);
|
||||||
runtime
|
runtime
|
||||||
.status_publisher
|
.status_publisher
|
||||||
.publish(crate::status::PlaybackStatus::from_player(&state.player));
|
.publish(crate::status::PlaybackStatus::from_player(&state.player));
|
||||||
@@ -578,6 +601,7 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
state.shutting_down = true;
|
state.shutting_down = true;
|
||||||
terminal.draw(|frame| ui::draw(frame, &state, &keymap))?;
|
terminal.draw(|frame| ui::draw(frame, &state, &keymap))?;
|
||||||
|
podcast_runtime::shutdown(&mut runtime).await;
|
||||||
runtime.federation.shutdown().await;
|
runtime.federation.shutdown().await;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -684,10 +708,12 @@ fn playback_state_from_ui(state: &AppState) -> crate::devices::PlaybackStateWire
|
|||||||
volume: state.player.volume,
|
volume: state.player.volume,
|
||||||
shuffle: state.player.shuffle,
|
shuffle: state.player.shuffle,
|
||||||
repeat: playback_repeat_to_wire(state.player.repeat),
|
repeat: playback_repeat_to_wire(state.player.repeat),
|
||||||
|
playback_rate: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn playback_track_to_ui(
|
/// A received music item, resolved to the library copy when there is one.
|
||||||
|
fn music_track_to_ui(
|
||||||
wire: &crate::devices::PlaybackTrack,
|
wire: &crate::devices::PlaybackTrack,
|
||||||
library: Option<&Library>,
|
library: Option<&Library>,
|
||||||
) -> crate::library::models::TrackItem {
|
) -> crate::library::models::TrackItem {
|
||||||
@@ -708,11 +734,11 @@ fn apply_playback_state_to_ui(
|
|||||||
wire: &crate::devices::PlaybackStateWire,
|
wire: &crate::devices::PlaybackStateWire,
|
||||||
library: Option<&Library>,
|
library: Option<&Library>,
|
||||||
) {
|
) {
|
||||||
let queue: Vec<_> = wire
|
// Podcast episodes keep their explicit source; Jam private placeholders
|
||||||
.queue
|
// stay placeholders (numbered, so each has its own queue key).
|
||||||
.iter()
|
let queue = crate::devices::track_items_from_wire(&wire.queue, |track| {
|
||||||
.map(|track| playback_track_to_ui(track, library))
|
music_track_to_ui(track, library)
|
||||||
.collect();
|
});
|
||||||
let queue_pos = queue
|
let queue_pos = queue
|
||||||
.iter()
|
.iter()
|
||||||
.take(wire.queue_pos)
|
.take(wire.queue_pos)
|
||||||
@@ -837,9 +863,10 @@ fn extrapolate_control_position(state: &mut AppState) {
|
|||||||
|
|
||||||
pub(crate) fn become_control_device(
|
pub(crate) fn become_control_device(
|
||||||
state: &mut AppState,
|
state: &mut AppState,
|
||||||
runtime: &Runtime,
|
runtime: &mut Runtime,
|
||||||
snapshot: crate::devices::PlaybackSnapshot,
|
snapshot: crate::devices::PlaybackSnapshot,
|
||||||
) {
|
) {
|
||||||
|
podcast_runtime::end_playback(runtime);
|
||||||
runtime.player.set_playback_allowed(false);
|
runtime.player.set_playback_allowed(false);
|
||||||
if state.device_playback.is_audio_owner() {
|
if state.device_playback.is_audio_owner() {
|
||||||
runtime.player.stop();
|
runtime.player.stop();
|
||||||
@@ -873,12 +900,7 @@ pub(crate) fn become_active_device(state: &mut AppState, runtime: &mut Runtime,
|
|||||||
state.device_playback.last_remote_snapshot = None;
|
state.device_playback.last_remote_snapshot = None;
|
||||||
state.device_playback.local_idle_since_ms = None;
|
state.device_playback.local_idle_since_ms = None;
|
||||||
if was_control && start_audio && state.player.playing {
|
if was_control && start_audio && state.player.playing {
|
||||||
start_current_audio(
|
start_current_audio_from(state, runtime, remote_start(state), state.player.paused);
|
||||||
state,
|
|
||||||
runtime,
|
|
||||||
state.player.position_secs,
|
|
||||||
state.player.paused,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
publish_playback_snapshot(state, runtime);
|
publish_playback_snapshot(state, runtime);
|
||||||
}
|
}
|
||||||
@@ -909,7 +931,7 @@ pub(crate) fn transfer_active_to_this_device(state: &mut AppState, runtime: &mut
|
|||||||
}
|
}
|
||||||
become_active_device(state, runtime, false);
|
become_active_device(state, runtime, false);
|
||||||
if should_start && state.player.current.is_some() {
|
if should_start && state.player.current.is_some() {
|
||||||
start_current_audio(state, runtime, state.player.position_secs, false);
|
start_current_audio_from(state, runtime, remote_start(state), false);
|
||||||
push_media_metadata(state, runtime);
|
push_media_metadata(state, runtime);
|
||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
}
|
}
|
||||||
@@ -930,6 +952,15 @@ pub(crate) fn transfer_active_to_remote_device(
|
|||||||
transfer_active_to_this_device(state, runtime);
|
transfer_active_to_this_device(state, runtime);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// A device below sync v4 could neither play nor keep podcast episodes:
|
||||||
|
// refuse before ownership changes (never strip them from the queue).
|
||||||
|
if let Err(error) = runtime
|
||||||
|
.devices
|
||||||
|
.ensure_playback_supported_by(&target_device_id, &playback_state_from_ui(state))
|
||||||
|
{
|
||||||
|
state.status_message = Some(format!("device handoff: {error}"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
if let Err(error) = runtime.devices.claim_playback(&target_device_id) {
|
if let Err(error) = runtime.devices.claim_playback(&target_device_id) {
|
||||||
state.status_message = Some(format!("device handoff: {error}"));
|
state.status_message = Some(format!("device handoff: {error}"));
|
||||||
return;
|
return;
|
||||||
@@ -1307,6 +1338,9 @@ fn maintenance(state: &mut AppState, runtime: &mut Runtime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(path) = podcast_runtime::wanted_cover(state) {
|
||||||
|
wanted.push((path, header.0, header.1));
|
||||||
|
}
|
||||||
for (path, width, height) in wanted {
|
for (path, width, height) in wanted {
|
||||||
let key = crate::art::cache_key(&path, width, height);
|
let key = crate::art::cache_key(&path, width, height);
|
||||||
if state.art.contains_key(&key) {
|
if state.art.contains_key(&key) {
|
||||||
@@ -1556,6 +1590,9 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
perform_control_playback_effect(state, runtime, effect);
|
perform_control_playback_effect(state, runtime, effect);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let Some(effect) = podcast_runtime::perform(state, runtime, effect) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
match effect {
|
match effect {
|
||||||
Effect::CheckUpdate => {
|
Effect::CheckUpdate => {
|
||||||
let tx = runtime.event_tx.clone();
|
let tx = runtime.event_tx.clone();
|
||||||
@@ -1587,6 +1624,11 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
push_media_metadata(state, runtime);
|
push_media_metadata(state, runtime);
|
||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
}
|
}
|
||||||
|
Effect::PlayCurrentFromStart => {
|
||||||
|
start_current_audio_from(state, runtime, StartAt::Restart, false);
|
||||||
|
push_media_metadata(state, runtime);
|
||||||
|
push_media_update(state, runtime, true);
|
||||||
|
}
|
||||||
Effect::TogglePause => {
|
Effect::TogglePause => {
|
||||||
if state.player.paused {
|
if state.player.paused {
|
||||||
runtime.player.pause();
|
runtime.player.pause();
|
||||||
@@ -1596,17 +1638,23 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
}
|
}
|
||||||
Effect::StopPlayback => {
|
Effect::StopPlayback => {
|
||||||
|
podcast_runtime::end_playback(runtime);
|
||||||
runtime.player_start_pending = false;
|
runtime.player_start_pending = false;
|
||||||
runtime.player.stop();
|
runtime.player.stop();
|
||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
}
|
}
|
||||||
Effect::SeekBy(delta) => {
|
Effect::SeekBy(delta) => match podcast_runtime::seek_refusal(state, runtime) {
|
||||||
|
Some(reason) => state.status_message = Some(reason),
|
||||||
|
None => {
|
||||||
let target = (state.player.position_secs + delta as f64).max(0.0);
|
let target = (state.player.position_secs + delta as f64).max(0.0);
|
||||||
state.player.position_secs = target;
|
state.player.position_secs = target;
|
||||||
|
// An episode's new position is saved once the audio thread
|
||||||
|
// reports that the seek worked (`PlayerEvent::Seeked`).
|
||||||
runtime
|
runtime
|
||||||
.player
|
.player
|
||||||
.seek(std::time::Duration::from_secs_f64(target));
|
.seek(std::time::Duration::from_secs_f64(target));
|
||||||
}
|
}
|
||||||
|
},
|
||||||
Effect::SetVolume(volume) => {
|
Effect::SetVolume(volume) => {
|
||||||
runtime.player.set_volume(player::amplitude(volume));
|
runtime.player.set_volume(player::amplitude(volume));
|
||||||
save_app_settings(state);
|
save_app_settings(state);
|
||||||
@@ -1679,6 +1727,19 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// Executed by podcast_runtime::perform above.
|
||||||
|
Effect::PodcastsReload
|
||||||
|
| Effect::PodcastLoadSeasons(_)
|
||||||
|
| Effect::PodcastLoadEpisodes { .. }
|
||||||
|
| Effect::PodcastSubscribe(_)
|
||||||
|
| Effect::PodcastRefresh { .. }
|
||||||
|
| Effect::PodcastUnsubscribe { .. }
|
||||||
|
| Effect::PodcastSetPlayed { .. }
|
||||||
|
| Effect::PodcastPrepareShare { .. }
|
||||||
|
| Effect::PodcastSetShared { .. }
|
||||||
|
| Effect::PodcastPublicationSync
|
||||||
|
| Effect::PodcastSubscribeFromCard(_)
|
||||||
|
| Effect::PodcastPreview { .. } => {}
|
||||||
Effect::LoadListenHistory => {
|
Effect::LoadListenHistory => {
|
||||||
let library = Arc::clone(&runtime.library);
|
let library = Arc::clone(&runtime.library);
|
||||||
let devices = Arc::clone(&runtime.devices);
|
let devices = Arc::clone(&runtime.devices);
|
||||||
@@ -1812,7 +1873,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
Ok(()) => "federation: library published".to_string(),
|
Ok(()) => "federation: library published".to_string(),
|
||||||
Err(err) => format!("federation sync failed: {err:#}"),
|
Err(err) => format!("federation sync failed: {err:#}"),
|
||||||
};
|
};
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
let _ = tx.send(AppEvent::FedSyncFinished(message));
|
let _ = tx.send(AppEvent::FedSyncFinished(message));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1872,7 +1933,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let result = fed.device_invite().await.map_err(|err| format!("{err:#}"));
|
let result = fed.device_invite().await.map_err(|err| format!("{err:#}"));
|
||||||
let _ = tx.send(AppEvent::DeviceInvite(result));
|
let _ = tx.send(AppEvent::DeviceInvite(result));
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1995,6 +2056,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
let result = federation
|
let result = federation
|
||||||
.track_info(preview)
|
.track_info(preview)
|
||||||
.await
|
.await
|
||||||
|
.map(Box::new)
|
||||||
.map_err(|err| format!("{err:#}"));
|
.map_err(|err| format!("{err:#}"));
|
||||||
let _ = tx.send(AppEvent::FedTrackInfoLoaded {
|
let _ = tx.send(AppEvent::FedTrackInfoLoaded {
|
||||||
placeholder_id,
|
placeholder_id,
|
||||||
@@ -2035,7 +2097,7 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
|||||||
runtime.player.stop();
|
runtime.player.stop();
|
||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
} else if let Some(paused) = restart_paused {
|
} else if let Some(paused) = restart_paused {
|
||||||
start_current_audio(state, runtime, 0.0, paused);
|
start_current_audio_from(state, runtime, StartAt::Resume, paused);
|
||||||
push_media_metadata(state, runtime);
|
push_media_metadata(state, runtime);
|
||||||
push_media_update(state, runtime, true);
|
push_media_update(state, runtime, true);
|
||||||
} else {
|
} else {
|
||||||
@@ -2049,6 +2111,7 @@ fn is_controlled_playback_effect(effect: &Effect) -> bool {
|
|||||||
matches!(
|
matches!(
|
||||||
effect,
|
effect,
|
||||||
Effect::PlayCurrent
|
Effect::PlayCurrent
|
||||||
|
| Effect::PlayCurrentFromStart
|
||||||
| Effect::TogglePause
|
| Effect::TogglePause
|
||||||
| Effect::StopPlayback
|
| Effect::StopPlayback
|
||||||
| Effect::SeekBy(_)
|
| Effect::SeekBy(_)
|
||||||
@@ -2065,7 +2128,7 @@ fn perform_control_playback_effect(state: &mut AppState, runtime: &mut Runtime,
|
|||||||
let local_only_volume = state.device_playback.role == state::DevicePlaybackRole::Jam
|
let local_only_volume = state.device_playback.role == state::DevicePlaybackRole::Jam
|
||||||
&& matches!(effect, Effect::SetVolume(_));
|
&& matches!(effect, Effect::SetVolume(_));
|
||||||
match effect {
|
match effect {
|
||||||
Effect::PlayCurrent => {
|
Effect::PlayCurrent | Effect::PlayCurrentFromStart => {
|
||||||
state.player.current = state.player.queue.get(state.player.queue_pos).cloned();
|
state.player.current = state.player.queue.get(state.player.queue_pos).cloned();
|
||||||
state.player.playing = state.player.current.is_some();
|
state.player.playing = state.player.current.is_some();
|
||||||
state.player.paused = false;
|
state.player.paused = false;
|
||||||
@@ -2153,21 +2216,62 @@ fn shell_quote(path: &Path) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Start playing `queue[queue_pos]`: open the local file in a background
|
/// Start playing `queue[queue_pos]`: open the local file in a background
|
||||||
/// task and hand the reader to the audio thread.
|
/// task and hand the reader to the audio thread. A podcast episode resumes
|
||||||
|
/// from its saved position.
|
||||||
fn play_current(state: &mut AppState, runtime: &mut Runtime) {
|
fn play_current(state: &mut AppState, runtime: &mut Runtime) {
|
||||||
start_current_audio(state, runtime, 0.0, false);
|
start_current_audio_from(state, runtime, StartAt::Resume, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start `queue[queue_pos]` at an exact position (handoff, restarting the
|
||||||
|
/// same item after a queue change).
|
||||||
fn start_current_audio(
|
fn start_current_audio(
|
||||||
state: &mut AppState,
|
state: &mut AppState,
|
||||||
runtime: &mut Runtime,
|
runtime: &mut Runtime,
|
||||||
position_secs: f64,
|
position_secs: f64,
|
||||||
paused: bool,
|
paused: bool,
|
||||||
) {
|
) {
|
||||||
|
let ms = (position_secs.max(0.0) * 1_000.0).round() as i64;
|
||||||
|
start_current_audio_from(state, runtime, StartAt::Position { ms }, paused);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the current item starts when another device decided it (a
|
||||||
|
/// handoff, a command of a controlling device, taking playback over here).
|
||||||
|
///
|
||||||
|
/// A podcast episode at position 0 resumes from its saved position: a
|
||||||
|
/// controlling device sends 0 for an ordinary Play, and the saved position
|
||||||
|
/// is at least as fresh. Any other position is the live position of the
|
||||||
|
/// handoff or seek.
|
||||||
|
fn remote_start(state: &AppState) -> StartAt {
|
||||||
|
let ms = (state.player.position_secs.max(0.0) * 1_000.0).round() as i64;
|
||||||
|
let episode = state
|
||||||
|
.player
|
||||||
|
.queue
|
||||||
|
.get(state.player.queue_pos)
|
||||||
|
.is_some_and(crate::library::models::TrackItem::is_podcast);
|
||||||
|
if episode && ms == 0 {
|
||||||
|
StartAt::Resume
|
||||||
|
} else {
|
||||||
|
StartAt::Position { ms }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_current_audio_from(
|
||||||
|
state: &mut AppState,
|
||||||
|
runtime: &mut Runtime,
|
||||||
|
start: StartAt,
|
||||||
|
paused: bool,
|
||||||
|
) {
|
||||||
|
let position_secs = match start {
|
||||||
|
StartAt::Position { ms } => ms as f64 / 1_000.0,
|
||||||
|
StartAt::Resume | StartAt::Restart => 0.0,
|
||||||
|
};
|
||||||
let Some(mut track) = state.player.queue.get(state.player.queue_pos).cloned() else {
|
let Some(mut track) = state.player.queue.get(state.player.queue_pos).cloned() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if track_file_missing(&track) {
|
// The episode that was playing ends here (its last seen position is
|
||||||
|
// saved), whatever plays next.
|
||||||
|
podcast_runtime::end_playback(runtime);
|
||||||
|
if !track.is_podcast() && track_file_missing(&track) {
|
||||||
if let Some(local) = local_track_for_playback(runtime, &track) {
|
if let Some(local) = local_track_for_playback(runtime, &track) {
|
||||||
state.player.queue[state.player.queue_pos] = local.clone();
|
state.player.queue[state.player.queue_pos] = local.clone();
|
||||||
track = local;
|
track = local;
|
||||||
@@ -2246,6 +2350,12 @@ fn start_current_audio(
|
|||||||
state.status_message = Some(format!("▶ {} — {}", track.title, track.artist_line()));
|
state.status_message = Some(format!("▶ {} — {}", track.title, track.artist_line()));
|
||||||
|
|
||||||
runtime.player_start_pending = true;
|
runtime.player_start_pending = true;
|
||||||
|
if track.is_podcast() {
|
||||||
|
// Streamed from the publisher; the start position (resume, exact or
|
||||||
|
// start over) is resolved with the store in the background.
|
||||||
|
podcast_runtime::start_episode(state, runtime, &track, start, paused);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if track.is_fed_pending() {
|
if track.is_fed_pending() {
|
||||||
// A federated track that is not on disk yet: silence the previous
|
// A federated track that is not on disk yet: silence the previous
|
||||||
// audio, download it and resume through FedTrackResolved.
|
// audio, download it and resume through FedTrackResolved.
|
||||||
@@ -2348,6 +2458,11 @@ fn maybe_prefetch_next(state: &mut AppState, runtime: &Runtime) {
|
|||||||
let Some(track) = &player.current else {
|
let Some(track) = &player.current else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
// An episode ends through its own completion path, and the next item
|
||||||
|
// must not start behind a stream; episodes are never prefetched either.
|
||||||
|
if track.is_podcast() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if track.duration_seconds <= 0.0
|
if track.duration_seconds <= 0.0
|
||||||
|| track.duration_seconds - player.position_secs > PREFETCH_MARGIN_SECS
|
|| track.duration_seconds - player.position_secs > PREFETCH_MARGIN_SECS
|
||||||
{
|
{
|
||||||
@@ -2359,6 +2474,9 @@ fn maybe_prefetch_next(state: &mut AppState, runtime: &Runtime) {
|
|||||||
let Some(mut next) = player.queue.get(next_pos).cloned() else {
|
let Some(mut next) = player.queue.get(next_pos).cloned() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
if next.is_podcast() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if track_file_missing(&next) {
|
if track_file_missing(&next) {
|
||||||
if let Some(local) = local_track_for_playback(runtime, &next) {
|
if let Some(local) = local_track_for_playback(runtime, &next) {
|
||||||
state.player.queue[next_pos] = local.clone();
|
state.player.queue[next_pos] = local.clone();
|
||||||
@@ -2402,6 +2520,11 @@ fn report_history(
|
|||||||
listened_ms: i64,
|
listened_ms: i64,
|
||||||
ended_reason: music_dht::device_sync::ListenEndReason,
|
ended_reason: music_dht::device_sync::ListenEndReason,
|
||||||
) {
|
) {
|
||||||
|
// Podcast episodes are not music history (no content id); their
|
||||||
|
// listening sessions go to the podcast store (`podcast_runtime`).
|
||||||
|
if track.is_podcast() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let Some(listen_id) = listen_id else {
|
let Some(listen_id) = listen_id else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -2432,7 +2555,7 @@ pub(crate) fn fed_apply_settings(state: &mut AppState, runtime: &Runtime) {
|
|||||||
if let Err(err) = fed.apply_settings(settings).await {
|
if let Err(err) = fed.apply_settings(settings).await {
|
||||||
let _ = tx.send(AppEvent::StatusMessage(format!("federation: {err:#}")));
|
let _ = tx.send(AppEvent::StatusMessage(format!("federation: {err:#}")));
|
||||||
}
|
}
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2445,7 +2568,7 @@ pub(crate) fn fed_connect(runtime: &Runtime, ticket: String) {
|
|||||||
Ok(peer) => format!("federation: connected to {}…", &peer[..peer.len().min(10)]),
|
Ok(peer) => format!("federation: connected to {}…", &peer[..peer.len().min(10)]),
|
||||||
Err(err) => format!("federation: {err:#}"),
|
Err(err) => format!("federation: {err:#}"),
|
||||||
};
|
};
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
let _ = tx.send(AppEvent::StatusMessage(message));
|
let _ = tx.send(AppEvent::StatusMessage(message));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -2463,7 +2586,7 @@ pub(crate) fn device_connect(runtime: &Runtime, invite: String) {
|
|||||||
.device_connect(&invite)
|
.device_connect(&invite)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| format!("{err:#}"));
|
.map_err(|err| format!("{err:#}"));
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
||||||
let _ = tx.send(AppEvent::DeviceConnectResult(result));
|
let _ = tx.send(AppEvent::DeviceConnectResult(result));
|
||||||
});
|
});
|
||||||
@@ -2784,7 +2907,7 @@ fn fed_spawn_status(runtime: &Runtime) {
|
|||||||
let devices = Arc::clone(&runtime.devices);
|
let devices = Arc::clone(&runtime.devices);
|
||||||
let tx = runtime.event_tx.clone();
|
let tx = runtime.event_tx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let _ = tx.send(AppEvent::FederationStatus(fed.status().await));
|
let _ = tx.send(AppEvent::FederationStatus(Box::new(fed.status().await)));
|
||||||
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
let _ = tx.send(AppEvent::DeviceSyncStatus(devices.status()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -2825,7 +2948,7 @@ fn handle_terminal_event(
|
|||||||
}
|
}
|
||||||
TermEvent::Paste(pasted) => {
|
TermEvent::Paste(pasted) => {
|
||||||
if state.popup.is_some() {
|
if state.popup.is_some() {
|
||||||
popup::handle_paste(state, &pasted);
|
popup::handle_paste(state, runtime, &pasted);
|
||||||
} else if state.cmdline.active {
|
} else if state.cmdline.active {
|
||||||
cmdline::handle_paste(state, runtime, &pasted);
|
cmdline::handle_paste(state, runtime, &pasted);
|
||||||
}
|
}
|
||||||
@@ -3036,6 +3159,12 @@ fn reset_artist_pagination(state: &mut AppState) {
|
|||||||
global.reloading = false;
|
global.reloading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A row of the local music library (not a podcast episode, not a pending
|
||||||
|
/// federated placeholder).
|
||||||
|
fn is_library_row(track: &crate::library::models::TrackItem) -> bool {
|
||||||
|
!track.is_podcast() && track.id >= 0
|
||||||
|
}
|
||||||
|
|
||||||
/// Preserve the queue position while refreshing library-backed entries.
|
/// Preserve the queue position while refreshing library-backed entries.
|
||||||
///
|
///
|
||||||
/// A playback key identifies a track, not a queue occurrence: the same track
|
/// A playback key identifies a track, not a queue occurrence: the same track
|
||||||
@@ -3049,7 +3178,7 @@ fn queue_position_after_refresh(
|
|||||||
let removed_before_current = queue
|
let removed_before_current = queue
|
||||||
.iter()
|
.iter()
|
||||||
.take(queue_pos)
|
.take(queue_pos)
|
||||||
.filter(|track| track.id >= 0 && !refreshed_ids.contains(&track.id))
|
.filter(|track| is_library_row(track) && !refreshed_ids.contains(&track.id))
|
||||||
.count();
|
.count();
|
||||||
queue_pos.saturating_sub(removed_before_current)
|
queue_pos.saturating_sub(removed_before_current)
|
||||||
}
|
}
|
||||||
@@ -3071,9 +3200,22 @@ fn apply_queue_refresh(
|
|||||||
.cloned()
|
.cloned()
|
||||||
.map(|track| (track_playback_key(&track), track))
|
.map(|track| (track_playback_key(&track), track))
|
||||||
.collect();
|
.collect();
|
||||||
let current_id = state.player.current.as_ref().map(|track| track.id);
|
// A current podcast episode (sentinel id) is never a deleted library
|
||||||
|
// row; it takes the "not a library row" branch below.
|
||||||
|
let current_id = state.player.current.as_ref().map(|track| {
|
||||||
|
if track.is_podcast() {
|
||||||
|
crate::library::models::TrackItem::NOT_A_LIBRARY_ROW
|
||||||
|
} else {
|
||||||
|
track.id
|
||||||
|
}
|
||||||
|
});
|
||||||
let current_key = state.player.current.as_ref().map(track_playback_key);
|
let current_key = state.player.current.as_ref().map(track_playback_key);
|
||||||
for track in &mut state.player.queue {
|
for track in &mut state.player.queue {
|
||||||
|
// Podcast episodes are not library rows: their sentinel id matches
|
||||||
|
// nothing and they are never refreshed from the library.
|
||||||
|
if track.is_podcast() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if let Some(fresh) = by_id.get(&track.id) {
|
if let Some(fresh) = by_id.get(&track.id) {
|
||||||
*track = fresh.clone();
|
*track = fresh.clone();
|
||||||
} else if let Some(fresh) = by_key.get(&track_playback_key(track)) {
|
} else if let Some(fresh) = by_key.get(&track_playback_key(track)) {
|
||||||
@@ -3084,12 +3226,12 @@ fn apply_queue_refresh(
|
|||||||
.player
|
.player
|
||||||
.queue
|
.queue
|
||||||
.iter()
|
.iter()
|
||||||
.any(|track| track.id >= 0 && !by_id.contains_key(&track.id));
|
.any(|track| is_library_row(track) && !by_id.contains_key(&track.id));
|
||||||
if had_missing {
|
if had_missing {
|
||||||
state
|
state
|
||||||
.player
|
.player
|
||||||
.queue
|
.queue
|
||||||
.retain(|track| track.id < 0 || by_id.contains_key(&track.id));
|
.retain(|track| !is_library_row(track) || by_id.contains_key(&track.id));
|
||||||
state.player.prefetched_pos = None;
|
state.player.prefetched_pos = None;
|
||||||
}
|
}
|
||||||
if state.player.queue.is_empty() {
|
if state.player.queue.is_empty() {
|
||||||
@@ -3222,6 +3364,8 @@ fn handle_playback_command(
|
|||||||
) {
|
) {
|
||||||
match command {
|
match command {
|
||||||
crate::devices::PlaybackCommand::SetState { state: wire, seek } => {
|
crate::devices::PlaybackCommand::SetState { state: wire, seek } => {
|
||||||
|
// Commands from devices below sync v4 never reach this point
|
||||||
|
// while the queue has podcast items (`DeviceSync`).
|
||||||
let old_current_key = state.player.current.as_ref().map(track_playback_key);
|
let old_current_key = state.player.current.as_ref().map(track_playback_key);
|
||||||
let old_playing = state.player.playing;
|
let old_playing = state.player.playing;
|
||||||
let old_paused = state.player.paused;
|
let old_paused = state.player.paused;
|
||||||
@@ -3239,18 +3383,19 @@ fn handle_playback_command(
|
|||||||
}
|
}
|
||||||
let current_key = state.player.current.as_ref().map(track_playback_key);
|
let current_key = state.player.current.as_ref().map(track_playback_key);
|
||||||
if !old_playing || old_current_key != current_key {
|
if !old_playing || old_current_key != current_key {
|
||||||
start_current_audio(
|
start_current_audio_from(state, runtime, remote_start(state), state.player.paused);
|
||||||
state,
|
|
||||||
runtime,
|
|
||||||
state.player.position_secs,
|
|
||||||
state.player.paused,
|
|
||||||
);
|
|
||||||
push_media_metadata(state, runtime);
|
push_media_metadata(state, runtime);
|
||||||
} else {
|
} else {
|
||||||
if seek {
|
if seek {
|
||||||
runtime.player.seek(std::time::Duration::from_secs_f64(
|
match podcast_runtime::seek_refusal(state, runtime) {
|
||||||
|
Some(reason) => state.status_message = Some(reason),
|
||||||
|
// A seek from a controlling device is the user's
|
||||||
|
// explicit action too (podcast progress
|
||||||
|
// generation), saved once the seek worked.
|
||||||
|
None => runtime.player.seek(std::time::Duration::from_secs_f64(
|
||||||
state.player.position_secs,
|
state.player.position_secs,
|
||||||
));
|
)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if state.player.paused && !old_paused {
|
if state.player.paused && !old_paused {
|
||||||
runtime.player.pause();
|
runtime.player.pause();
|
||||||
@@ -3297,8 +3442,105 @@ fn handle_playback_command(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shown when a Jam guest's change would drop, add or reveal the host's
|
||||||
|
/// private items.
|
||||||
|
const JAM_PRIVATE_ITEMS_KEPT: &str =
|
||||||
|
"Jam: ignored a guest's change that would remove or add podcast episodes";
|
||||||
|
|
||||||
|
/// A Jam guest's queue state mapped back onto this host's queue.
|
||||||
|
///
|
||||||
|
/// Guests see the host's podcast episodes as private placeholders
|
||||||
|
/// (`redact_for_jam`). Their placeholders become the host's episodes again
|
||||||
|
/// (`restore_jam_private_items`); a state that drops, duplicates or adds
|
||||||
|
/// private items is refused (`None`). Guests cannot seek in a private item:
|
||||||
|
/// while it stays current, the host's position is kept. The host keeps its
|
||||||
|
/// own volume.
|
||||||
|
fn jam_participant_state(
|
||||||
|
state: &AppState,
|
||||||
|
wire: crate::devices::PlaybackStateWire,
|
||||||
|
seek: bool,
|
||||||
|
) -> Option<(crate::devices::PlaybackStateWire, bool)> {
|
||||||
|
let host = playback_state_from_ui(state);
|
||||||
|
if !music_dht::device_sync::jam_command_allowed(&crate::devices::PlaybackCommand::SetState {
|
||||||
|
state: wire.clone(),
|
||||||
|
seek,
|
||||||
|
}) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut restored = music_dht::device_sync::restore_jam_private_items(&host, wire)?;
|
||||||
|
restored.volume = state.player.volume;
|
||||||
|
let private_current = restored
|
||||||
|
.queue
|
||||||
|
.get(restored.queue_pos)
|
||||||
|
.filter(|track| track.requires_podcast_support());
|
||||||
|
let same_current = private_current.is_some()
|
||||||
|
&& restored.queue.get(restored.queue_pos) == host.queue.get(host.queue_pos);
|
||||||
|
if same_current {
|
||||||
|
restored.position_secs = host.position_secs;
|
||||||
|
return Some((restored, false));
|
||||||
|
}
|
||||||
|
Some((restored, seek))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes Jam private placeholders from the queue (a guest leaving).
|
||||||
|
fn drop_private_items(state: &mut AppState) {
|
||||||
|
let player = &mut state.player;
|
||||||
|
let before = player
|
||||||
|
.queue
|
||||||
|
.iter()
|
||||||
|
.take(player.queue_pos)
|
||||||
|
.filter(|track| crate::devices::is_private_track_item(track))
|
||||||
|
.count();
|
||||||
|
let current_private = player
|
||||||
|
.queue
|
||||||
|
.get(player.queue_pos)
|
||||||
|
.is_some_and(crate::devices::is_private_track_item);
|
||||||
|
player
|
||||||
|
.queue
|
||||||
|
.retain(|track| !crate::devices::is_private_track_item(track));
|
||||||
|
player.queue_pos = player
|
||||||
|
.queue_pos
|
||||||
|
.saturating_sub(before)
|
||||||
|
.min(player.queue.len().saturating_sub(1));
|
||||||
|
player.play_next_end = None;
|
||||||
|
player.original_order = None;
|
||||||
|
player.prefetched_pos = None;
|
||||||
|
if current_private
|
||||||
|
|| player
|
||||||
|
.current
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(crate::devices::is_private_track_item)
|
||||||
|
{
|
||||||
|
player.current = None;
|
||||||
|
player.playing = false;
|
||||||
|
player.paused = false;
|
||||||
|
player.position_secs = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent) {
|
fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent) {
|
||||||
|
let Some(event) = podcast_runtime::handle_event(state, runtime, event) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
match event {
|
match event {
|
||||||
|
// Folded in by podcast_runtime::handle_event above.
|
||||||
|
AppEvent::PodcastStoreOpened(_)
|
||||||
|
| AppEvent::PodcastsSynced { .. }
|
||||||
|
| AppEvent::PodcastsLoaded(_)
|
||||||
|
| AppEvent::PodcastSeasonsLoaded { .. }
|
||||||
|
| AppEvent::PodcastEpisodesLoaded { .. }
|
||||||
|
| AppEvent::PodcastSubscribed(_)
|
||||||
|
| AppEvent::PodcastRefreshed { .. }
|
||||||
|
| AppEvent::PodcastUnsubscribed { .. }
|
||||||
|
| AppEvent::PodcastProgress(_)
|
||||||
|
| AppEvent::PodcastEpisodeStarted { .. }
|
||||||
|
| AppEvent::PodcastEpisodeFailed { .. }
|
||||||
|
| AppEvent::PodcastRefreshTick
|
||||||
|
| AppEvent::PodcastsDue(_)
|
||||||
|
| AppEvent::PodcastSharePrepared { .. }
|
||||||
|
| AppEvent::PodcastShareSet { .. }
|
||||||
|
| AppEvent::PodcastPublication(_)
|
||||||
|
| AppEvent::PodcastPreviewLoaded { .. } => {}
|
||||||
AppEvent::StatusVisualizationReady(ready) => {
|
AppEvent::StatusVisualizationReady(ready) => {
|
||||||
runtime.status_visualizer.busy = false;
|
runtime.status_visualizer.busy = false;
|
||||||
state.status_visualizer.apply(ready);
|
state.status_visualizer.apply(ready);
|
||||||
@@ -3384,7 +3626,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
AppEvent::FederationStatus(status) => {
|
AppEvent::FederationStatus(status) => {
|
||||||
state.federation.status = Some(status);
|
state.federation.status = Some(*status);
|
||||||
}
|
}
|
||||||
AppEvent::DeviceSyncStatus(status) => {
|
AppEvent::DeviceSyncStatus(status) => {
|
||||||
state.federation.devices = Some(status);
|
state.federation.devices = Some(status);
|
||||||
@@ -3481,6 +3723,11 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
}
|
}
|
||||||
crate::jam::JamRole::None => {
|
crate::jam::JamRole::None => {
|
||||||
if state.device_playback.role == state::DevicePlaybackRole::Jam {
|
if state.device_playback.role == state::DevicePlaybackRole::Jam {
|
||||||
|
if !state.device_playback.jam_host {
|
||||||
|
// The host's private placeholders are not this
|
||||||
|
// device's queue items.
|
||||||
|
drop_private_items(state);
|
||||||
|
}
|
||||||
state.device_playback.role = state::DevicePlaybackRole::Active;
|
state.device_playback.role = state::DevicePlaybackRole::Active;
|
||||||
state.device_playback.jam_host = false;
|
state.device_playback.jam_host = false;
|
||||||
}
|
}
|
||||||
@@ -3500,13 +3747,17 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
}
|
}
|
||||||
AppEvent::JamCommand(command) => {
|
AppEvent::JamCommand(command) => {
|
||||||
let command = match command {
|
let command = match command {
|
||||||
crate::devices::PlaybackCommand::SetState {
|
crate::devices::PlaybackCommand::SetState { state: wire, seek } => {
|
||||||
state: mut wire,
|
match jam_participant_state(state, wire, seek) {
|
||||||
seek,
|
Some((wire, seek)) => {
|
||||||
} => {
|
|
||||||
wire.volume = state.player.volume;
|
|
||||||
crate::devices::PlaybackCommand::SetState { state: wire, seek }
|
crate::devices::PlaybackCommand::SetState { state: wire, seek }
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
state.status_message = Some(JAM_PRIVATE_ITEMS_KEPT.into());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
crate::devices::PlaybackCommand::ActiveChanged { .. } => {
|
crate::devices::PlaybackCommand::ActiveChanged { .. } => {
|
||||||
state.status_message =
|
state.status_message =
|
||||||
Some("Jam cannot transfer audio away from its host".into());
|
Some("Jam cannot transfer audio away from its host".into());
|
||||||
@@ -3554,6 +3805,13 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
Err(message) => tracing::warn!(%message, "federated search failed"),
|
Err(message) => tracing::warn!(%message, "federated search failed"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AppEvent::FedPodcastsLoaded { seq, hits } => {
|
||||||
|
if runtime.search_seq.load(std::sync::atomic::Ordering::SeqCst) != seq {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.search.fed_podcasts_loading = false;
|
||||||
|
state.search.fed_podcasts = hits;
|
||||||
|
}
|
||||||
AppEvent::FedSimilaritySearchLoaded { seq, result } => {
|
AppEvent::FedSimilaritySearchLoaded { seq, result } => {
|
||||||
if runtime.search_seq.load(std::sync::atomic::Ordering::SeqCst) != seq {
|
if runtime.search_seq.load(std::sync::atomic::Ordering::SeqCst) != seq {
|
||||||
return;
|
return;
|
||||||
@@ -3747,7 +4005,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
&& track.fed.as_ref().is_some_and(|fed| fed.item_id == item_id)
|
&& track.fed.as_ref().is_some_and(|fed| fed.item_id == item_id)
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
*slot = enriched;
|
*slot = *enriched;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(message) => {
|
Err(message) => {
|
||||||
@@ -3996,6 +4254,15 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
};
|
};
|
||||||
state.art.insert(key, entry);
|
state.art.insert(key, entry);
|
||||||
}
|
}
|
||||||
|
AppEvent::Player(player::PlayerEvent::Seeked {
|
||||||
|
generation,
|
||||||
|
position,
|
||||||
|
result,
|
||||||
|
}) => {
|
||||||
|
if !state.device_playback.is_control() {
|
||||||
|
podcast_runtime::seek_done(state, runtime, generation, position, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
AppEvent::Player(event) if state.device_playback.is_control() => {
|
AppEvent::Player(event) if state.device_playback.is_control() => {
|
||||||
runtime.player_start_pending = false;
|
runtime.player_start_pending = false;
|
||||||
// A background decoder may finish after ownership was handed off.
|
// A background decoder may finish after ownership was handed off.
|
||||||
@@ -4010,6 +4277,15 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
runtime.player_start_pending = false;
|
runtime.player_start_pending = false;
|
||||||
}
|
}
|
||||||
AppEvent::Player(player::PlayerEvent::TrackFinished { has_next }) => {
|
AppEvent::Player(player::PlayerEvent::TrackFinished { has_next }) => {
|
||||||
|
// An episode that played to the end is complete; one whose
|
||||||
|
// stream broke off stops where it was, without moving on.
|
||||||
|
if let podcast_runtime::Finished::Interrupted(message) =
|
||||||
|
podcast_runtime::finished(state, runtime)
|
||||||
|
{
|
||||||
|
state.status_message = Some(message);
|
||||||
|
push_media_update(state, runtime, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// The finished track gets a full-duration, completed entry.
|
// The finished track gets a full-duration, completed entry.
|
||||||
if let Some(finished) = state.player.current.clone() {
|
if let Some(finished) = state.player.current.clone() {
|
||||||
report_history(
|
report_history(
|
||||||
@@ -4046,6 +4322,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
AppEvent::Player(player::PlayerEvent::Failed(message)) => {
|
AppEvent::Player(player::PlayerEvent::Failed(message)) => {
|
||||||
|
podcast_runtime::player_failed(runtime);
|
||||||
runtime.player_start_pending = false;
|
runtime.player_start_pending = false;
|
||||||
tracing::error!(%message, "playback failed");
|
tracing::error!(%message, "playback failed");
|
||||||
state.player.playing = false;
|
state.player.playing = false;
|
||||||
@@ -4354,6 +4631,7 @@ mod similarity_search_tests {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
},
|
},
|
||||||
score,
|
score,
|
||||||
embedding_signature: [signature; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES],
|
embedding_signature: [signature; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES],
|
||||||
@@ -4431,6 +4709,7 @@ mod queue_refresh_tests {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4491,3 +4770,219 @@ fn push_media_metadata(state: &AppState, runtime: &Runtime) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod status_message_tests {
|
||||||
|
use super::join_status_messages;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn startup_messages_are_joined_not_dropped() {
|
||||||
|
assert_eq!(
|
||||||
|
join_status_messages(
|
||||||
|
Some("keymap notice".into()),
|
||||||
|
Some("logging disabled: x".into())
|
||||||
|
)
|
||||||
|
.as_deref(),
|
||||||
|
Some("keymap notice; logging disabled: x")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
join_status_messages(None, Some("only".into())).as_deref(),
|
||||||
|
Some("only")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
join_status_messages(Some("only".into()), None).as_deref(),
|
||||||
|
Some("only")
|
||||||
|
);
|
||||||
|
assert_eq!(join_status_messages(None, None), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod playback_bridge_tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::app::podcast_fixtures::episode_track;
|
||||||
|
use crate::devices::{PlaybackStateWire, PlaybackTrack, PlaybackTrackExt};
|
||||||
|
use crate::library::models::TrackItem;
|
||||||
|
use music_dht::device_sync::PlaybackSource;
|
||||||
|
|
||||||
|
fn music(id: i64) -> TrackItem {
|
||||||
|
TrackItem {
|
||||||
|
id,
|
||||||
|
title: format!("song {id}"),
|
||||||
|
track_number: None,
|
||||||
|
disc_number: None,
|
||||||
|
duration_seconds: 200.0,
|
||||||
|
artists: vec![],
|
||||||
|
featured_artists: vec![],
|
||||||
|
release_id: 1,
|
||||||
|
release_title: "release".into(),
|
||||||
|
release_year: None,
|
||||||
|
cover_path: None,
|
||||||
|
file_path: format!("/music/{id}"),
|
||||||
|
content_id: Some(format!("b3:{id:064x}")),
|
||||||
|
audio_format: None,
|
||||||
|
audio_bitrate: None,
|
||||||
|
audio_sample_rate: None,
|
||||||
|
audio_bit_depth: None,
|
||||||
|
file_size_bytes: None,
|
||||||
|
play_count: 0,
|
||||||
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mixed_state() -> AppState {
|
||||||
|
let mut state = AppState::default();
|
||||||
|
state.player.queue = vec![music(1), episode_track(1), music(2), episode_track(2)];
|
||||||
|
state.player.queue_pos = 1;
|
||||||
|
state.player.current = Some(episode_track(1));
|
||||||
|
state.player.playing = true;
|
||||||
|
state.player.paused = true;
|
||||||
|
state.player.position_secs = 754.5;
|
||||||
|
state
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keys(queue: &[TrackItem]) -> Vec<String> {
|
||||||
|
queue.iter().map(state::track_key).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_mixed_queue_crosses_the_wire_with_its_episodes_position_and_pause() {
|
||||||
|
let sender = mixed_state();
|
||||||
|
let wire = playback_state_from_ui(&sender);
|
||||||
|
assert_eq!(wire.playback_rate, None);
|
||||||
|
assert!(matches!(
|
||||||
|
wire.queue[1].source,
|
||||||
|
Some(PlaybackSource::PodcastEpisode(_))
|
||||||
|
));
|
||||||
|
assert_eq!(wire.queue[0].source, None, "music stays untyped (v3 shape)");
|
||||||
|
let json = serde_json::to_string(&wire).unwrap();
|
||||||
|
let received: PlaybackStateWire = serde_json::from_str(&json).unwrap();
|
||||||
|
|
||||||
|
let mut receiver = AppState::default();
|
||||||
|
// Even a local-only source mode never drops episodes.
|
||||||
|
receiver.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Local;
|
||||||
|
apply_playback_state_to_ui(&mut receiver, &received, None);
|
||||||
|
assert_eq!(keys(&receiver.player.queue), keys(&sender.player.queue));
|
||||||
|
assert_eq!(receiver.player.queue_pos, 1);
|
||||||
|
assert_eq!(
|
||||||
|
receiver.player.current.as_ref().map(state::track_key),
|
||||||
|
Some(state::track_key(&episode_track(1)))
|
||||||
|
);
|
||||||
|
assert!(receiver.player.paused);
|
||||||
|
assert_eq!(receiver.player.position_secs, 754.5);
|
||||||
|
assert_eq!(
|
||||||
|
receiver.player.queue[1].podcast_episode_ref(),
|
||||||
|
episode_track(1).podcast_episode_ref()
|
||||||
|
);
|
||||||
|
// Taken over there, the episode continues at the handed-off
|
||||||
|
// position; an episode handed over at 0 resumes its saved one.
|
||||||
|
assert_eq!(remote_start(&receiver), StartAt::Position { ms: 754_500 });
|
||||||
|
receiver.player.position_secs = 0.0;
|
||||||
|
assert_eq!(remote_start(&receiver), StartAt::Resume);
|
||||||
|
receiver.player.queue_pos = 0;
|
||||||
|
assert_eq!(remote_start(&receiver), StartAt::Position { ms: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn jam_guests_see_placeholders_and_their_commands_keep_the_hosts_episodes() {
|
||||||
|
let host = mixed_state();
|
||||||
|
let published = music_dht::device_sync::redact_for_jam(playback_state_from_ui(&host));
|
||||||
|
let json = serde_json::to_string(&published).unwrap();
|
||||||
|
for secret in ["Episode 1", "Episode 2", "example.test", "Show"] {
|
||||||
|
assert!(!json.contains(secret), "{secret} leaked to Jam: {json}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The guest shows (and sends back) numbered private items.
|
||||||
|
let mut guest = AppState::default();
|
||||||
|
apply_playback_state_to_ui(&mut guest, &published, None);
|
||||||
|
assert!(crate::devices::is_private_track_item(
|
||||||
|
&guest.player.queue[1]
|
||||||
|
));
|
||||||
|
assert_ne!(
|
||||||
|
state::track_key(&guest.player.queue[1]),
|
||||||
|
state::track_key(&guest.player.queue[3])
|
||||||
|
);
|
||||||
|
// It moves a placeholder and skips to the last item.
|
||||||
|
guest.player.queue.swap(2, 3);
|
||||||
|
guest.player.queue_pos = 3;
|
||||||
|
let command = playback_state_from_ui(&guest);
|
||||||
|
assert!(command.queue[2].is_private_placeholder());
|
||||||
|
let (restored, _) = jam_participant_state(&host, command.clone(), true).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
restored.queue[2].podcast_episode_ref(),
|
||||||
|
episode_track(2).podcast_episode_ref()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
restored.queue[3].content_id.as_deref(),
|
||||||
|
music(2).content_id.as_deref()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Dropping, adding or duplicating private items is refused.
|
||||||
|
let mut dropped = command.clone();
|
||||||
|
dropped.queue.remove(1);
|
||||||
|
assert!(jam_participant_state(&host, dropped, false).is_none());
|
||||||
|
let mut injected = command.clone();
|
||||||
|
injected
|
||||||
|
.queue
|
||||||
|
.push(PlaybackTrack::from_track(&episode_track(9)));
|
||||||
|
assert!(jam_participant_state(&host, injected, false).is_none());
|
||||||
|
let mut duplicated = command;
|
||||||
|
duplicated.queue.push(PlaybackTrack::private_placeholder());
|
||||||
|
assert!(jam_participant_state(&host, duplicated, false).is_none());
|
||||||
|
|
||||||
|
// Guests cannot seek in the host's private episode.
|
||||||
|
let mut seek = published.clone();
|
||||||
|
seek.position_secs = 3.0;
|
||||||
|
let (restored, seek) = jam_participant_state(&host, seek, true).unwrap();
|
||||||
|
assert!(!seek);
|
||||||
|
assert_eq!(restored.position_secs, host.player.position_secs);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_older_jam_guest_cannot_drop_the_hosts_episodes() {
|
||||||
|
// A furumi 0.3 guest reads the redacted queue with its own types,
|
||||||
|
// which drop the `source` of every item
|
||||||
|
// (`devices::wire_compat_tests::v3_peers_would_drop_the_source_of_podcast_items`):
|
||||||
|
// the state it sends back has bare music items where the host's
|
||||||
|
// episodes are.
|
||||||
|
let host = mixed_state();
|
||||||
|
let published = music_dht::device_sync::redact_for_jam(playback_state_from_ui(&host));
|
||||||
|
let mut echoed = serde_json::to_value(&published).unwrap();
|
||||||
|
for item in echoed["queue"].as_array_mut().unwrap() {
|
||||||
|
item.as_object_mut().unwrap().remove("source");
|
||||||
|
}
|
||||||
|
echoed["paused"] = false.into();
|
||||||
|
let echoed: PlaybackStateWire = serde_json::from_value(echoed).unwrap();
|
||||||
|
assert_eq!(echoed.queue.len(), host.player.queue.len());
|
||||||
|
assert!(
|
||||||
|
echoed
|
||||||
|
.queue
|
||||||
|
.iter()
|
||||||
|
.all(|track| !track.requires_podcast_support())
|
||||||
|
);
|
||||||
|
let json = serde_json::to_string(&echoed).unwrap();
|
||||||
|
for secret in ["Episode 1", "Episode 2", "example.test", "Show"] {
|
||||||
|
assert!(!json.contains(secret), "{secret} reached the old guest");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Its commands are refused while the queue holds episodes: the host
|
||||||
|
// keeps them instead of playing "Private item" as music.
|
||||||
|
assert!(jam_participant_state(&host, echoed.clone(), false).is_none());
|
||||||
|
assert!(jam_participant_state(&host, echoed, true).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_guest_leaving_a_jam_drops_the_hosts_placeholders() {
|
||||||
|
let mut guest = AppState::default();
|
||||||
|
let published =
|
||||||
|
music_dht::device_sync::redact_for_jam(playback_state_from_ui(&mixed_state()));
|
||||||
|
apply_playback_state_to_ui(&mut guest, &published, None);
|
||||||
|
guest.player.queue_pos = 2;
|
||||||
|
guest.player.current = guest.player.queue.get(2).cloned();
|
||||||
|
drop_private_items(&mut guest);
|
||||||
|
assert_eq!(keys(&guest.player.queue), keys(&[music(1), music(2)]));
|
||||||
|
assert_eq!(guest.player.queue_pos, 1);
|
||||||
|
assert!(guest.player.current.is_some());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 } => {
|
Popup::ConfirmDelete { target, label } => {
|
||||||
handle_confirm_delete(state, runtime, target, label, key);
|
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 {
|
Popup::SimilarityPrivacyConsent { enable_federation } => match key.code {
|
||||||
KeyCode::Enter | KeyCode::Char('y') => {
|
KeyCode::Enter | KeyCode::Char('y') => {
|
||||||
state.similarity.settings.federation_consent = true;
|
state.similarity.settings.federation_consent = true;
|
||||||
@@ -557,10 +580,31 @@ fn handle_fed_input(
|
|||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
) {
|
) {
|
||||||
match key.code {
|
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 => {
|
KeyCode::Enter => {
|
||||||
let value = input.trim().to_string();
|
let value = input.trim().to_string();
|
||||||
match field {
|
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 => {
|
FedInputField::NetworkId => {
|
||||||
state.federation.settings.network_id = value;
|
state.federation.settings.network_id = value;
|
||||||
// An empty id turns federation off rather than leaving a
|
// An empty id turns federation off rather than leaving a
|
||||||
@@ -666,7 +710,13 @@ fn handle_fed_input(
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
input.handle_key(key);
|
input.handle_key(key);
|
||||||
|
let search = (field == FedInputField::PodcastSearch).then(|| input.to_string());
|
||||||
state.popup = Some(Popup::FedInput { field, input });
|
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.
|
/// 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 cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect();
|
||||||
|
let mut search = None;
|
||||||
match &mut state.popup {
|
match &mut state.popup {
|
||||||
Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned),
|
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, .. }) => {
|
Some(Popup::Edit { fields, focus, .. }) => {
|
||||||
if let Some(field) = fields.get_mut(*focus) {
|
if let Some(field) = fields.get_mut(*focus) {
|
||||||
field.value.insert_str(&cleaned);
|
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();
|
let len = tracks.len();
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q') => {}
|
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') => {
|
KeyCode::Char('a') => {
|
||||||
let artists = tracks
|
let artists = tracks
|
||||||
.get(cursor.min(len.saturating_sub(1)))
|
.get(cursor.min(len.saturating_sub(1)))
|
||||||
@@ -1079,6 +1155,10 @@ fn handle_track_info(
|
|||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
if let Some(track) = tracks.get(cursor.min(len.saturating_sub(1))) {
|
if let Some(track) = tracks.get(cursor.min(len.saturating_sub(1))) {
|
||||||
match crate::share::track_share_link(track) {
|
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) => {
|
Some(link) => {
|
||||||
state.popup = Some(Popup::FedCopyText {
|
state.popup = Some(Popup::FedCopyText {
|
||||||
title: "Track share link".into(),
|
title: "Track share link".into(),
|
||||||
@@ -1108,7 +1188,7 @@ fn handle_track_info(
|
|||||||
cursor,
|
cursor,
|
||||||
scroll,
|
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.status_message = Some("similarity search starts from a local track".into());
|
||||||
state.popup = Some(Popup::TrackInfo {
|
state.popup = Some(Popup::TrackInfo {
|
||||||
tracks,
|
tracks,
|
||||||
|
|||||||
+312
-3
@@ -338,6 +338,7 @@ mod tests {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
let mut state = AppState::default();
|
let mut state = AppState::default();
|
||||||
@@ -762,6 +763,37 @@ pub enum Popup {
|
|||||||
},
|
},
|
||||||
/// Delete confirmation; Enter/y deletes, Esc/n cancels.
|
/// Delete confirmation; Enter/y deletes, Esc/n cancels.
|
||||||
ConfirmDelete { target: DeleteTarget, label: String },
|
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.
|
/// Enabling network similarity reveals an embedding query to peers.
|
||||||
SimilarityPrivacyConsent { enable_federation: bool },
|
SimilarityPrivacyConsent { enable_federation: bool },
|
||||||
/// Derived data is safe to recreate but potentially expensive.
|
/// Derived data is safe to recreate but potentially expensive.
|
||||||
@@ -899,6 +931,10 @@ pub enum FedInputField {
|
|||||||
DeviceName,
|
DeviceName,
|
||||||
ConnectInvite,
|
ConnectInvite,
|
||||||
JamInvite,
|
JamInvite,
|
||||||
|
/// Podcasts: RSS/Atom feed URL of a new subscription.
|
||||||
|
PodcastFeedUrl,
|
||||||
|
/// Podcasts: live search in the opened episode list.
|
||||||
|
PodcastSearch,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FedInputField {
|
impl FedInputField {
|
||||||
@@ -913,6 +949,8 @@ impl FedInputField {
|
|||||||
FedInputField::DeviceName => "Device name",
|
FedInputField::DeviceName => "Device name",
|
||||||
FedInputField::ConnectInvite => "Connect device (paste frid://i invite)",
|
FedInputField::ConnectInvite => "Connect device (paste frid://i invite)",
|
||||||
FedInputField::JamInvite => "Join Jam (paste frid://j invite)",
|
FedInputField::JamInvite => "Join Jam (paste frid://j invite)",
|
||||||
|
FedInputField::PodcastFeedUrl => "Add podcast (RSS feed URL)",
|
||||||
|
FedInputField::PodcastSearch => "Search episodes",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -945,6 +983,12 @@ impl FedInputField {
|
|||||||
FedInputField::JamInvite => {
|
FedInputField::JamInvite => {
|
||||||
"Paste a frid://j capability to control the host player for this Jam only."
|
"Paste a frid://j capability to control the host player for this Jam only."
|
||||||
}
|
}
|
||||||
|
FedInputField::PodcastFeedUrl => {
|
||||||
|
"Paste the podcast's RSS or Atom feed URL. Only episode information is stored; audio streams from the publisher when you play. New subscriptions are private."
|
||||||
|
}
|
||||||
|
FedInputField::PodcastSearch => {
|
||||||
|
"Words from episode titles or descriptions; the list updates as you type. Enter keeps the search, Esc clears it."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1382,7 +1426,7 @@ impl SimilaritySearchHit {
|
|||||||
Self::Local { track, .. } => track
|
Self::Local { track, .. } => track
|
||||||
.content_id
|
.content_id
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or_else(|| format!("local:{}", track.id)),
|
.unwrap_or_else(|| track.key().to_string()),
|
||||||
Self::Federated { track, .. } => track
|
Self::Federated { track, .. } => track
|
||||||
.content_id
|
.content_id
|
||||||
.clone()
|
.clone()
|
||||||
@@ -1402,7 +1446,14 @@ pub struct SearchState {
|
|||||||
/// Artists a federated card can be opened for — from artist records and
|
/// Artists a federated card can be opened for — from artist records and
|
||||||
/// from the artist names of matching tracks.
|
/// from the artist names of matching tracks.
|
||||||
pub fed_artists: Vec<crate::federation::FedArtistHit>,
|
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,
|
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
|
/// Present only for a track-seeded search; text-search refreshes must not
|
||||||
/// replace this page with a title query.
|
/// replace this page with a title query.
|
||||||
pub similarity_source: Option<i64>,
|
pub similarity_source: Option<i64>,
|
||||||
@@ -1415,6 +1466,24 @@ pub struct SearchState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
pub fn similarity_len(&self) -> usize {
|
||||||
usize::from(self.similarity_source_track.is_some()) + self.similarity_tracks.len()
|
usize::from(self.similarity_source_track.is_some()) + self.similarity_tracks.len()
|
||||||
}
|
}
|
||||||
@@ -1467,15 +1536,20 @@ pub enum Tab {
|
|||||||
Global,
|
Global,
|
||||||
Playlists,
|
Playlists,
|
||||||
Queue,
|
Queue,
|
||||||
|
Podcasts,
|
||||||
Federation,
|
Federation,
|
||||||
Logs,
|
Logs,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tab {
|
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::Global,
|
||||||
Tab::Playlists,
|
Tab::Playlists,
|
||||||
Tab::Queue,
|
Tab::Queue,
|
||||||
|
Tab::Podcasts,
|
||||||
Tab::Federation,
|
Tab::Federation,
|
||||||
Tab::Logs,
|
Tab::Logs,
|
||||||
];
|
];
|
||||||
@@ -1485,11 +1559,24 @@ impl Tab {
|
|||||||
Tab::Global => "Library",
|
Tab::Global => "Library",
|
||||||
Tab::Playlists => "Playlists",
|
Tab::Playlists => "Playlists",
|
||||||
Tab::Queue => "Queue",
|
Tab::Queue => "Queue",
|
||||||
|
Tab::Podcasts => "Podcasts",
|
||||||
Tab::Federation => "Settings",
|
Tab::Federation => "Settings",
|
||||||
Tab::Logs => "Logs",
|
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 {
|
pub fn index(self) -> usize {
|
||||||
Self::ALL.iter().position(|t| *t == self).unwrap()
|
Self::ALL.iter().position(|t| *t == self).unwrap()
|
||||||
}
|
}
|
||||||
@@ -1511,6 +1598,7 @@ impl Tab {
|
|||||||
Tab::Global => KeyContext::Library,
|
Tab::Global => KeyContext::Library,
|
||||||
Tab::Playlists => KeyContext::Playlists,
|
Tab::Playlists => KeyContext::Playlists,
|
||||||
Tab::Queue => KeyContext::Queue,
|
Tab::Queue => KeyContext::Queue,
|
||||||
|
Tab::Podcasts => KeyContext::Podcasts,
|
||||||
Tab::Federation => KeyContext::Federation,
|
Tab::Federation => KeyContext::Federation,
|
||||||
Tab::Logs => KeyContext::Logs,
|
Tab::Logs => KeyContext::Logs,
|
||||||
}
|
}
|
||||||
@@ -1646,6 +1734,208 @@ 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
|
/// Single source of truth for the UI. Mutated only by `update()` and the
|
||||||
/// event handlers in the main loop; views render from `&AppState`.
|
/// event handlers in the main loop; views render from `&AppState`.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@@ -1698,6 +1988,7 @@ pub struct AppState {
|
|||||||
pub queue_tab: QueueTab,
|
pub queue_tab: QueueTab,
|
||||||
pub federation: FederationTab,
|
pub federation: FederationTab,
|
||||||
pub similarity: SimilarityTab,
|
pub similarity: SimilarityTab,
|
||||||
|
pub podcasts: PodcastsTab,
|
||||||
/// The one federated artist card being viewed (name + loading state);
|
/// The one federated artist card being viewed (name + loading state);
|
||||||
/// opening another card replaces it.
|
/// opening another card replaces it.
|
||||||
pub fed_artist_view: Option<(String, Loadable<crate::federation::FedArtistCard>)>,
|
pub fed_artist_view: Option<(String, Loadable<crate::federation::FedArtistCard>)>,
|
||||||
@@ -2162,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> {
|
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||||
|
if track.is_podcast() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
track
|
track
|
||||||
.content_id
|
.content_id
|
||||||
.as_deref()
|
.as_deref()
|
||||||
@@ -2176,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 {
|
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) {
|
if let Some(content_id) = track_content_id(track) {
|
||||||
return format!("content:{content_id}");
|
return format!("content:{content_id}");
|
||||||
}
|
}
|
||||||
if let Some(fed) = &track.fed {
|
if let Some(fed) = &track.fed {
|
||||||
return format!("fed:{}:{}", fed.owner, fed.item_id);
|
return format!("fed:{}:{}", fed.owner, fed.item_id);
|
||||||
}
|
}
|
||||||
format!("local:{}", track.id)
|
track.key().to_string()
|
||||||
}
|
}
|
||||||
+185
-16
@@ -20,8 +20,12 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit";
|
|||||||
pub enum Effect {
|
pub enum Effect {
|
||||||
CheckUpdate,
|
CheckUpdate,
|
||||||
InstallUpdate(crate::updater::Update),
|
InstallUpdate(crate::updater::Update),
|
||||||
/// (Re)start playback of `queue[queue_pos]`.
|
/// (Re)start playback of `queue[queue_pos]` (a podcast episode resumes
|
||||||
|
/// from its saved position).
|
||||||
PlayCurrent,
|
PlayCurrent,
|
||||||
|
/// Play `queue[queue_pos]`, a podcast episode, from the beginning as an
|
||||||
|
/// explicit "start over".
|
||||||
|
PlayCurrentFromStart,
|
||||||
TogglePause,
|
TogglePause,
|
||||||
StopPlayback,
|
StopPlayback,
|
||||||
/// Seek relative to the current position, in seconds.
|
/// Seek relative to the current position, in seconds.
|
||||||
@@ -106,6 +110,58 @@ pub enum Effect {
|
|||||||
},
|
},
|
||||||
/// Load qualified listening history without blocking the UI thread.
|
/// Load qualified listening history without blocking the UI thread.
|
||||||
LoadListenHistory,
|
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> {
|
pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||||
@@ -115,6 +171,14 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
.take()
|
.take()
|
||||||
.is_some_and(|deadline| Instant::now() <= deadline);
|
.is_some_and(|deadline| Instant::now() <= deadline);
|
||||||
state.status_message = None;
|
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
|
if state.additional_settings_open
|
||||||
&& !matches!(
|
&& !matches!(
|
||||||
action,
|
action,
|
||||||
@@ -325,6 +389,18 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Action::CycleVisualizerMode => {
|
||||||
|
if state.visualizer.active {
|
||||||
|
if let Err(err) = state.visualizer.cycle_mode() {
|
||||||
|
// the fullscreen visualizer hides the status line
|
||||||
|
tracing::warn!(error = %format!("{err:#}"), "saving the visualizer mode failed");
|
||||||
|
state.status_message = Some(format!("visualization settings: {err:#}"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state.status_message = Some("open the visualizer first (Shift+L)".into());
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
Action::Select => select_current(state),
|
Action::Select => select_current(state),
|
||||||
Action::Back if state.visualizer.active => {
|
Action::Back if state.visualizer.active => {
|
||||||
state.visualizer.close();
|
state.visualizer.close();
|
||||||
@@ -352,7 +428,14 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
let mut local_tracks: Vec<TrackItem> = Vec::new();
|
let mut local_tracks: Vec<TrackItem> = Vec::new();
|
||||||
let mut fed_tracks: Vec<crate::federation::FedTrack> = Vec::new();
|
let mut fed_tracks: Vec<crate::federation::FedTrack> = Vec::new();
|
||||||
let mut seen = std::collections::HashSet::new();
|
let mut seen = std::collections::HashSet::new();
|
||||||
|
let mut episodes = 0usize;
|
||||||
for track in tracks {
|
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)) {
|
if !seen.insert(track_key(&track)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -365,7 +448,11 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if local_tracks.is_empty() && fed_tracks.is_empty() {
|
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
|
None
|
||||||
} else {
|
} else {
|
||||||
let should_like = local_tracks.iter().any(|track| !state.track_liked(track))
|
let should_like = local_tracks.iter().any(|track| !state.track_liked(track))
|
||||||
@@ -411,6 +498,9 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
Action::GoToRelease => {
|
Action::GoToRelease => {
|
||||||
let track = selected_track(state).or_else(|| state.player.current.clone());
|
let track = selected_track(state).or_else(|| state.player.current.clone());
|
||||||
match track {
|
match track {
|
||||||
|
Some(track) if track.is_podcast() => {
|
||||||
|
return podcasts::reveal_episode_podcast(state, &track);
|
||||||
|
}
|
||||||
Some(track) => open_release_for_track(state, &track),
|
Some(track) => open_release_for_track(state, &track),
|
||||||
None => state.status_message = Some("no track selected".into()),
|
None => state.status_message = Some("no track selected".into()),
|
||||||
}
|
}
|
||||||
@@ -418,12 +508,17 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
}
|
}
|
||||||
Action::AddToPlaylist => {
|
Action::AddToPlaylist => {
|
||||||
let target = selected_playlist_target(state, true);
|
let target = selected_playlist_target(state, true);
|
||||||
|
let target = without_podcast_episodes(state, target);
|
||||||
match target {
|
match target {
|
||||||
Some(target) => {
|
Some(target) => {
|
||||||
state.popup = Some(super::state::Popup::AddToPlaylist { target, cursor: 0 });
|
state.popup = Some(super::state::Popup::AddToPlaylist { target, cursor: 0 });
|
||||||
state.track_selection.clear();
|
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
|
None
|
||||||
}
|
}
|
||||||
@@ -442,7 +537,8 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::NewPlaylist => {
|
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 {
|
state.popup = Some(super::state::Popup::NewPlaylist {
|
||||||
for_target,
|
for_target,
|
||||||
input: crate::app::input::LineEdit::default(),
|
input: crate::app::input::LineEdit::default(),
|
||||||
@@ -475,6 +571,22 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
Action::DeleteSelected => delete_selected(state),
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +601,9 @@ fn current_track_for_info(state: &AppState) -> Option<TrackItem> {
|
|||||||
queued_at_position
|
queued_at_position
|
||||||
.filter(|queued| {
|
.filter(|queued| {
|
||||||
super::state::track_key(queued) == current_key
|
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()
|
|| current.is_fed_pending()
|
||||||
})
|
})
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
@@ -542,6 +656,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(
|
fn selected_playlist_target(
|
||||||
state: &AppState,
|
state: &AppState,
|
||||||
include_current: bool,
|
include_current: bool,
|
||||||
@@ -629,6 +773,10 @@ fn open_edit_popup(state: &mut AppState) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Some(track) = selected_track(state).or_else(|| state.player.current.clone()) {
|
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));
|
state.popup = Some(track_edit_popup(&track));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1016,7 +1164,7 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
|
|||||||
state.queue_tab.cursor,
|
state.queue_tab.cursor,
|
||||||
state.player.queue.len(),
|
state.player.queue.len(),
|
||||||
)),
|
)),
|
||||||
Tab::Federation | Tab::Logs => None,
|
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1070,7 +1218,7 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
|
|||||||
state.queue_tab.cursor,
|
state.queue_tab.cursor,
|
||||||
state.player.queue.clone(),
|
state.player.queue.clone(),
|
||||||
)),
|
)),
|
||||||
Tab::Federation | Tab::Logs => None,
|
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1371,6 +1519,7 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
|
|||||||
.map(|track| (*track).clone())
|
.map(|track| (*track).clone())
|
||||||
}
|
}
|
||||||
Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(),
|
Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(),
|
||||||
|
Tab::Podcasts => state.podcasts.selected_episode_track(),
|
||||||
Tab::Federation | Tab::Logs => None,
|
Tab::Federation | Tab::Logs => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1390,8 +1539,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 {
|
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).
|
/// A *release* under the cursor (artist-view tile/row or a search release).
|
||||||
@@ -1427,6 +1582,9 @@ fn selected_release_id(state: &AppState) -> Option<i64> {
|
|||||||
/// an async fetch effect.
|
/// an async fetch effect.
|
||||||
fn queue_add(state: &mut AppState, next: bool) -> Option<Effect> {
|
fn queue_add(state: &mut AppState, next: bool) -> Option<Effect> {
|
||||||
let tracks = selected_tracks(state);
|
let tracks = selected_tracks(state);
|
||||||
|
if tracks.iter().any(TrackItem::is_podcast) && !podcasts::episodes_play_here(state) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
if !tracks.is_empty() {
|
if !tracks.is_empty() {
|
||||||
let count = tracks.len();
|
let count = tracks.len();
|
||||||
let title = tracks[0].title.clone();
|
let title = tracks[0].title.clone();
|
||||||
@@ -1931,9 +2089,7 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
|
|||||||
let total = if state.search.similarity_source.is_some() {
|
let total = if state.search.similarity_source.is_some() {
|
||||||
state.search.similarity_len()
|
state.search.similarity_len()
|
||||||
} else {
|
} else {
|
||||||
state.search.results.as_ref().map_or(0, |r| r.len())
|
state.search.total_len()
|
||||||
+ state.search.fed_artists.len()
|
|
||||||
+ state.search.fed_tracks.len()
|
|
||||||
} as isize;
|
} as isize;
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
return;
|
return;
|
||||||
@@ -2057,9 +2213,7 @@ fn current_view_len(state: &AppState) -> usize {
|
|||||||
if state.search.similarity_source.is_some() {
|
if state.search.similarity_source.is_some() {
|
||||||
state.search.similarity_len()
|
state.search.similarity_len()
|
||||||
} else {
|
} else {
|
||||||
state.search.results.as_ref().map_or(0, |r| r.len())
|
state.search.total_len()
|
||||||
+ state.search.fed_artists.len()
|
|
||||||
+ state.search.fed_tracks.len()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(GlobalView::FedArtist { .. }) => fed_card_len(state),
|
Some(GlobalView::FedArtist { .. }) => fed_card_len(state),
|
||||||
@@ -2495,6 +2649,10 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
|
|||||||
state.active_tab = Tab::Global;
|
state.active_tab = Tab::Global;
|
||||||
Some(Effect::FedOpenArtist(name))
|
Some(Effect::FedOpenArtist(name))
|
||||||
}
|
}
|
||||||
|
Outcome::OpenPodcastCard(index) => {
|
||||||
|
podcast_discovery::open_card(state, index);
|
||||||
|
None
|
||||||
|
}
|
||||||
Outcome::OpenFedRelease { name, card, index } => {
|
Outcome::OpenFedRelease { name, card, index } => {
|
||||||
let start =
|
let start =
|
||||||
if card.releases.get(index).is_some_and(|release| {
|
if card.releases.get(index).is_some_and(|release| {
|
||||||
@@ -2736,6 +2894,7 @@ enum Outcome {
|
|||||||
start: usize,
|
start: usize,
|
||||||
},
|
},
|
||||||
OpenFedArtist(String),
|
OpenFedArtist(String),
|
||||||
|
OpenPodcastCard(usize),
|
||||||
OpenFedRelease {
|
OpenFedRelease {
|
||||||
name: String,
|
name: String,
|
||||||
card: crate::federation::FedArtistCard,
|
card: crate::federation::FedArtistCard,
|
||||||
@@ -2746,7 +2905,8 @@ enum Outcome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Enter inside the federated section of the search results: artists open
|
/// 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 {
|
fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
|
||||||
let artists = &state.search.fed_artists;
|
let artists = &state.search.fed_artists;
|
||||||
if fed_index < artists.len() {
|
if fed_index < artists.len() {
|
||||||
@@ -2754,7 +2914,12 @@ fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome {
|
|||||||
}
|
}
|
||||||
let start = fed_index - artists.len();
|
let start = fed_index - artists.len();
|
||||||
if start >= state.search.fed_tracks.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 {
|
Outcome::Play {
|
||||||
tracks: state
|
tracks: state
|
||||||
@@ -3251,6 +3416,7 @@ fn reset_tab(state: &mut AppState, tab: Tab) {
|
|||||||
state.logs.selected_seq = None;
|
state.logs.selected_seq = None;
|
||||||
}
|
}
|
||||||
Tab::Queue => {}
|
Tab::Queue => {}
|
||||||
|
Tab::Podcasts => podcasts::close(state),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3258,6 +3424,9 @@ fn not_yet(state: &mut AppState, what: &str) {
|
|||||||
state.status_message = Some(format!("{what}: coming in a later milestone"));
|
state.status_message = Some(format!("{what}: coming in a later milestone"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) mod podcast_discovery;
|
||||||
|
pub(crate) mod podcasts;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[path = "update_tests.rs"]
|
#[path = "update_tests.rs"]
|
||||||
mod tests;
|
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.
|
# Default keybindings for furumi.
|
||||||
#
|
#
|
||||||
# To customize, copy entries into <config dir>/furumi/keymap.toml
|
# To customize, copy entries into keymap.toml in the platform config dir
|
||||||
# (~/.config/furumi/keymap.toml on Linux/macOS). A user binding replaces the
|
# (~/.config/furumi/keymap.toml on Linux,
|
||||||
# default binding with the same key sequence and context.
|
# ~/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".
|
# key_sequence: space-separated chords, e.g. "g g" or "ctrl-a x".
|
||||||
# Modifiers: ctrl-, alt-, shift-, cmd-. Uppercase letters: "shift-g".
|
# Modifiers: ctrl-, alt-, shift-, cmd-. Uppercase letters: "shift-g".
|
||||||
# command: an Action name, optionally with parameters:
|
# command: an Action name, optionally with parameters:
|
||||||
# command = { SeekForward = { seconds = 30 } }
|
# command = { SeekForward = { seconds = 30 } }
|
||||||
# context: optional view filter — global (default), library, search,
|
# context: optional view filter — global (default), library, search,
|
||||||
# playlists, queue, logs.
|
# playlists, queue, podcasts, settings, logs.
|
||||||
|
|
||||||
|
version = 2
|
||||||
|
|
||||||
[[keymaps]]
|
[[keymaps]]
|
||||||
key_sequence = "q"
|
key_sequence = "q"
|
||||||
@@ -51,6 +63,10 @@ command = { GoToTab = 3 }
|
|||||||
key_sequence = "5"
|
key_sequence = "5"
|
||||||
command = { GoToTab = 4 }
|
command = { GoToTab = 4 }
|
||||||
|
|
||||||
|
[[keymaps]]
|
||||||
|
key_sequence = "6"
|
||||||
|
command = { GoToTab = 5 }
|
||||||
|
|
||||||
[[keymaps]]
|
[[keymaps]]
|
||||||
key_sequence = "y"
|
key_sequence = "y"
|
||||||
command = "DownloadSelected"
|
command = "DownloadSelected"
|
||||||
@@ -207,6 +223,10 @@ command = "CycleRepeat"
|
|||||||
key_sequence = "shift-l"
|
key_sequence = "shift-l"
|
||||||
command = "ToggleVisualizer"
|
command = "ToggleVisualizer"
|
||||||
|
|
||||||
|
[[keymaps]]
|
||||||
|
key_sequence = "shift-m"
|
||||||
|
command = "CycleVisualizerMode"
|
||||||
|
|
||||||
[[keymaps]]
|
[[keymaps]]
|
||||||
key_sequence = "shift-h"
|
key_sequence = "shift-h"
|
||||||
command = "OpenListenHistory"
|
command = "OpenListenHistory"
|
||||||
@@ -255,3 +275,56 @@ command = "OpenCommandLine"
|
|||||||
[[keymaps]]
|
[[keymaps]]
|
||||||
key_sequence = "/"
|
key_sequence = "/"
|
||||||
command = "OpenSearch"
|
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"
|
||||||
+341
-4
@@ -9,6 +9,14 @@ use crate::app::action::Action;
|
|||||||
|
|
||||||
const DEFAULT_KEYMAP: &str = include_str!("default_keymap.toml");
|
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;
|
/// Input context a binding applies to. `Global` bindings work everywhere;
|
||||||
/// view-specific bindings shadow global ones for the same key sequence.
|
/// view-specific bindings shadow global ones for the same key sequence.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
|
||||||
@@ -20,6 +28,7 @@ pub enum KeyContext {
|
|||||||
Search,
|
Search,
|
||||||
Playlists,
|
Playlists,
|
||||||
Queue,
|
Queue,
|
||||||
|
Podcasts,
|
||||||
#[serde(rename = "settings", alias = "federation")]
|
#[serde(rename = "settings", alias = "federation")]
|
||||||
Federation,
|
Federation,
|
||||||
Logs,
|
Logs,
|
||||||
@@ -33,6 +42,7 @@ impl KeyContext {
|
|||||||
KeyContext::Search => "search",
|
KeyContext::Search => "search",
|
||||||
KeyContext::Playlists => "playlists",
|
KeyContext::Playlists => "playlists",
|
||||||
KeyContext::Queue => "queue",
|
KeyContext::Queue => "queue",
|
||||||
|
KeyContext::Podcasts => "podcasts",
|
||||||
KeyContext::Federation => "settings",
|
KeyContext::Federation => "settings",
|
||||||
KeyContext::Logs => "logs",
|
KeyContext::Logs => "logs",
|
||||||
}
|
}
|
||||||
@@ -52,10 +62,18 @@ struct RawBinding {
|
|||||||
command: Action,
|
command: Action,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
context: KeyContext,
|
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)]
|
#[derive(Debug, Default, Deserialize)]
|
||||||
struct KeymapFile {
|
struct KeymapFile {
|
||||||
|
/// Format version; `None` means a legacy (pre-version-2) file.
|
||||||
|
#[serde(default)]
|
||||||
|
version: Option<u32>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
keymaps: Vec<RawBinding>,
|
keymaps: Vec<RawBinding>,
|
||||||
}
|
}
|
||||||
@@ -87,9 +105,15 @@ impl Keymap {
|
|||||||
{
|
{
|
||||||
match fs::read_to_string(&path)
|
match fs::read_to_string(&path)
|
||||||
.map_err(anyhow::Error::from)
|
.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) => {
|
Err(err) => {
|
||||||
warning = Some(format!(
|
warning = Some(format!(
|
||||||
"{} ignored: {err:#}; using default keybindings",
|
"{} ignored: {err:#}; using default keybindings",
|
||||||
@@ -106,6 +130,16 @@ impl Keymap {
|
|||||||
(keymap, warning)
|
(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
|
/// Feed one key combination; returns an action, a pending-chord state, or
|
||||||
/// nothing. Esc clears a pending chord instead of resolving.
|
/// nothing. Esc clears a pending chord instead of resolving.
|
||||||
pub fn resolve(&mut self, key: KeyCombination, context: KeyContext) -> KeyResolution {
|
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>> {
|
fn parse_bindings(text: &str) -> Result<Vec<Binding>> {
|
||||||
|
parse_file(text).map(|(_, bindings)| bindings)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A user keymap after version handling, plus a one-time, non-blocking
|
||||||
|
/// notice for the status bar and log when its meaning was adapted.
|
||||||
|
struct UserKeymap {
|
||||||
|
bindings: Vec<Binding>,
|
||||||
|
notice: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_user_keymap(text: &str) -> Result<UserKeymap> {
|
||||||
|
let (version, mut bindings) = parse_file(text)?;
|
||||||
|
let notice = match version {
|
||||||
|
None | Some(1) => {
|
||||||
|
let remapped = remap_legacy_tab_indices(&mut bindings);
|
||||||
|
(remapped > 0).then(|| {
|
||||||
|
format!(
|
||||||
|
"{remapped} tab shortcut(s) adapted to the new tab order \
|
||||||
|
(4 Podcasts, 5 Settings, 6 Logs); \
|
||||||
|
add `version = {KEYMAP_VERSION}` to use indices as written"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Some(KEYMAP_VERSION) => None,
|
||||||
|
Some(0) => bail!("unsupported keymap version 0"),
|
||||||
|
Some(newer) => Some(format!(
|
||||||
|
"keymap version {newer} is newer than this build supports \
|
||||||
|
({KEYMAP_VERSION}); bindings are used as written"
|
||||||
|
)),
|
||||||
|
};
|
||||||
|
Ok(UserKeymap { bindings, notice })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pre-version-2 `GoToTab` index → current index. Index 5 only existed in the
|
||||||
|
/// prototype podcast build, where it opened Podcasts.
|
||||||
|
fn legacy_tab_index(index: usize) -> usize {
|
||||||
|
match index {
|
||||||
|
3 => 4, // Settings
|
||||||
|
4 => 5, // Logs
|
||||||
|
5 => 3, // Podcasts (prototype)
|
||||||
|
other => other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rewrites legacy `GoToTab` indices in memory; returns how many changed.
|
||||||
|
fn remap_legacy_tab_indices(bindings: &mut [Binding]) -> usize {
|
||||||
|
let mut remapped = 0;
|
||||||
|
for binding in bindings {
|
||||||
|
if let Action::GoToTab(index) = &mut binding.action {
|
||||||
|
let current = legacy_tab_index(*index);
|
||||||
|
if current != *index {
|
||||||
|
*index = current;
|
||||||
|
remapped += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
remapped
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_file(text: &str) -> Result<(Option<u32>, Vec<Binding>)> {
|
||||||
let file: KeymapFile = toml::from_str(text).context("invalid TOML")?;
|
let file: KeymapFile = toml::from_str(text).context("invalid TOML")?;
|
||||||
file.keymaps
|
let bindings = file
|
||||||
|
.keymaps
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|raw| {
|
.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)
|
let keys = parse_sequence(&raw.key_sequence)
|
||||||
.with_context(|| format!("bad key_sequence {:?}", raw.key_sequence))?;
|
.with_context(|| format!("bad key_sequence {:?}", raw.key_sequence))?;
|
||||||
Ok(Binding {
|
Ok(Binding {
|
||||||
@@ -287,7 +387,8 @@ fn parse_bindings(text: &str) -> Result<Vec<Binding>> {
|
|||||||
context: raw.context,
|
context: raw.context,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect()
|
.collect::<Result<_>>()?;
|
||||||
|
Ok((file.version, bindings))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_sequence(s: &str) -> Result<Vec<KeyCombination>> {
|
fn parse_sequence(s: &str) -> Result<Vec<KeyCombination>> {
|
||||||
@@ -483,6 +584,11 @@ mod tests {
|
|||||||
km.resolve(shift_l, KeyContext::Library),
|
km.resolve(shift_l, KeyContext::Library),
|
||||||
KeyResolution::Action(Action::ToggleVisualizer)
|
KeyResolution::Action(Action::ToggleVisualizer)
|
||||||
);
|
);
|
||||||
|
let shift_m = KeyCombination::new(KeyCode::Char('M'), KeyModifiers::SHIFT);
|
||||||
|
assert_eq!(
|
||||||
|
km.resolve(shift_m, KeyContext::Library),
|
||||||
|
KeyResolution::Action(Action::CycleVisualizerMode)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -597,6 +703,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]
|
#[test]
|
||||||
fn default_listening_history_key_resolves() {
|
fn default_listening_history_key_resolves() {
|
||||||
let mut km = keymap_from(DEFAULT_KEYMAP);
|
let mut km = keymap_from(DEFAULT_KEYMAP);
|
||||||
|
|||||||
+1139
-531
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
|
||||||
@@ -25,6 +25,7 @@ async fn localhost_web_peer() {
|
|||||||
library: Arc::new(Library::open(&dir.join("interop-library.sqlite3")).unwrap()),
|
library: Arc::new(Library::open(&dir.join("interop-library.sqlite3")).unwrap()),
|
||||||
event_tx: Default::default(),
|
event_tx: Default::default(),
|
||||||
playback: Default::default(),
|
playback: Default::default(),
|
||||||
|
podcasts: Default::default(),
|
||||||
};
|
};
|
||||||
let id = sync.ensure_identity().unwrap();
|
let id = sync.ensure_identity().unwrap();
|
||||||
let (tx, mut events) = tokio::sync::mpsc::unbounded_channel();
|
let (tx, mut events) = tokio::sync::mpsc::unbounded_channel();
|
||||||
@@ -55,13 +56,16 @@ async fn localhost_web_peer() {
|
|||||||
vector: BTreeMap::new(),
|
vector: BTreeMap::new(),
|
||||||
ops: vec![],
|
ops: vec![],
|
||||||
snapshot: SyncSnapshot::default(),
|
snapshot: SyncSnapshot::default(),
|
||||||
playback: sync.local_playback_snapshot(),
|
playback: sync.local_playback_snapshot_for(PROTOCOL_VERSION),
|
||||||
};
|
};
|
||||||
let mut bytes = serde_json::to_vec(&hello).unwrap();
|
let mut bytes = serde_json::to_vec(&hello).unwrap();
|
||||||
bytes.push(b'\n');
|
bytes.push(b'\n');
|
||||||
stream.write_all(&bytes).await.unwrap();
|
stream.write_all(&bytes).await.unwrap();
|
||||||
let response: WireMessage =
|
let response: WireMessage =
|
||||||
serde_json::from_slice(&read_line(&mut stream).await.unwrap()).unwrap();
|
serde_json::from_slice(
|
||||||
|
&music_dht::device_sync::read_line(&mut stream).await.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
let WireMessage::SyncResponse {
|
let WireMessage::SyncResponse {
|
||||||
accepted: true,
|
accepted: true,
|
||||||
playback: Some(snapshot),
|
playback: Some(snapshot),
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
+524
-13
@@ -30,6 +30,7 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
|||||||
library: Arc::new(Library::open(&dir.path().join("library.sqlite3")).unwrap()),
|
library: Arc::new(Library::open(&dir.path().join("library.sqlite3")).unwrap()),
|
||||||
event_tx: Default::default(),
|
event_tx: Default::default(),
|
||||||
playback: Default::default(),
|
playback: Default::default(),
|
||||||
|
podcasts: Default::default(),
|
||||||
});
|
});
|
||||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
sync.set_event_tx(tx);
|
sync.set_event_tx(tx);
|
||||||
@@ -54,8 +55,18 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
|||||||
let profile_b = b
|
let profile_b = b
|
||||||
.own_profile(&service_b.ticket().await.unwrap().to_string())
|
.own_profile(&service_b.ticket().await.unwrap().to_string())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
a.apply_device_profile(&profile_b, true).unwrap();
|
a.apply_device_profile(
|
||||||
b.apply_device_profile(&profile_a, true).unwrap();
|
&profile_b,
|
||||||
|
true,
|
||||||
|
VersionReport::SelfReported(Some(profile_b.protocol_version)),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
b.apply_device_profile(
|
||||||
|
&profile_a,
|
||||||
|
true,
|
||||||
|
VersionReport::SelfReported(Some(profile_a.protocol_version)),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
a.claim_playback(&profile_a.device_id).unwrap();
|
a.claim_playback(&profile_a.device_id).unwrap();
|
||||||
a.playback_tick(true, true).unwrap();
|
a.playback_tick(true, true).unwrap();
|
||||||
let mut state = empty_playback_state();
|
let mut state = empty_playback_state();
|
||||||
@@ -163,7 +174,12 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
|||||||
silent_profile.device_id = "silent-peer".into();
|
silent_profile.device_id = "silent-peer".into();
|
||||||
silent_profile.endpoint_id = silent.endpoint_id().to_string();
|
silent_profile.endpoint_id = silent.endpoint_id().to_string();
|
||||||
silent_profile.endpoint_ticket = silent.ticket().await.unwrap().to_string();
|
silent_profile.endpoint_ticket = silent.ticket().await.unwrap().to_string();
|
||||||
a.apply_device_profile(&silent_profile, true).unwrap();
|
a.apply_device_profile(
|
||||||
|
&silent_profile,
|
||||||
|
true,
|
||||||
|
VersionReport::SelfReported(Some(silent_profile.protocol_version)),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
lock(&a.conn)
|
lock(&a.conn)
|
||||||
.execute(
|
.execute(
|
||||||
"UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'",
|
"UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'",
|
||||||
@@ -218,6 +234,121 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
|||||||
.expect("localhost sync must complete within 30 seconds");
|
.expect("localhost sync must complete within 30 seconds");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Podcast state over the production stream handlers: a subscription and
|
||||||
|
/// progress from A reach B through `sync_device` → `handle_hello`, and B's
|
||||||
|
/// start over reaches A through the response of B's own sync.
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn localhost_devices_sync_podcasts() {
|
||||||
|
use super::podcast_sync_tests::{checkpoint, device_with_store, episode_ref, start, subscribe};
|
||||||
|
tokio::time::timeout(Duration::from_secs(30), async {
|
||||||
|
let dirs = [tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()];
|
||||||
|
let network = NetworkId::from_name(&format!("podcast-sync-test-{}", random_hex(16)));
|
||||||
|
let mut peers = Vec::new();
|
||||||
|
let mut servers = Vec::new();
|
||||||
|
for dir in &dirs {
|
||||||
|
std::fs::create_dir_all(dir.path().join("network")).unwrap();
|
||||||
|
let config = music_dht::MusicDhtConfig::builder()
|
||||||
|
.data_dir(dir.path().join("network"))
|
||||||
|
.network_id(network)
|
||||||
|
.stream_protocol(SYNC_ALPN)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let (service, _events) = MusicDhtService::start(config).await.unwrap();
|
||||||
|
let service = Arc::new(service);
|
||||||
|
let store = Arc::new(
|
||||||
|
furumi_library::podcasts::PodcastStore::open(&dir.path().join("podcasts.sqlite3"))
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
let device = device_with_store(store, true);
|
||||||
|
let sync = Arc::new(device.sync.clone());
|
||||||
|
let stats = Arc::new(crate::federation::TransportStats::default());
|
||||||
|
servers.push(tokio::spawn(serve_peers(
|
||||||
|
service.stream_acceptor(SYNC_ALPN).unwrap(),
|
||||||
|
sync.clone(),
|
||||||
|
service.clone(),
|
||||||
|
stats.clone(),
|
||||||
|
)));
|
||||||
|
peers.push((device, sync, service, stats));
|
||||||
|
}
|
||||||
|
let (a, sync_a, service_a, stats_a) = &peers[0];
|
||||||
|
let (b, sync_b, service_b, stats_b) = &peers[1];
|
||||||
|
sync_b
|
||||||
|
.set_group_id(&sync_a.ensure_identity().unwrap().group_id)
|
||||||
|
.unwrap();
|
||||||
|
let profile_a = sync_a
|
||||||
|
.own_profile(&service_a.ticket().await.unwrap().to_string())
|
||||||
|
.unwrap();
|
||||||
|
let profile_b = sync_b
|
||||||
|
.own_profile(&service_b.ticket().await.unwrap().to_string())
|
||||||
|
.unwrap();
|
||||||
|
for (sync, profile) in [(sync_a, &profile_b), (sync_b, &profile_a)] {
|
||||||
|
sync.apply_device_profile(
|
||||||
|
profile,
|
||||||
|
true,
|
||||||
|
VersionReport::SelfReported(Some(profile.protocol_version)),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let podcast_id = subscribe(a).podcast_id;
|
||||||
|
let episode = episode_ref(1);
|
||||||
|
let session = start(a, &episode, 0);
|
||||||
|
checkpoint(a, &session, 1_200_000);
|
||||||
|
sync_a
|
||||||
|
.sync_device(
|
||||||
|
service_a.clone(),
|
||||||
|
&sync_a.active_remote_devices().unwrap()[0],
|
||||||
|
stats_a.clone(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let subscribed = b
|
||||||
|
.store
|
||||||
|
.list_podcasts(furumi_library::podcasts::PodcastListFilter::Subscribed)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(subscribed.len(), 1);
|
||||||
|
assert_eq!(subscribed[0].podcast_id, podcast_id);
|
||||||
|
assert_eq!(
|
||||||
|
b.store.resume_position_ms(&episode.episode_id).unwrap(),
|
||||||
|
1_200_000
|
||||||
|
);
|
||||||
|
|
||||||
|
// B starts over; its next sync carries the start over to A, which
|
||||||
|
// then refuses A's older session.
|
||||||
|
let store = Arc::clone(&b.store);
|
||||||
|
sync_b
|
||||||
|
.run_podcast_action(|stamps| {
|
||||||
|
let write = store.restart(stamps, &episode, "restart-on-b")?;
|
||||||
|
Ok(((), vec![write.op]))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
sync_b
|
||||||
|
.sync_device(
|
||||||
|
service_b.clone(),
|
||||||
|
&sync_b.active_remote_devices().unwrap()[0],
|
||||||
|
stats_b.clone(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let progress = a.store.progress(&episode.episode_id).unwrap().unwrap();
|
||||||
|
assert_eq!(progress.position_ms, 0);
|
||||||
|
assert_eq!(progress.session_id, "restart-on-b");
|
||||||
|
assert!(matches!(
|
||||||
|
checkpoint(a, &session, 1_230_000),
|
||||||
|
furumi_library::podcasts::CheckpointOutcome::Superseded { .. }
|
||||||
|
));
|
||||||
|
|
||||||
|
for server in servers {
|
||||||
|
server.abort();
|
||||||
|
}
|
||||||
|
for (_, _, service, _) in &peers {
|
||||||
|
service.shutdown().await.unwrap();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("localhost podcast sync must complete within 30 seconds");
|
||||||
|
}
|
||||||
|
|
||||||
fn empty_playback_state() -> PlaybackStateWire {
|
fn empty_playback_state() -> PlaybackStateWire {
|
||||||
PlaybackStateWire {
|
PlaybackStateWire {
|
||||||
queue: vec![],
|
queue: vec![],
|
||||||
@@ -229,6 +360,7 @@ fn empty_playback_state() -> PlaybackStateWire {
|
|||||||
volume: 80,
|
volume: 80,
|
||||||
shuffle: false,
|
shuffle: false,
|
||||||
repeat: PlaybackRepeat::Off,
|
repeat: PlaybackRepeat::Off,
|
||||||
|
playback_rate: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,7 +414,7 @@ fn coordination_ignores_legacy_commands_and_wrong_snapshot_sender() {
|
|||||||
coordination: None,
|
coordination: None,
|
||||||
});
|
});
|
||||||
let gossip = sync
|
let gossip = sync
|
||||||
.local_playback_snapshot()
|
.local_playback_snapshot_for(PROTOCOL_VERSION)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.coordination
|
.coordination
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -342,6 +474,7 @@ fn test_sync() -> DeviceSync {
|
|||||||
library: Arc::new(Library::open(&library_path).unwrap()),
|
library: Arc::new(Library::open(&library_path).unwrap()),
|
||||||
event_tx: Arc::new(std::sync::Mutex::new(None)),
|
event_tx: Arc::new(std::sync::Mutex::new(None)),
|
||||||
playback: Arc::new(std::sync::Mutex::new(PlaybackShared::default())),
|
playback: Arc::new(std::sync::Mutex::new(PlaybackShared::default())),
|
||||||
|
podcasts: Default::default(),
|
||||||
};
|
};
|
||||||
sync.ensure_identity().unwrap();
|
sync.ensure_identity().unwrap();
|
||||||
sync
|
sync
|
||||||
@@ -349,6 +482,29 @@ fn test_sync() -> DeviceSync {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn device_identity_has_one_coordinator_across_installation_paths() {
|
fn device_identity_has_one_coordinator_across_installation_paths() {
|
||||||
|
// Other tests spawn processes in parallel. A child can briefly inherit
|
||||||
|
// this test's locked file before exec closes it, delaying release after
|
||||||
|
// drop(first). Keep the lock lifecycle in its own test process.
|
||||||
|
const ISOLATED: &str = "FURUMI_TEST_ISOLATED_IDENTITY_LOCK";
|
||||||
|
if std::env::var_os(ISOLATED).is_none() {
|
||||||
|
let output = std::process::Command::new(std::env::current_exe().unwrap())
|
||||||
|
.args([
|
||||||
|
"--exact",
|
||||||
|
"devices::tests::device_identity_has_one_coordinator_across_installation_paths",
|
||||||
|
"--nocapture",
|
||||||
|
])
|
||||||
|
.env(ISOLATED, "1")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
output.status.success(),
|
||||||
|
"isolated identity lock test failed:\n{}\n{}",
|
||||||
|
String::from_utf8_lossy(&output.stdout),
|
||||||
|
String::from_utf8_lossy(&output.stderr),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let path = dir.path().join("sync.sqlite3");
|
let path = dir.path().join("sync.sqlite3");
|
||||||
let first = acquire_identity_lock(&path).unwrap();
|
let first = acquire_identity_lock(&path).unwrap();
|
||||||
@@ -424,12 +580,25 @@ fn test_fed_track(content_id: &str) -> crate::federation::FedTrack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn base64url_round_trip_without_padding() {
|
fn invites_round_trip_through_the_shared_encoding() {
|
||||||
for input in [b"".as_slice(), b"a", b"ab", b"abc", b"abcdef"] {
|
let invite = InviteWire {
|
||||||
let encoded = base64url_encode(input);
|
v: 1,
|
||||||
assert!(!encoded.contains('='));
|
ticket: "ticket".into(),
|
||||||
assert_eq!(base64url_decode(&encoded).unwrap(), input);
|
device_id: "dev_a".into(),
|
||||||
}
|
invite_id: "inv".into(),
|
||||||
|
secret: "s3cr3t".into(),
|
||||||
|
expires_at_ms: 10,
|
||||||
|
protocol_version: None,
|
||||||
|
};
|
||||||
|
let link = encode_invite(&invite).unwrap();
|
||||||
|
assert!(!link.contains('='));
|
||||||
|
assert_eq!(parse_invite(&link).unwrap(), invite);
|
||||||
|
assert!(
|
||||||
|
parse_invite("frid://x/abc")
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string()
|
||||||
|
.contains(":connect")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -452,6 +621,35 @@ fn tombstone_detection() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn music_track(id: i64, content_id: &str) -> TrackItem {
|
||||||
|
TrackItem {
|
||||||
|
id,
|
||||||
|
title: "Local Song".to_string(),
|
||||||
|
track_number: Some(1),
|
||||||
|
disc_number: Some(1),
|
||||||
|
duration_seconds: 180.0,
|
||||||
|
artists: vec![ArtistRef {
|
||||||
|
id: 1,
|
||||||
|
name: "Local Artist".to_string(),
|
||||||
|
}],
|
||||||
|
featured_artists: Vec::new(),
|
||||||
|
release_id: 2,
|
||||||
|
release_title: "Local Release".to_string(),
|
||||||
|
release_year: Some(2026),
|
||||||
|
file_path: format!("/music/{id}.mp3"),
|
||||||
|
content_id: Some(content_id.to_string()),
|
||||||
|
cover_path: None,
|
||||||
|
audio_format: Some("mp3".to_string()),
|
||||||
|
audio_bitrate: Some(320),
|
||||||
|
audio_sample_rate: Some(44_100),
|
||||||
|
audio_bit_depth: None,
|
||||||
|
file_size_bytes: Some(123_456),
|
||||||
|
play_count: 3,
|
||||||
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn playback_tracks_do_not_sync_device_local_paths() {
|
fn playback_tracks_do_not_sync_device_local_paths() {
|
||||||
let source = TrackItem {
|
let source = TrackItem {
|
||||||
@@ -478,6 +676,7 @@ fn playback_tracks_do_not_sync_device_local_paths() {
|
|||||||
file_size_bytes: Some(123_456),
|
file_size_bytes: Some(123_456),
|
||||||
play_count: 3,
|
play_count: 3,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let wire = PlaybackTrack::from_track(&source);
|
let wire = PlaybackTrack::from_track(&source);
|
||||||
@@ -565,6 +764,7 @@ fn playback_command_is_targeted_and_deduplicated() {
|
|||||||
volume: 42,
|
volume: 42,
|
||||||
shuffle: false,
|
shuffle: false,
|
||||||
repeat: PlaybackRepeat::Off,
|
repeat: PlaybackRepeat::Off,
|
||||||
|
playback_rate: None,
|
||||||
},
|
},
|
||||||
seek: false,
|
seek: false,
|
||||||
};
|
};
|
||||||
@@ -621,6 +821,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
|||||||
volume: 42,
|
volume: 42,
|
||||||
shuffle: false,
|
shuffle: false,
|
||||||
repeat: PlaybackRepeat::Off,
|
repeat: PlaybackRepeat::Off,
|
||||||
|
playback_rate: None,
|
||||||
},
|
},
|
||||||
seek: false,
|
seek: false,
|
||||||
};
|
};
|
||||||
@@ -629,7 +830,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
sync.record_playback_command("dev_other", command).unwrap();
|
sync.record_playback_command("dev_other", command).unwrap();
|
||||||
|
|
||||||
let target_ops = sync.ops_for_peer("dev_target").unwrap();
|
let target_ops = sync.ops_for_peer("dev_target", PROTOCOL_VERSION).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
target_ops
|
target_ops
|
||||||
.iter()
|
.iter()
|
||||||
@@ -638,7 +839,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
|||||||
1
|
1
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
sync.ops_for_peer("dev_unknown")
|
sync.ops_for_peer("dev_unknown", PROTOCOL_VERSION)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
||||||
@@ -653,7 +854,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
sync.ops_for_peer("dev_target")
|
sync.ops_for_peer("dev_target", PROTOCOL_VERSION)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
||||||
@@ -692,6 +893,7 @@ fn newer_device_trust_reactivates_revoked_device() {
|
|||||||
updated_at_ms: 20,
|
updated_at_ms: 20,
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
|
VersionReport::Relayed(PROTOCOL_VERSION),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!device_revoked(&sync, device_id));
|
assert!(!device_revoked(&sync, device_id));
|
||||||
@@ -936,3 +1138,312 @@ fn stale_synced_playlist_item_metadata_repairs_pending_fed_track() {
|
|||||||
assert!(detail.tracks[0].is_fed_pending());
|
assert!(detail.tracks[0].is_fed_pending());
|
||||||
assert_eq!(detail.tracks[0].title, fed.title);
|
assert_eq!(detail.tracks[0].title, fed.title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn podcast_meta() -> music_dht::podcast::PodcastMeta {
|
||||||
|
music_dht::podcast::PodcastMeta {
|
||||||
|
feed_url: music_dht::podcast::FeedUrl::parse("https://example.test/feed.xml").unwrap(),
|
||||||
|
title: "Show".into(),
|
||||||
|
author: None,
|
||||||
|
description: None,
|
||||||
|
link: None,
|
||||||
|
image_url: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rows of the local sync log: (op_id, seq, kind, payload_json, hlc_ms).
|
||||||
|
fn logged_ops(sync: &DeviceSync) -> Vec<(String, i64, String, String, i64)> {
|
||||||
|
let conn = lock(&sync.conn);
|
||||||
|
let mut stmt = conn
|
||||||
|
.prepare("SELECT op_id, seq, kind, payload_json, hlc_ms FROM sync_ops ORDER BY seq")
|
||||||
|
.unwrap();
|
||||||
|
stmt.query_map([], |row| {
|
||||||
|
Ok((
|
||||||
|
row.get(0)?,
|
||||||
|
row.get(1)?,
|
||||||
|
row.get(2)?,
|
||||||
|
row.get(3)?,
|
||||||
|
row.get(4)?,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.collect::<rusqlite::Result<Vec<_>>>()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn podcast_actions_are_appended_to_the_sync_log_with_their_stamps() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let device_id = sync.ensure_identity().unwrap().device_id;
|
||||||
|
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||||
|
sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true)
|
||||||
|
.unwrap();
|
||||||
|
let outcome = sync
|
||||||
|
.run_podcast_action(|stamps| {
|
||||||
|
let outcome = store.subscribe(stamps, &podcast_meta())?;
|
||||||
|
let ops = outcome.ops.clone();
|
||||||
|
Ok((outcome, ops))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let ops = logged_ops(&sync);
|
||||||
|
assert_eq!(ops.len(), 2, "{ops:?}");
|
||||||
|
let (op_id, seq, kind, payload_json, hlc_ms) = &ops[1];
|
||||||
|
assert_eq!(kind, "podcast_subscription_set");
|
||||||
|
assert_eq!(*seq, 2);
|
||||||
|
assert_eq!(*op_id, format!("{device_id}:2"));
|
||||||
|
assert!(*hlc_ms > ops[0].4, "hlc keeps increasing");
|
||||||
|
// The log carries the shared FRID payload, and the store's register
|
||||||
|
// was written with exactly this stamp.
|
||||||
|
let payload: music_dht::device_sync::SyncOpPayload =
|
||||||
|
serde_json::from_str(payload_json).unwrap();
|
||||||
|
assert!(payload.is_podcast());
|
||||||
|
let register = store.subscription(&outcome.podcast_id).unwrap().unwrap();
|
||||||
|
assert_eq!(register.op_id, *op_id);
|
||||||
|
assert_eq!(register.hlc_ms, *hlc_ms);
|
||||||
|
let vector = sync.vector().unwrap();
|
||||||
|
assert_eq!(vector.get(&device_id), Some(&2));
|
||||||
|
|
||||||
|
// Unsubscribing emits two operations with consecutive stamps; a music
|
||||||
|
// operation afterwards continues the same sequence.
|
||||||
|
let unsubscribe = sync
|
||||||
|
.run_podcast_action(|stamps| {
|
||||||
|
let ops = store.unsubscribe(stamps, &outcome.podcast_id)?;
|
||||||
|
Ok((ops.len(), ops))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(unsubscribe, 2);
|
||||||
|
sync.record_content_like(&format!("b3:{}", "b".repeat(64)), true)
|
||||||
|
.unwrap();
|
||||||
|
let ops = logged_ops(&sync);
|
||||||
|
let kinds: Vec<(i64, &str)> = ops
|
||||||
|
.iter()
|
||||||
|
.map(|(_, seq, kind, ..)| (*seq, kind.as_str()))
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
kinds,
|
||||||
|
[
|
||||||
|
(1, "track_like_set"),
|
||||||
|
(2, "podcast_subscription_set"),
|
||||||
|
(3, "podcast_subscription_set"),
|
||||||
|
(4, "podcast_share_set"),
|
||||||
|
(5, "track_like_set"),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// An action that emits nothing leaves the log alone.
|
||||||
|
sync.run_podcast_action(|stamps| {
|
||||||
|
let ops = store.unsubscribe(stamps, &outcome.podcast_id)?;
|
||||||
|
Ok(((), ops))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(logged_ops(&sync).len(), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn podcast_ops_are_sent_only_to_v4_peers() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||||
|
sync.run_podcast_action(|stamps| {
|
||||||
|
let outcome = store.subscribe(stamps, &podcast_meta())?;
|
||||||
|
let ops = outcome.ops.clone();
|
||||||
|
Ok(((), ops))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true)
|
||||||
|
.unwrap();
|
||||||
|
// A v3 peer cannot parse the podcast payload (an unknown `kind` breaks
|
||||||
|
// its whole message), so its batch keeps only the music operation.
|
||||||
|
let ops = sync.ops_for_peer("dev_peer", 3).unwrap();
|
||||||
|
assert_eq!(ops.len(), 1);
|
||||||
|
assert!(matches!(ops[0].payload, SyncOpPayload::TrackLikeSet { .. }));
|
||||||
|
let ops = sync.ops_for_peer("dev_peer", 4).unwrap();
|
||||||
|
assert_eq!(ops.len(), 2);
|
||||||
|
assert!(ops[0].payload.is_podcast());
|
||||||
|
// Unreported versions are the oldest protocol, except that every
|
||||||
|
// `furumi/sync/2` peer keeps listen history.
|
||||||
|
let event = sync
|
||||||
|
.listen_event_for_track(
|
||||||
|
sync.new_listen_id(),
|
||||||
|
&music_track(7, &format!("b3:{}", "b".repeat(64))),
|
||||||
|
1,
|
||||||
|
120_000,
|
||||||
|
music_dht::device_sync::ListenEndReason::Finished,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
sync.record_listen(event).unwrap();
|
||||||
|
let kinds: Vec<_> = sync
|
||||||
|
.ops_for_peer(
|
||||||
|
"dev_peer",
|
||||||
|
music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION,
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.map(|op| payload_kind(&op.payload))
|
||||||
|
.collect();
|
||||||
|
assert_eq!(kinds, ["track_like_set", "listen_recorded"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn failed_podcast_actions_append_nothing() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||||
|
let unknown = music_dht::podcast::PodcastId::from_feed_url(
|
||||||
|
&music_dht::podcast::FeedUrl::parse("https://example.test/other.xml").unwrap(),
|
||||||
|
);
|
||||||
|
let result = sync.run_podcast_action(|stamps| {
|
||||||
|
let ops = store.set_shared(stamps, &unknown, true)?;
|
||||||
|
Ok(((), ops))
|
||||||
|
});
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(logged_ops(&sync).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn podcast_episodes_never_enter_music_listening_history() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let episode = crate::app::podcast_fixtures::episode_track(1);
|
||||||
|
assert!(
|
||||||
|
sync.listen_event_for_track(
|
||||||
|
"listen-1".into(),
|
||||||
|
&episode,
|
||||||
|
1_000,
|
||||||
|
120_000,
|
||||||
|
music_dht::device_sync::ListenEndReason::Finished,
|
||||||
|
)
|
||||||
|
.is_none()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stored_version(sync: &DeviceSync, device_id: &str) -> Option<u16> {
|
||||||
|
stored_protocol_version(&lock(&sync.conn), device_id).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn profile_wire(device_id: &str, protocol_version: u16) -> DeviceProfileWire {
|
||||||
|
DeviceProfileWire {
|
||||||
|
device_id: device_id.to_string(),
|
||||||
|
name: device_id.to_string(),
|
||||||
|
client_version: "0.0.0".to_string(),
|
||||||
|
protocol_version,
|
||||||
|
endpoint_id: String::new(),
|
||||||
|
endpoint_ticket: String::new(),
|
||||||
|
revoked: false,
|
||||||
|
revoke_cutoff_seq: None,
|
||||||
|
updated_at_ms: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn profile_set_op(origin: &str, seq: i64, protocol_version: Option<u16>) -> SyncOpWire {
|
||||||
|
SyncOpWire {
|
||||||
|
op_id: format!("{origin}:{seq}"),
|
||||||
|
origin_device_id: origin.to_string(),
|
||||||
|
seq,
|
||||||
|
hlc_ms: seq,
|
||||||
|
payload: SyncOpPayload::DeviceProfileSet {
|
||||||
|
name: origin.to_string(),
|
||||||
|
client_version: "0.0.0".to_string(),
|
||||||
|
endpoint_ticket: String::new(),
|
||||||
|
endpoint_id: String::new(),
|
||||||
|
protocol_version,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn peer_versions_come_from_the_peer_itself_never_from_this_build() {
|
||||||
|
let sync = test_sync();
|
||||||
|
sync.apply_device_trusted("dev_new", 1).unwrap();
|
||||||
|
sync.apply_device_trusted("dev_old", 1).unwrap();
|
||||||
|
// A device known only from `device_trusted` has reported nothing.
|
||||||
|
assert_eq!(
|
||||||
|
stored_version(&sync, "dev_new"),
|
||||||
|
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Its own profile announcement (v4) is a self-report.
|
||||||
|
sync.apply_op(&profile_set_op("dev_new", 1, Some(4)))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stored_version(&sync, "dev_new"), Some(4));
|
||||||
|
// An announcement of an older client carries no version: this device
|
||||||
|
// keeps what it knows and never substitutes its own constant.
|
||||||
|
sync.apply_op(&profile_set_op("dev_old", 1, None)).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
stored_version(&sync, "dev_old"),
|
||||||
|
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Relays of v1-v3 clients list every device with their own constant:
|
||||||
|
// a relayed value only raises the stored version.
|
||||||
|
sync.apply_device_profiles(
|
||||||
|
&[profile_wire("dev_new", 3), profile_wire("dev_old", 3)],
|
||||||
|
Some("dev_relay"),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stored_version(&sync, "dev_new"), Some(4));
|
||||||
|
assert_eq!(stored_version(&sync, "dev_old"), Some(3));
|
||||||
|
|
||||||
|
// The sender's own entry of a `devices` list is a self-report, so a
|
||||||
|
// downgraded device is noticed.
|
||||||
|
sync.apply_device_profiles(&[profile_wire("dev_new", 3)], Some("dev_new"))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stored_version(&sync, "dev_new"), Some(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn accepted_pairing_keeps_the_version_the_requester_reported() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let insert = |request_id: &str, device_id: &str, version: Option<i64>| {
|
||||||
|
lock(&sync.conn)
|
||||||
|
.execute(
|
||||||
|
"INSERT INTO sync_pending_pairing
|
||||||
|
(request_id, device_id, name, client_version, endpoint_id,
|
||||||
|
endpoint_ticket, invite_id, created_at_ms, status, protocol_version)
|
||||||
|
VALUES (?1, ?2, 'phone', '0.0.0', '', '', 'inv', 1, 'pending', ?3)",
|
||||||
|
params![request_id, device_id, version],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
};
|
||||||
|
insert("req_v3", "dev_v3", Some(3));
|
||||||
|
// A request stored by an older build of this client has no version.
|
||||||
|
insert("req_legacy", "dev_legacy", None);
|
||||||
|
sync.answer_pairing("req_v3", true, false).unwrap();
|
||||||
|
sync.answer_pairing("req_legacy", true, false).unwrap();
|
||||||
|
assert_eq!(stored_version(&sync, "dev_v3"), Some(3));
|
||||||
|
assert_eq!(
|
||||||
|
stored_version(&sync, "dev_legacy"),
|
||||||
|
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_new_protocol_version_is_announced_once_in_the_log() {
|
||||||
|
let sync = test_sync();
|
||||||
|
let device_id = sync.ensure_identity().unwrap().device_id;
|
||||||
|
let announcements = |sync: &DeviceSync| {
|
||||||
|
logged_ops(sync)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|(_, _, kind, ..)| kind == "device_profile_set")
|
||||||
|
.map(|(_, _, _, payload, _)| serde_json::from_str::<SyncOpPayload>(&payload).unwrap())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
// A fresh identity already reports this version in every hello.
|
||||||
|
sync.own_profile("").unwrap();
|
||||||
|
assert!(announcements(&sync).is_empty());
|
||||||
|
// The row written by the previous release.
|
||||||
|
lock(&sync.conn)
|
||||||
|
.execute(
|
||||||
|
"UPDATE sync_devices SET protocol_version = ?2 WHERE device_id = ?1",
|
||||||
|
params![device_id, PROTOCOL_VERSION - 1],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
sync.own_profile("").unwrap();
|
||||||
|
sync.own_profile("").unwrap();
|
||||||
|
let announced = announcements(&sync);
|
||||||
|
assert_eq!(announced.len(), 1, "{announced:?}");
|
||||||
|
assert!(matches!(
|
||||||
|
announced[0],
|
||||||
|
SyncOpPayload::DeviceProfileSet {
|
||||||
|
protocol_version: Some(PROTOCOL_VERSION),
|
||||||
|
..
|
||||||
|
}
|
||||||
|
));
|
||||||
|
assert_eq!(stored_version(&sync, &device_id), Some(PROTOCOL_VERSION));
|
||||||
|
}
|
||||||
@@ -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,
|
CAPABILITIES_PROTOCOL_VERSION, CapabilityManifest, CapabilityMessage, read_message,
|
||||||
write_message,
|
write_message,
|
||||||
};
|
};
|
||||||
|
use music_dht::podcast_dht::PodcastDht;
|
||||||
use music_dht::{ByteStream, EndpointId, MusicDhtService, StreamAcceptor};
|
use music_dht::{ByteStream, EndpointId, MusicDhtService, StreamAcceptor};
|
||||||
|
|
||||||
const PROBE_INTERVAL: Duration = Duration::from_secs(30);
|
const PROBE_INTERVAL: Duration = Duration::from_secs(30);
|
||||||
@@ -30,8 +31,10 @@ pub struct NewerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ProtocolVersions {
|
impl ProtocolVersions {
|
||||||
pub fn snapshot(observed: &ObservedVersions) -> Self {
|
/// `podcast_dht`: whether this node runs the podcast-discovery overlay
|
||||||
let local = local_manifest().protocols;
|
/// (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 observed_versions = lock(&observed.versions).clone();
|
||||||
let newer = observed_versions
|
let newer = observed_versions
|
||||||
.iter()
|
.iter()
|
||||||
@@ -59,27 +62,35 @@ pub struct ObservedVersions {
|
|||||||
peers: Mutex<BTreeMap<String, String>>,
|
peers: Mutex<BTreeMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn local_manifest() -> CapabilityManifest {
|
/// This node's manifest. The podcast-discovery overlay is opt-in in FRID:
|
||||||
CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION"))
|
/// it is advertised only when this node serves it (`podcast_dht`), so
|
||||||
.with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION)
|
/// 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 {
|
while let Some(stream) = acceptor.accept().await {
|
||||||
tokio::spawn(async move {
|
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:#}");
|
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? {
|
let response = match read_message(&mut stream).await? {
|
||||||
CapabilityMessage::Get {
|
CapabilityMessage::Get {
|
||||||
version: CAPABILITIES_PROTOCOL_VERSION,
|
version: CAPABILITIES_PROTOCOL_VERSION,
|
||||||
} => CapabilityMessage::Manifest {
|
} => CapabilityMessage::Manifest {
|
||||||
manifest: local_manifest(),
|
manifest: local_manifest(podcast_dht),
|
||||||
},
|
},
|
||||||
CapabilityMessage::Get { version } => CapabilityMessage::Error {
|
CapabilityMessage::Get { version } => CapabilityMessage::Error {
|
||||||
message: format!("unsupported capability protocol {version}"),
|
message: format!("unsupported capability protocol {version}"),
|
||||||
@@ -94,7 +105,14 @@ async fn serve_one(mut stream: ByteStream) -> Result<()> {
|
|||||||
Ok(())
|
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);
|
let mut interval = tokio::time::interval(PROBE_INTERVAL);
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
@@ -109,7 +127,8 @@ pub async fn probe_loop(service: Arc<MusicDhtService>, observed: Arc<ObservedVer
|
|||||||
)
|
)
|
||||||
.collect::<std::collections::BTreeSet<_>>();
|
.collect::<std::collections::BTreeSet<_>>();
|
||||||
for peer in peers {
|
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:#}");
|
tracing::trace!(%peer, "peer capability probe unavailable: {error:#}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,6 +139,7 @@ async fn probe_peer(
|
|||||||
service: &MusicDhtService,
|
service: &MusicDhtService,
|
||||||
peer: EndpointId,
|
peer: EndpointId,
|
||||||
observed: &ObservedVersions,
|
observed: &ObservedVersions,
|
||||||
|
podcast_dht: Option<&PodcastDht>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut stream = service.open_stream(peer, CAPABILITIES_ALPN).await?;
|
let mut stream = service.open_stream(peer, CAPABILITIES_ALPN).await?;
|
||||||
write_message(
|
write_message(
|
||||||
@@ -137,6 +157,11 @@ async fn probe_peer(
|
|||||||
anyhow::bail!("peer did not return a capability manifest");
|
anyhow::bail!("peer did not return a capability manifest");
|
||||||
};
|
};
|
||||||
manifest.validate()?;
|
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);
|
let mut versions = lock(&observed.versions);
|
||||||
for (id, version) in manifest.protocols {
|
for (id, version) in manifest.protocols {
|
||||||
@@ -162,7 +187,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn local_manifest_lists_every_player_protocol() {
|
fn local_manifest_lists_every_player_protocol() {
|
||||||
let manifest = local_manifest();
|
let manifest = local_manifest(true);
|
||||||
for id in [
|
for id in [
|
||||||
"federation_net",
|
"federation_net",
|
||||||
"ticket",
|
"ticket",
|
||||||
@@ -174,10 +199,31 @@ mod tests {
|
|||||||
"similarity_dht",
|
"similarity_dht",
|
||||||
"device_sync",
|
"device_sync",
|
||||||
"jam",
|
"jam",
|
||||||
|
"podcast_dht",
|
||||||
] {
|
] {
|
||||||
assert!(manifest.protocols.contains_key(id), "missing {id}");
|
assert!(manifest.protocols.contains_key(id), "missing {id}");
|
||||||
}
|
}
|
||||||
manifest.validate().unwrap();
|
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]
|
#[test]
|
||||||
@@ -185,7 +231,7 @@ mod tests {
|
|||||||
let observed = ObservedVersions::default();
|
let observed = ObservedVersions::default();
|
||||||
lock(&observed.versions).insert("music_dht".to_string(), 99);
|
lock(&observed.versions).insert("music_dht".to_string(), 99);
|
||||||
lock(&observed.versions).insert("jam".to_string(), crate::jam::PROTOCOL_VERSION);
|
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.len(), 1);
|
||||||
assert_eq!(snapshot.newer[0].id, "music_dht");
|
assert_eq!(snapshot.newer[0].id, "music_dht");
|
||||||
}
|
}
|
||||||
|
|||||||
+107
-9
@@ -25,6 +25,7 @@ use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht};
|
||||||
use music_dht::similarity_dht::SimilarityDht;
|
use music_dht::similarity_dht::SimilarityDht;
|
||||||
use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN;
|
use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN;
|
||||||
use music_dht::{
|
use music_dht::{
|
||||||
@@ -44,6 +45,10 @@ pub use capabilities::ProtocolVersions;
|
|||||||
pub use catalog::{CATALOG_ALPN, FedAppearsOn, FedArtistCard, FedCardTrack, FedRelease};
|
pub use catalog::{CATALOG_ALPN, FedAppearsOn, FedArtistCard, FedCardTrack, FedRelease};
|
||||||
pub use similarity::SIMILARITY_ALPN;
|
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.
|
/// How often the published library is re-synchronized with the local index.
|
||||||
const SYNC_INTERVAL: Duration = Duration::from_secs(60);
|
const SYNC_INTERVAL: Duration = Duration::from_secs(60);
|
||||||
/// How often the application checks whether the shared transport needs a
|
/// How often the application checks whether the shared transport needs a
|
||||||
@@ -412,6 +417,9 @@ pub struct NetworkLibrarySource {
|
|||||||
struct Running {
|
struct Running {
|
||||||
service: Arc<MusicDhtService>,
|
service: Arc<MusicDhtService>,
|
||||||
similarity_dht: Arc<SimilarityDht>,
|
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_name: String,
|
||||||
network_id: NetworkId,
|
network_id: NetworkId,
|
||||||
tasks: Vec<tokio::task::JoinHandle<()>>,
|
tasks: Vec<tokio::task::JoinHandle<()>>,
|
||||||
@@ -438,6 +446,9 @@ pub struct Federation {
|
|||||||
last_error: std::sync::Mutex<Option<String>>,
|
last_error: std::sync::Mutex<Option<String>>,
|
||||||
transport_stats: Arc<TransportStats>,
|
transport_stats: Arc<TransportStats>,
|
||||||
observed_protocols: Arc<capabilities::ObservedVersions>,
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -572,9 +583,17 @@ impl Federation {
|
|||||||
last_error: std::sync::Mutex::new(initial_error),
|
last_error: std::sync::Mutex::new(initial_error),
|
||||||
transport_stats: Arc::new(TransportStats::default()),
|
transport_stats: Arc::new(TransportStats::default()),
|
||||||
observed_protocols: Arc::new(capabilities::ObservedVersions::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 {
|
pub fn settings(&self) -> FedSettings {
|
||||||
lock(&self.settings).clone()
|
lock(&self.settings).clone()
|
||||||
}
|
}
|
||||||
@@ -730,6 +749,7 @@ impl Federation {
|
|||||||
} else if recovery_only {
|
} else if recovery_only {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
self.podcast_dht.send_replace(None);
|
||||||
stop_running(guard.take()).await;
|
stop_running(guard.take()).await;
|
||||||
} else if recovery_only {
|
} else if recovery_only {
|
||||||
tracing::warn!(network = %network_name, "retrying stopped federation service");
|
tracing::warn!(network = %network_name, "retrying stopped federation service");
|
||||||
@@ -756,6 +776,8 @@ impl Federation {
|
|||||||
.stream_protocol(crate::jam::JAM_ALPN)
|
.stream_protocol(crate::jam::JAM_ALPN)
|
||||||
// Signed LSH summaries form their own upgrade-safe DHT overlay.
|
// Signed LSH summaries form their own upgrade-safe DHT overlay.
|
||||||
.schema_independent_stream_protocol(SIMILARITY_DHT_ALPN)
|
.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.
|
// Informational application/protocol versions.
|
||||||
.schema_independent_stream_protocol(capabilities::CAPABILITIES_ALPN)
|
.schema_independent_stream_protocol(capabilities::CAPABILITIES_ALPN)
|
||||||
.build()
|
.build()
|
||||||
@@ -788,6 +810,18 @@ impl Federation {
|
|||||||
Arc::clone(&self.similarity),
|
Arc::clone(&self.similarity),
|
||||||
Arc::clone(&self.library),
|
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.
|
// Drain DHT events into the log; the channel is bounded.
|
||||||
let event_task = tokio::spawn(async move {
|
let event_task = tokio::spawn(async move {
|
||||||
@@ -861,18 +895,18 @@ impl Federation {
|
|||||||
let capabilities_acceptor = service
|
let capabilities_acceptor = service
|
||||||
.stream_acceptor(capabilities::CAPABILITIES_ALPN)
|
.stream_acceptor(capabilities::CAPABILITIES_ALPN)
|
||||||
.map_err(|err| anyhow::anyhow!("failed to take capabilities acceptor: {err}"))?;
|
.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(
|
let capabilities_probe_task = tokio::spawn(capabilities::probe_loop(
|
||||||
Arc::clone(&service),
|
Arc::clone(&service),
|
||||||
Arc::clone(&self.observed_protocols),
|
Arc::clone(&self.observed_protocols),
|
||||||
|
podcast_dht.clone(),
|
||||||
));
|
));
|
||||||
|
|
||||||
*guard = Some(Running {
|
let mut tasks = podcast_dht_tasks;
|
||||||
service,
|
tasks.extend([
|
||||||
similarity_dht,
|
|
||||||
network_name,
|
|
||||||
network_id,
|
|
||||||
tasks: vec![
|
|
||||||
event_task,
|
event_task,
|
||||||
sync_task,
|
sync_task,
|
||||||
audio_task,
|
audio_task,
|
||||||
@@ -887,7 +921,15 @@ impl Federation {
|
|||||||
similarity_dht_serve_task,
|
similarity_dht_serve_task,
|
||||||
similarity_dht_maintenance_task,
|
similarity_dht_maintenance_task,
|
||||||
similarity_dht_sync_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,
|
||||||
});
|
});
|
||||||
self.set_error(None);
|
self.set_error(None);
|
||||||
Ok(true)
|
Ok(true)
|
||||||
@@ -895,6 +937,7 @@ impl Federation {
|
|||||||
|
|
||||||
async fn stop(&self) {
|
async fn stop(&self) {
|
||||||
let mut guard = self.running.lock().await;
|
let mut guard = self.running.lock().await;
|
||||||
|
self.podcast_dht.send_replace(None);
|
||||||
stop_running(guard.take()).await;
|
stop_running(guard.take()).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1088,7 +1131,12 @@ impl Federation {
|
|||||||
recovery_count: self.recovery_count.load(Ordering::Relaxed),
|
recovery_count: self.recovery_count.load(Ordering::Relaxed),
|
||||||
last_sync: lock(&self.last_sync).clone(),
|
last_sync: lock(&self.last_sync).clone(),
|
||||||
last_error: lock(&self.last_error).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()
|
..FedStatus::default()
|
||||||
};
|
};
|
||||||
if let Some(running) = guard.as_ref() {
|
if let Some(running) = guard.as_ref() {
|
||||||
@@ -1214,6 +1262,10 @@ impl Federation {
|
|||||||
/// Searches the federated network: matching tracks plus the artists a
|
/// Searches the federated network: matching tracks plus the artists a
|
||||||
/// card can be assembled for (from artist records and from the artist
|
/// card can be assembled for (from artist records and from the artist
|
||||||
/// names of matching tracks/releases).
|
/// 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> {
|
pub async fn search(&self, query: &str) -> Result<FedSearchResults> {
|
||||||
let service = self.service().await?;
|
let service = self.service().await?;
|
||||||
let normalized = music_dht::normalize_name(query);
|
let normalized = music_dht::normalize_name(query);
|
||||||
@@ -1309,6 +1361,24 @@ impl Federation {
|
|||||||
Ok(FedSearchResults { artists, tracks })
|
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
|
/// Bounded fan-out to known peers using the exact model/profile
|
||||||
/// fingerprint carried with the query. No DHT records are written.
|
/// fingerprint carried with the query. No DHT records are written.
|
||||||
pub async fn search_similar(
|
pub async fn search_similar(
|
||||||
@@ -2333,6 +2403,7 @@ fn metadata_preview_track(
|
|||||||
.or(base.file_size_bytes),
|
.or(base.file_size_bytes),
|
||||||
play_count: base.play_count,
|
play_count: base.play_count,
|
||||||
fed: Some(fed.clone()),
|
fed: Some(fed.clone()),
|
||||||
|
podcast: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2803,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>) {
|
async fn stop_running(running: Option<Running>) {
|
||||||
let Some(running) = running else { return };
|
let Some(running) = running else { return };
|
||||||
for task in &running.tasks {
|
for task in &running.tasks {
|
||||||
@@ -2948,6 +3044,7 @@ pub fn pending_track(fed: &FedTrack) -> TrackItem {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: Some(fed.clone()),
|
fed: Some(fed.clone()),
|
||||||
|
podcast: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3020,6 +3117,7 @@ fn ephemeral_track(
|
|||||||
// Keep the federation reference: the cached track can still be
|
// Keep the federation reference: the cached track can still be
|
||||||
// liked, re-downloaded and marked as federated in the lists.
|
// liked, re-downloaded and marked as federated in the lists.
|
||||||
fed: Some(fed.clone()),
|
fed: Some(fed.clone()),
|
||||||
|
podcast: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,3 +135,35 @@ fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The podcast-discovery overlay opens next to the other overlay caches; a
|
||||||
|
/// cache that cannot be opened only turns discovery off.
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn podcast_discovery_opens_or_turns_itself_off() {
|
||||||
|
async fn service(dir: &Path) -> Arc<MusicDhtService> {
|
||||||
|
let config = MusicDhtConfig::builder()
|
||||||
|
.data_dir(dir)
|
||||||
|
.network_id(NetworkId::from_name("furumi-tui-podcast-dht-open-test"))
|
||||||
|
.schema_independent_stream_protocol(PODCAST_DHT_ALPN)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let (service, mut events) = MusicDhtService::start(config).await.unwrap();
|
||||||
|
tokio::spawn(async move { while events.recv().await.is_some() {} });
|
||||||
|
Arc::new(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
let ok_dir = tempfile::tempdir().unwrap();
|
||||||
|
let ok = service(ok_dir.path()).await;
|
||||||
|
let opened = open_podcast_dht(&ok, ok_dir.path()).await;
|
||||||
|
assert!(opened.is_some());
|
||||||
|
assert!(ok_dir.path().join(PODCAST_DHT_CACHE_FILE).is_file());
|
||||||
|
drop(opened);
|
||||||
|
ok.shutdown().await.unwrap();
|
||||||
|
|
||||||
|
// A directory where the cache file belongs cannot be opened as SQLite.
|
||||||
|
let broken_dir = tempfile::tempdir().unwrap();
|
||||||
|
std::fs::create_dir(broken_dir.path().join(PODCAST_DHT_CACHE_FILE)).unwrap();
|
||||||
|
let broken = service(broken_dir.path()).await;
|
||||||
|
assert!(open_podcast_dht(&broken, broken_dir.path()).await.is_none());
|
||||||
|
broken.shutdown().await.unwrap();
|
||||||
|
}
|
||||||
+51
-11
@@ -225,10 +225,16 @@ impl JamManager {
|
|||||||
JamStatus::default()
|
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) {
|
pub fn publish_host_playback(&self, snapshot: PlaybackSnapshot) {
|
||||||
let mut state = lock(&self.state);
|
let mut state = lock(&self.state);
|
||||||
if state.host.invite.is_some() {
|
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());
|
acknowledged.push(command.command_id.clone());
|
||||||
if state.host.seen_commands.insert(command.command_id.clone()) {
|
if state.host.seen_commands.insert(command.command_id.clone()) {
|
||||||
state.host.seen_order.push_back(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 {
|
while state.host.seen_order.len() > 4096 {
|
||||||
@@ -656,8 +670,14 @@ mod tests {
|
|||||||
.join(&invite, "dev_guest".into(), "Guest".into())
|
.join(&invite, "dev_guest".into(), "Guest".into())
|
||||||
.unwrap();
|
.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 {
|
let playback_state = crate::devices::PlaybackStateWire {
|
||||||
queue: Vec::new(),
|
queue: vec![crate::devices::PlaybackTrack::podcast_episode(
|
||||||
|
episode.clone(),
|
||||||
|
)],
|
||||||
queue_pos: 0,
|
queue_pos: 0,
|
||||||
playing: false,
|
playing: false,
|
||||||
paused: false,
|
paused: false,
|
||||||
@@ -666,6 +686,7 @@ mod tests {
|
|||||||
volume: 73,
|
volume: 73,
|
||||||
shuffle: false,
|
shuffle: false,
|
||||||
repeat: crate::devices::PlaybackRepeat::Off,
|
repeat: crate::devices::PlaybackRepeat::Off,
|
||||||
|
playback_rate: None,
|
||||||
};
|
};
|
||||||
host.publish_host_playback(PlaybackSnapshot {
|
host.publish_host_playback(PlaybackSnapshot {
|
||||||
coordination: None,
|
coordination: None,
|
||||||
@@ -690,12 +711,27 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(playback.device_id, "dev_host");
|
assert_eq!(playback.device_id, "dev_host");
|
||||||
assert_eq!(playback.state.volume, 73);
|
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
|
guest
|
||||||
.submit_command(PlaybackCommand::SetState {
|
.submit_command(PlaybackCommand::SetState {
|
||||||
state: crate::devices::PlaybackStateWire {
|
state: crate::devices::PlaybackStateWire {
|
||||||
paused: true,
|
paused: true,
|
||||||
..playback_state
|
..playback.state
|
||||||
},
|
},
|
||||||
seek: false,
|
seek: false,
|
||||||
})
|
})
|
||||||
@@ -710,13 +746,17 @@ mod tests {
|
|||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(matches!(
|
let PlaybackCommand::SetState {
|
||||||
command,
|
state: received,
|
||||||
PlaybackCommand::SetState {
|
seek: false,
|
||||||
state: crate::devices::PlaybackStateWire { paused: true, .. },
|
} = command
|
||||||
seek: false
|
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();
|
server.abort();
|
||||||
host_drain.abort();
|
host_drain.abort();
|
||||||
|
|||||||
+4
-1
@@ -7,6 +7,7 @@ mod jam;
|
|||||||
mod library;
|
mod library;
|
||||||
mod media;
|
mod media;
|
||||||
mod player;
|
mod player;
|
||||||
|
mod podcasts;
|
||||||
mod share;
|
mod share;
|
||||||
mod similarity;
|
mod similarity;
|
||||||
mod status;
|
mod status;
|
||||||
@@ -65,7 +66,9 @@ fn main() -> Result<()> {
|
|||||||
// after local guards have been dropped and the terminal has been restored.
|
// after local guards have been dropped and the terminal has been restored.
|
||||||
let _stderr_capture = capture_stderr();
|
let _stderr_capture = capture_stderr();
|
||||||
let (keymap, keymap_warning) = config::keymap::Keymap::load();
|
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
|
// 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
|
// dedicated to the OS media-key event loop — on macOS the system only
|
||||||
|
|||||||
+265
-15
@@ -1,10 +1,12 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
use std::sync::atomic::{AtomicI16, AtomicU32, AtomicU64, AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicI16, AtomicU32, AtomicU64, AtomicUsize, Ordering};
|
||||||
use std::time::Duration;
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use rodio::source::SeekError;
|
use rodio::source::SeekError;
|
||||||
use rodio::{ChannelCount, Sample, SampleRate, Source};
|
use rodio::{ChannelCount, Sample, SampleRate, Source};
|
||||||
|
|
||||||
|
use super::spectrum::{Meter, PCM_SAMPLES, StereoReading};
|
||||||
|
|
||||||
const LEVEL_SCALE: f32 = 1_000_000.0;
|
const LEVEL_SCALE: f32 = 1_000_000.0;
|
||||||
const SCOPE_SAMPLES: usize = 256;
|
const SCOPE_SAMPLES: usize = 256;
|
||||||
const SCOPE_SAMPLE_SCALE: f32 = i16::MAX as f32;
|
const SCOPE_SAMPLE_SCALE: f32 = i16::MAX as f32;
|
||||||
@@ -20,6 +22,26 @@ pub struct AudioAnalysisSnapshot {
|
|||||||
pub treble: f64,
|
pub treble: f64,
|
||||||
pub beat: f64,
|
pub beat: f64,
|
||||||
pub scope: Vec<f64>,
|
pub scope: Vec<f64>,
|
||||||
|
/// Third-octave band levels (see `spectrum::BAND_CENTERS_HZ`), 0.0..1.0
|
||||||
|
/// over `spectrum::FLOOR_DB`..0 dBFS, with analyzer ballistics.
|
||||||
|
pub spectrum: Vec<f64>,
|
||||||
|
/// Per-band peak-hold markers on the same scale as `spectrum`.
|
||||||
|
pub spectrum_peaks: Vec<f64>,
|
||||||
|
/// Left/right RMS levels and held sample peaks on the same dB scale.
|
||||||
|
/// Mono sources report the same value for both channels.
|
||||||
|
pub level_l: f64,
|
||||||
|
pub level_r: f64,
|
||||||
|
pub peak_l: f64,
|
||||||
|
pub peak_r: f64,
|
||||||
|
/// RMS of the side signal (L - R) / 2 on the same scale: the floor for
|
||||||
|
/// mono sources and for stereo files that carry the same signal twice.
|
||||||
|
pub level_side: f64,
|
||||||
|
/// Whether the channels carry different signals: decided on loud windows
|
||||||
|
/// only (side within 30 dB of the louder channel), `None` until then.
|
||||||
|
pub stereo: Option<bool>,
|
||||||
|
/// Sample rate of the analyzed stream in Hz (0 when nothing has played
|
||||||
|
/// since the player last stopped).
|
||||||
|
pub sample_rate: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -32,6 +54,20 @@ pub struct AnalyzerShared {
|
|||||||
beat: AtomicU32,
|
beat: AtomicU32,
|
||||||
scope_write: AtomicUsize,
|
scope_write: AtomicUsize,
|
||||||
scope: Box<[AtomicI16]>,
|
scope: Box<[AtomicI16]>,
|
||||||
|
/// Full-rate mono history for the spectrum; written by the audio thread.
|
||||||
|
pcm_write: AtomicUsize,
|
||||||
|
pcm: Box<[AtomicI16]>,
|
||||||
|
sample_rate: AtomicU32,
|
||||||
|
/// Per-window channel levels (linear, `LEVEL_SCALE`). Peaks accumulate
|
||||||
|
/// with `fetch_max` until the meter consumes them.
|
||||||
|
rms_l: AtomicU32,
|
||||||
|
rms_r: AtomicU32,
|
||||||
|
rms_side: AtomicU32,
|
||||||
|
peak_l: AtomicU32,
|
||||||
|
peak_r: AtomicU32,
|
||||||
|
/// Ballistics and FFT state. Only snapshot readers (the UI side) lock it;
|
||||||
|
/// the audio thread never does.
|
||||||
|
meter: Mutex<Meter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AnalyzerShared {
|
impl Default for AnalyzerShared {
|
||||||
@@ -44,12 +80,25 @@ impl Default for AnalyzerShared {
|
|||||||
treble: AtomicU32::new(0),
|
treble: AtomicU32::new(0),
|
||||||
beat: AtomicU32::new(0),
|
beat: AtomicU32::new(0),
|
||||||
scope_write: AtomicUsize::new(0),
|
scope_write: AtomicUsize::new(0),
|
||||||
scope: (0..SCOPE_SAMPLES)
|
scope: atomic_ring(SCOPE_SAMPLES),
|
||||||
|
pcm_write: AtomicUsize::new(0),
|
||||||
|
pcm: atomic_ring(PCM_SAMPLES),
|
||||||
|
sample_rate: AtomicU32::new(0),
|
||||||
|
rms_l: AtomicU32::new(0),
|
||||||
|
rms_r: AtomicU32::new(0),
|
||||||
|
rms_side: AtomicU32::new(0),
|
||||||
|
peak_l: AtomicU32::new(0),
|
||||||
|
peak_r: AtomicU32::new(0),
|
||||||
|
meter: Mutex::new(Meter::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn atomic_ring(len: usize) -> Box<[AtomicI16]> {
|
||||||
|
(0..len)
|
||||||
.map(|_| AtomicI16::new(0))
|
.map(|_| AtomicI16::new(0))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.into_boxed_slice(),
|
.into_boxed_slice()
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnalyzerShared {
|
impl AnalyzerShared {
|
||||||
@@ -63,10 +112,37 @@ impl AnalyzerShared {
|
|||||||
for sample in self.scope.iter() {
|
for sample in self.scope.iter() {
|
||||||
sample.store(0, Ordering::Relaxed);
|
sample.store(0, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
self.pcm_write.store(0, Ordering::Relaxed);
|
||||||
|
for sample in self.pcm.iter() {
|
||||||
|
sample.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
for level in [
|
||||||
|
&self.rms_l,
|
||||||
|
&self.rms_r,
|
||||||
|
&self.rms_side,
|
||||||
|
&self.peak_l,
|
||||||
|
&self.peak_r,
|
||||||
|
] {
|
||||||
|
level.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
self.sequence.store(0, Ordering::Relaxed);
|
self.sequence.store(0, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Like [`Self::clear`], and also forgets the sample rate: nothing is
|
||||||
|
/// loaded any more.
|
||||||
|
pub fn reset(&self) {
|
||||||
|
self.clear();
|
||||||
|
self.sample_rate.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn snapshot(&self) -> AudioAnalysisSnapshot {
|
pub fn snapshot(&self) -> AudioAnalysisSnapshot {
|
||||||
|
self.snapshot_at(Instant::now())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like [`Self::snapshot`] with an explicit clock, so meter ballistics are
|
||||||
|
/// deterministic in tests. Ballistics are time-based: how often callers
|
||||||
|
/// read does not change how fast bars rise or fall.
|
||||||
|
pub fn snapshot_at(&self, now: Instant) -> AudioAnalysisSnapshot {
|
||||||
let write = self.scope_write.load(Ordering::Relaxed);
|
let write = self.scope_write.load(Ordering::Relaxed);
|
||||||
let len = self.scope.len().max(1);
|
let len = self.scope.len().max(1);
|
||||||
let scope = (0..self.scope.len())
|
let scope = (0..self.scope.len())
|
||||||
@@ -76,6 +152,25 @@ impl AnalyzerShared {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let sample_rate = self.sample_rate.load(Ordering::Relaxed);
|
||||||
|
let pcm_write = self.pcm_write.load(Ordering::Relaxed);
|
||||||
|
let stereo = StereoReading {
|
||||||
|
rms_l: load_norm(&self.rms_l) as f32,
|
||||||
|
rms_r: load_norm(&self.rms_r) as f32,
|
||||||
|
rms_side: load_norm(&self.rms_side) as f32,
|
||||||
|
peak_l: f64::from(self.peak_l.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
|
||||||
|
peak_r: f64::from(self.peak_r.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
|
||||||
|
};
|
||||||
|
let reading = {
|
||||||
|
let mut meter = self
|
||||||
|
.meter
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||||
|
meter.update(now, pcm_write, sample_rate, stereo, |out| {
|
||||||
|
self.copy_pcm(pcm_write, out)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
AudioAnalysisSnapshot {
|
AudioAnalysisSnapshot {
|
||||||
sequence: self.sequence.load(Ordering::Relaxed),
|
sequence: self.sequence.load(Ordering::Relaxed),
|
||||||
energy: load_norm(&self.energy),
|
energy: load_norm(&self.energy),
|
||||||
@@ -84,6 +179,31 @@ impl AnalyzerShared {
|
|||||||
treble: load_norm(&self.treble),
|
treble: load_norm(&self.treble),
|
||||||
beat: load_norm(&self.beat),
|
beat: load_norm(&self.beat),
|
||||||
scope,
|
scope,
|
||||||
|
spectrum: reading.spectrum,
|
||||||
|
spectrum_peaks: reading.spectrum_peaks,
|
||||||
|
level_l: reading.level_l,
|
||||||
|
level_r: reading.level_r,
|
||||||
|
peak_l: reading.peak_l,
|
||||||
|
peak_r: reading.peak_r,
|
||||||
|
level_side: reading.level_side,
|
||||||
|
stereo: reading.stereo,
|
||||||
|
sample_rate,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copies the newest `out.len()` mono samples, oldest first. Slots not
|
||||||
|
/// written since the last clear read as silence.
|
||||||
|
fn copy_pcm(&self, write: usize, out: &mut [f32]) {
|
||||||
|
let len = self.pcm.len();
|
||||||
|
let count = out.len().min(len);
|
||||||
|
let start = write.wrapping_sub(count);
|
||||||
|
for (offset, slot) in out.iter_mut().enumerate() {
|
||||||
|
*slot = if offset < count && write >= count - offset {
|
||||||
|
let index = start.wrapping_add(offset) % len;
|
||||||
|
f32::from(self.pcm[index].load(Ordering::Relaxed)) / SCOPE_SAMPLE_SCALE
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +216,22 @@ impl AnalyzerShared {
|
|||||||
self.sequence.fetch_add(1, Ordering::Relaxed);
|
self.sequence.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn push_pcm(&self, sample: f32) {
|
||||||
|
let index = self.pcm_write.fetch_add(1, Ordering::Relaxed) % self.pcm.len();
|
||||||
|
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
|
||||||
|
self.pcm[index].store(value, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn store_channels(&self, sample_rate: f32, rms: [f32; 3], peak: [f32; 2]) {
|
||||||
|
self.sample_rate
|
||||||
|
.store(sample_rate.round() as u32, Ordering::Relaxed);
|
||||||
|
store_norm(&self.rms_l, rms[0]);
|
||||||
|
store_norm(&self.rms_r, rms[1]);
|
||||||
|
store_norm(&self.rms_side, rms[2]);
|
||||||
|
self.peak_l.fetch_max(norm_bits(peak[0]), Ordering::Relaxed);
|
||||||
|
self.peak_r.fetch_max(norm_bits(peak[1]), Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
fn push_scope(&self, sample: f32) {
|
fn push_scope(&self, sample: f32) {
|
||||||
let index = self.scope_write.fetch_add(1, Ordering::Relaxed) % self.scope.len();
|
let index = self.scope_write.fetch_add(1, Ordering::Relaxed) % self.scope.len();
|
||||||
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
|
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
|
||||||
@@ -181,6 +317,12 @@ struct AnalyzerState {
|
|||||||
mid_alpha: f32,
|
mid_alpha: f32,
|
||||||
channel_index: usize,
|
channel_index: usize,
|
||||||
frame_sum: f32,
|
frame_sum: f32,
|
||||||
|
/// Left/right of the current frame (mono feeds both).
|
||||||
|
frame_lr: [f32; 2],
|
||||||
|
channel_sq: [f32; 2],
|
||||||
|
/// Sum of squared side samples (L - R) / 2 in the current window.
|
||||||
|
side_sq: f32,
|
||||||
|
channel_peak: [f32; 2],
|
||||||
frame_count: usize,
|
frame_count: usize,
|
||||||
window_frames: usize,
|
window_frames: usize,
|
||||||
scope_counter: usize,
|
scope_counter: usize,
|
||||||
@@ -208,6 +350,10 @@ impl AnalyzerState {
|
|||||||
mid_alpha: 0.0,
|
mid_alpha: 0.0,
|
||||||
channel_index: 0,
|
channel_index: 0,
|
||||||
frame_sum: 0.0,
|
frame_sum: 0.0,
|
||||||
|
frame_lr: [0.0; 2],
|
||||||
|
channel_sq: [0.0; 2],
|
||||||
|
side_sq: 0.0,
|
||||||
|
channel_peak: [0.0; 2],
|
||||||
frame_count: 0,
|
frame_count: 0,
|
||||||
window_frames: 0,
|
window_frames: 0,
|
||||||
scope_counter: 0,
|
scope_counter: 0,
|
||||||
@@ -252,6 +398,10 @@ impl AnalyzerState {
|
|||||||
fn reset_filters(&mut self) {
|
fn reset_filters(&mut self) {
|
||||||
self.channel_index = 0;
|
self.channel_index = 0;
|
||||||
self.frame_sum = 0.0;
|
self.frame_sum = 0.0;
|
||||||
|
self.frame_lr = [0.0; 2];
|
||||||
|
self.channel_sq = [0.0; 2];
|
||||||
|
self.side_sq = 0.0;
|
||||||
|
self.channel_peak = [0.0; 2];
|
||||||
self.frame_count = 0;
|
self.frame_count = 0;
|
||||||
self.scope_counter = 0;
|
self.scope_counter = 0;
|
||||||
self.low_bass = 0.0;
|
self.low_bass = 0.0;
|
||||||
@@ -275,10 +425,23 @@ impl AnalyzerState {
|
|||||||
0.0
|
0.0
|
||||||
};
|
};
|
||||||
self.frame_sum += sample;
|
self.frame_sum += sample;
|
||||||
|
// First channel is left, second right; extra channels only feed mono.
|
||||||
|
if self.channel_index < 2 {
|
||||||
|
self.frame_lr[self.channel_index] = sample;
|
||||||
|
}
|
||||||
self.channel_index += 1;
|
self.channel_index += 1;
|
||||||
if self.channel_index < self.channels {
|
if self.channel_index < self.channels {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if self.channels == 1 {
|
||||||
|
self.frame_lr[1] = self.frame_lr[0];
|
||||||
|
}
|
||||||
|
for (channel, value) in self.frame_lr.iter().enumerate() {
|
||||||
|
self.channel_sq[channel] += value * value;
|
||||||
|
self.channel_peak[channel] = self.channel_peak[channel].max(value.abs());
|
||||||
|
}
|
||||||
|
let side = (self.frame_lr[0] - self.frame_lr[1]) * 0.5;
|
||||||
|
self.side_sq += side * side;
|
||||||
|
|
||||||
let mono = self.frame_sum / self.channels as f32;
|
let mono = self.frame_sum / self.channels as f32;
|
||||||
self.channel_index = 0;
|
self.channel_index = 0;
|
||||||
@@ -299,6 +462,7 @@ impl AnalyzerState {
|
|||||||
self.mid_sq += mid * mid;
|
self.mid_sq += mid * mid;
|
||||||
self.treble_sq += treble * treble;
|
self.treble_sq += treble * treble;
|
||||||
self.frame_count += 1;
|
self.frame_count += 1;
|
||||||
|
shared.push_pcm(sample);
|
||||||
|
|
||||||
self.scope_counter += 1;
|
self.scope_counter += 1;
|
||||||
if self.scope_counter >= self.scope_stride {
|
if self.scope_counter >= self.scope_stride {
|
||||||
@@ -339,10 +503,23 @@ impl AnalyzerState {
|
|||||||
self.smooth_beat,
|
self.smooth_beat,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
shared.store_channels(
|
||||||
|
self.sample_rate,
|
||||||
|
[
|
||||||
|
(self.channel_sq[0] / frames).sqrt(),
|
||||||
|
(self.channel_sq[1] / frames).sqrt(),
|
||||||
|
(self.side_sq / frames).sqrt(),
|
||||||
|
],
|
||||||
|
self.channel_peak,
|
||||||
|
);
|
||||||
|
|
||||||
self.full_sq = 0.0;
|
self.full_sq = 0.0;
|
||||||
self.bass_sq = 0.0;
|
self.bass_sq = 0.0;
|
||||||
self.mid_sq = 0.0;
|
self.mid_sq = 0.0;
|
||||||
self.treble_sq = 0.0;
|
self.treble_sq = 0.0;
|
||||||
|
self.channel_sq = [0.0; 2];
|
||||||
|
self.side_sq = 0.0;
|
||||||
|
self.channel_peak = [0.0; 2];
|
||||||
self.frame_count = 0;
|
self.frame_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -361,10 +538,11 @@ fn smooth_level(previous: f32, next: f32, attack: f32, release: f32) -> f32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn store_norm(target: &AtomicU32, value: f32) {
|
fn store_norm(target: &AtomicU32, value: f32) {
|
||||||
target.store(
|
target.store(norm_bits(value), Ordering::Relaxed);
|
||||||
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32,
|
}
|
||||||
Ordering::Relaxed,
|
|
||||||
);
|
fn norm_bits(value: f32) -> u32 {
|
||||||
|
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_norm(source: &AtomicU32) -> f64 {
|
fn load_norm(source: &AtomicU32) -> f64 {
|
||||||
@@ -384,19 +562,24 @@ mod tests {
|
|||||||
|
|
||||||
impl TestSource {
|
impl TestSource {
|
||||||
fn sine(frames: usize, channels: u16, sample_rate: u32, frequency: f32) -> Self {
|
fn sine(frames: usize, channels: u16, sample_rate: u32, frequency: f32) -> Self {
|
||||||
let channels_count = channels.max(1);
|
let gains = vec![1.0; usize::from(channels.max(1))];
|
||||||
let mut samples = Vec::with_capacity(frames * usize::from(channels_count));
|
Self::sine_gains(frames, sample_rate, frequency, &gains)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A sine with a gain per channel (one entry per channel).
|
||||||
|
fn sine_gains(frames: usize, sample_rate: u32, frequency: f32, gains: &[f32]) -> Self {
|
||||||
|
let mut samples = Vec::with_capacity(frames * gains.len());
|
||||||
for frame in 0..frames {
|
for frame in 0..frames {
|
||||||
let time = frame as f32 / sample_rate as f32;
|
let time = frame as f32 / sample_rate as f32;
|
||||||
let sample = (std::f32::consts::TAU * frequency * time).sin() * 0.5;
|
let sample = (std::f32::consts::TAU * frequency * time).sin() * 0.5;
|
||||||
for _ in 0..channels_count {
|
for gain in gains {
|
||||||
samples.push(sample as Sample);
|
samples.push((sample * gain) as Sample);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self {
|
Self {
|
||||||
samples,
|
samples,
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
channels: ChannelCount::new(channels_count).unwrap(),
|
channels: ChannelCount::new(u16::try_from(gains.len()).unwrap()).unwrap(),
|
||||||
sample_rate: SampleRate::new(sample_rate).unwrap(),
|
sample_rate: SampleRate::new(sample_rate).unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -452,4 +635,71 @@ mod tests {
|
|||||||
assert!(snapshot.bass > 0.0);
|
assert!(snapshot.bass > 0.0);
|
||||||
assert!(snapshot.scope.iter().any(|sample| sample.abs() > 0.001));
|
assert!(snapshot.scope.iter().any(|sample| sample.abs() > 0.001));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn analyzed_source_feeds_the_spectrum_and_channel_meters() {
|
||||||
|
let shared = Arc::new(AnalyzerShared::default());
|
||||||
|
let source = TestSource::sine(48_000, 2, 48_000, 1_000.0);
|
||||||
|
let analyzed = AnalyzedSource::new(source, Arc::clone(&shared));
|
||||||
|
|
||||||
|
for _ in analyzed {}
|
||||||
|
|
||||||
|
let snapshot = shared.snapshot();
|
||||||
|
assert_eq!(snapshot.sample_rate, 48_000);
|
||||||
|
let bands = crate::player::BAND_CENTERS_HZ;
|
||||||
|
let at_1k = bands.iter().position(|hz| *hz == 1_000.0).unwrap();
|
||||||
|
let loudest = snapshot
|
||||||
|
.spectrum
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.max_by(|left, right| left.1.total_cmp(right.1))
|
||||||
|
.map(|(index, _)| index);
|
||||||
|
assert_eq!(loudest, Some(at_1k));
|
||||||
|
assert!(snapshot.level_l > 0.5 && (snapshot.level_l - snapshot.level_r).abs() < 1e-6);
|
||||||
|
// the same signal in both channels has no side component
|
||||||
|
assert_eq!(snapshot.level_side, 0.0);
|
||||||
|
assert_eq!(snapshot.stereo, Some(false));
|
||||||
|
assert!(snapshot.peak_l >= snapshot.level_l);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn analyzed_source_measures_the_side_signal() {
|
||||||
|
let measure = |gains: &[f32]| {
|
||||||
|
let shared = Arc::new(AnalyzerShared::default());
|
||||||
|
let source = TestSource::sine_gains(48_000, 48_000, 1_000.0, gains);
|
||||||
|
for _ in AnalyzedSource::new(source, Arc::clone(&shared)) {}
|
||||||
|
shared.snapshot()
|
||||||
|
};
|
||||||
|
// R = -L: the side signal (L - R) / 2 equals L
|
||||||
|
let opposite = measure(&[1.0, -1.0]);
|
||||||
|
assert!(opposite.level_side > 0.5);
|
||||||
|
assert!((opposite.level_side - opposite.level_l).abs() < 1e-3);
|
||||||
|
assert_eq!(opposite.stereo, Some(true));
|
||||||
|
// mono duplicates L into R
|
||||||
|
let mono = measure(&[1.0]);
|
||||||
|
assert_eq!(mono.level_side, 0.0);
|
||||||
|
assert_eq!(mono.stereo, Some(false));
|
||||||
|
// a third channel only feeds the mono sum
|
||||||
|
let surround = measure(&[1.0, 0.0, 1.0]);
|
||||||
|
assert!(surround.level_side > 0.0 && surround.level_side < surround.level_l);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pcm_history_reads_newest_samples_and_silence_before_the_first() {
|
||||||
|
let shared = AnalyzerShared::default();
|
||||||
|
for value in [0.25_f32, 0.5, -0.5] {
|
||||||
|
shared.push_pcm(value);
|
||||||
|
}
|
||||||
|
let mut out = [1.0_f32; 5];
|
||||||
|
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
|
||||||
|
let rounded: Vec<f32> = out
|
||||||
|
.iter()
|
||||||
|
.map(|value| (value * 4.0).round() / 4.0)
|
||||||
|
.collect();
|
||||||
|
assert_eq!(rounded, [0.0, 0.0, 0.25, 0.5, -0.5]);
|
||||||
|
|
||||||
|
shared.clear();
|
||||||
|
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
|
||||||
|
assert!(out.iter().all(|value| *value == 0.0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+325
-44
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
mod analyzer;
|
mod analyzer;
|
||||||
mod opus;
|
mod opus;
|
||||||
|
mod spectrum;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
@@ -15,6 +16,7 @@ use std::time::Duration;
|
|||||||
use rodio::{Decoder, DeviceSinkBuilder, Player, Source, stream::MixerDeviceSink};
|
use rodio::{Decoder, DeviceSinkBuilder, Player, Source, stream::MixerDeviceSink};
|
||||||
|
|
||||||
pub use analyzer::AudioAnalysisSnapshot;
|
pub use analyzer::AudioAnalysisSnapshot;
|
||||||
|
pub use spectrum::{BAND_CENTERS_HZ, FLOOR_DB};
|
||||||
|
|
||||||
pub trait TrackReadSeek: std::io::Read + std::io::Seek + Send + Sync {}
|
pub trait TrackReadSeek: std::io::Read + std::io::Seek + Send + Sync {}
|
||||||
|
|
||||||
@@ -41,6 +43,14 @@ pub enum PlayerEvent {
|
|||||||
has_next: bool,
|
has_next: bool,
|
||||||
},
|
},
|
||||||
Failed(String),
|
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 {
|
enum Command {
|
||||||
@@ -51,6 +61,18 @@ enum Command {
|
|||||||
seekable: bool,
|
seekable: bool,
|
||||||
volume: f32,
|
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
|
/// Append the next track behind the current one without interrupting
|
||||||
/// playback — rodio switches sources back to back (gapless-ish).
|
/// playback — rodio switches sources back to back (gapless-ish).
|
||||||
Enqueue {
|
Enqueue {
|
||||||
@@ -91,6 +113,13 @@ pub struct Controller {
|
|||||||
tx: Sender<Command>,
|
tx: Sender<Command>,
|
||||||
pub shared: Arc<Shared>,
|
pub shared: Arc<Shared>,
|
||||||
playback_allowed: Arc<AtomicBool>,
|
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 {
|
impl Controller {
|
||||||
@@ -102,7 +131,52 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The current source generation: capture it when starting to prepare a
|
||||||
|
/// source in the background and pass it to [`Controller::play_source`].
|
||||||
|
pub fn source_generation(&self) -> u64 {
|
||||||
|
self.generation.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn supersede(&self) {
|
||||||
|
self.generation.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether a start or stop happened since `generation` was read; for
|
||||||
|
/// cancelling background preparation and reads of a superseded source.
|
||||||
|
pub fn superseded_check(&self, generation: u64) -> impl Fn() -> bool + Send + Sync + 'static {
|
||||||
|
let current = Arc::clone(&self.generation);
|
||||||
|
move || current.load(Ordering::SeqCst) != generation
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A source is being prepared in the background and should start
|
||||||
|
/// paused or not. Pause and resume calls made before it is handed over
|
||||||
|
/// update this intent, so a pause pressed while it connects is kept.
|
||||||
|
pub fn expect_source(&self, paused: bool) {
|
||||||
|
self.start_paused.store(paused, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Play a source decoded (and possibly positioned at `start`) outside
|
||||||
|
/// the audio thread, built with [`decode_source_with`]. The audio thread
|
||||||
|
/// drops it when another start or stop happened after `generation` was
|
||||||
|
/// read, so a slow stream never interrupts what the user chose since.
|
||||||
|
pub fn play_source(
|
||||||
|
&self,
|
||||||
|
source: DecodedSource,
|
||||||
|
volume: f32,
|
||||||
|
start: Duration,
|
||||||
|
generation: u64,
|
||||||
|
) {
|
||||||
|
let _ = self.tx.send(Command::PlaySource {
|
||||||
|
source,
|
||||||
|
volume,
|
||||||
|
start,
|
||||||
|
generation,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn play(&self, reader: TrackReader, byte_len: Option<u64>, volume: f32) {
|
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 {
|
let _ = self.tx.send(Command::Play {
|
||||||
reader,
|
reader,
|
||||||
byte_len,
|
byte_len,
|
||||||
@@ -113,6 +187,8 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn play_stream(&self, reader: TrackReader, mime_type: Option<String>, volume: f32) {
|
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 {
|
let _ = self.tx.send(Command::Play {
|
||||||
reader,
|
reader,
|
||||||
byte_len: None,
|
byte_len: None,
|
||||||
@@ -127,14 +203,17 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pause(&self) {
|
pub fn pause(&self) {
|
||||||
|
self.start_paused.store(true, Ordering::SeqCst);
|
||||||
let _ = self.tx.send(Command::Pause);
|
let _ = self.tx.send(Command::Pause);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resume(&self) {
|
pub fn resume(&self) {
|
||||||
|
self.start_paused.store(false, Ordering::SeqCst);
|
||||||
let _ = self.tx.send(Command::Resume);
|
let _ = self.tx.send(Command::Resume);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
|
self.supersede();
|
||||||
let _ = self.tx.send(Command::Stop);
|
let _ = self.tx.send(Command::Stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,31 +232,73 @@ pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Controller {
|
|||||||
let thread_shared = Arc::clone(&shared);
|
let thread_shared = Arc::clone(&shared);
|
||||||
let playback_allowed = Arc::new(AtomicBool::new(true));
|
let playback_allowed = Arc::new(AtomicBool::new(true));
|
||||||
let thread_allowed = Arc::clone(&playback_allowed);
|
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()
|
std::thread::Builder::new()
|
||||||
.name("audio".to_string())
|
.name("audio".to_string())
|
||||||
.spawn(move || run(rx, thread_shared, thread_allowed, on_event))
|
.spawn(move || {
|
||||||
|
run(
|
||||||
|
rx,
|
||||||
|
thread_shared,
|
||||||
|
thread_allowed,
|
||||||
|
Generations {
|
||||||
|
current: thread_generation,
|
||||||
|
start_paused: thread_start_paused,
|
||||||
|
},
|
||||||
|
on_event,
|
||||||
|
)
|
||||||
|
})
|
||||||
.expect("spawning the audio thread cannot fail");
|
.expect("spawning the audio thread cannot fail");
|
||||||
Controller {
|
Controller {
|
||||||
tx,
|
tx,
|
||||||
shared,
|
shared,
|
||||||
playback_allowed,
|
playback_allowed,
|
||||||
|
generation,
|
||||||
|
start_paused,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Controller state the audio thread reads.
|
||||||
|
struct Generations {
|
||||||
|
current: Arc<AtomicU64>,
|
||||||
|
start_paused: Arc<AtomicBool>,
|
||||||
|
}
|
||||||
|
|
||||||
struct Output {
|
struct Output {
|
||||||
/// Keeps the audio device open; dropping it stops the mixer.
|
/// Keeps the audio device open; dropping it stops the mixer.
|
||||||
_device: MixerDeviceSink,
|
_device: MixerDeviceSink,
|
||||||
player: Player,
|
player: Player,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Audio-thread state besides the output device.
|
||||||
|
struct Playing {
|
||||||
|
track_loaded: bool,
|
||||||
|
/// Position of the current source when it was appended: sources
|
||||||
|
/// positioned before playback (resumed podcast episodes) start there,
|
||||||
|
/// while rodio counts from zero until the next seek.
|
||||||
|
base: Duration,
|
||||||
|
/// Controller generation the current source was started under. When
|
||||||
|
/// the controller moved on, the end of this source is not reported: a
|
||||||
|
/// stop or another start is already on its way.
|
||||||
|
generation: u64,
|
||||||
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
rx: Receiver<Command>,
|
rx: Receiver<Command>,
|
||||||
shared: Arc<Shared>,
|
shared: Arc<Shared>,
|
||||||
playback_allowed: Arc<AtomicBool>,
|
playback_allowed: Arc<AtomicBool>,
|
||||||
|
generations: Generations,
|
||||||
on_event: impl Fn(PlayerEvent),
|
on_event: impl Fn(PlayerEvent),
|
||||||
) {
|
) {
|
||||||
|
let generation = &generations.current;
|
||||||
let mut output: Option<Output> = None;
|
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;
|
let mut last_len = 0usize;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -186,33 +307,70 @@ fn run(
|
|||||||
if !playback_allowed.load(Ordering::Acquire)
|
if !playback_allowed.load(Ordering::Acquire)
|
||||||
&& matches!(
|
&& matches!(
|
||||||
command,
|
command,
|
||||||
Command::Play { .. } | Command::Enqueue { .. } | Command::Resume
|
Command::Play { .. }
|
||||||
|
| Command::PlaySource { .. }
|
||||||
|
| Command::Enqueue { .. }
|
||||||
|
| Command::Resume
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
continue;
|
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
|
// Commands change the source queue legitimately; resync the
|
||||||
// length so the next tick doesn't read it as a track ending.
|
// 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());
|
last_len = output.as_ref().map_or(0, |out| out.player.len());
|
||||||
}
|
}
|
||||||
Err(RecvTimeoutError::Timeout) => {
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
if let Some(out) = &output {
|
if let Some(out) = &output {
|
||||||
|
let position = out.player.get_pos() + playing.base;
|
||||||
shared
|
shared
|
||||||
.position_ms
|
.position_ms
|
||||||
.store(out.player.get_pos().as_millis() as u64, Ordering::Relaxed);
|
.store(position.as_millis() as u64, Ordering::Relaxed);
|
||||||
shared
|
shared
|
||||||
.paused
|
.paused
|
||||||
.store(out.player.is_paused(), Ordering::Relaxed);
|
.store(out.player.is_paused(), Ordering::Relaxed);
|
||||||
let len = out.player.len();
|
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 {
|
if len == 0 {
|
||||||
track_loaded = false;
|
playing.track_loaded = false;
|
||||||
}
|
}
|
||||||
|
// A source that ended because it was superseded
|
||||||
|
// (its stream cancelled) did not finish.
|
||||||
|
if generation.load(Ordering::SeqCst) == playing.generation {
|
||||||
for _ in len..last_len {
|
for _ in len..last_len {
|
||||||
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
last_len = len;
|
last_len = len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,8 +382,9 @@ fn run(
|
|||||||
fn handle(
|
fn handle(
|
||||||
command: Command,
|
command: Command,
|
||||||
shared: &Arc<Shared>,
|
shared: &Arc<Shared>,
|
||||||
|
start_paused: &AtomicBool,
|
||||||
output: &mut Option<Output>,
|
output: &mut Option<Output>,
|
||||||
track_loaded: &mut bool,
|
playing: &mut Playing,
|
||||||
on_event: &impl Fn(PlayerEvent),
|
on_event: &impl Fn(PlayerEvent),
|
||||||
) {
|
) {
|
||||||
match command {
|
match command {
|
||||||
@@ -236,42 +395,35 @@ fn handle(
|
|||||||
seekable,
|
seekable,
|
||||||
volume,
|
volume,
|
||||||
} => {
|
} => {
|
||||||
// The device is opened lazily on first playback so the app works
|
let Some(out) = ensure_output(output, on_event) else {
|
||||||
// on machines with no audio output until you actually press play.
|
return;
|
||||||
if output.is_none() {
|
};
|
||||||
match DeviceSinkBuilder::open_default_sink() {
|
match decode_source(reader, byte_len, mime_type.as_deref(), seekable) {
|
||||||
Ok(device) => {
|
Ok(source) => start_source(out, source, volume, false, shared, on_event),
|
||||||
let player = Player::connect_new(device.mixer());
|
|
||||||
*output = Some(Output {
|
|
||||||
_device: device,
|
|
||||||
player,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
on_event(PlayerEvent::Failed(format!("no audio device: {err}")));
|
on_event(PlayerEvent::Failed(format!("cannot decode track: {err}")));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
playing.track_loaded = true;
|
||||||
|
playing.base = Duration::ZERO;
|
||||||
}
|
}
|
||||||
let out = output.as_ref().expect("output opened above");
|
Command::PlaySource {
|
||||||
|
|
||||||
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,
|
source,
|
||||||
Arc::clone(&shared.analysis),
|
volume,
|
||||||
));
|
start,
|
||||||
out.player.play();
|
generation: _,
|
||||||
*track_loaded = true;
|
} => {
|
||||||
on_event(PlayerEvent::Started);
|
let Some(out) = ensure_output(output, on_event) else {
|
||||||
}
|
return;
|
||||||
Err(err) => {
|
};
|
||||||
on_event(PlayerEvent::Failed(format!("cannot decode track: {err}")));
|
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 } => {
|
Command::Enqueue { reader, byte_len } => {
|
||||||
let Some(out) = output.as_ref() else {
|
let Some(out) = output.as_ref() else {
|
||||||
@@ -303,16 +455,26 @@ fn handle(
|
|||||||
if let Some(out) = output.take() {
|
if let Some(out) = output.take() {
|
||||||
out.player.stop();
|
out.player.stop();
|
||||||
}
|
}
|
||||||
shared.analysis.clear();
|
shared.analysis.reset();
|
||||||
*track_loaded = false;
|
playing.track_loaded = false;
|
||||||
|
playing.base = Duration::ZERO;
|
||||||
}
|
}
|
||||||
Command::Seek(position) => {
|
Command::Seek(position) => {
|
||||||
if let Some(out) = output
|
let result = match output {
|
||||||
&& let Err(err) = out.player.try_seek(position)
|
Some(out) => out.player.try_seek(position).map_err(|err| err.to_string()),
|
||||||
{
|
None => Err("nothing is playing".to_string()),
|
||||||
tracing::warn!(%err, "seek failed");
|
};
|
||||||
|
match &result {
|
||||||
|
// rodio now reports absolute positions.
|
||||||
|
Ok(()) => playing.base = Duration::ZERO,
|
||||||
|
Err(err) => tracing::warn!(%err, "seek failed"),
|
||||||
}
|
}
|
||||||
shared.analysis.clear();
|
shared.analysis.clear();
|
||||||
|
on_event(PlayerEvent::Seeked {
|
||||||
|
generation: playing.generation,
|
||||||
|
position,
|
||||||
|
result,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Command::SetVolume(volume) => {
|
Command::SetVolume(volume) => {
|
||||||
if let Some(out) = output {
|
if let Some(out) = output {
|
||||||
@@ -322,13 +484,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) type DecodedSource = Box<dyn Source + Send>;
|
||||||
|
|
||||||
pub(crate) fn decode_source(
|
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,
|
mut reader: TrackReader,
|
||||||
byte_len: Option<u64>,
|
byte_len: Option<u64>,
|
||||||
mime_type: Option<&str>,
|
mime_type: Option<&str>,
|
||||||
seekable: bool,
|
seekable: bool,
|
||||||
|
coarse_seek: bool,
|
||||||
) -> Result<DecodedSource, String> {
|
) -> Result<DecodedSource, String> {
|
||||||
let mime_is_opus = mime_type.is_some_and(|mime| {
|
let mime_is_opus = mime_type.is_some_and(|mime| {
|
||||||
let mime = mime.to_ascii_lowercase();
|
let mime = mime.to_ascii_lowercase();
|
||||||
@@ -344,6 +567,7 @@ pub(crate) fn decode_source(
|
|||||||
let mut builder = Decoder::builder()
|
let mut builder = Decoder::builder()
|
||||||
.with_data(reader)
|
.with_data(reader)
|
||||||
.with_seekable(seekable)
|
.with_seekable(seekable)
|
||||||
|
.with_coarse_seek(coarse_seek)
|
||||||
.with_gapless(true);
|
.with_gapless(true);
|
||||||
if let Some(len) = byte_len {
|
if let Some(len) = byte_len {
|
||||||
builder = builder.with_byte_len(len);
|
builder = builder.with_byte_len(len);
|
||||||
@@ -356,3 +580,60 @@ pub(crate) fn decode_source(
|
|||||||
.map(|decoder| Box::new(decoder) as DecodedSource)
|
.map(|decoder| Box::new(decoder) as DecodedSource)
|
||||||
.map_err(|error| error.to_string())
|
.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,650 @@
|
|||||||
|
//! Display meter for visualizations: a third-octave spectrum and stereo levels
|
||||||
|
//! with analyzer ballistics (fast attack, steady fall, peak hold).
|
||||||
|
//!
|
||||||
|
//! The audio thread only writes samples and per-window channel levels into
|
||||||
|
//! `AnalyzerShared`; the FFT and all smoothing run here, on the reader side,
|
||||||
|
//! when a snapshot is taken. Ballistics are time-based: rises and falls
|
||||||
|
//! advance with the clock, not with the number of snapshots.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use rustfft::num_complex::Complex;
|
||||||
|
use rustfft::{Fft, FftPlanner};
|
||||||
|
|
||||||
|
/// Mono samples kept for the spectrum: enough for an ~85 ms window at
|
||||||
|
/// 192 kHz. The FFT uses the newest `fft_len(rate)` of them.
|
||||||
|
pub const PCM_SAMPLES: usize = 16_384;
|
||||||
|
|
||||||
|
/// Bottom of the display scale: values are normalized from `FLOOR_DB`..0 dBFS
|
||||||
|
/// to 0.0..1.0, so a full-scale sine reads 1.0.
|
||||||
|
pub const FLOOR_DB: f32 = -60.0;
|
||||||
|
|
||||||
|
/// Nominal ISO third-octave band centres (Hz), 20 Hz .. 20 kHz. These are
|
||||||
|
/// labels; band edges come from the exact base-2 centres (see `exact_center`)
|
||||||
|
/// so neighbouring bands meet without gaps or overlaps.
|
||||||
|
pub const BAND_CENTERS_HZ: [f32; 31] = [
|
||||||
|
20.0, 25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0, 400.0,
|
||||||
|
500.0, 630.0, 800.0, 1_000.0, 1_250.0, 1_600.0, 2_000.0, 2_500.0, 3_150.0, 4_000.0, 5_000.0,
|
||||||
|
6_300.0, 8_000.0, 10_000.0, 12_500.0, 16_000.0, 20_000.0,
|
||||||
|
];
|
||||||
|
|
||||||
|
const BANDS: usize = BAND_CENTERS_HZ.len();
|
||||||
|
/// Index of the 1 kHz band.
|
||||||
|
const KHZ_BAND: i32 = 17;
|
||||||
|
const MIN_FFT: usize = 4096;
|
||||||
|
const BAND_ATTACK_SECS: f32 = 0.025;
|
||||||
|
const BAND_FALL_DB_PER_SEC: f32 = 36.0;
|
||||||
|
const PEAK_HOLD_SECS: f32 = 1.2;
|
||||||
|
const PEAK_FALL_DB_PER_SEC: f32 = 18.0;
|
||||||
|
const LEVEL_ATTACK_SECS: f32 = 0.05;
|
||||||
|
const LEVEL_RELEASE_SECS: f32 = 0.3;
|
||||||
|
/// A window counts as stereo when its side signal (L - R) / 2 is no more
|
||||||
|
/// than this far below the louder channel: lossy coding of a mono recording
|
||||||
|
/// leaves a side signal far below that, real stereo mixes sit well above it.
|
||||||
|
const STEREO_SIDE_DB: f32 = -30.0;
|
||||||
|
/// Only windows this far above the floor decide; quieter ones keep the last
|
||||||
|
/// decision, since their side signal is lost in the floor.
|
||||||
|
const STEREO_DECIDE_ABOVE_FLOOR_DB: f32 = 30.0;
|
||||||
|
|
||||||
|
/// Longest step applied at once; a stalled reader must not fast-forward a
|
||||||
|
/// whole second of decay in one frame.
|
||||||
|
const MAX_STEP_SECS: f32 = 0.25;
|
||||||
|
/// Audio arrives in device-callback bursts (rodio uses ~46 ms buffers), so a
|
||||||
|
/// snapshot may see no new samples while playback runs. Only a longer stall
|
||||||
|
/// (pause, stop) counts as silence.
|
||||||
|
const STALL_SECS: f32 = 0.15;
|
||||||
|
|
||||||
|
/// FFT length for a sample rate: ~85 ms of audio, a power of two.
|
||||||
|
fn fft_len(sample_rate: u32) -> usize {
|
||||||
|
(sample_rate as usize * MIN_FFT / 48_000)
|
||||||
|
.next_power_of_two()
|
||||||
|
.clamp(MIN_FFT, PCM_SAMPLES)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exact base-2 third-octave centre of band `index` (1 kHz at index 17).
|
||||||
|
fn exact_center(index: usize) -> f32 {
|
||||||
|
1_000.0 * 2f32.powf((index as i32 - KHZ_BAND) as f32 / 3.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Linear channel levels of the latest analysis window.
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
pub struct StereoReading {
|
||||||
|
pub rms_l: f32,
|
||||||
|
pub rms_r: f32,
|
||||||
|
/// RMS of the side signal (L - R) / 2.
|
||||||
|
pub rms_side: f32,
|
||||||
|
/// Highest sample peaks since the previous reading.
|
||||||
|
pub peak_l: f32,
|
||||||
|
pub peak_r: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct MeterReading {
|
||||||
|
pub spectrum: Vec<f64>,
|
||||||
|
pub spectrum_peaks: Vec<f64>,
|
||||||
|
pub level_l: f64,
|
||||||
|
pub level_r: f64,
|
||||||
|
pub peak_l: f64,
|
||||||
|
pub peak_r: f64,
|
||||||
|
pub level_side: f64,
|
||||||
|
/// Whether the two channels carry different signals (see
|
||||||
|
/// `STEREO_SIDE_DB`); `None` until a loud enough window was measured.
|
||||||
|
pub stereo: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Hold {
|
||||||
|
db: f32,
|
||||||
|
age: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hold {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
db: FLOOR_DB,
|
||||||
|
age: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, value_db: f32, dt: f32) {
|
||||||
|
if value_db >= self.db {
|
||||||
|
self.db = value_db;
|
||||||
|
self.age = 0.0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.age += dt;
|
||||||
|
if self.age > PEAK_HOLD_SECS {
|
||||||
|
self.db = (self.db - PEAK_FALL_DB_PER_SEC * dt).max(value_db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// FFT plan, window and band bin ranges for one sample rate.
|
||||||
|
struct Analysis {
|
||||||
|
sample_rate: u32,
|
||||||
|
fft: Arc<dyn Fft<f32>>,
|
||||||
|
window: Vec<f32>,
|
||||||
|
/// Sum of squared window weights, for power normalization.
|
||||||
|
window_power: f32,
|
||||||
|
pcm: Vec<f32>,
|
||||||
|
buffer: Vec<Complex<f32>>,
|
||||||
|
scratch: Vec<Complex<f32>>,
|
||||||
|
/// Inclusive bin range per band; `None` above Nyquist.
|
||||||
|
bins: [Option<(usize, usize)>; BANDS],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Analysis {
|
||||||
|
fn new(sample_rate: u32) -> Self {
|
||||||
|
let len = fft_len(sample_rate);
|
||||||
|
let fft = FftPlanner::<f32>::new().plan_fft_forward(len);
|
||||||
|
let scratch = vec![Complex::default(); fft.get_inplace_scratch_len()];
|
||||||
|
let window: Vec<f32> = (0..len)
|
||||||
|
.map(|index| {
|
||||||
|
let phase = std::f32::consts::TAU * index as f32 / len as f32;
|
||||||
|
0.5 - 0.5 * phase.cos()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let window_power = window.iter().map(|weight| weight * weight).sum();
|
||||||
|
Self {
|
||||||
|
sample_rate,
|
||||||
|
fft,
|
||||||
|
window,
|
||||||
|
window_power,
|
||||||
|
pcm: vec![0.0; len],
|
||||||
|
buffer: vec![Complex::default(); len],
|
||||||
|
scratch,
|
||||||
|
bins: band_bins(sample_rate, len),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Band power in dBFS (a full-scale sine reads 0 dB in its band). The
|
||||||
|
/// caller has filled `pcm`.
|
||||||
|
fn band_targets(&mut self) -> [f32; BANDS] {
|
||||||
|
for ((slot, sample), weight) in self.buffer.iter_mut().zip(&self.pcm).zip(&self.window) {
|
||||||
|
*slot = Complex::new(sample * weight, 0.0);
|
||||||
|
}
|
||||||
|
self.fft
|
||||||
|
.process_with_scratch(&mut self.buffer, &mut self.scratch);
|
||||||
|
// One-sided power per bin, normalized so the bins of a sine sum to
|
||||||
|
// its mean square (A^2 / 2); doubled below so the sine reads 0 dB.
|
||||||
|
let scale = 2.0 / (self.buffer.len() as f32 * self.window_power);
|
||||||
|
let mut out = [FLOOR_DB; BANDS];
|
||||||
|
for (slot, range) in out.iter_mut().zip(self.bins) {
|
||||||
|
if let Some((first, last)) = range {
|
||||||
|
let power: f32 = self.buffer[first..=last]
|
||||||
|
.iter()
|
||||||
|
.map(|bin| bin.norm_sqr() * scale)
|
||||||
|
.sum();
|
||||||
|
*slot = to_db_power(2.0 * power);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Partitions the FFT bins between the bands: each bin belongs to exactly one
|
||||||
|
/// band, from the first bin at or above the band's lower edge up to the bin
|
||||||
|
/// before the next band starts. A band narrower than one bin (deep bass at
|
||||||
|
/// short windows) reads the bin that contains its centre, which it then shares
|
||||||
|
/// with its neighbour.
|
||||||
|
fn band_bins(sample_rate: u32, len: usize) -> [Option<(usize, usize)>; BANDS] {
|
||||||
|
let bin_hz = sample_rate as f32 / len as f32;
|
||||||
|
let last_bin = len / 2 - 1;
|
||||||
|
let nyquist = sample_rate as f32 / 2.0;
|
||||||
|
let edge = 2f32.powf(1.0 / 6.0);
|
||||||
|
let first_bin = |index: usize| ((exact_center(index) / edge / bin_hz).ceil() as usize).max(1);
|
||||||
|
std::array::from_fn(|index| {
|
||||||
|
let low = exact_center(index) / edge;
|
||||||
|
if low >= nyquist {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let first = first_bin(index);
|
||||||
|
let last = if index + 1 < BANDS {
|
||||||
|
first_bin(index + 1).saturating_sub(1)
|
||||||
|
} else {
|
||||||
|
((exact_center(index) * edge / bin_hz).floor() as usize).max(first)
|
||||||
|
}
|
||||||
|
.min(last_bin);
|
||||||
|
if first <= last {
|
||||||
|
Some((first, last))
|
||||||
|
} else {
|
||||||
|
let center = ((exact_center(index) / bin_hz).round() as usize).clamp(1, last_bin);
|
||||||
|
Some((center, center))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Meter {
|
||||||
|
analysis: Option<Analysis>,
|
||||||
|
bands_db: [f32; BANDS],
|
||||||
|
band_peaks: [Hold; BANDS],
|
||||||
|
/// Left, right and side RMS levels.
|
||||||
|
level_db: [f32; 3],
|
||||||
|
level_peaks: [Hold; 2],
|
||||||
|
last_update: Option<Instant>,
|
||||||
|
last_write: usize,
|
||||||
|
/// When the PCM counter last moved, and what that audio measured; reused
|
||||||
|
/// between device callbacks until `STALL_SECS` pass.
|
||||||
|
last_fresh: Option<Instant>,
|
||||||
|
held_targets: [f32; BANDS],
|
||||||
|
held_rms: [f32; 3],
|
||||||
|
/// Last stereo decision; kept through silence, pauses and stalls.
|
||||||
|
stereo: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for Meter {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Meter")
|
||||||
|
.field("bands_db", &self.bands_db)
|
||||||
|
.field("level_db", &self.level_db)
|
||||||
|
.finish_non_exhaustive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Meter {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
analysis: None,
|
||||||
|
bands_db: [FLOOR_DB; BANDS],
|
||||||
|
band_peaks: std::array::from_fn(|_| Hold::new()),
|
||||||
|
level_db: [FLOOR_DB; 3],
|
||||||
|
level_peaks: std::array::from_fn(|_| Hold::new()),
|
||||||
|
last_update: None,
|
||||||
|
last_write: 0,
|
||||||
|
last_fresh: None,
|
||||||
|
held_targets: [FLOOR_DB; BANDS],
|
||||||
|
held_rms: [0.0; 3],
|
||||||
|
stereo: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Meter {
|
||||||
|
/// Advances the meter to `now`. `write` is the analyzer's PCM write
|
||||||
|
/// counter: while it keeps moving the newest audio is measured; once it
|
||||||
|
/// has not moved for `STALL_SECS` (paused, stopped) the meter is fed
|
||||||
|
/// silence and falls instead of freezing. `fill_pcm` copies the newest
|
||||||
|
/// `out.len()` mono samples, oldest first.
|
||||||
|
pub fn update(
|
||||||
|
&mut self,
|
||||||
|
now: Instant,
|
||||||
|
write: usize,
|
||||||
|
sample_rate: u32,
|
||||||
|
stereo: StereoReading,
|
||||||
|
fill_pcm: impl FnOnce(&mut [f32]),
|
||||||
|
) -> MeterReading {
|
||||||
|
let dt = self
|
||||||
|
.last_update
|
||||||
|
.map(|last| now.saturating_duration_since(last).as_secs_f32())
|
||||||
|
.unwrap_or(MAX_STEP_SECS)
|
||||||
|
.min(MAX_STEP_SECS);
|
||||||
|
self.last_update = Some(now);
|
||||||
|
let fresh = write != self.last_write;
|
||||||
|
self.last_write = write;
|
||||||
|
|
||||||
|
let (targets, rms, peaks) = if fresh && sample_rate > 0 {
|
||||||
|
if self
|
||||||
|
.analysis
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|analysis| analysis.sample_rate != sample_rate)
|
||||||
|
{
|
||||||
|
self.analysis = Some(Analysis::new(sample_rate));
|
||||||
|
}
|
||||||
|
let analysis = self.analysis.as_mut().expect("analysis was just set");
|
||||||
|
fill_pcm(&mut analysis.pcm);
|
||||||
|
self.held_targets = analysis.band_targets();
|
||||||
|
self.held_rms = [stereo.rms_l, stereo.rms_r, stereo.rms_side];
|
||||||
|
// decided on the raw window, before ballistics: smoothed levels
|
||||||
|
// converge on the floor together and would read as stereo
|
||||||
|
let loud = stereo.rms_l.max(stereo.rms_r);
|
||||||
|
if to_db(loud) > FLOOR_DB + STEREO_DECIDE_ABOVE_FLOOR_DB {
|
||||||
|
self.stereo = Some(to_db(stereo.rms_side) > to_db(loud) + STEREO_SIDE_DB);
|
||||||
|
}
|
||||||
|
self.last_fresh = Some(now);
|
||||||
|
(
|
||||||
|
self.held_targets,
|
||||||
|
self.held_rms,
|
||||||
|
[stereo.peak_l, stereo.peak_r],
|
||||||
|
)
|
||||||
|
} else if self
|
||||||
|
.last_fresh
|
||||||
|
.is_some_and(|last| now.saturating_duration_since(last).as_secs_f32() <= STALL_SECS)
|
||||||
|
{
|
||||||
|
(self.held_targets, self.held_rms, [0.0; 2])
|
||||||
|
} else {
|
||||||
|
([FLOOR_DB; BANDS], [0.0; 3], [0.0; 2])
|
||||||
|
};
|
||||||
|
|
||||||
|
for ((current, target), peak) in self
|
||||||
|
.bands_db
|
||||||
|
.iter_mut()
|
||||||
|
.zip(targets)
|
||||||
|
.zip(self.band_peaks.iter_mut())
|
||||||
|
{
|
||||||
|
*current = follow_band(*current, target, dt);
|
||||||
|
peak.update(*current, dt);
|
||||||
|
}
|
||||||
|
for (level, value) in self.level_db.iter_mut().zip(rms) {
|
||||||
|
*level = follow_level(*level, to_db(value), dt);
|
||||||
|
}
|
||||||
|
for (hold, value) in self.level_peaks.iter_mut().zip(peaks) {
|
||||||
|
hold.update(to_db(value), dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
MeterReading {
|
||||||
|
spectrum: self.bands_db.iter().map(|db| normalize(*db)).collect(),
|
||||||
|
spectrum_peaks: self
|
||||||
|
.band_peaks
|
||||||
|
.iter()
|
||||||
|
.map(|hold| normalize(hold.db))
|
||||||
|
.collect(),
|
||||||
|
level_l: normalize(self.level_db[0]),
|
||||||
|
level_r: normalize(self.level_db[1]),
|
||||||
|
peak_l: normalize(self.level_peaks[0].db),
|
||||||
|
peak_r: normalize(self.level_peaks[1].db),
|
||||||
|
level_side: normalize(self.level_db[2]),
|
||||||
|
stereo: self.stereo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn follow_band(current: f32, target: f32, dt: f32) -> f32 {
|
||||||
|
if target > current {
|
||||||
|
current + (target - current) * (1.0 - (-dt / BAND_ATTACK_SECS).exp())
|
||||||
|
} else {
|
||||||
|
(current - BAND_FALL_DB_PER_SEC * dt).max(target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn follow_level(current: f32, target: f32, dt: f32) -> f32 {
|
||||||
|
let tau = if target > current {
|
||||||
|
LEVEL_ATTACK_SECS
|
||||||
|
} else {
|
||||||
|
LEVEL_RELEASE_SECS
|
||||||
|
};
|
||||||
|
current + (target - current) * (1.0 - (-dt / tau).exp())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_db(amplitude: f32) -> f32 {
|
||||||
|
if amplitude <= 0.0 || !amplitude.is_finite() {
|
||||||
|
FLOOR_DB
|
||||||
|
} else {
|
||||||
|
(20.0 * amplitude.log10()).max(FLOOR_DB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_db_power(power: f32) -> f32 {
|
||||||
|
if power <= 0.0 || !power.is_finite() {
|
||||||
|
FLOOR_DB
|
||||||
|
} else {
|
||||||
|
(10.0 * power.log10()).max(FLOOR_DB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize(db: f32) -> f64 {
|
||||||
|
f64::from(((db - FLOOR_DB) / -FLOOR_DB).clamp(0.0, 1.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn sine(frequency: f32, amplitude: f32, sample_rate: u32, len: usize) -> Vec<f32> {
|
||||||
|
(0..len)
|
||||||
|
.map(|index| {
|
||||||
|
let time = index as f32 / sample_rate as f32;
|
||||||
|
amplitude * (std::f32::consts::TAU * frequency * time).sin()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn band(center: f32) -> usize {
|
||||||
|
BAND_CENTERS_HZ
|
||||||
|
.iter()
|
||||||
|
.position(|value| *value == center)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Feeds a steady tone for `steps` snapshots 50 ms apart.
|
||||||
|
fn settle(meter: &mut Meter, frequency: f32, sample_rate: u32, steps: u64) -> MeterReading {
|
||||||
|
let samples = sine(frequency, 0.5, sample_rate, PCM_SAMPLES);
|
||||||
|
let start = Instant::now();
|
||||||
|
let mut reading = MeterReading::default();
|
||||||
|
for step in 1..=steps {
|
||||||
|
reading = meter.update(
|
||||||
|
start + Duration::from_millis(50 * step),
|
||||||
|
step as usize,
|
||||||
|
sample_rate,
|
||||||
|
StereoReading::default(),
|
||||||
|
|out| out.copy_from_slice(&samples[..out.len()]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
reading
|
||||||
|
}
|
||||||
|
|
||||||
|
fn band_powers_db(frequency: f32, sample_rate: u32) -> [f32; BANDS] {
|
||||||
|
let mut analysis = Analysis::new(sample_rate);
|
||||||
|
let len = analysis.pcm.len();
|
||||||
|
analysis
|
||||||
|
.pcm
|
||||||
|
.copy_from_slice(&sine(frequency, 1.0, sample_rate, len));
|
||||||
|
analysis.band_targets()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_sine_lights_its_own_third_octave_band() {
|
||||||
|
let reading = settle(&mut Meter::default(), 1_000.0, 48_000, 10);
|
||||||
|
let at_1k = reading.spectrum[band(1_000.0)];
|
||||||
|
// -6 dBFS on a 60 dB scale reads 0.9.
|
||||||
|
assert!((at_1k - 0.9).abs() < 0.03, "1 kHz band read {at_1k}");
|
||||||
|
assert!(reading.spectrum[band(250.0)] < 0.2);
|
||||||
|
assert!(reading.spectrum[band(4_000.0)] < 0.2);
|
||||||
|
assert!(reading.spectrum_peaks[band(1_000.0)] >= at_1k);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bands_cover_the_axis_without_gaps_or_double_counting() {
|
||||||
|
for sample_rate in [44_100, 48_000, 96_000] {
|
||||||
|
// Up to just below the top band's upper edge (~22.4 kHz) or
|
||||||
|
// Nyquist; 1/24-octave steps also land right on band edges.
|
||||||
|
let top = (sample_rate as f32 / 2.0 * 0.85).min(exact_center(BANDS - 1) * 1.1);
|
||||||
|
let mut frequency = 80.0_f32;
|
||||||
|
while frequency < top {
|
||||||
|
let powers = band_powers_db(frequency, sample_rate);
|
||||||
|
let loudest = powers.iter().copied().fold(FLOOR_DB, f32::max);
|
||||||
|
let total: f32 = powers
|
||||||
|
.iter()
|
||||||
|
.filter(|db| **db > FLOOR_DB)
|
||||||
|
.map(|db| 10f32.powf(db / 10.0))
|
||||||
|
.sum();
|
||||||
|
assert!(
|
||||||
|
(-3.5..=0.5).contains(&loudest),
|
||||||
|
"{frequency} Hz at {sample_rate}: loudest band {loudest} dB"
|
||||||
|
);
|
||||||
|
let total_db = 10.0 * total.log10();
|
||||||
|
assert!(
|
||||||
|
total_db.abs() < 0.5,
|
||||||
|
"{frequency} Hz at {sample_rate}: bands sum to {total_db} dB"
|
||||||
|
);
|
||||||
|
frequency *= 2f32.powf(1.0 / 24.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deep_bass_reads_at_every_sample_rate() {
|
||||||
|
for sample_rate in [44_100, 48_000, 96_000, 192_000] {
|
||||||
|
let powers = band_powers_db(31.5, sample_rate);
|
||||||
|
assert!(
|
||||||
|
powers[band(31.5)] > -3.0,
|
||||||
|
"31.5 Hz at {sample_rate}: {} dB",
|
||||||
|
powers[band(31.5)]
|
||||||
|
);
|
||||||
|
assert!(powers[band(1_000.0)] < -40.0);
|
||||||
|
}
|
||||||
|
// Bands above Nyquist stay at the floor.
|
||||||
|
assert_eq!(band_powers_db(1_000.0, 32_000)[band(20_000.0)], FLOOR_DB);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn callback_gaps_keep_the_meter_steady_but_a_stall_falls() {
|
||||||
|
let mut meter = Meter::default();
|
||||||
|
let samples = sine(1_000.0, 0.5, 48_000, PCM_SAMPLES);
|
||||||
|
let tone = StereoReading {
|
||||||
|
rms_l: 0.35,
|
||||||
|
rms_r: 0.35,
|
||||||
|
rms_side: 0.0,
|
||||||
|
peak_l: 0.5,
|
||||||
|
peak_r: 0.5,
|
||||||
|
};
|
||||||
|
let start = Instant::now();
|
||||||
|
let mut write = 0;
|
||||||
|
let mut steady = MeterReading::default();
|
||||||
|
for step in 1..=40u64 {
|
||||||
|
// Every third snapshot lands between two device callbacks.
|
||||||
|
if step % 3 != 0 {
|
||||||
|
write += 1;
|
||||||
|
}
|
||||||
|
let reading = meter.update(
|
||||||
|
start + Duration::from_millis(50 * step),
|
||||||
|
write,
|
||||||
|
48_000,
|
||||||
|
tone,
|
||||||
|
|out| out.copy_from_slice(&samples[..out.len()]),
|
||||||
|
);
|
||||||
|
if step > 20 {
|
||||||
|
assert!(
|
||||||
|
(reading.level_l - steady.level_l).abs() < 0.02 || step == 21,
|
||||||
|
"level jumped at step {step}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
(reading.spectrum[band(1_000.0)] - 0.9).abs() < 0.03,
|
||||||
|
"band jumped at step {step}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
steady = reading;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The counter stops for longer than a callback gap: paused.
|
||||||
|
let index = band(1_000.0);
|
||||||
|
let paused_at = start + Duration::from_millis(2_000);
|
||||||
|
let first = meter.update(paused_at, write, 48_000, tone, |_| {});
|
||||||
|
let later = meter.update(
|
||||||
|
paused_at + Duration::from_millis(500),
|
||||||
|
write,
|
||||||
|
48_000,
|
||||||
|
tone,
|
||||||
|
|_| {},
|
||||||
|
);
|
||||||
|
assert!(later.spectrum[index] < first.spectrum[index]);
|
||||||
|
assert!(later.level_l < first.level_l);
|
||||||
|
// The peak marker holds for 1.2 s before it falls too.
|
||||||
|
assert!((later.spectrum_peaks[index] - 0.9).abs() < 0.03);
|
||||||
|
let mut reading = later;
|
||||||
|
for step in 1..=20u64 {
|
||||||
|
reading = meter.update(
|
||||||
|
paused_at + Duration::from_millis(500 + 250 * step),
|
||||||
|
write,
|
||||||
|
48_000,
|
||||||
|
tone,
|
||||||
|
|_| {},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(reading.spectrum[index], 0.0);
|
||||||
|
assert!(reading.spectrum_peaks[index] < 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn channel_levels_follow_rms_and_hold_peaks() {
|
||||||
|
let mut meter = Meter::default();
|
||||||
|
let start = Instant::now();
|
||||||
|
let loud_left = StereoReading {
|
||||||
|
rms_l: 0.5,
|
||||||
|
rms_r: 0.05,
|
||||||
|
rms_side: 0.2,
|
||||||
|
peak_l: 0.9,
|
||||||
|
peak_r: 0.1,
|
||||||
|
};
|
||||||
|
let mut reading = MeterReading::default();
|
||||||
|
for step in 1..=20 {
|
||||||
|
reading = meter.update(
|
||||||
|
start + Duration::from_millis(50 * step),
|
||||||
|
step as usize,
|
||||||
|
48_000,
|
||||||
|
loud_left,
|
||||||
|
|out| out.fill(0.0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(reading.level_l > reading.level_r);
|
||||||
|
assert!((reading.level_l - normalize(to_db(0.5))).abs() < 0.02);
|
||||||
|
assert!((reading.peak_l - normalize(to_db(0.9))).abs() < 0.01);
|
||||||
|
assert!(reading.peak_l >= reading.level_l);
|
||||||
|
assert!((reading.level_side - normalize(to_db(0.2))).abs() < 0.02);
|
||||||
|
assert_eq!(reading.stereo, Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stereo_is_decided_on_loud_windows_and_kept_through_silence() {
|
||||||
|
let mut meter = Meter::default();
|
||||||
|
let start = Instant::now();
|
||||||
|
let mut read = |step: u64, stereo: StereoReading| {
|
||||||
|
meter.update(
|
||||||
|
start + Duration::from_millis(50 * step),
|
||||||
|
step as usize,
|
||||||
|
48_000,
|
||||||
|
stereo,
|
||||||
|
|out| out.fill(0.0),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let quiet = StereoReading {
|
||||||
|
rms_l: 0.0005,
|
||||||
|
rms_r: 0.0001,
|
||||||
|
rms_side: 0.0002,
|
||||||
|
..StereoReading::default()
|
||||||
|
};
|
||||||
|
assert_eq!(read(1, quiet).stereo, None);
|
||||||
|
let near_mono = StereoReading {
|
||||||
|
rms_l: 0.3,
|
||||||
|
rms_r: 0.3,
|
||||||
|
rms_side: 0.003,
|
||||||
|
..StereoReading::default()
|
||||||
|
};
|
||||||
|
assert_eq!(read(2, near_mono).stereo, Some(false));
|
||||||
|
// silence and then a stall keep the decision
|
||||||
|
assert_eq!(read(3, StereoReading::default()).stereo, Some(false));
|
||||||
|
let wide = StereoReading {
|
||||||
|
rms_side: 0.1,
|
||||||
|
..near_mono
|
||||||
|
};
|
||||||
|
assert_eq!(read(4, wide).stereo, Some(true));
|
||||||
|
let later = meter.update(
|
||||||
|
start + Duration::from_secs(5),
|
||||||
|
4,
|
||||||
|
48_000,
|
||||||
|
StereoReading::default(),
|
||||||
|
|out| out.fill(0.0),
|
||||||
|
);
|
||||||
|
assert_eq!(later.stereo, Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn silence_and_unknown_rate_read_the_floor() {
|
||||||
|
let mut meter = Meter::default();
|
||||||
|
let reading = meter.update(Instant::now(), 1, 0, StereoReading::default(), |_| {});
|
||||||
|
assert!(reading.spectrum.iter().all(|value| *value == 0.0));
|
||||||
|
assert_eq!(reading.level_l, 0.0);
|
||||||
|
assert_eq!(reading.level_side, 0.0);
|
||||||
|
assert_eq!(reading.spectrum.len(), BAND_CENTERS_HZ.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fft_length_tracks_the_sample_rate() {
|
||||||
|
assert_eq!(fft_len(44_100), 4096);
|
||||||
|
assert_eq!(fft_len(48_000), 4096);
|
||||||
|
assert_eq!(fft_len(96_000), 8192);
|
||||||
|
assert_eq!(fft_len(192_000), 16_384);
|
||||||
|
assert_eq!(fft_len(0), 4096);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
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> {
|
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||||
|
if track.is_podcast() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
track
|
track
|
||||||
.fed
|
.fed
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -14,6 +19,9 @@ pub fn track_can_share(track: &TrackItem) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn track_share_link(track: &TrackItem) -> Option<String> {
|
pub fn track_share_link(track: &TrackItem) -> Option<String> {
|
||||||
|
if track.is_podcast() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let content_id = track_content_id(track).or_else(|| {
|
let content_id = track_content_id(track).or_else(|| {
|
||||||
(!track.file_path.trim().is_empty())
|
(!track.file_path.trim().is_empty())
|
||||||
.then(|| crate::library::audio_content_id(&track.file_path))
|
.then(|| crate::library::audio_content_id(&track.file_path))
|
||||||
@@ -154,6 +162,7 @@ mod tests {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -231,6 +240,7 @@ mod tests {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
};
|
};
|
||||||
let link = track_share_link(&track).expect("link");
|
let link = track_share_link(&track).expect("link");
|
||||||
let parsed = parse_frid_link(&link).expect("parses back");
|
let parsed = parse_frid_link(&link).expect("parses back");
|
||||||
|
|||||||
+63
-17
@@ -947,12 +947,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
|
|||||||
let empty_results = SearchResults::default();
|
let empty_results = SearchResults::default();
|
||||||
let results = match &search.results {
|
let results = match &search.results {
|
||||||
Some(results) => results,
|
Some(results) => results,
|
||||||
None if !state.search.fed_tracks.is_empty()
|
None if state.search.has_federated() => &empty_results,
|
||||||
|| !state.search.fed_artists.is_empty()
|
|
||||||
|| state.search.fed_loading =>
|
|
||||||
{
|
|
||||||
&empty_results
|
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
let hint = if search.query.is_empty() {
|
let hint = if search.query.is_empty() {
|
||||||
"type to search artists, releases and tracks"
|
"type to search artists, releases and tracks"
|
||||||
@@ -967,11 +962,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
|
|||||||
return centered_line(frame, inner, line);
|
return centered_line(frame, inner, line);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if results.is_empty()
|
if results.is_empty() && !state.search.has_federated() {
|
||||||
&& state.search.fed_tracks.is_empty()
|
|
||||||
&& state.search.fed_artists.is_empty()
|
|
||||||
&& !state.search.fed_loading
|
|
||||||
{
|
|
||||||
return centered_line(frame, inner, Line::styled("nothing found", theme::dim()));
|
return centered_line(frame, inner, Line::styled("nothing found", theme::dim()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1037,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
|
// Federated section: artists whose card can be assembled, then tracks
|
||||||
// (both marked with the owning peers).
|
// (both marked with the owning peers), then public podcast cards.
|
||||||
if !state.search.fed_tracks.is_empty()
|
if state.search.has_federated() {
|
||||||
|| !state.search.fed_artists.is_empty()
|
|
||||||
|| state.search.fed_loading
|
|
||||||
{
|
|
||||||
if !rows.is_empty() {
|
if !rows.is_empty() {
|
||||||
rows.push((Line::default(), None, None));
|
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…"
|
"Federation · searching…"
|
||||||
} else {
|
} else {
|
||||||
"Federation"
|
"Federation"
|
||||||
@@ -1100,6 +1088,23 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
|
|||||||
));
|
));
|
||||||
index += 1;
|
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
|
// Absolute cursor indices covered by an active Shift-V range in the
|
||||||
@@ -1145,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) {
|
fn draw_similarity_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
|
||||||
let search = &state.search;
|
let search = &state.search;
|
||||||
let status = if search.fed_loading {
|
let status = if search.fed_loading {
|
||||||
|
|||||||
+546
-6
@@ -4,6 +4,8 @@ mod federation;
|
|||||||
pub(crate) mod global;
|
pub(crate) mod global;
|
||||||
mod logs;
|
mod logs;
|
||||||
mod playlists;
|
mod playlists;
|
||||||
|
mod podcast_card;
|
||||||
|
mod podcasts;
|
||||||
mod popup;
|
mod popup;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod status_tests;
|
mod status_tests;
|
||||||
@@ -50,6 +52,510 @@ mod tests {
|
|||||||
assert_eq!(label, "●");
|
assert_eq!(label, "●");
|
||||||
assert_eq!(style, client_style);
|
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) {
|
pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
|
||||||
@@ -73,6 +579,7 @@ pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
|
|||||||
Tab::Global => global::draw(frame, main_area, state),
|
Tab::Global => global::draw(frame, main_area, state),
|
||||||
Tab::Playlists => playlists::draw(frame, main_area, state),
|
Tab::Playlists => playlists::draw(frame, main_area, state),
|
||||||
Tab::Queue => draw_queue(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::Federation => federation::draw(frame, main_area, state),
|
||||||
Tab::Logs => logs::draw(frame, main_area, state),
|
Tab::Logs => logs::draw(frame, main_area, state),
|
||||||
}
|
}
|
||||||
@@ -140,15 +647,48 @@ fn is_waiting_message(message: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
|
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||||
let titles = Tab::ALL
|
frame.render_widget(tab_bar(state, area.width), area);
|
||||||
.iter()
|
}
|
||||||
.map(|tab| format!(" {} {} ", tab.index() + 1, tab.title()));
|
|
||||||
let tabs = Tabs::new(titles)
|
/// The widest tab bar that fits `width`. Narrow terminals drop the outer
|
||||||
.select(state.active_tab.index())
|
/// 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())
|
.style(theme::dim())
|
||||||
.highlight_style(theme::tab_active_for(state))
|
.highlight_style(theme::tab_active_for(state))
|
||||||
.divider("");
|
.divider("");
|
||||||
frame.render_widget(tabs, area);
|
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
|
/// One track row used by every track list: ♥ marker for liked tracks, the
|
||||||
|
|||||||
@@ -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()),
|
}) => draw_edit(frame, state, title, fields, *focus, error.as_deref()),
|
||||||
Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label),
|
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 { .. }) => {
|
Some(Popup::SimilarityPrivacyConsent { .. }) => {
|
||||||
draw_similarity_privacy_consent(frame, state)
|
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),
|
}) => draw_track_artists(frame, state, tracks, *cursor, *selected),
|
||||||
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
|
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
|
||||||
Some(Popup::FedInput { field, input }) => {
|
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::FedText { title, text }) => draw_fed_text(frame, state, title, text),
|
||||||
Some(Popup::FedCopyText {
|
Some(Popup::FedCopyText {
|
||||||
@@ -891,6 +912,7 @@ fn draw_fed_input(
|
|||||||
state: &AppState,
|
state: &AppState,
|
||||||
title: &str,
|
title: &str,
|
||||||
help: &str,
|
help: &str,
|
||||||
|
keys: &str,
|
||||||
input: &crate::app::input::LineEdit,
|
input: &crate::app::input::LineEdit,
|
||||||
) {
|
) {
|
||||||
let area = centered(frame.area(), 72, 8);
|
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)));
|
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::from(spans)), entry_area);
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(Line::styled("enter: apply · esc: cancel", theme::dim()))
|
Paragraph::new(Line::styled(keys.to_string(), theme::dim())).alignment(Alignment::Center),
|
||||||
.alignment(Alignment::Center),
|
|
||||||
hint_area,
|
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) {
|
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 width = 90.min(frame.area().width.saturating_sub(4)).max(40);
|
||||||
let height = 18.min(frame.area().height.saturating_sub(2)).max(7);
|
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);
|
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))])
|
let [rect] = Layout::horizontal([Constraint::Length(width.min(area.width))])
|
||||||
.flex(Flex::Center)
|
.flex(Flex::Center)
|
||||||
.areas(area);
|
.areas(area);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ fn playing_state() -> AppState {
|
|||||||
file_size_bytes: None,
|
file_size_bytes: None,
|
||||||
play_count: 0,
|
play_count: 0,
|
||||||
fed: None,
|
fed: None,
|
||||||
|
podcast: None,
|
||||||
});
|
});
|
||||||
state.player.playing = true;
|
state.player.playing = true;
|
||||||
state.player.position_secs = 12.0;
|
state.player.position_secs = 12.0;
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ use semver::Version;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
const REPOSITORY: &str = "https://api.github.com/repos/house-of-vanity/furumi_tui";
|
const REPOSITORY: &str = "https://api.github.com/repos/Furumi-stack/furumi-tui";
|
||||||
const MAX_ARCHIVE: u64 = 512 * 1024 * 1024;
|
const MAX_ARCHIVE: u64 = 512 * 1024 * 1024;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
+980
-25
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user