forked from Furumi/furumi-tui
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0da9862cf3 |
No files matched your search
@@ -61,19 +61,13 @@ update it only when dependencies change.
|
||||
- `src/player/` owns rodio playback and audio analysis.
|
||||
- `src/federation/` owns DHT discovery, peer catalogs, audio exchange, and
|
||||
federation caches.
|
||||
- `src/podcasts/` owns podcast feed HTTP and refresh policy, the episode
|
||||
stream reader (HTTP Range, bounded buffer) and the ordered progress writer;
|
||||
podcast identity, merge rules and the SQLite store come from FRID
|
||||
(`music_dht::podcast`, `furumi_library::podcasts`) and must not be
|
||||
re-derived here. Queue items are identified by `state::track_key` /
|
||||
`TrackItem::key`, never by numeric id (every episode has the same sentinel
|
||||
id); music-only code must skip `TrackItem::is_podcast()` items.
|
||||
- `src/devices.rs` owns trusted-device replication, membership, and playback
|
||||
coordination. Its wire types are the shared `music_dht::device_sync`
|
||||
definitions; do not add local copies (golden tests in
|
||||
`devices/wire_compat_tests.rs` keep the v3 JSON).
|
||||
coordination.
|
||||
- `src/ui/` renders `AppState`; rendering code must not start background work
|
||||
or mutate persistence.
|
||||
- `src/podcasts/` owns RSS parsing, podcast persistence, feed fetching and
|
||||
the HTTP range-request stream reader; `src/app/podcast_runtime.rs`
|
||||
schedules its work and folds results into `AppState`.
|
||||
- `src/media.rs` owns OS media controls.
|
||||
- `src/visualizer.rs` and `src/visualizations/` own the Rhai visualization
|
||||
host and bundled scripts.
|
||||
@@ -164,25 +158,7 @@ When changing it:
|
||||
- keep snapshots able to repair peers that missed older operations;
|
||||
- treat membership and revocation as replicated durable state;
|
||||
- keep playback commands targeted and deduplicated;
|
||||
- test merge behavior from at least two operation orders;
|
||||
- never store this build's protocol version for another device: use the
|
||||
device's own report (`profile_protocol_version`) and only raise a stored
|
||||
version from relayed profiles (`relayed_protocol_version`);
|
||||
- gate everything a peer cannot parse by the version it reported: podcast
|
||||
operations, podcast snapshot sections and playback state with podcast items
|
||||
go only to v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`,
|
||||
`playback_allowed_for_peer`), a podcast queue is never handed to an older
|
||||
device and its playback commands are ignored while the queue has episodes;
|
||||
never strip episodes from a queue to make it fit an old peer; an older
|
||||
peer still gets the playback coordination, with the queue redacted
|
||||
(`redact_for_jam`), so its engine does not fail over, and the
|
||||
"Private item" music entries it echoes back in its own states are left
|
||||
out (`drop_echoed_private_items`), never adopted;
|
||||
- apply podcast operations and snapshot sections only through
|
||||
`PodcastStore` (`apply_remote_op`, `apply_snapshot`); never call the
|
||||
podcast store while holding the sync-log connection;
|
||||
- keep Jam snapshots redacted (`redact_for_jam`) and map guest placeholders
|
||||
back with `restore_jam_private_items`.
|
||||
- test merge behavior from at least two operation orders.
|
||||
|
||||
Do not replace eventual reconciliation with assumptions about a continuously
|
||||
connected leader.
|
||||
@@ -213,11 +189,6 @@ The TUI is keyboard-first and cross-platform.
|
||||
- Use semantic `Action` values rather than checking raw keys inside views.
|
||||
- Add default bindings in `src/config/default_keymap.toml`.
|
||||
- Preserve user overrides and context-specific bindings.
|
||||
- Tabs are ordered Library, Playlists, Queue, Podcasts, Settings, Logs
|
||||
(`1`–`6`). `Tab::ALL` defines that order for the tab bar, Tab/Shift+Tab,
|
||||
and `GoToTab` indices. Changing it, or the meaning of any other
|
||||
positional action argument, requires a new `KEYMAP_VERSION` and an
|
||||
in-memory migration of older keymap files; never rewrite the user's file.
|
||||
- Account for narrow terminals, Unicode display width, and empty/loading/error
|
||||
states.
|
||||
- Restore raw mode, alternate screen, bracketed paste, and keyboard
|
||||
@@ -248,9 +219,7 @@ Add tests at the closest stable boundary:
|
||||
- pure state transitions in `app/update_tests.rs`;
|
||||
- SQLite behavior and migrations in `library/tests.rs`;
|
||||
- operation merge, tombstone, membership, and playback sync behavior in
|
||||
`devices/tests.rs`; podcast sync between device stores and simulated v3
|
||||
peers in `devices/podcast_sync_tests.rs`; wire shapes in
|
||||
`devices/wire_compat_tests.rs`;
|
||||
`devices/tests.rs`;
|
||||
- search/ranking/federation conversion in `federation/tests.rs`;
|
||||
- protocol-specific tests beside `federation/audio.rs` or
|
||||
`federation/catalog.rs` when appropriate.
|
||||
|
||||
+27
-166
@@ -66,27 +66,12 @@ This layer connects devices owned or trusted by the same user. It carries
|
||||
personal state such as:
|
||||
|
||||
- likes and playlist operations;
|
||||
- podcast subscriptions, episode progress and listening sessions;
|
||||
- device membership and revocation;
|
||||
- acknowledgements and synchronization progress;
|
||||
- playback state, commands, and handoff information.
|
||||
|
||||
Pairing establishes the trust relationship. After that, changes are replicated
|
||||
through an append-only operation log and applied to materialized local tables.
|
||||
The wire types are the shared `music_dht::device_sync` definitions (protocol
|
||||
v4); golden tests keep the v3 JSON of furumi-tui 0.3.x byte for byte.
|
||||
|
||||
Each device stores the protocol version that the other devices report
|
||||
themselves (their hello and pairing profiles, the sender's own entry of a
|
||||
`devices` list, their own profile announcements). A relayed profile only
|
||||
raises a stored version, because older relays list every device with their
|
||||
own constant; this build never stores its own version for another device.
|
||||
Everything a peer cannot parse is gated by that version: podcast operations,
|
||||
podcast snapshot sections and playback state that contains podcast items
|
||||
reach only v4 peers (`op_allowed_for_peer`, `SyncSnapshot::for_peer`,
|
||||
`playback_allowed_for_peer`). An older client would reject a whole message
|
||||
over one unknown operation kind, or silently drop an episode it does not
|
||||
understand.
|
||||
|
||||
### Music federation
|
||||
|
||||
@@ -112,12 +97,8 @@ audio. Other TUI peers use a dedicated Jam ALPN to submit the same portable
|
||||
playback commands used by connected-device control and receive the host's
|
||||
playback snapshot. They receive queue metadata, not audio.
|
||||
|
||||
Jam never exchanges trusted membership, likes, playlists, listening history
|
||||
or podcast state. The host's podcast episodes are private: the published
|
||||
snapshot replaces them with opaque placeholders (`redact_for_jam`), guest
|
||||
commands that add episodes are dropped, and a guest's placeholders are mapped
|
||||
back to the host's episodes (`restore_jam_private_items`); a change that
|
||||
drops, duplicates or seeks in them is refused. Volume remains local. Commands carry unique IDs and are retried until
|
||||
Jam never exchanges trusted membership, likes, playlists, or listening
|
||||
history. Volume remains local. Commands carry unique IDs and are retried until
|
||||
the host acknowledges them, while inactive participants expire from the
|
||||
runtime session. Regenerating the capability or restarting the host invalidates
|
||||
the previous link.
|
||||
@@ -208,12 +189,6 @@ The synchronization model combines:
|
||||
- per-peer acknowledgements to determine when old tombstones can be compacted;
|
||||
- snapshots to repair a peer that missed older operations.
|
||||
|
||||
Vectors only grow, so operations hidden from a peer while it ran an older
|
||||
protocol are never sent again after it upgrades. Snapshots are the recovery
|
||||
path for that case; the podcast sections carry the subscription, share and
|
||||
progress registers and the most recent listening sessions, bounded by the
|
||||
shared FRID limits and by the line budget of each message.
|
||||
|
||||
This is eventual consistency scoped to a trusted device group. A temporarily
|
||||
offline laptop can modify playlists, another device can continue playback, and
|
||||
both can later converge without a permanently available coordinator.
|
||||
@@ -237,25 +212,7 @@ The synchronized state describes the queue, current item, position, pause
|
||||
state, volume, shuffle/repeat mode, and the active playback owner. Commands are
|
||||
targeted and deduplicated. Handoff transfers intent and position; the receiving
|
||||
device resolves the track against its own library or federation sources before
|
||||
starting its audio engine. Each queue item names its source: a podcast episode
|
||||
travels as `PlaybackSource::PodcastEpisode` with the reference needed to play
|
||||
it (the receiving device streams it from the publisher at the handed-off
|
||||
position, with or without a subscription). A queue with episodes is not handed
|
||||
to a device below protocol v4, and playback commands from such a device are
|
||||
ignored while the local queue has episodes. Such a device still gets the
|
||||
playback snapshot with its coordination envelope, but with every episode
|
||||
replaced by an opaque "Private item" (`redact_for_jam`): an engine renews the
|
||||
owner's liveness only from the owner's own announcements, so an old device
|
||||
that heard nothing would take over by automatic failover after
|
||||
`owner_timeout_ms` and replace the queue. This departs from FRID's
|
||||
`PLAYBACK_PROTOCOL.md` rule 1 ("otherwise send `None`") and is reported to
|
||||
FRID as a contract gap. An old device that mirrors such a queue drops the
|
||||
`source` its types do not know, so each placeholder becomes a music item
|
||||
titled "Private item"; when it later takes over (Play on it, a paused-startup
|
||||
claim, failover while this device is gone) or sends a command, it announces
|
||||
those. States from devices below v4 are therefore cleaned on receipt
|
||||
(`drop_echoed_private_items`: bare music items with that title and no content
|
||||
id, federated source, artist, release or duration), with a status message.
|
||||
starting its audio engine.
|
||||
|
||||
Active-device leases and idle timing prevent stale snapshots from immediately
|
||||
taking control after a device reconnects. This provides practical coordination
|
||||
@@ -300,119 +257,6 @@ The audio engine is similarly isolated. The application owns the logical
|
||||
queue, while `player::Controller` owns rodio playback and receives explicit
|
||||
commands. Prefetching prepares the next source before the current item ends.
|
||||
|
||||
### Versioned key bindings
|
||||
|
||||
Key bindings are semantic `Action` values loaded from the bundled defaults and
|
||||
merged with the user's `keymap.toml`. Some actions carry positional meaning
|
||||
(`GoToTab(index)` follows the displayed tab order), so changing the tab order
|
||||
changes what an existing user file means. The keymap file therefore carries a
|
||||
format `version`. Files from an older format are adapted in memory when they
|
||||
are loaded and the user is told once per start; Furumi never rewrites a
|
||||
user's configuration. The current format, version 2, orders tabs as Library,
|
||||
Playlists, Queue, Podcasts, Settings, Logs; unversioned files use the earlier
|
||||
order and have their tab indices remapped. A `version` key that TOML attached
|
||||
to a binding (written after a `[[keymaps]]` header) is rejected, so a versioned
|
||||
file is never silently mistaken for a legacy one.
|
||||
|
||||
## Podcasts
|
||||
|
||||
Podcasts are a separate content type, not library tracks. The shared contract
|
||||
lives in FRID: `music_dht::podcast` defines podcast and episode identity, feed
|
||||
URL canonicalization, the RSS/Atom parser and the merge rules, and
|
||||
`furumi_library::podcasts::PodcastStore` persists the catalog, the personal
|
||||
registers (subscription, share, progress) and listening sessions in
|
||||
`podcasts.sqlite3`. The TUI does not re-derive ids or merge rules.
|
||||
|
||||
The TUI owns what the contract leaves to clients:
|
||||
|
||||
- **Fetching.** Feeds are fetched with conditional requests (ETag,
|
||||
Last-Modified), bounded size (32 MiB per feed, 10 MiB per cover) and time
|
||||
(15 s to connect, 60 s per request, 120 s for a whole fetch with its
|
||||
redirects), and at most 10 redirects (http and https only) followed by
|
||||
hand. The
|
||||
address that the leading permanent hops (301/308) lead to becomes a feed
|
||||
alias and the fetch URL, even when a temporary hop (CDN, tracker) follows
|
||||
it; a chain that starts with a temporary hop never moves the feed.
|
||||
Validators are dropped after a permanent redirect because they belong to
|
||||
the old address. The prototype database's validators are dropped once at
|
||||
import, so the first check parses the feed (seasons and episode numbers
|
||||
were not stored by the prototype).
|
||||
- **Scheduling.** The store keeps each feed's local fetch state (validators,
|
||||
last error, consecutive failures, next check). A timer asks it which
|
||||
subscriptions are due; feeds are checked at most a few at a time, a success
|
||||
waits an hour, and failures back off exponentially (honouring
|
||||
`Retry-After`). Errors never clear the stored catalog.
|
||||
- **Runtime boundary.** Every store call, fetch and parse of the app runs on
|
||||
the blocking pool or as a task and reports back through `AppEvent`s;
|
||||
`update::podcasts` holds the pure state transitions. The device-sync
|
||||
stream handlers call the store (snapshot export and merges) inside
|
||||
`block_in_place`, so a store busy with a large feed refresh does not hold
|
||||
a Tokio worker's other tasks; they are never called on the UI thread. Store open failures (newer schema,
|
||||
foreign file) disable only the Podcasts tab and leave the file untouched.
|
||||
- **Sync log.** Local actions (subscribe, unsubscribe, mark played, progress)
|
||||
draw their operation stamps from the device-sync log through
|
||||
`DeviceSync::run_podcast_action`, and the operations are appended with
|
||||
exactly those stamps, so the store's registers and the log agree. The lock
|
||||
order is podcast store, then sync log; device sync never calls the store
|
||||
while it holds the log. Only the newest progress operation of an origin per
|
||||
episode stays in the log (later operations of one origin always win the
|
||||
register comparison).
|
||||
- **Device sync.** Received podcast operations are kept in the log (and
|
||||
relayed) and merged into the store with `PodcastStore::apply_remote_op`;
|
||||
invalid ones are skipped but acknowledged. Operations that arrive before
|
||||
the store opens wait in `sync_podcast_pending` and are applied when it is
|
||||
attached. Snapshot sections are exported with `fill_snapshot` and merged
|
||||
with `apply_snapshot` after the operations of the same message. The UI
|
||||
reloads what it shows after such changes and checks the feed of a
|
||||
subscription that came from another device.
|
||||
- **Queue identity.** An episode is a `TrackItem` with an explicit
|
||||
`podcast` reference (`TrackItem::podcast_episode`); its numeric id is a
|
||||
shared sentinel and never identifies it. Queue keys check the episode id
|
||||
first (`ep:…`), then music content ids, federated sources and library rows.
|
||||
Music-only paths (likes, playlists, history, content-id resolution,
|
||||
federation download, similarity, share links, metadata edits, release and
|
||||
artist jumps) check `is_podcast()` and skip episodes. On the wire an
|
||||
episode is `PlaybackSource::PodcastEpisode`; received ones become episodes
|
||||
again (`TrackItem::from_playback_track`) and the library source mode never
|
||||
drops them. Jam private placeholders are numbered queue entries that go
|
||||
back on the wire as `PlaybackSource::Private`.
|
||||
- **Playback.** Starting an episode resolves its current audio URL and
|
||||
resume position from the store, opens `podcasts::stream` (a bounded
|
||||
Range-request reader on its own thread), decodes and positions it with
|
||||
coarse seeking, all off the UI and audio threads, then hands the source to
|
||||
the audio thread with the player's source generation. Any later start or
|
||||
stop bumps that generation, so a slow stream never replaces what the user
|
||||
chose since. A stale audio URL (4xx) triggers one feed refresh and a retry
|
||||
with the address the feed lists for the same episode id. A stream that
|
||||
cannot seek does not start at a saved position it cannot reach, and seeks
|
||||
in it (or in an episode still connecting) are refused. The stream is
|
||||
cancelled by the same generation: a superseded start stops connecting or
|
||||
reading up to its resume offset, and a read blocked on a stalled server
|
||||
inside the audio callback returns within 100 ms, so it cannot hold the
|
||||
audio thread past the next stop or start. The player does not report the
|
||||
end of a superseded source. Pause and resume pressed while a source is
|
||||
prepared update the intent it starts with.
|
||||
- **End of a stream.** The reader records whether the decoder stopped on a
|
||||
read error (lost connection, expired URL, cancellation) and whether the
|
||||
server announced the file size. Only an end that the server described, or
|
||||
one within the completion window, completes the episode and moves the
|
||||
queue on; otherwise the last seen position is saved and playback stops on
|
||||
the episode (`playback::played_to_the_end`).
|
||||
- **Progress.** `podcasts::progress` runs one writer thread so every
|
||||
progress write of this device is ordered. The app tick follows the
|
||||
position and actual listening time and sends: an explicit start (or
|
||||
"start over"), checkpoints every 20 s and on pause, explicit seeks (saved
|
||||
once the audio thread reports that the seek worked,
|
||||
`PlayerEvent::Seeked`), completion (`reaches_completion` or the real end
|
||||
of the stream) and an end with the last
|
||||
seen position on stop, track change, handoff and exit. When the store
|
||||
reports a newer generation (another device started over, for example), the
|
||||
playback stops checkpointing until the user's next explicit action.
|
||||
Listening sessions are recorded at pause, completion and end; the store
|
||||
keeps those that pass the shared threshold. Queue items carry the latest
|
||||
progress generation this device saw, so a handoff target starts its
|
||||
session above it.
|
||||
|
||||
## Scripted visualizations
|
||||
|
||||
Visualizations are an extension boundary rather than hard-coded rendering
|
||||
@@ -533,6 +377,25 @@ timeouts are bounded; incompatible profiles are rejected. This direct
|
||||
peer-selection layer can later be replaced by DHT routing without changing
|
||||
local storage or ranking.
|
||||
|
||||
## Podcasts
|
||||
|
||||
Podcasts are a separate local source beside the music library. A dedicated
|
||||
SQLite store holds subscriptions and episode metadata keyed by
|
||||
`(podcast, RSS GUID)`. A feed refresh upserts episodes and never deletes
|
||||
ones that dropped out of the feed. Refreshes use conditional HTTP requests
|
||||
and run in the background with bounded concurrency. The store is
|
||||
independent of `furumi-library`, so podcast data cannot affect the music
|
||||
catalog, its migrations or federation publishing.
|
||||
|
||||
Episodes enter the queue as ordinary `TrackItem`s whose `file_path` is the
|
||||
remote audio URL and whose ids sit in a reserved negative range. Playback
|
||||
opens an HTTP range-request reader on the blocking pool. The reader buffers a
|
||||
bounded window ahead of the decoder, and rodio uses coarse, byte-based
|
||||
seeking for it, so a seek far into an episode fetches only the bytes it
|
||||
needs. A sequence number discards a stream that finishes connecting after a
|
||||
newer playback request. Episodes have no content id: listening history,
|
||||
likes, playlists and cross-device handoff skip them.
|
||||
|
||||
## Persistence boundaries
|
||||
|
||||
Furumi stores different kinds of state according to their lifetime:
|
||||
@@ -540,16 +403,16 @@ Furumi stores different kinds of state according to their lifetime:
|
||||
| State | Storage | Role |
|
||||
| --- | --- | --- |
|
||||
| Library, playlists, likes, history | SQLite | Durable local source of truth |
|
||||
| Podcast subscriptions, episodes, progress | SQLite (`podcasts.sqlite3`, FRID store) | Durable personal state (synced with trusted devices) and a refreshable episode catalog |
|
||||
| Device operation log and replicas | SQLite | Offline synchronization |
|
||||
| Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data |
|
||||
| Federation catalog cache | SQLite/cache | Faster network browsing |
|
||||
| Podcast subscriptions and episode metadata | SQLite (`podcasts.sqlite3`) | Durable local podcast catalog; refetchable from feeds |
|
||||
| Podcast covers | Filesystem cache | Replaceable artwork |
|
||||
| Audio and artwork cache | Filesystem cache | Reusable fetched data |
|
||||
| Settings, keymap, identity | Platform config/data dirs | Node configuration |
|
||||
| Queue and playback snapshots | Application/device sync state | Continuity and handoff |
|
||||
|
||||
Caches are replaceable. The local library, the podcast store's personal state
|
||||
and the device operation log are durable.
|
||||
Caches are replaceable. The local library and device operation log are durable.
|
||||
This distinction lets maintenance and recovery code discard derived network
|
||||
data without risking the user's collection.
|
||||
|
||||
@@ -578,13 +441,11 @@ The source tree follows the architectural responsibilities:
|
||||
- `similarity.rs` owns model acquisition, preprocessing, background indexing,
|
||||
and the replaceable exact in-memory index;
|
||||
- `federation/` owns DHT-facing search, peer catalogs, and audio exchange;
|
||||
- `podcasts/` owns feed HTTP (conditional requests, redirects, limits),
|
||||
refresh scheduling policy, the episode stream reader and the progress
|
||||
writer; the podcast model, identity, merge rules and SQLite store come
|
||||
from FRID;
|
||||
- `devices.rs` owns trusted-device replication and playback coordination;
|
||||
- `app/` owns state transitions and runtime orchestration;
|
||||
- `ui/` renders the TUI;
|
||||
- `podcasts/` owns RSS parsing, the podcast store, feed fetching and the
|
||||
HTTP range-request stream reader;
|
||||
- `media.rs` integrates platform media controls;
|
||||
- `visualizer.rs` hosts programmable Rhai visualizations.
|
||||
|
||||
|
||||
+9
-85
@@ -7,93 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.4.0] - unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- A Podcasts tab on `4`: subscribe to RSS/Atom feeds by URL, browse
|
||||
podcast → seasons → episodes with saved position, played state, duration and
|
||||
date, search episodes, filter by listening state, change the order, mark
|
||||
episodes played or unplayed, check one or all feeds, and unsubscribe (progress
|
||||
and history are kept). Feeds are checked in the background with conditional
|
||||
requests and back-off; permanent redirects become feed aliases.
|
||||
- Podcast data uses the shared FRID store (`furumi_library::podcasts`) in
|
||||
`podcasts.sqlite3`; a database of the earlier podcast test build is migrated
|
||||
in place, and a newer or foreign file is refused without changes.
|
||||
- Podcasts sync between trusted devices (device-sync protocol 4):
|
||||
subscriptions, unsubscribing, feed addresses, episode progress ("played",
|
||||
position, "start over") and listening sessions. A podcast subscribed on one
|
||||
device appears on the others without its URL; each device checks the feed
|
||||
itself. Podcast operations are applied to the podcast store, kept in the
|
||||
sync log and relayed; snapshots carry the podcast state (bounded) so new
|
||||
devices and devices that upgrade from an older version catch up. Periodic
|
||||
progress saves are coalesced in the log.
|
||||
- A queue with podcast episodes is handed to other devices with the episode,
|
||||
its position and pause; the receiving device does not need the
|
||||
subscription.
|
||||
- Podcast episodes play from the Podcasts tab and the shared queue (mixed
|
||||
with music): audio streams from the publisher through a bounded HTTP Range
|
||||
buffer, Play continues from the saved position, `b` starts over, and the
|
||||
position is saved every 20 s and on pause, seek, stop, track change and
|
||||
exit. Completion and listening sessions follow the shared FRID rules. A
|
||||
stale audio URL triggers one feed check for the episode's new address. A
|
||||
lost connection stops playback at the saved position (the episode is not
|
||||
completed and the queue does not move on). Without a file size from the
|
||||
server, a closed stream counts as the end within the completion window of
|
||||
the duration the audio file states, or past 80 % of a duration only the
|
||||
feed announced; a superseded or stalled stream
|
||||
is cancelled at the next start or stop; pause pressed while an episode
|
||||
connects is kept; seeks that do not move the audio are not saved.
|
||||
- Podcast key bindings in the `podcasts` context: `o`, `/`, `Shift+R`,
|
||||
`Ctrl+R`, `Shift+S`, `f`, `m`, `Shift+U`, `b`.
|
||||
- An optional top-level `version` in `keymap.toml`; the current format is
|
||||
`version = 2`, and a `podcasts` binding context.
|
||||
|
||||
### Changed
|
||||
|
||||
- `music-dht` 0.6.0 and `furumi-library` 0.3.0.
|
||||
- Queue items are keyed by their source: podcast episodes by episode id,
|
||||
library rows as `lib:<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
|
||||
|
||||
- The protocol version stored for another device came from this build's own
|
||||
constant (on profile announcements and accepted pairings), not from the
|
||||
device. Versions now come from the device's own reports; relayed profiles
|
||||
only raise them. Invites and profile announcements carry the version, and
|
||||
the first start of a new version announces it.
|
||||
- A Podcasts tab (key `6`) for RSS subscriptions: add a feed by URL, browse
|
||||
the episode catalog with title search and date sorting, and play an episode
|
||||
through the regular player and queue.
|
||||
- Episodes stream over HTTP with range-request seeking; only feed metadata
|
||||
is stored locally, in `podcasts.sqlite3`, with covers in the cache
|
||||
directory.
|
||||
- Background refresh of stale feeds (conditional requests, GUID
|
||||
deduplication) and a `RefreshPodcasts` action (`Shift+R` in the
|
||||
`podcasts` key context).
|
||||
|
||||
## [0.2.8] - 2026-09-02
|
||||
|
||||
|
||||
Generated
+11
-9
@@ -1559,7 +1559,8 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "federation-net"
|
||||
version = "0.3.1"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01e5c2e6e5af020dc202d4924125ef57719aa21f8d2567e2d943914386bc8980"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"data-encoding",
|
||||
@@ -1697,8 +1698,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "furumi-library"
|
||||
version = "0.3.0"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f78466b6345cb90d7bb37db5d9dd67476ab6c2c3b2202876d24f448c314a9a08"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"blake3",
|
||||
@@ -1713,7 +1715,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "furumi_tui"
|
||||
version = "0.4.0"
|
||||
version = "0.3.5"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64",
|
||||
@@ -1722,6 +1724,7 @@ dependencies = [
|
||||
"crokey",
|
||||
"crossterm",
|
||||
"directories",
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
"fs2",
|
||||
"furumi-library",
|
||||
@@ -1731,6 +1734,7 @@ dependencies = [
|
||||
"lofty",
|
||||
"music-dht",
|
||||
"object",
|
||||
"quick-xml",
|
||||
"ratatui",
|
||||
"reqwest 0.12.28",
|
||||
"rhai",
|
||||
@@ -3237,17 +3241,16 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "music-dht"
|
||||
version = "0.6.0"
|
||||
source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c3f75d49d3a742a6777a1c4cc53f397399dfc9ddb70034c57aeb44d75d2ee4f"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"blake3",
|
||||
"data-encoding",
|
||||
"encoding_rs",
|
||||
"federation-net",
|
||||
"futures",
|
||||
"postcard",
|
||||
"quick-xml",
|
||||
"rand 0.9.5",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
@@ -3256,7 +3259,6 @@ dependencies = [
|
||||
"tokio",
|
||||
"tracing",
|
||||
"unicode-normalization",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
+7
-6
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumi_tui"
|
||||
version = "0.4.0"
|
||||
version = "0.3.5"
|
||||
edition = "2024"
|
||||
rust-version = "1.97"
|
||||
description = "A federated P2P player for personal music libraries"
|
||||
@@ -26,13 +26,17 @@ blake3 = "1"
|
||||
crokey = "1.4.0"
|
||||
crossterm = { version = "0.29.0", features = ["event-stream"] }
|
||||
directories = "6.0.0"
|
||||
# Podcast feeds: non-UTF-8 XML declarations (windows-1251, koi8-r, ...).
|
||||
encoding_rs = "0.8.35"
|
||||
futures-util = "0.3.32"
|
||||
furumi-library = "0.3.0"
|
||||
furumi-library = "0.2.0"
|
||||
image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] }
|
||||
lofty = "0.22"
|
||||
# P2P federation: library index in a shared DHT + audio streaming between
|
||||
# peers (same protocol as furumi-fd).
|
||||
music-dht = { version = "0.6.0", features = ["podcast-feed"] }
|
||||
music-dht = "0.5.0"
|
||||
# Podcast RSS/Atom parsing.
|
||||
quick-xml = "0.41.0"
|
||||
ratatui = "0.30.1"
|
||||
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
|
||||
rhai = { version = "1", features = ["sync"] }
|
||||
@@ -57,9 +61,6 @@ unicode-width = "0.2.2"
|
||||
# souvlaki 0.8.3 still depends on the unmaintained block 0.1.6. Keep its API
|
||||
# intact while using an opaque inhabited FFI type accepted by current Rust.
|
||||
block = { path = "vendor/block" }
|
||||
# Unpublished frid (MR-F1, MR-F2); remove once music-dht 0.6.0 and furumi-library 0.3.0 are on crates.io.
|
||||
music-dht = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" }
|
||||
furumi-library = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" }
|
||||
|
||||
[target.'cfg(target_os="macos")'.dependencies]
|
||||
core-foundation = "0.10.1"
|
||||
|
||||
@@ -79,6 +79,33 @@ The first selectable model is downloaded on demand and is licensed separately
|
||||
by MTG under CC BY-NC-SA 4.0 (a proprietary license is also available from
|
||||
MTG); Furumi itself remains WTFPL.
|
||||
|
||||
## Podcasts
|
||||
|
||||
The **Podcasts** tab (key `6`) subscribes to podcast RSS feeds and plays
|
||||
episodes through the same player, queue and media keys as your music.
|
||||
|
||||
- Choose **Add podcast** and paste the feed URL, for example
|
||||
`https://podster.fm/rss.xml?pid=778`. Furumi stores the feed's metadata
|
||||
only: titles, notes, dates and durations. Audio files are never
|
||||
downloaded in bulk.
|
||||
- Press `Enter` on a podcast to open its episode list. `/` searches episode
|
||||
titles as you type, `v` switches between newest-first and oldest-first,
|
||||
and `Shift+R` checks for new episodes.
|
||||
- `Enter` on an episode plays it right away, ahead of the rest of your
|
||||
queue. `a` / `Shift+A` queue it like a track. The episode streams over HTTP
|
||||
and seeks with range requests, so jumping into a long episode does not
|
||||
download everything before that point.
|
||||
- Subscriptions are checked for new episodes in the background. A feed is
|
||||
fetched again only after 30 minutes, and the request is conditional, so
|
||||
an unchanged feed costs almost nothing. Episodes are matched by their RSS
|
||||
GUID, so each one is listed once.
|
||||
- `Shift+D` on a podcast in the list unsubscribes from it.
|
||||
|
||||
Subscriptions live in Furumi's data directory (`podcasts.sqlite3`). Covers are
|
||||
kept in the cache directory. Podcasts are local to each device: they are not
|
||||
yet synchronized between trusted devices or shared through federation, and
|
||||
playback position is not remembered.
|
||||
|
||||
## Install
|
||||
|
||||
### macOS
|
||||
@@ -149,144 +176,6 @@ The **Additional settings** window also contains the music save directory and
|
||||
visualization controls. Use Up/Down (or j/k) to navigate, Enter to select, and
|
||||
Esc to return to Settings. Long lists scroll with the selection.
|
||||
|
||||
### Tabs and key bindings
|
||||
|
||||
The number keys open the tabs in the order they are shown, and Tab /
|
||||
Shift+Tab cycle through them:
|
||||
|
||||
| Key | Tab |
|
||||
| --- | --- |
|
||||
| `1` | Library |
|
||||
| `2` | Playlists |
|
||||
| `3` | Queue |
|
||||
| `4` | Podcasts |
|
||||
| `5` | Settings |
|
||||
| `6` | Logs |
|
||||
|
||||
On narrow terminals the tab bar shortens titles and then shows only the
|
||||
numbers, keeping the active tab's full name. Press `?` for the full list of
|
||||
bindings and commands.
|
||||
|
||||
Bindings can be overridden in `keymap.toml` in the platform config directory
|
||||
(`~/.config/furumi/keymap.toml` on Linux,
|
||||
`~/Library/Application Support/furumi/keymap.toml` on macOS); the bundled
|
||||
`src/config/default_keymap.toml` documents the format. Start the file with
|
||||
`version = 2`, the current format: its `GoToTab` indices are used exactly as
|
||||
written (`0` Library … `5` Logs). `version` must come before the first
|
||||
`[[keymaps]]` entry; written after one, TOML would attach it to that entry, so
|
||||
Furumi rejects such a file with an error instead of guessing. Files without `version` were written for the
|
||||
older tab order (Settings and Logs on `4`/`5`, Podcasts on `6` in the
|
||||
prototype podcast build); Furumi adapts their tab shortcuts in memory so they
|
||||
keep opening the same tabs, shows a short notice at startup, and never
|
||||
rewrites the file. Add `version = 2` once the indices match the new order to
|
||||
take them literally and silence the notice.
|
||||
|
||||
### Podcasts
|
||||
|
||||
The Podcasts tab (`4`) follows RSS and Atom podcast feeds. Furumi stores only
|
||||
the feed's episode information; audio stays on the publisher's server and is
|
||||
never imported into the music library or offered to other peers.
|
||||
|
||||
- **Add podcast** (`o`, or the first row) takes a feed URL; `feed://`,
|
||||
`podcast://` and URLs without a scheme are accepted. Nothing is stored unless
|
||||
the URL serves a feed. New subscriptions are private.
|
||||
- The list shows each podcast's unplayed and total episode counts and its
|
||||
check status; `!` marks a podcast whose last check failed, with the error in
|
||||
the side panel (or below the list on narrow terminals).
|
||||
- `Enter` opens a podcast. Podcasts with seasons show their seasons first
|
||||
(plus "All episodes" and "Without a season"); `Esc` goes back.
|
||||
- Episodes show the date, season/episode number, duration and listening
|
||||
state: `●` not started, `◐` in progress with the saved position, `✓` played.
|
||||
`/` searches titles and descriptions, `f` cycles all / unplayed / in
|
||||
progress / played, `Shift+S` changes the order, `m` marks the selected
|
||||
episode played or unplayed.
|
||||
- `Shift+R` checks the selected podcast for new episodes, `Ctrl+R` checks all
|
||||
of them. Feeds are also checked in the background about once an hour with
|
||||
conditional requests; a failing feed is retried with growing pauses and its
|
||||
known episodes stay available. A permanent redirect moves the subscription
|
||||
to the new address.
|
||||
- `Shift+U` (or `Shift+D`) unsubscribes after a confirmation. Listening
|
||||
progress and history are kept, so subscribing again continues where you
|
||||
left off.
|
||||
|
||||
Episodes play in the same queue as music:
|
||||
|
||||
- `Enter` on an episode plays it right after the current item and continues
|
||||
from the saved position; a played episode starts from the beginning. `b`
|
||||
("start over") plays it from the beginning even when it is in progress.
|
||||
`a` / `Shift+A` queue episodes like tracks, and the queue can mix music and
|
||||
episodes.
|
||||
- Audio streams from the publisher's server through a bounded buffer (about
|
||||
9 MB) and seeks with HTTP range requests. Nothing is downloaded ahead of
|
||||
time and nothing is kept after playback. If the server cannot seek (no
|
||||
file length), Furumi says that it cannot continue at the saved position
|
||||
instead of starting from the wrong place; "start over" still works, and
|
||||
seeking in such an episode is refused with a message. If the episode's
|
||||
address no longer works, the feed is checked once for a new one.
|
||||
- If the connection is lost during an episode (no data for 30 seconds, or
|
||||
the address stops working), playback stops there: the position is saved,
|
||||
the episode is not marked played and the queue does not move on; play
|
||||
continues from that position. A pause pressed while an episode is still
|
||||
connecting is kept when the audio arrives.
|
||||
- Some servers send episodes without a file size. There a closed connection
|
||||
looks like the end of the file, so Furumi compares the position with the
|
||||
episode's duration: the one the audio file states, or else the
|
||||
`itunes:duration` of the feed. Feed durations are often a little off, so
|
||||
reaching 80 % of one is enough; an episode that stops earlier is treated as
|
||||
a lost connection. Such servers do not allow seeking either, so play cannot
|
||||
continue in the middle: start the episode over (`b`) or mark it played
|
||||
(`m`).
|
||||
- The position is saved every 20 seconds while playing and on pause, seek,
|
||||
stop, track change and exit. An episode counts as played within the last
|
||||
30 seconds (or 5 %) or when its stream reaches its end; a listen enters the episode's
|
||||
history when it completed or lasted at least 30 seconds. Seeking back and
|
||||
"start over" are real changes: an older save never moves the position
|
||||
forward again.
|
||||
- Likes, playlists, music history, similarity search and share links are for
|
||||
music only and skip episodes; `Shift+J` on an episode opens its podcast.
|
||||
|
||||
Podcasts and your connected devices:
|
||||
|
||||
- **Private by default.** Subscriptions, episode progress and listening
|
||||
history are personal. They are synchronized only between your trusted
|
||||
devices (Connected Devices), never published to the federation: no DHT
|
||||
record, public catalog or search result contains them. Publishing a
|
||||
podcast for discovery is a separate, explicit choice that this version
|
||||
does not offer yet.
|
||||
- **Synchronized between trusted devices.** A podcast you subscribe to on
|
||||
one device appears on the others without typing its URL again; each
|
||||
device then checks the feed itself. Unsubscribing, "played", the saved
|
||||
position and "start over" follow the same way, also through a device
|
||||
that relays them and after a device was offline for a while. When two
|
||||
devices played the same episode offline, they agree on one position and
|
||||
keep both listening sessions.
|
||||
- **Handoff.** A queue with episodes moves to another device like music:
|
||||
the episode continues there at the handed-off position, paused or
|
||||
playing, even if that device is not subscribed. Devices that run an older
|
||||
Furumi (device sync before v4) cannot keep episodes: handing such a queue
|
||||
to them is refused with "needs a Furumi update", their remote control is
|
||||
ignored while the queue has episodes, and they get no podcast data: they
|
||||
see such a queue with "Private item" in place of each episode, so they
|
||||
keep seeing this device as the playing one. An older device still takes
|
||||
over when you choose it as the output (Play on it), when it starts while
|
||||
this device is paused (`take_paused_on_startup`), or by itself once this
|
||||
device has been closed, asleep or offline for about two minutes. Its queue
|
||||
then has an unplayable "Private item" music entry for each episode; this
|
||||
device shows that queue without them (with a status message), and the
|
||||
episodes' saved positions stay.
|
||||
- **Audio from the publisher.** Every device streams the episode from the
|
||||
publisher's server itself. Furumi peers never pass podcast audio to each
|
||||
other, and nothing is kept after playback.
|
||||
- **Jam.** Guests see the host's episodes as "Private item" (no title, feed
|
||||
or address), cannot add episodes to the host's queue and cannot seek in a
|
||||
private item. They can still reorder the queue and skip.
|
||||
|
||||
Podcast data lives in `podcasts.sqlite3` in the platform data directory (the
|
||||
same file the earlier podcast test build used; it is upgraded in place). If
|
||||
that file was written by a newer Furumi or is not a podcast database, the tab
|
||||
explains why it is unavailable and the file is left unchanged; podcast
|
||||
changes from other devices then wait in the device-sync log until it opens.
|
||||
|
||||
### Now playing in tmux
|
||||
|
||||
While Furumi is running, a second invocation can print a cheap, single-line
|
||||
@@ -374,16 +263,11 @@ Run the local protocol checks from the TUI repository:
|
||||
|
||||
```bash
|
||||
cargo test localhost_devices_exchange_state_and_handoff
|
||||
cargo test localhost_devices_sync_podcasts
|
||||
cargo test devices::podcast_sync_tests
|
||||
python scripts/test_device_interop.py ../furumusic
|
||||
```
|
||||
|
||||
The first test uses real iroh streams, isolated SQLite databases, ownership
|
||||
handoff, and a stalled peer. The podcast tests sync subscriptions, progress
|
||||
and "start over" between TUI device stores (over iroh streams and through
|
||||
the JSON messages), check a handoff with an episode and simulate a device
|
||||
sync v3 peer with the exact JSON types of furumi-tui 0.3.x. The second builds both player test binaries and
|
||||
handoff, and a stalled peer. The second builds both player test binaries and
|
||||
exchanges their actual JSON messages over `127.0.0.1`: queue metadata,
|
||||
bidirectional handoff, pause/seek and duplicate command fencing. It exercises
|
||||
the TUI command adapter and web player hub, but does not start a browser or
|
||||
|
||||
+7
-45
@@ -57,24 +57,8 @@ pub enum Action {
|
||||
OpenSearch,
|
||||
EditSelected,
|
||||
DeleteSelected,
|
||||
/// Podcasts: subscribe to an RSS feed URL.
|
||||
AddPodcast,
|
||||
/// Podcasts: search the episodes of the opened podcast.
|
||||
SearchEpisodes,
|
||||
/// Podcasts: check the selected or opened podcast for new episodes.
|
||||
RefreshPodcast,
|
||||
/// Podcasts: check every subscription for new episodes.
|
||||
RefreshAllPodcasts,
|
||||
/// Podcasts: cycle the episode order.
|
||||
CycleEpisodeSort,
|
||||
/// Podcasts: cycle the played-state filter.
|
||||
CycleEpisodeFilter,
|
||||
/// Podcasts: mark the selected episode played / unplayed.
|
||||
ToggleEpisodePlayed,
|
||||
/// Podcasts: unsubscribe (progress and history are kept).
|
||||
UnsubscribePodcast,
|
||||
/// Podcasts: play the selected (or playing) episode from the beginning.
|
||||
RestartEpisode,
|
||||
/// Podcasts: check the opened (or every) subscription for new episodes.
|
||||
RefreshPodcasts,
|
||||
}
|
||||
|
||||
/// Help-window sections, in display order.
|
||||
@@ -84,18 +68,16 @@ pub enum Category {
|
||||
Queue,
|
||||
Library,
|
||||
Navigation,
|
||||
Podcasts,
|
||||
Search,
|
||||
System,
|
||||
}
|
||||
|
||||
impl Category {
|
||||
pub const ALL: [Category; 7] = [
|
||||
pub const ALL: [Category; 6] = [
|
||||
Category::Playback,
|
||||
Category::Queue,
|
||||
Category::Library,
|
||||
Category::Navigation,
|
||||
Category::Podcasts,
|
||||
Category::Search,
|
||||
Category::System,
|
||||
];
|
||||
@@ -106,7 +88,6 @@ impl Category {
|
||||
Category::Queue => "Queue & playlists",
|
||||
Category::Library => "Library",
|
||||
Category::Navigation => "Navigation",
|
||||
Category::Podcasts => "Podcasts",
|
||||
Category::Search => "Search & commands",
|
||||
Category::System => "System",
|
||||
}
|
||||
@@ -159,16 +140,8 @@ impl Action {
|
||||
Action::EditSelected
|
||||
| Action::DeleteSelected
|
||||
| Action::CycleSourceMode
|
||||
| Action::OpenLibraryFilters => Category::Library,
|
||||
Action::AddPodcast
|
||||
| Action::SearchEpisodes
|
||||
| Action::RefreshPodcast
|
||||
| Action::RefreshAllPodcasts
|
||||
| Action::CycleEpisodeSort
|
||||
| Action::CycleEpisodeFilter
|
||||
| Action::ToggleEpisodePlayed
|
||||
| Action::UnsubscribePodcast
|
||||
| Action::RestartEpisode => Category::Podcasts,
|
||||
| Action::OpenLibraryFilters
|
||||
| Action::RefreshPodcasts => Category::Library,
|
||||
Action::OpenSearch | Action::OpenCommandLine => Category::Search,
|
||||
Action::ToggleHelp | Action::Quit => Category::System,
|
||||
}
|
||||
@@ -200,10 +173,7 @@ impl Action {
|
||||
Action::Quit => "Quit".into(),
|
||||
Action::NextTab => "Next tab".into(),
|
||||
Action::PrevTab => "Previous tab".into(),
|
||||
Action::GoToTab(i) => match crate::app::state::Tab::from_index(*i) {
|
||||
Some(tab) => format!("Go to {}", tab.title()),
|
||||
None => format!("Go to tab {}", i + 1),
|
||||
},
|
||||
Action::GoToTab(i) => format!("Go to tab {}", i + 1),
|
||||
Action::MoveUp => "Move up".into(),
|
||||
Action::MoveDown => "Move down".into(),
|
||||
Action::MoveLeft => "Move left".into(),
|
||||
@@ -248,15 +218,7 @@ impl Action {
|
||||
Action::OpenSearch => "Search artists, releases, tracks".into(),
|
||||
Action::EditSelected => "Edit the selected item".into(),
|
||||
Action::DeleteSelected => "Delete the selected item".into(),
|
||||
Action::AddPodcast => "Add podcast (RSS URL)".into(),
|
||||
Action::SearchEpisodes => "Search episodes".into(),
|
||||
Action::RefreshPodcast => "Check podcast for new episodes".into(),
|
||||
Action::RefreshAllPodcasts => "Check all podcasts".into(),
|
||||
Action::CycleEpisodeSort => "Episode order".into(),
|
||||
Action::CycleEpisodeFilter => "Filter: all/unplayed/played".into(),
|
||||
Action::ToggleEpisodePlayed => "Mark played / unplayed".into(),
|
||||
Action::UnsubscribePodcast => "Unsubscribe (keeps progress)".into(),
|
||||
Action::RestartEpisode => "Play episode from the start".into(),
|
||||
Action::RefreshPodcasts => "Podcasts: check for new episodes".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
-57
@@ -130,8 +130,7 @@ pub enum AppEvent {
|
||||
tracks: Vec<TrackItem>,
|
||||
},
|
||||
/// A status snapshot for the Federation tab.
|
||||
/// Boxed to keep `AppEvent` small; the status is large and rare.
|
||||
FederationStatus(Box<crate::federation::FedStatus>),
|
||||
FederationStatus(crate::federation::FedStatus),
|
||||
/// Federated live-search results (artists a card can be opened for,
|
||||
/// plus matching tracks).
|
||||
FedSearchLoaded {
|
||||
@@ -173,8 +172,7 @@ pub enum AppEvent {
|
||||
FedTrackInfoLoaded {
|
||||
placeholder_id: i64,
|
||||
item_id: String,
|
||||
/// Boxed: `TrackItem` is large since it can carry a podcast episode.
|
||||
result: Result<Box<TrackItem>, String>,
|
||||
result: Result<TrackItem, String>,
|
||||
},
|
||||
/// This peer's connection ticket, requested from the Federation tab.
|
||||
FedTicket(Result<String, String>),
|
||||
@@ -208,64 +206,26 @@ pub enum AppEvent {
|
||||
JamInvite(Result<String, String>),
|
||||
/// Result of joining a Jam capability.
|
||||
JamJoined(Result<String, String>),
|
||||
/// Received podcast operations or snapshot sections changed the podcast
|
||||
/// store: `progress` lists changed progress registers, `changed` means
|
||||
/// anything else (subscriptions, feeds, sessions).
|
||||
PodcastsSynced {
|
||||
progress: Vec<music_dht::podcast::ProgressRecord>,
|
||||
changed: bool,
|
||||
},
|
||||
/// The podcast store opened (with the feed client), or why it did not.
|
||||
PodcastStoreOpened(Result<crate::podcasts::refresh::Services, String>),
|
||||
/// Subscribed podcasts reloaded from the podcast store.
|
||||
PodcastsLoaded(Result<Vec<crate::app::state::PodcastRow>, String>),
|
||||
PodcastSeasonsLoaded {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
result: Result<Vec<furumi_library::podcasts::SeasonSummary>, String>,
|
||||
},
|
||||
/// One page of episodes; `seq` drops results of older requests.
|
||||
/// Podcast subscriptions (re)loaded from the podcast store.
|
||||
PodcastsLoaded(Result<Vec<crate::podcasts::Podcast>, String>),
|
||||
/// The stored episode catalog of one podcast.
|
||||
PodcastEpisodesLoaded {
|
||||
seq: u64,
|
||||
offset: usize,
|
||||
result: Result<Vec<furumi_library::podcasts::EpisodeListItem>, String>,
|
||||
podcast_id: i64,
|
||||
result: Result<Vec<crate::podcasts::Episode>, String>,
|
||||
},
|
||||
PodcastSubscribed(Result<crate::podcasts::refresh::SubscribeReport, String>),
|
||||
/// One feed check finished.
|
||||
/// A new subscription was stored (its id) or rejected.
|
||||
PodcastSubscribed(Result<i64, String>),
|
||||
/// One feed refresh finished.
|
||||
PodcastRefreshed {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
result: Result<crate::podcasts::refresh::RefreshReport, String>,
|
||||
/// Asked by the user (status bar), not the background scheduler.
|
||||
manual: bool,
|
||||
podcast_id: i64,
|
||||
result: Result<crate::podcasts::RefreshSummary, String>,
|
||||
/// Background sweep: report errors in the log, not the status bar.
|
||||
quiet: bool,
|
||||
},
|
||||
PodcastUnsubscribed {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
title: String,
|
||||
podcast_id: i64,
|
||||
result: Result<(), String>,
|
||||
},
|
||||
/// Result of the progress writer (saves, supersedes, mark played).
|
||||
PodcastProgress(crate::podcasts::progress::ProgressEvent),
|
||||
/// An episode stream is playing; `generation` is the player source
|
||||
/// generation of the start that requested it.
|
||||
PodcastEpisodeStarted {
|
||||
generation: u64,
|
||||
episode: Box<music_dht::podcast::PodcastEpisodeRef>,
|
||||
start_ms: i64,
|
||||
duration_ms: Option<i64>,
|
||||
/// The decoder measured `duration_ms` (else the feed announced it).
|
||||
duration_measured: bool,
|
||||
restart: bool,
|
||||
/// The server allows seeking in the stream.
|
||||
seekable: bool,
|
||||
/// How the stream ended, once it does.
|
||||
probe: crate::podcasts::stream::StreamProbe,
|
||||
},
|
||||
/// Starting an episode failed (network, decoding, cannot resume).
|
||||
PodcastEpisodeFailed {
|
||||
generation: u64,
|
||||
message: String,
|
||||
},
|
||||
/// Scheduler timer: ask the store which feeds are due.
|
||||
PodcastRefreshTick,
|
||||
/// Feeds due for a background refresh.
|
||||
PodcastsDue(Result<Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>, String>),
|
||||
/// Periodic timer: refresh subscriptions whose feed went stale.
|
||||
PodcastAutoRefreshDue,
|
||||
}
|
||||
+213
-548
File diff suppressed because it is too large.
Load diff
@@ -1,138 +0,0 @@
|
||||
//! Podcast state fixtures shared by update and rendering tests.
|
||||
|
||||
use furumi_library::podcasts::{
|
||||
EpisodeListItem, EpisodeQuery, FetchState, PodcastSummary, SeasonSummary,
|
||||
};
|
||||
use music_dht::podcast::{
|
||||
EpisodeId, EpisodeMeta, EpisodeType, FeedUrl, PodcastId, PodcastMeta, ProgressRecord,
|
||||
ProgressStatus,
|
||||
};
|
||||
|
||||
use crate::app::state::{
|
||||
AppState, EpisodeList, Loadable, OpenedPodcast, PodcastRow, PodcastStoreStatus, Tab,
|
||||
};
|
||||
|
||||
pub fn podcast_id() -> PodcastId {
|
||||
PodcastId::from_feed_url(&FeedUrl::parse("https://example.test/feed.xml").unwrap())
|
||||
}
|
||||
|
||||
pub fn row(title: &str, unplayed: u64, episodes: u64, seasons: u64) -> PodcastRow {
|
||||
PodcastRow {
|
||||
summary: PodcastSummary {
|
||||
podcast_id: podcast_id(),
|
||||
meta: PodcastMeta {
|
||||
feed_url: FeedUrl::parse("https://example.test/feed.xml").unwrap(),
|
||||
title: title.into(),
|
||||
author: Some("Host".into()),
|
||||
description: Some("A show about tests.".into()),
|
||||
link: None,
|
||||
image_url: None,
|
||||
},
|
||||
subscribed: true,
|
||||
shared: false,
|
||||
episode_count: episodes,
|
||||
unplayed_count: unplayed,
|
||||
in_progress_count: 0,
|
||||
latest_published_at_ms: None,
|
||||
season_count: seasons,
|
||||
},
|
||||
fetch: FetchState::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn episode(n: u32, title: &str, status: Option<(ProgressStatus, i64)>) -> EpisodeListItem {
|
||||
let podcast = podcast_id();
|
||||
let episode_id = EpisodeId::from_guid(&podcast, &format!("ep-{n}")).unwrap();
|
||||
EpisodeListItem {
|
||||
episode: EpisodeMeta {
|
||||
episode_id: episode_id.clone(),
|
||||
podcast_id: podcast.clone(),
|
||||
guid: Some(format!("ep-{n}")),
|
||||
title: title.into(),
|
||||
description: Some(format!("Notes for {title}.")),
|
||||
season: Some(1),
|
||||
episode_number: Some(n),
|
||||
episode_type: EpisodeType::Full,
|
||||
published_at_ms: Some(1_718_409_600_000),
|
||||
duration_ms: Some(2_700_000),
|
||||
audio_url: format!("https://example.test/{n}.mp3"),
|
||||
mime: Some("audio/mpeg".into()),
|
||||
byte_len: None,
|
||||
image_url: None,
|
||||
link: None,
|
||||
},
|
||||
progress: status.map(|(status, position_ms)| ProgressRecord {
|
||||
episode_id,
|
||||
podcast_id: podcast,
|
||||
position_ms,
|
||||
status,
|
||||
generation: 1,
|
||||
session_id: "s".into(),
|
||||
hlc_ms: 1,
|
||||
device_id: "dev".into(),
|
||||
op_id: "dev:1".into(),
|
||||
}),
|
||||
completed_count: 0,
|
||||
in_feed: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ready_state(rows: Vec<PodcastRow>) -> AppState {
|
||||
let mut state = AppState {
|
||||
active_tab: Tab::Podcasts,
|
||||
..AppState::default()
|
||||
};
|
||||
state.podcasts.store = PodcastStoreStatus::Ready;
|
||||
state.podcasts.list = Some(Loadable::Ready(rows));
|
||||
state
|
||||
}
|
||||
|
||||
pub fn with_episodes(mut state: AppState, items: Vec<EpisodeListItem>) -> AppState {
|
||||
let season = furumi_library::podcasts::SeasonFilter::Any;
|
||||
state.podcasts.opened = Some(OpenedPodcast {
|
||||
podcast_id: podcast_id(),
|
||||
season_cursor: None,
|
||||
season: Some(season),
|
||||
episode_cursor: 0,
|
||||
});
|
||||
state.podcasts.episodes = Some(EpisodeList {
|
||||
seq: 1,
|
||||
query: EpisodeQuery::new(podcast_id()),
|
||||
items,
|
||||
loading: false,
|
||||
loading_more: false,
|
||||
complete: true,
|
||||
error: None,
|
||||
focus: None,
|
||||
});
|
||||
state
|
||||
}
|
||||
|
||||
pub fn seasons() -> Vec<SeasonSummary> {
|
||||
vec![
|
||||
SeasonSummary {
|
||||
season: Some(1),
|
||||
episode_count: 3,
|
||||
unplayed_count: 1,
|
||||
first_published_at_ms: Some(1_609_459_200_000),
|
||||
last_published_at_ms: Some(1_640_995_200_000),
|
||||
},
|
||||
SeasonSummary {
|
||||
season: None,
|
||||
episode_count: 2,
|
||||
unplayed_count: 0,
|
||||
first_published_at_ms: None,
|
||||
last_published_at_ms: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Queue item for episode `n` of the fixture podcast ("Show").
|
||||
pub fn episode_track(n: u32) -> crate::library::models::TrackItem {
|
||||
let meta = row("Show", 0, 0, 0).summary.meta;
|
||||
crate::library::models::TrackItem::podcast_episode(
|
||||
episode(n, &format!("Episode {n}"), None)
|
||||
.episode
|
||||
.to_ref(&meta),
|
||||
)
|
||||
}
|
||||
+409
-1065
File diff suppressed because it is too large.
Load diff
+38
-63
@@ -115,15 +115,6 @@ pub fn handle_key(state: &mut AppState, runtime: &mut Runtime, key: KeyEvent) {
|
||||
Popup::ConfirmDelete { target, label } => {
|
||||
handle_confirm_delete(state, runtime, target, label, key);
|
||||
}
|
||||
Popup::ConfirmPodcastUnsubscribe { podcast_id, title } => match key.code {
|
||||
KeyCode::Enter | KeyCode::Char('y') => super::perform_effect(
|
||||
state,
|
||||
runtime,
|
||||
crate::app::update::Effect::PodcastUnsubscribe { podcast_id, title },
|
||||
),
|
||||
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {}
|
||||
_ => state.popup = Some(Popup::ConfirmPodcastUnsubscribe { podcast_id, title }),
|
||||
},
|
||||
Popup::SimilarityPrivacyConsent { enable_federation } => match key.code {
|
||||
KeyCode::Enter | KeyCode::Char('y') => {
|
||||
state.similarity.settings.federation_consent = true;
|
||||
@@ -567,30 +558,13 @@ fn handle_fed_input(
|
||||
) {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
// Esc in the episode search clears it.
|
||||
if field == FedInputField::PodcastSearch
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, String::new())
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
if field == FedInputField::PodcastFilter {
|
||||
crate::app::update::podcasts_set_filter(state, String::new());
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let value = input.trim().to_string();
|
||||
match field {
|
||||
FedInputField::PodcastFeedUrl => {
|
||||
if let Some(effect) =
|
||||
crate::app::update::podcasts::submit_feed_url(state, &value)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
// The search is applied while typing; Enter keeps it.
|
||||
FedInputField::PodcastSearch => {
|
||||
let search = input.to_string();
|
||||
if let Some(effect) = crate::app::update::podcasts::set_search(state, search) {
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
FedInputField::NetworkId => {
|
||||
state.federation.settings.network_id = value;
|
||||
// An empty id turns federation off rather than leaving a
|
||||
@@ -692,17 +666,29 @@ fn handle_fed_input(
|
||||
);
|
||||
}
|
||||
}
|
||||
FedInputField::PodcastFeedUrl => {
|
||||
if value.is_empty() {
|
||||
state.status_message = Some("podcast feed URL is empty".into());
|
||||
} else {
|
||||
super::perform_effect(
|
||||
state,
|
||||
runtime,
|
||||
crate::app::update::Effect::PodcastSubscribe(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
// The filter is already applied live; Enter just keeps it.
|
||||
FedInputField::PodcastFilter => {
|
||||
crate::app::update::podcasts_set_filter(state, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
input.handle_key(key);
|
||||
let search = (field == FedInputField::PodcastSearch).then(|| input.to_string());
|
||||
state.popup = Some(Popup::FedInput { field, input });
|
||||
if let Some(search) = search
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, search)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
if field == FedInputField::PodcastFilter {
|
||||
crate::app::update::podcasts_set_filter(state, input.to_string());
|
||||
}
|
||||
state.popup = Some(Popup::FedInput { field, input });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -821,15 +807,15 @@ fn handle_device_leave(state: &mut AppState, runtime: &mut Runtime, key: KeyEven
|
||||
}
|
||||
|
||||
/// Pasted text goes into the focused text field when one is open.
|
||||
pub fn handle_paste(state: &mut AppState, runtime: &mut Runtime, pasted: &str) {
|
||||
pub fn handle_paste(state: &mut AppState, pasted: &str) {
|
||||
let cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect();
|
||||
let mut search = None;
|
||||
let mut filter = None;
|
||||
match &mut state.popup {
|
||||
Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned),
|
||||
Some(Popup::FedInput { field, input }) => {
|
||||
input.insert_str(&cleaned);
|
||||
if *field == FedInputField::PodcastSearch {
|
||||
search = Some(input.to_string());
|
||||
if *field == FedInputField::PodcastFilter {
|
||||
filter = Some(input.to_string());
|
||||
}
|
||||
}
|
||||
Some(Popup::Edit { fields, focus, .. }) => {
|
||||
@@ -839,10 +825,8 @@ pub fn handle_paste(state: &mut AppState, runtime: &mut Runtime, pasted: &str) {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(search) = search
|
||||
&& let Some(effect) = crate::app::update::podcasts::set_search(state, search)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
if let Some(filter) = filter {
|
||||
crate::app::update::podcasts_set_filter(state, filter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1016,13 +1000,22 @@ fn save_edit(
|
||||
|
||||
fn handle_confirm_delete(
|
||||
state: &mut AppState,
|
||||
runtime: &Runtime,
|
||||
runtime: &mut Runtime,
|
||||
target: DeleteTarget,
|
||||
label: String,
|
||||
key: KeyEvent,
|
||||
) {
|
||||
match key.code {
|
||||
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {}
|
||||
KeyCode::Enter | KeyCode::Char('y') if matches!(target, DeleteTarget::Podcast(_)) => {
|
||||
if let DeleteTarget::Podcast(id) = target {
|
||||
super::perform_effect(
|
||||
state,
|
||||
runtime,
|
||||
crate::app::update::Effect::PodcastUnsubscribe(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
KeyCode::Enter | KeyCode::Char('y') => {
|
||||
let library = Arc::clone(&runtime.library);
|
||||
let result = match target {
|
||||
@@ -1036,6 +1029,7 @@ fn handle_confirm_delete(
|
||||
library.delete_playlist(id)
|
||||
}
|
||||
},
|
||||
DeleteTarget::Podcast(_) => unreachable!("handled by the guarded arm above"),
|
||||
};
|
||||
match result {
|
||||
Ok(()) => {
|
||||
@@ -1065,21 +1059,6 @@ fn handle_track_info(
|
||||
let len = tracks.len();
|
||||
match key.code {
|
||||
KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q') => {}
|
||||
// An episode's "artist" is its podcast title: never look it up as a
|
||||
// federated artist (that would send a private title to the
|
||||
// network); open the podcast instead.
|
||||
KeyCode::Char('a')
|
||||
if tracks
|
||||
.get(cursor.min(len.saturating_sub(1)))
|
||||
.is_some_and(TrackItem::is_podcast) =>
|
||||
{
|
||||
let track = tracks[cursor.min(len.saturating_sub(1))].clone();
|
||||
if let Some(effect) =
|
||||
crate::app::update::podcasts::reveal_episode_podcast(state, &track)
|
||||
{
|
||||
super::perform_effect(state, runtime, effect);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('a') => {
|
||||
let artists = tracks
|
||||
.get(cursor.min(len.saturating_sub(1)))
|
||||
@@ -1141,10 +1120,6 @@ fn handle_track_info(
|
||||
KeyCode::Char('c') => {
|
||||
if let Some(track) = tracks.get(cursor.min(len.saturating_sub(1))) {
|
||||
match crate::share::track_share_link(track) {
|
||||
None if track.is_podcast() => {
|
||||
state.status_message =
|
||||
Some("podcast episodes have no track share link".into())
|
||||
}
|
||||
Some(link) => {
|
||||
state.popup = Some(Popup::FedCopyText {
|
||||
title: "Track share link".into(),
|
||||
@@ -1174,7 +1149,7 @@ fn handle_track_info(
|
||||
cursor,
|
||||
scroll,
|
||||
});
|
||||
} else if track.is_podcast() || track.id < 0 || track.file_path.is_empty() {
|
||||
} else if track.id < 0 || track.file_path.is_empty() {
|
||||
state.status_message = Some("similarity search starts from a local track".into());
|
||||
state.popup = Some(Popup::TrackInfo {
|
||||
tracks,
|
||||
|
||||
+128
-192
@@ -338,7 +338,6 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
}],
|
||||
};
|
||||
let mut state = AppState::default();
|
||||
@@ -696,6 +695,8 @@ pub enum DeleteTarget {
|
||||
Release(i64),
|
||||
Artist(i64),
|
||||
Playlist(i64),
|
||||
/// Unsubscribe: drops the feed's stored metadata and cached cover.
|
||||
Podcast(i64),
|
||||
}
|
||||
|
||||
/// One text field of an edit form.
|
||||
@@ -763,11 +764,6 @@ pub enum Popup {
|
||||
},
|
||||
/// Delete confirmation; Enter/y deletes, Esc/n cancels.
|
||||
ConfirmDelete { target: DeleteTarget, label: String },
|
||||
/// Unsubscribe confirmation; progress and history are kept.
|
||||
ConfirmPodcastUnsubscribe {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
title: String,
|
||||
},
|
||||
/// Enabling network similarity reveals an embedding query to peers.
|
||||
SimilarityPrivacyConsent { enable_federation: bool },
|
||||
/// Derived data is safe to recreate but potentially expensive.
|
||||
@@ -905,10 +901,9 @@ pub enum FedInputField {
|
||||
DeviceName,
|
||||
ConnectInvite,
|
||||
JamInvite,
|
||||
/// Podcasts: RSS/Atom feed URL of a new subscription.
|
||||
PodcastFeedUrl,
|
||||
/// Podcasts: live search in the opened episode list.
|
||||
PodcastSearch,
|
||||
/// Live episode-title filter of the opened podcast.
|
||||
PodcastFilter,
|
||||
}
|
||||
|
||||
impl FedInputField {
|
||||
@@ -924,7 +919,7 @@ impl FedInputField {
|
||||
FedInputField::ConnectInvite => "Connect device (paste frid://i invite)",
|
||||
FedInputField::JamInvite => "Join Jam (paste frid://j invite)",
|
||||
FedInputField::PodcastFeedUrl => "Add podcast (RSS feed URL)",
|
||||
FedInputField::PodcastSearch => "Search episodes",
|
||||
FedInputField::PodcastFilter => "Search episodes by title",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -958,10 +953,10 @@ impl FedInputField {
|
||||
"Paste a frid://j capability to control the host player for this Jam only."
|
||||
}
|
||||
FedInputField::PodcastFeedUrl => {
|
||||
"Paste the podcast's RSS or Atom feed URL. Only episode information is stored; audio streams from the publisher when you play. New subscriptions are private."
|
||||
"Paste the podcast's RSS feed URL. Only the episode list is stored; audio streams when you play an episode."
|
||||
}
|
||||
FedInputField::PodcastSearch => {
|
||||
"Words from episode titles or descriptions; the list updates as you type. Enter keeps the search, Esc clears it."
|
||||
FedInputField::PodcastFilter => {
|
||||
"Type words from the episode title; the list updates as you type. Enter keeps the filter, Esc clears it."
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1400,7 +1395,7 @@ impl SimilaritySearchHit {
|
||||
Self::Local { track, .. } => track
|
||||
.content_id
|
||||
.clone()
|
||||
.unwrap_or_else(|| track.key().to_string()),
|
||||
.unwrap_or_else(|| format!("local:{}", track.id)),
|
||||
Self::Federated { track, .. } => track
|
||||
.content_id
|
||||
.clone()
|
||||
@@ -1485,22 +1480,21 @@ pub enum Tab {
|
||||
Global,
|
||||
Playlists,
|
||||
Queue,
|
||||
Podcasts,
|
||||
Federation,
|
||||
Logs,
|
||||
Podcasts,
|
||||
}
|
||||
|
||||
impl Tab {
|
||||
/// Display, Tab/Shift+Tab and `GoToTab` index order (`1`..`6`). Changing
|
||||
/// it changes what `GoToTab(n)` means in users' keymap files: bump
|
||||
/// `config::keymap::KEYMAP_VERSION` and remap older files in memory.
|
||||
/// Display and `GoToTab` index order. Podcasts is appended so the
|
||||
/// existing number keys (and user keymaps) keep their meaning.
|
||||
pub const ALL: [Tab; 6] = [
|
||||
Tab::Global,
|
||||
Tab::Playlists,
|
||||
Tab::Queue,
|
||||
Tab::Podcasts,
|
||||
Tab::Federation,
|
||||
Tab::Logs,
|
||||
Tab::Podcasts,
|
||||
];
|
||||
|
||||
pub fn title(self) -> &'static str {
|
||||
@@ -1508,21 +1502,9 @@ impl Tab {
|
||||
Tab::Global => "Library",
|
||||
Tab::Playlists => "Playlists",
|
||||
Tab::Queue => "Queue",
|
||||
Tab::Podcasts => "Podcasts",
|
||||
Tab::Federation => "Settings",
|
||||
Tab::Logs => "Logs",
|
||||
}
|
||||
}
|
||||
|
||||
/// Abbreviated title for the tab bar on narrow terminals.
|
||||
pub fn short_title(self) -> &'static str {
|
||||
match self {
|
||||
Tab::Global => "Lib",
|
||||
Tab::Playlists => "Lists",
|
||||
Tab::Queue => "Queue",
|
||||
Tab::Podcasts => "Pods",
|
||||
Tab::Federation => "Set",
|
||||
Tab::Logs => "Logs",
|
||||
Tab::Podcasts => "Podcasts",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1547,9 +1529,9 @@ impl Tab {
|
||||
Tab::Global => KeyContext::Library,
|
||||
Tab::Playlists => KeyContext::Playlists,
|
||||
Tab::Queue => KeyContext::Queue,
|
||||
Tab::Podcasts => KeyContext::Podcasts,
|
||||
Tab::Federation => KeyContext::Federation,
|
||||
Tab::Logs => KeyContext::Logs,
|
||||
Tab::Podcasts => KeyContext::Podcasts,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1683,131 +1665,82 @@ impl DevicePlaybackState {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Podcasts tab
|
||||
// ---------------------------------------------------------------------------
|
||||
/// Podcasts tab: subscriptions list, or the episode catalog of one opened
|
||||
/// podcast. Only RSS metadata lives here; audio streams on demand.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PodcastsTab {
|
||||
/// Subscriptions; `None` until the first load is requested.
|
||||
pub list: Option<Loadable<Vec<crate::podcasts::Podcast>>>,
|
||||
/// Cursor over `podcast_list_rows` (action rows first, then podcasts).
|
||||
pub selected: usize,
|
||||
pub opened: Option<OpenedPodcast>,
|
||||
/// Episodes of the opened podcast, keyed by its id so a late result for
|
||||
/// a podcast that is no longer open is ignored.
|
||||
pub episodes: Option<(i64, Loadable<Vec<crate::podcasts::Episode>>)>,
|
||||
/// Case-insensitive episode title filter; every word must match.
|
||||
pub filter: String,
|
||||
pub sort: EpisodeSort,
|
||||
/// Feeds with a refresh in flight (spinner + dedup of requests).
|
||||
pub refreshing: HashSet<i64>,
|
||||
pub subscribing: bool,
|
||||
/// Wall-clock seconds of the last automatic refresh sweep.
|
||||
pub last_auto_refresh: Option<i64>,
|
||||
/// Newly subscribed podcast to select once the reloaded list arrives.
|
||||
pub pending_select: Option<i64>,
|
||||
}
|
||||
|
||||
/// Episodes loaded per page; the next page loads when the cursor nears the end.
|
||||
pub const PODCAST_EPISODE_PAGE: usize = 500;
|
||||
/// "Add podcast" and "Check all" rows above the subscriptions.
|
||||
/// "Add podcast" and "Check for new episodes" rows above the podcasts.
|
||||
pub const PODCAST_ACTION_ROWS: usize = 2;
|
||||
|
||||
/// State of the podcast store (`podcasts.sqlite3`).
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub enum PodcastStoreStatus {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OpenedPodcast {
|
||||
pub id: i64,
|
||||
/// Index into `visible_episode_indices`.
|
||||
pub cursor: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum EpisodeSort {
|
||||
#[default]
|
||||
Opening,
|
||||
Ready,
|
||||
/// The store could not be opened (newer schema, foreign file, I/O). The
|
||||
/// file was left unchanged; only the Podcasts tab is unavailable.
|
||||
Failed(String),
|
||||
NewestFirst,
|
||||
OldestFirst,
|
||||
}
|
||||
|
||||
/// A subscribed podcast with its local fetch state.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PodcastRow {
|
||||
pub summary: furumi_library::podcasts::PodcastSummary,
|
||||
pub fetch: furumi_library::podcasts::FetchState,
|
||||
}
|
||||
|
||||
impl PodcastRow {
|
||||
pub fn id(&self) -> &music_dht::podcast::PodcastId {
|
||||
&self.summary.podcast_id
|
||||
impl EpisodeSort {
|
||||
pub fn toggle(self) -> Self {
|
||||
match self {
|
||||
EpisodeSort::NewestFirst => EpisodeSort::OldestFirst,
|
||||
EpisodeSort::OldestFirst => EpisodeSort::NewestFirst,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &str {
|
||||
&self.summary.meta.title
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
EpisodeSort::NewestFirst => "newest first",
|
||||
EpisodeSort::OldestFirst => "oldest first",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One row of the subscriptions list.
|
||||
/// One row of the podcast subscriptions list.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PodcastListRow {
|
||||
Add,
|
||||
RefreshAll,
|
||||
/// Index into the loaded podcasts.
|
||||
/// Index into the loaded podcast list.
|
||||
Podcast(usize),
|
||||
}
|
||||
|
||||
/// One row of a podcast's season list.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SeasonRow {
|
||||
All,
|
||||
/// Index into the loaded seasons.
|
||||
Group(usize),
|
||||
}
|
||||
|
||||
/// The podcast opened from the list: its season list (when the feed has
|
||||
/// seasons) and the episode list of the chosen group.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct OpenedPodcast {
|
||||
pub podcast_id: music_dht::podcast::PodcastId,
|
||||
/// Season list cursor; `None` when the podcast has no seasons and its
|
||||
/// episodes open directly.
|
||||
pub season_cursor: Option<usize>,
|
||||
/// The episode group shown; `None` while the season list is shown.
|
||||
pub season: Option<furumi_library::podcasts::SeasonFilter>,
|
||||
pub episode_cursor: usize,
|
||||
}
|
||||
|
||||
/// The episode list of the opened group, loaded page by page.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EpisodeList {
|
||||
/// Request sequence: results of older requests are dropped.
|
||||
pub seq: u64,
|
||||
/// Query of the first page (`offset` 0, `limit` one page).
|
||||
pub query: furumi_library::podcasts::EpisodeQuery,
|
||||
pub items: Vec<furumi_library::podcasts::EpisodeListItem>,
|
||||
/// The first page of `query` is in flight (`items` may still hold the
|
||||
/// previous result, so the list does not flash empty).
|
||||
pub loading: bool,
|
||||
pub loading_more: bool,
|
||||
/// Every matching episode is loaded.
|
||||
pub complete: bool,
|
||||
pub error: Option<String>,
|
||||
/// Episode to keep the cursor on when the first page arrives.
|
||||
pub focus: Option<music_dht::podcast::EpisodeId>,
|
||||
}
|
||||
|
||||
/// Podcasts tab: subscriptions, seasons and episodes from the podcast store.
|
||||
/// Nothing here touches SQLite or the network; see `app::podcast_runtime`.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PodcastsTab {
|
||||
pub store: PodcastStoreStatus,
|
||||
/// Subscribed podcasts; `None` until the first load.
|
||||
pub list: Option<Loadable<Vec<PodcastRow>>>,
|
||||
/// Cursor over [`PodcastsTab::list_rows`] (action rows first).
|
||||
pub selected: usize,
|
||||
pub opened: Option<OpenedPodcast>,
|
||||
/// Season groups of the opened podcast.
|
||||
pub seasons: Option<(
|
||||
music_dht::podcast::PodcastId,
|
||||
Loadable<Vec<furumi_library::podcasts::SeasonSummary>>,
|
||||
)>,
|
||||
pub episodes: Option<EpisodeList>,
|
||||
/// Episode search text (titles and descriptions).
|
||||
pub search: String,
|
||||
pub played: furumi_library::podcasts::PlayedFilter,
|
||||
pub sort: furumi_library::podcasts::EpisodeSort,
|
||||
/// Feeds with a refresh in flight (spinner and request de-duplication).
|
||||
pub refreshing: HashSet<music_dht::podcast::PodcastId>,
|
||||
pub subscribing: bool,
|
||||
/// Podcast to select once the reloaded list arrives.
|
||||
pub pending_select: Option<music_dht::podcast::PodcastId>,
|
||||
/// Last episode request sequence handed out.
|
||||
pub episode_seq: u64,
|
||||
}
|
||||
|
||||
impl PodcastsTab {
|
||||
pub fn podcasts(&self) -> &[PodcastRow] {
|
||||
pub fn podcasts(&self) -> &[crate::podcasts::Podcast] {
|
||||
match &self.list {
|
||||
Some(Loadable::Ready(list)) => list,
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn podcast(&self, id: &music_dht::podcast::PodcastId) -> Option<&PodcastRow> {
|
||||
self.podcasts().iter().find(|row| row.id() == id)
|
||||
pub fn podcast(&self, id: i64) -> Option<&crate::podcasts::Podcast> {
|
||||
self.podcasts().iter().find(|podcast| podcast.id == id)
|
||||
}
|
||||
|
||||
pub fn list_rows(&self) -> Vec<PodcastListRow> {
|
||||
@@ -1816,10 +1749,10 @@ impl PodcastsTab {
|
||||
rows
|
||||
}
|
||||
|
||||
/// The opened podcast, or the one under the list cursor.
|
||||
pub fn current_podcast(&self) -> Option<&PodcastRow> {
|
||||
if let Some(opened) = &self.opened {
|
||||
return self.podcast(&opened.podcast_id);
|
||||
/// The podcast under the list cursor (list view) or the opened one.
|
||||
pub fn current_podcast(&self) -> Option<&crate::podcasts::Podcast> {
|
||||
if let Some(opened) = self.opened {
|
||||
return self.podcast(opened.id);
|
||||
}
|
||||
match self.list_rows().get(self.selected)? {
|
||||
PodcastListRow::Podcast(index) => self.podcasts().get(*index),
|
||||
@@ -1827,51 +1760,72 @@ impl PodcastsTab {
|
||||
}
|
||||
}
|
||||
|
||||
/// Season groups of the opened podcast, when loaded.
|
||||
pub fn opened_seasons(&self) -> Option<&[furumi_library::podcasts::SeasonSummary]> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
match &self.seasons {
|
||||
Some((id, Loadable::Ready(seasons))) if *id == opened.podcast_id => Some(seasons),
|
||||
/// Episodes of the opened podcast, when loaded.
|
||||
pub fn opened_episodes(&self) -> Option<&[crate::podcasts::Episode]> {
|
||||
let opened = self.opened?;
|
||||
match &self.episodes {
|
||||
Some((id, Loadable::Ready(episodes))) if *id == opened.id => Some(episodes),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn season_rows(&self) -> Vec<SeasonRow> {
|
||||
let mut rows = vec![SeasonRow::All];
|
||||
rows.extend((0..self.opened_seasons().map_or(0, <[_]>::len)).map(SeasonRow::Group));
|
||||
rows
|
||||
/// Indices into `opened_episodes()` after the title filter and sort.
|
||||
pub fn visible_episode_indices(&self) -> Vec<usize> {
|
||||
let Some(episodes) = self.opened_episodes() else {
|
||||
return Vec::new();
|
||||
};
|
||||
visible_episode_indices(episodes, &self.filter, self.sort)
|
||||
}
|
||||
|
||||
/// Whether the episode list (rather than the season list) is shown.
|
||||
pub fn showing_episodes(&self) -> bool {
|
||||
self.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.season.is_some())
|
||||
pub fn selected_episode(&self) -> Option<&crate::podcasts::Episode> {
|
||||
let opened = self.opened?;
|
||||
let episodes = self.opened_episodes()?;
|
||||
let visible = visible_episode_indices(episodes, &self.filter, self.sort);
|
||||
visible
|
||||
.get(opened.cursor.min(visible.len().saturating_sub(1)))
|
||||
.and_then(|&index| episodes.get(index))
|
||||
}
|
||||
}
|
||||
|
||||
/// Episodes of the opened group, when they belong to it.
|
||||
pub fn opened_episodes(&self) -> Option<&EpisodeList> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
let season = opened.season?;
|
||||
self.episodes.as_ref().filter(|list| {
|
||||
list.query.podcast_id == opened.podcast_id && list.query.season == season
|
||||
/// Filter by title (every whitespace-separated term, case-insensitively)
|
||||
/// and order by publication date; undated episodes always go last.
|
||||
pub fn visible_episode_indices(
|
||||
episodes: &[crate::podcasts::Episode],
|
||||
filter: &str,
|
||||
sort: EpisodeSort,
|
||||
) -> Vec<usize> {
|
||||
let terms: Vec<String> = filter.split_whitespace().map(str::to_lowercase).collect();
|
||||
let mut indices: Vec<usize> = episodes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, episode)| {
|
||||
if terms.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let title = episode.title.to_lowercase();
|
||||
terms.iter().all(|term| title.contains(term.as_str()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn selected_episode(&self) -> Option<&furumi_library::podcasts::EpisodeListItem> {
|
||||
let opened = self.opened.as_ref()?;
|
||||
let list = self.opened_episodes()?;
|
||||
list.items.get(opened.episode_cursor)
|
||||
}
|
||||
|
||||
/// The selected episode as a queue item (explicit podcast source).
|
||||
pub fn selected_episode_track(&self) -> Option<TrackItem> {
|
||||
let item = self.selected_episode()?;
|
||||
let row = self.podcast(&item.episode.podcast_id)?;
|
||||
let mut episode = item.episode.to_ref(&row.summary.meta);
|
||||
episode.progress_generation = item.progress.as_ref().map(|record| record.generation);
|
||||
Some(TrackItem::podcast_episode(episode))
|
||||
}
|
||||
.map(|(index, _)| index)
|
||||
.collect();
|
||||
indices.sort_by(|&a, &b| {
|
||||
let (a_ep, b_ep) = (&episodes[a], &episodes[b]);
|
||||
match (a_ep.published_at, b_ep.published_at) {
|
||||
(Some(x), Some(y)) => match sort {
|
||||
EpisodeSort::NewestFirst => y.cmp(&x),
|
||||
EpisodeSort::OldestFirst => x.cmp(&y),
|
||||
},
|
||||
(Some(_), None) => std::cmp::Ordering::Less,
|
||||
(None, Some(_)) => std::cmp::Ordering::Greater,
|
||||
(None, None) => std::cmp::Ordering::Equal,
|
||||
}
|
||||
// Stable sort keeps the store order (newest id first) for ties;
|
||||
// reverse it for oldest-first so ties follow the same direction.
|
||||
.then_with(|| match sort {
|
||||
EpisodeSort::NewestFirst => a.cmp(&b),
|
||||
EpisodeSort::OldestFirst => b.cmp(&a),
|
||||
})
|
||||
});
|
||||
indices
|
||||
}
|
||||
|
||||
/// Single source of truth for the UI. Mutated only by `update()` and the
|
||||
@@ -2391,13 +2345,7 @@ fn fed_release_slot_availability(
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio content id of a music track (library or federated). Podcast
|
||||
/// episodes have none: they are never content-addressed, resolved by hash
|
||||
/// or fetched from peers.
|
||||
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||
if track.is_podcast() {
|
||||
return None;
|
||||
}
|
||||
track
|
||||
.content_id
|
||||
.as_deref()
|
||||
@@ -2411,24 +2359,12 @@ pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Identity of a queue item for matching it across queue edits, shuffle,
|
||||
/// resolution and playback state.
|
||||
///
|
||||
/// Podcast episodes are keyed by their episode id only (`ep:…`, from
|
||||
/// `TrackItem::key`): all of them share the sentinel numeric id, which must
|
||||
/// never identify anything. Music is keyed content-first (a pending
|
||||
/// federated item and its library copy are the same track), then by its
|
||||
/// federated source, then by `TrackItem::key` (`lib:<row id>` for library
|
||||
/// rows, `tmp:<id>` for process-local placeholders).
|
||||
pub fn track_key(track: &TrackItem) -> String {
|
||||
if track.is_podcast() {
|
||||
return track.key().to_string();
|
||||
}
|
||||
if let Some(content_id) = track_content_id(track) {
|
||||
return format!("content:{content_id}");
|
||||
}
|
||||
if let Some(fed) = &track.fed {
|
||||
return format!("fed:{}:{}", fed.owner, fed.item_id);
|
||||
}
|
||||
track.key().to_string()
|
||||
format!("local:{}", track.id)
|
||||
}
|
||||
+247
-130
@@ -20,12 +20,8 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit";
|
||||
pub enum Effect {
|
||||
CheckUpdate,
|
||||
InstallUpdate(crate::updater::Update),
|
||||
/// (Re)start playback of `queue[queue_pos]` (a podcast episode resumes
|
||||
/// from its saved position).
|
||||
/// (Re)start playback of `queue[queue_pos]`.
|
||||
PlayCurrent,
|
||||
/// Play `queue[queue_pos]`, a podcast episode, from the beginning as an
|
||||
/// explicit "start over".
|
||||
PlayCurrentFromStart,
|
||||
TogglePause,
|
||||
StopPlayback,
|
||||
/// Seek relative to the current position, in seconds.
|
||||
@@ -110,32 +106,13 @@ pub enum Effect {
|
||||
},
|
||||
/// Load qualified listening history without blocking the UI thread.
|
||||
LoadListenHistory,
|
||||
/// Reload the subscribed podcasts with their fetch state.
|
||||
PodcastsReload,
|
||||
/// Load the season groups of a podcast.
|
||||
PodcastLoadSeasons(music_dht::podcast::PodcastId),
|
||||
/// Load one page of episodes; `seq` drops stale results.
|
||||
PodcastLoadEpisodes {
|
||||
seq: u64,
|
||||
query: furumi_library::podcasts::EpisodeQuery,
|
||||
},
|
||||
/// Fetch a feed the user typed and subscribe to it.
|
||||
/// Load the episode catalog of one podcast (and refresh it when stale).
|
||||
PodcastLoadEpisodes(i64),
|
||||
/// Fetch an RSS feed and store it as a new subscription.
|
||||
PodcastSubscribe(String),
|
||||
/// Check feeds for new episodes (`manual`: asked by the user).
|
||||
PodcastRefresh {
|
||||
targets: Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>,
|
||||
manual: bool,
|
||||
},
|
||||
/// Unsubscribe; progress and history are kept.
|
||||
PodcastUnsubscribe {
|
||||
podcast_id: music_dht::podcast::PodcastId,
|
||||
title: String,
|
||||
},
|
||||
/// Mark an episode played or unplayed (an explicit progress action).
|
||||
PodcastSetPlayed {
|
||||
episode_id: music_dht::podcast::EpisodeId,
|
||||
played: bool,
|
||||
},
|
||||
/// Check these subscriptions for new episodes.
|
||||
PodcastRefresh(Vec<i64>),
|
||||
PodcastUnsubscribe(i64),
|
||||
}
|
||||
|
||||
pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
@@ -145,14 +122,6 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
.take()
|
||||
.is_some_and(|deadline| Instant::now() <= deadline);
|
||||
state.status_message = None;
|
||||
if state.active_tab == Tab::Podcasts
|
||||
&& !state.help_visible
|
||||
&& !state.visualizer.active
|
||||
&& !state.additional_settings_open
|
||||
&& let Some(effect) = podcasts::handle(state, &action)
|
||||
{
|
||||
return effect;
|
||||
}
|
||||
if state.additional_settings_open
|
||||
&& !matches!(
|
||||
action,
|
||||
@@ -321,6 +290,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
state.logs.selected_seq = None;
|
||||
state.logs.follow = true;
|
||||
}
|
||||
Tab::Podcasts => podcasts_toggle_sort(state),
|
||||
_ => {}
|
||||
}
|
||||
None
|
||||
@@ -349,6 +319,15 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
state.cmdline.begin_history_navigation();
|
||||
None
|
||||
}
|
||||
Action::OpenSearch
|
||||
if state.active_tab == Tab::Podcasts && state.podcasts.opened.is_some() =>
|
||||
{
|
||||
state.popup = Some(super::state::Popup::FedInput {
|
||||
field: super::state::FedInputField::PodcastFilter,
|
||||
input: crate::app::input::LineEdit::new(state.podcasts.filter.clone()),
|
||||
});
|
||||
None
|
||||
}
|
||||
Action::OpenSearch => {
|
||||
// The command line opens pre-filled with "/": typing continues
|
||||
// the live search, exactly as if `:` then `/` were pressed.
|
||||
@@ -390,14 +369,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
let mut local_tracks: Vec<TrackItem> = Vec::new();
|
||||
let mut fed_tracks: Vec<crate::federation::FedTrack> = Vec::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut episodes = 0usize;
|
||||
for track in tracks {
|
||||
// Likes are music library state; episodes have their own
|
||||
// "played" progress instead.
|
||||
if track.is_podcast() {
|
||||
episodes += 1;
|
||||
continue;
|
||||
}
|
||||
if !seen.insert(track_key(&track)) {
|
||||
continue;
|
||||
}
|
||||
@@ -410,11 +382,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
}
|
||||
}
|
||||
if local_tracks.is_empty() && fed_tracks.is_empty() {
|
||||
state.status_message = Some(if episodes > 0 {
|
||||
PODCAST_NOT_MUSIC.into()
|
||||
} else {
|
||||
"no track selected".into()
|
||||
});
|
||||
state.status_message = Some("no track selected".into());
|
||||
None
|
||||
} else {
|
||||
let should_like = local_tracks.iter().any(|track| !state.track_liked(track))
|
||||
@@ -460,9 +428,6 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
Action::GoToRelease => {
|
||||
let track = selected_track(state).or_else(|| state.player.current.clone());
|
||||
match track {
|
||||
Some(track) if track.is_podcast() => {
|
||||
return podcasts::reveal_episode_podcast(state, &track);
|
||||
}
|
||||
Some(track) => open_release_for_track(state, &track),
|
||||
None => state.status_message = Some("no track selected".into()),
|
||||
}
|
||||
@@ -470,17 +435,16 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
}
|
||||
Action::AddToPlaylist => {
|
||||
let target = selected_playlist_target(state, true);
|
||||
let target = without_podcast_episodes(state, target);
|
||||
match target {
|
||||
Some(target) => {
|
||||
state.popup = Some(super::state::Popup::AddToPlaylist { target, cursor: 0 });
|
||||
state.track_selection.clear();
|
||||
}
|
||||
None => {
|
||||
state
|
||||
.status_message
|
||||
.get_or_insert_with(|| "no track selected".into());
|
||||
None if state.active_tab == Tab::Podcasts => {
|
||||
state.status_message =
|
||||
Some("podcast episodes cannot be added to playlists".into());
|
||||
}
|
||||
None => state.status_message = Some("no track selected".into()),
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -499,8 +463,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
}
|
||||
}
|
||||
Action::NewPlaylist => {
|
||||
let for_target =
|
||||
without_podcast_episodes(state, selected_playlist_target(state, false));
|
||||
let for_target = selected_playlist_target(state, false);
|
||||
state.popup = Some(super::state::Popup::NewPlaylist {
|
||||
for_target,
|
||||
input: crate::app::input::LineEdit::default(),
|
||||
@@ -528,26 +491,16 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
|
||||
None
|
||||
}
|
||||
}
|
||||
Action::EditSelected if state.active_tab == Tab::Podcasts => {
|
||||
state.status_message = Some("podcast metadata comes from the RSS feed".into());
|
||||
None
|
||||
}
|
||||
Action::EditSelected => {
|
||||
open_edit_popup(state);
|
||||
None
|
||||
}
|
||||
Action::DeleteSelected => delete_selected(state),
|
||||
Action::RestartEpisode => podcasts::restart_current(state),
|
||||
Action::AddPodcast
|
||||
| Action::SearchEpisodes
|
||||
| Action::RefreshPodcast
|
||||
| Action::RefreshAllPodcasts
|
||||
| Action::CycleEpisodeSort
|
||||
| Action::CycleEpisodeFilter
|
||||
| Action::ToggleEpisodePlayed
|
||||
| Action::UnsubscribePodcast => {
|
||||
state.status_message = Some(format!(
|
||||
"open the Podcasts tab ({}) for this command",
|
||||
Tab::Podcasts.index() + 1
|
||||
));
|
||||
None
|
||||
}
|
||||
Action::RefreshPodcasts => podcasts_refresh(state),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,9 +515,7 @@ fn current_track_for_info(state: &AppState) -> Option<TrackItem> {
|
||||
queued_at_position
|
||||
.filter(|queued| {
|
||||
super::state::track_key(queued) == current_key
|
||||
// Podcast episodes share the sentinel id; only their key
|
||||
// identifies them.
|
||||
|| (!current.is_podcast() && !queued.is_podcast() && queued.id == current.id)
|
||||
|| queued.id == current.id
|
||||
|| current.is_fed_pending()
|
||||
})
|
||||
.or_else(|| {
|
||||
@@ -617,36 +568,6 @@ fn open_track_info(
|
||||
}
|
||||
}
|
||||
|
||||
/// Status text when a music-only action meets a podcast episode.
|
||||
pub const PODCAST_NOT_MUSIC: &str =
|
||||
"podcast episodes are not music: likes, playlists and library actions do not apply";
|
||||
|
||||
/// Playlists hold music only: drops podcast episodes from a local target
|
||||
/// (and says so when nothing is left).
|
||||
fn without_podcast_episodes(
|
||||
state: &mut AppState,
|
||||
target: Option<super::state::PlaylistAddTarget>,
|
||||
) -> Option<super::state::PlaylistAddTarget> {
|
||||
use super::state::PlaylistAddTarget;
|
||||
match target {
|
||||
Some(PlaylistAddTarget::Local(tracks)) => {
|
||||
let total = tracks.len();
|
||||
let music: Vec<TrackItem> = tracks
|
||||
.into_iter()
|
||||
.filter(|track| !track.is_podcast())
|
||||
.collect();
|
||||
if music.is_empty() {
|
||||
if total > 0 {
|
||||
state.status_message = Some(PODCAST_NOT_MUSIC.into());
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Some(PlaylistAddTarget::Local(music))
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
fn selected_playlist_target(
|
||||
state: &AppState,
|
||||
include_current: bool,
|
||||
@@ -656,14 +577,22 @@ fn selected_playlist_target(
|
||||
return Some(super::state::PlaylistAddTarget::Fed(fed));
|
||||
}
|
||||
|
||||
let local = selected_tracks(state);
|
||||
// Playlists hold library rows; streamed podcast episodes have none.
|
||||
let local: Vec<TrackItem> = selected_tracks(state)
|
||||
.into_iter()
|
||||
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
|
||||
.collect();
|
||||
if !local.is_empty() {
|
||||
return Some(super::state::PlaylistAddTarget::Local(local));
|
||||
}
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
return None;
|
||||
}
|
||||
|
||||
include_current
|
||||
.then(|| state.player.current.clone())
|
||||
.flatten()
|
||||
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
|
||||
.map(|track| super::state::PlaylistAddTarget::Local(vec![track]))
|
||||
}
|
||||
|
||||
@@ -734,10 +663,6 @@ fn open_edit_popup(state: &mut AppState) {
|
||||
return;
|
||||
}
|
||||
if let Some(track) = selected_track(state).or_else(|| state.player.current.clone()) {
|
||||
if track.is_podcast() {
|
||||
state.status_message = Some("podcast details come from the RSS feed".into());
|
||||
return;
|
||||
}
|
||||
state.popup = Some(track_edit_popup(&track));
|
||||
return;
|
||||
}
|
||||
@@ -800,6 +725,9 @@ fn delete_selected(state: &mut AppState) -> Option<Effect> {
|
||||
if state.active_tab == Tab::Queue {
|
||||
return remove_selected_from_queue(state);
|
||||
}
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
return podcasts_confirm_unsubscribe(state);
|
||||
}
|
||||
if state.active_tab == Tab::Playlists {
|
||||
match state.playlists.opened {
|
||||
None => {
|
||||
@@ -1125,7 +1053,7 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
|
||||
state.queue_tab.cursor,
|
||||
state.player.queue.len(),
|
||||
)),
|
||||
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1179,11 +1107,14 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
|
||||
state.queue_tab.cursor,
|
||||
state.player.queue.clone(),
|
||||
)),
|
||||
Tab::Podcasts | Tab::Federation | Tab::Logs => None,
|
||||
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected_tracks(state: &AppState) -> Vec<TrackItem> {
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
return selected_episode_track(state).into_iter().collect();
|
||||
}
|
||||
if state.active_tab == Tab::Global
|
||||
&& state.search.similarity_source.is_some()
|
||||
&& let Some(GlobalView::Search { cursor }) = state.global.stack.last()
|
||||
@@ -1480,8 +1411,7 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
|
||||
.map(|track| (*track).clone())
|
||||
}
|
||||
Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(),
|
||||
Tab::Podcasts => state.podcasts.selected_episode_track(),
|
||||
Tab::Federation | Tab::Logs => None,
|
||||
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1500,14 +1430,8 @@ pub fn playlist_tracks(state: &AppState, id: i64) -> Option<Vec<&TrackItem>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the library source mode lets a queue item stay in a received
|
||||
/// queue. Only pending federated music depends on it: podcast episodes
|
||||
/// (streamed from their publisher) and Jam private placeholders are never
|
||||
/// dropped, or a received queue would lose them.
|
||||
pub(crate) fn track_allowed_by_source_mode(state: &AppState, track: &TrackItem) -> bool {
|
||||
track.is_podcast()
|
||||
|| state.global.filters.source_mode.includes_network()
|
||||
|| !track.is_fed_pending()
|
||||
state.global.filters.source_mode.includes_network() || !track.is_fed_pending()
|
||||
}
|
||||
|
||||
/// A *release* under the cursor (artist-view tile/row or a search release).
|
||||
@@ -1543,9 +1467,6 @@ fn selected_release_id(state: &AppState) -> Option<i64> {
|
||||
/// an async fetch effect.
|
||||
fn queue_add(state: &mut AppState, next: bool) -> Option<Effect> {
|
||||
let tracks = selected_tracks(state);
|
||||
if tracks.iter().any(TrackItem::is_podcast) && !podcasts::episodes_play_here(state) {
|
||||
return None;
|
||||
}
|
||||
if !tracks.is_empty() {
|
||||
let count = tracks.len();
|
||||
let title = tracks[0].title.clone();
|
||||
@@ -1894,6 +1815,10 @@ fn page_step(state: &AppState) -> isize {
|
||||
}
|
||||
|
||||
fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
podcasts_move(state, dy);
|
||||
return;
|
||||
}
|
||||
if state.active_tab == Tab::Federation {
|
||||
if dy != 0 {
|
||||
let last = settings_rows(state).len() as isize - 1;
|
||||
@@ -2288,6 +2213,10 @@ fn jump_selection(state: &mut AppState, first: bool) {
|
||||
state.settings_cursor = if first { 0 } else { last };
|
||||
return;
|
||||
}
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
podcasts_move(state, if first { isize::MIN } else { isize::MAX });
|
||||
return;
|
||||
}
|
||||
if state.active_tab == Tab::Logs {
|
||||
if first {
|
||||
let level = super::state::LOG_LEVELS[state.logs.level_index];
|
||||
@@ -2378,6 +2307,9 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
|
||||
if state.active_tab == Tab::Federation {
|
||||
return federation_select(state);
|
||||
}
|
||||
if state.active_tab == Tab::Podcasts {
|
||||
return podcasts_select(state);
|
||||
}
|
||||
// Queue: jump playback to the track under the cursor. Earlier tracks
|
||||
// stay in the queue as "played"; picking one of them just moves the
|
||||
// playing position back.
|
||||
@@ -3316,6 +3248,7 @@ fn go_back(state: &mut AppState) {
|
||||
Tab::Playlists => {
|
||||
state.playlists.opened = None;
|
||||
}
|
||||
Tab::Podcasts => podcasts_close(state),
|
||||
Tab::Global => {
|
||||
// Esc on a view opened by Shift-J from another tab goes back to
|
||||
// that tab, not down the Global stack.
|
||||
@@ -3370,16 +3303,200 @@ fn reset_tab(state: &mut AppState, tab: Tab) {
|
||||
state.logs.selected_seq = None;
|
||||
}
|
||||
Tab::Queue => {}
|
||||
Tab::Podcasts => podcasts::close(state),
|
||||
Tab::Podcasts => podcasts_close(state),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Podcasts tab
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// How long a feed counts as fresh: opening it or the periodic sweep only
|
||||
/// re-fetches feeds checked longer ago than this.
|
||||
pub const PODCAST_STALE_SECS: i64 = 30 * 60;
|
||||
|
||||
fn podcasts_move(state: &mut AppState, dy: isize) {
|
||||
let tab = &mut state.podcasts;
|
||||
let (cursor, len) = match tab.opened {
|
||||
Some(opened) => (opened.cursor, tab.visible_episode_indices().len()),
|
||||
None => (tab.selected, tab.list_rows().len()),
|
||||
};
|
||||
if len == 0 {
|
||||
return;
|
||||
}
|
||||
let target = (cursor as isize)
|
||||
.saturating_add(dy)
|
||||
.clamp(0, len as isize - 1) as usize;
|
||||
match &mut tab.opened {
|
||||
Some(opened) => opened.cursor = target,
|
||||
None => tab.selected = target,
|
||||
}
|
||||
}
|
||||
|
||||
fn podcasts_toggle_sort(state: &mut AppState) {
|
||||
let tab = &mut state.podcasts;
|
||||
tab.sort = tab.sort.toggle();
|
||||
if let Some(opened) = &mut tab.opened {
|
||||
opened.cursor = 0;
|
||||
}
|
||||
state.status_message = Some(format!("episodes: {}", state.podcasts.sort.label()));
|
||||
}
|
||||
|
||||
/// Esc: episode catalog → subscriptions list. The filter belongs to the
|
||||
/// catalog it was typed in, so it is dropped as well.
|
||||
fn podcasts_close(state: &mut AppState) {
|
||||
let tab = &mut state.podcasts;
|
||||
if let Some(opened) = tab.opened.take()
|
||||
&& let Some(index) = tab.podcasts().iter().position(|p| p.id == opened.id)
|
||||
{
|
||||
tab.selected = index + super::state::PODCAST_ACTION_ROWS;
|
||||
}
|
||||
tab.filter.clear();
|
||||
}
|
||||
|
||||
/// Called whenever the filter text changes: keep the cursor on the first
|
||||
/// match so the list does not appear empty below a stale cursor.
|
||||
pub fn podcasts_set_filter(state: &mut AppState, filter: String) {
|
||||
// Caret movement and Enter on an unchanged filter keep the cursor.
|
||||
if state.podcasts.filter == filter {
|
||||
return;
|
||||
}
|
||||
state.podcasts.filter = filter;
|
||||
if let Some(opened) = &mut state.podcasts.opened {
|
||||
opened.cursor = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn podcasts_select(state: &mut AppState) -> Option<Effect> {
|
||||
use super::state::PodcastListRow;
|
||||
|
||||
if state.podcasts.opened.is_some() {
|
||||
return play_selected_episode(state);
|
||||
}
|
||||
match state
|
||||
.podcasts
|
||||
.list_rows()
|
||||
.get(state.podcasts.selected)
|
||||
.copied()?
|
||||
{
|
||||
PodcastListRow::Add => {
|
||||
if state.podcasts.subscribing {
|
||||
state.status_message =
|
||||
Some("podcasts: a subscription is already being added".into());
|
||||
return None;
|
||||
}
|
||||
state.popup = Some(super::state::Popup::FedInput {
|
||||
field: super::state::FedInputField::PodcastFeedUrl,
|
||||
input: crate::app::input::LineEdit::default(),
|
||||
});
|
||||
None
|
||||
}
|
||||
PodcastListRow::RefreshAll => podcasts_refresh(state),
|
||||
PodcastListRow::Podcast(index) => {
|
||||
let id = state.podcasts.podcasts().get(index)?.id;
|
||||
state.podcasts.opened = Some(super::state::OpenedPodcast { id, cursor: 0 });
|
||||
state.podcasts.filter.clear();
|
||||
if !matches!(&state.podcasts.episodes, Some((loaded, Loadable::Ready(_))) if *loaded == id)
|
||||
{
|
||||
state.podcasts.episodes = Some((id, Loadable::Loading));
|
||||
}
|
||||
Some(Effect::PodcastLoadEpisodes(id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shift-R: the opened podcast, or every subscription from the list.
|
||||
fn podcasts_refresh(state: &mut AppState) -> Option<Effect> {
|
||||
if state.active_tab != Tab::Podcasts {
|
||||
return None;
|
||||
}
|
||||
let ids: Vec<i64> = match state.podcasts.opened {
|
||||
Some(opened) => vec![opened.id],
|
||||
None => state.podcasts.podcasts().iter().map(|p| p.id).collect(),
|
||||
};
|
||||
let ids: Vec<i64> = ids
|
||||
.into_iter()
|
||||
.filter(|id| !state.podcasts.refreshing.contains(id))
|
||||
.collect();
|
||||
if ids.is_empty() {
|
||||
state.status_message = Some(if state.podcasts.podcasts().is_empty() {
|
||||
"podcasts: no subscriptions yet".into()
|
||||
} else {
|
||||
"podcasts: already checking for new episodes".into()
|
||||
});
|
||||
return None;
|
||||
}
|
||||
state.podcasts.refreshing.extend(ids.iter().copied());
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: checking {} feed(s) for new episodes…",
|
||||
ids.len()
|
||||
));
|
||||
Some(Effect::PodcastRefresh(ids))
|
||||
}
|
||||
|
||||
fn podcasts_confirm_unsubscribe(state: &mut AppState) -> Option<Effect> {
|
||||
if state.podcasts.opened.is_some() {
|
||||
state.status_message =
|
||||
Some("podcasts: go back to the podcast list (esc) to unsubscribe".into());
|
||||
return None;
|
||||
}
|
||||
let Some(podcast) = state.podcasts.current_podcast() else {
|
||||
state.status_message = Some("podcasts: select a podcast to unsubscribe".into());
|
||||
return None;
|
||||
};
|
||||
state.popup = Some(super::state::Popup::ConfirmDelete {
|
||||
target: super::state::DeleteTarget::Podcast(podcast.id),
|
||||
label: format!("podcast \"{}\"", podcast.title),
|
||||
});
|
||||
None
|
||||
}
|
||||
|
||||
/// The queueable track for the episode under the cursor.
|
||||
fn selected_episode_track(state: &AppState) -> Option<TrackItem> {
|
||||
let opened = state.podcasts.opened?;
|
||||
let podcast = state.podcasts.podcast(opened.id)?;
|
||||
let episode = state.podcasts.selected_episode()?;
|
||||
Some(crate::podcasts::episode_track(podcast, episode))
|
||||
}
|
||||
|
||||
/// Enter on an episode: play it right now without discarding the queue. It
|
||||
/// is inserted after the current track and becomes the current track.
|
||||
fn play_selected_episode(state: &mut AppState) -> Option<Effect> {
|
||||
let Some(track) = selected_episode_track(state) else {
|
||||
state.status_message = Some(match state.podcasts.opened_episodes() {
|
||||
Some(_) if !state.podcasts.filter.is_empty() => "no episode matches the filter".into(),
|
||||
Some(_) => "this podcast has no episodes".into(),
|
||||
None => "episodes are still loading".into(),
|
||||
});
|
||||
return None;
|
||||
};
|
||||
Some(play_track_now(state, track))
|
||||
}
|
||||
|
||||
pub fn play_track_now(state: &mut AppState, track: TrackItem) -> Effect {
|
||||
let player = &mut state.player;
|
||||
let insert_at = if player.current.is_some() {
|
||||
(player.queue_pos + 1).min(player.queue.len())
|
||||
} else {
|
||||
player.queue_pos.min(player.queue.len())
|
||||
};
|
||||
player.queue.insert(insert_at, track);
|
||||
if let Some(end) = player.play_next_end.as_mut()
|
||||
&& *end >= insert_at
|
||||
{
|
||||
*end += 1;
|
||||
}
|
||||
player.queue_pos = insert_at;
|
||||
player.prefetched_pos = None;
|
||||
normalize_play_next_block(player);
|
||||
state.track_selection.clear();
|
||||
Effect::PlayCurrent
|
||||
}
|
||||
|
||||
fn not_yet(state: &mut AppState, what: &str) {
|
||||
state.status_message = Some(format!("{what}: coming in a later milestone"));
|
||||
}
|
||||
|
||||
pub(crate) mod podcasts;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "update_tests.rs"]
|
||||
mod tests;
|
||||
@@ -1,975 +0,0 @@
|
||||
//! Podcasts tab state transitions.
|
||||
//!
|
||||
//! Pure functions over `AppState`: they request store, HTTP and sync work
|
||||
//! through [`Effect`]s (executed by `app::podcast_runtime`) and fold the
|
||||
//! results back in when the runtime reports them.
|
||||
|
||||
use furumi_library::podcasts::{
|
||||
EpisodeListItem, EpisodeQuery, EpisodeSort, PlayedFilter, SeasonFilter, SeasonSummary,
|
||||
};
|
||||
use music_dht::podcast::{EpisodeId, FeedUrl, PodcastId, ProgressRecord, ProgressStatus};
|
||||
|
||||
use super::Effect;
|
||||
use crate::app::action::Action;
|
||||
use crate::app::input::LineEdit;
|
||||
use crate::app::state::track_key;
|
||||
use crate::app::state::{
|
||||
AppState, EpisodeList, FedInputField, Loadable, OpenedPodcast, PODCAST_ACTION_ROWS,
|
||||
PODCAST_EPISODE_PAGE, PodcastListRow, PodcastRow, PodcastStoreStatus, Popup, SeasonRow, Tab,
|
||||
};
|
||||
use crate::library::models::TrackItem;
|
||||
use crate::podcasts::refresh::{RefreshReport, SubscribeReport};
|
||||
|
||||
/// The next page starts loading when the cursor is this close to the end.
|
||||
const LOAD_MORE_MARGIN: usize = 20;
|
||||
|
||||
/// Podcasts-tab handling of `action`; `None` lets the global handler run.
|
||||
pub(super) fn handle(state: &mut AppState, action: &Action) -> Option<Option<Effect>> {
|
||||
let effect = match action {
|
||||
Action::MoveUp => move_by(state, -1),
|
||||
Action::MoveDown => move_by(state, 1),
|
||||
Action::PageUp => move_by(state, -super::page_step(state)),
|
||||
Action::PageDown => move_by(state, super::page_step(state)),
|
||||
Action::SelectFirst => move_by(state, isize::MIN),
|
||||
Action::SelectLast => move_by(state, isize::MAX),
|
||||
Action::Select => select(state),
|
||||
Action::Back if state.podcasts.opened.is_some() => {
|
||||
back(state);
|
||||
None
|
||||
}
|
||||
Action::OpenSearch | Action::SearchEpisodes => {
|
||||
open_search(state);
|
||||
None
|
||||
}
|
||||
Action::DeleteSelected | Action::UnsubscribePodcast => {
|
||||
confirm_unsubscribe(state);
|
||||
None
|
||||
}
|
||||
Action::EditSelected => {
|
||||
state.status_message = Some("podcast details come from the RSS feed".into());
|
||||
None
|
||||
}
|
||||
Action::AddPodcast => {
|
||||
open_add(state);
|
||||
None
|
||||
}
|
||||
Action::RefreshPodcast => refresh_current(state),
|
||||
Action::RefreshAllPodcasts => refresh_all(state),
|
||||
Action::CycleEpisodeSort => cycle_sort(state),
|
||||
Action::CycleEpisodeFilter => cycle_played_filter(state),
|
||||
Action::ToggleEpisodePlayed => toggle_played(state),
|
||||
Action::RestartEpisode => restart_selected(state),
|
||||
_ => return None,
|
||||
};
|
||||
Some(effect)
|
||||
}
|
||||
|
||||
/// Whether podcast actions may run; explains why not in the status bar.
|
||||
fn store_ready(state: &mut AppState) -> bool {
|
||||
match &state.podcasts.store {
|
||||
PodcastStoreStatus::Ready => true,
|
||||
PodcastStoreStatus::Opening => {
|
||||
state.status_message = Some("podcasts: the podcast database is still opening".into());
|
||||
false
|
||||
}
|
||||
PodcastStoreStatus::Failed(err) => {
|
||||
state.status_message = Some(format!("podcasts are unavailable: {err}"));
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Navigation
|
||||
|
||||
fn move_by(state: &mut AppState, delta: isize) -> Option<Effect> {
|
||||
let tab = &mut state.podcasts;
|
||||
let len = match &tab.opened {
|
||||
None => tab.list_rows().len(),
|
||||
Some(opened) if opened.season.is_none() => tab.season_rows().len(),
|
||||
Some(_) => tab.opened_episodes().map_or(0, |list| list.items.len()),
|
||||
};
|
||||
if len == 0 {
|
||||
return None;
|
||||
}
|
||||
let target = |cursor: usize| {
|
||||
(cursor as isize)
|
||||
.saturating_add(delta)
|
||||
.clamp(0, len as isize - 1) as usize
|
||||
};
|
||||
match &mut tab.opened {
|
||||
None => tab.selected = target(tab.selected),
|
||||
Some(opened) => match (opened.season, &mut opened.season_cursor) {
|
||||
(None, Some(cursor)) => *cursor = target(*cursor),
|
||||
(None, None) => {}
|
||||
(Some(_), _) => opened.episode_cursor = target(opened.episode_cursor),
|
||||
},
|
||||
}
|
||||
load_more_if_needed(state)
|
||||
}
|
||||
|
||||
/// Requests the next episode page when the cursor nears the loaded end.
|
||||
fn load_more_if_needed(state: &mut AppState) -> Option<Effect> {
|
||||
let cursor = state.podcasts.opened.as_ref()?.episode_cursor;
|
||||
state.podcasts.opened_episodes()?;
|
||||
let list = state.podcasts.episodes.as_mut()?;
|
||||
if list.complete
|
||||
|| list.loading
|
||||
|| list.loading_more
|
||||
|| cursor + LOAD_MORE_MARGIN < list.items.len()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
list.loading_more = true;
|
||||
let mut query = list.query.clone();
|
||||
query.offset = list.items.len();
|
||||
Some(Effect::PodcastLoadEpisodes {
|
||||
seq: list.seq,
|
||||
query,
|
||||
})
|
||||
}
|
||||
|
||||
fn select(state: &mut AppState) -> Option<Effect> {
|
||||
let Some(opened) = state.podcasts.opened.clone() else {
|
||||
return match state
|
||||
.podcasts
|
||||
.list_rows()
|
||||
.get(state.podcasts.selected)
|
||||
.copied()?
|
||||
{
|
||||
PodcastListRow::Add => {
|
||||
open_add(state);
|
||||
None
|
||||
}
|
||||
PodcastListRow::RefreshAll => refresh_all(state),
|
||||
PodcastListRow::Podcast(index) => open_podcast(state, index),
|
||||
};
|
||||
};
|
||||
if opened.season.is_none() {
|
||||
let cursor = opened.season_cursor.unwrap_or(0);
|
||||
let season = match state.podcasts.season_rows().get(cursor).copied()? {
|
||||
SeasonRow::All => SeasonFilter::Any,
|
||||
SeasonRow::Group(index) => match state.podcasts.opened_seasons()?.get(index)?.season {
|
||||
Some(number) => SeasonFilter::Season(number),
|
||||
None => SeasonFilter::NoSeason,
|
||||
},
|
||||
};
|
||||
if let Some(opened) = &mut state.podcasts.opened {
|
||||
opened.season = Some(season);
|
||||
opened.episode_cursor = 0;
|
||||
}
|
||||
state.podcasts.search.clear();
|
||||
return Some(load_episodes(state, None));
|
||||
}
|
||||
let track = state.podcasts.selected_episode_track()?;
|
||||
play_episode_now(state, track, false)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Playback
|
||||
|
||||
/// Whether podcast episodes may be added to the queue that plays.
|
||||
///
|
||||
/// On this device, always. A device controlling another one sends the
|
||||
/// queue there, so that device must speak sync v4 (an older one would drop
|
||||
/// the episodes). A Jam guest never adds episodes to the host's queue.
|
||||
/// Says why and returns `false` otherwise.
|
||||
pub fn episodes_play_here(state: &mut AppState) -> bool {
|
||||
use crate::app::state::DevicePlaybackRole;
|
||||
match state.device_playback.role {
|
||||
DevicePlaybackRole::Jam if !state.device_playback.jam_host => {
|
||||
state.status_message =
|
||||
Some("podcasts: Jam guests cannot add podcast episodes to the host's queue".into());
|
||||
false
|
||||
}
|
||||
DevicePlaybackRole::Control => {
|
||||
let target = state.device_playback.active_device_id.clone();
|
||||
let version = target.as_deref().and_then(|target| {
|
||||
state
|
||||
.federation
|
||||
.devices
|
||||
.as_ref()?
|
||||
.devices
|
||||
.iter()
|
||||
.find(|row| row.device_id == target)
|
||||
.map(|row| row.protocol_version)
|
||||
});
|
||||
if version.is_some_and(music_dht::device_sync::supports_podcasts) {
|
||||
return true;
|
||||
}
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: {} needs a Furumi update to play podcast episodes",
|
||||
state.device_playback.active_label()
|
||||
));
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Enter on an episode: play it now, right after the current item, keeping
|
||||
/// the rest of the queue. Ordinary Play resumes from the saved position;
|
||||
/// `restart` is the explicit "start over".
|
||||
pub(super) fn play_episode_now(
|
||||
state: &mut AppState,
|
||||
track: TrackItem,
|
||||
restart: bool,
|
||||
) -> Option<Effect> {
|
||||
if !episodes_play_here(state) {
|
||||
return None;
|
||||
}
|
||||
let key = track_key(&track);
|
||||
let player = &mut state.player;
|
||||
if let Some(index) = player
|
||||
.current
|
||||
.as_ref()
|
||||
.filter(|current| track_key(current) == key)
|
||||
.and_then(|_| {
|
||||
if player
|
||||
.queue
|
||||
.get(player.queue_pos)
|
||||
.is_some_and(|queued| track_key(queued) == key)
|
||||
{
|
||||
Some(player.queue_pos)
|
||||
} else {
|
||||
player
|
||||
.queue
|
||||
.iter()
|
||||
.position(|queued| track_key(queued) == key)
|
||||
}
|
||||
})
|
||||
{
|
||||
// Already the current item.
|
||||
player.queue_pos = index;
|
||||
if restart {
|
||||
return Some(Effect::PlayCurrentFromStart);
|
||||
}
|
||||
if player.playing && player.paused {
|
||||
player.paused = false;
|
||||
return Some(Effect::TogglePause);
|
||||
}
|
||||
if player.playing {
|
||||
state.status_message = Some("podcasts: this episode is playing".into());
|
||||
return None;
|
||||
}
|
||||
return Some(Effect::PlayCurrent);
|
||||
}
|
||||
let len = player.queue.len();
|
||||
let at = if player.current.is_some() {
|
||||
(player.queue_pos + 1).min(len)
|
||||
} else {
|
||||
player.queue_pos.min(len)
|
||||
};
|
||||
player.queue.insert(at, track);
|
||||
if let Some(end) = player.play_next_end
|
||||
&& end >= at
|
||||
{
|
||||
player.play_next_end = Some(end + 1);
|
||||
}
|
||||
player.prefetched_pos = None;
|
||||
player.queue_pos = at;
|
||||
super::normalize_play_next_block(player);
|
||||
Some(if restart {
|
||||
Effect::PlayCurrentFromStart
|
||||
} else {
|
||||
Effect::PlayCurrent
|
||||
})
|
||||
}
|
||||
|
||||
/// `b` on the episode list: play the selected episode from the beginning.
|
||||
fn restart_selected(state: &mut AppState) -> Option<Effect> {
|
||||
match state.podcasts.selected_episode_track() {
|
||||
Some(track) => play_episode_now(state, track, true),
|
||||
None => restart_current(state),
|
||||
}
|
||||
}
|
||||
|
||||
/// "Start over" for the episode that is playing.
|
||||
pub fn restart_current(state: &mut AppState) -> Option<Effect> {
|
||||
let Some(track) = state.player.current.clone().filter(TrackItem::is_podcast) else {
|
||||
state.status_message = Some("podcasts: select an episode or play one first".into());
|
||||
return None;
|
||||
};
|
||||
play_episode_now(state, track, true)
|
||||
}
|
||||
|
||||
/// The progress writer saved (or found) a progress register: show it in
|
||||
/// the episode list without reloading it, and carry its generation in the
|
||||
/// queue items of that episode (a handoff passes it on).
|
||||
pub fn progress_saved(state: &mut AppState, record: ProgressRecord) -> Vec<Effect> {
|
||||
if let Some(list) = state.podcasts.episodes.as_mut() {
|
||||
for item in &mut list.items {
|
||||
if item.episode.episode_id == record.episode_id {
|
||||
item.progress = Some(record.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
let player = &mut state.player;
|
||||
for track in player.queue.iter_mut().chain(player.current.as_mut()) {
|
||||
if let Some(episode) = track.podcast.as_mut()
|
||||
&& episode.episode_id == record.episode_id
|
||||
{
|
||||
episode.progress_generation = Some(
|
||||
episode
|
||||
.progress_generation
|
||||
.unwrap_or(0)
|
||||
.max(record.generation),
|
||||
);
|
||||
}
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Podcast state changed by other devices (received operations or
|
||||
/// snapshot sections): progress registers update the list in place, the
|
||||
/// rest reloads what is on screen.
|
||||
pub fn synced(state: &mut AppState, progress: Vec<ProgressRecord>, changed: bool) -> Vec<Effect> {
|
||||
let mut effects = Vec::new();
|
||||
for record in progress {
|
||||
effects.extend(progress_saved(state, record));
|
||||
}
|
||||
if state.podcasts.store != PodcastStoreStatus::Ready {
|
||||
return effects;
|
||||
}
|
||||
// Counts in the list (unplayed, in progress) follow both.
|
||||
effects.push(Effect::PodcastsReload);
|
||||
if changed {
|
||||
if let Some(opened) = &state.podcasts.opened
|
||||
&& opened.season_cursor.is_some()
|
||||
{
|
||||
effects.push(Effect::PodcastLoadSeasons(opened.podcast_id.clone()));
|
||||
}
|
||||
effects.extend(reload_episodes(state));
|
||||
}
|
||||
effects
|
||||
}
|
||||
|
||||
fn open_podcast(state: &mut AppState, index: usize) -> Option<Effect> {
|
||||
let row = state.podcasts.podcasts().get(index)?;
|
||||
let podcast_id = row.id().clone();
|
||||
let has_seasons = row.summary.season_count > 0;
|
||||
state.podcasts.opened = Some(OpenedPodcast {
|
||||
podcast_id: podcast_id.clone(),
|
||||
season_cursor: has_seasons.then_some(0),
|
||||
season: (!has_seasons).then_some(SeasonFilter::Any),
|
||||
episode_cursor: 0,
|
||||
});
|
||||
state.podcasts.search.clear();
|
||||
if has_seasons {
|
||||
state.podcasts.seasons = Some((podcast_id.clone(), Loadable::Loading));
|
||||
Some(Effect::PodcastLoadSeasons(podcast_id))
|
||||
} else {
|
||||
Some(load_episodes(state, None))
|
||||
}
|
||||
}
|
||||
|
||||
/// Shift-J (or the info popup's artist jump) on a podcast episode: opens its
|
||||
/// podcast on the Podcasts tab instead of a music release or artist.
|
||||
pub fn reveal_episode_podcast(state: &mut AppState, track: &TrackItem) -> Option<Effect> {
|
||||
let episode = track.podcast_episode_ref()?;
|
||||
let podcast_id = episode.podcast_id.clone();
|
||||
let title = episode.podcast_title.clone();
|
||||
state.active_tab = Tab::Podcasts;
|
||||
state.help_visible = false;
|
||||
state.track_selection.clear();
|
||||
state.jump_origin = None;
|
||||
if !store_ready(state) {
|
||||
return None;
|
||||
}
|
||||
if state
|
||||
.podcasts
|
||||
.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.podcast_id == podcast_id)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
close(state);
|
||||
match state
|
||||
.podcasts
|
||||
.podcasts()
|
||||
.iter()
|
||||
.position(|row| *row.id() == podcast_id)
|
||||
{
|
||||
Some(index) => {
|
||||
state.podcasts.selected = index + PODCAST_ACTION_ROWS;
|
||||
open_podcast(state, index)
|
||||
}
|
||||
None => {
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: {} is not subscribed on this device",
|
||||
display_title(&title)
|
||||
));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Esc: episodes -> seasons (when the podcast has them) -> podcast list.
|
||||
fn back(state: &mut AppState) {
|
||||
let tab = &mut state.podcasts;
|
||||
match &mut tab.opened {
|
||||
Some(opened) if opened.season.is_some() && opened.season_cursor.is_some() => {
|
||||
opened.season = None;
|
||||
tab.search.clear();
|
||||
}
|
||||
Some(_) => close(state),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes the opened podcast and puts the list cursor on it.
|
||||
pub(super) fn close(state: &mut AppState) {
|
||||
let tab = &mut state.podcasts;
|
||||
if let Some(opened) = tab.opened.take()
|
||||
&& let Some(index) = tab
|
||||
.podcasts()
|
||||
.iter()
|
||||
.position(|row| *row.id() == opened.podcast_id)
|
||||
{
|
||||
tab.selected = index + PODCAST_ACTION_ROWS;
|
||||
}
|
||||
tab.search.clear();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episode queries
|
||||
|
||||
/// Starts loading the first page of the opened group with the current
|
||||
/// search, filter and sort. `focus` keeps the cursor on that episode when
|
||||
/// it is still listed (reloads after a refresh); otherwise the cursor goes
|
||||
/// to the top.
|
||||
fn load_episodes(state: &mut AppState, focus: Option<EpisodeId>) -> Effect {
|
||||
let tab = &mut state.podcasts;
|
||||
let opened = tab
|
||||
.opened
|
||||
.as_mut()
|
||||
.expect("load_episodes needs an opened podcast");
|
||||
let season = opened.season.unwrap_or_default();
|
||||
let search = tab.search.trim();
|
||||
let query = EpisodeQuery {
|
||||
podcast_id: opened.podcast_id.clone(),
|
||||
season,
|
||||
played: tab.played,
|
||||
search: (!search.is_empty()).then(|| search.to_string()),
|
||||
sort: tab.sort,
|
||||
limit: Some(PODCAST_EPISODE_PAGE),
|
||||
offset: 0,
|
||||
};
|
||||
if focus.is_none() {
|
||||
opened.episode_cursor = 0;
|
||||
}
|
||||
tab.episode_seq += 1;
|
||||
let seq = tab.episode_seq;
|
||||
// Keep showing the previous rows of the same group while the new query
|
||||
// runs, so typing a search does not flash an empty list.
|
||||
let items = match tab.episodes.take() {
|
||||
Some(list) if list.query.podcast_id == query.podcast_id && list.query.season == season => {
|
||||
list.items
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
tab.episodes = Some(EpisodeList {
|
||||
seq,
|
||||
query: query.clone(),
|
||||
items,
|
||||
loading: true,
|
||||
loading_more: false,
|
||||
complete: false,
|
||||
error: None,
|
||||
focus,
|
||||
});
|
||||
Effect::PodcastLoadEpisodes { seq, query }
|
||||
}
|
||||
|
||||
/// Reloads the visible episodes, keeping the cursor on the same episode.
|
||||
fn reload_episodes(state: &mut AppState) -> Option<Effect> {
|
||||
if !state.podcasts.showing_episodes() {
|
||||
return None;
|
||||
}
|
||||
let focus = state
|
||||
.podcasts
|
||||
.selected_episode()
|
||||
.map(|item| item.episode.episode_id.clone());
|
||||
Some(load_episodes(state, focus))
|
||||
}
|
||||
|
||||
/// Live search: called for every edit of the search field.
|
||||
pub fn set_search(state: &mut AppState, search: String) -> Option<Effect> {
|
||||
if state.podcasts.search == search {
|
||||
return None;
|
||||
}
|
||||
state.podcasts.search = search;
|
||||
state
|
||||
.podcasts
|
||||
.showing_episodes()
|
||||
.then(|| load_episodes(state, None))
|
||||
}
|
||||
|
||||
fn open_search(state: &mut AppState) {
|
||||
if !state.podcasts.showing_episodes() {
|
||||
state.status_message = Some("podcasts: open a podcast to search its episodes".into());
|
||||
return;
|
||||
}
|
||||
state.popup = Some(Popup::FedInput {
|
||||
field: FedInputField::PodcastSearch,
|
||||
input: LineEdit::new(state.podcasts.search.clone()),
|
||||
});
|
||||
}
|
||||
|
||||
fn cycle_sort(state: &mut AppState) -> Option<Effect> {
|
||||
let next = match state.podcasts.sort {
|
||||
EpisodeSort::NewestFirst => EpisodeSort::OldestFirst,
|
||||
EpisodeSort::OldestFirst => EpisodeSort::EpisodeNumber,
|
||||
EpisodeSort::EpisodeNumber => EpisodeSort::EpisodeNumberDesc,
|
||||
EpisodeSort::EpisodeNumberDesc => EpisodeSort::NewestFirst,
|
||||
};
|
||||
state.podcasts.sort = next;
|
||||
state.status_message = Some(format!("episodes: {}", sort_label(next)));
|
||||
state
|
||||
.podcasts
|
||||
.showing_episodes()
|
||||
.then(|| load_episodes(state, None))
|
||||
}
|
||||
|
||||
fn cycle_played_filter(state: &mut AppState) -> Option<Effect> {
|
||||
let next = match state.podcasts.played {
|
||||
PlayedFilter::Any => PlayedFilter::Unplayed,
|
||||
PlayedFilter::Unplayed => PlayedFilter::InProgress,
|
||||
PlayedFilter::InProgress => PlayedFilter::Played,
|
||||
PlayedFilter::Played | PlayedFilter::NotStarted => PlayedFilter::Any,
|
||||
};
|
||||
state.podcasts.played = next;
|
||||
state.status_message = Some(format!("episodes: {}", played_label(next)));
|
||||
state
|
||||
.podcasts
|
||||
.showing_episodes()
|
||||
.then(|| load_episodes(state, None))
|
||||
}
|
||||
|
||||
pub fn sort_label(sort: EpisodeSort) -> &'static str {
|
||||
match sort {
|
||||
EpisodeSort::NewestFirst => "newest first",
|
||||
EpisodeSort::OldestFirst => "oldest first",
|
||||
EpisodeSort::EpisodeNumber => "by episode number",
|
||||
EpisodeSort::EpisodeNumberDesc => "by episode number, descending",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn played_label(filter: PlayedFilter) -> &'static str {
|
||||
match filter {
|
||||
PlayedFilter::Any => "all episodes",
|
||||
PlayedFilter::Unplayed => "unplayed",
|
||||
PlayedFilter::NotStarted => "not started",
|
||||
PlayedFilter::InProgress => "in progress",
|
||||
PlayedFilter::Played => "played",
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subscriptions
|
||||
|
||||
fn open_add(state: &mut AppState) {
|
||||
if !store_ready(state) {
|
||||
return;
|
||||
}
|
||||
if state.podcasts.subscribing {
|
||||
state.status_message = Some("podcasts: a subscription is already being added".into());
|
||||
return;
|
||||
}
|
||||
state.popup = Some(Popup::FedInput {
|
||||
field: FedInputField::PodcastFeedUrl,
|
||||
input: LineEdit::default(),
|
||||
});
|
||||
}
|
||||
|
||||
/// Enter in the "Add podcast" field.
|
||||
pub fn submit_feed_url(state: &mut AppState, input: &str) -> Option<Effect> {
|
||||
let input = input.trim();
|
||||
if input.is_empty() {
|
||||
state.status_message = Some("podcasts: the feed URL is empty".into());
|
||||
return None;
|
||||
}
|
||||
if !store_ready(state) {
|
||||
return None;
|
||||
}
|
||||
if let Err(err) = FeedUrl::from_user_input(input) {
|
||||
state.status_message = Some(format!("podcasts: {err}"));
|
||||
return None;
|
||||
}
|
||||
state.podcasts.subscribing = true;
|
||||
state.status_message = Some("podcasts: reading the feed…".into());
|
||||
Some(Effect::PodcastSubscribe(input.to_string()))
|
||||
}
|
||||
|
||||
fn confirm_unsubscribe(state: &mut AppState) {
|
||||
if !store_ready(state) {
|
||||
return;
|
||||
}
|
||||
let Some(row) = state.podcasts.current_podcast() else {
|
||||
state.status_message = Some("podcasts: select a podcast to unsubscribe".into());
|
||||
return;
|
||||
};
|
||||
state.popup = Some(Popup::ConfirmPodcastUnsubscribe {
|
||||
podcast_id: row.id().clone(),
|
||||
title: row.title().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
fn refresh_targets(state: &mut AppState, rows: Vec<&PodcastRow>) -> Vec<(PodcastId, FeedUrl)> {
|
||||
rows.into_iter()
|
||||
.filter(|row| !state.podcasts.refreshing.contains(row.id()))
|
||||
.map(|row| (row.id().clone(), row.summary.meta.feed_url.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn start_refresh(
|
||||
state: &mut AppState,
|
||||
targets: Vec<(PodcastId, FeedUrl)>,
|
||||
manual: bool,
|
||||
) -> Option<Effect> {
|
||||
if targets.is_empty() {
|
||||
return None;
|
||||
}
|
||||
state
|
||||
.podcasts
|
||||
.refreshing
|
||||
.extend(targets.iter().map(|(id, _)| id.clone()));
|
||||
Some(Effect::PodcastRefresh { targets, manual })
|
||||
}
|
||||
|
||||
/// Shift-R: the opened podcast or the one under the cursor.
|
||||
fn refresh_current(state: &mut AppState) -> Option<Effect> {
|
||||
if !store_ready(state) {
|
||||
return None;
|
||||
}
|
||||
let Some(row) = state.podcasts.current_podcast().cloned() else {
|
||||
return refresh_all(state);
|
||||
};
|
||||
if state.podcasts.refreshing.contains(row.id()) {
|
||||
state.status_message = Some(format!("podcasts: already checking {}", row.title()));
|
||||
return None;
|
||||
}
|
||||
let targets = refresh_targets(state, vec![&row]);
|
||||
state.status_message = Some(format!("podcasts: checking {}…", row.title()));
|
||||
start_refresh(state, targets, true)
|
||||
}
|
||||
|
||||
/// Ctrl-R / "Check all": every subscription.
|
||||
fn refresh_all(state: &mut AppState) -> Option<Effect> {
|
||||
if !store_ready(state) {
|
||||
return None;
|
||||
}
|
||||
let rows: Vec<PodcastRow> = state.podcasts.podcasts().to_vec();
|
||||
if rows.is_empty() {
|
||||
state.status_message = Some("podcasts: no subscriptions yet".into());
|
||||
return None;
|
||||
}
|
||||
let targets = refresh_targets(state, rows.iter().collect());
|
||||
if targets.is_empty() {
|
||||
state.status_message = Some("podcasts: already checking every feed".into());
|
||||
return None;
|
||||
}
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: checking {} feed(s) for new episodes…",
|
||||
targets.len()
|
||||
));
|
||||
start_refresh(state, targets, true)
|
||||
}
|
||||
|
||||
/// Scheduler: feeds the store reports as due (back-off already applied).
|
||||
pub fn refresh_due(state: &mut AppState, due: Vec<(PodcastId, FeedUrl)>) -> Option<Effect> {
|
||||
let targets: Vec<_> = due
|
||||
.into_iter()
|
||||
.filter(|(id, _)| !state.podcasts.refreshing.contains(id))
|
||||
.collect();
|
||||
start_refresh(state, targets, false)
|
||||
}
|
||||
|
||||
fn toggle_played(state: &mut AppState) -> Option<Effect> {
|
||||
if !store_ready(state) {
|
||||
return None;
|
||||
}
|
||||
let Some(item) = state.podcasts.selected_episode() else {
|
||||
state.status_message = Some("podcasts: select an episode".into());
|
||||
return None;
|
||||
};
|
||||
let played = item.status() != ProgressStatus::Completed;
|
||||
Some(Effect::PodcastSetPlayed {
|
||||
episode_id: item.episode.episode_id.clone(),
|
||||
played,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Results from the runtime
|
||||
|
||||
/// The store opened (or failed to).
|
||||
pub fn store_opened(state: &mut AppState, result: Result<(), String>) -> Option<Effect> {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
state.podcasts.store = PodcastStoreStatus::Ready;
|
||||
state.podcasts.list = Some(Loadable::Loading);
|
||||
Some(Effect::PodcastsReload)
|
||||
}
|
||||
Err(err) => {
|
||||
state.podcasts.store = PodcastStoreStatus::Failed(err);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_loaded(state: &mut AppState, result: Result<Vec<PodcastRow>, String>) {
|
||||
let tab = &mut state.podcasts;
|
||||
let previous = match (&tab.opened, tab.list_rows().get(tab.selected)) {
|
||||
(None, Some(PodcastListRow::Podcast(index))) => {
|
||||
tab.podcasts().get(*index).map(|row| row.id().clone())
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
match result {
|
||||
Ok(rows) => tab.list = Some(Loadable::Ready(rows)),
|
||||
Err(err) => {
|
||||
tracing::warn!(%err, "podcast list load failed");
|
||||
if matches!(tab.list, Some(Loadable::Ready(_))) {
|
||||
state.status_message = Some(format!("podcasts: {err}"));
|
||||
return;
|
||||
}
|
||||
tab.list = Some(Loadable::Failed(err));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if let Some(id) = tab.pending_select.take().or(previous)
|
||||
&& let Some(index) = tab.podcasts().iter().position(|row| *row.id() == id)
|
||||
{
|
||||
tab.selected = index + PODCAST_ACTION_ROWS;
|
||||
}
|
||||
tab.selected = tab.selected.min(tab.list_rows().len().saturating_sub(1));
|
||||
// A podcast unsubscribed elsewhere closes.
|
||||
if let Some(opened) = &tab.opened
|
||||
&& tab.podcast(&opened.podcast_id).is_none()
|
||||
{
|
||||
close(state);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seasons_loaded(
|
||||
state: &mut AppState,
|
||||
podcast_id: PodcastId,
|
||||
result: Result<Vec<SeasonSummary>, String>,
|
||||
) {
|
||||
let tab = &mut state.podcasts;
|
||||
if tab.opened.as_ref().map(|opened| &opened.podcast_id) != Some(&podcast_id) {
|
||||
return;
|
||||
}
|
||||
let loaded = match result {
|
||||
Ok(seasons) => Loadable::Ready(seasons),
|
||||
Err(err) => {
|
||||
if matches!(&tab.seasons, Some((id, Loadable::Ready(_))) if *id == podcast_id) {
|
||||
state.status_message = Some(format!("podcasts: {err}"));
|
||||
return;
|
||||
}
|
||||
Loadable::Failed(err)
|
||||
}
|
||||
};
|
||||
tab.seasons = Some((podcast_id, loaded));
|
||||
let rows = tab.season_rows().len();
|
||||
if let Some(OpenedPodcast {
|
||||
season_cursor: Some(cursor),
|
||||
..
|
||||
}) = &mut tab.opened
|
||||
{
|
||||
*cursor = (*cursor).min(rows.saturating_sub(1));
|
||||
}
|
||||
}
|
||||
|
||||
/// A page of episodes arrived. Stale sequences and pages that no longer
|
||||
/// line up with the loaded rows are dropped.
|
||||
pub fn episodes_loaded(
|
||||
state: &mut AppState,
|
||||
seq: u64,
|
||||
offset: usize,
|
||||
result: Result<Vec<EpisodeListItem>, String>,
|
||||
) {
|
||||
let tab = &mut state.podcasts;
|
||||
let Some(list) = tab.episodes.as_mut().filter(|list| list.seq == seq) else {
|
||||
return;
|
||||
};
|
||||
match result {
|
||||
Ok(page) => {
|
||||
let complete = page.len() < PODCAST_EPISODE_PAGE;
|
||||
if offset == 0 {
|
||||
list.items = page;
|
||||
list.loading = false;
|
||||
} else if offset == list.items.len() && list.loading_more {
|
||||
list.items.extend(page);
|
||||
list.loading_more = false;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
list.complete = complete;
|
||||
list.error = None;
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(%err, "podcast episodes load failed");
|
||||
list.loading = false;
|
||||
list.loading_more = false;
|
||||
list.error = Some(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
let focus = list.focus.take();
|
||||
let len = list.items.len();
|
||||
let focused = focus.and_then(|id| {
|
||||
list.items
|
||||
.iter()
|
||||
.position(|item| item.episode.episode_id == id)
|
||||
});
|
||||
if let Some(opened) = &mut tab.opened {
|
||||
if let Some(index) = focused {
|
||||
opened.episode_cursor = index;
|
||||
}
|
||||
opened.episode_cursor = opened.episode_cursor.min(len.saturating_sub(1));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribed(state: &mut AppState, result: Result<SubscribeReport, String>) -> Option<Effect> {
|
||||
state.podcasts.subscribing = false;
|
||||
match result {
|
||||
Ok(report) => {
|
||||
let title = display_title(&report.title);
|
||||
state.status_message = Some(if report.already_subscribed {
|
||||
format!("podcasts: already subscribed to {title}")
|
||||
} else {
|
||||
format!(
|
||||
"podcasts: subscribed to {title} ({} episode(s); private)",
|
||||
report.new_episodes
|
||||
)
|
||||
});
|
||||
if state.podcasts.opened.is_some() {
|
||||
close(state);
|
||||
}
|
||||
state.podcasts.pending_select = Some(report.podcast_id);
|
||||
Some(Effect::PodcastsReload)
|
||||
}
|
||||
Err(err) => {
|
||||
state.status_message = Some(format!("podcasts: cannot subscribe: {err}"));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn display_title(title: &str) -> String {
|
||||
if title.trim().is_empty() {
|
||||
"the podcast".to_string()
|
||||
} else {
|
||||
format!("\"{title}\"")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refreshed(
|
||||
state: &mut AppState,
|
||||
podcast_id: PodcastId,
|
||||
result: Result<RefreshReport, String>,
|
||||
manual: bool,
|
||||
) -> Vec<Effect> {
|
||||
state.podcasts.refreshing.remove(&podcast_id);
|
||||
let title = state
|
||||
.podcasts
|
||||
.podcast(&podcast_id)
|
||||
.map(|row| display_title(row.title()))
|
||||
.unwrap_or_else(|| "the podcast".to_string());
|
||||
let mut effects = vec![Effect::PodcastsReload];
|
||||
match result {
|
||||
Ok(report) => {
|
||||
if report.new_episodes > 0 {
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: {} new episode(s) in {title}",
|
||||
report.new_episodes
|
||||
));
|
||||
} else if manual && state.podcasts.refreshing.is_empty() {
|
||||
state.status_message = Some("podcasts: no new episodes".into());
|
||||
}
|
||||
let changed = report.new_episodes > 0 || report.updated_episodes > 0;
|
||||
let opened_here = state
|
||||
.podcasts
|
||||
.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.podcast_id == podcast_id);
|
||||
if changed && opened_here {
|
||||
if state
|
||||
.podcasts
|
||||
.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.season_cursor.is_some())
|
||||
{
|
||||
effects.push(Effect::PodcastLoadSeasons(podcast_id.clone()));
|
||||
}
|
||||
effects.extend(reload_episodes(state));
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(podcast = %podcast_id, %err, "podcast refresh failed");
|
||||
if manual {
|
||||
state.status_message = Some(format!("podcasts: {title}: {err}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
effects
|
||||
}
|
||||
|
||||
pub fn unsubscribed(
|
||||
state: &mut AppState,
|
||||
podcast_id: PodcastId,
|
||||
title: String,
|
||||
result: Result<(), String>,
|
||||
) -> Option<Effect> {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
state.status_message = Some(format!(
|
||||
"podcasts: unsubscribed from {}; progress and history are kept",
|
||||
display_title(&title)
|
||||
));
|
||||
if state
|
||||
.podcasts
|
||||
.opened
|
||||
.as_ref()
|
||||
.is_some_and(|opened| opened.podcast_id == podcast_id)
|
||||
{
|
||||
close(state);
|
||||
}
|
||||
Some(Effect::PodcastsReload)
|
||||
}
|
||||
Err(err) => {
|
||||
state.status_message = Some(format!("podcasts: unsubscribe failed: {err}"));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn played_set(state: &mut AppState, played: bool, result: Result<(), String>) -> Vec<Effect> {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
state.status_message = Some(
|
||||
if played {
|
||||
"podcasts: marked as played"
|
||||
} else {
|
||||
"podcasts: marked as unplayed"
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
let mut effects = vec![Effect::PodcastsReload];
|
||||
if let Some(opened) = &state.podcasts.opened
|
||||
&& opened.season_cursor.is_some()
|
||||
{
|
||||
effects.push(Effect::PodcastLoadSeasons(opened.podcast_id.clone()));
|
||||
}
|
||||
effects.extend(reload_episodes(state));
|
||||
effects
|
||||
}
|
||||
Err(err) => {
|
||||
state.status_message = Some(format!("podcasts: {err}"));
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
+239
-864
File diff suppressed because it is too large.
Load diff
@@ -1,27 +1,15 @@
|
||||
# Default keybindings for furumi.
|
||||
#
|
||||
# To customize, copy entries into keymap.toml in the platform config dir
|
||||
# (~/.config/furumi/keymap.toml on Linux,
|
||||
# ~/Library/Application Support/furumi/keymap.toml on macOS). A user binding
|
||||
# replaces the default binding with the same key sequence and context.
|
||||
#
|
||||
# version: keymap format version; put `version = 2` at the top of your file,
|
||||
# before the first [[keymaps]] entry (after one, TOML attaches it to that
|
||||
# entry and the file is rejected). Version 2 numbers tabs in this order:
|
||||
# GoToTab = 0 Library, 1 Playlists, 2 Queue, 3 Podcasts, 4 Settings, 5 Logs.
|
||||
# A file without `version` is treated as the older layout and its GoToTab
|
||||
# indices are adapted in memory (3 -> 4 Settings, 4 -> 5 Logs, and 5 -> 3
|
||||
# Podcasts from the prototype podcast build); the file itself is never
|
||||
# rewritten. With `version = 2` indices are used exactly as written.
|
||||
# To customize, copy entries into <config dir>/furumi/keymap.toml
|
||||
# (~/.config/furumi/keymap.toml on Linux/macOS). A user binding replaces the
|
||||
# default binding with the same key sequence and context.
|
||||
#
|
||||
# key_sequence: space-separated chords, e.g. "g g" or "ctrl-a x".
|
||||
# Modifiers: ctrl-, alt-, shift-, cmd-. Uppercase letters: "shift-g".
|
||||
# command: an Action name, optionally with parameters:
|
||||
# command = { SeekForward = { seconds = 30 } }
|
||||
# context: optional view filter — global (default), library, search,
|
||||
# playlists, queue, podcasts, settings, logs.
|
||||
|
||||
version = 2
|
||||
# playlists, queue, settings, logs, podcasts.
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "q"
|
||||
@@ -272,50 +260,7 @@ command = "OpenCommandLine"
|
||||
key_sequence = "/"
|
||||
command = "OpenSearch"
|
||||
|
||||
# Podcasts tab. Enter opens a podcast, its seasons and episodes, and plays
|
||||
# an episode (continuing where you stopped); Esc goes back. a / shift-a queue
|
||||
# the episode; Shift-D also unsubscribes here.
|
||||
[[keymaps]]
|
||||
key_sequence = "o"
|
||||
command = "AddPodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "/"
|
||||
command = "SearchEpisodes"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-r"
|
||||
command = "RefreshPodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "ctrl-r"
|
||||
command = "RefreshAllPodcasts"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-s"
|
||||
command = "CycleEpisodeSort"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f"
|
||||
command = "CycleEpisodeFilter"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "m"
|
||||
command = "ToggleEpisodePlayed"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "shift-u"
|
||||
command = "UnsubscribePodcast"
|
||||
context = "podcasts"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "b"
|
||||
command = "RestartEpisode"
|
||||
command = "RefreshPodcasts"
|
||||
context = "podcasts"
|
||||
+6
-335
@@ -9,14 +9,6 @@ use crate::app::action::Action;
|
||||
|
||||
const DEFAULT_KEYMAP: &str = include_str!("default_keymap.toml");
|
||||
|
||||
/// Current keymap file format, declared as a top-level `version = 2`.
|
||||
///
|
||||
/// Version 2 introduced the tab order Library, Playlists, Queue, Podcasts,
|
||||
/// Settings, Logs. Files without a version (or with `version = 1`) were
|
||||
/// written for the older order and have their `GoToTab` indices remapped in
|
||||
/// memory; the user's file itself is never rewritten.
|
||||
pub const KEYMAP_VERSION: u32 = 2;
|
||||
|
||||
/// Input context a binding applies to. `Global` bindings work everywhere;
|
||||
/// view-specific bindings shadow global ones for the same key sequence.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
|
||||
@@ -28,10 +20,10 @@ pub enum KeyContext {
|
||||
Search,
|
||||
Playlists,
|
||||
Queue,
|
||||
Podcasts,
|
||||
#[serde(rename = "settings", alias = "federation")]
|
||||
Federation,
|
||||
Logs,
|
||||
Podcasts,
|
||||
}
|
||||
|
||||
impl KeyContext {
|
||||
@@ -42,9 +34,9 @@ impl KeyContext {
|
||||
KeyContext::Search => "search",
|
||||
KeyContext::Playlists => "playlists",
|
||||
KeyContext::Queue => "queue",
|
||||
KeyContext::Podcasts => "podcasts",
|
||||
KeyContext::Federation => "settings",
|
||||
KeyContext::Logs => "logs",
|
||||
KeyContext::Podcasts => "podcasts",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,18 +54,10 @@ struct RawBinding {
|
||||
command: Action,
|
||||
#[serde(default)]
|
||||
context: KeyContext,
|
||||
/// Never valid here: a `version = N` line written after a `[[keymaps]]`
|
||||
/// header lands inside that entry. Caught so the file is not silently
|
||||
/// treated as legacy (and its tab indices remapped) despite the version.
|
||||
#[serde(default)]
|
||||
version: Option<toml::Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
struct KeymapFile {
|
||||
/// Format version; `None` means a legacy (pre-version-2) file.
|
||||
#[serde(default)]
|
||||
version: Option<u32>,
|
||||
#[serde(default)]
|
||||
keymaps: Vec<RawBinding>,
|
||||
}
|
||||
@@ -105,15 +89,9 @@ impl Keymap {
|
||||
{
|
||||
match fs::read_to_string(&path)
|
||||
.map_err(anyhow::Error::from)
|
||||
.and_then(|text| parse_user_keymap(&text))
|
||||
.and_then(|text| parse_bindings(&text))
|
||||
{
|
||||
Ok(user) => {
|
||||
if let Some(notice) = user.notice {
|
||||
tracing::info!(path = %path.display(), "{notice}");
|
||||
warning = Some(format!("{}: {notice}", path.display()));
|
||||
}
|
||||
merge(&mut bindings, user.bindings);
|
||||
}
|
||||
Ok(user) => merge(&mut bindings, user),
|
||||
Err(err) => {
|
||||
warning = Some(format!(
|
||||
"{} ignored: {err:#}; using default keybindings",
|
||||
@@ -130,16 +108,6 @@ impl Keymap {
|
||||
(keymap, warning)
|
||||
}
|
||||
|
||||
/// The embedded defaults only, without reading the user's file.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn defaults() -> Self {
|
||||
Self {
|
||||
bindings: parse_bindings(DEFAULT_KEYMAP).expect("embedded default keymap must parse"),
|
||||
pending: Vec::new(),
|
||||
format: KeyCombinationFormat::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Feed one key combination; returns an action, a pending-chord state, or
|
||||
/// nothing. Esc clears a pending chord instead of resolving.
|
||||
pub fn resolve(&mut self, key: KeyCombination, context: KeyContext) -> KeyResolution {
|
||||
@@ -309,76 +277,10 @@ pub fn user_keymap_path() -> Option<PathBuf> {
|
||||
}
|
||||
|
||||
fn parse_bindings(text: &str) -> Result<Vec<Binding>> {
|
||||
parse_file(text).map(|(_, bindings)| bindings)
|
||||
}
|
||||
|
||||
/// A user keymap after version handling, plus a one-time, non-blocking
|
||||
/// notice for the status bar and log when its meaning was adapted.
|
||||
struct UserKeymap {
|
||||
bindings: Vec<Binding>,
|
||||
notice: Option<String>,
|
||||
}
|
||||
|
||||
fn parse_user_keymap(text: &str) -> Result<UserKeymap> {
|
||||
let (version, mut bindings) = parse_file(text)?;
|
||||
let notice = match version {
|
||||
None | Some(1) => {
|
||||
let remapped = remap_legacy_tab_indices(&mut bindings);
|
||||
(remapped > 0).then(|| {
|
||||
format!(
|
||||
"{remapped} tab shortcut(s) adapted to the new tab order \
|
||||
(4 Podcasts, 5 Settings, 6 Logs); \
|
||||
add `version = {KEYMAP_VERSION}` to use indices as written"
|
||||
)
|
||||
})
|
||||
}
|
||||
Some(KEYMAP_VERSION) => None,
|
||||
Some(0) => bail!("unsupported keymap version 0"),
|
||||
Some(newer) => Some(format!(
|
||||
"keymap version {newer} is newer than this build supports \
|
||||
({KEYMAP_VERSION}); bindings are used as written"
|
||||
)),
|
||||
};
|
||||
Ok(UserKeymap { bindings, notice })
|
||||
}
|
||||
|
||||
/// Pre-version-2 `GoToTab` index → current index. Index 5 only existed in the
|
||||
/// prototype podcast build, where it opened Podcasts.
|
||||
fn legacy_tab_index(index: usize) -> usize {
|
||||
match index {
|
||||
3 => 4, // Settings
|
||||
4 => 5, // Logs
|
||||
5 => 3, // Podcasts (prototype)
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites legacy `GoToTab` indices in memory; returns how many changed.
|
||||
fn remap_legacy_tab_indices(bindings: &mut [Binding]) -> usize {
|
||||
let mut remapped = 0;
|
||||
for binding in bindings {
|
||||
if let Action::GoToTab(index) = &mut binding.action {
|
||||
let current = legacy_tab_index(*index);
|
||||
if current != *index {
|
||||
*index = current;
|
||||
remapped += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
remapped
|
||||
}
|
||||
|
||||
fn parse_file(text: &str) -> Result<(Option<u32>, Vec<Binding>)> {
|
||||
let file: KeymapFile = toml::from_str(text).context("invalid TOML")?;
|
||||
let bindings = file
|
||||
.keymaps
|
||||
file.keymaps
|
||||
.into_iter()
|
||||
.map(|raw| {
|
||||
if raw.version.is_some() {
|
||||
bail!(
|
||||
"`version` must be a top-level key placed before the first [[keymaps]] entry"
|
||||
);
|
||||
}
|
||||
let keys = parse_sequence(&raw.key_sequence)
|
||||
.with_context(|| format!("bad key_sequence {:?}", raw.key_sequence))?;
|
||||
Ok(Binding {
|
||||
@@ -387,8 +289,7 @@ fn parse_file(text: &str) -> Result<(Option<u32>, Vec<Binding>)> {
|
||||
context: raw.context,
|
||||
})
|
||||
})
|
||||
.collect::<Result<_>>()?;
|
||||
Ok((file.version, bindings))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_sequence(s: &str) -> Result<Vec<KeyCombination>> {
|
||||
@@ -698,236 +599,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_keymap_declares_current_version() {
|
||||
let (version, _) = parse_file(DEFAULT_KEYMAP).unwrap();
|
||||
assert_eq!(version, Some(KEYMAP_VERSION));
|
||||
assert!(parse_user_keymap(DEFAULT_KEYMAP).unwrap().notice.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_digit_keys_follow_tab_order() {
|
||||
let mut km = keymap_from(DEFAULT_KEYMAP);
|
||||
for (index, key) in ['1', '2', '3', '4', '5', '6'].into_iter().enumerate() {
|
||||
let combo = KeyCombination::new(KeyCode::Char(key), KeyModifiers::NONE);
|
||||
assert_eq!(
|
||||
km.resolve(combo, KeyContext::Library),
|
||||
KeyResolution::Action(Action::GoToTab(index))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn go_to_tab_indices(bindings: &[Binding]) -> Vec<usize> {
|
||||
bindings
|
||||
.iter()
|
||||
.filter_map(|b| match b.action {
|
||||
Action::GoToTab(index) => Some(index),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
const LEGACY_TABS: &str = r#"
|
||||
[[keymaps]]
|
||||
key_sequence = "1"
|
||||
command = { GoToTab = 0 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f2"
|
||||
command = { GoToTab = 2 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f4"
|
||||
command = { GoToTab = 3 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f5"
|
||||
command = { GoToTab = 4 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "f6"
|
||||
command = { GoToTab = 5 }
|
||||
context = "logs"
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "x"
|
||||
command = "ToggleHelp"
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn legacy_keymap_tab_indices_are_remapped_in_memory() {
|
||||
let user = parse_user_keymap(LEGACY_TABS).unwrap();
|
||||
// Settings 3 -> 4, Logs 4 -> 5, prototype Podcasts 5 -> 3.
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 4, 5, 3]);
|
||||
assert_eq!(user.bindings[4].context, KeyContext::Logs);
|
||||
assert_eq!(user.bindings[5].action, Action::ToggleHelp);
|
||||
let notice = user.notice.expect("remap must be reported");
|
||||
assert!(notice.starts_with("3 tab shortcut(s)"), "{notice}");
|
||||
assert!(notice.contains("version = 2"), "{notice}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_version_one_is_treated_as_legacy() {
|
||||
let user = parse_user_keymap(&format!("version = 1\n{LEGACY_TABS}")).unwrap();
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 4, 5, 3]);
|
||||
assert!(user.notice.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_keymap_without_changed_tab_indices_has_no_notice() {
|
||||
let user = parse_user_keymap(
|
||||
r#"
|
||||
[[keymaps]]
|
||||
key_sequence = "1"
|
||||
command = { GoToTab = 0 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "q"
|
||||
command = "Back"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [0]);
|
||||
assert!(user.notice.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn versioned_keymap_is_taken_literally() {
|
||||
let user = parse_user_keymap(&format!("version = 2\n{LEGACY_TABS}")).unwrap();
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 3, 4, 5]);
|
||||
assert!(user.notice.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newer_keymap_version_is_used_as_written_with_a_notice() {
|
||||
let user = parse_user_keymap(&format!("version = 3\n{LEGACY_TABS}")).unwrap();
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [0, 2, 3, 4, 5]);
|
||||
assert!(user.notice.unwrap().contains("newer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_inside_a_keymaps_entry_is_rejected() {
|
||||
// Appended at the end, TOML puts `version` into the last entry; the
|
||||
// file must not silently fall back to the legacy remap.
|
||||
let err = parse_user_keymap(
|
||||
"[[keymaps]]\nkey_sequence = \"4\"\ncommand = { GoToTab = 3 }\nversion = 2\n",
|
||||
)
|
||||
.err()
|
||||
.expect("misplaced version must be rejected");
|
||||
assert!(
|
||||
format!("{err:#}").contains("before the first [[keymaps]]"),
|
||||
"{err:#}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_keys_in_keymaps_entries_are_still_ignored() {
|
||||
let user = parse_user_keymap(
|
||||
"version = 2\n[[keymaps]]\nkey_sequence = \"4\"\ncommand = { GoToTab = 3 }\nnote = \"mine\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(go_to_tab_indices(&user.bindings), [3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keymap_version_zero_is_rejected() {
|
||||
assert!(parse_user_keymap("version = 0").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_user_digit_override_keeps_its_old_target() {
|
||||
let mut bindings = parse_bindings(DEFAULT_KEYMAP).unwrap();
|
||||
// An old file that swapped "4" and "5" (Settings / Logs).
|
||||
let user = parse_user_keymap(
|
||||
r#"
|
||||
[[keymaps]]
|
||||
key_sequence = "4"
|
||||
command = { GoToTab = 4 }
|
||||
|
||||
[[keymaps]]
|
||||
key_sequence = "5"
|
||||
command = { GoToTab = 3 }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
merge(&mut bindings, user.bindings);
|
||||
let mut km = Keymap {
|
||||
bindings,
|
||||
pending: Vec::new(),
|
||||
format: KeyCombinationFormat::default(),
|
||||
};
|
||||
let four = KeyCombination::new(KeyCode::Char('4'), KeyModifiers::NONE);
|
||||
let five = KeyCombination::new(KeyCode::Char('5'), KeyModifiers::NONE);
|
||||
let six = KeyCombination::new(KeyCode::Char('6'), KeyModifiers::NONE);
|
||||
// Still Logs and Settings, now at their new indices.
|
||||
assert_eq!(
|
||||
km.resolve(four, KeyContext::Library),
|
||||
KeyResolution::Action(Action::GoToTab(5))
|
||||
);
|
||||
assert_eq!(
|
||||
km.resolve(five, KeyContext::Library),
|
||||
KeyResolution::Action(Action::GoToTab(4))
|
||||
);
|
||||
// The new default "6" is untouched by the user's file.
|
||||
assert_eq!(
|
||||
km.resolve(six, KeyContext::Library),
|
||||
KeyResolution::Action(Action::GoToTab(5))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcasts_context_parses() {
|
||||
let bindings = parse_bindings(
|
||||
r#"
|
||||
[[keymaps]]
|
||||
key_sequence = "z"
|
||||
command = "ToggleHelp"
|
||||
context = "podcasts"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(bindings[0].context, KeyContext::Podcasts);
|
||||
assert_eq!(KeyContext::Podcasts.label(), "podcasts");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_podcast_keys_resolve_only_in_the_podcasts_context() {
|
||||
let mut km = keymap_from(DEFAULT_KEYMAP);
|
||||
let shift = |c| KeyCombination::new(KeyCode::Char(c), KeyModifiers::SHIFT);
|
||||
let cases = [
|
||||
(key!(o), Action::AddPodcast),
|
||||
(key!('/'), Action::SearchEpisodes),
|
||||
(shift('R'), Action::RefreshPodcast),
|
||||
(key!(ctrl - r), Action::RefreshAllPodcasts),
|
||||
(shift('S'), Action::CycleEpisodeSort),
|
||||
(key!(f), Action::CycleEpisodeFilter),
|
||||
(key!(m), Action::ToggleEpisodePlayed),
|
||||
(shift('U'), Action::UnsubscribePodcast),
|
||||
(key!(b), Action::RestartEpisode),
|
||||
];
|
||||
for (key, action) in cases {
|
||||
assert_eq!(
|
||||
km.resolve(key, KeyContext::Podcasts),
|
||||
KeyResolution::Action(action.clone()),
|
||||
"{action:?}"
|
||||
);
|
||||
assert_ne!(
|
||||
km.resolve(key, KeyContext::Library),
|
||||
KeyResolution::Action(action.clone()),
|
||||
"{action:?} leaks into the library"
|
||||
);
|
||||
}
|
||||
// Playback keys keep working on the Podcasts tab.
|
||||
assert_eq!(
|
||||
km.resolve(key!(space), KeyContext::Podcasts),
|
||||
KeyResolution::Action(Action::PlayPause)
|
||||
);
|
||||
assert_eq!(
|
||||
km.resolve(key!(n), KeyContext::Podcasts),
|
||||
KeyResolution::Action(Action::NextTrack)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_listening_history_key_resolves() {
|
||||
let mut km = keymap_from(DEFAULT_KEYMAP);
|
||||
|
||||
+557
-1071
File diff suppressed because it is too large.
Load diff
@@ -1 +0,0 @@
|
||||
{"type":"hello","group_id":"grp_1","profile":{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},"devices":[{"device_id":"tui_a","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5},{"device_id":"web_b","name":"TUI","client_version":"0.3.4","protocol_version":3,"endpoint_id":"e","endpoint_ticket":"t","revoked":false,"revoke_cutoff_seq":null,"updated_at_ms":5}],"vector":{"tui_a":12,"web_b":4},"ops":[{"op_id":"tui_a:1","origin_device_id":"tui_a","seq":1,"hlc_ms":1700000000001,"payload":{"kind":"track_like_set","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","liked":true,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}}},{"op_id":"tui_a:2","origin_device_id":"tui_a","seq":2,"hlc_ms":1700000000002,"payload":{"kind":"playlist_created","playlist_id":"pl","title":"Mix"}},{"op_id":"tui_a:3","origin_device_id":"tui_a","seq":3,"hlc_ms":1700000000003,"payload":{"kind":"playlist_renamed","playlist_id":"pl","title":"Mix 2"}},{"op_id":"tui_a:4","origin_device_id":"tui_a","seq":4,"hlc_ms":1700000000004,"payload":{"kind":"playlist_track_added","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","position":0,"fed":null}},{"op_id":"tui_a:5","origin_device_id":"tui_a","seq":5,"hlc_ms":1700000000005,"payload":{"kind":"playlist_track_removed","playlist_id":"pl","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111"}},{"op_id":"tui_a:6","origin_device_id":"tui_a","seq":6,"hlc_ms":1700000000006,"payload":{"kind":"playlist_deleted","playlist_id":"pl"}},{"op_id":"tui_a:7","origin_device_id":"tui_a","seq":7,"hlc_ms":1700000000007,"payload":{"kind":"device_profile_set","name":"TUI","client_version":"0.3.4","endpoint_ticket":"t","endpoint_id":"e"}},{"op_id":"tui_a:8","origin_device_id":"tui_a","seq":8,"hlc_ms":1700000000008,"payload":{"kind":"device_trusted","target_device_id":"web_b"}},{"op_id":"tui_a:9","origin_device_id":"tui_a","seq":9,"hlc_ms":1700000000009,"payload":{"kind":"device_revoked","target_device_id":"old","target_max_seq_seen":3}},{"op_id":"tui_a:10","origin_device_id":"tui_a","seq":10,"hlc_ms":1700000000010,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"set_state","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","track_number":null,"disc_number":null,"duration_seconds":0.0,"artist_names":[],"featured_artist_names":[],"release_id":0,"release_title":"","release_year":null,"file_path":"","content_id":null,"audio_format":null,"audio_bitrate":null,"audio_sample_rate":null,"audio_bit_depth":null,"file_size_bytes":null,"play_count":0,"fed":null}],"queue_pos":1,"playing":true,"paused":false,"idle_since_ms":null,"position_secs":42.5,"volume":73,"shuffle":true,"repeat":"all"},"seek":true},"authority":{"version":1,"claim":{"counter":3,"issuer":"tui_a","owner":"tui_a"},"sequence":9}}},{"op_id":"tui_a:11","origin_device_id":"tui_a","seq":11,"hlc_ms":1700000000011,"payload":{"kind":"playback_command","target_device_id":"web_b","command":{"kind":"active_changed","active_device_id":"web_b","active_device_name":"WEB","state":{"queue":[{"id":7,"title":"Song","track_number":1,"disc_number":null,"duration_seconds":180.5,"artist_names":["A"],"featured_artist_names":["B"],"release_id":3,"release_title":"Album","release_year":2001,"file_path":"","content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","audio_format":"flac","audio_bitrate":900,"audio_sample_rate":44100,"audio_bit_depth":16,"file_size_bytes":12345,"play_count":2,"fed":{"item_id":"item","owner":"owner","title":"Song","artist_names":["A"],"featured_artist_names":[],"year":2001,"duration_seconds":180,"content_id":"b3:1111111111111111111111111111111111111111111111111111111111111111","release_title":"Album","track_number":1,"disc_number":1}},{"id":0,"title":"Remote","trackLine truncated
|
||||
@@ -25,7 +25,6 @@ async fn localhost_web_peer() {
|
||||
library: Arc::new(Library::open(&dir.join("interop-library.sqlite3")).unwrap()),
|
||||
event_tx: Default::default(),
|
||||
playback: Default::default(),
|
||||
podcasts: Default::default(),
|
||||
};
|
||||
let id = sync.ensure_identity().unwrap();
|
||||
let (tx, mut events) = tokio::sync::mpsc::unbounded_channel();
|
||||
@@ -56,16 +55,13 @@ async fn localhost_web_peer() {
|
||||
vector: BTreeMap::new(),
|
||||
ops: vec![],
|
||||
snapshot: SyncSnapshot::default(),
|
||||
playback: sync.local_playback_snapshot_for(PROTOCOL_VERSION),
|
||||
playback: sync.local_playback_snapshot(),
|
||||
};
|
||||
let mut bytes = serde_json::to_vec(&hello).unwrap();
|
||||
bytes.push(b'\n');
|
||||
stream.write_all(&bytes).await.unwrap();
|
||||
let response: WireMessage =
|
||||
serde_json::from_slice(
|
||||
&music_dht::device_sync::read_line(&mut stream).await.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
serde_json::from_slice(&read_line(&mut stream).await.unwrap()).unwrap();
|
||||
let WireMessage::SyncResponse {
|
||||
accepted: true,
|
||||
playback: Some(snapshot),
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
+55
-492
@@ -30,7 +30,6 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
||||
library: Arc::new(Library::open(&dir.path().join("library.sqlite3")).unwrap()),
|
||||
event_tx: Default::default(),
|
||||
playback: Default::default(),
|
||||
podcasts: Default::default(),
|
||||
});
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
sync.set_event_tx(tx);
|
||||
@@ -55,18 +54,8 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
||||
let profile_b = b
|
||||
.own_profile(&service_b.ticket().await.unwrap().to_string())
|
||||
.unwrap();
|
||||
a.apply_device_profile(
|
||||
&profile_b,
|
||||
true,
|
||||
VersionReport::SelfReported(Some(profile_b.protocol_version)),
|
||||
)
|
||||
.unwrap();
|
||||
b.apply_device_profile(
|
||||
&profile_a,
|
||||
true,
|
||||
VersionReport::SelfReported(Some(profile_a.protocol_version)),
|
||||
)
|
||||
.unwrap();
|
||||
a.apply_device_profile(&profile_b, true).unwrap();
|
||||
b.apply_device_profile(&profile_a, true).unwrap();
|
||||
a.claim_playback(&profile_a.device_id).unwrap();
|
||||
a.playback_tick(true, true).unwrap();
|
||||
let mut state = empty_playback_state();
|
||||
@@ -174,12 +163,7 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
||||
silent_profile.device_id = "silent-peer".into();
|
||||
silent_profile.endpoint_id = silent.endpoint_id().to_string();
|
||||
silent_profile.endpoint_ticket = silent.ticket().await.unwrap().to_string();
|
||||
a.apply_device_profile(
|
||||
&silent_profile,
|
||||
true,
|
||||
VersionReport::SelfReported(Some(silent_profile.protocol_version)),
|
||||
)
|
||||
.unwrap();
|
||||
a.apply_device_profile(&silent_profile, true).unwrap();
|
||||
lock(&a.conn)
|
||||
.execute(
|
||||
"UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'",
|
||||
@@ -234,121 +218,6 @@ async fn localhost_devices_exchange_state_and_handoff() {
|
||||
.expect("localhost sync must complete within 30 seconds");
|
||||
}
|
||||
|
||||
/// Podcast state over the production stream handlers: a subscription and
|
||||
/// progress from A reach B through `sync_device` → `handle_hello`, and B's
|
||||
/// start over reaches A through the response of B's own sync.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn localhost_devices_sync_podcasts() {
|
||||
use super::podcast_sync_tests::{checkpoint, device_with_store, episode_ref, start, subscribe};
|
||||
tokio::time::timeout(Duration::from_secs(30), async {
|
||||
let dirs = [tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()];
|
||||
let network = NetworkId::from_name(&format!("podcast-sync-test-{}", random_hex(16)));
|
||||
let mut peers = Vec::new();
|
||||
let mut servers = Vec::new();
|
||||
for dir in &dirs {
|
||||
std::fs::create_dir_all(dir.path().join("network")).unwrap();
|
||||
let config = music_dht::MusicDhtConfig::builder()
|
||||
.data_dir(dir.path().join("network"))
|
||||
.network_id(network)
|
||||
.stream_protocol(SYNC_ALPN)
|
||||
.build()
|
||||
.unwrap();
|
||||
let (service, _events) = MusicDhtService::start(config).await.unwrap();
|
||||
let service = Arc::new(service);
|
||||
let store = Arc::new(
|
||||
furumi_library::podcasts::PodcastStore::open(&dir.path().join("podcasts.sqlite3"))
|
||||
.unwrap(),
|
||||
);
|
||||
let device = device_with_store(store, true);
|
||||
let sync = Arc::new(device.sync.clone());
|
||||
let stats = Arc::new(crate::federation::TransportStats::default());
|
||||
servers.push(tokio::spawn(serve_peers(
|
||||
service.stream_acceptor(SYNC_ALPN).unwrap(),
|
||||
sync.clone(),
|
||||
service.clone(),
|
||||
stats.clone(),
|
||||
)));
|
||||
peers.push((device, sync, service, stats));
|
||||
}
|
||||
let (a, sync_a, service_a, stats_a) = &peers[0];
|
||||
let (b, sync_b, service_b, stats_b) = &peers[1];
|
||||
sync_b
|
||||
.set_group_id(&sync_a.ensure_identity().unwrap().group_id)
|
||||
.unwrap();
|
||||
let profile_a = sync_a
|
||||
.own_profile(&service_a.ticket().await.unwrap().to_string())
|
||||
.unwrap();
|
||||
let profile_b = sync_b
|
||||
.own_profile(&service_b.ticket().await.unwrap().to_string())
|
||||
.unwrap();
|
||||
for (sync, profile) in [(sync_a, &profile_b), (sync_b, &profile_a)] {
|
||||
sync.apply_device_profile(
|
||||
profile,
|
||||
true,
|
||||
VersionReport::SelfReported(Some(profile.protocol_version)),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let podcast_id = subscribe(a).podcast_id;
|
||||
let episode = episode_ref(1);
|
||||
let session = start(a, &episode, 0);
|
||||
checkpoint(a, &session, 1_200_000);
|
||||
sync_a
|
||||
.sync_device(
|
||||
service_a.clone(),
|
||||
&sync_a.active_remote_devices().unwrap()[0],
|
||||
stats_a.clone(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let subscribed = b
|
||||
.store
|
||||
.list_podcasts(furumi_library::podcasts::PodcastListFilter::Subscribed)
|
||||
.unwrap();
|
||||
assert_eq!(subscribed.len(), 1);
|
||||
assert_eq!(subscribed[0].podcast_id, podcast_id);
|
||||
assert_eq!(
|
||||
b.store.resume_position_ms(&episode.episode_id).unwrap(),
|
||||
1_200_000
|
||||
);
|
||||
|
||||
// B starts over; its next sync carries the start over to A, which
|
||||
// then refuses A's older session.
|
||||
let store = Arc::clone(&b.store);
|
||||
sync_b
|
||||
.run_podcast_action(|stamps| {
|
||||
let write = store.restart(stamps, &episode, "restart-on-b")?;
|
||||
Ok(((), vec![write.op]))
|
||||
})
|
||||
.unwrap();
|
||||
sync_b
|
||||
.sync_device(
|
||||
service_b.clone(),
|
||||
&sync_b.active_remote_devices().unwrap()[0],
|
||||
stats_b.clone(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let progress = a.store.progress(&episode.episode_id).unwrap().unwrap();
|
||||
assert_eq!(progress.position_ms, 0);
|
||||
assert_eq!(progress.session_id, "restart-on-b");
|
||||
assert!(matches!(
|
||||
checkpoint(a, &session, 1_230_000),
|
||||
furumi_library::podcasts::CheckpointOutcome::Superseded { .. }
|
||||
));
|
||||
|
||||
for server in servers {
|
||||
server.abort();
|
||||
}
|
||||
for (_, _, service, _) in &peers {
|
||||
service.shutdown().await.unwrap();
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("localhost podcast sync must complete within 30 seconds");
|
||||
}
|
||||
|
||||
fn empty_playback_state() -> PlaybackStateWire {
|
||||
PlaybackStateWire {
|
||||
queue: vec![],
|
||||
@@ -360,7 +229,6 @@ fn empty_playback_state() -> PlaybackStateWire {
|
||||
volume: 80,
|
||||
shuffle: false,
|
||||
repeat: PlaybackRepeat::Off,
|
||||
playback_rate: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,7 +282,7 @@ fn coordination_ignores_legacy_commands_and_wrong_snapshot_sender() {
|
||||
coordination: None,
|
||||
});
|
||||
let gossip = sync
|
||||
.local_playback_snapshot_for(PROTOCOL_VERSION)
|
||||
.local_playback_snapshot()
|
||||
.unwrap()
|
||||
.coordination
|
||||
.unwrap();
|
||||
@@ -474,7 +342,6 @@ fn test_sync() -> DeviceSync {
|
||||
library: Arc::new(Library::open(&library_path).unwrap()),
|
||||
event_tx: Arc::new(std::sync::Mutex::new(None)),
|
||||
playback: Arc::new(std::sync::Mutex::new(PlaybackShared::default())),
|
||||
podcasts: Default::default(),
|
||||
};
|
||||
sync.ensure_identity().unwrap();
|
||||
sync
|
||||
@@ -557,25 +424,12 @@ fn test_fed_track(content_id: &str) -> crate::federation::FedTrack {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invites_round_trip_through_the_shared_encoding() {
|
||||
let invite = InviteWire {
|
||||
v: 1,
|
||||
ticket: "ticket".into(),
|
||||
device_id: "dev_a".into(),
|
||||
invite_id: "inv".into(),
|
||||
secret: "s3cr3t".into(),
|
||||
expires_at_ms: 10,
|
||||
protocol_version: None,
|
||||
};
|
||||
let link = encode_invite(&invite).unwrap();
|
||||
assert!(!link.contains('='));
|
||||
assert_eq!(parse_invite(&link).unwrap(), invite);
|
||||
assert!(
|
||||
parse_invite("frid://x/abc")
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains(":connect")
|
||||
);
|
||||
fn base64url_round_trip_without_padding() {
|
||||
for input in [b"".as_slice(), b"a", b"ab", b"abc", b"abcdef"] {
|
||||
let encoded = base64url_encode(input);
|
||||
assert!(!encoded.contains('='));
|
||||
assert_eq!(base64url_decode(&encoded).unwrap(), input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -598,35 +452,6 @@ fn tombstone_detection() {
|
||||
);
|
||||
}
|
||||
|
||||
fn music_track(id: i64, content_id: &str) -> TrackItem {
|
||||
TrackItem {
|
||||
id,
|
||||
title: "Local Song".to_string(),
|
||||
track_number: Some(1),
|
||||
disc_number: Some(1),
|
||||
duration_seconds: 180.0,
|
||||
artists: vec![ArtistRef {
|
||||
id: 1,
|
||||
name: "Local Artist".to_string(),
|
||||
}],
|
||||
featured_artists: Vec::new(),
|
||||
release_id: 2,
|
||||
release_title: "Local Release".to_string(),
|
||||
release_year: Some(2026),
|
||||
file_path: format!("/music/{id}.mp3"),
|
||||
content_id: Some(content_id.to_string()),
|
||||
cover_path: None,
|
||||
audio_format: Some("mp3".to_string()),
|
||||
audio_bitrate: Some(320),
|
||||
audio_sample_rate: Some(44_100),
|
||||
audio_bit_depth: None,
|
||||
file_size_bytes: Some(123_456),
|
||||
play_count: 3,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn playback_tracks_do_not_sync_device_local_paths() {
|
||||
let source = TrackItem {
|
||||
@@ -653,7 +478,6 @@ fn playback_tracks_do_not_sync_device_local_paths() {
|
||||
file_size_bytes: Some(123_456),
|
||||
play_count: 3,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
|
||||
let wire = PlaybackTrack::from_track(&source);
|
||||
@@ -741,7 +565,6 @@ fn playback_command_is_targeted_and_deduplicated() {
|
||||
volume: 42,
|
||||
shuffle: false,
|
||||
repeat: PlaybackRepeat::Off,
|
||||
playback_rate: None,
|
||||
},
|
||||
seek: false,
|
||||
};
|
||||
@@ -798,7 +621,6 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
||||
volume: 42,
|
||||
shuffle: false,
|
||||
repeat: PlaybackRepeat::Off,
|
||||
playback_rate: None,
|
||||
},
|
||||
seek: false,
|
||||
};
|
||||
@@ -807,7 +629,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
||||
.unwrap();
|
||||
sync.record_playback_command("dev_other", command).unwrap();
|
||||
|
||||
let target_ops = sync.ops_for_peer("dev_target", PROTOCOL_VERSION).unwrap();
|
||||
let target_ops = sync.ops_for_peer("dev_target").unwrap();
|
||||
assert_eq!(
|
||||
target_ops
|
||||
.iter()
|
||||
@@ -816,7 +638,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
||||
1
|
||||
);
|
||||
assert!(
|
||||
sync.ops_for_peer("dev_unknown", PROTOCOL_VERSION)
|
||||
sync.ops_for_peer("dev_unknown")
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
||||
@@ -831,7 +653,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
sync.ops_for_peer("dev_target", PROTOCOL_VERSION)
|
||||
sync.ops_for_peer("dev_target")
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|op| !matches!(op.payload, SyncOpPayload::PlaybackCommand { .. }))
|
||||
@@ -870,7 +692,6 @@ fn newer_device_trust_reactivates_revoked_device() {
|
||||
updated_at_ms: 20,
|
||||
},
|
||||
false,
|
||||
VersionReport::Relayed(PROTOCOL_VERSION),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!device_revoked(&sync, device_id));
|
||||
@@ -1116,311 +937,53 @@ fn stale_synced_playlist_item_metadata_repairs_pending_fed_track() {
|
||||
assert_eq!(detail.tracks[0].title, fed.title);
|
||||
}
|
||||
|
||||
fn podcast_meta() -> music_dht::podcast::PodcastMeta {
|
||||
music_dht::podcast::PodcastMeta {
|
||||
feed_url: music_dht::podcast::FeedUrl::parse("https://example.test/feed.xml").unwrap(),
|
||||
#[test]
|
||||
fn podcast_episode_survives_the_playback_wire_but_local_paths_do_not() {
|
||||
let podcast = crate::podcasts::Podcast {
|
||||
id: 1,
|
||||
feed_url: "https://example.test/feed.xml".into(),
|
||||
title: "Show".into(),
|
||||
author: None,
|
||||
description: None,
|
||||
link: None,
|
||||
image_url: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Rows of the local sync log: (op_id, seq, kind, payload_json, hlc_ms).
|
||||
fn logged_ops(sync: &DeviceSync) -> Vec<(String, i64, String, String, i64)> {
|
||||
let conn = lock(&sync.conn);
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT op_id, seq, kind, payload_json, hlc_ms FROM sync_ops ORDER BY seq")
|
||||
.unwrap();
|
||||
stmt.query_map([], |row| {
|
||||
Ok((
|
||||
row.get(0)?,
|
||||
row.get(1)?,
|
||||
row.get(2)?,
|
||||
row.get(3)?,
|
||||
row.get(4)?,
|
||||
))
|
||||
})
|
||||
.unwrap()
|
||||
.collect::<rusqlite::Result<Vec<_>>>()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_actions_are_appended_to_the_sync_log_with_their_stamps() {
|
||||
let sync = test_sync();
|
||||
let device_id = sync.ensure_identity().unwrap().device_id;
|
||||
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||
sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true)
|
||||
.unwrap();
|
||||
let outcome = sync
|
||||
.run_podcast_action(|stamps| {
|
||||
let outcome = store.subscribe(stamps, &podcast_meta())?;
|
||||
let ops = outcome.ops.clone();
|
||||
Ok((outcome, ops))
|
||||
})
|
||||
.unwrap();
|
||||
let ops = logged_ops(&sync);
|
||||
assert_eq!(ops.len(), 2, "{ops:?}");
|
||||
let (op_id, seq, kind, payload_json, hlc_ms) = &ops[1];
|
||||
assert_eq!(kind, "podcast_subscription_set");
|
||||
assert_eq!(*seq, 2);
|
||||
assert_eq!(*op_id, format!("{device_id}:2"));
|
||||
assert!(*hlc_ms > ops[0].4, "hlc keeps increasing");
|
||||
// The log carries the shared FRID payload, and the store's register
|
||||
// was written with exactly this stamp.
|
||||
let payload: music_dht::device_sync::SyncOpPayload =
|
||||
serde_json::from_str(payload_json).unwrap();
|
||||
assert!(payload.is_podcast());
|
||||
let register = store.subscription(&outcome.podcast_id).unwrap().unwrap();
|
||||
assert_eq!(register.op_id, *op_id);
|
||||
assert_eq!(register.hlc_ms, *hlc_ms);
|
||||
let vector = sync.vector().unwrap();
|
||||
assert_eq!(vector.get(&device_id), Some(&2));
|
||||
|
||||
// Unsubscribing emits two operations with consecutive stamps; a music
|
||||
// operation afterwards continues the same sequence.
|
||||
let unsubscribe = sync
|
||||
.run_podcast_action(|stamps| {
|
||||
let ops = store.unsubscribe(stamps, &outcome.podcast_id)?;
|
||||
Ok((ops.len(), ops))
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(unsubscribe, 2);
|
||||
sync.record_content_like(&format!("b3:{}", "b".repeat(64)), true)
|
||||
.unwrap();
|
||||
let ops = logged_ops(&sync);
|
||||
let kinds: Vec<(i64, &str)> = ops
|
||||
.iter()
|
||||
.map(|(_, seq, kind, ..)| (*seq, kind.as_str()))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
kinds,
|
||||
[
|
||||
(1, "track_like_set"),
|
||||
(2, "podcast_subscription_set"),
|
||||
(3, "podcast_subscription_set"),
|
||||
(4, "podcast_share_set"),
|
||||
(5, "track_like_set"),
|
||||
]
|
||||
);
|
||||
|
||||
// An action that emits nothing leaves the log alone.
|
||||
sync.run_podcast_action(|stamps| {
|
||||
let ops = store.unsubscribe(stamps, &outcome.podcast_id)?;
|
||||
Ok(((), ops))
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(logged_ops(&sync).len(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_ops_are_sent_only_to_v4_peers() {
|
||||
let sync = test_sync();
|
||||
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||
sync.run_podcast_action(|stamps| {
|
||||
let outcome = store.subscribe(stamps, &podcast_meta())?;
|
||||
let ops = outcome.ops.clone();
|
||||
Ok(((), ops))
|
||||
})
|
||||
.unwrap();
|
||||
sync.record_content_like(&format!("b3:{}", "a".repeat(64)), true)
|
||||
.unwrap();
|
||||
// A v3 peer cannot parse the podcast payload (an unknown `kind` breaks
|
||||
// its whole message), so its batch keeps only the music operation.
|
||||
let ops = sync.ops_for_peer("dev_peer", 3).unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert!(matches!(ops[0].payload, SyncOpPayload::TrackLikeSet { .. }));
|
||||
let ops = sync.ops_for_peer("dev_peer", 4).unwrap();
|
||||
assert_eq!(ops.len(), 2);
|
||||
assert!(ops[0].payload.is_podcast());
|
||||
// Unreported versions are the oldest protocol, except that every
|
||||
// `furumi/sync/2` peer keeps listen history.
|
||||
let event = sync
|
||||
.listen_event_for_track(
|
||||
sync.new_listen_id(),
|
||||
&music_track(7, &format!("b3:{}", "b".repeat(64))),
|
||||
1,
|
||||
120_000,
|
||||
music_dht::device_sync::ListenEndReason::Finished,
|
||||
)
|
||||
.unwrap();
|
||||
sync.record_listen(event).unwrap();
|
||||
let kinds: Vec<_> = sync
|
||||
.ops_for_peer(
|
||||
"dev_peer",
|
||||
music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION,
|
||||
)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|op| payload_kind(&op.payload))
|
||||
.collect();
|
||||
assert_eq!(kinds, ["track_like_set", "listen_recorded"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_podcast_actions_append_nothing() {
|
||||
let sync = test_sync();
|
||||
let store = furumi_library::podcasts::PodcastStore::open_in_memory().unwrap();
|
||||
let unknown = music_dht::podcast::PodcastId::from_feed_url(
|
||||
&music_dht::podcast::FeedUrl::parse("https://example.test/other.xml").unwrap(),
|
||||
);
|
||||
let result = sync.run_podcast_action(|stamps| {
|
||||
let ops = store.set_shared(stamps, &unknown, true)?;
|
||||
Ok(((), ops))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert!(logged_ops(&sync).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_episodes_never_enter_music_listening_history() {
|
||||
let sync = test_sync();
|
||||
let episode = crate::app::podcast_fixtures::episode_track(1);
|
||||
assert!(
|
||||
sync.listen_event_for_track(
|
||||
"listen-1".into(),
|
||||
&episode,
|
||||
1_000,
|
||||
120_000,
|
||||
music_dht::device_sync::ListenEndReason::Finished,
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
fn stored_version(sync: &DeviceSync, device_id: &str) -> Option<u16> {
|
||||
stored_protocol_version(&lock(&sync.conn), device_id).unwrap()
|
||||
}
|
||||
|
||||
fn profile_wire(device_id: &str, protocol_version: u16) -> DeviceProfileWire {
|
||||
DeviceProfileWire {
|
||||
device_id: device_id.to_string(),
|
||||
name: device_id.to_string(),
|
||||
client_version: "0.0.0".to_string(),
|
||||
protocol_version,
|
||||
endpoint_id: String::new(),
|
||||
endpoint_ticket: String::new(),
|
||||
revoked: false,
|
||||
revoke_cutoff_seq: None,
|
||||
updated_at_ms: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn profile_set_op(origin: &str, seq: i64, protocol_version: Option<u16>) -> SyncOpWire {
|
||||
SyncOpWire {
|
||||
op_id: format!("{origin}:{seq}"),
|
||||
origin_device_id: origin.to_string(),
|
||||
seq,
|
||||
hlc_ms: seq,
|
||||
payload: SyncOpPayload::DeviceProfileSet {
|
||||
name: origin.to_string(),
|
||||
client_version: "0.0.0".to_string(),
|
||||
endpoint_ticket: String::new(),
|
||||
endpoint_id: String::new(),
|
||||
protocol_version,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn peer_versions_come_from_the_peer_itself_never_from_this_build() {
|
||||
let sync = test_sync();
|
||||
sync.apply_device_trusted("dev_new", 1).unwrap();
|
||||
sync.apply_device_trusted("dev_old", 1).unwrap();
|
||||
// A device known only from `device_trusted` has reported nothing.
|
||||
assert_eq!(
|
||||
stored_version(&sync, "dev_new"),
|
||||
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||
);
|
||||
|
||||
// Its own profile announcement (v4) is a self-report.
|
||||
sync.apply_op(&profile_set_op("dev_new", 1, Some(4)))
|
||||
.unwrap();
|
||||
assert_eq!(stored_version(&sync, "dev_new"), Some(4));
|
||||
// An announcement of an older client carries no version: this device
|
||||
// keeps what it knows and never substitutes its own constant.
|
||||
sync.apply_op(&profile_set_op("dev_old", 1, None)).unwrap();
|
||||
assert_eq!(
|
||||
stored_version(&sync, "dev_old"),
|
||||
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||
);
|
||||
|
||||
// Relays of v1-v3 clients list every device with their own constant:
|
||||
// a relayed value only raises the stored version.
|
||||
sync.apply_device_profiles(
|
||||
&[profile_wire("dev_new", 3), profile_wire("dev_old", 3)],
|
||||
Some("dev_relay"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(stored_version(&sync, "dev_new"), Some(4));
|
||||
assert_eq!(stored_version(&sync, "dev_old"), Some(3));
|
||||
|
||||
// The sender's own entry of a `devices` list is a self-report, so a
|
||||
// downgraded device is noticed.
|
||||
sync.apply_device_profiles(&[profile_wire("dev_new", 3)], Some("dev_new"))
|
||||
.unwrap();
|
||||
assert_eq!(stored_version(&sync, "dev_new"), Some(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_pairing_keeps_the_version_the_requester_reported() {
|
||||
let sync = test_sync();
|
||||
let insert = |request_id: &str, device_id: &str, version: Option<i64>| {
|
||||
lock(&sync.conn)
|
||||
.execute(
|
||||
"INSERT INTO sync_pending_pairing
|
||||
(request_id, device_id, name, client_version, endpoint_id,
|
||||
endpoint_ticket, invite_id, created_at_ms, status, protocol_version)
|
||||
VALUES (?1, ?2, 'phone', '0.0.0', '', '', 'inv', 1, 'pending', ?3)",
|
||||
params![request_id, device_id, version],
|
||||
)
|
||||
.unwrap();
|
||||
cover_path: Some("/home/user/.cache/cover.jpg".into()),
|
||||
episode_count: 1,
|
||||
latest_published_at: None,
|
||||
last_checked_at: None,
|
||||
last_error: None,
|
||||
};
|
||||
insert("req_v3", "dev_v3", Some(3));
|
||||
// A request stored by an older build of this client has no version.
|
||||
insert("req_legacy", "dev_legacy", None);
|
||||
sync.answer_pairing("req_v3", true, false).unwrap();
|
||||
sync.answer_pairing("req_legacy", true, false).unwrap();
|
||||
assert_eq!(stored_version(&sync, "dev_v3"), Some(3));
|
||||
assert_eq!(
|
||||
stored_version(&sync, "dev_legacy"),
|
||||
Some(music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_protocol_version_is_announced_once_in_the_log() {
|
||||
let sync = test_sync();
|
||||
let device_id = sync.ensure_identity().unwrap().device_id;
|
||||
let announcements = |sync: &DeviceSync| {
|
||||
logged_ops(sync)
|
||||
.into_iter()
|
||||
.filter(|(_, _, kind, ..)| kind == "device_profile_set")
|
||||
.map(|(_, _, _, payload, _)| serde_json::from_str::<SyncOpPayload>(&payload).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
let episode = crate::podcasts::Episode {
|
||||
id: 42,
|
||||
podcast_id: 1,
|
||||
guid: "g".into(),
|
||||
title: "Episode".into(),
|
||||
description: None,
|
||||
published_at: None,
|
||||
duration_seconds: Some(60.0),
|
||||
audio_url: "https://cdn.example.test/42.mp3".into(),
|
||||
mime_type: Some("audio/mpeg".into()),
|
||||
byte_len: None,
|
||||
link: None,
|
||||
image_url: None,
|
||||
};
|
||||
// A fresh identity already reports this version in every hello.
|
||||
sync.own_profile("").unwrap();
|
||||
assert!(announcements(&sync).is_empty());
|
||||
// The row written by the previous release.
|
||||
lock(&sync.conn)
|
||||
.execute(
|
||||
"UPDATE sync_devices SET protocol_version = ?2 WHERE device_id = ?1",
|
||||
params![device_id, PROTOCOL_VERSION - 1],
|
||||
)
|
||||
.unwrap();
|
||||
sync.own_profile("").unwrap();
|
||||
sync.own_profile("").unwrap();
|
||||
let announced = announcements(&sync);
|
||||
assert_eq!(announced.len(), 1, "{announced:?}");
|
||||
assert!(matches!(
|
||||
announced[0],
|
||||
SyncOpPayload::DeviceProfileSet {
|
||||
protocol_version: Some(PROTOCOL_VERSION),
|
||||
..
|
||||
}
|
||||
));
|
||||
assert_eq!(stored_version(&sync, &device_id), Some(PROTOCOL_VERSION));
|
||||
let track = crate::podcasts::episode_track(&podcast, &episode);
|
||||
let round_trip = PlaybackTrack::from_track(&track).to_track_item();
|
||||
assert_eq!(round_trip.id, track.id);
|
||||
assert_eq!(round_trip.file_path, episode.audio_url);
|
||||
assert_eq!(
|
||||
crate::app::state::track_key(&round_trip),
|
||||
crate::app::state::track_key(&track)
|
||||
);
|
||||
|
||||
// Local files never leak their path; a URL without an episode id is
|
||||
// not trusted as a stream either.
|
||||
let mut local = track.clone();
|
||||
local.id = 7;
|
||||
local.file_path = "/home/user/Music/a.flac".into();
|
||||
assert!(PlaybackTrack::from_track(&local).file_path.is_empty());
|
||||
local.file_path = "https://cdn.example.test/other.mp3".into();
|
||||
let wire = PlaybackTrack::from_track(&local);
|
||||
assert_eq!(wire.file_path, "https://cdn.example.test/other.mp3");
|
||||
assert!(wire.to_track_item().file_path.is_empty());
|
||||
}
|
||||
@@ -1,576 +0,0 @@
|
||||
//! Golden wire-compatibility tests: the shared `music_dht::device_sync`
|
||||
//! types this client now uses must serialize v3 content byte-for-byte like
|
||||
//! the private copies of furumi-tui 0.3.x (kept verbatim in `legacy`
|
||||
//! below), so 0.3.x clients and furumi-web 0.12.x keep interoperating.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Private wire types of furumi-tui 0.3.x (`src/devices.rs` before sync
|
||||
/// v4), copied verbatim apart from visibility, `PartialEq` and doc comments.
|
||||
#[allow(dead_code)]
|
||||
pub(super) mod legacy {
|
||||
use music_dht::device_sync::ListenEvent;
|
||||
use music_dht::playback::CommandStamp;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct InviteWire {
|
||||
pub v: u16,
|
||||
#[serde(rename = "t")]
|
||||
pub ticket: String,
|
||||
#[serde(rename = "d")]
|
||||
pub device_id: String,
|
||||
#[serde(rename = "i")]
|
||||
pub invite_id: String,
|
||||
#[serde(rename = "s")]
|
||||
pub secret: String,
|
||||
#[serde(rename = "e")]
|
||||
pub expires_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DeviceProfileWire {
|
||||
pub device_id: String,
|
||||
pub name: String,
|
||||
pub client_version: String,
|
||||
pub protocol_version: u16,
|
||||
pub endpoint_id: String,
|
||||
pub endpoint_ticket: String,
|
||||
#[serde(default)]
|
||||
pub revoked: bool,
|
||||
#[serde(default)]
|
||||
pub revoke_cutoff_seq: Option<i64>,
|
||||
#[serde(default)]
|
||||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SyncedFedTrack {
|
||||
pub item_id: String,
|
||||
pub owner: String,
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
pub artist_names: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub featured_artist_names: Vec<String>,
|
||||
pub year: Option<i32>,
|
||||
pub duration_seconds: Option<i64>,
|
||||
pub content_id: String,
|
||||
pub release_title: Option<String>,
|
||||
pub track_number: Option<i32>,
|
||||
pub disc_number: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct PlaybackTrack {
|
||||
pub id: i64,
|
||||
pub title: String,
|
||||
pub track_number: Option<i32>,
|
||||
pub disc_number: Option<i32>,
|
||||
pub duration_seconds: f64,
|
||||
#[serde(default)]
|
||||
pub artist_names: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub featured_artist_names: Vec<String>,
|
||||
pub release_id: i64,
|
||||
pub release_title: String,
|
||||
pub release_year: Option<i32>,
|
||||
#[serde(default)]
|
||||
pub file_path: String,
|
||||
pub content_id: Option<String>,
|
||||
pub audio_format: Option<String>,
|
||||
pub audio_bitrate: Option<i32>,
|
||||
pub audio_sample_rate: Option<i32>,
|
||||
pub audio_bit_depth: Option<i32>,
|
||||
pub file_size_bytes: Option<i64>,
|
||||
#[serde(default)]
|
||||
pub play_count: i64,
|
||||
#[serde(default)]
|
||||
pub fed: Option<SyncedFedTrack>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum PlaybackRepeat {
|
||||
#[default]
|
||||
Off,
|
||||
One,
|
||||
All,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct PlaybackStateWire {
|
||||
#[serde(default)]
|
||||
pub queue: Vec<PlaybackTrack>,
|
||||
#[serde(default)]
|
||||
pub queue_pos: usize,
|
||||
pub playing: bool,
|
||||
pub paused: bool,
|
||||
#[serde(default)]
|
||||
pub idle_since_ms: Option<i64>,
|
||||
pub position_secs: f64,
|
||||
#[serde(default)]
|
||||
pub volume: u8,
|
||||
pub shuffle: bool,
|
||||
pub repeat: PlaybackRepeat,
|
||||
}
|
||||
|
||||
pub type PlaybackSnapshot = music_dht::playback::Snapshot<PlaybackStateWire>;
|
||||
pub type PlaybackCommand = music_dht::playback::Command<PlaybackStateWire>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SyncOpWire {
|
||||
pub op_id: String,
|
||||
pub origin_device_id: String,
|
||||
pub seq: i64,
|
||||
pub hlc_ms: i64,
|
||||
pub payload: SyncOpPayload,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum SyncOpPayload {
|
||||
TrackLikeSet {
|
||||
content_id: String,
|
||||
liked: bool,
|
||||
#[serde(default)]
|
||||
fed: Option<SyncedFedTrack>,
|
||||
},
|
||||
PlaylistCreated {
|
||||
playlist_id: String,
|
||||
title: String,
|
||||
},
|
||||
PlaylistRenamed {
|
||||
playlist_id: String,
|
||||
title: String,
|
||||
},
|
||||
PlaylistDeleted {
|
||||
playlist_id: String,
|
||||
},
|
||||
PlaylistTrackAdded {
|
||||
playlist_id: String,
|
||||
content_id: String,
|
||||
position: i64,
|
||||
#[serde(default)]
|
||||
fed: Option<SyncedFedTrack>,
|
||||
},
|
||||
PlaylistTrackRemoved {
|
||||
playlist_id: String,
|
||||
content_id: String,
|
||||
},
|
||||
DeviceProfileSet {
|
||||
name: String,
|
||||
client_version: String,
|
||||
endpoint_ticket: String,
|
||||
endpoint_id: String,
|
||||
},
|
||||
DeviceTrusted {
|
||||
target_device_id: String,
|
||||
},
|
||||
DeviceRevoked {
|
||||
target_device_id: String,
|
||||
target_max_seq_seen: i64,
|
||||
},
|
||||
PlaybackCommand {
|
||||
target_device_id: String,
|
||||
command: PlaybackCommand,
|
||||
#[serde(default)]
|
||||
authority: Option<CommandStamp>,
|
||||
},
|
||||
ListenRecorded {
|
||||
event: ListenEvent,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SyncSnapshot {
|
||||
#[serde(default)]
|
||||
pub likes: Vec<SnapshotLike>,
|
||||
#[serde(default)]
|
||||
pub unlikes: Vec<SnapshotLikeTombstone>,
|
||||
#[serde(default)]
|
||||
pub playlists: Vec<SnapshotPlaylist>,
|
||||
#[serde(default)]
|
||||
pub deleted_playlists: Vec<SnapshotPlaylistTombstone>,
|
||||
#[serde(default)]
|
||||
pub removed_playlist_items: Vec<SnapshotPlaylistItemTombstone>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotLike {
|
||||
pub content_id: String,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
#[serde(default)]
|
||||
pub fed: Option<SyncedFedTrack>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotLikeTombstone {
|
||||
pub content_id: String,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotPlaylist {
|
||||
pub playlist_id: String,
|
||||
pub title: String,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
#[serde(default)]
|
||||
pub items: Vec<SnapshotPlaylistItem>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotPlaylistTombstone {
|
||||
pub playlist_id: String,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotPlaylistItem {
|
||||
pub content_id: String,
|
||||
pub position: i64,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
#[serde(default)]
|
||||
pub fed: Option<SyncedFedTrack>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SnapshotPlaylistItemTombstone {
|
||||
pub playlist_id: String,
|
||||
pub content_id: String,
|
||||
pub hlc_ms: i64,
|
||||
pub op_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum WireMessage {
|
||||
PairRequest {
|
||||
invite_id: String,
|
||||
secret: String,
|
||||
profile: DeviceProfileWire,
|
||||
#[serde(default)]
|
||||
group_id: Option<String>,
|
||||
#[serde(default)]
|
||||
group_active_devices: usize,
|
||||
#[serde(default)]
|
||||
devices: Vec<DeviceProfileWire>,
|
||||
vector: BTreeMap<String, i64>,
|
||||
ops: Vec<SyncOpWire>,
|
||||
snapshot: SyncSnapshot,
|
||||
#[serde(default)]
|
||||
playback: Option<PlaybackSnapshot>,
|
||||
},
|
||||
PairResponse {
|
||||
accepted: bool,
|
||||
#[serde(default)]
|
||||
pending: bool,
|
||||
#[serde(default)]
|
||||
error: Option<String>,
|
||||
#[serde(default)]
|
||||
group_id: Option<String>,
|
||||
#[serde(default)]
|
||||
profile: Option<DeviceProfileWire>,
|
||||
#[serde(default)]
|
||||
devices: Vec<DeviceProfileWire>,
|
||||
#[serde(default)]
|
||||
vector: BTreeMap<String, i64>,
|
||||
#[serde(default)]
|
||||
ops: Vec<SyncOpWire>,
|
||||
#[serde(default)]
|
||||
snapshot: SyncSnapshot,
|
||||
#[serde(default)]
|
||||
playback: Option<PlaybackSnapshot>,
|
||||
},
|
||||
Hello {
|
||||
group_id: String,
|
||||
profile: DeviceProfileWire,
|
||||
devices: Vec<DeviceProfileWire>,
|
||||
vector: BTreeMap<String, i64>,
|
||||
ops: Vec<SyncOpWire>,
|
||||
snapshot: SyncSnapshot,
|
||||
#[serde(default)]
|
||||
playback: Option<PlaybackSnapshot>,
|
||||
},
|
||||
SyncResponse {
|
||||
accepted: bool,
|
||||
#[serde(default)]
|
||||
error: Option<String>,
|
||||
#[serde(default)]
|
||||
devices: Vec<DeviceProfileWire>,
|
||||
#[serde(default)]
|
||||
vector: BTreeMap<String, i64>,
|
||||
#[serde(default)]
|
||||
ops: Vec<SyncOpWire>,
|
||||
#[serde(default)]
|
||||
snapshot: SyncSnapshot,
|
||||
#[serde(default)]
|
||||
playback: Option<PlaybackSnapshot>,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const CONTENT_ID: &str = "b3:1111111111111111111111111111111111111111111111111111111111111111";
|
||||
|
||||
/// A v3 `hello` exercising every v3 operation kind, every snapshot section
|
||||
/// and a coordinated playback snapshot (input order is irrelevant).
|
||||
fn v3_hello_input() -> String {
|
||||
let fed = serde_json::json!({
|
||||
"item_id": "item", "owner": "owner", "title": "Song", "artist_names": ["A"],
|
||||
"featured_artist_names": [], "year": 2001, "duration_seconds": 180,
|
||||
"content_id": CONTENT_ID, "release_title": "Album", "track_number": 1, "disc_number": 1,
|
||||
});
|
||||
let track = serde_json::json!({
|
||||
"id": 7, "title": "Song", "track_number": 1, "disc_number": null,
|
||||
"duration_seconds": 180.5, "artist_names": ["A"], "featured_artist_names": ["B"],
|
||||
"release_id": 3, "release_title": "Album", "release_year": 2001, "file_path": "",
|
||||
"content_id": CONTENT_ID, "audio_format": "flac", "audio_bitrate": 900,
|
||||
"audio_sample_rate": 44100, "audio_bit_depth": 16, "file_size_bytes": 12345,
|
||||
"play_count": 2, "fed": fed,
|
||||
});
|
||||
let state = serde_json::json!({
|
||||
"queue": [track, {"id": 0, "title": "Remote", "track_number": null, "disc_number": null,
|
||||
"duration_seconds": 0.0, "release_id": 0, "release_title": "", "release_year": null,
|
||||
"content_id": null, "audio_format": null, "audio_bitrate": null,
|
||||
"audio_sample_rate": null, "audio_bit_depth": null, "file_size_bytes": null}],
|
||||
"queue_pos": 1, "playing": true, "paused": false, "idle_since_ms": null,
|
||||
"position_secs": 42.5, "volume": 73, "shuffle": true, "repeat": "all",
|
||||
});
|
||||
let claim = serde_json::json!({"counter": 3, "issuer": "tui_a", "owner": "tui_a"});
|
||||
let stamp = serde_json::json!({"version": 1, "claim": claim, "sequence": 9});
|
||||
let op = |seq: i64, payload: serde_json::Value| {
|
||||
serde_json::json!({"op_id": format!("tui_a:{seq}"), "origin_device_id": "tui_a",
|
||||
"seq": seq, "hlc_ms": 1_700_000_000_000i64 + seq, "payload": payload})
|
||||
};
|
||||
let profile = |id: &str| {
|
||||
serde_json::json!({"device_id": id, "name": "TUI", "client_version": "0.3.4",
|
||||
"protocol_version": 3, "endpoint_id": "e", "endpoint_ticket": "t",
|
||||
"revoked": false, "revoke_cutoff_seq": null, "updated_at_ms": 5})
|
||||
};
|
||||
serde_json::json!({
|
||||
"type": "hello",
|
||||
"group_id": "grp_1",
|
||||
"profile": profile("tui_a"),
|
||||
"devices": [profile("tui_a"), profile("web_b")],
|
||||
"vector": {"tui_a": 12, "web_b": 4},
|
||||
"ops": [
|
||||
op(1, serde_json::json!({"kind": "track_like_set", "content_id": CONTENT_ID, "liked": true, "fed": fed})),
|
||||
op(2, serde_json::json!({"kind": "playlist_created", "playlist_id": "pl", "title": "Mix"})),
|
||||
op(3, serde_json::json!({"kind": "playlist_renamed", "playlist_id": "pl", "title": "Mix 2"})),
|
||||
op(4, serde_json::json!({"kind": "playlist_track_added", "playlist_id": "pl", "content_id": CONTENT_ID, "position": 0, "fed": null})),
|
||||
op(5, serde_json::json!({"kind": "playlist_track_removed", "playlist_id": "pl", "content_id": CONTENT_ID})),
|
||||
op(6, serde_json::json!({"kind": "playlist_deleted", "playlist_id": "pl"})),
|
||||
op(7, serde_json::json!({"kind": "device_profile_set", "name": "TUI", "client_version": "0.3.4", "endpoint_ticket": "t", "endpoint_id": "e"})),
|
||||
op(8, serde_json::json!({"kind": "device_trusted", "target_device_id": "web_b"})),
|
||||
op(9, serde_json::json!({"kind": "device_revoked", "target_device_id": "old", "target_max_seq_seen": 3})),
|
||||
op(10, serde_json::json!({"kind": "playback_command", "target_device_id": "web_b",
|
||||
"command": {"kind": "set_state", "state": state, "seek": true}, "authority": stamp})),
|
||||
op(11, serde_json::json!({"kind": "playback_command", "target_device_id": "web_b",
|
||||
"command": {"kind": "active_changed", "active_device_id": "web_b", "active_device_name": "WEB", "state": state}})),
|
||||
op(12, serde_json::json!({"kind": "listen_recorded", "event": {"listen_id": "l1",
|
||||
"content_id": CONTENT_ID, "started_at_ms": 1, "listened_ms": 60000, "track_duration_ms": 180000,
|
||||
"ended_reason": "finished", "track": {"title": "Song", "artist_names": ["A"], "featured_artist_names": [], "release_title": null}}})),
|
||||
],
|
||||
"snapshot": {
|
||||
"likes": [{"content_id": CONTENT_ID, "hlc_ms": 1, "op_id": "tui_a:1", "fed": fed}],
|
||||
"unlikes": [{"content_id": CONTENT_ID, "hlc_ms": 2, "op_id": "tui_a:2"}],
|
||||
"playlists": [{"playlist_id": "pl", "title": "Mix", "hlc_ms": 3, "op_id": "tui_a:3",
|
||||
"items": [{"content_id": CONTENT_ID, "position": 0, "hlc_ms": 4, "op_id": "tui_a:4", "fed": null}]}],
|
||||
"deleted_playlists": [{"playlist_id": "gone", "hlc_ms": 5, "op_id": "tui_a:5"}],
|
||||
"removed_playlist_items": [{"playlist_id": "pl", "content_id": CONTENT_ID, "hlc_ms": 6, "op_id": "tui_a:6"}],
|
||||
},
|
||||
"playback": {"device_id": "tui_a", "device_name": "TUI", "active": true, "updated_at_ms": 9,
|
||||
"state": state, "coordination": {"version": 1, "claim": claim, "heartbeat": 4, "available": true, "playing": true}},
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn v3_other_inputs() -> Vec<String> {
|
||||
vec![
|
||||
r#"{"type":"sync_response","accepted":false,"error":"device is not trusted"}"#.into(),
|
||||
r#"{"type":"pair_response","accepted":false,"pending":true,"error":"pairing pending"}"#
|
||||
.into(),
|
||||
serde_json::json!({"type": "pair_request", "invite_id": "i", "secret": "s",
|
||||
"profile": {"device_id": "tui_a", "name": "TUI", "client_version": "0.3.4",
|
||||
"protocol_version": 3, "endpoint_id": "e", "endpoint_ticket": "t"},
|
||||
"vector": {}, "ops": [], "snapshot": {}})
|
||||
.to_string(),
|
||||
]
|
||||
}
|
||||
|
||||
/// Parses `input` with the 0.3.x types and the shared types and checks
|
||||
/// that both serialize to the same bytes, and that each side parses the
|
||||
/// other's output.
|
||||
fn assert_identical(input: &str) -> String {
|
||||
let old: legacy::WireMessage = serde_json::from_str(input).unwrap();
|
||||
let new: WireMessage = serde_json::from_str(input).unwrap();
|
||||
let old_json = serde_json::to_string(&old).unwrap();
|
||||
let new_json = serde_json::to_string(&new).unwrap();
|
||||
assert_eq!(old_json, new_json);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<legacy::WireMessage>(&new_json).unwrap(),
|
||||
old
|
||||
);
|
||||
assert_eq!(serde_json::from_str::<WireMessage>(&old_json).unwrap(), new);
|
||||
new_json
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v3_messages_serialize_like_furumi_tui_0_3() {
|
||||
let hello = assert_identical(&v3_hello_input());
|
||||
let golden = concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/src/devices/fixtures/v3_hello.golden.json"
|
||||
);
|
||||
if std::env::var_os("FURUMI_BLESS_GOLDEN").is_some() {
|
||||
// Only for a deliberate change of the v3 shape.
|
||||
std::fs::write(golden, format!("{hello}\n")).unwrap();
|
||||
}
|
||||
assert_eq!(hello, std::fs::read_to_string(golden).unwrap().trim_end());
|
||||
for input in v3_other_inputs() {
|
||||
assert_identical(&input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn music_queue_items_keep_their_v3_shape() {
|
||||
let track = TrackItem {
|
||||
id: 7,
|
||||
title: "Song".into(),
|
||||
track_number: Some(1),
|
||||
disc_number: None,
|
||||
duration_seconds: 180.5,
|
||||
artists: vec![ArtistRef {
|
||||
id: 1,
|
||||
name: "A".into(),
|
||||
}],
|
||||
featured_artists: Vec::new(),
|
||||
release_id: 3,
|
||||
release_title: "Album".into(),
|
||||
release_year: Some(2001),
|
||||
file_path: "/Users/me/Music/song.flac".into(),
|
||||
content_id: Some(CONTENT_ID.into()),
|
||||
cover_path: None,
|
||||
audio_format: Some("flac".into()),
|
||||
audio_bitrate: Some(900),
|
||||
audio_sample_rate: Some(44_100),
|
||||
audio_bit_depth: Some(16),
|
||||
file_size_bytes: Some(12_345),
|
||||
play_count: 2,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
let wire = PlaybackTrack::from_track(&track);
|
||||
let json = serde_json::to_string(&wire).unwrap();
|
||||
assert!(
|
||||
!json.contains("source"),
|
||||
"music items carry no source: {json}"
|
||||
);
|
||||
let old: legacy::PlaybackTrack = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(serde_json::to_string(&old).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invites_from_older_clients_stay_readable() {
|
||||
let old = legacy::InviteWire {
|
||||
v: 1,
|
||||
ticket: "ticket".into(),
|
||||
device_id: "tui_a".into(),
|
||||
invite_id: "inv".into(),
|
||||
secret: "s3cr3t".into(),
|
||||
expires_at_ms: 10,
|
||||
};
|
||||
let old_json = serde_json::to_string(&old).unwrap();
|
||||
assert_eq!(
|
||||
old_json,
|
||||
r#"{"v":1,"t":"ticket","d":"tui_a","i":"inv","s":"s3cr3t","e":10}"#
|
||||
);
|
||||
let shared: InviteWire = serde_json::from_str(&old_json).unwrap();
|
||||
assert_eq!(
|
||||
shared.inviter_protocol_version(),
|
||||
music_dht::device_sync::UNREPORTED_PROTOCOL_VERSION
|
||||
);
|
||||
assert_eq!(serde_json::to_string(&shared).unwrap(), old_json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v3_peers_cannot_parse_podcast_operations() {
|
||||
// Why podcast operations must be gated: an unknown `kind` breaks the
|
||||
// whole message for a 0.3.x peer, not one operation.
|
||||
let mut hello: serde_json::Value = serde_json::from_str(&v3_hello_input()).unwrap();
|
||||
hello["ops"].as_array_mut().unwrap().push(serde_json::json!({
|
||||
"op_id": "tui_a:13", "origin_device_id": "tui_a", "seq": 13, "hlc_ms": 13,
|
||||
"payload": {"kind": "podcast_share_set",
|
||||
"podcast_id": music_dht::podcast::PodcastId::from_feed_url(
|
||||
&music_dht::podcast::FeedUrl::parse("https://podcasts.example/feed.xml").unwrap()),
|
||||
"shared": false, "generation": 1},
|
||||
}));
|
||||
assert!(serde_json::from_value::<legacy::WireMessage>(hello.clone()).is_err());
|
||||
assert!(serde_json::from_value::<WireMessage>(hello).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v3_peers_would_drop_the_source_of_podcast_items() {
|
||||
// Why podcast playback must be gated: older types ignore `source` and
|
||||
// forward the episode as a broken music item.
|
||||
let episode = crate::app::podcast_fixtures::episode_track(1)
|
||||
.podcast
|
||||
.unwrap();
|
||||
let wire = PlaybackTrack::podcast_episode(episode);
|
||||
let json = serde_json::to_string(&wire).unwrap();
|
||||
assert!(json.contains(r#""source":{"kind":"podcast_episode""#));
|
||||
let old: legacy::PlaybackTrack = serde_json::from_str(&json).unwrap();
|
||||
let forwarded: PlaybackTrack =
|
||||
serde_json::from_str(&serde_json::to_string(&old).unwrap()).unwrap();
|
||||
assert_eq!(forwarded.source, None);
|
||||
assert_eq!(forwarded.id, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_reports_stay_readable_by_older_clients() {
|
||||
let link = encode_invite(&InviteWire {
|
||||
v: 1,
|
||||
ticket: "ticket".into(),
|
||||
device_id: "tui_a".into(),
|
||||
invite_id: "inv".into(),
|
||||
secret: "s3cr3t".into(),
|
||||
expires_at_ms: 10,
|
||||
protocol_version: Some(PROTOCOL_VERSION),
|
||||
})
|
||||
.unwrap();
|
||||
let invite = parse_invite(&link).unwrap();
|
||||
assert_eq!(invite.inviter_protocol_version(), PROTOCOL_VERSION);
|
||||
let json = serde_json::to_string(&invite).unwrap();
|
||||
assert!(
|
||||
json.ends_with(&format!(r#","p":{PROTOCOL_VERSION}}}"#)),
|
||||
"{json}"
|
||||
);
|
||||
// `p` is an unknown field for 0.3.x and is ignored there.
|
||||
assert_eq!(
|
||||
serde_json::from_str::<legacy::InviteWire>(&json)
|
||||
.unwrap()
|
||||
.invite_id,
|
||||
"inv"
|
||||
);
|
||||
|
||||
let announcement = SyncOpPayload::DeviceProfileSet {
|
||||
name: "TUI".into(),
|
||||
client_version: CLIENT_VERSION.into(),
|
||||
endpoint_ticket: "t".into(),
|
||||
endpoint_id: "e".into(),
|
||||
protocol_version: Some(PROTOCOL_VERSION),
|
||||
};
|
||||
let json = serde_json::to_string(&announcement).unwrap();
|
||||
assert!(json.ends_with(&format!(r#","protocol_version":{PROTOCOL_VERSION}}}"#)));
|
||||
let old: legacy::SyncOpPayload = serde_json::from_str(&json).unwrap();
|
||||
assert!(matches!(
|
||||
old,
|
||||
legacy::SyncOpPayload::DeviceProfileSet { .. }
|
||||
));
|
||||
}
|
||||
@@ -2333,7 +2333,6 @@ fn metadata_preview_track(
|
||||
.or(base.file_size_bytes),
|
||||
play_count: base.play_count,
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2949,7 +2948,6 @@ pub fn pending_track(fed: &FedTrack) -> TrackItem {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3022,7 +3020,6 @@ fn ephemeral_track(
|
||||
// Keep the federation reference: the cached track can still be
|
||||
// liked, re-downloaded and marked as federated in the lists.
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-51
@@ -225,16 +225,10 @@ impl JamManager {
|
||||
JamStatus::default()
|
||||
}
|
||||
|
||||
/// Publishes the host's playback to participants. Podcast episodes are
|
||||
/// private: participants get opaque placeholders (`redact_for_jam`),
|
||||
/// never their titles, feeds or URLs.
|
||||
pub fn publish_host_playback(&self, snapshot: PlaybackSnapshot) {
|
||||
let mut state = lock(&self.state);
|
||||
if state.host.invite.is_some() {
|
||||
state.host.playback = Some(PlaybackSnapshot {
|
||||
state: music_dht::device_sync::redact_for_jam(snapshot.state),
|
||||
..snapshot
|
||||
});
|
||||
state.host.playback = Some(snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -380,15 +374,7 @@ async fn serve_one(mut stream: ByteStream, manager: Arc<JamManager>) -> Result<(
|
||||
acknowledged.push(command.command_id.clone());
|
||||
if state.host.seen_commands.insert(command.command_id.clone()) {
|
||||
state.host.seen_order.push_back(command.command_id.clone());
|
||||
// Participants cannot add podcast episodes to the
|
||||
// host queue; placeholders are mapped back by the
|
||||
// host (`restore_jam_private_items`).
|
||||
if music_dht::device_sync::jam_command_allowed(&command.command) {
|
||||
let _ =
|
||||
manager.event_tx.send(AppEvent::JamCommand(command.command));
|
||||
} else {
|
||||
tracing::info!("ignored a Jam command that adds podcast episodes");
|
||||
}
|
||||
let _ = manager.event_tx.send(AppEvent::JamCommand(command.command));
|
||||
}
|
||||
}
|
||||
while state.host.seen_order.len() > 4096 {
|
||||
@@ -670,14 +656,8 @@ mod tests {
|
||||
.join(&invite, "dev_guest".into(), "Guest".into())
|
||||
.unwrap();
|
||||
|
||||
// The host plays a private podcast episode.
|
||||
let episode = crate::app::podcast_fixtures::episode_track(1)
|
||||
.podcast
|
||||
.unwrap();
|
||||
let playback_state = crate::devices::PlaybackStateWire {
|
||||
queue: vec![crate::devices::PlaybackTrack::podcast_episode(
|
||||
episode.clone(),
|
||||
)],
|
||||
queue: Vec::new(),
|
||||
queue_pos: 0,
|
||||
playing: false,
|
||||
paused: false,
|
||||
@@ -686,7 +666,6 @@ mod tests {
|
||||
volume: 73,
|
||||
shuffle: false,
|
||||
repeat: crate::devices::PlaybackRepeat::Off,
|
||||
playback_rate: None,
|
||||
};
|
||||
host.publish_host_playback(PlaybackSnapshot {
|
||||
coordination: None,
|
||||
@@ -711,27 +690,12 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(playback.device_id, "dev_host");
|
||||
assert_eq!(playback.state.volume, 73);
|
||||
// Guests get an opaque placeholder: no title, feed or URL.
|
||||
assert!(playback.state.queue[0].is_private_placeholder());
|
||||
let seen = serde_json::to_string(&playback).unwrap();
|
||||
assert!(!seen.contains(&episode.audio_url) && !seen.contains(&episode.episode_title));
|
||||
|
||||
// A guest cannot add an episode to the host's queue; its ordinary
|
||||
// command (with the placeholder) still arrives.
|
||||
guest
|
||||
.submit_command(PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire {
|
||||
queue: vec![crate::devices::PlaybackTrack::podcast_episode(episode)],
|
||||
..playback_state.clone()
|
||||
},
|
||||
seek: false,
|
||||
})
|
||||
.unwrap();
|
||||
guest
|
||||
.submit_command(PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire {
|
||||
paused: true,
|
||||
..playback.state
|
||||
..playback_state
|
||||
},
|
||||
seek: false,
|
||||
})
|
||||
@@ -746,17 +710,13 @@ mod tests {
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let PlaybackCommand::SetState {
|
||||
state: received,
|
||||
seek: false,
|
||||
} = command
|
||||
else {
|
||||
panic!("expected the guest's set_state");
|
||||
};
|
||||
// The command that adds an episode was sent first; had it been
|
||||
// forwarded, it would have arrived first.
|
||||
assert!(received.paused);
|
||||
assert!(received.queue[0].is_private_placeholder());
|
||||
assert!(matches!(
|
||||
command,
|
||||
PlaybackCommand::SetState {
|
||||
state: crate::devices::PlaybackStateWire { paused: true, .. },
|
||||
seek: false
|
||||
}
|
||||
));
|
||||
|
||||
server.abort();
|
||||
host_drain.abort();
|
||||
|
||||
+1
-3
@@ -66,9 +66,7 @@ fn main() -> Result<()> {
|
||||
// after local guards have been dropped and the terminal has been restored.
|
||||
let _stderr_capture = capture_stderr();
|
||||
let (keymap, keymap_warning) = config::keymap::Keymap::load();
|
||||
// The keymap message may be an informational notice; keep both so a
|
||||
// logging failure is not hidden behind it.
|
||||
let startup_warning = app::join_status_messages(keymap_warning, startup_warning);
|
||||
let startup_warning = keymap_warning.or(startup_warning);
|
||||
|
||||
// The app (tokio + TUI) runs on a worker thread; the main thread stays
|
||||
// dedicated to the OS media-key event loop — on macOS the system only
|
||||
|
||||
+30
-257
@@ -41,14 +41,6 @@ pub enum PlayerEvent {
|
||||
has_next: bool,
|
||||
},
|
||||
Failed(String),
|
||||
/// A seek was handled for the source started under `generation`
|
||||
/// ([`Controller::source_generation`]); `result` says whether the source
|
||||
/// actually moved to `position`.
|
||||
Seeked {
|
||||
generation: u64,
|
||||
position: Duration,
|
||||
result: Result<(), String>,
|
||||
},
|
||||
}
|
||||
|
||||
enum Command {
|
||||
@@ -59,17 +51,11 @@ enum Command {
|
||||
seekable: bool,
|
||||
volume: f32,
|
||||
},
|
||||
/// Play a source that was already opened, probed and positioned off
|
||||
/// this thread (remote podcast streams, whose probing and seeking wait
|
||||
/// on the network). Dropped unless `generation` is still current. It
|
||||
/// starts paused if the latest pause/resume says so
|
||||
/// ([`Controller::expect_source`]), not as it was when requested.
|
||||
/// Play a source that was already probed and decoded off this thread
|
||||
/// (remote podcast streams, whose probing waits on the network).
|
||||
PlaySource {
|
||||
source: DecodedSource,
|
||||
volume: f32,
|
||||
/// Where `source` was positioned before it was handed over.
|
||||
start: Duration,
|
||||
generation: u64,
|
||||
},
|
||||
/// Append the next track behind the current one without interrupting
|
||||
/// playback — rodio switches sources back to back (gapless-ish).
|
||||
@@ -111,13 +97,6 @@ pub struct Controller {
|
||||
tx: Sender<Command>,
|
||||
pub shared: Arc<Shared>,
|
||||
playback_allowed: Arc<AtomicBool>,
|
||||
/// Bumped by every start and stop; a source prepared in the background
|
||||
/// plays only if no start or stop happened since it was requested.
|
||||
generation: Arc<AtomicU64>,
|
||||
/// The user's latest play/pause intent for a source still being
|
||||
/// prepared: set by [`Controller::expect_source`], [`Controller::pause`]
|
||||
/// and [`Controller::resume`], read when the source is handed over.
|
||||
start_paused: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Controller {
|
||||
@@ -129,52 +108,7 @@ impl Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/// The current source generation: capture it when starting to prepare a
|
||||
/// source in the background and pass it to [`Controller::play_source`].
|
||||
pub fn source_generation(&self) -> u64 {
|
||||
self.generation.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
fn supersede(&self) {
|
||||
self.generation.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Whether a start or stop happened since `generation` was read; for
|
||||
/// cancelling background preparation and reads of a superseded source.
|
||||
pub fn superseded_check(&self, generation: u64) -> impl Fn() -> bool + Send + Sync + 'static {
|
||||
let current = Arc::clone(&self.generation);
|
||||
move || current.load(Ordering::SeqCst) != generation
|
||||
}
|
||||
|
||||
/// A source is being prepared in the background and should start
|
||||
/// paused or not. Pause and resume calls made before it is handed over
|
||||
/// update this intent, so a pause pressed while it connects is kept.
|
||||
pub fn expect_source(&self, paused: bool) {
|
||||
self.start_paused.store(paused, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Play a source decoded (and possibly positioned at `start`) outside
|
||||
/// the audio thread, built with [`decode_source_with`]. The audio thread
|
||||
/// drops it when another start or stop happened after `generation` was
|
||||
/// read, so a slow stream never interrupts what the user chose since.
|
||||
pub fn play_source(
|
||||
&self,
|
||||
source: DecodedSource,
|
||||
volume: f32,
|
||||
start: Duration,
|
||||
generation: u64,
|
||||
) {
|
||||
let _ = self.tx.send(Command::PlaySource {
|
||||
source,
|
||||
volume,
|
||||
start,
|
||||
generation,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn play(&self, reader: TrackReader, byte_len: Option<u64>, volume: f32) {
|
||||
self.supersede();
|
||||
self.start_paused.store(false, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Play {
|
||||
reader,
|
||||
byte_len,
|
||||
@@ -185,8 +119,6 @@ impl Controller {
|
||||
}
|
||||
|
||||
pub fn play_stream(&self, reader: TrackReader, mime_type: Option<String>, volume: f32) {
|
||||
self.supersede();
|
||||
self.start_paused.store(false, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Play {
|
||||
reader,
|
||||
byte_len: None,
|
||||
@@ -196,22 +128,25 @@ impl Controller {
|
||||
});
|
||||
}
|
||||
|
||||
/// Play an already decoded source, built with `decode_source_with`
|
||||
/// outside the audio thread.
|
||||
pub fn play_source(&self, source: DecodedSource, volume: f32) {
|
||||
let _ = self.tx.send(Command::PlaySource { source, volume });
|
||||
}
|
||||
|
||||
pub fn enqueue(&self, reader: TrackReader, byte_len: Option<u64>) {
|
||||
let _ = self.tx.send(Command::Enqueue { reader, byte_len });
|
||||
}
|
||||
|
||||
pub fn pause(&self) {
|
||||
self.start_paused.store(true, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Pause);
|
||||
}
|
||||
|
||||
pub fn resume(&self) {
|
||||
self.start_paused.store(false, Ordering::SeqCst);
|
||||
let _ = self.tx.send(Command::Resume);
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
self.supersede();
|
||||
let _ = self.tx.send(Command::Stop);
|
||||
}
|
||||
|
||||
@@ -230,73 +165,31 @@ pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Controller {
|
||||
let thread_shared = Arc::clone(&shared);
|
||||
let playback_allowed = Arc::new(AtomicBool::new(true));
|
||||
let thread_allowed = Arc::clone(&playback_allowed);
|
||||
let generation = Arc::new(AtomicU64::new(0));
|
||||
let thread_generation = Arc::clone(&generation);
|
||||
let start_paused = Arc::new(AtomicBool::new(false));
|
||||
let thread_start_paused = Arc::clone(&start_paused);
|
||||
std::thread::Builder::new()
|
||||
.name("audio".to_string())
|
||||
.spawn(move || {
|
||||
run(
|
||||
rx,
|
||||
thread_shared,
|
||||
thread_allowed,
|
||||
Generations {
|
||||
current: thread_generation,
|
||||
start_paused: thread_start_paused,
|
||||
},
|
||||
on_event,
|
||||
)
|
||||
})
|
||||
.spawn(move || run(rx, thread_shared, thread_allowed, on_event))
|
||||
.expect("spawning the audio thread cannot fail");
|
||||
Controller {
|
||||
tx,
|
||||
shared,
|
||||
playback_allowed,
|
||||
generation,
|
||||
start_paused,
|
||||
}
|
||||
}
|
||||
|
||||
/// Controller state the audio thread reads.
|
||||
struct Generations {
|
||||
current: Arc<AtomicU64>,
|
||||
start_paused: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
struct Output {
|
||||
/// Keeps the audio device open; dropping it stops the mixer.
|
||||
_device: MixerDeviceSink,
|
||||
player: Player,
|
||||
}
|
||||
|
||||
/// Audio-thread state besides the output device.
|
||||
struct Playing {
|
||||
track_loaded: bool,
|
||||
/// Position of the current source when it was appended: sources
|
||||
/// positioned before playback (resumed podcast episodes) start there,
|
||||
/// while rodio counts from zero until the next seek.
|
||||
base: Duration,
|
||||
/// Controller generation the current source was started under. When
|
||||
/// the controller moved on, the end of this source is not reported: a
|
||||
/// stop or another start is already on its way.
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
fn run(
|
||||
rx: Receiver<Command>,
|
||||
shared: Arc<Shared>,
|
||||
playback_allowed: Arc<AtomicBool>,
|
||||
generations: Generations,
|
||||
on_event: impl Fn(PlayerEvent),
|
||||
) {
|
||||
let generation = &generations.current;
|
||||
let mut output: Option<Output> = None;
|
||||
let mut playing = Playing {
|
||||
track_loaded: false,
|
||||
base: Duration::ZERO,
|
||||
generation: generation.load(Ordering::SeqCst),
|
||||
};
|
||||
let mut track_loaded = false;
|
||||
let mut last_len = 0usize;
|
||||
|
||||
loop {
|
||||
@@ -313,60 +206,26 @@ fn run(
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if let Command::PlaySource {
|
||||
generation: requested,
|
||||
..
|
||||
} = &command
|
||||
&& *requested != generation.load(Ordering::SeqCst)
|
||||
{
|
||||
// Superseded while it was being prepared.
|
||||
continue;
|
||||
}
|
||||
let started_under = match &command {
|
||||
Command::PlaySource {
|
||||
generation: requested,
|
||||
..
|
||||
} => Some(*requested),
|
||||
Command::Play { .. } | Command::Stop => Some(generation.load(Ordering::SeqCst)),
|
||||
_ => None,
|
||||
};
|
||||
// Commands change the source queue legitimately; resync the
|
||||
// length so the next tick doesn't read it as a track ending.
|
||||
handle(
|
||||
command,
|
||||
&shared,
|
||||
&generations.start_paused,
|
||||
&mut output,
|
||||
&mut playing,
|
||||
&on_event,
|
||||
);
|
||||
if let Some(started_under) = started_under {
|
||||
playing.generation = started_under;
|
||||
}
|
||||
handle(command, &shared, &mut output, &mut track_loaded, &on_event);
|
||||
last_len = output.as_ref().map_or(0, |out| out.player.len());
|
||||
}
|
||||
Err(RecvTimeoutError::Timeout) => {
|
||||
if let Some(out) = &output {
|
||||
let position = out.player.get_pos() + playing.base;
|
||||
shared
|
||||
.position_ms
|
||||
.store(position.as_millis() as u64, Ordering::Relaxed);
|
||||
.store(out.player.get_pos().as_millis() as u64, Ordering::Relaxed);
|
||||
shared
|
||||
.paused
|
||||
.store(out.player.is_paused(), Ordering::Relaxed);
|
||||
let len = out.player.len();
|
||||
if playing.track_loaded && len < last_len {
|
||||
// The next (gapless) source starts at zero.
|
||||
playing.base = Duration::ZERO;
|
||||
if track_loaded && len < last_len {
|
||||
if len == 0 {
|
||||
playing.track_loaded = false;
|
||||
track_loaded = false;
|
||||
}
|
||||
// A source that ended because it was superseded
|
||||
// (its stream cancelled) did not finish.
|
||||
if generation.load(Ordering::SeqCst) == playing.generation {
|
||||
for _ in len..last_len {
|
||||
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
||||
}
|
||||
for _ in len..last_len {
|
||||
on_event(PlayerEvent::TrackFinished { has_next: len > 0 });
|
||||
}
|
||||
}
|
||||
last_len = len;
|
||||
@@ -380,9 +239,8 @@ fn run(
|
||||
fn handle(
|
||||
command: Command,
|
||||
shared: &Arc<Shared>,
|
||||
start_paused: &AtomicBool,
|
||||
output: &mut Option<Output>,
|
||||
playing: &mut Playing,
|
||||
track_loaded: &mut bool,
|
||||
on_event: &impl Fn(PlayerEvent),
|
||||
) {
|
||||
match command {
|
||||
@@ -397,31 +255,16 @@ fn handle(
|
||||
return;
|
||||
};
|
||||
match decode_source(reader, byte_len, mime_type.as_deref(), seekable) {
|
||||
Ok(source) => start_source(out, source, volume, false, shared, on_event),
|
||||
Ok(source) => start_source(out, source, volume, shared, track_loaded, on_event),
|
||||
Err(err) => {
|
||||
on_event(PlayerEvent::Failed(format!("cannot decode track: {err}")));
|
||||
return;
|
||||
}
|
||||
}
|
||||
playing.track_loaded = true;
|
||||
playing.base = Duration::ZERO;
|
||||
}
|
||||
Command::PlaySource {
|
||||
source,
|
||||
volume,
|
||||
start,
|
||||
generation: _,
|
||||
} => {
|
||||
let Some(out) = ensure_output(output, on_event) else {
|
||||
return;
|
||||
};
|
||||
let paused = start_paused.load(Ordering::SeqCst);
|
||||
start_source(out, source, volume, paused, shared, on_event);
|
||||
playing.track_loaded = true;
|
||||
playing.base = start;
|
||||
shared
|
||||
.position_ms
|
||||
.store(start.as_millis() as u64, Ordering::Relaxed);
|
||||
Command::PlaySource { source, volume } => {
|
||||
if let Some(out) = ensure_output(output, on_event) {
|
||||
start_source(out, source, volume, shared, track_loaded, on_event);
|
||||
}
|
||||
}
|
||||
Command::Enqueue { reader, byte_len } => {
|
||||
let Some(out) = output.as_ref() else {
|
||||
@@ -454,25 +297,15 @@ fn handle(
|
||||
out.player.stop();
|
||||
}
|
||||
shared.analysis.clear();
|
||||
playing.track_loaded = false;
|
||||
playing.base = Duration::ZERO;
|
||||
*track_loaded = false;
|
||||
}
|
||||
Command::Seek(position) => {
|
||||
let result = match output {
|
||||
Some(out) => out.player.try_seek(position).map_err(|err| err.to_string()),
|
||||
None => Err("nothing is playing".to_string()),
|
||||
};
|
||||
match &result {
|
||||
// rodio now reports absolute positions.
|
||||
Ok(()) => playing.base = Duration::ZERO,
|
||||
Err(err) => tracing::warn!(%err, "seek failed"),
|
||||
if let Some(out) = output
|
||||
&& let Err(err) = out.player.try_seek(position)
|
||||
{
|
||||
tracing::warn!(%err, "seek failed");
|
||||
}
|
||||
shared.analysis.clear();
|
||||
on_event(PlayerEvent::Seeked {
|
||||
generation: playing.generation,
|
||||
position,
|
||||
result,
|
||||
});
|
||||
}
|
||||
Command::SetVolume(volume) => {
|
||||
if let Some(out) = output {
|
||||
@@ -511,8 +344,8 @@ fn start_source(
|
||||
out: &Output,
|
||||
source: DecodedSource,
|
||||
volume: f32,
|
||||
paused: bool,
|
||||
shared: &Shared,
|
||||
track_loaded: &mut bool,
|
||||
on_event: &impl Fn(PlayerEvent),
|
||||
) {
|
||||
shared.analysis.clear();
|
||||
@@ -522,11 +355,8 @@ fn start_source(
|
||||
source,
|
||||
Arc::clone(&shared.analysis),
|
||||
));
|
||||
if paused {
|
||||
out.player.pause();
|
||||
} else {
|
||||
out.player.play();
|
||||
}
|
||||
out.player.play();
|
||||
*track_loaded = true;
|
||||
on_event(PlayerEvent::Started);
|
||||
}
|
||||
|
||||
@@ -578,60 +408,3 @@ pub(crate) fn decode_source_with(
|
||||
.map(|decoder| Box::new(decoder) as DecodedSource)
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A controller without the audio thread; the commands it sends are
|
||||
/// returned instead (no audio device in tests).
|
||||
fn detached() -> (Controller, Receiver<Command>) {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let controller = Controller {
|
||||
tx,
|
||||
shared: Arc::default(),
|
||||
playback_allowed: Arc::new(AtomicBool::new(true)),
|
||||
generation: Arc::new(AtomicU64::new(0)),
|
||||
start_paused: Arc::new(AtomicBool::new(false)),
|
||||
};
|
||||
(controller, rx)
|
||||
}
|
||||
|
||||
/// The audio thread starts a prepared source with the intent read when
|
||||
/// the source arrives (`Command::PlaySource` reads `start_paused`), so a
|
||||
/// pause pressed while an episode was connecting is kept, and a resume
|
||||
/// pressed after a paused start is too.
|
||||
#[test]
|
||||
fn play_and_pause_pressed_while_a_source_is_prepared_are_kept() {
|
||||
let (controller, rx) = detached();
|
||||
controller.stop();
|
||||
let generation = controller.source_generation();
|
||||
let superseded = controller.superseded_check(generation);
|
||||
controller.expect_source(false);
|
||||
controller.pause();
|
||||
controller.play_source(
|
||||
Box::new(rodio::source::Zero::new(
|
||||
std::num::NonZero::new(1).unwrap(),
|
||||
std::num::NonZero::new(8_000).unwrap(),
|
||||
)),
|
||||
1.0,
|
||||
Duration::ZERO,
|
||||
generation,
|
||||
);
|
||||
let commands: Vec<_> = rx.try_iter().collect();
|
||||
assert!(matches!(
|
||||
commands.as_slice(),
|
||||
[Command::Stop, Command::Pause, Command::PlaySource { .. }]
|
||||
));
|
||||
assert!(controller.start_paused.load(Ordering::SeqCst));
|
||||
assert!(!superseded(), "pausing does not supersede the source");
|
||||
|
||||
controller.expect_source(true);
|
||||
controller.resume();
|
||||
assert!(!controller.start_paused.load(Ordering::SeqCst));
|
||||
|
||||
// Another start or a stop supersedes the prepared source.
|
||||
controller.stop();
|
||||
assert!(superseded());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
+119
-301
@@ -1,103 +1,32 @@
|
||||
//! HTTP for podcast feeds and cover images.
|
||||
//!
|
||||
//! Redirects are followed by hand so the caller learns where the feed
|
||||
//! really lives: the address reached by the leading permanent (301/308)
|
||||
//! hops is where the feed moved, and the store records it as the new fetch
|
||||
//! URL (the old one becomes an alias). Temporary hops after it (a CDN or a
|
||||
//! tracking prefix) do not move the feed further. Conditional requests
|
||||
//! (`If-None-Match` / `If-Modified-Since`), response size caps, timeouts and
|
||||
//! the user agent are fixed here; parsing and storage happen elsewhere.
|
||||
//! Async HTTP for podcast feeds and cover images.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use music_dht::podcast::{FeedUrl, PodcastId};
|
||||
use reqwest::header::{
|
||||
ACCEPT, ETAG, HeaderMap, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, RETRY_AFTER,
|
||||
};
|
||||
use reqwest::header::{ACCEPT, ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED};
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
use super::feed::{ParsedFeed, parse_feed_with_base};
|
||||
|
||||
/// Hard cap on a feed document (large catalogs are a few MiB).
|
||||
pub const MAX_FEED_BYTES: usize = 32 * 1024 * 1024;
|
||||
/// Hard cap on a cover image.
|
||||
pub const MAX_COVER_BYTES: usize = 10 * 1024 * 1024;
|
||||
/// Redirect hops followed before giving up.
|
||||
pub const MAX_REDIRECTS: usize = 10;
|
||||
/// TCP + TLS connection setup.
|
||||
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
/// Whole request, including the body, per hop.
|
||||
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
/// A whole feed or cover fetch, all redirect hops and the body included, so
|
||||
/// a slow server holds one of the few refresh slots for at most this long.
|
||||
#[cfg(not(test))]
|
||||
pub const FETCH_DEADLINE: Duration = Duration::from_secs(120);
|
||||
#[cfg(test)]
|
||||
pub const FETCH_DEADLINE: Duration = Duration::from_secs(3);
|
||||
/// Sent with every podcast request.
|
||||
pub const USER_AGENT: &str = concat!("furumi/", env!("CARGO_PKG_VERSION"), " (podcast reader)");
|
||||
|
||||
const FEED_ACCEPT: &str = "application/rss+xml, application/atom+xml, application/xml;q=0.9, \
|
||||
text/xml;q=0.9, */*;q=0.5";
|
||||
const FEED_ACCEPT: &str =
|
||||
"application/rss+xml, application/atom+xml, application/xml;q=0.9, text/xml;q=0.9, */*;q=0.5";
|
||||
|
||||
/// HTTP validators of the last successful fetch.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct Validators<'a> {
|
||||
pub etag: Option<&'a str>,
|
||||
pub last_modified: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// What a feed request returned.
|
||||
#[derive(Debug)]
|
||||
pub enum FeedBody {
|
||||
/// `304 Not Modified` to a conditional request.
|
||||
pub enum FeedFetch {
|
||||
NotModified,
|
||||
/// A new document with its validators.
|
||||
Modified {
|
||||
body: Vec<u8>,
|
||||
Fetched {
|
||||
feed: ParsedFeed,
|
||||
etag: Option<String>,
|
||||
last_modified: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
/// A feed response and the redirect chain that led to it.
|
||||
#[derive(Debug)]
|
||||
pub struct FeedResponse {
|
||||
pub body: FeedBody,
|
||||
/// URL that finally answered, canonicalized; the base for relative
|
||||
/// URLs in the document.
|
||||
pub final_url: FeedUrl,
|
||||
/// Where the leading permanent redirects (301/308, one or more, before
|
||||
/// any temporary one) lead: the feed's new address. `None` when the
|
||||
/// first hop was temporary or there was none. Temporary redirects
|
||||
/// (tracking prefixes, CDNs) never move a feed.
|
||||
pub moved_permanently_to: Option<FeedUrl>,
|
||||
/// Redirect hops followed.
|
||||
pub redirects: usize,
|
||||
}
|
||||
|
||||
/// A non-success HTTP status, with the server's `Retry-After` (seconds form)
|
||||
/// so the scheduler can back off as asked.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct HttpStatusError {
|
||||
pub status: u16,
|
||||
pub retry_after: Option<Duration>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for HttpStatusError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let reason = StatusCode::from_u16(self.status)
|
||||
.ok()
|
||||
.and_then(|status| status.canonical_reason())
|
||||
.unwrap_or("error");
|
||||
write!(f, "HTTP {} {reason}", self.status)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for HttpStatusError {}
|
||||
|
||||
/// Shared HTTP client for feeds and covers.
|
||||
pub struct Fetcher {
|
||||
client: reqwest::Client,
|
||||
covers_dir: PathBuf,
|
||||
@@ -107,268 +36,107 @@ impl Fetcher {
|
||||
pub fn new(covers_dir: PathBuf) -> Result<Fetcher> {
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
let client = reqwest::Client::builder()
|
||||
.connect_timeout(CONNECT_TIMEOUT)
|
||||
.timeout(REQUEST_TIMEOUT)
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.user_agent(USER_AGENT)
|
||||
.connect_timeout(Duration::from_secs(15))
|
||||
.timeout(Duration::from_secs(60))
|
||||
.redirect(reqwest::redirect::Policy::limited(10))
|
||||
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
|
||||
.build()
|
||||
.context("building the podcast HTTP client")?;
|
||||
Ok(Fetcher { client, covers_dir })
|
||||
}
|
||||
|
||||
/// GET of a feed. `validators` make it conditional; they are dropped
|
||||
/// after a permanent redirect because they belong to the old URL.
|
||||
/// Conditional GET of a feed. Parsing runs on the blocking pool.
|
||||
pub async fn fetch_feed(
|
||||
&self,
|
||||
url: &FeedUrl,
|
||||
validators: Validators<'_>,
|
||||
) -> Result<FeedResponse> {
|
||||
within_deadline("feed", self.fetch_feed_unbounded(url, validators)).await
|
||||
}
|
||||
|
||||
async fn fetch_feed_unbounded(
|
||||
&self,
|
||||
url: &FeedUrl,
|
||||
validators: Validators<'_>,
|
||||
) -> Result<FeedResponse> {
|
||||
let Followed {
|
||||
response,
|
||||
url: final_url,
|
||||
moved_permanently_to,
|
||||
redirects,
|
||||
conditional,
|
||||
} = self
|
||||
.get_following(url.as_str(), Some(FEED_ACCEPT), validators)
|
||||
.await?;
|
||||
let final_url = FeedUrl::parse(final_url.as_str()).map_err(|err| {
|
||||
anyhow!(
|
||||
"the feed was redirected to an unsupported URL {}: {err}",
|
||||
redact(&final_url)
|
||||
)
|
||||
})?;
|
||||
// An address that is not a valid feed URL is not worth moving to.
|
||||
let moved_permanently_to =
|
||||
moved_permanently_to.and_then(|url| FeedUrl::parse(url.as_str()).ok());
|
||||
url: &str,
|
||||
etag: Option<&str>,
|
||||
last_modified: Option<&str>,
|
||||
) -> Result<FeedFetch> {
|
||||
let mut request = self.client.get(url).header(ACCEPT, FEED_ACCEPT);
|
||||
if let Some(etag) = etag {
|
||||
request = request.header(IF_NONE_MATCH, etag);
|
||||
}
|
||||
if let Some(last_modified) = last_modified {
|
||||
request = request.header(IF_MODIFIED_SINCE, last_modified);
|
||||
}
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("fetching feed {url}"))?;
|
||||
let status = response.status();
|
||||
if status == StatusCode::NOT_MODIFIED {
|
||||
if !conditional {
|
||||
bail!("the server answered 304 Not Modified to an unconditional request");
|
||||
}
|
||||
return Ok(FeedResponse {
|
||||
body: FeedBody::NotModified,
|
||||
final_url,
|
||||
moved_permanently_to,
|
||||
redirects,
|
||||
});
|
||||
return Ok(FeedFetch::NotModified);
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(status_error(status, response.headers()).into());
|
||||
bail!("feed request failed: HTTP {status}");
|
||||
}
|
||||
let etag = header_string(response.headers(), ETAG);
|
||||
let last_modified = header_string(response.headers(), LAST_MODIFIED);
|
||||
let header = |name| {
|
||||
response
|
||||
.headers()
|
||||
.get(name)
|
||||
.and_then(|value: &reqwest::header::HeaderValue| value.to_str().ok())
|
||||
.map(str::to_string)
|
||||
};
|
||||
let etag = header(ETAG);
|
||||
let last_modified = header(LAST_MODIFIED);
|
||||
// Relative enclosure URLs resolve against where the feed really is
|
||||
// (after redirects).
|
||||
let base = response.url().to_string();
|
||||
let body = read_capped(response, MAX_FEED_BYTES, "feed").await?;
|
||||
Ok(FeedResponse {
|
||||
body: FeedBody::Modified {
|
||||
body,
|
||||
etag,
|
||||
last_modified,
|
||||
},
|
||||
final_url,
|
||||
moved_permanently_to,
|
||||
redirects,
|
||||
let feed = tokio::task::spawn_blocking(move || parse_feed_with_base(&body, Some(&base)))
|
||||
.await
|
||||
.map_err(|err| anyhow!("feed parser task failed: {err}"))??;
|
||||
Ok(FeedFetch::Fetched {
|
||||
feed,
|
||||
etag,
|
||||
last_modified,
|
||||
})
|
||||
}
|
||||
|
||||
/// Downloads a cover image into the covers directory; returns its path.
|
||||
pub async fn fetch_cover(&self, podcast_id: &PodcastId, image_url: &str) -> Result<PathBuf> {
|
||||
let body = within_deadline("cover image", self.download_cover(image_url)).await?;
|
||||
let format = image::guess_format(&body).context("the cover is not a supported image")?;
|
||||
/// Downloads a cover image into the covers directory and returns its path.
|
||||
pub async fn fetch_cover(&self, podcast_id: i64, image_url: &str) -> Result<PathBuf> {
|
||||
let response = self
|
||||
.client
|
||||
.get(image_url)
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("fetching cover {image_url}"))?;
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
bail!("cover request failed: HTTP {status}");
|
||||
}
|
||||
let body = read_capped(response, MAX_COVER_BYTES, "cover image").await?;
|
||||
let format = image::guess_format(&body).context("cover is not a supported image")?;
|
||||
let extension = format.extensions_str().first().copied().unwrap_or("img");
|
||||
let name = cover_file_name(podcast_id, image_url, extension);
|
||||
let hash = blake3::hash(image_url.as_bytes()).to_hex();
|
||||
let name = format!("{podcast_id}-{}.{extension}", &hash.as_str()[..16]);
|
||||
let dir = self.covers_dir.clone();
|
||||
tokio::task::spawn_blocking(move || write_atomically(&dir, &name, &body))
|
||||
.await
|
||||
.map_err(|err| anyhow!("cover writer task failed: {err}"))?
|
||||
}
|
||||
|
||||
async fn download_cover(&self, image_url: &str) -> Result<Vec<u8>> {
|
||||
let response = self
|
||||
.get_following(image_url, None, Validators::default())
|
||||
.await?
|
||||
.response;
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
return Err(status_error(status, response.headers()).into());
|
||||
}
|
||||
read_capped(response, MAX_COVER_BYTES, "cover image").await
|
||||
}
|
||||
|
||||
/// GET following up to [`MAX_REDIRECTS`] http(s) redirects by hand.
|
||||
async fn get_following(
|
||||
&self,
|
||||
url: &str,
|
||||
accept: Option<&str>,
|
||||
validators: Validators<'_>,
|
||||
) -> Result<Followed> {
|
||||
let mut current = Url::parse(url).with_context(|| format!("invalid URL {url:?}"))?;
|
||||
check_scheme(¤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})");
|
||||
bail!("{what} is too large ({len} bytes, limit {cap})");
|
||||
}
|
||||
let mut body = Vec::new();
|
||||
while let Some(chunk) = response
|
||||
.chunk()
|
||||
.await
|
||||
.map_err(reqwest::Error::without_url)
|
||||
.with_context(|| format!("reading the {what}"))?
|
||||
.with_context(|| format!("reading {what}"))?
|
||||
{
|
||||
if body.len() + chunk.len() > cap {
|
||||
bail!("the {what} is too large (over {cap} bytes)");
|
||||
bail!("{what} is too large (over {cap} bytes)");
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Stable per podcast and image URL, so a changed image gets a new file and
|
||||
/// the previous one can be removed.
|
||||
fn cover_file_name(podcast_id: &PodcastId, image_url: &str, extension: &str) -> String {
|
||||
let podcast = blake3::hash(podcast_id.as_str().as_bytes()).to_hex();
|
||||
let image = blake3::hash(image_url.as_bytes()).to_hex();
|
||||
format!("{}-{}.{extension}", &podcast[..16], &image[..16])
|
||||
}
|
||||
|
||||
fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result<PathBuf> {
|
||||
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
|
||||
@@ -387,3 +155,53 @@ fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result<PathBuf> {
|
||||
result?;
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
/// Whether `text` starts with `<scheme>://`. Only a prefix counts: a URL
|
||||
/// embedded in the query (`a.test/rss?src=https://b.test`) is not a scheme.
|
||||
fn has_scheme(text: &str) -> bool {
|
||||
let Some((scheme, rest)) = text.split_once(':') else {
|
||||
return false;
|
||||
};
|
||||
let mut chars = scheme.chars();
|
||||
chars.next().is_some_and(|c| c.is_ascii_alphabetic())
|
||||
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
|
||||
&& rest.starts_with("//")
|
||||
}
|
||||
|
||||
/// Normalizes user input into a feed URL: trims, maps `feed://`,
|
||||
/// `podcast://`, `pcast://` and `itpc://` to `https://`, defaults to
|
||||
/// `https://` without a scheme and accepts only http(s) URLs with a host.
|
||||
pub fn normalize_feed_url(input: &str) -> Result<String> {
|
||||
let trimmed = input.trim();
|
||||
if trimmed.is_empty() {
|
||||
bail!("feed URL is empty");
|
||||
}
|
||||
if trimmed.chars().any(char::is_whitespace) {
|
||||
bail!("feed URL must not contain whitespace");
|
||||
}
|
||||
let lower = trimmed.to_ascii_lowercase();
|
||||
let mut candidate = trimmed.to_string();
|
||||
for scheme in ["feed:", "podcast:", "pcast:", "itpc:"] {
|
||||
if lower.starts_with(scheme) {
|
||||
let rest = &trimmed[scheme.len()..];
|
||||
let rest_lower = rest.to_ascii_lowercase();
|
||||
candidate = if rest_lower.starts_with("http://") || rest_lower.starts_with("https://") {
|
||||
rest.to_string()
|
||||
} else {
|
||||
format!("https://{}", rest.trim_start_matches('/'))
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !has_scheme(&candidate) {
|
||||
candidate = format!("https://{candidate}");
|
||||
}
|
||||
let url = Url::parse(&candidate).with_context(|| format!("invalid feed URL {trimmed:?}"))?;
|
||||
if !matches!(url.scheme(), "http" | "https") {
|
||||
bail!("unsupported feed URL scheme {:?}", url.scheme());
|
||||
}
|
||||
if url.host_str().is_none_or(str::is_empty) {
|
||||
bail!("feed URL has no host");
|
||||
}
|
||||
Ok(url.to_string())
|
||||
}
|
||||
+314
-201
@@ -1,37 +1,22 @@
|
||||
//! Podcast runtime of the TUI: feed HTTP, refresh scheduling and the glue
|
||||
//! between the shared podcast store and the device-sync log.
|
||||
//! Podcast RSS/Atom subscriptions.
|
||||
//!
|
||||
//! The data model, identity rules, merge rules and the SQLite store are not
|
||||
//! defined here: they come from FRID (`music_dht::podcast` and
|
||||
//! `furumi_library::podcasts::PodcastStore`, file `podcasts.sqlite3`). This
|
||||
//! module only does what the contract leaves to clients:
|
||||
//! Furumi stores only subscription and episode *metadata* (SQLite, see
|
||||
//! [`store`]); audio stays on the publisher's host and is streamed on demand
|
||||
//! through [`stream`] when an episode is played. Adding a subscription never
|
||||
//! downloads episodes.
|
||||
//!
|
||||
//! * [`fetch`] downloads feeds (conditional GET, redirects reported so
|
||||
//! permanent moves become feed aliases, size and time limits) and covers;
|
||||
//! * [`refresh`] subscribes and refreshes feeds: it parses them with the
|
||||
//! shared `music_dht::podcast::feed` parser, ingests them with
|
||||
//! `PodcastStore::apply_feed` and keeps the local fetch state (validators,
|
||||
//! errors, back-off);
|
||||
//! * [`OpLog`] hands store actions the stamps of the device-sync log and
|
||||
//! appends the operations they return;
|
||||
//! * [`stream`] plays an episode straight from the publisher through a
|
||||
//! bounded HTTP Range reader, and [`playback`] resolves the current audio
|
||||
//! URL and resume position, opens and positions it;
|
||||
//! * [`progress`] writes resume positions, completions and listening
|
||||
//! sessions in order on its own thread.
|
||||
//!
|
||||
//! Only RSS metadata is stored. Audio stays on the publisher's server.
|
||||
//! * [`feed`] parses RSS 2.0 / RSS 1.0 / Atom documents.
|
||||
//! * [`fetch`] downloads feeds (conditional GET) and cover images.
|
||||
//! * [`store`] persists podcasts and episodes, deduplicated by GUID.
|
||||
//! * [`stream`] plays a remote episode through a bounded read-ahead buffer.
|
||||
//! * [`subscribe`] / [`refresh`] orchestrate the above without blocking
|
||||
//! Tokio workers.
|
||||
|
||||
pub mod feed;
|
||||
pub mod fetch;
|
||||
pub mod playback;
|
||||
pub mod progress;
|
||||
pub mod refresh;
|
||||
pub mod store;
|
||||
pub mod stream;
|
||||
|
||||
#[cfg(test)]
|
||||
mod playback_tests;
|
||||
#[cfg(test)]
|
||||
mod stream_tests;
|
||||
#[cfg(test)]
|
||||
mod test_server;
|
||||
#[cfg(test)]
|
||||
@@ -40,187 +25,74 @@ mod tests;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use furumi_library::podcasts::{
|
||||
LEGACY_PROTOTYPE_SCHEMA_VERSION, PODCAST_DB_FILE, PodcastListFilter, PodcastLocalOp,
|
||||
PodcastStore, PodcastStoreError, StampSource,
|
||||
};
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
|
||||
pub use fetch::Fetcher;
|
||||
use refresh::Services;
|
||||
use crate::library::models::{ArtistRef, TrackItem};
|
||||
|
||||
/// Appends podcast operations to the device-sync log.
|
||||
///
|
||||
/// [`OpLog::record`] gives `action` a [`StampSource`] that reserves stamps
|
||||
/// in the log. The action runs a `PodcastStore` local action and returns the
|
||||
/// operations it emitted; they are appended with exactly those stamps.
|
||||
pub trait OpLog: Send + Sync {
|
||||
fn record(
|
||||
&self,
|
||||
action: &mut dyn FnMut(&mut dyn StampSource) -> Result<Vec<PodcastLocalOp>>,
|
||||
) -> Result<()>;
|
||||
pub use fetch::{FeedFetch, Fetcher, normalize_feed_url};
|
||||
pub use store::Store;
|
||||
// Convenience re-exports used by the module's tests.
|
||||
#[cfg(test)]
|
||||
pub use feed::{ParsedEpisode, ParsedFeed, parse_feed, parse_feed_with_base};
|
||||
#[cfg(test)]
|
||||
pub use stream::HttpStream;
|
||||
|
||||
/// Track ids of podcast episodes are `-(EPISODE_TRACK_ID_BASE + episode.id)`
|
||||
/// for episode ids in `1..EPISODE_TRACK_ID_BASE`, i.e. the narrow band
|
||||
/// `-(2^41 - 1)..=-(2^40 + 1)`, far away from library rows (positive) and
|
||||
/// other synthetic ids.
|
||||
pub const EPISODE_TRACK_ID_BASE: i64 = 1 << 40;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Podcast {
|
||||
pub id: i64,
|
||||
pub feed_url: String,
|
||||
pub title: String,
|
||||
pub author: Option<String>,
|
||||
/// Plain text (HTML stripped, entities decoded, whitespace collapsed),
|
||||
/// at most 4000 characters.
|
||||
pub description: Option<String>,
|
||||
pub link: Option<String>,
|
||||
pub image_url: Option<String>,
|
||||
/// Absolute path of the locally cached cover image, if downloaded.
|
||||
pub cover_path: Option<String>,
|
||||
pub episode_count: usize,
|
||||
/// Unix seconds, max over episodes.
|
||||
pub latest_published_at: Option<i64>,
|
||||
/// Unix seconds of the last refresh attempt.
|
||||
pub last_checked_at: Option<i64>,
|
||||
/// Error of the last refresh attempt; `None` if it succeeded.
|
||||
pub last_error: Option<String>,
|
||||
}
|
||||
|
||||
/// The podcast store file. It is the file the prototype podcast build
|
||||
/// used, so an existing prototype database is migrated in place.
|
||||
pub fn db_path() -> Result<PathBuf> {
|
||||
let dirs = crate::config::project_dirs()
|
||||
.ok_or_else(|| anyhow!("no home directory for podcast data"))?;
|
||||
Ok(dirs.data_dir().join(PODCAST_DB_FILE))
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Episode {
|
||||
/// SQLite rowid, stable across refreshes.
|
||||
pub id: i64,
|
||||
pub podcast_id: i64,
|
||||
pub guid: String,
|
||||
pub title: String,
|
||||
/// Plain text, at most 4000 characters.
|
||||
pub description: Option<String>,
|
||||
/// Unix seconds.
|
||||
pub published_at: Option<i64>,
|
||||
pub duration_seconds: Option<f64>,
|
||||
pub audio_url: String,
|
||||
pub mime_type: Option<String>,
|
||||
pub byte_len: Option<u64>,
|
||||
pub link: Option<String>,
|
||||
pub image_url: Option<String>,
|
||||
}
|
||||
|
||||
/// Replaceable cache of downloaded podcast covers.
|
||||
pub fn covers_dir() -> Result<PathBuf> {
|
||||
let dirs = crate::config::project_dirs()
|
||||
.ok_or_else(|| anyhow!("no cache directory for podcast covers"))?;
|
||||
Ok(dirs.cache_dir().join("podcasts").join("covers"))
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct RefreshSummary {
|
||||
pub new_episodes: usize,
|
||||
pub updated_episodes: usize,
|
||||
pub not_modified: bool,
|
||||
}
|
||||
|
||||
/// Opens the podcast store at `db_path` and builds the feed client.
|
||||
///
|
||||
/// `PodcastStore::open` migrates a prototype database in place and refuses,
|
||||
/// without writing anything, a database from a newer Furumi or a file that
|
||||
/// is not a podcast database. Blocking: call it on the blocking pool.
|
||||
pub fn open_services(db_path: &Path, covers_dir: PathBuf, log: Arc<dyn OpLog>) -> Result<Services> {
|
||||
// The import commits inside `PodcastStore::open`; dropping its
|
||||
// validators happens afterwards. A marker written before the import and
|
||||
// removed only once every podcast was handled makes that step resumable
|
||||
// after a crash or a failed write in between.
|
||||
let marker = validators_marker(db_path);
|
||||
let mut pending = marker.is_file();
|
||||
if is_prototype_database(db_path) {
|
||||
if let Err(err) = std::fs::write(&marker, b"") {
|
||||
tracing::warn!("cannot record the pending prototype feed checks: {err}");
|
||||
}
|
||||
pending = true;
|
||||
}
|
||||
let store = PodcastStore::open(db_path)?;
|
||||
tracing::info!(path = %db_path.display(), "podcast store opened");
|
||||
if pending && forget_prototype_validators(&store) {
|
||||
match std::fs::remove_file(&marker) {
|
||||
Ok(()) => {}
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(err) => tracing::warn!("cannot remove {}: {err}", marker.display()),
|
||||
}
|
||||
}
|
||||
match store.legacy_quarantine() {
|
||||
Ok(entries) if !entries.is_empty() => tracing::warn!(
|
||||
count = entries.len(),
|
||||
"some prototype podcast rows could not be imported; they are kept in the legacy_* tables"
|
||||
),
|
||||
Ok(_) => {}
|
||||
Err(err) => tracing::debug!("reading the podcast import quarantine failed: {err:#}"),
|
||||
}
|
||||
Ok(Services {
|
||||
store: Arc::new(store),
|
||||
fetcher: Arc::new(Fetcher::new(covers_dir)?),
|
||||
log,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `path` is a database of the prototype podcast build, which
|
||||
/// `PodcastStore::open` is about to import. Read-only; any doubt is "no".
|
||||
fn is_prototype_database(path: &Path) -> bool {
|
||||
if !path.is_file() {
|
||||
return false;
|
||||
}
|
||||
let flags =
|
||||
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX;
|
||||
rusqlite::Connection::open_with_flags(path, flags)
|
||||
.and_then(|conn| conn.query_row("PRAGMA user_version", [], |row| row.get::<_, i64>(0)))
|
||||
.is_ok_and(|version| version == LEGACY_PROTOTYPE_SCHEMA_VERSION)
|
||||
}
|
||||
|
||||
/// Marker next to the podcast store: the validators imported from a
|
||||
/// prototype database still have to be dropped
|
||||
/// ([`forget_prototype_validators`]).
|
||||
fn validators_marker(db_path: &Path) -> PathBuf {
|
||||
let mut name = db_path
|
||||
.file_name()
|
||||
.map(std::ffi::OsStr::to_os_string)
|
||||
.unwrap_or_default();
|
||||
name.push(".full-check-pending");
|
||||
db_path.with_file_name(name)
|
||||
}
|
||||
|
||||
/// Drops the HTTP validators imported from a prototype database. Returns
|
||||
/// whether every podcast was handled (otherwise it runs again at the next
|
||||
/// start).
|
||||
///
|
||||
/// The prototype stored no seasons, episode numbers or episode types, and
|
||||
/// its GUID-less episodes stay provisional until a feed is parsed again. With
|
||||
/// the imported `ETag` / `Last-Modified` the first check would be answered
|
||||
/// "304 Not Modified" and the feed never parsed while it does not change, so
|
||||
/// a finished series would never show its seasons. Without them the first
|
||||
/// check downloads and applies the feed. The fetch state is local (never
|
||||
/// synced).
|
||||
///
|
||||
/// Only the public fetch-state API exists for this: the validators are
|
||||
/// cleared by recording a success at the last check, keeping the next check
|
||||
/// time, and an imported error is recorded again as many times as it was
|
||||
/// counted, so the back-off stays as imported. The one side effect is that a
|
||||
/// podcast imported with an error gets a last success time (its last check),
|
||||
/// which is not shown while the error is. Running this again only costs one
|
||||
/// unconditional feed download per podcast. Contract gap: FRID's prototype
|
||||
/// import should not carry the validators over.
|
||||
fn forget_prototype_validators(store: &PodcastStore) -> bool {
|
||||
let result = (|| -> Result<usize> {
|
||||
let mut cleared = 0;
|
||||
for podcast in store.list_podcasts(PodcastListFilter::All)? {
|
||||
let id = &podcast.podcast_id;
|
||||
let state = store.fetch_state(id)?;
|
||||
if state.etag.is_none() && state.last_modified.is_none() {
|
||||
continue;
|
||||
}
|
||||
let checked = state
|
||||
.last_success_at_ms
|
||||
.or(state.checked_at_ms)
|
||||
.unwrap_or(0);
|
||||
store.record_fetch_success(id, checked, None, None, state.next_check_at_ms)?;
|
||||
if let Some(error) = &state.last_error {
|
||||
for _ in 0..state.consecutive_failures.max(1) {
|
||||
store.record_fetch_failure(
|
||||
id,
|
||||
state.checked_at_ms.unwrap_or(checked),
|
||||
error,
|
||||
state.next_check_at_ms,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
cleared += 1;
|
||||
}
|
||||
Ok(cleared)
|
||||
})();
|
||||
match result {
|
||||
Ok(cleared) => {
|
||||
if cleared > 0 {
|
||||
tracing::info!(
|
||||
cleared,
|
||||
"imported podcasts get a full first feed check (prototype validators dropped)"
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
"dropping imported podcast validators failed (retried at the next start): {err:#}"
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A readable reason why the store did not open; the typed store errors
|
||||
/// already say that the file was left unchanged.
|
||||
pub fn describe_open_error(err: &anyhow::Error) -> String {
|
||||
match err.downcast_ref::<PodcastStoreError>() {
|
||||
Some(store_error) => store_error.to_string(),
|
||||
None => format!("{err:#}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs blocking store or filesystem work off the Tokio workers.
|
||||
pub(crate) async fn blocking<T, F>(work: F) -> Result<T>
|
||||
/// Runs blocking store/filesystem work off the Tokio workers.
|
||||
async fn blocking<T, F>(work: F) -> Result<T>
|
||||
where
|
||||
T: Send + 'static,
|
||||
F: FnOnce() -> Result<T> + Send + 'static,
|
||||
@@ -229,3 +101,244 @@ where
|
||||
.await
|
||||
.map_err(|err| anyhow!("background task failed: {err}"))?
|
||||
}
|
||||
|
||||
/// Subscribes to a feed: normalizes the URL, rejects duplicates, fetches and
|
||||
/// parses the feed (nothing is stored if it is not a feed), stores the
|
||||
/// podcast with all episode metadata, then downloads the cover (best effort).
|
||||
pub async fn subscribe(
|
||||
store: Arc<Store>,
|
||||
fetcher: &Fetcher,
|
||||
input_url: &str,
|
||||
now: i64,
|
||||
) -> Result<i64> {
|
||||
let feed_url = normalize_feed_url(input_url)?;
|
||||
let existing = {
|
||||
let store = Arc::clone(&store);
|
||||
let feed_url = feed_url.clone();
|
||||
blocking(move || store.podcast_by_feed_url(&feed_url)).await?
|
||||
};
|
||||
if let Some(podcast) = existing {
|
||||
bail!("already subscribed to {}", podcast.title);
|
||||
}
|
||||
let (feed, etag, last_modified) = match fetcher.fetch_feed(&feed_url, None, None).await? {
|
||||
FeedFetch::Fetched {
|
||||
feed,
|
||||
etag,
|
||||
last_modified,
|
||||
} => (feed, etag, last_modified),
|
||||
FeedFetch::NotModified => {
|
||||
bail!("server answered 304 Not Modified to an unconditional request")
|
||||
}
|
||||
};
|
||||
let image_url = feed.image_url.clone();
|
||||
let podcast_id = {
|
||||
let store = Arc::clone(&store);
|
||||
blocking(move || {
|
||||
let id = store.add_podcast(&feed_url, &feed, now)?;
|
||||
store.set_http_validators(id, etag.as_deref(), last_modified.as_deref())?;
|
||||
Ok(id)
|
||||
})
|
||||
.await?
|
||||
};
|
||||
if let Some(image_url) = image_url {
|
||||
update_cover(&store, fetcher, podcast_id, &image_url).await;
|
||||
}
|
||||
Ok(podcast_id)
|
||||
}
|
||||
|
||||
/// Refreshes one subscription with a conditional request. Failures are
|
||||
/// recorded on the podcast (`last_error`) and returned.
|
||||
pub async fn refresh(
|
||||
store: Arc<Store>,
|
||||
fetcher: &Fetcher,
|
||||
podcast_id: i64,
|
||||
now: i64,
|
||||
) -> Result<RefreshSummary> {
|
||||
let (podcast, (etag, last_modified)) = {
|
||||
let store = Arc::clone(&store);
|
||||
blocking(move || {
|
||||
let podcast = store
|
||||
.podcast(podcast_id)?
|
||||
.ok_or_else(|| anyhow!("podcast {podcast_id} not found"))?;
|
||||
Ok((podcast, store.http_validators(podcast_id)?))
|
||||
})
|
||||
.await?
|
||||
};
|
||||
let fetched = fetcher
|
||||
.fetch_feed(&podcast.feed_url, etag.as_deref(), last_modified.as_deref())
|
||||
.await;
|
||||
let (summary, image_url) = match fetched {
|
||||
Err(err) => {
|
||||
let message = format!("{err:#}");
|
||||
let store = Arc::clone(&store);
|
||||
if let Err(record_err) =
|
||||
blocking(move || store.record_check(podcast_id, now, Some(&message))).await
|
||||
{
|
||||
tracing::warn!("recording podcast refresh error failed: {record_err:#}");
|
||||
}
|
||||
return Err(err);
|
||||
}
|
||||
Ok(FeedFetch::NotModified) => {
|
||||
let store = Arc::clone(&store);
|
||||
blocking(move || store.record_check(podcast_id, now, None)).await?;
|
||||
let summary = RefreshSummary {
|
||||
not_modified: true,
|
||||
..RefreshSummary::default()
|
||||
};
|
||||
(summary, podcast.image_url)
|
||||
}
|
||||
Ok(FeedFetch::Fetched {
|
||||
feed,
|
||||
etag,
|
||||
last_modified,
|
||||
}) => {
|
||||
let image_url = feed.image_url.clone();
|
||||
let store = Arc::clone(&store);
|
||||
let applied = blocking(move || {
|
||||
let summary = store.apply_feed(podcast_id, &feed, now);
|
||||
match &summary {
|
||||
Ok(_) => store.set_http_validators(
|
||||
podcast_id,
|
||||
etag.as_deref(),
|
||||
last_modified.as_deref(),
|
||||
)?,
|
||||
Err(err) => {
|
||||
store.record_check(podcast_id, now, Some(&format!("{err:#}")))?;
|
||||
}
|
||||
}
|
||||
summary
|
||||
})
|
||||
.await?;
|
||||
(applied, image_url)
|
||||
}
|
||||
};
|
||||
if let Some(image_url) = image_url {
|
||||
refresh_cover(&store, fetcher, podcast_id, &image_url).await;
|
||||
}
|
||||
Ok(summary)
|
||||
}
|
||||
|
||||
/// Re-downloads the cover when the feed image changed or the cached file is
|
||||
/// gone. Best effort: failures are only logged.
|
||||
async fn refresh_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
|
||||
let source = {
|
||||
let store = Arc::clone(store);
|
||||
blocking(move || {
|
||||
let (url, path) = store.cover_source(podcast_id)?;
|
||||
let missing = path
|
||||
.as_deref()
|
||||
.is_none_or(|path| !Path::new(path).is_file());
|
||||
Ok((url, missing))
|
||||
})
|
||||
.await
|
||||
};
|
||||
match source {
|
||||
Ok((url, missing)) if url.as_deref() == Some(image_url) && !missing => {}
|
||||
Ok(_) => update_cover(store, fetcher, podcast_id, image_url).await,
|
||||
Err(err) => tracing::warn!("reading podcast cover state failed: {err:#}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Downloads `image_url` as the podcast cover, records it and deletes a
|
||||
/// superseded cover file. Best effort.
|
||||
async fn update_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
|
||||
let path = match fetcher.fetch_cover(podcast_id, image_url).await {
|
||||
Ok(path) => path,
|
||||
Err(err) => {
|
||||
tracing::warn!("downloading podcast cover {image_url} failed: {err:#}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let store = Arc::clone(store);
|
||||
let image_url = image_url.to_string();
|
||||
let result = blocking(move || {
|
||||
let new_path = path_string(&path);
|
||||
if store.podcast(podcast_id)?.is_none() {
|
||||
// Unsubscribed while the cover was downloading.
|
||||
let _ = std::fs::remove_file(&path);
|
||||
return Ok(());
|
||||
}
|
||||
let (_, old_path) = store.cover_source(podcast_id)?;
|
||||
store.set_cover(podcast_id, Some(&image_url), Some(&new_path))?;
|
||||
if let Some(old_path) = old_path.filter(|old| *old != new_path) {
|
||||
let _ = std::fs::remove_file(old_path);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.await;
|
||||
if let Err(err) = result {
|
||||
tracing::warn!("recording podcast cover failed: {err:#}");
|
||||
}
|
||||
}
|
||||
|
||||
fn path_string(path: &Path) -> String {
|
||||
let absolute: PathBuf = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
|
||||
absolute.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// Whether a track path is a remote (http/https) URL rather than a file.
|
||||
pub fn is_remote_url(value: &str) -> bool {
|
||||
let value = value.as_bytes();
|
||||
let has_prefix = |prefix: &[u8]| {
|
||||
value.len() >= prefix.len() && value[..prefix.len()].eq_ignore_ascii_case(prefix)
|
||||
};
|
||||
has_prefix(b"http://") || has_prefix(b"https://")
|
||||
}
|
||||
|
||||
/// The player-facing track for an episode. `file_path` is the remote audio
|
||||
/// URL; see [`is_remote_url`] and [`stream::open`].
|
||||
pub fn episode_track(podcast: &Podcast, episode: &Episode) -> TrackItem {
|
||||
TrackItem {
|
||||
id: -(EPISODE_TRACK_ID_BASE + episode.id),
|
||||
title: episode.title.clone(),
|
||||
track_number: None,
|
||||
disc_number: None,
|
||||
duration_seconds: episode.duration_seconds.unwrap_or(0.0),
|
||||
artists: vec![ArtistRef {
|
||||
id: -1,
|
||||
name: podcast.title.clone(),
|
||||
}],
|
||||
featured_artists: Vec::new(),
|
||||
release_id: -1,
|
||||
release_title: podcast.title.clone(),
|
||||
release_year: episode.published_at.map(feed::year_from_unix),
|
||||
file_path: episode.audio_url.clone(),
|
||||
content_id: None,
|
||||
cover_path: podcast.cover_path.clone(),
|
||||
audio_format: episode.mime_type.as_deref().and_then(audio_format),
|
||||
audio_bitrate: None,
|
||||
audio_sample_rate: None,
|
||||
audio_bit_depth: None,
|
||||
file_size_bytes: episode
|
||||
.byte_len
|
||||
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse of the id mapping in [`episode_track`]. Only the band reserved
|
||||
/// for episodes maps back, so hash-derived negative ids (for example device
|
||||
/// sync placeholders spread over the whole negative range) are almost never
|
||||
/// mistaken for episodes; callers should still confirm with a store lookup.
|
||||
pub fn episode_id_from_track_id(track_id: i64) -> Option<i64> {
|
||||
let episode_id = track_id.checked_neg()?.checked_sub(EPISODE_TRACK_ID_BASE)?;
|
||||
(episode_id > 0 && episode_id < EPISODE_TRACK_ID_BASE).then_some(episode_id)
|
||||
}
|
||||
|
||||
/// Display format from a MIME type: `audio/mpeg` -> `MP3`, otherwise the
|
||||
/// upper-cased subtype without an `x-` prefix.
|
||||
fn audio_format(mime: &str) -> Option<String> {
|
||||
let (kind, subtype) = mime.split(';').next()?.trim().split_once('/')?;
|
||||
let subtype = subtype.strip_prefix("x-").unwrap_or(subtype);
|
||||
if subtype.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let audio = kind.eq_ignore_ascii_case("audio");
|
||||
Some(match subtype.to_ascii_lowercase().as_str() {
|
||||
"mpeg" | "mp3" | "mpeg3" | "mpg" if audio => "MP3".to_string(),
|
||||
"mp4" | "m4a" if audio => "M4A".to_string(),
|
||||
"vorbis" => "OGG".to_string(),
|
||||
_ => subtype.to_ascii_uppercase(),
|
||||
})
|
||||
}
|
||||
@@ -1,291 +0,0 @@
|
||||
//! Preparing an episode for playback: resolve its current audio URL and
|
||||
//! resume position from the store, open the HTTP stream, decode it and
|
||||
//! position it, all off the UI and audio threads.
|
||||
//!
|
||||
//! The queue item ([`PodcastEpisodeRef`]) carries the URL that was current
|
||||
//! when the episode was queued (or received); the store is asked again at
|
||||
//! play time, so a feed refresh that changed the episode's audio URL is
|
||||
//! picked up. When the publisher answers a stale URL with a client error,
|
||||
//! the feed is refreshed once and the episode retried with the address the
|
||||
//! feed now lists for the same episode id.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use furumi_library::podcasts::PodcastStore;
|
||||
use music_dht::podcast::{PodcastEpisodeRef, reaches_completion};
|
||||
use rodio::Source as _;
|
||||
|
||||
use super::blocking;
|
||||
use super::refresh::{self, Services};
|
||||
use super::stream::{self, CancelToken, Cancelled, StreamEnd, StreamProbe, StreamStatusError};
|
||||
use crate::player::{DecodedSource, decode_source_with};
|
||||
|
||||
/// Where playback of an episode starts.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StartAt {
|
||||
/// Ordinary Play: the saved resume position (the beginning for a new
|
||||
/// or completed episode).
|
||||
Resume,
|
||||
/// An exact position (handoff, restarting the same item).
|
||||
Position { ms: i64 },
|
||||
/// "Start over": from the beginning, recorded as an explicit restart.
|
||||
Restart,
|
||||
}
|
||||
|
||||
/// An episode ready to hand to the audio thread.
|
||||
pub struct PreparedEpisode {
|
||||
/// The episode as the store knows it now (current URL and duration), or
|
||||
/// the queued reference when the store does not know it.
|
||||
pub episode: PodcastEpisodeRef,
|
||||
pub source: DecodedSource,
|
||||
/// Position the source was moved to.
|
||||
pub start_ms: i64,
|
||||
/// Duration from the decoder, else the one the feed announced.
|
||||
pub duration_ms: Option<i64>,
|
||||
/// `duration_ms` was measured by the decoder (container header, frame
|
||||
/// count or file size); otherwise it is only the feed's announcement,
|
||||
/// which is often off (rounded `itunes:duration`, inserted ads).
|
||||
pub duration_measured: bool,
|
||||
/// The server allows seeking (it sent the file size).
|
||||
pub seekable: bool,
|
||||
/// How the stream ended, once it did.
|
||||
pub probe: StreamProbe,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PreparedEpisode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("PreparedEpisode")
|
||||
.field("episode", &self.episode.episode_id)
|
||||
.field("start_ms", &self.start_ms)
|
||||
.field("duration_ms", &self.duration_ms)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
/// The episode cannot start where it should, so it does not start at all:
|
||||
/// the saved position is kept and the user is told why.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CannotResume {
|
||||
pub position_ms: i64,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CannotResume {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"the server does not allow seeking in this episode, so it cannot continue at {}; \
|
||||
the saved position is kept (use \"start over\" to play it from the beginning)",
|
||||
format_position(self.position_ms)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CannotResume {}
|
||||
|
||||
fn format_position(ms: i64) -> String {
|
||||
let secs = ms.max(0) / 1_000;
|
||||
let (hours, minutes, seconds) = (secs / 3_600, secs / 60 % 60, secs % 60);
|
||||
if hours > 0 {
|
||||
format!("{hours}:{minutes:02}:{seconds:02}")
|
||||
} else {
|
||||
format!("{minutes}:{seconds:02}")
|
||||
}
|
||||
}
|
||||
|
||||
/// The episode reference and start position to use, from the store when it
|
||||
/// knows the episode. Blocking.
|
||||
pub fn resolve(
|
||||
store: Option<&PodcastStore>,
|
||||
queued: &PodcastEpisodeRef,
|
||||
start: StartAt,
|
||||
) -> Result<(PodcastEpisodeRef, i64)> {
|
||||
let current = match store {
|
||||
Some(store) => store.episode_ref(&queued.episode_id)?,
|
||||
None => None,
|
||||
};
|
||||
let mut episode = current.unwrap_or_else(|| queued.clone());
|
||||
// Keep the greatest generation either side has seen (handoff).
|
||||
episode.progress_generation = episode.progress_generation.max(queued.progress_generation);
|
||||
let start_ms = match start {
|
||||
StartAt::Resume => match store {
|
||||
Some(store) => store.resume_position_ms(&queued.episode_id)?,
|
||||
None => 0,
|
||||
},
|
||||
StartAt::Position { ms } => ms.max(0),
|
||||
StartAt::Restart => 0,
|
||||
};
|
||||
Ok((episode, start_ms))
|
||||
}
|
||||
|
||||
/// Share of a feed-announced duration (percent) that a stream of unknown
|
||||
/// size must have played before its clean close counts as its end.
|
||||
const FEED_DURATION_END_PERCENT: i64 = 80;
|
||||
|
||||
/// Whether the end of an episode's stream means it was played to the end:
|
||||
///
|
||||
/// * a stream that ended where the server said it would is the end;
|
||||
/// * a stream that failed (connection lost, URL expired, outage) only
|
||||
/// counts when it failed within the shared completion window;
|
||||
/// * a stream of unknown size that closed cleanly (where an early close
|
||||
/// looks like the end) counts when the position is within the completion
|
||||
/// window of a duration the decoder measured. A duration that only the
|
||||
/// feed announced is a hint: then reaching [`FEED_DURATION_END_PERCENT`]
|
||||
/// of it is enough, and with no duration at all the close is the end.
|
||||
pub fn played_to_the_end(
|
||||
end: StreamEnd,
|
||||
position_ms: i64,
|
||||
duration_ms: Option<i64>,
|
||||
duration_measured: bool,
|
||||
) -> bool {
|
||||
let near_end = duration_ms.is_some() && reaches_completion(position_ms, duration_ms);
|
||||
match end {
|
||||
StreamEnd::Complete => true,
|
||||
StreamEnd::Failed => near_end,
|
||||
StreamEnd::Unknown => match duration_ms {
|
||||
None => true,
|
||||
Some(_) if duration_measured => near_end,
|
||||
Some(ms) => {
|
||||
near_end
|
||||
|| position_ms.saturating_mul(100)
|
||||
>= ms.saturating_mul(FEED_DURATION_END_PERCENT)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens, decodes and positions the audio of `episode`. Blocking (network).
|
||||
/// Stops with [`Cancelled`] once `cancel` fires: a superseded resume does
|
||||
/// not keep downloading up to its offset.
|
||||
pub fn open_at(
|
||||
episode: &PodcastEpisodeRef,
|
||||
start_ms: i64,
|
||||
cancel: &CancelToken,
|
||||
) -> Result<PreparedEpisode> {
|
||||
let opened = stream::open_with(&episode.audio_url, cancel.clone())?;
|
||||
let seekable = opened.seekable;
|
||||
let probe = opened.probe.clone();
|
||||
// Coarse seeking maps a time to a byte offset (one Range request)
|
||||
// instead of decoding everything before it.
|
||||
let coarse_seek = seekable && opened.byte_len.is_some();
|
||||
let mime = opened.mime_type.clone().or_else(|| episode.mime.clone());
|
||||
let mut source = decode_source_with(
|
||||
Box::new(opened.reader),
|
||||
opened.byte_len,
|
||||
mime.as_deref(),
|
||||
seekable,
|
||||
coarse_seek,
|
||||
)
|
||||
.map_err(|err| {
|
||||
if cancel.is_cancelled() {
|
||||
anyhow!(Cancelled)
|
||||
} else {
|
||||
anyhow!("cannot decode the episode audio: {err}")
|
||||
}
|
||||
})?;
|
||||
if start_ms > 0 {
|
||||
if !seekable {
|
||||
return Err(CannotResume {
|
||||
position_ms: start_ms,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let target = Duration::from_millis(start_ms as u64);
|
||||
if let Err(err) = source.try_seek(target) {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(Cancelled.into());
|
||||
}
|
||||
tracing::warn!(%err, "seeking a podcast stream failed");
|
||||
return Err(CannotResume {
|
||||
position_ms: start_ms,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
}
|
||||
let measured = source
|
||||
.total_duration()
|
||||
.and_then(|duration| i64::try_from(duration.as_millis()).ok())
|
||||
.filter(|ms| *ms > 0);
|
||||
Ok(PreparedEpisode {
|
||||
episode: episode.clone(),
|
||||
source,
|
||||
start_ms,
|
||||
duration_ms: measured.or(episode.duration_ms),
|
||||
duration_measured: measured.is_some(),
|
||||
seekable,
|
||||
probe,
|
||||
})
|
||||
}
|
||||
|
||||
/// [`resolve`] + [`open_at`], refreshing the feed once when the audio URL
|
||||
/// answers with a client error (moved or expired file).
|
||||
pub async fn prepare(
|
||||
services: Option<Services>,
|
||||
queued: PodcastEpisodeRef,
|
||||
start: StartAt,
|
||||
now_ms: i64,
|
||||
cancel: CancelToken,
|
||||
) -> Result<PreparedEpisode> {
|
||||
let store = services
|
||||
.as_ref()
|
||||
.map(|services| Arc::clone(&services.store));
|
||||
let lookup = queued.clone();
|
||||
let (episode, start_ms) = blocking(move || resolve(store.as_deref(), &lookup, start)).await?;
|
||||
let attempt = episode.clone();
|
||||
let attempt_cancel = cancel.clone();
|
||||
let error = match blocking(move || open_at(&attempt, start_ms, &attempt_cancel)).await {
|
||||
Ok(prepared) => return Ok(prepared),
|
||||
Err(error) => error,
|
||||
};
|
||||
if cancel.is_cancelled() {
|
||||
return Err(Cancelled.into());
|
||||
}
|
||||
let stale = error
|
||||
.downcast_ref::<StreamStatusError>()
|
||||
.is_some_and(|status| status.url_is_stale());
|
||||
let Some(services) = services.filter(|_| stale) else {
|
||||
return Err(error);
|
||||
};
|
||||
let store = Arc::clone(&services.store);
|
||||
let podcast_id = episode.podcast_id.clone();
|
||||
let feed_url = blocking(move || {
|
||||
Ok(store
|
||||
.podcast(&podcast_id)?
|
||||
.map(|summary| summary.meta.feed_url))
|
||||
})
|
||||
.await?;
|
||||
let Some(feed_url) = feed_url else {
|
||||
return Err(error);
|
||||
};
|
||||
tracing::info!(
|
||||
episode = %episode.episode_id,
|
||||
"episode audio unavailable; checking the feed for a new address"
|
||||
);
|
||||
if let Err(err) = refresh::refresh(&services, &episode.podcast_id, &feed_url, now_ms).await {
|
||||
tracing::warn!("refreshing the feed of an unavailable episode failed: {err:#}");
|
||||
return Err(error);
|
||||
}
|
||||
let store = Arc::clone(&services.store);
|
||||
let lookup = queued.clone();
|
||||
let (fresh, _) = blocking(move || resolve(Some(&store), &lookup, StartAt::Restart)).await?;
|
||||
if fresh.audio_url == episode.audio_url {
|
||||
return Err(error).context("the feed lists the same audio address");
|
||||
}
|
||||
if cancel.is_cancelled() {
|
||||
return Err(Cancelled.into());
|
||||
}
|
||||
blocking(move || open_at(&fresh, start_ms, &cancel)).await
|
||||
}
|
||||
|
||||
/// Message for a failed start; the URL never appears (it may carry a
|
||||
/// private token).
|
||||
pub fn describe_failure(err: &anyhow::Error) -> String {
|
||||
if err.downcast_ref::<Cancelled>().is_some() {
|
||||
return Cancelled.to_string();
|
||||
}
|
||||
if let Some(cannot) = err.downcast_ref::<CannotResume>() {
|
||||
return cannot.to_string();
|
||||
}
|
||||
format!("{err:#}")
|
||||
}
|
||||
@@ -1,836 +0,0 @@
|
||||
//! Episode playback preparation and durable progress, against a local HTTP
|
||||
//! server and a temp-dir store (no internet, no audio device).
|
||||
|
||||
use std::sync::PoisonError;
|
||||
use std::sync::mpsc::{Receiver, RecvTimeoutError};
|
||||
use std::time::Duration;
|
||||
|
||||
use furumi_library::podcasts::{EpisodeQuery, PodcastStore};
|
||||
use music_dht::podcast::{
|
||||
EpisodeId, PodcastEpisodeRef, PodcastId, ProgressStatus, reaches_completion,
|
||||
};
|
||||
|
||||
use super::playback::{self, CannotResume, StartAt};
|
||||
use super::progress::{ProgressCommand, ProgressEvent, ProgressWriter};
|
||||
use super::refresh::{Services, refresh, subscribe};
|
||||
use super::stream::{CancelToken, StreamEnd};
|
||||
use super::test_server::{Route, WAV_RATE, wav};
|
||||
use super::tests::{Fixture, NOW, fixture, rss};
|
||||
|
||||
const MINUTE_MS: i64 = 60_000;
|
||||
|
||||
/// Subscribes the fixture to a feed with episodes `a` and `b` and returns
|
||||
/// the podcast and the reference of episode `a`.
|
||||
async fn subscribed(f: &Fixture) -> (PodcastId, PodcastEpisodeRef) {
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
let report = subscribe(&f.services, &f.server.url("/feed.xml"), NOW)
|
||||
.await
|
||||
.unwrap();
|
||||
let episode = episode_ref(&f.services.store, &report.podcast_id, "Pilot");
|
||||
(report.podcast_id, episode)
|
||||
}
|
||||
|
||||
fn episode_ref(store: &PodcastStore, podcast_id: &PodcastId, title: &str) -> PodcastEpisodeRef {
|
||||
let item = store
|
||||
.episodes(&EpisodeQuery::new(podcast_id.clone()))
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|item| item.episode.title == title)
|
||||
.expect("episode in the catalog");
|
||||
store
|
||||
.episode_ref(&item.episode.episode_id)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A progress writer whose events can be awaited.
|
||||
struct Writer {
|
||||
writer: ProgressWriter,
|
||||
events: Receiver<ProgressEvent>,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
fn spawn(services: &Services) -> Writer {
|
||||
let (tx, events) = std::sync::mpsc::channel();
|
||||
let tx = std::sync::Mutex::new(tx);
|
||||
let writer = ProgressWriter::spawn(services.clone(), move |event| {
|
||||
let _ = tx
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.send(event);
|
||||
})
|
||||
.unwrap();
|
||||
Writer { writer, events }
|
||||
}
|
||||
|
||||
fn send(&self, command: ProgressCommand) {
|
||||
self.writer.send(command);
|
||||
}
|
||||
|
||||
/// The next event (the writer handles commands in order).
|
||||
fn next(&self) -> ProgressEvent {
|
||||
match self.events.recv_timeout(Duration::from_secs(10)) {
|
||||
Ok(event) => event,
|
||||
Err(RecvTimeoutError::Timeout) => panic!("no progress event"),
|
||||
Err(RecvTimeoutError::Disconnected) => panic!("writer stopped"),
|
||||
}
|
||||
}
|
||||
|
||||
fn saved(&self) -> music_dht::podcast::ProgressRecord {
|
||||
match self.next() {
|
||||
ProgressEvent::Saved(record) => record,
|
||||
other => panic!("expected a save, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn close(mut self) {
|
||||
assert!(self.writer.close(Duration::from_secs(10)));
|
||||
}
|
||||
}
|
||||
|
||||
fn begin(
|
||||
playback: u64,
|
||||
episode: &PodcastEpisodeRef,
|
||||
position_ms: i64,
|
||||
restart: bool,
|
||||
) -> ProgressCommand {
|
||||
ProgressCommand::Begin {
|
||||
playback,
|
||||
episode: Box::new(episode.clone()),
|
||||
session_id: format!("session-{playback}"),
|
||||
position_ms,
|
||||
restart,
|
||||
now_ms: NOW,
|
||||
}
|
||||
}
|
||||
|
||||
fn resume(store: &PodcastStore, id: &EpisodeId) -> (i64, ProgressStatus) {
|
||||
let progress = store.progress(id).unwrap().expect("progress");
|
||||
(store.resume_position_ms(id).unwrap(), progress.status)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resume_position_survives_restarts_and_late_checkpoints_lose() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
let store = &f.services.store;
|
||||
let id = &episode.episode_id;
|
||||
|
||||
// First run: play, save periodically, quit.
|
||||
let first = Writer::spawn(&f.services);
|
||||
first.send(begin(1, &episode, 0, false));
|
||||
let started = first.saved();
|
||||
assert_eq!(started.status, ProgressStatus::InProgress);
|
||||
first.send(ProgressCommand::Checkpoint {
|
||||
playback: 1,
|
||||
position_ms: 10 * MINUTE_MS,
|
||||
});
|
||||
assert_eq!(
|
||||
first.saved().generation,
|
||||
started.generation,
|
||||
"checkpoints keep it"
|
||||
);
|
||||
assert_eq!(
|
||||
resume(store, id),
|
||||
(10 * MINUTE_MS, ProgressStatus::InProgress)
|
||||
);
|
||||
|
||||
// "Start over" in a later run takes a newer generation…
|
||||
let second = Writer::spawn(&f.services);
|
||||
second.send(begin(2, &episode, 0, true));
|
||||
let restarted = second.saved();
|
||||
assert!(restarted.generation > started.generation);
|
||||
assert_eq!(resume(store, id), (0, ProgressStatus::InProgress));
|
||||
|
||||
// …so a late checkpoint of the old session loses, whatever its clock.
|
||||
first.send(ProgressCommand::Checkpoint {
|
||||
playback: 1,
|
||||
position_ms: 20 * MINUTE_MS,
|
||||
});
|
||||
match first.next() {
|
||||
ProgressEvent::Superseded { current } => assert_eq!(current.position_ms, 0),
|
||||
other => panic!("expected supersede, got {other:?}"),
|
||||
}
|
||||
assert_eq!(resume(store, id), (0, ProgressStatus::InProgress));
|
||||
// The old session no longer writes, not even when it ends.
|
||||
first.send(ProgressCommand::End {
|
||||
playback: 1,
|
||||
position_ms: 21 * MINUTE_MS,
|
||||
listened_ms: 0,
|
||||
now_ms: NOW + 1,
|
||||
});
|
||||
assert!(matches!(first.next(), ProgressEvent::Ended { .. }));
|
||||
first.close();
|
||||
assert_eq!(resume(store, id), (0, ProgressStatus::InProgress));
|
||||
|
||||
// The new session saves normally, including a seek backwards.
|
||||
second.send(ProgressCommand::Checkpoint {
|
||||
playback: 2,
|
||||
position_ms: 5 * MINUTE_MS,
|
||||
});
|
||||
second.saved();
|
||||
second.send(ProgressCommand::Seek {
|
||||
playback: 2,
|
||||
position_ms: MINUTE_MS,
|
||||
});
|
||||
let seeked = second.saved();
|
||||
assert!(
|
||||
seeked.generation > restarted.generation,
|
||||
"a seek is explicit"
|
||||
);
|
||||
assert_eq!(resume(store, id), (MINUTE_MS, ProgressStatus::InProgress));
|
||||
second.send(ProgressCommand::End {
|
||||
playback: 2,
|
||||
position_ms: MINUTE_MS + 2_000,
|
||||
listened_ms: 2_000,
|
||||
now_ms: NOW + 2,
|
||||
});
|
||||
second.saved();
|
||||
assert!(matches!(second.next(), ProgressEvent::Ended { .. }));
|
||||
second.close();
|
||||
assert_eq!(
|
||||
resume(store, id),
|
||||
(MINUTE_MS + 2_000, ProgressStatus::InProgress)
|
||||
);
|
||||
// The sync log carries every write, in order.
|
||||
let kinds = f.log.kinds();
|
||||
assert_eq!(kinds[0], "podcast_subscription_set");
|
||||
assert!(
|
||||
kinds[1..].iter().all(|kind| kind == "podcast_progress_set"),
|
||||
"{kinds:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn completion_counts_once_and_short_sessions_stay_out_of_history() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
let store = &f.services.store;
|
||||
let id = &episode.episode_id;
|
||||
let duration = episode.duration_ms.expect("the feed has a duration");
|
||||
assert_eq!(duration, 45 * MINUTE_MS);
|
||||
let near_end = duration - 20_000;
|
||||
assert!(reaches_completion(near_end, Some(duration)));
|
||||
|
||||
// A short listen: progress is saved, history is not.
|
||||
let writer = Writer::spawn(&f.services);
|
||||
writer.send(begin(1, &episode, 0, false));
|
||||
writer.saved();
|
||||
writer.send(ProgressCommand::End {
|
||||
playback: 1,
|
||||
position_ms: 5_000,
|
||||
listened_ms: 5_000,
|
||||
now_ms: NOW + 5_000,
|
||||
});
|
||||
writer.saved();
|
||||
assert!(matches!(writer.next(), ProgressEvent::Ended { .. }));
|
||||
assert!(store.listen_history(None, 10).unwrap().is_empty());
|
||||
|
||||
// A full listen completes the episode once, even if the end is
|
||||
// reported twice and the session ends afterwards.
|
||||
writer.send(begin(2, &episode, 5_000, false));
|
||||
writer.saved();
|
||||
for _ in 0..2 {
|
||||
writer.send(ProgressCommand::Complete {
|
||||
playback: 2,
|
||||
position_ms: near_end,
|
||||
listened_ms: near_end - 5_000,
|
||||
now_ms: NOW + duration,
|
||||
});
|
||||
}
|
||||
let completed = writer.saved();
|
||||
assert_eq!(completed.status, ProgressStatus::Completed);
|
||||
writer.send(ProgressCommand::End {
|
||||
playback: 2,
|
||||
position_ms: duration,
|
||||
listened_ms: duration - 5_000,
|
||||
now_ms: NOW + duration + 20_000,
|
||||
});
|
||||
assert!(matches!(writer.next(), ProgressEvent::Ended { .. }));
|
||||
writer.close();
|
||||
assert_eq!(
|
||||
resume(store, id),
|
||||
(0, ProgressStatus::Completed),
|
||||
"Play starts over"
|
||||
);
|
||||
assert_eq!(store.completed_listen_count(id).unwrap(), 1);
|
||||
let history = store.listen_history(None, 10).unwrap();
|
||||
assert_eq!(history.len(), 1);
|
||||
assert!(history[0].session.completed);
|
||||
assert_eq!(history[0].session.listened_ms, duration - 5_000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn marking_played_while_playing_stops_checkpoints_until_the_next_seek() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
let store = &f.services.store;
|
||||
let id = &episode.episode_id;
|
||||
let writer = Writer::spawn(&f.services);
|
||||
writer.send(begin(1, &episode, 0, false));
|
||||
writer.saved();
|
||||
writer.send(ProgressCommand::SetPlayed {
|
||||
episode_id: id.clone(),
|
||||
played: true,
|
||||
});
|
||||
assert_eq!(writer.saved().status, ProgressStatus::Completed);
|
||||
assert!(matches!(
|
||||
writer.next(),
|
||||
ProgressEvent::PlayedSet {
|
||||
played: true,
|
||||
result: Ok(())
|
||||
}
|
||||
));
|
||||
writer.send(ProgressCommand::Checkpoint {
|
||||
playback: 1,
|
||||
position_ms: 90_000,
|
||||
});
|
||||
assert!(matches!(writer.next(), ProgressEvent::Superseded { .. }));
|
||||
assert_eq!(resume(store, id).1, ProgressStatus::Completed);
|
||||
// Seeking is the user's explicit choice to keep listening here.
|
||||
writer.send(ProgressCommand::Seek {
|
||||
playback: 1,
|
||||
position_ms: 30_000,
|
||||
});
|
||||
assert_eq!(writer.saved().status, ProgressStatus::InProgress);
|
||||
assert_eq!(resume(store, id), (30_000, ProgressStatus::InProgress));
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/// Serves a position-coded WAV for episode `a` and returns its seconds.
|
||||
fn serve_audio(f: &Fixture, route: impl FnOnce(Route) -> Route, seconds: u32) -> u32 {
|
||||
f.server
|
||||
.set("/audio/a.mp3", route(Route::new(wav(seconds), "audio/wav")));
|
||||
seconds
|
||||
}
|
||||
|
||||
/// Seconds marker of the next decoded sample (see `test_server::wav`).
|
||||
fn second_marker(source: &mut crate::player::DecodedSource) -> i64 {
|
||||
let sample = source.next().expect("a sample");
|
||||
(sample * 32_768.0 / 40.0).round() as i64
|
||||
}
|
||||
|
||||
/// The source plays from `second`. A seek may land up to one decoder packet
|
||||
/// early (1024 frames, 0.128 s at 8 kHz) in the previous second.
|
||||
fn assert_starts_at(source: &mut crate::player::DecodedSource, second: i64) {
|
||||
let marker = second_marker(source);
|
||||
assert!(
|
||||
marker == second || marker == second - 1,
|
||||
"starts in second {marker}, expected {second}"
|
||||
);
|
||||
// A quarter of a second later it is exactly there.
|
||||
let marker = (0..WAV_RATE / 4)
|
||||
.map(|_| second_marker(source))
|
||||
.last()
|
||||
.unwrap();
|
||||
assert_eq!(marker, second);
|
||||
}
|
||||
|
||||
async fn save_position(services: &Services, episode: &PodcastEpisodeRef, position_ms: i64) {
|
||||
let writer = Writer::spawn(services);
|
||||
writer.send(begin(99, episode, position_ms, false));
|
||||
writer.saved();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn play_resumes_at_the_saved_position_with_a_range_request() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
let seconds = serve_audio(&f, |route| route, 12 * 60);
|
||||
save_position(&f.services, &episode, 11 * MINUTE_MS).await;
|
||||
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode.clone(),
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(prepared.start_ms, 11 * MINUTE_MS);
|
||||
assert_eq!(prepared.duration_ms, Some(i64::from(seconds) * 1_000));
|
||||
let mut source = prepared.source;
|
||||
assert_starts_at(&mut source, 11 * 60);
|
||||
// The far seek became a ranged request instead of a full download.
|
||||
let byte = 11 * 60 * WAV_RATE as usize * 2;
|
||||
assert!(
|
||||
f.server.ranges("/audio/a.mp3").iter().any(|range| range
|
||||
.as_deref()
|
||||
.and_then(|range| range.strip_prefix("bytes="))
|
||||
.and_then(|range| range.trim_end_matches('-').parse::<usize>().ok())
|
||||
.is_some_and(|start| start + 4_096 >= byte && start <= byte + 4_096)),
|
||||
"{:?}",
|
||||
f.server.ranges("/audio/a.mp3")
|
||||
);
|
||||
|
||||
// "Start over" ignores the saved position.
|
||||
let restarted = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode,
|
||||
StartAt::Restart,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(restarted.start_ms, 0);
|
||||
let mut source = restarted.source;
|
||||
assert_eq!(second_marker(&mut source), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_server_without_range_support_still_resumes_at_the_right_place() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
serve_audio(&f, Route::no_ranges, 30);
|
||||
save_position(&f.services, &episode, 20_000).await;
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode,
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(prepared.start_ms, 20_000);
|
||||
let mut source = prepared.source;
|
||||
assert_starts_at(&mut source, 20);
|
||||
assert!(
|
||||
f.server
|
||||
.ranges("/audio/a.mp3")
|
||||
.iter()
|
||||
.skip(1)
|
||||
.all(Option::is_none),
|
||||
"no ranges are sent to a server that ignores them"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_unseekable_stream_does_not_pretend_to_resume() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
serve_audio(&f, |route| route.no_ranges().no_length(), 30);
|
||||
save_position(&f.services, &episode, 20_000).await;
|
||||
let err = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode.clone(),
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.expect_err("cannot resume");
|
||||
assert_eq!(
|
||||
err.downcast_ref::<CannotResume>(),
|
||||
Some(&CannotResume {
|
||||
position_ms: 20_000
|
||||
})
|
||||
);
|
||||
assert!(playback::describe_failure(&err).contains("0:20"));
|
||||
// The saved position is untouched, and "start over" still plays.
|
||||
let store = &f.services.store;
|
||||
assert_eq!(
|
||||
store.resume_position_ms(&episode.episode_id).unwrap(),
|
||||
20_000
|
||||
);
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode,
|
||||
StartAt::Restart,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(prepared.start_ms, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_audio_url_comes_from_the_feed_at_play_time() {
|
||||
let f = fixture();
|
||||
let (podcast_id, queued) = subscribed(&f).await;
|
||||
// The publisher moved the file and the feed already says so.
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]).pipe_replace("/audio/a.mp3", "/audio/a-v2.mp3"),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
let url = f
|
||||
.services
|
||||
.store
|
||||
.podcast(&podcast_id)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.meta
|
||||
.feed_url;
|
||||
refresh(&f.services, &podcast_id, &url, NOW + 1)
|
||||
.await
|
||||
.unwrap();
|
||||
f.server
|
||||
.set("/audio/a-v2.mp3", Route::new(wav(5), "audio/wav"));
|
||||
// The queue item still has the old URL; the store's is used.
|
||||
assert!(queued.audio_url.ends_with("/audio/a.mp3"));
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
queued.clone(),
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(prepared.episode.audio_url.ends_with("/audio/a-v2.mp3"));
|
||||
assert!(f.server.requests("/audio/a.mp3").is_empty());
|
||||
assert_eq!(
|
||||
prepared.episode.episode_id, queued.episode_id,
|
||||
"same episode"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_stale_audio_url_refreshes_the_feed_once_and_retries() {
|
||||
let f = fixture();
|
||||
let (_, queued) = subscribed(&f).await;
|
||||
// The old file is gone (404) and only the feed knows the new one.
|
||||
f.server.set(
|
||||
"/feed.xml",
|
||||
Route::new(
|
||||
rss(&[("a", "Pilot"), ("b", "Second")]).pipe_replace("/audio/a.mp3", "/audio/a-v3.mp3"),
|
||||
"application/rss+xml",
|
||||
),
|
||||
);
|
||||
f.server
|
||||
.set("/audio/a-v3.mp3", Route::new(wav(5), "audio/wav"));
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
queued.clone(),
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(prepared.episode.audio_url.ends_with("/audio/a-v3.mp3"));
|
||||
assert_eq!(f.server.requests("/audio/a.mp3").len(), 1);
|
||||
assert_eq!(
|
||||
f.server.requests("/feed.xml").len(),
|
||||
2,
|
||||
"one extra feed check"
|
||||
);
|
||||
|
||||
// Without a newer address the original error is reported, once.
|
||||
f.server.set(
|
||||
"/audio/a-v3.mp3",
|
||||
Route::new("gone", "text/plain").status(410),
|
||||
);
|
||||
let err = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
queued,
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.expect_err("still unavailable");
|
||||
assert!(format!("{err:#}").contains("410"), "{err:#}");
|
||||
assert_eq!(f.server.requests("/feed.xml").len(), 3);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn without_a_store_the_queued_reference_plays_from_the_start() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
serve_audio(&f, |route| route, 5);
|
||||
save_position(&f.services, &episode, 3_000).await;
|
||||
let prepared = playback::prepare(None, episode, StartAt::Resume, NOW, CancelToken::never())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(prepared.start_ms, 0);
|
||||
}
|
||||
|
||||
trait PipeReplace {
|
||||
fn pipe_replace(self, from: &str, to: &str) -> Vec<u8>;
|
||||
}
|
||||
|
||||
impl PipeReplace for Vec<u8> {
|
||||
fn pipe_replace(self, from: &str, to: &str) -> Vec<u8> {
|
||||
String::from_utf8(self)
|
||||
.unwrap()
|
||||
.replace(from, to)
|
||||
.into_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes `source` to its end, as the audio thread would; returns the
|
||||
/// position reached (mono samples of `test_server::wav`).
|
||||
fn play_out(source: &mut crate::player::DecodedSource) -> i64 {
|
||||
let mut samples = 0i64;
|
||||
while source.next().is_some() {
|
||||
samples += 1;
|
||||
}
|
||||
samples * 1_000 / i64::from(WAV_RATE)
|
||||
}
|
||||
|
||||
/// Byte offset of second `second` in `test_server::wav`.
|
||||
fn wav_offset(second: u32) -> usize {
|
||||
44 + (second * WAV_RATE * 2) as usize
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_lost_connection_does_not_complete_the_episode_and_keeps_its_position() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
// Two minutes of audio; the server stops sending after 30 s for good.
|
||||
serve_audio(&f, |route| route.cut(wav_offset(30), usize::MAX), 120);
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode.clone(),
|
||||
StartAt::Resume,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let writer = Writer::spawn(&f.services);
|
||||
writer.send(begin(1, &episode, 0, false));
|
||||
writer.saved();
|
||||
|
||||
let mut source = prepared.source;
|
||||
// The decoder ends once the stalled read gives up (STARVE_TIMEOUT).
|
||||
let position_ms = play_out(&mut source);
|
||||
assert!(
|
||||
(29_000..=30_500).contains(&position_ms),
|
||||
"stopped at {position_ms} ms"
|
||||
);
|
||||
assert_eq!(prepared.probe.end(), StreamEnd::Failed);
|
||||
assert_eq!(prepared.duration_ms, Some(120_000));
|
||||
assert!(!playback::played_to_the_end(
|
||||
prepared.probe.end(),
|
||||
position_ms,
|
||||
prepared.duration_ms,
|
||||
prepared.duration_measured
|
||||
));
|
||||
|
||||
// What the runtime then saves: the last position, not a completion.
|
||||
writer.send(ProgressCommand::End {
|
||||
playback: 1,
|
||||
position_ms,
|
||||
listened_ms: position_ms,
|
||||
now_ms: NOW + 1,
|
||||
});
|
||||
while !matches!(writer.next(), ProgressEvent::Ended { .. }) {}
|
||||
writer.close();
|
||||
let store = &f.services.store;
|
||||
assert_eq!(
|
||||
resume(store, &episode.episode_id),
|
||||
(position_ms, ProgressStatus::InProgress)
|
||||
);
|
||||
assert_eq!(
|
||||
store.completed_listen_count(&episode.episode_id).unwrap(),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_stream_of_unknown_length_that_closes_early_is_not_the_end() {
|
||||
let f = fixture();
|
||||
let (_, episode) = subscribed(&f).await;
|
||||
serve_audio(
|
||||
&f,
|
||||
|route| route.no_length().cut(wav_offset(30), usize::MAX),
|
||||
120,
|
||||
);
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode.clone(),
|
||||
StartAt::Restart,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut source = prepared.source;
|
||||
let position_ms = play_out(&mut source);
|
||||
assert!((29_000..=30_500).contains(&position_ms), "{position_ms} ms");
|
||||
// A closed connection looks like the end of the file here...
|
||||
assert_eq!(prepared.probe.end(), StreamEnd::Unknown);
|
||||
// ...but the duration the decoder read from the header says otherwise.
|
||||
assert_eq!(prepared.duration_ms, Some(120_000));
|
||||
assert!(prepared.duration_measured);
|
||||
assert!(!playback::played_to_the_end(
|
||||
StreamEnd::Unknown,
|
||||
position_ms,
|
||||
Some(120_000),
|
||||
true
|
||||
));
|
||||
assert!(playback::played_to_the_end(
|
||||
StreamEnd::Unknown,
|
||||
119_000,
|
||||
Some(120_000),
|
||||
true
|
||||
));
|
||||
// A duration only the feed announced is a hint: an audio file that ends
|
||||
// more than the completion window before it still ends there...
|
||||
assert!(playback::played_to_the_end(
|
||||
StreamEnd::Unknown,
|
||||
100_000,
|
||||
Some(120_000),
|
||||
false
|
||||
));
|
||||
// ...while a close at a quarter of it is still a lost connection.
|
||||
assert!(!playback::played_to_the_end(
|
||||
StreamEnd::Unknown,
|
||||
position_ms,
|
||||
Some(120_000),
|
||||
false
|
||||
));
|
||||
|
||||
// A whole file with a size is played to its end.
|
||||
serve_audio(&f, |route| route, 3);
|
||||
let prepared = playback::prepare(
|
||||
Some(f.services.clone()),
|
||||
episode,
|
||||
StartAt::Restart,
|
||||
NOW,
|
||||
CancelToken::never(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut source = prepared.source;
|
||||
assert_eq!(play_out(&mut source), 3_000);
|
||||
assert_eq!(prepared.probe.end(), StreamEnd::Complete);
|
||||
assert!(playback::played_to_the_end(
|
||||
StreamEnd::Complete,
|
||||
3_000,
|
||||
prepared.duration_ms,
|
||||
prepared.duration_measured
|
||||
));
|
||||
// A failure in the last seconds still counts as the end, whatever
|
||||
// announced the duration; earlier it never does.
|
||||
for measured in [true, false] {
|
||||
assert!(playback::played_to_the_end(
|
||||
StreamEnd::Failed,
|
||||
119_500,
|
||||
Some(120_000),
|
||||
measured
|
||||
));
|
||||
assert!(!playback::played_to_the_end(
|
||||
StreamEnd::Failed,
|
||||
100_000,
|
||||
Some(120_000),
|
||||
measured
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// About `seconds` of silent MPEG-1 Layer III (mono, 44.1 kHz, 128 kbit/s)
|
||||
/// frames with no Xing/Info header: sent without a size, the decoder
|
||||
/// cannot measure its duration.
|
||||
fn mp3_without_header(seconds: u32) -> Vec<u8> {
|
||||
const FRAME_LEN: usize = 417; // 144 * 128_000 / 44_100
|
||||
let frames = (u64::from(seconds) * 44_100 / 1_152) as usize;
|
||||
let mut frame = vec![0u8; FRAME_LEN];
|
||||
frame[..4].copy_from_slice(&[0xFF, 0xFB, 0x90, 0xC0]);
|
||||
frame.repeat(frames)
|
||||
}
|
||||
|
||||
/// Decodes `source` to its end; returns the position reached.
|
||||
fn play_out_any(source: &mut crate::player::DecodedSource) -> i64 {
|
||||
let per_second = i64::from(source.sample_rate().get()) * i64::from(source.channels().get());
|
||||
let mut samples = 0i64;
|
||||
while source.next().is_some() {
|
||||
samples += 1;
|
||||
}
|
||||
samples * 1_000 / per_second
|
||||
}
|
||||
|
||||
/// Without a size from the server and a duration the decoder could
|
||||
/// measure, only the feed's duration is known, and it may be longer than
|
||||
/// the audio by more than the completion window. The clean end of the
|
||||
/// stream then still completes the episode.
|
||||
#[tokio::test]
|
||||
async fn a_stream_shorter_than_the_feed_says_completes_at_its_end() {
|
||||
let f = fixture();
|
||||
let (_, mut episode) = subscribed(&f).await;
|
||||
f.server.set(
|
||||
"/audio/a.mp3",
|
||||
Route::new(mp3_without_header(100), "audio/mpeg").no_length(),
|
||||
);
|
||||
// The feed announces two minutes, 20 s more than the audio.
|
||||
episode.duration_ms = Some(120_000);
|
||||
let prepared =
|
||||
tokio::task::spawn_blocking(move || playback::open_at(&episode, 0, &CancelToken::never()))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(!prepared.seekable);
|
||||
assert!(!prepared.duration_measured, "{:?}", prepared.duration_ms);
|
||||
assert_eq!(prepared.duration_ms, Some(120_000));
|
||||
let mut source = prepared.source;
|
||||
let position_ms = play_out_any(&mut source);
|
||||
assert!(
|
||||
(99_000..=100_100).contains(&position_ms),
|
||||
"{position_ms} ms"
|
||||
);
|
||||
assert_eq!(prepared.probe.end(), StreamEnd::Unknown);
|
||||
assert!(!reaches_completion(position_ms, Some(120_000)));
|
||||
assert!(playback::played_to_the_end(
|
||||
prepared.probe.end(),
|
||||
position_ms,
|
||||
prepared.duration_ms,
|
||||
prepared.duration_measured
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_superseded_resume_stops_reading_up_to_its_offset() {
|
||||
let f = fixture();
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let (_, episode) = rt.block_on(subscribed(&f));
|
||||
// No Range support: resuming at 60 s means reading and discarding
|
||||
// everything before it (while positioning, or on the first reads after
|
||||
// it, depending on the format), and this server stalls after 10 s.
|
||||
serve_audio(
|
||||
&f,
|
||||
|route| route.no_ranges().cut(wav_offset(10), usize::MAX),
|
||||
120,
|
||||
);
|
||||
let superseded = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let flag = std::sync::Arc::clone(&superseded);
|
||||
let cancel = CancelToken::new(move || flag.load(std::sync::atomic::Ordering::SeqCst));
|
||||
let started = std::time::Instant::now();
|
||||
let starting = std::thread::spawn(move || {
|
||||
let prepared = playback::open_at(&episode, 60_000, &cancel)?;
|
||||
let mut source = prepared.source;
|
||||
Ok::<_, anyhow::Error>((source.next(), prepared.probe))
|
||||
});
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
superseded.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
match starting.join().unwrap() {
|
||||
Err(err) => assert!(
|
||||
err.downcast_ref::<super::stream::Cancelled>().is_some(),
|
||||
"{err:#}"
|
||||
),
|
||||
Ok((sample, probe)) => {
|
||||
assert_eq!(sample, None, "nothing plays after the cancel");
|
||||
assert_eq!(probe.end(), StreamEnd::Failed);
|
||||
}
|
||||
}
|
||||
// Well before a stalled read would give up by itself.
|
||||
assert!(
|
||||
started.elapsed() < super::stream::STARVE_TIMEOUT / 2,
|
||||
"{:?}",
|
||||
started.elapsed()
|
||||
);
|
||||
}
|
||||
@@ -1,488 +0,0 @@
|
||||
//! Durable episode progress: one background thread writes every progress
|
||||
//! action of this device to the podcast store and the sync log, in order.
|
||||
//!
|
||||
//! The rules come from the FRID contract (`music_dht::podcast::state`,
|
||||
//! `PodcastStore` progress actions); this module only decides *when* to
|
||||
//! call them:
|
||||
//!
|
||||
//! * a playback start is an explicit action (`start_session`, or `restart`
|
||||
//! for "start over") that takes a new progress generation;
|
||||
//! * periodic checkpoints, pause and the final save keep the session's
|
||||
//! generation (`checkpoint`), so a late checkpoint of an older session
|
||||
//! always loses against a newer explicit action;
|
||||
//! * a seek is explicit (`seek`) and becomes the session's generation;
|
||||
//! * reaching the end (`reaches_completion` or the end of the stream) is
|
||||
//! `complete_session`;
|
||||
//! * when the store reports a newer generation (`Superseded`), the session
|
||||
//! stops writing checkpoints until the user's next explicit action;
|
||||
//! * the listening session itself (`ListenSession`) is recorded at pause,
|
||||
//! completion and end; the store only keeps sessions that pass the shared
|
||||
//! history threshold and only when they grew.
|
||||
//!
|
||||
//! "Mark played" / "Mark unplayed" go through the same thread so they are
|
||||
//! ordered with the checkpoints of a playing episode.
|
||||
//!
|
||||
//! Writes never happen on the UI or audio thread, and a failed write is
|
||||
//! reported and logged without stopping playback.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use furumi_library::podcasts::{
|
||||
CheckpointOutcome, PodcastLocalOp, PodcastStore, ProgressSession, ProgressWrite, StampSource,
|
||||
};
|
||||
use music_dht::podcast::{EpisodeId, ListenSession, PodcastEpisodeRef, ProgressRecord};
|
||||
|
||||
use super::OpLog;
|
||||
use super::refresh::Services;
|
||||
|
||||
/// How often a playing episode saves its position.
|
||||
pub const CHECKPOINT_INTERVAL: Duration = Duration::from_secs(20);
|
||||
|
||||
/// What the UI asks the writer to do. `playback` identifies one start of
|
||||
/// one episode on this device; commands for a playback that is no longer
|
||||
/// the active one are ignored (they can only arrive late).
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ProgressCommand {
|
||||
/// Playback actually started at `position_ms`.
|
||||
Begin {
|
||||
playback: u64,
|
||||
episode: Box<PodcastEpisodeRef>,
|
||||
session_id: String,
|
||||
position_ms: i64,
|
||||
/// "Start over" instead of an ordinary start or resume.
|
||||
restart: bool,
|
||||
now_ms: i64,
|
||||
},
|
||||
/// Periodic save while playing.
|
||||
Checkpoint { playback: u64, position_ms: i64 },
|
||||
/// Paused: save the position and the listening session so far.
|
||||
Pause {
|
||||
playback: u64,
|
||||
position_ms: i64,
|
||||
listened_ms: i64,
|
||||
now_ms: i64,
|
||||
},
|
||||
/// The user moved to `position_ms`.
|
||||
Seek { playback: u64, position_ms: i64 },
|
||||
/// The position reached the end of the episode.
|
||||
Complete {
|
||||
playback: u64,
|
||||
position_ms: i64,
|
||||
listened_ms: i64,
|
||||
now_ms: i64,
|
||||
},
|
||||
/// Playback of this start ended (stop, next item, handoff, exit…).
|
||||
End {
|
||||
playback: u64,
|
||||
position_ms: i64,
|
||||
listened_ms: i64,
|
||||
now_ms: i64,
|
||||
},
|
||||
/// "Mark played" / "Mark unplayed" from the episode list.
|
||||
SetPlayed { episode_id: EpisodeId, played: bool },
|
||||
}
|
||||
|
||||
/// What the writer reports back.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ProgressEvent {
|
||||
/// The progress register of an episode was written.
|
||||
Saved(ProgressRecord),
|
||||
/// A newer explicit action holds the register; this playback stopped
|
||||
/// saving its position.
|
||||
Superseded { current: ProgressRecord },
|
||||
/// A playback ended (its session was recorded when long enough).
|
||||
Ended { episode_id: EpisodeId },
|
||||
/// The result of a `SetPlayed` command.
|
||||
PlayedSet {
|
||||
played: bool,
|
||||
result: Result<(), String>,
|
||||
},
|
||||
/// A write failed; playback goes on.
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
/// Handle of the writer thread.
|
||||
pub struct ProgressWriter {
|
||||
tx: Option<Sender<ProgressCommand>>,
|
||||
thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl ProgressWriter {
|
||||
pub fn spawn(
|
||||
services: Services,
|
||||
on_event: impl Fn(ProgressEvent) + Send + 'static,
|
||||
) -> Result<ProgressWriter> {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let thread = std::thread::Builder::new()
|
||||
.name("podcast-progress".to_string())
|
||||
.spawn(move || run(rx, &services.store, services.log.as_ref(), on_event))
|
||||
.map_err(|err| anyhow!("starting the podcast progress writer: {err}"))?;
|
||||
Ok(ProgressWriter {
|
||||
tx: Some(tx),
|
||||
thread: Some(thread),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn send(&self, command: ProgressCommand) {
|
||||
if let Some(tx) = &self.tx
|
||||
&& tx.send(command).is_err()
|
||||
{
|
||||
tracing::warn!("podcast progress writer stopped; a progress update was lost");
|
||||
}
|
||||
}
|
||||
|
||||
/// Stops accepting commands and waits up to `timeout` for the queued
|
||||
/// ones to be written. Blocking; used at exit.
|
||||
pub fn close(&mut self, timeout: Duration) -> bool {
|
||||
self.tx = None;
|
||||
let Some(thread) = self.thread.take() else {
|
||||
return true;
|
||||
};
|
||||
let deadline = std::time::Instant::now() + timeout;
|
||||
while !thread.is_finished() {
|
||||
if std::time::Instant::now() >= deadline {
|
||||
tracing::warn!("podcast progress writer did not finish before exit");
|
||||
return false;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
let _ = thread.join();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// The playback whose session the writer is maintaining.
|
||||
struct Active {
|
||||
playback: u64,
|
||||
/// `None` when starting the session failed: nothing is written for it.
|
||||
session: Option<ProgressSession>,
|
||||
episode: PodcastEpisodeRef,
|
||||
session_id: String,
|
||||
started_at_ms: i64,
|
||||
start_position_ms: i64,
|
||||
/// A newer generation holds the register: no more checkpoints.
|
||||
superseded: bool,
|
||||
/// The register says this session completed the episode (a later seek
|
||||
/// back makes it "in progress" again).
|
||||
completed: bool,
|
||||
/// The session reached the end at least once; it counts as a completed
|
||||
/// listen whatever happens afterwards.
|
||||
listen_completed: bool,
|
||||
}
|
||||
|
||||
fn run(
|
||||
rx: Receiver<ProgressCommand>,
|
||||
store: &Arc<PodcastStore>,
|
||||
log: &dyn OpLog,
|
||||
on_event: impl Fn(ProgressEvent),
|
||||
) {
|
||||
let mut writer = Writer {
|
||||
store,
|
||||
log,
|
||||
active: None,
|
||||
on_event: &on_event,
|
||||
};
|
||||
while let Ok(command) = rx.recv() {
|
||||
writer.handle(command);
|
||||
}
|
||||
}
|
||||
|
||||
struct Writer<'a, F: Fn(ProgressEvent)> {
|
||||
store: &'a PodcastStore,
|
||||
log: &'a dyn OpLog,
|
||||
active: Option<Active>,
|
||||
on_event: &'a F,
|
||||
}
|
||||
|
||||
/// Runs a store action through the sync log and returns its value.
|
||||
fn record<T>(
|
||||
log: &dyn OpLog,
|
||||
mut action: impl FnMut(&mut dyn StampSource) -> Result<(T, Vec<PodcastLocalOp>)>,
|
||||
) -> Result<T> {
|
||||
let mut value = None;
|
||||
log.record(&mut |stamps| {
|
||||
let (result, ops) = action(stamps)?;
|
||||
value = Some(result);
|
||||
Ok(ops)
|
||||
})?;
|
||||
value.ok_or_else(|| anyhow!("the podcast action did not run"))
|
||||
}
|
||||
|
||||
impl<F: Fn(ProgressEvent)> Writer<'_, F> {
|
||||
fn emit(&self, event: ProgressEvent) {
|
||||
(self.on_event)(event);
|
||||
}
|
||||
|
||||
fn fail(&self, what: &str, err: anyhow::Error) {
|
||||
let message = format!("podcast progress: {what} failed: {err:#}");
|
||||
tracing::warn!("{message}");
|
||||
self.emit(ProgressEvent::Failed(message));
|
||||
}
|
||||
|
||||
fn active_mut(&mut self, playback: u64) -> Option<&mut Active> {
|
||||
self.active
|
||||
.as_mut()
|
||||
.filter(|active| active.playback == playback)
|
||||
}
|
||||
|
||||
fn handle(&mut self, command: ProgressCommand) {
|
||||
match command {
|
||||
ProgressCommand::Begin {
|
||||
playback,
|
||||
episode,
|
||||
session_id,
|
||||
position_ms,
|
||||
restart,
|
||||
now_ms,
|
||||
} => self.begin(playback, *episode, session_id, position_ms, restart, now_ms),
|
||||
ProgressCommand::Checkpoint {
|
||||
playback,
|
||||
position_ms,
|
||||
} => self.checkpoint(playback, position_ms),
|
||||
ProgressCommand::Pause {
|
||||
playback,
|
||||
position_ms,
|
||||
listened_ms,
|
||||
now_ms,
|
||||
} => {
|
||||
self.checkpoint(playback, position_ms);
|
||||
self.record_session(playback, position_ms, listened_ms, now_ms);
|
||||
}
|
||||
ProgressCommand::Seek {
|
||||
playback,
|
||||
position_ms,
|
||||
} => self.seek(playback, position_ms),
|
||||
ProgressCommand::Complete {
|
||||
playback,
|
||||
position_ms,
|
||||
listened_ms,
|
||||
now_ms,
|
||||
} => {
|
||||
self.complete(playback, position_ms);
|
||||
self.record_session(playback, position_ms, listened_ms, now_ms);
|
||||
}
|
||||
ProgressCommand::End {
|
||||
playback,
|
||||
position_ms,
|
||||
listened_ms,
|
||||
now_ms,
|
||||
} => {
|
||||
self.checkpoint(playback, position_ms);
|
||||
self.record_session(playback, position_ms, listened_ms, now_ms);
|
||||
if let Some(active) = self.active.take_if(|active| active.playback == playback) {
|
||||
self.emit(ProgressEvent::Ended {
|
||||
episode_id: active.episode.episode_id,
|
||||
});
|
||||
}
|
||||
}
|
||||
ProgressCommand::SetPlayed { episode_id, played } => {
|
||||
let result = self.set_played(&episode_id, played);
|
||||
if let Err(err) = &result {
|
||||
tracing::warn!("podcast progress: marking played failed: {err:#}");
|
||||
}
|
||||
self.emit(ProgressEvent::PlayedSet {
|
||||
played,
|
||||
result: result.map_err(|err| format!("{err:#}")),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn begin(
|
||||
&mut self,
|
||||
playback: u64,
|
||||
episode: PodcastEpisodeRef,
|
||||
session_id: String,
|
||||
position_ms: i64,
|
||||
restart: bool,
|
||||
now_ms: i64,
|
||||
) {
|
||||
let store = self.store;
|
||||
let carried = episode.progress_generation;
|
||||
let result = record(self.log, |stamps| {
|
||||
let write = if restart {
|
||||
store.restart(stamps, &episode, &session_id)?
|
||||
} else {
|
||||
store.start_session(stamps, &episode, &session_id, position_ms, carried)?
|
||||
};
|
||||
let op = write.op.clone();
|
||||
Ok((write, vec![op]))
|
||||
});
|
||||
let session = match result {
|
||||
Ok(write) => {
|
||||
let session = write.session.clone();
|
||||
self.emit(ProgressEvent::Saved(write.record));
|
||||
Some(session)
|
||||
}
|
||||
Err(err) => {
|
||||
self.fail("starting a listening session", err);
|
||||
None
|
||||
}
|
||||
};
|
||||
self.active = Some(Active {
|
||||
playback,
|
||||
session,
|
||||
episode,
|
||||
session_id,
|
||||
started_at_ms: now_ms,
|
||||
start_position_ms: position_ms,
|
||||
superseded: false,
|
||||
completed: false,
|
||||
listen_completed: false,
|
||||
});
|
||||
}
|
||||
|
||||
fn checkpoint(&mut self, playback: u64, position_ms: i64) {
|
||||
let store = self.store;
|
||||
let log = self.log;
|
||||
let Some(active) = self.active_mut(playback) else {
|
||||
return;
|
||||
};
|
||||
if active.superseded {
|
||||
return;
|
||||
}
|
||||
let Some(session) = active.session.clone() else {
|
||||
return;
|
||||
};
|
||||
let result = record(log, |stamps| {
|
||||
let outcome = store.checkpoint(stamps, &session, position_ms)?;
|
||||
let ops = match &outcome {
|
||||
CheckpointOutcome::Written(write) => vec![write.op.clone()],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
Ok((outcome, ops))
|
||||
});
|
||||
match result {
|
||||
Ok(CheckpointOutcome::Written(write)) => self.emit(ProgressEvent::Saved(write.record)),
|
||||
Ok(CheckpointOutcome::Superseded { current }) => {
|
||||
if let Some(active) = self.active_mut(playback) {
|
||||
active.superseded = true;
|
||||
}
|
||||
tracing::info!(
|
||||
episode = %current.episode_id,
|
||||
"a newer progress action took over; this playback stops saving"
|
||||
);
|
||||
self.emit(ProgressEvent::Superseded { current });
|
||||
}
|
||||
Ok(CheckpointOutcome::AlreadyCompleted { .. }) => {}
|
||||
Err(err) => self.fail("saving the position", err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicit actions of a running session (seek, completion): they take
|
||||
/// a new generation and resume saving after a supersede.
|
||||
fn explicit(
|
||||
&mut self,
|
||||
playback: u64,
|
||||
what: &str,
|
||||
action: impl Fn(&PodcastStore, &mut dyn StampSource, &ProgressSession) -> Result<ProgressWrite>,
|
||||
) -> bool {
|
||||
let store = self.store;
|
||||
let log = self.log;
|
||||
let Some(active) = self.active_mut(playback) else {
|
||||
return false;
|
||||
};
|
||||
let Some(session) = active.session.clone() else {
|
||||
return false;
|
||||
};
|
||||
let result = record(log, |stamps| {
|
||||
let write = action(store, stamps, &session)?;
|
||||
let op = write.op.clone();
|
||||
Ok((write, vec![op]))
|
||||
});
|
||||
match result {
|
||||
Ok(write) => {
|
||||
if let Some(active) = self.active_mut(playback) {
|
||||
active.session = Some(write.session.clone());
|
||||
active.superseded = false;
|
||||
}
|
||||
self.emit(ProgressEvent::Saved(write.record));
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
self.fail(what, err);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn seek(&mut self, playback: u64, position_ms: i64) {
|
||||
if self.explicit(playback, "saving a seek", |store, stamps, session| {
|
||||
store.seek(stamps, session, position_ms)
|
||||
}) && let Some(active) = self.active_mut(playback)
|
||||
{
|
||||
active.completed = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn complete(&mut self, playback: u64, position_ms: i64) {
|
||||
if self
|
||||
.active_mut(playback)
|
||||
.is_none_or(|active| active.completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if self.explicit(
|
||||
playback,
|
||||
"marking the episode finished",
|
||||
|store, stamps, session| store.complete_session(stamps, session, position_ms),
|
||||
) && let Some(active) = self.active_mut(playback)
|
||||
{
|
||||
active.completed = true;
|
||||
active.listen_completed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Records (or grows) the listening session of `playback`.
|
||||
fn record_session(&mut self, playback: u64, position_ms: i64, listened_ms: i64, now_ms: i64) {
|
||||
let store = self.store;
|
||||
let log = self.log;
|
||||
let Some(active) = self.active_mut(playback) else {
|
||||
return;
|
||||
};
|
||||
let mut session = ListenSession {
|
||||
session_id: active.session_id.clone(),
|
||||
episode_id: active.episode.episode_id.clone(),
|
||||
podcast_id: active.episode.podcast_id.clone(),
|
||||
device_id: String::new(),
|
||||
started_at_ms: active.started_at_ms,
|
||||
ended_at_ms: now_ms.max(active.started_at_ms),
|
||||
start_position_ms: active.start_position_ms.max(0),
|
||||
end_position_ms: position_ms.max(0),
|
||||
listened_ms: listened_ms.max(0),
|
||||
completed: active.listen_completed,
|
||||
};
|
||||
let episode = active.episode.clone();
|
||||
let result = record(log, |stamps| {
|
||||
session.device_id = stamps.device_id().to_string();
|
||||
let op = store.record_listen_session(stamps, &session, Some(&episode))?;
|
||||
Ok(((), op.into_iter().collect()))
|
||||
});
|
||||
if let Err(err) = result {
|
||||
self.fail("recording the listening session", err);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_played(&mut self, episode_id: &EpisodeId, played: bool) -> Result<()> {
|
||||
let episode = self
|
||||
.store
|
||||
.episode_ref(episode_id)?
|
||||
.ok_or_else(|| anyhow!("the episode is no longer in the catalog"))?;
|
||||
let store = self.store;
|
||||
let write = record(self.log, |stamps| {
|
||||
let write = if played {
|
||||
store.mark_played(stamps, &episode)?
|
||||
} else {
|
||||
store.mark_unplayed(stamps, &episode)?
|
||||
};
|
||||
let op = write.op.clone();
|
||||
Ok((write, vec![op]))
|
||||
})?;
|
||||
self.emit(ProgressEvent::Saved(write.record));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,386 +0,0 @@
|
||||
//! Subscribing and refreshing feeds.
|
||||
//!
|
||||
//! Every function records its HTTP outcome in the store's local fetch state
|
||||
//! (validators, last error, consecutive failures, next check) so the
|
||||
//! scheduler never needs its own bookkeeping, and an error never clears the
|
||||
//! stored catalog.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow, bail};
|
||||
use furumi_library::podcasts::{FeedRefresh, PodcastStore};
|
||||
use music_dht::podcast::feed::{ParsedFeed, parse_feed_with_base};
|
||||
use music_dht::podcast::{FeedUrl, PodcastId};
|
||||
|
||||
use super::fetch::{FeedBody, Fetcher, HttpStatusError, Validators};
|
||||
use super::{OpLog, blocking};
|
||||
|
||||
/// A successfully checked feed is checked again after this long.
|
||||
pub const REFRESH_INTERVAL: Duration = Duration::from_secs(60 * 60);
|
||||
/// First retry after a failed check; doubles per consecutive failure.
|
||||
pub const FAILURE_BACKOFF_BASE: Duration = Duration::from_secs(5 * 60);
|
||||
/// Longest wait between checks of a failing feed (also caps `Retry-After`).
|
||||
pub const FAILURE_BACKOFF_MAX: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
|
||||
/// Store, HTTP client and sync log shared by podcast background tasks.
|
||||
#[derive(Clone)]
|
||||
pub struct Services {
|
||||
pub store: Arc<PodcastStore>,
|
||||
pub fetcher: Arc<Fetcher>,
|
||||
pub log: Arc<dyn OpLog>,
|
||||
}
|
||||
|
||||
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}"))?;
|
||||
let store = Arc::clone(&services.store);
|
||||
let lookup = url.clone();
|
||||
let existing = blocking(move || {
|
||||
let id = store.resolve_podcast_id(&lookup)?;
|
||||
let subscribed = store
|
||||
.subscription(&id)?
|
||||
.is_some_and(|record| record.subscribed);
|
||||
let title = store.podcast(&id)?.map(|summary| summary.meta.title);
|
||||
Ok(subscribed.then_some((id, title)))
|
||||
})
|
||||
.await?;
|
||||
if let Some((podcast_id, title)) = existing {
|
||||
return Ok(SubscribeReport {
|
||||
podcast_id,
|
||||
title: title.unwrap_or_default(),
|
||||
already_subscribed: true,
|
||||
new_episodes: 0,
|
||||
});
|
||||
}
|
||||
|
||||
let response = services
|
||||
.fetcher
|
||||
.fetch_feed(&url, Validators::default())
|
||||
.await?;
|
||||
let FeedBody::Modified {
|
||||
body,
|
||||
etag,
|
||||
last_modified,
|
||||
} = response.body
|
||||
else {
|
||||
bail!("the server answered 304 Not Modified to an unconditional request");
|
||||
};
|
||||
let final_url = response.final_url;
|
||||
let feed = parse(body, &final_url).await?;
|
||||
// The feed's own address: where its permanent redirects lead.
|
||||
let moved_permanently = response.moved_permanently_to.is_some();
|
||||
let landed = response.moved_permanently_to.unwrap_or(final_url);
|
||||
|
||||
let store = Arc::clone(&services.store);
|
||||
let log = Arc::clone(&services.log);
|
||||
let image_url = feed.image_url.clone();
|
||||
let report = blocking(move || {
|
||||
// Identity comes from the URL of the first subscription. Reuse a
|
||||
// podcast this device already knows under the typed or the final
|
||||
// URL; otherwise the typed URL defines the id and a permanent
|
||||
// redirect becomes an alias below.
|
||||
let identity_url = if knows(&store, &url)? || !knows(&store, &landed)? {
|
||||
url.clone()
|
||||
} else {
|
||||
landed.clone()
|
||||
};
|
||||
let meta = feed.to_podcast_meta(identity_url);
|
||||
let mut subscribed = None;
|
||||
log.record(&mut |stamps| {
|
||||
let outcome = store.subscribe(stamps, &meta)?;
|
||||
let ops = outcome.ops.clone();
|
||||
subscribed = Some(outcome);
|
||||
Ok(ops)
|
||||
})?;
|
||||
let outcome = subscribed.ok_or_else(|| anyhow!("subscription was not recorded"))?;
|
||||
let podcast_id = outcome.podcast_id;
|
||||
let mut refreshed = None;
|
||||
log.record(&mut |stamps| {
|
||||
let outcome = store.apply_feed(
|
||||
stamps,
|
||||
&FeedRefresh {
|
||||
podcast_id: &podcast_id,
|
||||
feed: &feed,
|
||||
fetched_from: &landed,
|
||||
moved_permanently,
|
||||
},
|
||||
)?;
|
||||
let ops = outcome.ops.clone();
|
||||
refreshed = Some(outcome);
|
||||
Ok(ops)
|
||||
})?;
|
||||
let refreshed = refreshed.ok_or_else(|| anyhow!("feed refresh was not recorded"))?;
|
||||
store.record_fetch_success(
|
||||
&podcast_id,
|
||||
now_ms,
|
||||
etag.as_deref(),
|
||||
last_modified.as_deref(),
|
||||
Some(next_check_after_success(now_ms)),
|
||||
)?;
|
||||
Ok(SubscribeReport {
|
||||
podcast_id,
|
||||
title: meta.title,
|
||||
already_subscribed: outcome.already_subscribed,
|
||||
new_episodes: refreshed.new_episodes,
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
if let Some(image_url) = image_url {
|
||||
update_cover(services, &report.podcast_id, &image_url).await;
|
||||
}
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
fn knows(store: &PodcastStore, url: &FeedUrl) -> Result<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.
|
||||
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,504 @@
|
||||
//! SQLite persistence for podcast subscriptions and episode metadata.
|
||||
//!
|
||||
//! Only metadata is stored; audio stays on the publisher's host. Episodes are
|
||||
//! unique per `(podcast_id, guid)` and their row ids stay stable across
|
||||
//! refreshes, so they can be referenced from playback state. Both tables use
|
||||
//! `AUTOINCREMENT`, so ids of removed podcasts and episodes are never handed
|
||||
//! out again (a stale reference or an in-flight refresh of a removed podcast
|
||||
//! can never hit a different subscription).
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::{Mutex, MutexGuard, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use rusqlite::{Connection, OptionalExtension, Row, params};
|
||||
|
||||
use super::feed::{ParsedEpisode, ParsedFeed};
|
||||
use super::{Episode, Podcast, RefreshSummary};
|
||||
|
||||
/// Schema migrations; `PRAGMA user_version` records how many have run.
|
||||
/// Every statement is idempotent so a partially applied step can be re-run.
|
||||
const MIGRATIONS: &[&str] = &[
|
||||
// v1: subscriptions and episode metadata.
|
||||
"
|
||||
CREATE TABLE IF NOT EXISTS podcasts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
feed_url TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL,
|
||||
author TEXT,
|
||||
description TEXT,
|
||||
link TEXT,
|
||||
image_url TEXT,
|
||||
cover_url TEXT,
|
||||
cover_path TEXT,
|
||||
etag TEXT,
|
||||
last_modified TEXT,
|
||||
added_at INTEGER NOT NULL,
|
||||
last_checked_at INTEGER,
|
||||
last_error TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS podcast_episodes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
podcast_id INTEGER NOT NULL REFERENCES podcasts(id) ON DELETE CASCADE,
|
||||
guid TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
published_at INTEGER,
|
||||
duration_seconds REAL,
|
||||
audio_url TEXT NOT NULL,
|
||||
mime_type TEXT,
|
||||
byte_len INTEGER,
|
||||
link TEXT,
|
||||
image_url TEXT,
|
||||
first_seen_at INTEGER NOT NULL,
|
||||
UNIQUE (podcast_id, guid)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS podcast_episodes_by_date
|
||||
ON podcast_episodes (podcast_id, published_at DESC, id DESC);
|
||||
",
|
||||
];
|
||||
|
||||
const PODCAST_COLUMNS: &str = "
|
||||
p.id, p.feed_url, p.title, p.author, p.description, p.link, p.image_url,
|
||||
p.cover_path, p.last_checked_at, p.last_error,
|
||||
(SELECT COUNT(*) FROM podcast_episodes e WHERE e.podcast_id = p.id),
|
||||
(SELECT MAX(e.published_at) FROM podcast_episodes e WHERE e.podcast_id = p.id)";
|
||||
|
||||
const EPISODE_COLUMNS: &str = "
|
||||
id, podcast_id, guid, title, description, published_at, duration_seconds,
|
||||
audio_url, mime_type, byte_len, link, image_url";
|
||||
|
||||
pub struct Store {
|
||||
conn: Mutex<Connection>,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
/// Opens (creating if needed) the podcast database at `path`.
|
||||
pub fn open(path: &Path) -> Result<Store> {
|
||||
if let Some(parent) = path.parent()
|
||||
&& !parent.as_os_str().is_empty()
|
||||
{
|
||||
std::fs::create_dir_all(parent)
|
||||
.with_context(|| format!("creating {}", parent.display()))?;
|
||||
}
|
||||
let conn = Connection::open(path)
|
||||
.with_context(|| format!("opening podcast database {}", path.display()))?;
|
||||
Self::init(conn, true)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn open_in_memory() -> Result<Store> {
|
||||
Self::init(Connection::open_in_memory()?, false)
|
||||
}
|
||||
|
||||
fn init(conn: Connection, wal: bool) -> Result<Store> {
|
||||
conn.busy_timeout(Duration::from_secs(5))?;
|
||||
conn.pragma_update(None, "foreign_keys", true)?;
|
||||
if wal {
|
||||
// Returns the resulting mode as a row, hence query instead of execute.
|
||||
let _: String = conn.query_row("PRAGMA journal_mode=WAL", [], |row| row.get(0))?;
|
||||
}
|
||||
migrate(&conn)?;
|
||||
Ok(Store {
|
||||
conn: Mutex::new(conn),
|
||||
})
|
||||
}
|
||||
|
||||
fn conn(&self) -> MutexGuard<'_, Connection> {
|
||||
self.conn.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
pub fn list_podcasts(&self) -> Result<Vec<Podcast>> {
|
||||
let conn = self.conn();
|
||||
let mut stmt = conn.prepare(&format!(
|
||||
"SELECT {PODCAST_COLUMNS} FROM podcasts p ORDER BY p.title COLLATE NOCASE, p.id"
|
||||
))?;
|
||||
let podcasts = stmt
|
||||
.query_map([], podcast_from_row)?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
Ok(podcasts)
|
||||
}
|
||||
|
||||
pub fn podcast(&self, id: i64) -> Result<Option<Podcast>> {
|
||||
let conn = self.conn();
|
||||
Ok(conn
|
||||
.query_row(
|
||||
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.id = ?1"),
|
||||
[id],
|
||||
podcast_from_row,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
pub fn podcast_by_feed_url(&self, feed_url: &str) -> Result<Option<Podcast>> {
|
||||
let conn = self.conn();
|
||||
Ok(conn
|
||||
.query_row(
|
||||
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.feed_url = ?1"),
|
||||
[feed_url],
|
||||
podcast_from_row,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
/// Inserts a subscription and all of its episodes in one transaction.
|
||||
pub fn add_podcast(&self, feed_url: &str, feed: &ParsedFeed, now: i64) -> Result<i64> {
|
||||
let mut conn = self.conn();
|
||||
let tx = conn.transaction()?;
|
||||
let exists: bool = tx.query_row(
|
||||
"SELECT EXISTS (SELECT 1 FROM podcasts WHERE feed_url = ?1)",
|
||||
[feed_url],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
if exists {
|
||||
bail!("already subscribed");
|
||||
}
|
||||
tx.execute(
|
||||
"INSERT INTO podcasts
|
||||
(feed_url, title, author, description, link, image_url, added_at, last_checked_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?7)",
|
||||
params![
|
||||
feed_url,
|
||||
feed.title,
|
||||
feed.author,
|
||||
feed.description,
|
||||
feed.link,
|
||||
feed.image_url,
|
||||
now
|
||||
],
|
||||
)
|
||||
.map_err(|err| match err {
|
||||
rusqlite::Error::SqliteFailure(code, _)
|
||||
if code.extended_code == rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE =>
|
||||
{
|
||||
anyhow!("already subscribed")
|
||||
}
|
||||
other => other.into(),
|
||||
})?;
|
||||
let podcast_id = tx.last_insert_rowid();
|
||||
upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
|
||||
tx.commit()?;
|
||||
Ok(podcast_id)
|
||||
}
|
||||
|
||||
/// Applies a freshly fetched feed: updates channel metadata and upserts
|
||||
/// episodes by guid. Episodes no longer in the feed are kept.
|
||||
pub fn apply_feed(
|
||||
&self,
|
||||
podcast_id: i64,
|
||||
feed: &ParsedFeed,
|
||||
now: i64,
|
||||
) -> Result<RefreshSummary> {
|
||||
let mut conn = self.conn();
|
||||
let tx = conn.transaction()?;
|
||||
let updated = tx.execute(
|
||||
"UPDATE podcasts
|
||||
SET title = ?2, author = ?3, description = ?4, link = ?5, image_url = ?6,
|
||||
last_checked_at = ?7, last_error = NULL
|
||||
WHERE id = ?1",
|
||||
params![
|
||||
podcast_id,
|
||||
feed.title,
|
||||
feed.author,
|
||||
feed.description,
|
||||
feed.link,
|
||||
feed.image_url,
|
||||
now
|
||||
],
|
||||
)?;
|
||||
if updated == 0 {
|
||||
bail!("podcast {podcast_id} not found");
|
||||
}
|
||||
let summary = upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
|
||||
tx.commit()?;
|
||||
Ok(summary)
|
||||
}
|
||||
|
||||
pub fn record_check(&self, podcast_id: i64, now: i64, error: Option<&str>) -> Result<()> {
|
||||
self.conn().execute(
|
||||
"UPDATE podcasts SET last_checked_at = ?2, last_error = ?3 WHERE id = ?1",
|
||||
params![podcast_id, now, error],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn http_validators(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
|
||||
Ok(self
|
||||
.conn()
|
||||
.query_row(
|
||||
"SELECT etag, last_modified FROM podcasts WHERE id = ?1",
|
||||
[podcast_id],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?
|
||||
.unwrap_or((None, None)))
|
||||
}
|
||||
|
||||
pub fn set_http_validators(
|
||||
&self,
|
||||
podcast_id: i64,
|
||||
etag: Option<&str>,
|
||||
last_modified: Option<&str>,
|
||||
) -> Result<()> {
|
||||
self.conn().execute(
|
||||
"UPDATE podcasts SET etag = ?2, last_modified = ?3 WHERE id = ?1",
|
||||
params![podcast_id, etag, last_modified],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Records the cached cover file and the image URL it was downloaded from.
|
||||
pub fn set_cover(
|
||||
&self,
|
||||
podcast_id: i64,
|
||||
cover_url: Option<&str>,
|
||||
cover_path: Option<&str>,
|
||||
) -> Result<()> {
|
||||
self.conn().execute(
|
||||
"UPDATE podcasts SET cover_url = ?2, cover_path = ?3 WHERE id = ?1",
|
||||
params![podcast_id, cover_url, cover_path],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `(cover_url, cover_path)`: the image URL the cached cover came from
|
||||
/// and the cached file.
|
||||
pub fn cover_source(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
|
||||
Ok(self
|
||||
.conn()
|
||||
.query_row(
|
||||
"SELECT cover_url, cover_path FROM podcasts WHERE id = ?1",
|
||||
[podcast_id],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?
|
||||
.unwrap_or((None, None)))
|
||||
}
|
||||
|
||||
/// Episodes of a podcast, newest first (undated episodes last).
|
||||
pub fn episodes(&self, podcast_id: i64) -> Result<Vec<Episode>> {
|
||||
let conn = self.conn();
|
||||
let mut stmt = conn.prepare(&format!(
|
||||
"SELECT {EPISODE_COLUMNS} FROM podcast_episodes
|
||||
WHERE podcast_id = ?1
|
||||
ORDER BY published_at IS NULL, published_at DESC, id DESC"
|
||||
))?;
|
||||
let episodes = stmt
|
||||
.query_map([podcast_id], episode_from_row)?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
Ok(episodes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn episode(&self, episode_id: i64) -> Result<Option<Episode>> {
|
||||
Ok(self
|
||||
.conn()
|
||||
.query_row(
|
||||
&format!("SELECT {EPISODE_COLUMNS} FROM podcast_episodes WHERE id = ?1"),
|
||||
[episode_id],
|
||||
episode_from_row,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
/// Deletes a subscription and its episodes; returns the cached cover path
|
||||
/// so the caller can delete the file.
|
||||
pub fn remove_podcast(&self, podcast_id: i64) -> Result<Option<String>> {
|
||||
let mut conn = self.conn();
|
||||
let tx = conn.transaction()?;
|
||||
let cover_path: Option<Option<String>> = tx
|
||||
.query_row(
|
||||
"SELECT cover_path FROM podcasts WHERE id = ?1",
|
||||
[podcast_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
// Explicit delete keeps this correct even on a connection where the
|
||||
// foreign-key cascade is somehow disabled.
|
||||
tx.execute(
|
||||
"DELETE FROM podcast_episodes WHERE podcast_id = ?1",
|
||||
[podcast_id],
|
||||
)?;
|
||||
tx.execute("DELETE FROM podcasts WHERE id = ?1", [podcast_id])?;
|
||||
tx.commit()?;
|
||||
Ok(cover_path.flatten())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) fn schema_version(&self) -> Result<i64> {
|
||||
Ok(self
|
||||
.conn()
|
||||
.query_row("PRAGMA user_version", [], |row| row.get(0))?)
|
||||
}
|
||||
}
|
||||
|
||||
fn migrate(conn: &Connection) -> Result<()> {
|
||||
let version: i64 = conn.query_row("PRAGMA user_version", [], |row| row.get(0))?;
|
||||
let target = MIGRATIONS.len() as i64;
|
||||
if version > target {
|
||||
bail!("podcast database schema v{version} is newer than this Furumi (v{target})");
|
||||
}
|
||||
for (index, sql) in MIGRATIONS.iter().enumerate().skip(version as usize) {
|
||||
let next = index as i64 + 1;
|
||||
conn.execute_batch(&format!(
|
||||
"BEGIN IMMEDIATE; {sql}; PRAGMA user_version = {next}; COMMIT;"
|
||||
))
|
||||
.inspect_err(|_| {
|
||||
let _ = conn.execute_batch("ROLLBACK");
|
||||
})
|
||||
.with_context(|| format!("migrating podcast database to v{next}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn podcast_from_row(row: &Row<'_>) -> rusqlite::Result<Podcast> {
|
||||
let episode_count: i64 = row.get(10)?;
|
||||
Ok(Podcast {
|
||||
id: row.get(0)?,
|
||||
feed_url: row.get(1)?,
|
||||
title: row.get(2)?,
|
||||
author: row.get(3)?,
|
||||
description: row.get(4)?,
|
||||
link: row.get(5)?,
|
||||
image_url: row.get(6)?,
|
||||
cover_path: row.get(7)?,
|
||||
last_checked_at: row.get(8)?,
|
||||
last_error: row.get(9)?,
|
||||
episode_count: usize::try_from(episode_count).unwrap_or(0),
|
||||
latest_published_at: row.get(11)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn episode_from_row(row: &Row<'_>) -> rusqlite::Result<Episode> {
|
||||
let byte_len: Option<i64> = row.get(9)?;
|
||||
Ok(Episode {
|
||||
id: row.get(0)?,
|
||||
podcast_id: row.get(1)?,
|
||||
guid: row.get(2)?,
|
||||
title: row.get(3)?,
|
||||
description: row.get(4)?,
|
||||
published_at: row.get(5)?,
|
||||
duration_seconds: row.get(6)?,
|
||||
audio_url: row.get(7)?,
|
||||
mime_type: row.get(8)?,
|
||||
byte_len: byte_len.and_then(|len| u64::try_from(len).ok()),
|
||||
link: row.get(10)?,
|
||||
image_url: row.get(11)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// The mutable columns of an episode row, for change detection.
|
||||
#[derive(PartialEq)]
|
||||
struct EpisodeFields {
|
||||
title: String,
|
||||
description: Option<String>,
|
||||
published_at: Option<i64>,
|
||||
duration_seconds: Option<f64>,
|
||||
audio_url: String,
|
||||
mime_type: Option<String>,
|
||||
byte_len: Option<i64>,
|
||||
link: Option<String>,
|
||||
image_url: Option<String>,
|
||||
}
|
||||
|
||||
impl EpisodeFields {
|
||||
fn from_parsed(episode: &ParsedEpisode) -> EpisodeFields {
|
||||
EpisodeFields {
|
||||
title: episode.title.clone(),
|
||||
description: episode.description.clone(),
|
||||
published_at: episode.published_at,
|
||||
duration_seconds: episode.duration_seconds,
|
||||
audio_url: episode.audio_url.clone(),
|
||||
mime_type: episode.mime_type.clone(),
|
||||
byte_len: episode
|
||||
.byte_len
|
||||
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
|
||||
link: episode.link.clone(),
|
||||
image_url: episode.image_url.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn upsert_episodes(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
podcast_id: i64,
|
||||
episodes: &[ParsedEpisode],
|
||||
now: i64,
|
||||
) -> Result<RefreshSummary> {
|
||||
let mut summary = RefreshSummary::default();
|
||||
let mut select = tx.prepare_cached(
|
||||
"SELECT id, title, description, published_at, duration_seconds, audio_url,
|
||||
mime_type, byte_len, link, image_url
|
||||
FROM podcast_episodes WHERE podcast_id = ?1 AND guid = ?2",
|
||||
)?;
|
||||
let mut insert = tx.prepare_cached(
|
||||
"INSERT INTO podcast_episodes
|
||||
(podcast_id, guid, title, description, published_at, duration_seconds,
|
||||
audio_url, mime_type, byte_len, link, image_url, first_seen_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
|
||||
)?;
|
||||
let mut update = tx.prepare_cached(
|
||||
"UPDATE podcast_episodes
|
||||
SET title = ?2, description = ?3, published_at = ?4, duration_seconds = ?5,
|
||||
audio_url = ?6, mime_type = ?7, byte_len = ?8, link = ?9, image_url = ?10
|
||||
WHERE id = ?1",
|
||||
)?;
|
||||
for episode in episodes {
|
||||
let fields = EpisodeFields::from_parsed(episode);
|
||||
let existing = select
|
||||
.query_row(params![podcast_id, episode.guid], |row| {
|
||||
Ok((
|
||||
row.get::<_, i64>(0)?,
|
||||
EpisodeFields {
|
||||
title: row.get(1)?,
|
||||
description: row.get(2)?,
|
||||
published_at: row.get(3)?,
|
||||
duration_seconds: row.get(4)?,
|
||||
audio_url: row.get(5)?,
|
||||
mime_type: row.get(6)?,
|
||||
byte_len: row.get(7)?,
|
||||
link: row.get(8)?,
|
||||
image_url: row.get(9)?,
|
||||
},
|
||||
))
|
||||
})
|
||||
.optional()?;
|
||||
match existing {
|
||||
None => {
|
||||
insert.execute(params![
|
||||
podcast_id,
|
||||
episode.guid,
|
||||
fields.title,
|
||||
fields.description,
|
||||
fields.published_at,
|
||||
fields.duration_seconds,
|
||||
fields.audio_url,
|
||||
fields.mime_type,
|
||||
fields.byte_len,
|
||||
fields.link,
|
||||
fields.image_url,
|
||||
now
|
||||
])?;
|
||||
summary.new_episodes += 1;
|
||||
}
|
||||
Some((id, stored)) if stored != fields => {
|
||||
update.execute(params![
|
||||
id,
|
||||
fields.title,
|
||||
fields.description,
|
||||
fields.published_at,
|
||||
fields.duration_seconds,
|
||||
fields.audio_url,
|
||||
fields.mime_type,
|
||||
fields.byte_len,
|
||||
fields.link,
|
||||
fields.image_url
|
||||
])?;
|
||||
summary.updated_episodes += 1;
|
||||
}
|
||||
Some(_) => {}
|
||||
}
|
||||
}
|
||||
Ok(summary)
|
||||
}
|
||||
+20
-197
@@ -1,9 +1,5 @@
|
||||
//! Seekable playback of a remote episode without downloading it first.
|
||||
//!
|
||||
//! Audio is never stored: the reader keeps a bounded in-memory window and
|
||||
//! the episode stays on the publisher's server (it is not imported, hashed
|
||||
//! or offered to peers).
|
||||
//!
|
||||
//! [`open`] issues one `Range: bytes=0-` request and hands the response to a
|
||||
//! dedicated downloader thread. The thread fills a bounded in-memory window
|
||||
//! around the reader position:
|
||||
@@ -41,18 +37,6 @@
|
||||
//! body, and long episodes are fine. The request-level
|
||||
//! `RequestBuilder::timeout` (which *is* a total timeout) is never used.
|
||||
//!
|
||||
//! Cancellation: a stream belongs to one playback start ([`CancelToken`]).
|
||||
//! Once that start is superseded (another item, stop, restart), every read,
|
||||
//! including one blocked waiting for data inside the audio callback, fails
|
||||
//! within [`CANCEL_POLL`], and the downloader thread stops. So a stalled
|
||||
//! stream never holds the audio thread past the user's next choice, and a
|
||||
//! superseded resume (which may be reading and discarding up to its offset)
|
||||
//! stops downloading.
|
||||
//!
|
||||
//! End of playback: [`StreamProbe::end`] tells whether the reader saw the
|
||||
//! file end the way the server described it or stopped on an error, so an
|
||||
//! episode whose connection was lost is not taken as played to the end.
|
||||
//!
|
||||
//! Decoder note for the player integration: symphonia's MP3 demuxer in
|
||||
//! accurate seek mode never seeks by byte offset (a backward seek re-parses
|
||||
//! from the first frame), which would re-download the episode. Build rodio
|
||||
@@ -65,11 +49,9 @@ use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use reqwest::StatusCode;
|
||||
use reqwest::blocking::{Client, Response};
|
||||
use reqwest::header::{CONTENT_RANGE, CONTENT_TYPE, RANGE};
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
use super::fetch::redact;
|
||||
|
||||
/// Maximum bytes buffered ahead of the reader position.
|
||||
pub const READ_AHEAD: u64 = 8 * 1024 * 1024;
|
||||
@@ -103,89 +85,9 @@ const STARVED_BACKOFF: Duration = Duration::from_secs(1);
|
||||
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
#[cfg(test)]
|
||||
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(4);
|
||||
/// How often a blocked read or an idle downloader checks its
|
||||
/// [`CancelToken`].
|
||||
pub const CANCEL_POLL: Duration = Duration::from_millis(100);
|
||||
|
||||
/// Tells a stream that the playback start it belongs to was superseded.
|
||||
#[derive(Clone)]
|
||||
pub struct CancelToken(Option<Arc<dyn Fn() -> bool + Send + Sync>>);
|
||||
|
||||
impl CancelToken {
|
||||
/// A stream that is only closed by dropping it.
|
||||
#[cfg(test)]
|
||||
pub fn never() -> CancelToken {
|
||||
CancelToken(None)
|
||||
}
|
||||
|
||||
/// `cancelled` becomes true once the start is superseded; it is polled.
|
||||
pub fn new(cancelled: impl Fn() -> bool + Send + Sync + 'static) -> CancelToken {
|
||||
CancelToken(Some(Arc::new(cancelled)))
|
||||
}
|
||||
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
self.0.as_ref().is_some_and(|cancelled| cancelled())
|
||||
}
|
||||
}
|
||||
|
||||
/// The playback start was superseded while the stream was opened or read.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Cancelled;
|
||||
|
||||
impl std::fmt::Display for Cancelled {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("the episode start was superseded")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Cancelled {}
|
||||
|
||||
fn cancelled_error() -> io::Error {
|
||||
io::Error::other(Cancelled)
|
||||
}
|
||||
|
||||
/// How the reader's last read ended, for telling a finished episode from a
|
||||
/// lost connection.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StreamEnd {
|
||||
/// No read failed and the server announced the file size, so the
|
||||
/// decoder ended at the end the server described.
|
||||
Complete,
|
||||
/// No read failed, but the server sent no size: a connection closed
|
||||
/// early looks exactly like the end of the file.
|
||||
Unknown,
|
||||
/// The last read failed (outage longer than [`STARVE_TIMEOUT`], a client
|
||||
/// error such as an expired URL, or cancellation).
|
||||
Failed,
|
||||
}
|
||||
|
||||
/// Read-only view of a stream's state that outlives handing the reader to
|
||||
/// the decoder.
|
||||
#[derive(Clone)]
|
||||
pub struct StreamProbe(Arc<Shared>);
|
||||
|
||||
impl std::fmt::Debug for StreamProbe {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_tuple("StreamProbe").field(&self.end()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamProbe {
|
||||
pub fn end(&self) -> StreamEnd {
|
||||
let state = self.0.lock();
|
||||
if state.read_failed {
|
||||
StreamEnd::Failed
|
||||
} else if state.declared_total {
|
||||
StreamEnd::Complete
|
||||
} else {
|
||||
StreamEnd::Unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OpenedStream {
|
||||
pub reader: HttpStream,
|
||||
pub probe: StreamProbe,
|
||||
pub byte_len: Option<u64>,
|
||||
pub mime_type: Option<String>,
|
||||
pub seekable: bool,
|
||||
@@ -199,7 +101,6 @@ pub struct HttpStream {
|
||||
|
||||
struct Shared {
|
||||
state: Mutex<State>,
|
||||
cancel: CancelToken,
|
||||
/// Signalled when data, EOF, an error or downloader exit is available to
|
||||
/// the reader.
|
||||
data_ready: Condvar,
|
||||
@@ -222,11 +123,6 @@ struct State {
|
||||
/// Last position the reader consumed from the window.
|
||||
reader_pos: u64,
|
||||
total: Option<u64>,
|
||||
/// `total` comes from the server (`Content-Length` / `Content-Range`),
|
||||
/// not from a connection that closed.
|
||||
declared_total: bool,
|
||||
/// The reader's last read returned an error (cleared by data).
|
||||
read_failed: bool,
|
||||
ranges: bool,
|
||||
/// The current generation reached the end of the body.
|
||||
done: bool,
|
||||
@@ -288,55 +184,24 @@ impl Shared {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`open_with`] without cancellation.
|
||||
#[cfg(test)]
|
||||
pub fn open(url: &str) -> Result<OpenedStream> {
|
||||
open_with(url, CancelToken::never())
|
||||
}
|
||||
|
||||
/// Opens a remote audio file for streaming playback. Blocking: call it from
|
||||
/// `spawn_blocking` or a plain thread, never directly on a Tokio worker.
|
||||
///
|
||||
/// Only http(s) URLs are accepted. Errors never contain the URL's query,
|
||||
/// which may hold a private token. Fails with [`Cancelled`] when `cancel`
|
||||
/// fires before the stream is ready.
|
||||
pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
|
||||
let parsed = Url::parse(url).context("the episode audio URL is invalid")?;
|
||||
if !matches!(parsed.scheme(), "http" | "https") {
|
||||
bail!("unsupported audio URL scheme {:?}", parsed.scheme());
|
||||
}
|
||||
let shown = redact(&parsed);
|
||||
pub fn open(url: &str) -> Result<OpenedStream> {
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
let client = Client::builder()
|
||||
.connect_timeout(CONNECT_TIMEOUT)
|
||||
// Per-read stall timeout, see the module docs.
|
||||
.timeout(STALL_TIMEOUT)
|
||||
.redirect(reqwest::redirect::Policy::custom(|attempt| {
|
||||
if attempt.previous().len() >= 10 {
|
||||
attempt.error("too many redirects")
|
||||
} else if matches!(attempt.url().scheme(), "http" | "https") {
|
||||
attempt.follow()
|
||||
} else {
|
||||
attempt.stop()
|
||||
}
|
||||
}))
|
||||
.user_agent(concat!(
|
||||
"furumi/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (podcast reader)"
|
||||
))
|
||||
.redirect(reqwest::redirect::Policy::limited(10))
|
||||
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
|
||||
.build()
|
||||
.context("building the streaming HTTP client")?;
|
||||
let response = client
|
||||
.get(parsed)
|
||||
.get(url)
|
||||
.header(RANGE, "bytes=0-")
|
||||
.send()
|
||||
.map_err(|err| anyhow!(err.without_url()))
|
||||
.with_context(|| format!("requesting {shown}"))?;
|
||||
if cancel.is_cancelled() {
|
||||
return Err(Cancelled.into());
|
||||
}
|
||||
let opened = classify(&response, 0).with_context(|| format!("requesting {shown}"))?;
|
||||
.with_context(|| format!("requesting {url}"))?;
|
||||
let opened = classify(&response, 0).with_context(|| format!("requesting {url}"))?;
|
||||
let mime_type = response
|
||||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
@@ -356,8 +221,6 @@ pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
|
||||
head: Vec::with_capacity(HEAD_KEEP),
|
||||
reader_pos: 0,
|
||||
total: opened.total,
|
||||
declared_total: opened.total.is_some(),
|
||||
read_failed: false,
|
||||
ranges: opened.ranges,
|
||||
done: false,
|
||||
failed: false,
|
||||
@@ -370,22 +233,19 @@ pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
|
||||
peak_buffered: 0,
|
||||
peak_capacity,
|
||||
}),
|
||||
cancel,
|
||||
data_ready: Condvar::new(),
|
||||
space_ready: Condvar::new(),
|
||||
});
|
||||
// Requests continue at the final URL of the redirect chain.
|
||||
let downloader = Downloader {
|
||||
shared: Arc::clone(&shared),
|
||||
client,
|
||||
url: response.url().clone(),
|
||||
url: url.to_string(),
|
||||
};
|
||||
std::thread::Builder::new()
|
||||
.name("podcast-stream".to_string())
|
||||
.spawn(move || downloader.run(response))
|
||||
.context("spawning the podcast stream thread")?;
|
||||
Ok(OpenedStream {
|
||||
probe: StreamProbe(Arc::clone(&shared)),
|
||||
reader: HttpStream { pos: 0, shared },
|
||||
byte_len: opened.total,
|
||||
mime_type,
|
||||
@@ -428,32 +288,9 @@ fn classify(response: &Response, requested: u64) -> Result<Classified> {
|
||||
ranges: false,
|
||||
});
|
||||
}
|
||||
Err(StreamStatusError(status.as_u16()).into())
|
||||
bail!("HTTP {status}");
|
||||
}
|
||||
|
||||
/// The audio server answered with an error status.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StreamStatusError(pub u16);
|
||||
|
||||
impl StreamStatusError {
|
||||
/// The URL itself is wrong or expired (4xx other than 408/429): the
|
||||
/// feed may know a newer address for the episode.
|
||||
pub fn url_is_stale(self) -> bool {
|
||||
(400..500).contains(&self.0) && self.0 != 408 && self.0 != 429
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StreamStatusError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match StatusCode::from_u16(self.0) {
|
||||
Ok(status) => write!(f, "HTTP {status}"),
|
||||
Err(_) => write!(f, "HTTP {}", self.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for StreamStatusError {}
|
||||
|
||||
/// Parses `bytes <start>-<end>/<total|*>` into `(start, total)`.
|
||||
fn parse_content_range(value: &str) -> Option<(u64, Option<u64>)> {
|
||||
let value = value.trim();
|
||||
@@ -482,7 +319,7 @@ enum Failure {
|
||||
struct Downloader {
|
||||
shared: Arc<Shared>,
|
||||
client: Client,
|
||||
url: Url,
|
||||
url: String,
|
||||
}
|
||||
|
||||
/// Marks the downloader as exited however `run` returns (including a panic).
|
||||
@@ -541,11 +378,7 @@ impl Downloader {
|
||||
let Some(body) = response.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
let read = body.read(&mut buf);
|
||||
if self.shared.cancel.is_cancelled() {
|
||||
return;
|
||||
}
|
||||
match read {
|
||||
match body.read(&mut buf) {
|
||||
Ok(0) => {
|
||||
response = None;
|
||||
let total = self.shared.lock().total;
|
||||
@@ -587,7 +420,7 @@ impl Downloader {
|
||||
) -> bool {
|
||||
let mut state = self.shared.lock();
|
||||
loop {
|
||||
if state.closed || self.shared.cancel.is_cancelled() {
|
||||
if state.closed {
|
||||
return false;
|
||||
}
|
||||
if state.generation != *generation {
|
||||
@@ -615,9 +448,8 @@ impl Downloader {
|
||||
state = self
|
||||
.shared
|
||||
.space_ready
|
||||
.wait_timeout(state, CANCEL_POLL)
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
.wait(state)
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -625,13 +457,13 @@ impl Downloader {
|
||||
/// `Ok(None)` means the server has nothing at that offset.
|
||||
fn reopen(&self, offset: u64) -> std::result::Result<Option<(Response, u64)>, Failure> {
|
||||
let ranges = self.shared.lock().ranges;
|
||||
let mut request = self.client.get(self.url.clone());
|
||||
let mut request = self.client.get(&self.url);
|
||||
if ranges {
|
||||
request = request.header(RANGE, format!("bytes={offset}-"));
|
||||
}
|
||||
let response = request
|
||||
.send()
|
||||
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err.without_url()))))?;
|
||||
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err))))?;
|
||||
let status = response.status();
|
||||
if ranges && status == StatusCode::RANGE_NOT_SATISFIABLE {
|
||||
return Ok(None);
|
||||
@@ -649,9 +481,8 @@ impl Downloader {
|
||||
// The server stopped honouring Range: read from 0 and discard.
|
||||
state.ranges = false;
|
||||
}
|
||||
if state.total.is_none() && classified.total.is_some() {
|
||||
if state.total.is_none() {
|
||||
state.total = classified.total;
|
||||
state.declared_total = true;
|
||||
}
|
||||
Ok(Some((response, classified.start)))
|
||||
}
|
||||
@@ -701,7 +532,7 @@ impl Downloader {
|
||||
// Wakes early when the reader goes away, seeks elsewhere or starts
|
||||
// waiting for data (which shortens the delay).
|
||||
loop {
|
||||
if state.closed || self.shared.cancel.is_cancelled() {
|
||||
if state.closed {
|
||||
return false;
|
||||
}
|
||||
if state.generation != generation {
|
||||
@@ -719,7 +550,7 @@ impl Downloader {
|
||||
state = self
|
||||
.shared
|
||||
.space_ready
|
||||
.wait_timeout(state, (limit - elapsed).min(CANCEL_POLL))
|
||||
.wait_timeout(state, limit - elapsed)
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
}
|
||||
@@ -773,11 +604,6 @@ impl Read for HttpStream {
|
||||
let state = shared.lock();
|
||||
let (mut state, result) = read_locked(shared, &mut self.pos, state, buf);
|
||||
state.reader_waiting = false;
|
||||
match &result {
|
||||
Ok(0) => {}
|
||||
Ok(_) => state.read_failed = false,
|
||||
Err(_) => state.read_failed = true,
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
@@ -793,9 +619,6 @@ fn read_locked<'a>(
|
||||
// When this read started waiting, and the download progress then.
|
||||
let mut starved: Option<(Instant, u64)> = None;
|
||||
loop {
|
||||
if shared.cancel.is_cancelled() {
|
||||
return (state, Err(cancelled_error()));
|
||||
}
|
||||
if state.total.is_some_and(|total| *pos >= total) {
|
||||
return (state, Ok(0));
|
||||
}
|
||||
@@ -869,7 +692,7 @@ fn read_locked<'a>(
|
||||
}
|
||||
state = shared
|
||||
.data_ready
|
||||
.wait_timeout(state, (STARVE_TIMEOUT - waited).min(CANCEL_POLL))
|
||||
.wait_timeout(state, STARVE_TIMEOUT - waited)
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.0;
|
||||
continue;
|
||||
|
||||
@@ -1,405 +0,0 @@
|
||||
//! Tests of the HTTP Range stream reader against the local test server:
|
||||
//! sequential reads, seeks inside and outside the window, servers without
|
||||
//! Range or Content-Length, transient cuts, persistent failures, bounded
|
||||
//! memory and thread shutdown.
|
||||
|
||||
use super::stream::{self, MAX_BUFFERED};
|
||||
use super::test_server::{Route, TestServer, noise};
|
||||
use std::io::{ErrorKind, Read, Seek, SeekFrom};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MIB: usize = 1024 * 1024;
|
||||
|
||||
fn read_to_end(reader: &mut impl Read) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
reader.read_to_end(&mut out).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
fn read_n(reader: &mut impl Read, len: usize) -> Vec<u8> {
|
||||
let mut out = vec![0; len];
|
||||
reader.read_exact(&mut out).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
fn serve(route: Route) -> (TestServer, String) {
|
||||
let server = TestServer::start();
|
||||
server.set("/audio.mp3", route);
|
||||
let url = server.url("/audio.mp3");
|
||||
(server, url)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sequential_read_matches_source() {
|
||||
let body = noise(3 * MIB + 123, 1);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg; charset=binary"));
|
||||
let mut opened = stream::open(&url).unwrap();
|
||||
assert_eq!(opened.byte_len, Some(body.len() as u64));
|
||||
assert_eq!(opened.mime_type.as_deref(), Some("audio/mpeg"));
|
||||
assert!(opened.seekable);
|
||||
assert_eq!(read_to_end(&mut opened.reader), body);
|
||||
let mut buf = [0u8; 16];
|
||||
assert_eq!(opened.reader.read(&mut buf).unwrap(), 0, "EOF stays EOF");
|
||||
assert_eq!(server.ranges("/audio.mp3"), [Some("bytes=0-".to_string())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seeks_forward_backward_and_from_end() {
|
||||
let body = noise(12 * MIB, 2);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg"));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
assert_eq!(read_n(&mut reader, 1000), body[..1000]);
|
||||
|
||||
// Far forward: outside the read-ahead window -> new range request.
|
||||
let far = 11 * MIB + 7;
|
||||
assert_eq!(
|
||||
reader.seek(SeekFrom::Start(far as u64)).unwrap(),
|
||||
far as u64
|
||||
);
|
||||
assert_eq!(read_n(&mut reader, 70_000), body[far..far + 70_000]);
|
||||
assert!(
|
||||
server
|
||||
.ranges("/audio.mp3")
|
||||
.contains(&Some(format!("bytes={far}-")))
|
||||
);
|
||||
|
||||
// Small backward seek within the kept window: no new request.
|
||||
let requests = server.ranges("/audio.mp3").len();
|
||||
reader.seek(SeekFrom::Current(-50_000)).unwrap();
|
||||
let pos = far + 20_000;
|
||||
assert_eq!(read_n(&mut reader, 1000), body[pos..pos + 1000]);
|
||||
assert_eq!(server.ranges("/audio.mp3").len(), requests);
|
||||
|
||||
// Back into the permanently kept head: no new request.
|
||||
reader.seek(SeekFrom::Start(10)).unwrap();
|
||||
assert_eq!(read_n(&mut reader, 100), body[10..110]);
|
||||
assert_eq!(server.ranges("/audio.mp3").len(), requests);
|
||||
|
||||
// Backward outside the window and the head.
|
||||
let back = 2 * MIB + 3;
|
||||
reader.seek(SeekFrom::Start(back as u64)).unwrap();
|
||||
assert_eq!(read_n(&mut reader, 300_000), body[back..back + 300_000]);
|
||||
|
||||
// From the end.
|
||||
let end = reader.seek(SeekFrom::End(-100)).unwrap();
|
||||
assert_eq!(end, body.len() as u64 - 100);
|
||||
assert_eq!(read_to_end(&mut reader), body[body.len() - 100..]);
|
||||
|
||||
// Past the end.
|
||||
let beyond = reader.seek(SeekFrom::End(50)).unwrap();
|
||||
assert_eq!(beyond, body.len() as u64 + 50);
|
||||
let mut buf = [0u8; 8];
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
||||
assert_eq!(
|
||||
reader
|
||||
.seek(SeekFrom::Current(-(beyond as i64) - 1))
|
||||
.unwrap_err()
|
||||
.kind(),
|
||||
ErrorKind::InvalidInput
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_without_range_support() {
|
||||
let body = noise(3 * MIB, 3);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").no_ranges());
|
||||
let opened = stream::open(&url).unwrap();
|
||||
assert!(opened.seekable, "length is known");
|
||||
let mut reader = opened.reader;
|
||||
assert_eq!(read_n(&mut reader, 400_000), body[..400_000]);
|
||||
// Forward seek: read and discard on the same response.
|
||||
reader.seek(SeekFrom::Start(2 * MIB as u64)).unwrap();
|
||||
assert_eq!(read_n(&mut reader, 1000), body[2 * MIB..2 * MIB + 1000]);
|
||||
// Backward seek outside window and head: restart from 0.
|
||||
reader.seek(SeekFrom::Start(300_000)).unwrap();
|
||||
let _ = read_n(&mut reader, 1);
|
||||
reader.seek(SeekFrom::Start(300_000)).unwrap();
|
||||
assert_eq!(read_to_end(&mut reader), body[300_000..]);
|
||||
let requests = server.ranges("/audio.mp3");
|
||||
assert!(requests.len() >= 2, "{requests:?}");
|
||||
assert!(requests[1..].iter().all(Option::is_none), "{requests:?}");
|
||||
reader.seek(SeekFrom::End(-10)).unwrap();
|
||||
assert_eq!(read_to_end(&mut reader), body[body.len() - 10..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_length_is_not_seekable_from_end() {
|
||||
let body = noise(MIB + 5, 4);
|
||||
let (_server, url) = serve(
|
||||
Route::new(body.clone(), "audio/mpeg")
|
||||
.no_ranges()
|
||||
.no_length(),
|
||||
);
|
||||
let opened = stream::open(&url).unwrap();
|
||||
assert!(!opened.seekable);
|
||||
assert_eq!(opened.byte_len, None);
|
||||
let mut reader = opened.reader;
|
||||
assert_eq!(
|
||||
reader.seek(SeekFrom::End(0)).unwrap_err().kind(),
|
||||
ErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(read_to_end(&mut reader), body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transient_disconnects_are_retried() {
|
||||
let body = noise(2 * MIB, 5);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(700_000, 2));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
assert_eq!(read_to_end(&mut reader), body);
|
||||
let requests = server.ranges("/audio.mp3");
|
||||
assert_eq!(requests.len(), 3, "{requests:?}");
|
||||
assert_eq!(requests[1], Some("bytes=700000-".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persistent_failure_surfaces_an_error() {
|
||||
let body = noise(2 * MIB, 6);
|
||||
let (_server, url) = serve(Route::new(body, "audio/mpeg").cut(100_000, 100));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
let mut out = Vec::new();
|
||||
let started = Instant::now();
|
||||
let err = reader.read_to_end(&mut out).unwrap_err();
|
||||
assert_eq!(out.len(), 100_000);
|
||||
assert_eq!(err.kind(), ErrorKind::TimedOut, "{err}");
|
||||
assert!(err.to_string().contains("stalled"), "{err}");
|
||||
// Bounded by the starvation deadline, not by a retry count.
|
||||
assert!(started.elapsed() >= stream::STARVE_TIMEOUT);
|
||||
assert!(started.elapsed() < stream::STARVE_TIMEOUT * 3);
|
||||
}
|
||||
|
||||
/// An outage that ends while buffered audio is still playing must not
|
||||
/// end playback: the downloader keeps retrying instead of giving up
|
||||
/// after a fixed number of attempts.
|
||||
#[test]
|
||||
fn outage_shorter_than_the_buffer_is_bridged() {
|
||||
let body = noise(3 * MIB, 10);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(MIB, 4));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
assert_eq!(read_n(&mut reader, 1000), body[..1000]);
|
||||
// "Play" buffered audio while the server fails four times and then
|
||||
// recovers.
|
||||
std::thread::sleep(Duration::from_millis(4500));
|
||||
assert_eq!(read_to_end(&mut reader), body[1000..]);
|
||||
let requests = server.ranges("/audio.mp3");
|
||||
assert_eq!(requests.len(), 5, "{requests:?}");
|
||||
assert!(
|
||||
requests[1..]
|
||||
.iter()
|
||||
.all(|range| range.as_deref() == Some("bytes=1048576-")),
|
||||
"{requests:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn client_errors_are_reported_without_waiting() {
|
||||
let body = noise(2 * MIB, 11);
|
||||
let (server, url) = serve(Route::new(body, "audio/mpeg").cut(100_000, 1));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
// The signed URL "expires" after the first response.
|
||||
server.set("/audio.mp3", Route::new("gone", "text/plain").status(404));
|
||||
let started = Instant::now();
|
||||
let mut out = Vec::new();
|
||||
let err = reader.read_to_end(&mut out).unwrap_err();
|
||||
assert_eq!(out.len(), 100_000);
|
||||
assert!(err.to_string().contains("404"), "{err}");
|
||||
assert!(
|
||||
started.elapsed() < stream::STARVE_TIMEOUT,
|
||||
"{:?}",
|
||||
started.elapsed()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_body_keeps_memory_bounded() {
|
||||
let body = noise(20 * MIB, 7);
|
||||
let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg"));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
let mut buf = vec![0u8; 32 * 1024];
|
||||
let mut pos = 0usize;
|
||||
let mut hasher_ok = true;
|
||||
loop {
|
||||
let read = reader.read(&mut buf).unwrap();
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher_ok &= buf[..read] == body[pos..pos + read];
|
||||
pos += read;
|
||||
if pos % (2 * MIB) < 32 * 1024 && pos + stream::READ_AHEAD as usize <= body.len() {
|
||||
// Let the downloader run ahead until it pauses at its cap.
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
}
|
||||
assert!(reader.buffered_bytes() <= MAX_BUFFERED);
|
||||
}
|
||||
assert_eq!(pos, body.len());
|
||||
assert!(hasher_ok, "data mismatch");
|
||||
let peak = reader.peak_buffered_bytes();
|
||||
assert!(peak <= MAX_BUFFERED, "peak {peak} > {MAX_BUFFERED}");
|
||||
assert!(
|
||||
peak > 4 * MIB,
|
||||
"read-ahead should have buffered, peak {peak}"
|
||||
);
|
||||
// Far seeks restart the window without growing its allocation.
|
||||
for target in [MIB + 5, 15 * MIB, 3 * MIB] {
|
||||
reader.seek(SeekFrom::Start(target as u64)).unwrap();
|
||||
assert_eq!(read_n(&mut reader, 1000), body[target..target + 1000]);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
}
|
||||
// The allocation, not just the length, is bounded.
|
||||
let allocated = reader.peak_allocated_bytes();
|
||||
assert!(
|
||||
allocated <= MAX_BUFFERED,
|
||||
"allocated {allocated} > {MAX_BUFFERED}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_the_reader_stops_the_thread() {
|
||||
let body = noise(20 * MIB, 8);
|
||||
let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg"));
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
assert_eq!(read_n(&mut reader, 100_000), body[..100_000]);
|
||||
let exited = reader.exit_probe();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
// Let it fill the read-ahead window (it then pauses).
|
||||
while reader.buffered_bytes() < stream::READ_AHEAD as usize && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
assert!(!exited());
|
||||
drop(reader);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !exited() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
assert!(exited(), "downloader thread did not stop");
|
||||
|
||||
// Also while actively downloading (not paused).
|
||||
let mut reader = stream::open(&url).unwrap().reader;
|
||||
let exited = reader.exit_probe();
|
||||
let _ = read_n(&mut reader, 10);
|
||||
drop(reader);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !exited() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
assert!(exited(), "downloader thread did not stop");
|
||||
}
|
||||
|
||||
/// The documented calling convention: `open` on Tokio's blocking pool,
|
||||
/// reads on another plain thread (like the audio thread).
|
||||
#[test]
|
||||
fn open_from_spawn_blocking() {
|
||||
let body = noise(MIB, 9);
|
||||
let (server, url) = serve(Route::new(body.clone(), "audio/mpeg"));
|
||||
server.set("/missing.mp3", Route::new("x", "text/plain").status(404));
|
||||
let missing = server.url("/missing.mp3");
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
let (opened, failed) = runtime.block_on(async move {
|
||||
let opened = tokio::task::spawn_blocking(move || stream::open(&url))
|
||||
.await
|
||||
.unwrap();
|
||||
let failed = tokio::task::spawn_blocking(move || stream::open(&missing).is_err())
|
||||
.await
|
||||
.unwrap();
|
||||
(opened, failed)
|
||||
});
|
||||
assert!(failed);
|
||||
let mut reader = opened.unwrap().reader;
|
||||
let read = std::thread::spawn(move || read_to_end(&mut reader))
|
||||
.join()
|
||||
.unwrap();
|
||||
assert_eq!(read, body);
|
||||
drop(runtime);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_fails_for_http_errors() {
|
||||
let (_server, url) = serve(Route::new("gone", "text/plain").status(404));
|
||||
let err = stream::open(&url).err().expect("404 must fail");
|
||||
assert!(format!("{err:#}").contains("404"), "{err:#}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_do_not_show_the_url_query() {
|
||||
let server = TestServer::start();
|
||||
// The test server matches paths with their query, so this is a 404.
|
||||
let err = stream::open(&server.url("/private.mp3?token=s3cr3t"))
|
||||
.err()
|
||||
.expect("404 must fail");
|
||||
let message = format!("{err:#}");
|
||||
assert!(message.contains("404"), "{message}");
|
||||
assert!(!message.contains("s3cr3t"), "{message}");
|
||||
let err = stream::open("file:///etc/passwd").err().expect("not http");
|
||||
assert!(format!("{err:#}").contains("scheme"), "{err:#}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_stream_is_send_and_sync() {
|
||||
fn check<T: Send + Sync + std::io::Read + std::io::Seek>() {}
|
||||
check::<stream::HttpStream>();
|
||||
}
|
||||
|
||||
/// A read blocked on a stalled server (inside the audio callback, in the
|
||||
/// player) returns as soon as its playback is superseded, instead of after
|
||||
/// `STARVE_TIMEOUT`, and the downloader stops; the stream then reports a
|
||||
/// failed end.
|
||||
#[test]
|
||||
fn a_blocked_read_fails_as_soon_as_its_playback_is_superseded() {
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
let body = noise(4 * MIB, 10);
|
||||
let (_server, url) = serve(Route::new(body.clone(), "audio/mpeg").cut(MIB, usize::MAX));
|
||||
let superseded = Arc::new(AtomicBool::new(false));
|
||||
let flag = Arc::clone(&superseded);
|
||||
let opened = stream::open_with(
|
||||
&url,
|
||||
stream::CancelToken::new(move || flag.load(Ordering::SeqCst)),
|
||||
)
|
||||
.unwrap();
|
||||
let probe = opened.probe.clone();
|
||||
let mut reader = opened.reader;
|
||||
let exited = reader.exit_probe();
|
||||
assert_eq!(read_n(&mut reader, MIB), body[..MIB]);
|
||||
assert_eq!(probe.end(), stream::StreamEnd::Complete);
|
||||
|
||||
let reading = std::thread::spawn(move || {
|
||||
let mut buf = [0u8; 1024];
|
||||
let started = Instant::now();
|
||||
let result = reader.read(&mut buf);
|
||||
(result, started.elapsed(), reader)
|
||||
});
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
superseded.store(true, Ordering::SeqCst);
|
||||
let (result, waited, mut reader) = reading.join().unwrap();
|
||||
let err = result.expect_err("superseded");
|
||||
assert!(
|
||||
err.get_ref()
|
||||
.is_some_and(|inner| inner.is::<stream::Cancelled>()),
|
||||
"{err}"
|
||||
);
|
||||
assert!(
|
||||
waited < stream::STARVE_TIMEOUT / 2,
|
||||
"blocked for {waited:?}"
|
||||
);
|
||||
assert_eq!(probe.end(), stream::StreamEnd::Failed);
|
||||
// Later reads fail at once, even from buffered data.
|
||||
reader.seek(SeekFrom::Start(0)).unwrap();
|
||||
assert!(reader.read(&mut [0u8; 16]).is_err());
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while !exited() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
assert!(exited(), "downloader thread did not stop");
|
||||
}
|
||||
+43
-147
@@ -1,8 +1,5 @@
|
||||
//! Minimal HTTP/1.1 server on 127.0.0.1 for podcast tests (no internet).
|
||||
//! One request per connection (`Connection: close`). Besides feeds it serves
|
||||
//! audio like a publisher: `Range` requests (206 / 416) unless disabled,
|
||||
//! optionally without `Content-Length`, and connections cut after a number
|
||||
//! of bytes to simulate outages.
|
||||
//! One request per connection (`Connection: close`).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
@@ -16,20 +13,14 @@ pub(super) struct Route {
|
||||
pub body: Arc<Vec<u8>>,
|
||||
pub content_type: String,
|
||||
pub etag: Option<String>,
|
||||
pub last_modified: Option<String>,
|
||||
/// `Location` of a redirect status.
|
||||
pub location: Option<String>,
|
||||
pub retry_after: Option<String>,
|
||||
/// Honour `Range` requests with 206 responses.
|
||||
pub ranges: bool,
|
||||
/// Send `Content-Length` (and the total in `Content-Range`).
|
||||
/// Send `Content-Length` (otherwise the body is close-delimited).
|
||||
pub send_length: bool,
|
||||
/// Cut the body after this many bytes (absolute file offset)...
|
||||
/// Close the connection once the body reaches this absolute file offset,
|
||||
/// on the first `fail_times` responses (simulates a flaky network).
|
||||
pub cut_after: Option<usize>,
|
||||
/// ...for this many requests; later requests are served in full.
|
||||
pub fail_times: usize,
|
||||
/// Wait this long before answering (a hanging server).
|
||||
pub stall: Option<Duration>,
|
||||
}
|
||||
|
||||
impl Route {
|
||||
@@ -39,47 +30,10 @@ impl Route {
|
||||
body: Arc::new(body.into()),
|
||||
content_type: content_type.to_string(),
|
||||
etag: None,
|
||||
last_modified: None,
|
||||
location: None,
|
||||
retry_after: None,
|
||||
ranges: true,
|
||||
send_length: true,
|
||||
cut_after: None,
|
||||
fail_times: 0,
|
||||
stall: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Answer only after `delay`.
|
||||
pub fn stall(mut self, delay: Duration) -> Route {
|
||||
self.stall = Some(delay);
|
||||
self
|
||||
}
|
||||
|
||||
/// A server that ignores `Range` and always answers 200 with the file.
|
||||
pub fn no_ranges(mut self) -> Route {
|
||||
self.ranges = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// No `Content-Length`: the client cannot know the file size.
|
||||
pub fn no_length(mut self) -> Route {
|
||||
self.send_length = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// The first `times` responses stop after byte `after`.
|
||||
pub fn cut(mut self, after: usize, times: usize) -> Route {
|
||||
self.cut_after = Some(after);
|
||||
self.fail_times = times;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn redirect(status: u16, location: &str) -> Route {
|
||||
Route {
|
||||
status,
|
||||
location: Some(location.to_string()),
|
||||
..Route::new(Vec::new(), "text/plain")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,26 +47,29 @@ impl Route {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn last_modified(mut self, value: &str) -> Route {
|
||||
self.last_modified = Some(value.to_string());
|
||||
pub fn no_ranges(mut self) -> Route {
|
||||
self.ranges = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn retry_after(mut self, value: &str) -> Route {
|
||||
self.retry_after = Some(value.to_string());
|
||||
pub fn no_length(mut self) -> Route {
|
||||
self.send_length = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cut(mut self, after: usize, times: usize) -> Route {
|
||||
self.cut_after = Some(after);
|
||||
self.fail_times = times;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Headers of one received request (names lowercased).
|
||||
pub(super) type RequestHeaders = HashMap<String, String>;
|
||||
|
||||
#[derive(Default)]
|
||||
struct ServerState {
|
||||
routes: HashMap<String, Route>,
|
||||
requests: HashMap<String, Vec<RequestHeaders>>,
|
||||
/// Cut responses served so far, per path.
|
||||
cuts: HashMap<String, usize>,
|
||||
/// Per path: the `Range` header of every request (None if absent).
|
||||
requests: HashMap<String, Vec<Option<String>>>,
|
||||
failures: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
pub(super) struct TestServer {
|
||||
@@ -144,19 +101,10 @@ impl TestServer {
|
||||
self.lock().routes.insert(path.to_string(), route);
|
||||
}
|
||||
|
||||
/// Headers of every request received for `path`, in order.
|
||||
pub fn requests(&self, path: &str) -> Vec<RequestHeaders> {
|
||||
pub fn requests(&self, path: &str) -> Vec<Option<String>> {
|
||||
self.lock().requests.get(path).cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The `Range` header of every request for `path` (`None` if absent).
|
||||
pub fn ranges(&self, path: &str) -> Vec<Option<String>> {
|
||||
self.requests(path)
|
||||
.into_iter()
|
||||
.map(|headers| headers.get("range").cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn lock(&self) -> std::sync::MutexGuard<'_, ServerState> {
|
||||
self.state.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
@@ -177,7 +125,7 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
|
||||
.nth(1)
|
||||
.unwrap_or("/")
|
||||
.to_string();
|
||||
let mut headers = RequestHeaders::new();
|
||||
let mut headers = HashMap::new();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
match reader.read_line(&mut line) {
|
||||
@@ -192,19 +140,20 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
|
||||
headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_string());
|
||||
}
|
||||
}
|
||||
let range_header = headers.get("range").cloned();
|
||||
let (route, cut) = {
|
||||
let mut state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state
|
||||
.requests
|
||||
.entry(path.clone())
|
||||
.or_default()
|
||||
.push(headers.clone());
|
||||
.push(range_header.clone());
|
||||
let route = state.routes.get(&path).cloned();
|
||||
let cut = match &route {
|
||||
Some(route) if route.cut_after.is_some() => {
|
||||
let served = state.cuts.entry(path.clone()).or_default();
|
||||
if *served < route.fail_times {
|
||||
*served += 1;
|
||||
let failures = state.failures.entry(path.clone()).or_default();
|
||||
if *failures < route.fail_times {
|
||||
*failures += 1;
|
||||
route.cut_after
|
||||
} else {
|
||||
None
|
||||
@@ -215,38 +164,30 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
|
||||
(route, cut)
|
||||
};
|
||||
let mut out = stream;
|
||||
if let Some(delay) = route.as_ref().and_then(|route| route.stall) {
|
||||
std::thread::sleep(delay);
|
||||
}
|
||||
let Some(route) = route else {
|
||||
let _ = out
|
||||
.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
|
||||
return;
|
||||
};
|
||||
let etag_matches = route
|
||||
.etag
|
||||
.as_ref()
|
||||
.is_some_and(|etag| headers.get("if-none-match") == Some(etag));
|
||||
let date_matches = route
|
||||
.last_modified
|
||||
.as_ref()
|
||||
.is_some_and(|date| headers.get("if-modified-since") == Some(date));
|
||||
if route.status == 200 && (etag_matches || date_matches) {
|
||||
if let (Some(etag), Some(sent)) = (&route.etag, headers.get("if-none-match"))
|
||||
&& etag == sent
|
||||
{
|
||||
let _ = write!(
|
||||
out,
|
||||
"HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n"
|
||||
"HTTP/1.1 304 Not Modified\r\nETag: {etag}\r\nConnection: close\r\n\r\n"
|
||||
);
|
||||
return;
|
||||
}
|
||||
let total = route.body.len();
|
||||
let mut status = route.status;
|
||||
let (mut start, mut end) = (0usize, total);
|
||||
let mut start = 0usize;
|
||||
let mut end = total;
|
||||
let mut extra = String::new();
|
||||
if route.status == 200
|
||||
&& route.ranges
|
||||
&& let Some(range) = headers
|
||||
.get("range")
|
||||
.and_then(|range| range.strip_prefix("bytes="))
|
||||
&& let Some(range) = range_header
|
||||
.as_deref()
|
||||
.and_then(|r| r.strip_prefix("bytes="))
|
||||
&& let Some((from, to)) = range.split_once('-')
|
||||
&& let Ok(from) = from.trim().parse::<usize>()
|
||||
{
|
||||
@@ -257,53 +198,38 @@ fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
|
||||
);
|
||||
return;
|
||||
}
|
||||
status = 206;
|
||||
start = from;
|
||||
end = to
|
||||
let to = to
|
||||
.trim()
|
||||
.parse::<usize>()
|
||||
.map_or(total, |to| (to + 1).min(total));
|
||||
let size = if route.send_length {
|
||||
total.to_string()
|
||||
} else {
|
||||
"*".to_string()
|
||||
};
|
||||
status = 206;
|
||||
start = from;
|
||||
end = to;
|
||||
extra.push_str(&format!(
|
||||
"Content-Range: bytes {start}-{}/{size}\r\n",
|
||||
"Content-Range: bytes {start}-{}/{total}\r\n",
|
||||
end - 1
|
||||
));
|
||||
}
|
||||
if route.ranges {
|
||||
extra.push_str("Accept-Ranges: bytes\r\n");
|
||||
}
|
||||
if let Some(etag) = &route.etag {
|
||||
extra.push_str(&format!("ETag: {etag}\r\n"));
|
||||
}
|
||||
if route.send_length {
|
||||
extra.push_str(&format!("Content-Length: {}\r\n", end - start));
|
||||
}
|
||||
for (name, value) in [
|
||||
("ETag", &route.etag),
|
||||
("Last-Modified", &route.last_modified),
|
||||
("Location", &route.location),
|
||||
("Retry-After", &route.retry_after),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
extra.push_str(&format!("{name}: {value}\r\n"));
|
||||
}
|
||||
}
|
||||
let reason = match status {
|
||||
200 => "OK",
|
||||
206 => "Partial Content",
|
||||
301 => "Moved Permanently",
|
||||
302 => "Found",
|
||||
307 => "Temporary Redirect",
|
||||
308 => "Permanent Redirect",
|
||||
404 => "Not Found",
|
||||
503 => "Service Unavailable",
|
||||
500 => "Internal Server Error",
|
||||
_ => "Status",
|
||||
};
|
||||
if write!(
|
||||
out,
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: {}\r\n{extra}Connection: close\r\n\r\n",
|
||||
route.content_type,
|
||||
route.content_type
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
@@ -332,33 +258,3 @@ pub(super) fn noise(len: usize, seed: u64) -> Vec<u8> {
|
||||
out.truncate(len);
|
||||
out
|
||||
}
|
||||
|
||||
/// Sample rate of [`wav`].
|
||||
pub(super) const WAV_RATE: u32 = 8_000;
|
||||
|
||||
/// A mono 16-bit PCM WAV file of `seconds` (8 kHz). Every sample of second
|
||||
/// `n` has the value `n * 40` (up to 13 minutes), so a decoded sample tells where playback
|
||||
/// is.
|
||||
pub(super) fn wav(seconds: u32) -> Vec<u8> {
|
||||
const RATE: u32 = WAV_RATE;
|
||||
let samples = RATE * seconds;
|
||||
let data_len = samples * 2;
|
||||
let mut out = Vec::with_capacity(44 + data_len as usize);
|
||||
out.extend_from_slice(b"RIFF");
|
||||
out.extend_from_slice(&(36 + data_len).to_le_bytes());
|
||||
out.extend_from_slice(b"WAVEfmt ");
|
||||
out.extend_from_slice(&16u32.to_le_bytes());
|
||||
out.extend_from_slice(&1u16.to_le_bytes());
|
||||
out.extend_from_slice(&1u16.to_le_bytes());
|
||||
out.extend_from_slice(&RATE.to_le_bytes());
|
||||
out.extend_from_slice(&(RATE * 2).to_le_bytes());
|
||||
out.extend_from_slice(&2u16.to_le_bytes());
|
||||
out.extend_from_slice(&16u16.to_le_bytes());
|
||||
out.extend_from_slice(b"data");
|
||||
out.extend_from_slice(&data_len.to_le_bytes());
|
||||
for n in 0..samples {
|
||||
let second = (n / RATE) as i16;
|
||||
out.extend_from_slice(&second.saturating_mul(40).to_le_bytes());
|
||||
}
|
||||
out
|
||||
}
|
||||
+1577
-951
File diff suppressed because it is too large.
Load diff
@@ -1,11 +1,6 @@
|
||||
use crate::library::models::TrackItem;
|
||||
|
||||
/// Content id of a music track. Podcast episodes have none: they are not
|
||||
/// content-addressed and never shared as `frid://` track links.
|
||||
pub fn track_content_id(track: &TrackItem) -> Option<String> {
|
||||
if track.is_podcast() {
|
||||
return None;
|
||||
}
|
||||
track
|
||||
.fed
|
||||
.as_ref()
|
||||
@@ -19,9 +14,6 @@ pub fn track_can_share(track: &TrackItem) -> bool {
|
||||
}
|
||||
|
||||
pub fn track_share_link(track: &TrackItem) -> Option<String> {
|
||||
if track.is_podcast() {
|
||||
return None;
|
||||
}
|
||||
let content_id = track_content_id(track).or_else(|| {
|
||||
(!track.file_path.trim().is_empty())
|
||||
.then(|| crate::library::audio_content_id(&track.file_path))
|
||||
@@ -162,7 +154,6 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
@@ -240,7 +231,6 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
let link = track_share_link(&track).expect("link");
|
||||
let parsed = parse_frid_link(&link).expect("parses back");
|
||||
|
||||
+17
-254
@@ -51,219 +51,6 @@ mod tests {
|
||||
assert_eq!(label, "●");
|
||||
assert_eq!(style, client_style);
|
||||
}
|
||||
|
||||
fn render(state: &AppState, width: u16, height: u16) -> String {
|
||||
let keymap = Keymap::defaults();
|
||||
let mut terminal =
|
||||
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
|
||||
terminal.draw(|frame| draw(frame, state, &keymap)).unwrap();
|
||||
let buffer = terminal.backend().buffer();
|
||||
(0..height)
|
||||
.map(|y| {
|
||||
(0..width)
|
||||
.map(|x| buffer[(x, y)].symbol())
|
||||
.collect::<String>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_bar_numbers_tabs_in_the_new_order() {
|
||||
let screen = render(&AppState::default(), 120, 10);
|
||||
let bar = screen.lines().next().unwrap();
|
||||
let positions: Vec<usize> = [
|
||||
"1 Library",
|
||||
"2 Playlists",
|
||||
"3 Queue",
|
||||
"4 Podcasts",
|
||||
"5 Settings",
|
||||
"6 Logs",
|
||||
]
|
||||
.iter()
|
||||
.map(|title| {
|
||||
bar.find(title)
|
||||
.unwrap_or_else(|| panic!("{title} in {bar:?}"))
|
||||
})
|
||||
.collect();
|
||||
assert!(positions.is_sorted(), "{bar:?}");
|
||||
}
|
||||
|
||||
/// Each tab's number appears once, in order, and the active tab's label
|
||||
/// is fully on screen.
|
||||
fn assert_tab_bar_fits(width: u16, active: Tab, active_label: &str) {
|
||||
let mut state = AppState::default();
|
||||
state.active_tab = active;
|
||||
let screen = render(&state, width, 10);
|
||||
let bar = screen.lines().next().unwrap();
|
||||
let digits: String = bar.chars().filter(char::is_ascii_digit).collect();
|
||||
assert_eq!(digits, "123456", "width {width}: {bar:?}");
|
||||
assert!(
|
||||
bar.contains(active_label),
|
||||
"width {width}: {active_label} missing in {bar:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_bar_keeps_every_tab_visible_on_narrow_terminals() {
|
||||
for width in [80, 77, 76, 70, 65, 60, 48, 40, 30] {
|
||||
assert_tab_bar_fits(width, Tab::Logs, "6 Logs");
|
||||
assert_tab_bar_fits(width, Tab::Global, "1 Lib");
|
||||
}
|
||||
// Full titles while they fit.
|
||||
assert_tab_bar_fits(80, Tab::Global, "5 Settings");
|
||||
assert_tab_bar_fits(65, Tab::Global, "5 Settings");
|
||||
// Numbers only, but the active tab keeps its full title.
|
||||
assert_tab_bar_fits(30, Tab::Federation, "5 Settings");
|
||||
assert_tab_bar_fits(30, Tab::Playlists, "2 Playlists");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_bar_highlights_active_tab_when_compact() {
|
||||
let mut state = AppState::default();
|
||||
state.active_tab = Tab::Logs;
|
||||
let keymap = Keymap::defaults();
|
||||
let mut terminal =
|
||||
ratatui::Terminal::new(ratatui::backend::TestBackend::new(60, 10)).unwrap();
|
||||
terminal.draw(|frame| draw(frame, &state, &keymap)).unwrap();
|
||||
let buffer = terminal.backend().buffer();
|
||||
let bar: String = (0..60).map(|x| buffer[(x, 0)].symbol()).collect();
|
||||
let x = bar.find("6 Logs").expect("active tab on screen") as u16;
|
||||
let highlight = theme::tab_active_for(&state);
|
||||
let cell = buffer[(x, 0)].style();
|
||||
assert_eq!((cell.fg, cell.bg), (highlight.fg, highlight.bg));
|
||||
}
|
||||
|
||||
use crate::app::podcast_fixtures;
|
||||
|
||||
#[test]
|
||||
fn podcasts_tab_reports_opening_and_failed_store() {
|
||||
let mut state = AppState::default();
|
||||
state.active_tab = Tab::Podcasts;
|
||||
let screen = render(&state, 100, 12);
|
||||
assert!(screen.contains("opening the podcast database"), "{screen}");
|
||||
|
||||
state.podcasts.store = crate::app::state::PodcastStoreStatus::Failed(
|
||||
"podcast database schema v9 is newer than this Furumi supports".into(),
|
||||
);
|
||||
for width in [100, 50] {
|
||||
let screen = render(&state, width, 16);
|
||||
let words: Vec<&str> = screen
|
||||
.split(|c: char| c.is_whitespace() || c == '│')
|
||||
.filter(|word| !word.is_empty())
|
||||
.collect();
|
||||
for word in ["unavailable", "v9", "newer", "unchanged."] {
|
||||
assert!(words.contains(&word), "{word} at {width}:\n{screen}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcasts_list_shows_actions_counts_and_errors() {
|
||||
let state = podcast_fixtures::ready_state(Vec::new());
|
||||
let screen = render(&state, 100, 14);
|
||||
assert!(screen.contains("+ Add podcast (RSS feed URL)"), "{screen}");
|
||||
assert!(screen.contains("Check all podcasts"), "{screen}");
|
||||
assert!(screen.contains("No subscriptions yet"), "{screen}");
|
||||
|
||||
let mut row = podcast_fixtures::row("Test Show", 2, 3, 0);
|
||||
row.fetch.last_error = Some("HTTP 503 Service Unavailable".into());
|
||||
row.fetch.consecutive_failures = 2;
|
||||
let mut state = podcast_fixtures::ready_state(vec![row]);
|
||||
state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS;
|
||||
// Wide: side panel with details; narrow: status below the list.
|
||||
for width in [110, 60] {
|
||||
let screen = render(&state, width, 30);
|
||||
assert!(screen.contains("! Test Show"), "{screen}");
|
||||
assert!(screen.contains("2 unplayed · 3 ep"), "{screen}");
|
||||
assert!(screen.contains("last check failed"), "{screen}");
|
||||
assert!(screen.contains("503"), "{screen}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_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) {
|
||||
@@ -287,9 +74,9 @@ pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
|
||||
Tab::Global => global::draw(frame, main_area, state),
|
||||
Tab::Playlists => playlists::draw(frame, main_area, state),
|
||||
Tab::Queue => draw_queue(frame, main_area, state),
|
||||
Tab::Podcasts => podcasts::draw(frame, main_area, state),
|
||||
Tab::Federation => federation::draw(frame, main_area, state),
|
||||
Tab::Logs => logs::draw(frame, main_area, state),
|
||||
Tab::Podcasts => podcasts::draw(frame, main_area, state),
|
||||
}
|
||||
draw_status(frame, status_area, state);
|
||||
|
||||
@@ -355,48 +142,24 @@ fn is_waiting_message(message: &str) -> bool {
|
||||
}
|
||||
|
||||
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
frame.render_widget(tab_bar(state, area.width), area);
|
||||
}
|
||||
|
||||
/// The widest tab bar that fits `width`. Narrow terminals drop the outer
|
||||
/// padding, then abbreviate titles, then show only numbers (keeping the
|
||||
/// active tab's full title), so all six shortcuts and the highlight stay
|
||||
/// visible instead of the last tabs being cut off.
|
||||
fn tab_bar(state: &AppState, width: u16) -> Tabs<'static> {
|
||||
use unicode_width::UnicodeWidthStr as _;
|
||||
|
||||
let active = state.active_tab;
|
||||
let build = |label: &dyn Fn(Tab) -> String, padded: bool| {
|
||||
let tabs = Tabs::new(Tab::ALL.iter().map(|&tab| label(tab)))
|
||||
.select(active.index())
|
||||
.style(theme::dim())
|
||||
.highlight_style(theme::tab_active_for(state))
|
||||
.divider("");
|
||||
if padded { tabs } else { tabs.padding("", "") }
|
||||
};
|
||||
let full = |tab: Tab| format!(" {} {} ", tab.index() + 1, tab.title());
|
||||
let short = |tab: Tab| format!(" {} {} ", tab.index() + 1, tab.short_title());
|
||||
let numbers_with_active = |tab: Tab| {
|
||||
if tab == active {
|
||||
full(tab)
|
||||
} else {
|
||||
// Titles carry their own spacing. When all of them do not fit, inactive
|
||||
// tabs collapse to their number so the active one is never cut off.
|
||||
let full: usize = Tab::ALL.iter().map(|tab| tab.title().len() + 4).sum();
|
||||
let compact = full > usize::from(area.width);
|
||||
let titles = Tab::ALL.iter().map(|tab| {
|
||||
if compact && *tab != state.active_tab {
|
||||
format!(" {} ", tab.index() + 1)
|
||||
} else {
|
||||
format!(" {} {} ", tab.index() + 1, tab.title())
|
||||
}
|
||||
};
|
||||
let numbers = |tab: Tab| format!(" {} ", tab.index() + 1);
|
||||
let candidates: [(&dyn Fn(Tab) -> String, bool); 4] = [
|
||||
(&full, true),
|
||||
(&full, false),
|
||||
(&short, false),
|
||||
(&numbers_with_active, false),
|
||||
];
|
||||
for (label, padded) in candidates {
|
||||
let tabs = build(label, padded);
|
||||
if tabs.width() <= usize::from(width) {
|
||||
return tabs;
|
||||
}
|
||||
}
|
||||
build(&numbers, false)
|
||||
});
|
||||
let tabs = Tabs::new(titles)
|
||||
.select(state.active_tab.index())
|
||||
.style(theme::dim())
|
||||
.highlight_style(theme::tab_active_for(state))
|
||||
.padding("", "")
|
||||
.divider("");
|
||||
frame.render_widget(tabs, area);
|
||||
}
|
||||
|
||||
/// One track row used by every track list: ♥ marker for liked tracks, the
|
||||
|
||||
+201
-476
@@ -1,9 +1,6 @@
|
||||
//! Podcasts tab: subscriptions with their check status, a podcast's
|
||||
//! seasons, and its episodes with saved position, played state, duration
|
||||
//! and date. Rendering only reads `AppState`.
|
||||
//! Podcasts tab: subscriptions with their cover, and the episode catalog of
|
||||
//! one podcast with title search, date sort and the selected episode's notes.
|
||||
|
||||
use furumi_library::podcasts::{EpisodeListItem, SeasonSummary};
|
||||
use music_dht::podcast::ProgressStatus;
|
||||
use ratatui::Frame;
|
||||
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
@@ -13,54 +10,18 @@ use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::{loading_line, theme};
|
||||
use crate::app::state::{
|
||||
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow, PodcastRow,
|
||||
PodcastStoreStatus, SeasonRow,
|
||||
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow,
|
||||
};
|
||||
use crate::app::update::podcasts::{played_label, sort_label};
|
||||
use crate::podcasts::{Episode, Podcast};
|
||||
|
||||
/// Width below which the side panel with the cover is dropped.
|
||||
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80;
|
||||
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 72;
|
||||
const SIDE_PANEL_WIDTH: u16 = ART_HEADER_WIDTH + 2;
|
||||
|
||||
pub(crate) const EMPTY_HINT: &str =
|
||||
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.";
|
||||
pub(crate) const STORE_FAILED_NOTE: &str = "The podcast database was left unchanged. \
|
||||
Music, playlists and sync keep working.";
|
||||
|
||||
pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
match &tab.store {
|
||||
PodcastStoreStatus::Opening => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
return centered_line(
|
||||
frame,
|
||||
inner,
|
||||
loading_line(state, "opening the podcast database…"),
|
||||
);
|
||||
}
|
||||
PodcastStoreStatus::Failed(err) => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
let lines = vec![
|
||||
Line::styled("Podcasts are unavailable", theme::header_for(state)),
|
||||
Line::default(),
|
||||
Line::styled(err.clone(), error_style()),
|
||||
Line::default(),
|
||||
Line::styled(STORE_FAILED_NOTE, theme::dim()),
|
||||
];
|
||||
frame.render_widget(
|
||||
Paragraph::new(lines)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
return;
|
||||
}
|
||||
PodcastStoreStatus::Ready => {}
|
||||
}
|
||||
match &tab.opened {
|
||||
match state.podcasts.opened {
|
||||
Some(opened) => draw_episodes(frame, area, state, opened.id, opened.cursor),
|
||||
None => draw_list(frame, area, state),
|
||||
Some(opened) if opened.season.is_none() => draw_seasons(frame, area, state),
|
||||
Some(_) => draw_episodes(frame, area, state),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,48 +51,6 @@ fn error_style() -> Style {
|
||||
Style::new().fg(Color::Red)
|
||||
}
|
||||
|
||||
/// First row so that `cursor` stays visible, roughly centered.
|
||||
fn first_visible(cursor: usize, len: usize, visible: usize) -> usize {
|
||||
cursor
|
||||
.saturating_sub(visible / 2)
|
||||
.min(len.saturating_sub(visible))
|
||||
}
|
||||
|
||||
fn row_area(area: Rect, offset: usize) -> Rect {
|
||||
Rect {
|
||||
y: area.y + offset as u16,
|
||||
height: 1,
|
||||
..area
|
||||
}
|
||||
}
|
||||
|
||||
/// A row with a left part and a right-aligned dim part.
|
||||
fn draw_row(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &AppState,
|
||||
left: Line<'static>,
|
||||
right: Option<String>,
|
||||
selected: bool,
|
||||
) {
|
||||
frame.render_widget(Paragraph::new(left), area);
|
||||
if let Some(right) = right {
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(format!("{right} "), theme::dim()))
|
||||
.alignment(Alignment::Right),
|
||||
area,
|
||||
);
|
||||
}
|
||||
if selected {
|
||||
frame
|
||||
.buffer_mut()
|
||||
.set_style(area, theme::tab_active_for(state));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subscriptions
|
||||
|
||||
fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let inner = bordered(frame, area, state, " Podcasts ".to_string());
|
||||
@@ -144,47 +63,60 @@ fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
}
|
||||
Some(Loadable::Ready(_)) => {}
|
||||
}
|
||||
|
||||
let (list_area, side_area) = split_side_panel(inner);
|
||||
if let Some(side) = side_area {
|
||||
draw_side_panel(frame, side, state, tab.current_podcast());
|
||||
}
|
||||
|
||||
let rows = tab.list_rows();
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(tab.selected, rows.len(), visible);
|
||||
let first = tab
|
||||
.selected
|
||||
.saturating_sub(visible / 2)
|
||||
.min(rows.len().saturating_sub(visible));
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (left, right) = list_row(state, *row, list_area.width);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
state,
|
||||
left,
|
||||
right,
|
||||
index == tab.selected,
|
||||
);
|
||||
let area = Rect {
|
||||
x: list_area.x,
|
||||
y: list_area.y + (index - first) as u16,
|
||||
width: list_area.width,
|
||||
height: 1,
|
||||
};
|
||||
let (left, right) = list_row_lines(state, *row, list_area.width);
|
||||
frame.render_widget(Paragraph::new(left), area);
|
||||
if let Some(right) = right {
|
||||
frame.render_widget(Paragraph::new(right).alignment(Alignment::Right), area);
|
||||
}
|
||||
if index == tab.selected {
|
||||
frame
|
||||
.buffer_mut()
|
||||
.set_style(area, theme::tab_active_for(state));
|
||||
}
|
||||
}
|
||||
let used = rows.len().saturating_sub(first).min(visible) as u16;
|
||||
let below = Rect {
|
||||
y: list_area.y + used + 1,
|
||||
height: list_area.height.saturating_sub(used + 1),
|
||||
..list_area
|
||||
};
|
||||
if below.height == 0 {
|
||||
return;
|
||||
if tab.podcasts().is_empty() {
|
||||
let hint_y = list_area.y + rows.len() as u16 + 1;
|
||||
if hint_y < list_area.y + list_area.height {
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(
|
||||
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.",
|
||||
theme::dim(),
|
||||
))
|
||||
.wrap(Wrap { trim: true }),
|
||||
Rect {
|
||||
y: hint_y,
|
||||
height: list_area.y + list_area.height - hint_y,
|
||||
..list_area
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
let hint = if tab.podcasts().is_empty() {
|
||||
Line::styled(EMPTY_HINT, theme::dim())
|
||||
} else if side_area.is_none()
|
||||
&& let Some(row) = tab.current_podcast()
|
||||
{
|
||||
// Narrow terminal: the selected podcast's status below the list.
|
||||
fetch_status_line(state, row)
|
||||
} else {
|
||||
Line::default()
|
||||
};
|
||||
frame.render_widget(Paragraph::new(hint).wrap(Wrap { trim: true }), below);
|
||||
}
|
||||
|
||||
fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>, Option<String>) {
|
||||
fn list_row_lines(
|
||||
state: &AppState,
|
||||
row: PodcastListRow,
|
||||
width: u16,
|
||||
) -> (Line<'static>, Option<Line<'static>>) {
|
||||
let tab = &state.podcasts;
|
||||
match row {
|
||||
PodcastListRow::Add => {
|
||||
@@ -196,14 +128,15 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
PodcastListRow::RefreshAll => {
|
||||
let label = if tab.refreshing.is_empty() {
|
||||
"↻ Check all podcasts for new episodes".to_string()
|
||||
} else {
|
||||
let busy = !tab.refreshing.is_empty();
|
||||
let label = if busy {
|
||||
format!(
|
||||
"{} Checking {} feed(s) for new episodes…",
|
||||
state.spinner(),
|
||||
tab.refreshing.len()
|
||||
)
|
||||
} else {
|
||||
"↻ Check all podcasts for new episodes".to_string()
|
||||
};
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
@@ -211,28 +144,26 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>
|
||||
let Some(podcast) = tab.podcasts().get(index) else {
|
||||
return (Line::raw(""), None);
|
||||
};
|
||||
let marker = if tab.refreshing.contains(podcast.id()) {
|
||||
let marker = if tab.refreshing.contains(&podcast.id) {
|
||||
Span::styled(format!("{} ", state.spinner()), theme::accent_for(state))
|
||||
} else if podcast.fetch.last_error.is_some() {
|
||||
} else if podcast.last_error.is_some() {
|
||||
Span::styled("! ", error_style())
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
let summary = &podcast.summary;
|
||||
let meta = if summary.unplayed_count > 0 {
|
||||
format!(
|
||||
"{} unplayed · {} ep",
|
||||
summary.unplayed_count, summary.episode_count
|
||||
)
|
||||
} else {
|
||||
format!("{} ep", summary.episode_count)
|
||||
};
|
||||
let title_width = usize::from(width).saturating_sub(2 + meta.width() + 2);
|
||||
let mut meta = format!("{} ep", podcast.episode_count);
|
||||
if let Some(latest) = podcast.latest_published_at {
|
||||
meta.push_str(&format!(" · {}", format_date(latest)));
|
||||
}
|
||||
meta.push(' ');
|
||||
// Marker (2), the title, one gap column, then the meta.
|
||||
let title_width =
|
||||
usize::from(width).saturating_sub(2 + UnicodeWidthStr::width(meta.as_str()) + 1);
|
||||
let left = Line::from(vec![
|
||||
marker,
|
||||
Span::raw(fit_width(podcast.title(), title_width)),
|
||||
Span::raw(fit_width(&podcast.title, title_width)),
|
||||
]);
|
||||
(left, Some(meta))
|
||||
(left, Some(Line::styled(meta, theme::dim())))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,7 +178,7 @@ fn split_side_panel(area: Rect) -> (Rect, Option<Rect>) {
|
||||
(main, Some(side))
|
||||
}
|
||||
|
||||
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&PodcastRow>) {
|
||||
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&Podcast>) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::LEFT)
|
||||
.border_style(theme::border_for(state));
|
||||
@@ -280,53 +211,42 @@ fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Opt
|
||||
if text_area.height == 0 {
|
||||
return;
|
||||
}
|
||||
let summary = &podcast.summary;
|
||||
let mut lines = vec![Line::styled(
|
||||
podcast.title().to_string(),
|
||||
podcast.title.clone(),
|
||||
Style::new().add_modifier(Modifier::BOLD),
|
||||
)];
|
||||
if let Some(author) = &summary.meta.author {
|
||||
if let Some(author) = &podcast.author {
|
||||
lines.push(Line::styled(author.clone(), theme::dim()));
|
||||
}
|
||||
let mut counts = format!("{} episodes", summary.episode_count);
|
||||
if summary.season_count > 0 {
|
||||
counts.push_str(&format!(" · {} seasons", summary.season_count));
|
||||
}
|
||||
lines.push(Line::styled(counts, theme::dim()));
|
||||
lines.push(Line::styled("private", theme::dim()));
|
||||
lines.push(fetch_status_line(state, podcast));
|
||||
if let Some(description) = &summary.meta.description {
|
||||
lines.push(Line::styled(
|
||||
format!("{} episodes", podcast.episode_count),
|
||||
theme::dim(),
|
||||
));
|
||||
lines.extend(check_status_line(state, podcast));
|
||||
if let Some(description) = &podcast.description {
|
||||
lines.push(Line::raw(""));
|
||||
lines.push(Line::raw(description.clone()));
|
||||
}
|
||||
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area);
|
||||
}
|
||||
|
||||
/// 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…");
|
||||
fn check_status_line(state: &AppState, podcast: &Podcast) -> Option<Line<'static>> {
|
||||
if state.podcasts.refreshing.contains(&podcast.id) {
|
||||
return Some(loading_line(state, "checking for new episodes…"));
|
||||
}
|
||||
let 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()),
|
||||
if let Some(error) = &podcast.last_error {
|
||||
return Some(Line::styled(
|
||||
format!("last check failed: {error}"),
|
||||
error_style(),
|
||||
));
|
||||
}
|
||||
podcast
|
||||
.last_checked_at
|
||||
.map(|at| Line::styled(format!("checked {}", format_date(at)), theme::dim()))
|
||||
}
|
||||
|
||||
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &PodcastRow) {
|
||||
let art = podcast.fetch.cover_path.as_ref().and_then(|path| {
|
||||
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &Podcast) {
|
||||
let art = podcast.cover_path.as_ref().and_then(|path| {
|
||||
state.art.get(&crate::art::cache_key(
|
||||
path,
|
||||
ART_HEADER_WIDTH,
|
||||
@@ -342,220 +262,97 @@ fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &Podcast
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Seasons
|
||||
|
||||
fn draw_seasons(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor: usize) {
|
||||
let tab = &state.podcasts;
|
||||
let podcast = tab.current_podcast();
|
||||
let title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let seasons = match &tab.seasons {
|
||||
Some((_, Loadable::Failed(error))) => {
|
||||
let podcast = tab.podcast(id);
|
||||
let title = podcast.map_or("…".to_string(), |podcast| podcast.title.clone());
|
||||
let mut header = format!(" Podcasts ▸ {title} · {} ", tab.sort.label());
|
||||
if !tab.filter.is_empty() {
|
||||
header.push_str(&format!("· search: \"{}\" ", tab.filter));
|
||||
}
|
||||
let inner = bordered(frame, area, state, header);
|
||||
|
||||
let episodes = match &tab.episodes {
|
||||
Some((loaded, Loadable::Ready(episodes))) if *loaded == id => episodes,
|
||||
Some((loaded, Loadable::Failed(error))) if *loaded == id => {
|
||||
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
_ => match tab.opened_seasons() {
|
||||
Some(seasons) => seasons,
|
||||
None => return centered_line(frame, inner, loading_line(state, "loading seasons…")),
|
||||
},
|
||||
_ => return centered_line(frame, inner, loading_line(state, "loading episodes…")),
|
||||
};
|
||||
let visible_indices = tab.visible_episode_indices();
|
||||
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let cursor = tab
|
||||
.opened
|
||||
.as_ref()
|
||||
.and_then(|opened| opened.season_cursor)
|
||||
.unwrap_or(0);
|
||||
let rows = tab.season_rows();
|
||||
let visible = usize::from(main.height.max(1));
|
||||
let first = first_visible(cursor, rows.len(), visible);
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (label, meta) = match row {
|
||||
SeasonRow::All => {
|
||||
let (episodes, unplayed) = seasons.iter().fold((0, 0), |(e, u), season| {
|
||||
(e + season.episode_count, u + season.unplayed_count)
|
||||
});
|
||||
("All episodes".to_string(), counts(episodes, unplayed))
|
||||
}
|
||||
SeasonRow::Group(index) => {
|
||||
let Some(season) = seasons.get(*index) else {
|
||||
continue;
|
||||
};
|
||||
(season_title(season), season_meta(season))
|
||||
}
|
||||
};
|
||||
let width = usize::from(main.width).saturating_sub(meta.width() + 4);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(main, index - first),
|
||||
state,
|
||||
Line::raw(format!(" {}", fit_width(&label, width))),
|
||||
Some(meta),
|
||||
index == cursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
// Episode notes below the list when there is room for them.
|
||||
let notes_height = if main.height >= 16 { 6 } else { 0 };
|
||||
let [list_area, notes_area] =
|
||||
Layout::vertical([Constraint::Min(1), Constraint::Length(notes_height)]).areas(main);
|
||||
let [summary_area, list_area] =
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(list_area);
|
||||
|
||||
fn season_title(season: &SeasonSummary) -> String {
|
||||
match season.season {
|
||||
Some(number) => format!("Season {number}"),
|
||||
None => "Without a season".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn counts(episodes: u64, unplayed: u64) -> String {
|
||||
if unplayed > 0 {
|
||||
format!("{unplayed} unplayed · {episodes} ep")
|
||||
let summary = if tab.filter.is_empty() {
|
||||
format!("{} episodes", episodes.len())
|
||||
} else {
|
||||
format!("{episodes} ep")
|
||||
}
|
||||
}
|
||||
|
||||
fn season_meta(season: &SeasonSummary) -> String {
|
||||
let mut meta = counts(season.episode_count, season.unplayed_count);
|
||||
let years = (
|
||||
season.first_published_at_ms.map(year_of),
|
||||
season.last_published_at_ms.map(year_of),
|
||||
);
|
||||
match years {
|
||||
(Some(first), Some(last)) if first != last => meta.push_str(&format!(" · {first}–{last}")),
|
||||
(Some(year), _) | (None, Some(year)) => meta.push_str(&format!(" · {year}")),
|
||||
(None, None) => {}
|
||||
}
|
||||
meta
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episodes
|
||||
|
||||
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let Some(opened) = &tab.opened else {
|
||||
return;
|
||||
format!(
|
||||
"{} of {} episodes match \"{}\"",
|
||||
visible_indices.len(),
|
||||
episodes.len(),
|
||||
tab.filter
|
||||
)
|
||||
};
|
||||
let podcast = tab.current_podcast();
|
||||
let mut title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
if opened.season_cursor.is_some() {
|
||||
let season = match opened.season {
|
||||
Some(furumi_library::podcasts::SeasonFilter::Season(number)) => {
|
||||
format!("Season {number}")
|
||||
}
|
||||
Some(furumi_library::podcasts::SeasonFilter::NoSeason) => {
|
||||
"Without a season".to_string()
|
||||
}
|
||||
_ => "All episodes".to_string(),
|
||||
};
|
||||
title = format!("{title} ▸ {season}");
|
||||
let mut summary_spans = vec![Span::styled(summary, theme::dim())];
|
||||
if tab.refreshing.contains(&id) {
|
||||
summary_spans.push(Span::raw(" "));
|
||||
summary_spans.extend(loading_line(state, "checking for new episodes…").spans);
|
||||
}
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let Some(list) = tab.opened_episodes() else {
|
||||
return centered_line(frame, inner, loading_line(state, "loading episodes…"));
|
||||
};
|
||||
frame.render_widget(Paragraph::new(Line::from(summary_spans)), summary_area);
|
||||
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let notes_height = if main.height >= 18 { 6 } else { 0 };
|
||||
let [summary_area, list_area, notes_area] = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(notes_height),
|
||||
])
|
||||
.areas(main);
|
||||
|
||||
let mut summary = vec![Span::styled(
|
||||
episode_summary(state, list.items.len(), list.complete),
|
||||
theme::dim(),
|
||||
)];
|
||||
if list.loading || list.loading_more {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "loading…").spans);
|
||||
} else if podcast.is_some_and(|row| tab.refreshing.contains(row.id())) {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "checking for new episodes…").spans);
|
||||
}
|
||||
frame.render_widget(Paragraph::new(Line::from(summary)), summary_area);
|
||||
|
||||
if let Some(error) = &list.error {
|
||||
return centered_line(frame, list_area, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
if list.items.is_empty() {
|
||||
if list.loading {
|
||||
return;
|
||||
}
|
||||
let message = if tab.search.trim().is_empty()
|
||||
&& tab.played == furumi_library::podcasts::PlayedFilter::Any
|
||||
{
|
||||
"this feed has no playable episodes yet"
|
||||
if visible_indices.is_empty() {
|
||||
let message = if episodes.is_empty() {
|
||||
"this feed has no playable episodes"
|
||||
} else {
|
||||
"no episode matches the search or filter"
|
||||
"no episode title matches the search"
|
||||
};
|
||||
return centered_line(frame, list_area, Line::styled(message, theme::dim()));
|
||||
}
|
||||
|
||||
let cursor = opened.episode_cursor.min(list.items.len() - 1);
|
||||
let cursor = cursor.min(visible_indices.len() - 1);
|
||||
let playing_id = state
|
||||
.player
|
||||
.current
|
||||
.as_ref()
|
||||
.and_then(|track| crate::podcasts::episode_id_from_track_id(track.id));
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(cursor, list.items.len(), visible);
|
||||
for (index, item) in list.items.iter().enumerate().skip(first).take(visible) {
|
||||
let first = cursor
|
||||
.saturating_sub(visible / 2)
|
||||
.min(visible_indices.len().saturating_sub(visible));
|
||||
for (row_index, &episode_index) in visible_indices.iter().enumerate().skip(first).take(visible)
|
||||
{
|
||||
let episode = &episodes[episode_index];
|
||||
let area = Rect {
|
||||
x: list_area.x,
|
||||
y: list_area.y + (row_index - first) as u16,
|
||||
width: list_area.width,
|
||||
height: 1,
|
||||
};
|
||||
draw_episode_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
area,
|
||||
state,
|
||||
item,
|
||||
index == cursor,
|
||||
episode,
|
||||
playing_id == Some(episode.id),
|
||||
row_index == cursor,
|
||||
);
|
||||
}
|
||||
|
||||
if notes_height > 0
|
||||
&& let Some(item) = list.items.get(cursor)
|
||||
&& let Some(episode) = visible_indices
|
||||
.get(cursor)
|
||||
.and_then(|&index| episodes.get(index))
|
||||
{
|
||||
draw_notes(frame, notes_area, state, item);
|
||||
}
|
||||
}
|
||||
|
||||
fn episode_summary(state: &AppState, loaded: usize, complete: bool) -> String {
|
||||
let tab = &state.podcasts;
|
||||
let count = if complete {
|
||||
format!("{loaded} episodes")
|
||||
} else {
|
||||
format!("{loaded}+ episodes")
|
||||
};
|
||||
let mut parts = vec![
|
||||
count,
|
||||
played_label(tab.played).to_string(),
|
||||
sort_label(tab.sort).to_string(),
|
||||
];
|
||||
let search = tab.search.trim();
|
||||
if !search.is_empty() {
|
||||
parts.push(format!("search: \"{search}\""));
|
||||
}
|
||||
parts.join(" · ")
|
||||
}
|
||||
|
||||
/// Marker and right-hand text for an episode's listening state.
|
||||
pub(crate) fn episode_state(item: &EpisodeListItem) -> (&'static str, String) {
|
||||
let duration = item.episode.duration_ms.filter(|ms| *ms > 0);
|
||||
match item.status() {
|
||||
ProgressStatus::Completed => (
|
||||
"✓",
|
||||
match duration {
|
||||
Some(ms) => format!("played · {}", format_duration_ms(ms)),
|
||||
None => "played".to_string(),
|
||||
},
|
||||
),
|
||||
ProgressStatus::InProgress => {
|
||||
let position = format_duration_ms(item.resume_position_ms());
|
||||
(
|
||||
"◐",
|
||||
match duration {
|
||||
Some(ms) => format!("{position} / {}", format_duration_ms(ms)),
|
||||
None => format!("at {position}"),
|
||||
},
|
||||
)
|
||||
}
|
||||
ProgressStatus::NotStarted => ("●", duration.map(format_duration_ms).unwrap_or_default()),
|
||||
draw_notes(frame, notes_area, state, episode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -563,98 +360,65 @@ fn draw_episode_row(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &AppState,
|
||||
item: &EpisodeListItem,
|
||||
episode: &Episode,
|
||||
playing: bool,
|
||||
selected: bool,
|
||||
) {
|
||||
let episode = &item.episode;
|
||||
let date = episode
|
||||
.published_at_ms
|
||||
.map(|ms| format_date(ms.div_euclid(1000)))
|
||||
.unwrap_or_else(|| " ".repeat(10));
|
||||
let (mut marker, mut right) = episode_state(item);
|
||||
let mut marker_style = match item.status() {
|
||||
ProgressStatus::NotStarted => theme::accent_for(state),
|
||||
_ => theme::dim(),
|
||||
};
|
||||
// The episode playing on this device shows its live position.
|
||||
if let Some(position_ms) = playing_position_ms(state, &episode.episode_id) {
|
||||
marker = "▶";
|
||||
marker_style = theme::accent_for(state);
|
||||
let position = format_duration_ms(position_ms);
|
||||
right = match episode.duration_ms.filter(|ms| *ms > 0) {
|
||||
Some(ms) => format!("{position} / {}", format_duration_ms(ms)),
|
||||
None => position,
|
||||
};
|
||||
}
|
||||
let number = match (episode.season, episode.episode_number) {
|
||||
(Some(season), Some(number)) => format!("S{season}E{number} "),
|
||||
(None, Some(number)) => format!("#{number} "),
|
||||
_ => String::new(),
|
||||
};
|
||||
let mut title = episode.title.clone();
|
||||
if !item.in_feed {
|
||||
title.push_str(" (no longer in the feed)");
|
||||
}
|
||||
let title_width =
|
||||
usize::from(area.width).saturating_sub(2 + 10 + 2 + number.width() + right.width() + 2);
|
||||
let title_style = match item.status() {
|
||||
ProgressStatus::Completed => theme::dim(),
|
||||
_ => Style::default(),
|
||||
.published_at
|
||||
.map(format_date)
|
||||
.unwrap_or_else(|| " ".to_string());
|
||||
let marker = if playing {
|
||||
Span::styled("▶ ", theme::accent_for(state))
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
let duration = episode
|
||||
.duration_seconds
|
||||
.map(format_duration)
|
||||
.unwrap_or_default();
|
||||
let title_width = usize::from(area.width)
|
||||
.saturating_sub(2 + 10 + 2 + UnicodeWidthStr::width(duration.as_str()) + 2);
|
||||
let line = Line::from(vec![
|
||||
Span::styled(format!("{marker} "), marker_style),
|
||||
marker,
|
||||
Span::styled(date, theme::dim()),
|
||||
Span::raw(" "),
|
||||
Span::styled(number, theme::dim()),
|
||||
Span::styled(fit_width(&title, title_width), title_style),
|
||||
Span::raw(fit_width(&episode.title, title_width)),
|
||||
]);
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
line,
|
||||
(!right.is_empty()).then_some(right),
|
||||
selected,
|
||||
);
|
||||
frame.render_widget(Paragraph::new(line), area);
|
||||
if !duration.is_empty() {
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(format!("{duration} "), theme::dim()))
|
||||
.alignment(Alignment::Right),
|
||||
area,
|
||||
);
|
||||
}
|
||||
if selected {
|
||||
frame
|
||||
.buffer_mut()
|
||||
.set_style(area, theme::tab_active_for(state));
|
||||
}
|
||||
}
|
||||
|
||||
/// Position of `episode_id` when it is the item playing (or paused) here.
|
||||
fn playing_position_ms(
|
||||
state: &AppState,
|
||||
episode_id: &music_dht::podcast::EpisodeId,
|
||||
) -> Option<i64> {
|
||||
let player = &state.player;
|
||||
let current = player.current.as_ref()?.podcast_episode_ref()?;
|
||||
(player.playing && current.episode_id == *episode_id)
|
||||
.then(|| (player.position_secs.max(0.0) * 1_000.0).round() as i64)
|
||||
}
|
||||
|
||||
fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, item: &EpisodeListItem) {
|
||||
fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, episode: &Episode) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::TOP)
|
||||
.border_style(theme::border_for(state))
|
||||
.title(Span::styled(" Episode notes ", theme::dim()));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
let mut text = item
|
||||
.episode
|
||||
let text = episode
|
||||
.description
|
||||
.clone()
|
||||
.unwrap_or_else(|| "No description in the feed.".to_string());
|
||||
if item.completed_count > 1 {
|
||||
text = format!("Listened {} times. {text}", item.completed_count);
|
||||
}
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::raw(text)).wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting
|
||||
|
||||
/// Truncate to a display width, adding an ellipsis when cut.
|
||||
pub(crate) fn fit_width(text: &str, width: usize) -> String {
|
||||
fn fit_width(text: &str, width: usize) -> String {
|
||||
if UnicodeWidthStr::width(text) <= width {
|
||||
return text.to_string();
|
||||
}
|
||||
@@ -675,8 +439,8 @@ pub(crate) fn fit_width(text: &str, width: usize) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn format_duration_ms(ms: i64) -> String {
|
||||
let total = (ms.max(0) + 500) / 1000;
|
||||
fn format_duration(seconds: f64) -> String {
|
||||
let total = seconds.max(0.0).round() as u64;
|
||||
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
|
||||
if hours > 0 {
|
||||
format!("{hours}:{minutes:02}:{secs:02}")
|
||||
@@ -685,17 +449,9 @@ pub(crate) fn format_duration_ms(ms: i64) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// `YYYY-MM-DD` (UTC) for unix seconds: Howard Hinnant's days-to-civil.
|
||||
pub(crate) fn format_date(unix_seconds: i64) -> String {
|
||||
let (year, month, day) = civil(unix_seconds);
|
||||
format!("{year:04}-{month:02}-{day:02}")
|
||||
}
|
||||
|
||||
fn year_of(unix_ms: i64) -> i64 {
|
||||
civil(unix_ms.div_euclid(1000)).0
|
||||
}
|
||||
|
||||
fn civil(unix_seconds: i64) -> (i64, i64, i64) {
|
||||
/// `YYYY-MM-DD` (UTC) for unix seconds, without a date library: Howard
|
||||
/// Hinnant's days-to-civil algorithm.
|
||||
fn format_date(unix_seconds: i64) -> String {
|
||||
let days = unix_seconds.div_euclid(86_400);
|
||||
let z = days + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
@@ -706,38 +462,7 @@ fn civil(unix_seconds: i64) -> (i64, i64, i64) {
|
||||
let day = doy - (153 * mp + 2) / 5 + 1;
|
||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let year = yoe + era * 400 + i64::from(month <= 2);
|
||||
(year, month, day)
|
||||
}
|
||||
|
||||
/// "5 min ago", "in 2 h", or a date for anything further away.
|
||||
fn relative_time(at_ms: i64) -> String {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as i64)
|
||||
.unwrap_or(0);
|
||||
relative_to(at_ms, now)
|
||||
}
|
||||
|
||||
pub(crate) fn relative_to(at_ms: i64, now_ms: i64) -> String {
|
||||
let delta = (now_ms - at_ms) / 1000;
|
||||
let (amount, future) = (delta.unsigned_abs(), delta < 0);
|
||||
let text = match amount {
|
||||
0..60 => {
|
||||
return if future {
|
||||
"soon".into()
|
||||
} else {
|
||||
"just now".into()
|
||||
};
|
||||
}
|
||||
60..3_600 => format!("{} min", amount / 60),
|
||||
3_600..86_400 => format!("{} h", amount / 3_600),
|
||||
_ => return format_date(at_ms.div_euclid(1000)),
|
||||
};
|
||||
if future {
|
||||
format!("in {text}")
|
||||
} else {
|
||||
format!("{text} ago")
|
||||
}
|
||||
format!("{year:04}-{month:02}-{day:02}")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -745,18 +470,18 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn formats_dates_durations_and_relative_times() {
|
||||
fn formats_dates_in_utc() {
|
||||
assert_eq!(format_date(0), "1970-01-01");
|
||||
assert_eq!(format_date(951_782_400), "2000-02-29");
|
||||
assert_eq!(format_date(1_718_409_600), "2024-06-15");
|
||||
assert_eq!(format_date(-86_400), "1969-12-31");
|
||||
assert_eq!(format_duration_ms(59_600), "1:00");
|
||||
assert_eq!(format_duration_ms(754_000), "12:34");
|
||||
assert_eq!(format_duration_ms(3_723_000), "1:02:03");
|
||||
let now = 1_700_000_000_000;
|
||||
assert_eq!(relative_to(now - 30_000, now), "just now");
|
||||
assert_eq!(relative_to(now - 5 * 60_000, now), "5 min ago");
|
||||
assert_eq!(relative_to(now + 2 * 3_600_000, now), "in 2 h");
|
||||
assert_eq!(relative_to(now - 3 * 86_400_000, now), "2023-11-11");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_durations() {
|
||||
assert_eq!(format_duration(59.6), "1:00");
|
||||
assert_eq!(format_duration(754.0), "12:34");
|
||||
assert_eq!(format_duration(3_723.0), "1:02:03");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+19
-49
@@ -24,9 +24,8 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
error,
|
||||
..
|
||||
}) => draw_edit(frame, state, title, fields, *focus, error.as_deref()),
|
||||
Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label),
|
||||
Some(Popup::ConfirmPodcastUnsubscribe { title, .. }) => {
|
||||
draw_confirm_podcast_unsubscribe(frame, state, title)
|
||||
Some(Popup::ConfirmDelete { target, label }) => {
|
||||
draw_confirm_delete(frame, state, *target, label)
|
||||
}
|
||||
Some(Popup::SimilarityPrivacyConsent { .. }) => {
|
||||
draw_similarity_privacy_consent(frame, state)
|
||||
@@ -46,7 +45,7 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
}) => draw_track_artists(frame, state, tracks, *cursor, *selected),
|
||||
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
|
||||
Some(Popup::FedInput { field, input }) => {
|
||||
let keys = if *field == crate::app::state::FedInputField::PodcastSearch {
|
||||
let keys = if *field == crate::app::state::FedInputField::PodcastFilter {
|
||||
"enter: keep · esc: clear"
|
||||
} else {
|
||||
"enter: apply · esc: cancel"
|
||||
@@ -1249,11 +1248,23 @@ fn draw_edit(
|
||||
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer);
|
||||
}
|
||||
|
||||
fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
|
||||
fn draw_confirm_delete(
|
||||
frame: &mut Frame,
|
||||
state: &AppState,
|
||||
target: crate::app::state::DeleteTarget,
|
||||
label: &str,
|
||||
) {
|
||||
// Unsubscribing only drops the feed's stored metadata, not music.
|
||||
let (title, verb, key) = match target {
|
||||
crate::app::state::DeleteTarget::Podcast(_) => {
|
||||
(" Unsubscribe? ", "Unsubscribe from", "unsubscribe")
|
||||
}
|
||||
_ => (" Delete? ", "Delete", "delete"),
|
||||
};
|
||||
let width = 64.min(frame.area().width.saturating_sub(4)).max(30);
|
||||
let area = centered(frame.area(), width, 7);
|
||||
let block = Block::bordered()
|
||||
.title(" Delete? ")
|
||||
.title(title)
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::strong_border_for(state));
|
||||
let inner = block.inner(area);
|
||||
@@ -1262,55 +1273,14 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
|
||||
|
||||
let [body, footer] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("Delete {label}?"))
|
||||
Paragraph::new(format!("{verb} {label}?"))
|
||||
.wrap(Wrap { trim: false })
|
||||
.alignment(Alignment::Center),
|
||||
body,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled("enter/y delete · esc/n cancel", theme::dim()))
|
||||
.alignment(Alignment::Center),
|
||||
footer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Text of the unsubscribe confirmation: unsubscribing keeps progress and
|
||||
/// listening history, so subscribing again continues where the user left off.
|
||||
pub(crate) const PODCAST_UNSUBSCRIBE_NOTE: &str = "Listening progress and history are kept: \
|
||||
subscribe again later to continue where you left off.";
|
||||
|
||||
fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title: &str) {
|
||||
let width = 66.min(frame.area().width.saturating_sub(4)).max(30);
|
||||
let area = centered(frame.area(), width, 9);
|
||||
let block = Block::bordered()
|
||||
.title(" Unsubscribe? ")
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::strong_border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(block, area);
|
||||
|
||||
let [question, note, footer] = Layout::vertical([
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(inner);
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("Unsubscribe from \"{title}\"?"))
|
||||
.wrap(Wrap { trim: false })
|
||||
.alignment(Alignment::Center),
|
||||
question,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(PODCAST_UNSUBSCRIBE_NOTE, theme::dim()))
|
||||
.wrap(Wrap { trim: true })
|
||||
.alignment(Alignment::Center),
|
||||
note,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(
|
||||
"enter/y unsubscribe · esc/n cancel",
|
||||
format!("enter/y {key} · esc/n cancel"),
|
||||
theme::dim(),
|
||||
))
|
||||
.alignment(Alignment::Center),
|
||||
|
||||
@@ -24,7 +24,6 @@ fn playing_state() -> AppState {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
});
|
||||
state.player.playing = true;
|
||||
state.player.position_secs = 12.0;
|
||||
|
||||
Reference in new issue
Block a user