diff --git a/AGENTS.md b/AGENTS.md index 8cb1fe3..1469119 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -65,8 +65,21 @@ update it only when dependencies change. 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 + re-derived here. Public podcast cards are published only through + `podcasts::publication` (`PodcastDht::sync_shares` over the merged + registers): never publish a podcast directly, never log feed URLs or ids of + podcasts that are not shared, never put progress, history, counters or + device names into a card. A local share-off or unsubscribe asks for an + immediate pass (`Reconciler::revoke_now`), which quitting waits for, and + privacy confirmations change only through `publication::set_shared` / + `ShareConfirmations` (bound to the share decision, never synced). Nothing + is published before `DeviceSync::podcast_registers_current` (a restarted + device may hold a revoked share decision); passes before it only revoke. + Public cards found by search are publisher claims: + subscribe and preview only through `podcasts::discovery`, which uses the + card's origin URL (`PodcastSearchHit::feed_url`), never a claimed URL, and + never stores card metadata. Queue items are identified by `state::track_key` + / `TrackItem::key`, never by numeric id (every episode has the same sentinel id); music-only code must skip `TrackItem::is_podcast()` items. - `src/devices.rs` owns trusted-device replication, membership, and playback coordination. Its wire types are the shared `music_dht::device_sync` diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 6191f0c..096845f 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -398,6 +398,63 @@ The TUI owns what the contract leaves to clients: one within the completion window, completes the episode and moves the queue on; otherwise the last seen position is saved and playback stops on the episode (`playback::played_to_the_end`). +- **Public discovery.** The federation node runs FRID's signed podcast + overlay (`music_dht::podcast_dht`, ALPN `furumi-fd/podcast-dht/1`, cache + `federation/podcast-dht.sqlite3`) next to the similarity overlay: it + serves the overlay, bootstraps, republishes and expires records, hands + every peer capability manifest to `note_peer_capabilities`, and advertises + `podcast_dht` in its own manifest only while the overlay runs. A cache + that cannot be opened is logged and only turns discovery off. What this + device publishes is decided by `podcasts::publication`: after local share + and unsubscribe actions (at once: `Reconciler::revoke_now`), received + podcast operations and feed refreshes (debounced 1 s), every overlay + (re)start and every 5 minutes it builds one + `PodcastShareState` per subscription (merged registers, a card from the + catalog for shared podcasts only, the local privacy confirmation) and + calls the declarative `PodcastDht::sync_shares`, which publishes what is + shared and tombstones every other own card. The share decision is the + synced `podcast_share_set` register, so share-off or unsubscribe on any + trusted device revokes every device's card and a stale device cannot + republish: until `DeviceSync::podcast_registers_current` turns true (a + sync exchange applied with a v4 device of the group after the podcast + store was attached, or no reachable v4 device in the group) a pass only + unpublishes the own cards its registers no longer share + (`publication::revoke_unshared`) and never calls `sync_shares`, whose + first call would re-send the cards FRID withholds from an earlier + session. Publication runs from each device's own identity. Privacy + confirmations of feed URLs that look private are device-local + (`podcast-share-confirmations.json`, mode 0600) and bound to the exact + URLs and to the share decision (the share register's operation): a newer + decision on any device drops them. Cards leave out artwork and web links + that look private, and both for a private-looking feed. Without a + readable podcast store this device revokes all its own cards. The report + of each pass lists the private podcasts whose own card is still live + (`unrevoked`: pending, failed or federation off) for the side panel; the + overlay peer count is refreshed every 10 s between passes. Before the + node stops on quit, `Reconciler::flush` waits up to 4 s for a revocation + requested just before. Logs carry counts only. +- **Finding and subscribing.** `Federation::search_podcasts` + (`podcasts::discovery::search`) runs `PodcastDht::search` as its own task, + bounded at 15 s including the overlay's bootstrap, apart from the music + search (`Federation::search`, unchanged), and delivers + `AppEvent::FedPodcastsLoaded` for the same search sequence, so music + results never wait for the overlay. The hits keep the podcast entries of + `merge_search_results` (exact titles first) as + `podcasts::discovery::PodcastHit`s. Off the UI thread each hit's origin + URL is resolved with + `PodcastStore::resolve_podcast_id`, and the "subscribed" mark compares + that id and the card's id with the subscribed podcasts, so a feed alias + counts. A card is a publisher's claim: only `PodcastSearchHit::feed_url` + (the origin URL the id is hashed from) is ever fetched, both for the + episode preview (`discovery::preview`, nothing stored) and for + «Подписаться» (`discovery::subscribe_from_hit` → `refresh::subscribe_url`), + which refuses a hit whose id does not match its URL and stores only what + the feed says. Claimed moved-feed URLs are displayed, never fetched or + recorded as aliases. The new subscription is private. Subscribing to an + address whose feed announces (`itunes:new-feed-url`) a newer one this + device already follows reports that subscription instead of adding a + second one, and the preview (`discovery::preview_for`) resolves the + address the feed moved to, so the card reads «Уже подписан» then. - **Progress.** `podcasts::progress` runs one writer thread so every progress write of this device is ordered. The app tick follows the position and actual listening time and sends: an explicit start (or @@ -541,6 +598,8 @@ Furumi stores different kinds of state according to their lifetime: | --- | --- | --- | | Library, playlists, likes, history | SQLite | Durable local source of truth | | Podcast subscriptions, episodes, progress | SQLite (`podcasts.sqlite3`, FRID store) | Durable personal state (synced with trusted devices) and a refreshable episode catalog | +| Podcast share confirmations | JSON next to `podcasts.sqlite3` | Device-local consent to publish feed URLs that look private | +| Public podcast cards (own and replicas) | SQLite (`federation/podcast-dht.sqlite3`) | Discovery overlay cache; replaceable | | Device operation log and replicas | SQLite | Offline synchronization | | Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data | | Federation catalog cache | SQLite/cache | Faster network browsing | @@ -580,8 +639,9 @@ The source tree follows the architectural responsibilities: - `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; + writer, the publication of shared podcasts and subscribing from public + cards; the podcast model, + identity, merge rules, SQLite store and discovery overlay come from FRID; - `devices.rs` owns trusted-device replication and playback coordination; - `app/` owns state transitions and runtime orchestration; - `ui/` renders the TUI; diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b7a022..61acca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.5.0] - unreleased + +### Added + +- Sharing a podcast in the federation (`Shift+F` in the `podcasts` context, + off by default): the synced share decision makes each device publish a + signed public card of the podcast in the FRID discovery overlay + (`podcast_dht`, advertised in the capability manifest); share-off and + unsubscribe revoke it with a tombstone. Feed URLs that look private are + published only after a confirmation on that device. The list and side + panel show whether a podcast is shared and whether this device publishes + it. +- Turning sharing off or unsubscribing revokes this device's card at once, + and quitting waits up to 4 s for that (also for a change still being + saved); the side panel says when a private podcast's card is not revoked + yet (pending or failed) or may not be (federation off: a card of an + earlier session cannot be ruled out). A privacy confirmation covers one + share decision: turning sharing off (or on again after that) on any + trusted device asks again, and until then this device holds the podcast + back and revokes the card it published for the earlier decision. A + confirmation whose decision could not be saved is dropped. Cards leave out + artwork and web links with an access key in the query or a member-feed + host (random-looking path parts of public CDNs are fine), and both for a + feed whose URL looks private. +- A device with other devices in its group publishes its cards only after + it synced with one of them since it started (or once none is left that + syncs podcasts), so a device that was closed while sharing was turned off + elsewhere does not send that card again; until then it only revokes, and + the side panel says it waits for a sync. +- Search (`/`, with the federation running) also finds public podcast cards. + They are listed after the music results under "Podcasts · public cards" with + a `[podcast]` marker, the number of publishing devices, and "✓ subscribed" + when this device is subscribed (also through a feed alias). The podcast + search runs apart from the federated music search (bounded at 15 s), so + music results never wait for it; local-only search is unchanged. +- Enter on a podcast hit opens its card: title, author, categories, + description, episode count, newest episode, publishing devices, feed URL + and unverified moved-feed claims. `e` reads the seasons and episodes from + the feed without subscribing. «Подписаться» (Enter) subscribes privately + with the card's origin feed URL, so the podcast id is the card's, and + reads everything from the feed itself; «Уже подписан» opens the podcast + in the Podcasts tab instead, also when this device follows the feed under + the newer address it moved to (known once the feed was read), and + subscribing to such an older address adds no second subscription. + +### Changed + +- `furumi-tui` 0.5.0. + ## [0.4.0] - unreleased ### Added diff --git a/Cargo.lock b/Cargo.lock index 096d153..493d33f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1559,7 +1559,7 @@ dependencies = [ [[package]] name = "federation-net" version = "0.3.1" -source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fdiscovery#9c82f6d91691a66d0a559caba06e13a1293f06be" dependencies = [ "blake3", "data-encoding", @@ -1698,7 +1698,7 @@ dependencies = [ [[package]] name = "furumi-library" version = "0.3.0" -source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fdiscovery#9c82f6d91691a66d0a559caba06e13a1293f06be" dependencies = [ "anyhow", "blake3", @@ -1713,7 +1713,7 @@ dependencies = [ [[package]] name = "furumi_tui" -version = "0.4.0" +version = "0.5.0" dependencies = [ "anyhow", "base64", @@ -3238,7 +3238,7 @@ dependencies = [ [[package]] name = "music-dht" version = "0.6.0" -source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fstore#d2530dca6ca0905cab2704615b89c8b89c70c7cf" +source = "git+https://gt.hexor.cy/Sanapach/frid.git?branch=podcasts%2Fdiscovery#9c82f6d91691a66d0a559caba06e13a1293f06be" dependencies = [ "async-trait", "blake3", diff --git a/Cargo.toml b/Cargo.toml index fddcb74..cee9a1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "furumi_tui" -version = "0.4.0" +version = "0.5.0" edition = "2024" rust-version = "1.97" description = "A federated P2P player for personal music libraries" @@ -33,7 +33,7 @@ lofty = "0.22" # P2P federation: library index in a shared DHT + audio streaming between # peers (same protocol as furumi-fd). music-dht = { version = "0.6.0", features = ["podcast-feed"] } -ratatui = "0.30.1" +ratatui = { version = "0.30.1", features = ["unstable-rendered-line-info"] } reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] } rhai = { version = "1", features = ["sync"] } rodio = { version = "0.22.2", default-features = false, features = ["playback", "mp3", "flac", "vorbis", "wav", "symphonia-aac", "symphonia-isomp4", "symphonia-alac"] } @@ -57,9 +57,9 @@ unicode-width = "0.2.2" # souvlaki 0.8.3 still depends on the unmaintained block 0.1.6. Keep its API # intact while using an opaque inhabited FFI type accepted by current Rust. block = { path = "vendor/block" } -# Unpublished frid (MR-F1, MR-F2); remove once music-dht 0.6.0 and furumi-library 0.3.0 are on crates.io. -music-dht = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" } -furumi-library = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/store" } +# Unpublished frid (MR-F1, MR-F2, MR-F3); 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/discovery" } +furumi-library = { git = "https://gt.hexor.cy/Sanapach/frid.git", branch = "podcasts/discovery" } [target.'cfg(target_os="macos")'.dependencies] core-foundation = "0.10.1" diff --git a/README.md b/README.md index 5d52dde..827f3d8 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,10 @@ never imported into the music library or offered to other peers. - `Shift+U` (or `Shift+D`) unsubscribes after a confirmation. Listening progress and history are kept, so subscribing again continues where you left off. +- `Shift+F` shares the selected (or opened) podcast in the federation, or + makes it private again. The list marks shared podcasts with `shared`, and + the side panel says whether this device publishes the card (see + "Sharing a podcast" below). Episodes play in the same queue as music: @@ -251,8 +255,7 @@ Podcasts and your connected devices: 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. + podcast for discovery is a separate, explicit choice (`Shift+F`, below). - **Synchronized between trusted devices.** A podcast you subscribe to on one device appears on the others without typing its URL again; each device then checks the feed itself. Unsubscribing, "played", the saved @@ -281,6 +284,94 @@ Podcasts and your connected devices: or address), cannot add episodes to the host's queue and cannot seek in a private item. They can still reorder the queue and skip. +Sharing a podcast (public discovery): + +- `Shift+F` turns "share in the federation" on for one podcast. The choice is + synchronized to your trusted devices like a subscription; each of your + devices that runs the federation then publishes a small public card of the + podcast under its own device key: the feed URL, title, author, + description, artwork and web links, the number of episodes and the date of + the newest one. The card never contains your progress, history, play + counts, device names or anything from podcasts you did not share. Artwork + and web links that look like they carry an access key (a token, key or + signature in the query, a member-feed host) are left out, and so are both + for a feed whose own URL looks private. Random-looking parts of their + path alone do not count: public podcast hosts name artwork that way. +- If the feed URL looks private (a token, key or signature in its query, a + member-feed host such as Patreon, a long random-looking part), Furumi asks + first and lists why (in Russian). Anyone who finds the card can read the + feed at that URL. The confirmation is stored only on this device + (`podcast-share-confirmations.json` next to `podcasts.sqlite3`) and covers + exactly the URLs shown and this one decision to share: another device of + yours asks again before it publishes, and so does this one if the feed + moves or if sharing is turned off (or turned on again after that) on any + of your devices. Until then the side panel says the publication is held + back, and a card this device published for the earlier decision is + revoked; press `Shift+F` there to confirm (or `x` in the dialog to stop + sharing). A confirmation whose decision could not be saved is dropped. +- `Shift+F` on a shared podcast, or unsubscribing, makes it private again on + every device in your group: each device replaces its card with a signed + removal record. This device sends its removal right away, and quitting + waits for it (up to 4 seconds, also when you quit before the change is + saved) and logs a warning when a card may still be live. While the + removal is not sent yet (or could not be sent), the side panel says the + public card is not revoked yet instead of just "private". With the + federation off this device cannot tell whether a card of an earlier + session is still live, so for a podcast that was shared the panel says + the card may not be revoked yet; the removal is sent the next time the + federation runs. Until then, and for a copy that missed the removal, the + card can still be found until it expires (at most 12 hours). Removing your card never touches other + people's subscriptions. An older device that comes back with the earlier + "share" decision does not publish it again: after starting, a device with + other devices in its group publishes nothing until it has synced with one + of them (the side panel says "waiting for a sync with your other + devices"); it still revokes at once what its own decisions no longer + share. Devices of your group that still run Furumi 0.3 (they do not sync + podcasts) and devices without an address are not waited for; remove a + device you no longer use under Connected Devices so this one stops + waiting for it. +- A device that left your device group, or was removed from it, no longer + receives your share decisions: it keeps publishing the podcasts it shares + until you turn sharing off (or unsubscribe) on that device itself. +- The card is signed with this device's federation key, which is also the + key of its public music library, so a shared podcast can be linked to + that library and to the device's network address. +- Publishing needs the federation to be running; the side panel shows + "not published" with the reason otherwise. + +Finding public podcasts: + +- The search (`/` outside the Podcasts tab, with the federation + running) also looks for public podcast cards. They are listed after the + music results under "Podcasts · public cards", each marked `[podcast]`, + with the number of devices that publish the card (devices, not + subscribers; anyone can create devices, so it is not a sign of trust) or + "✓ subscribed" when this device already follows the podcast, also when + you subscribed under an older address of the same feed that moved to the + card's address. When you follow the feed under a newer address than the + card's, the list cannot tell yet; the card can once its episodes are read + (`e`), and «Подписаться» never adds a second subscription for it. Your + query words go to the discovery peers, as they do for music search. The + podcast search runs apart from the music search (music results never wait + for it) and gives up after 15 seconds. With the federation off, search + stays local and shows no podcasts. +- Enter opens the card (in Russian): what its publishers claim (title, + author, categories, description, number of episodes and date of the + newest one), the feed URL, and addresses some publishers claim the feed + moved to, shown as unverified and never used. `e` reads the feed itself + and lists its seasons and episodes without subscribing; this is a request + to the podcast's own server, not to the publisher. +- «Подписаться» (Enter) subscribes with the card's feed URL, the address + the podcast's id is derived from, so the podcast is the same one the + publisher shares. Furumi then reads the feed from its own server and + stores only what the feed says; the publisher does not need to stay + online, and nothing from the card is kept. The new subscription is + private like any other: it is not shared until you press `Shift+F`, and + your progress is your own. «Уже подписан» means this device is already + subscribed (under the card's address, an older one, or — once the feed + was read — the newer address the feed moved to); Enter then opens the + podcast in the Podcasts tab. + Podcast data lives in `podcasts.sqlite3` in the platform data directory (the same file the earlier podcast test build used; it is upgraded in place). If that file was written by a newer Furumi or is not a podcast database, the tab diff --git a/src/app/action.rs b/src/app/action.rs index d1d4d2f..4f7b70f 100644 --- a/src/app/action.rs +++ b/src/app/action.rs @@ -75,6 +75,9 @@ pub enum Action { UnsubscribePodcast, /// Podcasts: play the selected (or playing) episode from the beginning. RestartEpisode, + /// Podcasts: turn "share in the federation" on or off for the selected + /// or opened podcast (asks first when its feed URL looks private). + TogglePodcastShare, } /// Help-window sections, in display order. @@ -168,7 +171,8 @@ impl Action { | Action::CycleEpisodeFilter | Action::ToggleEpisodePlayed | Action::UnsubscribePodcast - | Action::RestartEpisode => Category::Podcasts, + | Action::RestartEpisode + | Action::TogglePodcastShare => Category::Podcasts, Action::OpenSearch | Action::OpenCommandLine => Category::Search, Action::ToggleHelp | Action::Quit => Category::System, } @@ -245,7 +249,7 @@ impl Action { Action::CycleSourceMode => "Cycle source mode: Local / My / Global".into(), Action::OpenLibraryFilters => "Library filters…".into(), Action::OpenCommandLine => "Command line (:help for commands)".into(), - Action::OpenSearch => "Search artists, releases, tracks".into(), + Action::OpenSearch => "Search artists, releases, tracks, public podcasts".into(), Action::EditSelected => "Edit the selected item".into(), Action::DeleteSelected => "Delete the selected item".into(), Action::AddPodcast => "Add podcast (RSS URL)".into(), @@ -257,6 +261,7 @@ impl Action { Action::ToggleEpisodePlayed => "Mark played / unplayed".into(), Action::UnsubscribePodcast => "Unsubscribe (keeps progress)".into(), Action::RestartEpisode => "Play episode from the start".into(), + Action::TogglePodcastShare => "Share podcast in the federation on/off".into(), } } } diff --git a/src/app/cmdline.rs b/src/app/cmdline.rs index c14c1c7..c6ae50d 100644 --- a/src/app/cmdline.rs +++ b/src/app/cmdline.rs @@ -109,7 +109,9 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) { state.search.results = None; state.search.fed_tracks.clear(); state.search.fed_artists.clear(); + state.search.fed_podcasts.clear(); state.search.fed_loading = false; + state.search.fed_podcasts_loading = false; return; } let library = Arc::clone(&runtime.library); @@ -132,7 +134,9 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) { // node is up); its results render as a separate, marked section. state.search.fed_tracks.clear(); state.search.fed_artists.clear(); + state.search.fed_podcasts.clear(); state.search.fed_loading = false; + state.search.fed_podcasts_loading = false; if runtime.federation.settings().enabled { state.search.fed_loading = true; let fed = Arc::clone(&runtime.federation); @@ -147,9 +151,47 @@ pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) { let result = fed.search(&query).await.map_err(|err| format!("{err:#}")); let _ = tx.send(AppEvent::FedSearchLoaded { seq, result }); }); + schedule_podcast_search(state, runtime, seq); } } +/// Public podcast cards for the live search `seq`, as their own task and +/// event: the overlay search (bounded, bootstrap included) never holds back +/// the music results. +fn schedule_podcast_search(state: &mut AppState, runtime: &Runtime, seq: u64) { + state.search.fed_podcasts_loading = true; + let fed = Arc::clone(&runtime.federation); + let query = state.search.query.clone(); + let tx = runtime.event_tx.clone(); + let latest = Arc::clone(&runtime.search_seq); + let podcast_store = runtime + .podcasts + .as_ref() + .map(|podcasts| Arc::clone(&podcasts.services.store)); + tokio::spawn(async move { + tokio::time::sleep(SEARCH_DEBOUNCE).await; + if latest.load(Ordering::SeqCst) != seq { + return; + } + let mut hits = fed.search_podcasts(&query).await; + // Which public podcasts this device already knows (feed aliases + // included), for the "subscribed" mark. + if let Some(store) = podcast_store + && !hits.is_empty() + { + hits = tokio::task::spawn_blocking(move || { + if let Err(err) = crate::podcasts::discovery::resolve_local_ids(&store, &mut hits) { + tracing::warn!("podcast search: feed aliases not resolved: {err:#}"); + } + hits + }) + .await + .unwrap_or_default(); + } + let _ = tx.send(AppEvent::FedPodcastsLoaded { seq, hits }); + }); +} + pub(super) fn schedule_similarity_search( state: &mut AppState, runtime: &Runtime, @@ -171,7 +213,9 @@ pub(super) fn schedule_similarity_search( state.search.results = None; state.search.fed_tracks.clear(); state.search.fed_artists.clear(); + state.search.fed_podcasts.clear(); state.search.fed_loading = false; + state.search.fed_podcasts_loading = false; state.active_tab = crate::app::state::Tab::Global; if let Some(crate::app::state::GlobalView::Search { cursor }) = state.global.stack.last_mut() { *cursor = 0; diff --git a/src/app/event.rs b/src/app/event.rs index 89a9840..0cdd107 100644 --- a/src/app/event.rs +++ b/src/app/event.rs @@ -138,6 +138,13 @@ pub enum AppEvent { seq: u64, result: Result, }, + /// Public podcast cards for the same live search (`seq`), delivered on + /// their own so the music results never wait for the overlay. Already + /// resolved against the podcast store. + FedPodcastsLoaded { + seq: u64, + hits: Vec, + }, /// A federated artist card finished assembling. FedArtistLoaded { name: String, @@ -268,4 +275,24 @@ pub enum AppEvent { PodcastRefreshTick, /// Feeds due for a background refresh. PodcastsDue(Result, String>), + /// What sharing a podcast would publish (`Effect::PodcastPrepareShare`). + PodcastSharePrepared { + podcast_id: music_dht::podcast::PodcastId, + title: String, + result: Result, + }, + /// The share decision was written (`Effect::PodcastSetShared`). + PodcastShareSet { + podcast_id: music_dht::podcast::PodcastId, + title: String, + shared: bool, + result: Result<(), String>, + }, + /// The publication reconciler finished a pass. + PodcastPublication(crate::podcasts::publication::PublicationReport), + /// A public card's feed was read for a preview (`Effect::PodcastPreview`). + PodcastPreviewLoaded { + podcast_id: music_dht::podcast::PodcastId, + result: Result, String>, + }, } diff --git a/src/app/mod.rs b/src/app/mod.rs index 23071bb..56c232e 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1734,7 +1734,12 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) { | Effect::PodcastSubscribe(_) | Effect::PodcastRefresh { .. } | Effect::PodcastUnsubscribe { .. } - | Effect::PodcastSetPlayed { .. } => {} + | Effect::PodcastSetPlayed { .. } + | Effect::PodcastPrepareShare { .. } + | Effect::PodcastSetShared { .. } + | Effect::PodcastPublicationSync + | Effect::PodcastSubscribeFromCard(_) + | Effect::PodcastPreview { .. } => {} Effect::LoadListenHistory => { let library = Arc::clone(&runtime.library); let devices = Arc::clone(&runtime.devices); @@ -3531,7 +3536,11 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent | AppEvent::PodcastEpisodeStarted { .. } | AppEvent::PodcastEpisodeFailed { .. } | AppEvent::PodcastRefreshTick - | AppEvent::PodcastsDue(_) => {} + | AppEvent::PodcastsDue(_) + | AppEvent::PodcastSharePrepared { .. } + | AppEvent::PodcastShareSet { .. } + | AppEvent::PodcastPublication(_) + | AppEvent::PodcastPreviewLoaded { .. } => {} AppEvent::StatusVisualizationReady(ready) => { runtime.status_visualizer.busy = false; state.status_visualizer.apply(ready); @@ -3796,6 +3805,13 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent Err(message) => tracing::warn!(%message, "federated search failed"), } } + AppEvent::FedPodcastsLoaded { seq, hits } => { + if runtime.search_seq.load(std::sync::atomic::Ordering::SeqCst) != seq { + return; + } + state.search.fed_podcasts_loading = false; + state.search.fed_podcasts = hits; + } AppEvent::FedSimilaritySearchLoaded { seq, result } => { if runtime.search_seq.load(std::sync::atomic::Ordering::SeqCst) != seq { return; diff --git a/src/app/podcast_runtime.rs b/src/app/podcast_runtime.rs index 62af2ce..3df4139 100644 --- a/src/app/podcast_runtime.rs +++ b/src/app/podcast_runtime.rs @@ -12,13 +12,15 @@ use music_dht::podcast::{PodcastEpisodeRef, reaches_completion}; use super::event::AppEvent; use super::state::{AppState, PodcastRow}; -use super::update::{Effect, podcasts}; +use super::update::{Effect, podcast_discovery, podcasts}; use super::{Runtime, err_string, perform_effect}; use crate::library::models::TrackItem; +use crate::podcasts::discovery; use crate::podcasts::playback::{self, StartAt}; use crate::podcasts::progress::{ CHECKPOINT_INTERVAL, ProgressCommand, ProgressEvent, ProgressWriter, }; +use crate::podcasts::publication::{self, Reconciler, ShareConfirmations}; use crate::podcasts::refresh::{self, Services}; use crate::podcasts::stream::{CancelToken, StreamEnd, StreamProbe}; @@ -31,6 +33,9 @@ const SCHEDULER_FIRST_DELAY: Duration = Duration::from_secs(10); const MAX_CONCURRENT_REFRESHES: usize = 4; /// Feeds started per scheduler pass. const MAX_DUE_PER_PASS: usize = 16; +/// How long quitting waits for a pending revocation (share turned off or +/// unsubscribed just before) to be signed and pushed. +const PUBLICATION_FLUSH_TIMEOUT: Duration = Duration::from_secs(4); /// Podcast services available once the store opened. pub struct PodcastRuntime { @@ -39,6 +44,8 @@ pub struct PodcastRuntime { /// Writes progress in order on its own thread; `None` if it could not /// start (episodes then play without saving progress). progress: Option, + /// Publishes shared podcasts to public discovery and revokes the rest. + publication: Reconciler, } /// The episode this device is playing (or starting), as the progress @@ -124,6 +131,53 @@ fn spawn_scheduler(tx: tokio::sync::mpsc::UnboundedSender) { }); } +/// Starts the publication reconciler. Without a store every own public +/// card is revoked (see `publication::reconcile`). Nothing is published +/// before device sync says the share registers are current +/// (`DeviceSync::podcast_registers_current`). +fn spawn_publication( + runtime: &Runtime, + store: Option>, + confirmations: Arc, +) -> Reconciler { + let tx = runtime.event_tx.clone(); + publication::spawn( + runtime.federation.podcast_dht(), + runtime.devices.podcast_registers_current(), + store, + confirmations, + move |report| { + let _ = tx.send(AppEvent::PodcastPublication(report)); + }, + ) +} + +/// Asks the publication reconciler for a pass (share decisions, feed +/// metadata or subscriptions changed). +fn wake_publication(runtime: &Runtime) { + if let Some(podcasts) = &runtime.podcasts { + podcasts.publication.wake(); + } +} + +/// A card may have to be revoked (sharing turned off or unsubscribed here): +/// the write about to start ends with an immediate reconciler pass +/// ([`publication::RevocationTicket::done`]), and quitting waits for both, +/// bounded ([`PUBLICATION_FLUSH_TIMEOUT`]), even when it is handled before +/// the write finished. +fn begin_revocation(runtime: &Runtime) -> Option { + runtime + .podcasts + .as_ref() + .map(|podcasts| podcasts.publication.begin_revocation()) +} + +/// Whether `podcast_id` is shared now (a card from this device may be +/// live), read before a share-off or unsubscribe is written. Blocking. +fn shared_now(services: &Services, podcast_id: &music_dht::podcast::PodcastId) -> bool { + matches!(services.store.podcast(podcast_id), Ok(Some(summary)) if summary.shared) +} + fn services(runtime: &Runtime) -> Option { runtime .podcasts @@ -140,9 +194,24 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O state.podcasts.refreshing.remove(id); } } - if matches!(effect, Effect::PodcastSubscribe(_)) { + if matches!( + effect, + Effect::PodcastSubscribe(_) | Effect::PodcastSubscribeFromCard(_) + ) { state.podcasts.subscribing = false; } + if let Effect::PodcastPreview { podcast_id, .. } = &effect { + podcast_discovery::preview_loaded( + state, + podcast_id.clone(), + Err("podcasts are unavailable".into()), + ); + } + if let Effect::PodcastPrepareShare { podcast_id, .. } + | Effect::PodcastSetShared { podcast_id, .. } = &effect + { + state.podcasts.share_busy.remove(podcast_id); + } state.status_message = Some("podcasts are unavailable (see the Podcasts tab)".into()); return None; } @@ -181,6 +250,26 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O let _ = tx.send(AppEvent::PodcastSubscribed(result)); }); } + Effect::PodcastSubscribeFromCard(hit) => { + tokio::spawn(async move { + let result = discovery::subscribe_from_hit(&services, &hit, now_ms()) + .await + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastSubscribed(result)); + }); + } + Effect::PodcastPreview { + podcast_id, + feed_url, + } => { + tokio::spawn(async move { + let result = discovery::preview_for(&services, &feed_url) + .await + .map(Box::new) + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastPreviewLoaded { podcast_id, result }); + }); + } Effect::PodcastRefresh { targets, manual } => { let slots = runtime .podcasts @@ -206,13 +295,28 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O } } Effect::PodcastUnsubscribe { podcast_id, title } => { + let revocation = begin_revocation(runtime); tokio::task::spawn_blocking(move || { let store = Arc::clone(&services.store); let id = podcast_id.clone(); + let was_shared = shared_now(&services, &podcast_id); let result = services .log .record(&mut |stamps| store.unsubscribe(stamps, &id)) .map_err(err_string); + // Unsubscribing also revoked the share decision: sharing + // after subscribing again asks again. + if result.is_ok() + && let Err(err) = services.confirmations.forget(&podcast_id) + { + tracing::warn!("podcast share confirmation not removed: {err:#}"); + } + // Unsubscribing revokes this device's card, right away. + if let Some(revocation) = revocation + && result.is_ok() + { + revocation.done(was_shared.then_some(&podcast_id)); + } let _ = tx.send(AppEvent::PodcastUnsubscribed { podcast_id, title, @@ -235,11 +339,75 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O } } } + Effect::PodcastPrepareShare { podcast_id, title } => { + tokio::task::spawn_blocking(move || { + let result = + publication::check_share(&services.store, &services.confirmations, &podcast_id) + .map_err(err_string); + let _ = tx.send(AppEvent::PodcastSharePrepared { + podcast_id, + title, + result, + }); + }); + } + Effect::PodcastSetShared { + podcast_id, + title, + shared, + confirm, + } => { + let revocation = if shared { + None + } else { + begin_revocation(runtime) + }; + tokio::task::spawn_blocking(move || { + let was_shared = !shared && shared_now(&services, &podcast_id); + let result = + set_shared(&services, &podcast_id, shared, confirm).map_err(err_string); + if let Some(revocation) = revocation + && result.is_ok() + { + revocation.done(was_shared.then_some(&podcast_id)); + } + let _ = tx.send(AppEvent::PodcastShareSet { + podcast_id, + title, + shared, + result, + }); + }); + } + Effect::PodcastPublicationSync => wake_publication(runtime), other => return Some(other), } None } +/// Writes a share decision and keeps the local privacy confirmation bound +/// to it (`publication::set_shared`). Blocking. +fn set_shared( + services: &Services, + podcast_id: &music_dht::podcast::PodcastId, + shared: bool, + confirm: Option, +) -> anyhow::Result<()> { + let store = Arc::clone(&services.store); + publication::set_shared( + &services.store, + &services.confirmations, + podcast_id, + shared, + confirm, + || { + services + .log + .record(&mut |stamps| store.set_shared(stamps, podcast_id, shared)) + }, + ) +} + fn is_podcast_effect(effect: &Effect) -> bool { matches!( effect, @@ -250,6 +418,11 @@ fn is_podcast_effect(effect: &Effect) -> bool { | Effect::PodcastRefresh { .. } | Effect::PodcastUnsubscribe { .. } | Effect::PodcastSetPlayed { .. } + | Effect::PodcastPrepareShare { .. } + | Effect::PodcastSetShared { .. } + | Effect::PodcastPublicationSync + | Effect::PodcastSubscribeFromCard(_) + | Effect::PodcastPreview { .. } ) } @@ -274,6 +447,11 @@ pub fn handle_event( ) -> Option { let effects: Vec = match event { AppEvent::PodcastStoreOpened(result) => { + if result.is_err() { + // This device can no longer tell what is shared: revoke its + // own public cards (fail-safe for privacy). + let _ = spawn_publication(runtime, None, Arc::new(ShareConfirmations::in_memory())); + } let result = result.map(|services| { let tx = runtime.event_tx.clone(); let progress = ProgressWriter::spawn(services.clone(), move |event| { @@ -291,10 +469,16 @@ pub fn handle_event( tracing::warn!("podcast sync: {err:#}"); } }); + let publication = spawn_publication( + runtime, + Some(Arc::clone(&services.store)), + Arc::clone(&services.confirmations), + ); runtime.podcasts = Some(PodcastRuntime { services, refresh_slots: Arc::new(tokio::sync::Semaphore::new(MAX_CONCURRENT_REFRESHES)), progress, + publication, }); spawn_scheduler(runtime.event_tx.clone()); }); @@ -323,17 +507,59 @@ pub fn handle_event( podcast_id, result, manual, - } => podcasts::refreshed(state, podcast_id, result, manual), + } => { + // The card of a shared podcast follows its feed metadata. + if result.is_ok() { + wake_publication(runtime); + } + podcasts::refreshed(state, podcast_id, result, manual) + } AppEvent::PodcastUnsubscribed { podcast_id, title, result, - } => podcasts::unsubscribed(state, podcast_id, title, result) + } => { + // The blocking write already asked for the revocation pass. + podcasts::unsubscribed(state, podcast_id, title, result) + .into_iter() + .collect() + } + AppEvent::PodcastSharePrepared { + podcast_id, + title, + result, + } => podcasts::share_prepared(state, podcast_id, title, result) .into_iter() .collect(), + AppEvent::PodcastShareSet { + podcast_id, + title, + shared, + result, + } => { + // A share-off already asked for its revocation pass when it + // was written. + if shared || result.is_err() { + wake_publication(runtime); + } + podcasts::share_set(state, podcast_id, title, shared, result) + .into_iter() + .collect() + } + AppEvent::PodcastPreviewLoaded { podcast_id, result } => { + podcast_discovery::preview_loaded(state, podcast_id, result); + Vec::new() + } + AppEvent::PodcastPublication(report) => { + podcasts::publication_reported(state, report); + Vec::new() + } AppEvent::PodcastProgress(event) => progress_event(state, runtime, event), AppEvent::PodcastsSynced { progress, changed } => { if changed { + // Share or subscription decisions of another device: publish + // or revoke this device's own cards accordingly. + wake_publication(runtime); // A subscription that came from another device has no // episodes here until its feed is fetched: check now. let _ = runtime.event_tx.send(AppEvent::PodcastRefreshTick); @@ -916,18 +1142,33 @@ fn progress_event( } } -/// Before exit: end the playing episode's session and give the progress -/// writer a moment to save it. +/// Before exit (before the federation node stops): end the playing +/// episode's session and give the progress writer a moment to save it, and +/// let a revocation requested just before (sharing turned off, unsubscribed; +/// also one still being written) reach the overlay. Logs a warning when a +/// card of a private podcast may still be live. pub(crate) async fn shutdown(runtime: &mut Runtime) { end_playback(runtime); - let Some(mut progress) = runtime - .podcasts - .as_mut() - .and_then(|podcasts| podcasts.progress.take()) - else { + let Some(podcasts) = runtime.podcasts.as_mut() else { return; }; - let _ = tokio::task::spawn_blocking(move || progress.close(Duration::from_secs(3))).await; + let publication = podcasts.publication.clone(); + let progress = podcasts.progress.take(); + let flush = async { + if !publication.flush(PUBLICATION_FLUSH_TIMEOUT).await { + tracing::warn!( + "podcast publication: a public card may not be revoked yet; \ + it is revoked on the next start with federation on" + ); + } + }; + let close = async { + if let Some(mut progress) = progress { + let _ = + tokio::task::spawn_blocking(move || progress.close(Duration::from_secs(3))).await; + } + }; + tokio::join!(flush, close); } #[cfg(test)] diff --git a/src/app/popup.rs b/src/app/popup.rs index 4bba6e9..97fe21a 100644 --- a/src/app/popup.rs +++ b/src/app/popup.rs @@ -124,6 +124,20 @@ pub fn handle_key(state: &mut AppState, runtime: &mut Runtime, key: KeyEvent) { KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {} _ => state.popup = Some(Popup::ConfirmPodcastUnsubscribe { podcast_id, title }), }, + popup @ Popup::PodcastCard { .. } => { + if let Some(effect) = + crate::app::update::podcast_discovery::card_key(state, popup, key.code) + { + super::perform_effect(state, runtime, effect); + } + } + popup @ Popup::ConfirmPodcastShare { .. } => { + if let Some(effect) = + crate::app::update::podcasts::share_confirmation_key(state, popup, key.code) + { + super::perform_effect(state, runtime, effect); + } + } Popup::SimilarityPrivacyConsent { enable_federation } => match key.code { KeyCode::Enter | KeyCode::Char('y') => { state.similarity.settings.federation_consent = true; diff --git a/src/app/state.rs b/src/app/state.rs index 37c14dc..08c3f07 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -768,6 +768,32 @@ pub enum Popup { podcast_id: music_dht::podcast::PodcastId, title: String, }, + /// Sharing a podcast whose feed URL looks private: the user confirms + /// publishing exactly `urls`. Enter/y publishes, Esc/n cancels; when the + /// podcast is already shared (publication held back on this device), + /// `x` turns sharing off. + ConfirmPodcastShare { + podcast_id: music_dht::podcast::PodcastId, + title: String, + urls: crate::podcasts::publication::ConfirmedUrls, + /// Russian descriptions of the privacy warnings. + warnings: Vec, + already_shared: bool, + }, + /// A public podcast card from the search results: its claims, an + /// optional episode preview read from the feed, and "Подписаться" (or + /// "Уже подписан" when this device is subscribed under any of its ids). + PodcastCard { + hit: Box, + /// `None` until the user asks for the episodes (`e`). + preview: Option>, + /// First body line shown. + scroll: u16, + /// Largest useful `scroll` at the last draw (the wrapped body + /// height minus the visible rows); scroll keys stop there. + /// `u16::MAX` until the card was drawn. + max_scroll: std::cell::Cell, + }, /// Enabling network similarity reveals an embedding query to peers. SimilarityPrivacyConsent { enable_federation: bool }, /// Derived data is safe to recreate but potentially expensive. @@ -1420,7 +1446,14 @@ pub struct SearchState { /// Artists a federated card can be opened for — from artist records and /// from the artist names of matching tracks. pub fed_artists: Vec, + /// Public podcast cards (type `podcast`), listed after the federated + /// tracks; Enter opens the card. + pub fed_podcasts: Vec, + /// The federated music search is running. pub fed_loading: bool, + /// The podcast search in the discovery overlay is running (separately, + /// so the music results never wait for it). + pub fed_podcasts_loading: bool, /// Present only for a track-seeded search; text-search refreshes must not /// replace this page with a title query. pub similarity_source: Option, @@ -1433,6 +1466,24 @@ pub struct SearchState { } impl SearchState { + /// Rows the cursor can reach in a text search: local artists, releases + /// and tracks, then federated artists, tracks and podcasts. + pub fn total_len(&self) -> usize { + self.results.as_ref().map_or(0, SearchResults::len) + + self.fed_artists.len() + + self.fed_tracks.len() + + self.fed_podcasts.len() + } + + /// Something federated is shown (or still loading). + pub fn has_federated(&self) -> bool { + !self.fed_tracks.is_empty() + || !self.fed_artists.is_empty() + || !self.fed_podcasts.is_empty() + || self.fed_loading + || self.fed_podcasts_loading + } + pub fn similarity_len(&self) -> usize { usize::from(self.similarity_source_track.is_some()) + self.similarity_tracks.len() } @@ -1796,6 +1847,10 @@ pub struct PodcastsTab { pub pending_select: Option, /// Last episode request sequence handed out. pub episode_seq: u64, + /// This device's public discovery publications (last reconciler pass). + pub publication: crate::podcasts::publication::PublicationReport, + /// A share check or share decision is being written. + pub share_busy: HashSet, } impl PodcastsTab { @@ -1810,6 +1865,13 @@ impl PodcastsTab { self.podcasts().iter().find(|row| row.id() == id) } + /// This device is subscribed to the podcast of a public card: under the + /// card's id or under the id its origin URL resolves to here (a feed + /// alias of a podcast subscribed earlier). + pub fn subscribed_to(&self, hit: &crate::podcasts::discovery::PodcastHit) -> bool { + hit.ids().into_iter().any(|id| self.podcast(id).is_some()) + } + pub fn list_rows(&self) -> Vec { let mut rows = vec![PodcastListRow::Add, PodcastListRow::RefreshAll]; rows.extend((0..self.podcasts().len()).map(PodcastListRow::Podcast)); diff --git a/src/app/update.rs b/src/app/update.rs index 6fc379e..32599c3 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -136,6 +136,32 @@ pub enum Effect { episode_id: music_dht::podcast::EpisodeId, played: bool, }, + /// Check what sharing a podcast would publish (privacy warnings) before + /// turning sharing on or confirming a held-back publication. + PodcastPrepareShare { + podcast_id: music_dht::podcast::PodcastId, + title: String, + }, + /// Turn sharing on or off (synced decision). `confirm` records, on this + /// device only, that the user confirmed publishing these feed URLs + /// despite privacy warnings. + PodcastSetShared { + podcast_id: music_dht::podcast::PodcastId, + title: String, + shared: bool, + confirm: Option, + }, + /// Reconcile this device's public podcast cards now. + PodcastPublicationSync, + /// Subscribe to the podcast of a public card with its origin feed URL + /// (`podcasts::discovery::subscribe_from_hit`); reported as + /// `AppEvent::PodcastSubscribed`. + PodcastSubscribeFromCard(Box), + /// Read a public card's feed for an episode preview (nothing stored). + PodcastPreview { + podcast_id: music_dht::podcast::PodcastId, + feed_url: music_dht::podcast::FeedUrl, + }, } pub fn update(state: &mut AppState, action: Action) -> Option { @@ -541,7 +567,8 @@ pub fn update(state: &mut AppState, action: Action) -> Option { | Action::CycleEpisodeSort | Action::CycleEpisodeFilter | Action::ToggleEpisodePlayed - | Action::UnsubscribePodcast => { + | Action::UnsubscribePodcast + | Action::TogglePodcastShare => { state.status_message = Some(format!( "open the Podcasts tab ({}) for this command", Tab::Podcasts.index() + 1 @@ -2050,9 +2077,7 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) { let total = if state.search.similarity_source.is_some() { state.search.similarity_len() } else { - state.search.results.as_ref().map_or(0, |r| r.len()) - + state.search.fed_artists.len() - + state.search.fed_tracks.len() + state.search.total_len() } as isize; if total == 0 { return; @@ -2176,9 +2201,7 @@ fn current_view_len(state: &AppState) -> usize { if state.search.similarity_source.is_some() { state.search.similarity_len() } else { - state.search.results.as_ref().map_or(0, |r| r.len()) - + state.search.fed_artists.len() - + state.search.fed_tracks.len() + state.search.total_len() } } Some(GlobalView::FedArtist { .. }) => fed_card_len(state), @@ -2614,6 +2637,10 @@ fn select_current(state: &mut AppState) -> Option { state.active_tab = Tab::Global; Some(Effect::FedOpenArtist(name)) } + Outcome::OpenPodcastCard(index) => { + podcast_discovery::open_card(state, index); + None + } Outcome::OpenFedRelease { name, card, index } => { let start = if card.releases.get(index).is_some_and(|release| { @@ -2855,6 +2882,7 @@ enum Outcome { start: usize, }, OpenFedArtist(String), + OpenPodcastCard(usize), OpenFedRelease { name: String, card: crate::federation::FedArtistCard, @@ -2865,7 +2893,8 @@ enum Outcome { } /// Enter inside the federated section of the search results: artists open -/// their card, tracks queue up like a local track list. +/// their card, tracks queue up like a local track list, podcasts open their +/// public card. fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome { let artists = &state.search.fed_artists; if fed_index < artists.len() { @@ -2873,7 +2902,12 @@ fn fed_outcome(state: &AppState, fed_index: usize) -> Outcome { } let start = fed_index - artists.len(); if start >= state.search.fed_tracks.len() { - return Outcome::Nothing; + let podcast = start - state.search.fed_tracks.len(); + return if podcast < state.search.fed_podcasts.len() { + Outcome::OpenPodcastCard(podcast) + } else { + Outcome::Nothing + }; } Outcome::Play { tracks: state @@ -3378,6 +3412,7 @@ fn not_yet(state: &mut AppState, what: &str) { state.status_message = Some(format!("{what}: coming in a later milestone")); } +pub(crate) mod podcast_discovery; pub(crate) mod podcasts; #[cfg(test)] diff --git a/src/app/update/podcast_discovery.rs b/src/app/update/podcast_discovery.rs new file mode 100644 index 0000000..0e6b2ff --- /dev/null +++ b/src/app/update/podcast_discovery.rs @@ -0,0 +1,135 @@ +//! Public podcast cards opened from the search results. +//! +//! The card shows what its publishers claim. "Подписаться" subscribes with +//! the card's origin feed URL only (`Effect::PodcastSubscribeFromCard`); the +//! subscription is private and its metadata comes from the feed. When this +//! device is already subscribed (under the card's id or a feed alias), the +//! action reads «Уже подписан» and Enter opens the podcast instead. + +use crossterm::event::KeyCode; +use music_dht::podcast::PodcastId; + +use super::Effect; +use super::podcasts::{display_title, reveal_podcast, store_ready}; +use crate::app::state::{AppState, Loadable, Popup}; +use crate::podcasts::discovery::{FeedPreview, PodcastHit}; + +/// Lines a page key scrolls the card by. +const PAGE: u16 = 10; + +/// Enter on a podcast hit of the search results. +pub fn open_card(state: &mut AppState, index: usize) { + let Some(hit) = state.search.fed_podcasts.get(index) else { + return; + }; + state.popup = Some(Popup::PodcastCard { + hit: Box::new(hit.clone()), + preview: None, + scroll: 0, + max_scroll: std::cell::Cell::new(u16::MAX), + }); +} + +/// A key in [`Popup::PodcastCard`] (the popup was taken out of the state): +/// Enter/s subscribes (or opens the subscribed podcast), `e` reads the +/// episodes from the feed, arrows and page keys scroll, Esc/q closes. +pub fn card_key(state: &mut AppState, popup: Popup, key: KeyCode) -> Option { + let Popup::PodcastCard { + hit, + mut preview, + mut scroll, + max_scroll, + } = popup + else { + state.popup = Some(popup); + return None; + }; + let mut effect = None; + match key { + KeyCode::Esc | KeyCode::Char('q') => return None, + KeyCode::Enter | KeyCode::Char('s') => { + if let Some(id) = subscribed_id(state, &hit, preview.as_ref()) { + // «Уже подписан»: show the subscription instead. + return reveal_podcast(state, id, hit.title()); + } + effect = subscribe(state, &hit); + } + KeyCode::Char('e') => { + if !matches!(preview, Some(Loadable::Loading | Loadable::Ready(_))) { + preview = Some(Loadable::Loading); + effect = Some(Effect::PodcastPreview { + podcast_id: hit.hit.podcast_id.clone(), + feed_url: hit.hit.feed_url.clone(), + }); + } + } + KeyCode::Up | KeyCode::Char('k') => scroll = scroll.saturating_sub(1), + KeyCode::Down | KeyCode::Char('j') => scroll = scroll.saturating_add(1), + KeyCode::PageUp => scroll = scroll.saturating_sub(PAGE), + KeyCode::PageDown => scroll = scroll.saturating_add(PAGE), + KeyCode::Home | KeyCode::Char('g') => scroll = 0, + _ => {} + } + // Never past the end the last draw measured, so scrolling back moves + // the view at once. + scroll = scroll.min(max_scroll.get()); + state.popup = Some(Popup::PodcastCard { + hit, + preview, + scroll, + max_scroll, + }); + effect +} + +/// The id under which this device is subscribed to the card's podcast: the +/// card's id, the id its origin URL resolves to here (a feed alias), or — +/// once the feed was read — the podcast followed under the newer address +/// the feed moved to. +pub fn subscribed_id( + state: &AppState, + hit: &PodcastHit, + preview: Option<&Loadable>, +) -> Option { + let moved = match preview { + Some(Loadable::Ready(preview)) => preview.subscribed_as.as_ref(), + _ => None, + }; + hit.ids() + .into_iter() + .chain(moved) + .find(|id| state.podcasts.podcast(id).is_some()) + .cloned() +} + +fn subscribe(state: &mut AppState, hit: &PodcastHit) -> Option { + if !store_ready(state) { + return None; + } + if state.podcasts.subscribing { + state.status_message = Some("podcasts: a subscription is already being added".into()); + return None; + } + state.podcasts.subscribing = true; + state.status_message = Some(format!( + "podcasts: reading the feed of {}…", + display_title(hit.title()) + )); + Some(Effect::PodcastSubscribeFromCard(Box::new(hit.hit.clone()))) +} + +/// The preview of an open card arrived (ignored when another card is open). +pub fn preview_loaded( + state: &mut AppState, + podcast_id: PodcastId, + result: Result, String>, +) { + if let Some(Popup::PodcastCard { hit, preview, .. }) = &mut state.popup + && hit.hit.podcast_id == podcast_id + { + *preview = Some(match result { + Ok(feed) => Loadable::Ready(*feed), + Err(err) => Loadable::Failed(err), + }); + } +} diff --git a/src/app/update/podcasts.rs b/src/app/update/podcasts.rs index 7679718..188e522 100644 --- a/src/app/update/podcasts.rs +++ b/src/app/update/podcasts.rs @@ -18,6 +18,7 @@ use crate::app::state::{ PODCAST_EPISODE_PAGE, PodcastListRow, PodcastRow, PodcastStoreStatus, Popup, SeasonRow, Tab, }; use crate::library::models::TrackItem; +use crate::podcasts::publication::{Publication, PublicationReport, ShareCheck, warning_ru}; use crate::podcasts::refresh::{RefreshReport, SubscribeReport}; /// The next page starts loading when the cursor is this close to the end. @@ -59,13 +60,14 @@ pub(super) fn handle(state: &mut AppState, action: &Action) -> Option cycle_played_filter(state), Action::ToggleEpisodePlayed => toggle_played(state), Action::RestartEpisode => restart_selected(state), + Action::TogglePodcastShare => toggle_share(state), _ => return None, }; Some(effect) } /// Whether podcast actions may run; explains why not in the status bar. -fn store_ready(state: &mut AppState) -> bool { +pub(super) fn store_ready(state: &mut AppState) -> bool { match &state.podcasts.store { PodcastStoreStatus::Ready => true, PodcastStoreStatus::Opening => { @@ -369,6 +371,15 @@ pub fn reveal_episode_podcast(state: &mut AppState, track: &TrackItem) -> Option let episode = track.podcast_episode_ref()?; let podcast_id = episode.podcast_id.clone(); let title = episode.podcast_title.clone(); + reveal_podcast(state, podcast_id, &title) +} + +/// Switches to the Podcasts tab and opens a subscribed podcast. +pub(super) fn reveal_podcast( + state: &mut AppState, + podcast_id: PodcastId, + title: &str, +) -> Option { state.active_tab = Tab::Podcasts; state.help_visible = false; state.track_selection.clear(); @@ -398,7 +409,7 @@ pub fn reveal_episode_podcast(state: &mut AppState, track: &TrackItem) -> Option None => { state.status_message = Some(format!( "podcasts: {} is not subscribed on this device", - display_title(&title) + display_title(title) )); None } @@ -616,6 +627,237 @@ fn confirm_unsubscribe(state: &mut AppState) { }); } +/// Shift-F: share the selected or opened podcast in the federation, or stop +/// sharing it. +/// +/// Turning sharing on first checks the card it would publish +/// ([`Effect::PodcastPrepareShare`]); a feed URL that looks private asks for +/// confirmation in [`Popup::ConfirmPodcastShare`] ([`share_prepared`]). +/// A shared podcast whose publication is held back on this device (the URL +/// changed or was shared from another device) asks again instead of turning +/// sharing off; the dialog can still turn it off. +fn toggle_share(state: &mut AppState) -> Option { + if !store_ready(state) { + return None; + } + let Some(row) = state.podcasts.current_podcast() else { + state.status_message = Some("podcasts: select a podcast to share".into()); + return None; + }; + let podcast_id = row.id().clone(); + let title = row.title().to_string(); + let shared = row.summary.shared; + if state.podcasts.share_busy.contains(&podcast_id) { + state.status_message = Some("podcasts: still saving the share setting".into()); + return None; + } + let held = matches!( + state.podcasts.publication.podcasts.get(&podcast_id), + Some(Publication::HeldBack(_)) + ); + state.podcasts.share_busy.insert(podcast_id.clone()); + if shared && !held { + state.status_message = Some(format!( + "podcasts: stopping sharing {}…", + display_title(&title) + )); + return Some(Effect::PodcastSetShared { + podcast_id, + title, + shared: false, + confirm: None, + }); + } + Some(Effect::PodcastPrepareShare { podcast_id, title }) +} + +/// The card check of [`toggle_share`] arrived: share right away, or ask +/// about the privacy warnings first. +pub fn share_prepared( + state: &mut AppState, + podcast_id: PodcastId, + title: String, + result: Result, +) -> Option { + let check = match result { + Ok(check) => check, + Err(err) => { + state.podcasts.share_busy.remove(&podcast_id); + state.status_message = Some(format!("podcasts: cannot share: {err}")); + return None; + } + }; + let already_shared = state + .podcasts + .podcast(&podcast_id) + .is_some_and(|row| row.summary.shared); + if check.needs_confirmation() || (already_shared && !check.warnings.is_empty()) { + // Wait for the user; nothing is written until they confirm. + state.podcasts.share_busy.remove(&podcast_id); + state.popup = Some(Popup::ConfirmPodcastShare { + podcast_id, + title, + urls: check.urls, + warnings: check.warnings.iter().map(warning_ru).collect(), + already_shared, + }); + return None; + } + if already_shared { + // Held back without warnings any more: the next pass publishes it. + state.podcasts.share_busy.remove(&podcast_id); + state.status_message = Some(format!( + "podcasts: {} is shared; publishing…", + display_title(&title) + )); + return Some(Effect::PodcastPublicationSync); + } + state.status_message = Some(format!("podcasts: sharing {}…", display_title(&title))); + Some(Effect::PodcastSetShared { + podcast_id, + title, + shared: true, + // Already confirmed earlier for exactly these URLs (or no warning). + confirm: None, + }) +} + +/// A key in [`Popup::ConfirmPodcastShare`]: Enter/y publishes exactly the +/// URLs shown (and remembers that confirmation on this device), `x` stops +/// sharing a podcast that is already shared, Esc/n/q cancels (nothing is +/// written). Other keys keep the dialog open. +pub fn share_confirmation_key( + state: &mut AppState, + popup: Popup, + code: crossterm::event::KeyCode, +) -> Option { + use crossterm::event::KeyCode; + let Popup::ConfirmPodcastShare { + podcast_id, + title, + urls, + warnings, + already_shared, + } = popup + else { + state.popup = Some(popup); + return None; + }; + match code { + KeyCode::Enter | KeyCode::Char('y') => { + state.podcasts.share_busy.insert(podcast_id.clone()); + state.status_message = Some(format!("podcasts: sharing {}…", display_title(&title))); + Some(Effect::PodcastSetShared { + podcast_id, + title, + shared: true, + confirm: Some(urls), + }) + } + KeyCode::Char('x') if already_shared => { + state.podcasts.share_busy.insert(podcast_id.clone()); + state.status_message = Some(format!( + "podcasts: stopping sharing {}…", + display_title(&title) + )); + Some(Effect::PodcastSetShared { + podcast_id, + title, + shared: false, + confirm: None, + }) + } + KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => { + state.status_message = Some(if already_shared { + "podcasts: not published from this device until you confirm".into() + } else { + "podcasts: the podcast stays private".into() + }); + None + } + _ => { + state.popup = Some(Popup::ConfirmPodcastShare { + podcast_id, + title, + urls, + warnings, + already_shared, + }); + None + } + } +} + +/// The share decision was written; the reconciler publishes or revokes. +pub fn share_set( + state: &mut AppState, + podcast_id: PodcastId, + title: String, + shared: bool, + result: Result<(), String>, +) -> Option { + state.podcasts.share_busy.remove(&podcast_id); + let title = display_title(&title); + match result { + Ok(()) if shared => { + state.status_message = Some(format!( + "podcasts: {title} is shared in the federation (publishing from this device)" + )); + Some(Effect::PodcastsReload) + } + Ok(()) => { + note_revocation(state, &podcast_id); + state.status_message = Some(format!( + "podcasts: {title} is private again; the public card is being revoked" + )); + Some(Effect::PodcastsReload) + } + Err(err) => { + state.status_message = Some(format!("podcasts: cannot change sharing: {err}")); + None + } + } +} + +/// Sharing of `podcast_id` was turned off or it was unsubscribed here: its +/// card counts as not revoked until a pass says otherwise +/// (`PublicationReport::note_revocation`; the row is not reloaded yet, so it +/// still says whether the podcast was shared). +fn note_revocation(state: &mut AppState, podcast_id: &PodcastId) { + let was_shared = state + .podcasts + .podcast(podcast_id) + .is_some_and(|row| row.summary.shared); + state + .podcasts + .publication + .note_revocation(podcast_id, was_shared); +} + +/// A publication reconciler pass finished. Errors go to the status bar +/// when they are new (the log has them too). +pub fn publication_reported(state: &mut AppState, report: PublicationReport) { + let previous = std::mem::replace(&mut state.podcasts.publication, report); + let report = &state.podcasts.publication; + if let Some(error) = &report.error + && previous.error.as_ref() != Some(error) + { + state.status_message = Some(format!("podcasts: public discovery: {error}")); + } + for (podcast_id, publication) in &report.podcasts { + if let Publication::Failed(error) = publication + && previous.podcasts.get(podcast_id) != Some(publication) + { + let title = state + .podcasts + .podcast(podcast_id) + .map(|row| display_title(row.title())) + .unwrap_or_else(|| "a shared podcast".to_string()); + state.status_message = Some(format!("podcasts: cannot publish {title}: {error}")); + } + } +} + fn refresh_targets(state: &mut AppState, rows: Vec<&PodcastRow>) -> Vec<(PodcastId, FeedUrl)> { rows.into_iter() .filter(|row| !state.podcasts.refreshing.contains(row.id())) @@ -859,7 +1101,7 @@ pub fn subscribed(state: &mut AppState, result: Result) } } -fn display_title(title: &str) -> String { +pub(super) fn display_title(title: &str) -> String { if title.trim().is_empty() { "the podcast".to_string() } else { @@ -926,6 +1168,7 @@ pub fn unsubscribed( ) -> Option { match result { Ok(()) => { + note_revocation(state, &podcast_id); state.status_message = Some(format!( "podcasts: unsubscribed from {}; progress and history are kept", display_title(&title) diff --git a/src/app/update_tests.rs b/src/app/update_tests.rs index 59812d2..a3e3a16 100644 --- a/src/app/update_tests.rs +++ b/src/app/update_tests.rs @@ -1834,6 +1834,335 @@ mod podcast_tab { Some(Effect::PodcastLoadEpisodes { .. }) )); } + + // --- Sharing in the federation (MR-T3) --- + + use crate::podcasts::publication::{ + ConfirmedUrls, Discovery, Publication, PublicationReport, ShareCheck, + }; + use crossterm::event::KeyCode; + + fn urls() -> ConfirmedUrls { + ConfirmedUrls { + feed_url: FeedUrl::parse("https://example.test/feed.xml").unwrap(), + id_feed_url: None, + } + } + + fn check(warnings: bool, confirmed: bool) -> ShareCheck { + ShareCheck { + urls: urls(), + warnings: if warnings { + vec![ + music_dht::podcast_dht::FeedUrlPrivacyWarning::SecretQueryParameter { + name: "token".into(), + }, + ] + } else { + Vec::new() + }, + confirmed, + } + } + + fn set_shared(shared: bool, confirm: Option) -> Option { + Some(Effect::PodcastSetShared { + podcast_id: podcast_id(), + title: "Show".into(), + shared, + confirm, + }) + } + + fn shared_row() -> crate::app::state::PodcastRow { + let mut row = fx::row("Show", 0, 1, 0); + row.summary.shared = true; + row + } + + #[test] + fn sharing_a_podcast_checks_its_card_first_and_shares_without_warnings() { + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + assert_eq!( + update(&mut state, Action::TogglePodcastShare), + Some(Effect::PodcastPrepareShare { + podcast_id: podcast_id(), + title: "Show".into(), + }) + ); + // A second press while the check runs does nothing. + assert_eq!(update(&mut state, Action::TogglePodcastShare), None); + let effect = podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(false, false)), + ); + assert_eq!(effect, set_shared(true, None)); + assert!(state.popup.is_none()); + // Also inside the opened podcast. + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + let (seq, _) = episode_request(update(&mut state, Action::Select)); + podcasts::episodes_loaded(&mut state, seq, 0, Ok(page(1))); + assert!(matches!( + update(&mut state, Action::TogglePodcastShare), + Some(Effect::PodcastPrepareShare { .. }) + )); + // Already confirmed for exactly these URLs: no dialog. + let effect = podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(true, true)), + ); + assert_eq!(effect, set_shared(true, None)); + } + + #[test] + fn a_private_looking_feed_url_is_shared_only_after_confirmation() { + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + update(&mut state, Action::TogglePodcastShare); + let effect = podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(true, false)), + ); + assert_eq!(effect, None, "nothing is written before the user confirms"); + let Some(Popup::ConfirmPodcastShare { + warnings, + already_shared, + urls: shown, + .. + }) = &state.popup + else { + panic!("expected the share confirmation, got {:?}", state.popup); + }; + assert_eq!( + warnings, + &vec!["в адресе ленты есть параметр «token», похожий на ключ доступа".to_string()] + ); + assert!(!already_shared); + assert_eq!(shown, &urls()); + assert!(state.podcasts.share_busy.is_empty()); + + // Esc keeps the podcast private; other keys keep the dialog. + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Char('j')), + None + ); + assert!(state.popup.is_some()); + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Char('x')), + None + ); + assert!(state.popup.is_some(), "x only stops an existing share"); + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Esc), + None + ); + assert!(state.popup.is_none()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("stays private")) + ); + + // Enter confirms exactly the URLs shown. + update(&mut state, Action::TogglePodcastShare); + podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(true, false)), + ); + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Enter), + set_shared(true, Some(urls())) + ); + assert!(state.podcasts.share_busy.contains(&podcast_id())); + assert_eq!( + podcasts::share_set(&mut state, podcast_id(), "Show".into(), true, Ok(())), + Some(Effect::PodcastsReload) + ); + assert!(state.podcasts.share_busy.is_empty()); + } + + #[test] + fn stopping_sharing_needs_no_dialog_and_a_held_publication_asks_again() { + let mut state = ready(vec![shared_row()]); + assert_eq!( + update(&mut state, Action::TogglePodcastShare), + set_shared(false, None) + ); + state + .podcasts + .publication + .podcasts + .insert(podcast_id(), Publication::Published); + let effect = podcasts::share_set(&mut state, podcast_id(), "Show".into(), false, Ok(())); + assert_eq!(effect, Some(Effect::PodcastsReload)); + assert!(state.podcasts.publication.podcasts.is_empty()); + assert!(state.podcasts.publication.unrevoked.contains(&podcast_id())); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("private again")) + ); + + // The federation is off (since the start, or the app was restarted + // since the card was published): no pass knows whether a card from + // an earlier session is live, so one that was shared counts as not + // revoked yet, for share-off and unsubscribe alike. + for discovery in [ + Discovery::Off("federation is off".into()), + Discovery::Starting, + ] { + let mut state = ready(vec![shared_row()]); + state.podcasts.publication.discovery = discovery.clone(); + podcasts::share_set(&mut state, podcast_id(), "Show".into(), false, Ok(())); + assert!(state.podcasts.publication.unrevoked.contains(&podcast_id())); + let mut state = ready(vec![shared_row()]); + state.podcasts.publication.discovery = discovery.clone(); + podcasts::unsubscribed(&mut state, podcast_id(), "Show".into(), Ok(())); + assert!(state.podcasts.publication.unrevoked.contains(&podcast_id())); + // A podcast that was not shared has no card to revoke. + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + state.podcasts.publication.discovery = discovery; + podcasts::unsubscribed(&mut state, podcast_id(), "Show".into(), Ok(())); + assert!(state.podcasts.publication.unrevoked.is_empty()); + } + + // Shared (for example from another device) but held back here: the + // key asks for this device's confirmation instead of unsharing. + let mut state = ready(vec![shared_row()]); + state.podcasts.publication.podcasts.insert( + podcast_id(), + Publication::HeldBack(check(true, false).warnings), + ); + assert!(matches!( + update(&mut state, Action::TogglePodcastShare), + Some(Effect::PodcastPrepareShare { .. }) + )); + podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(true, false)), + ); + assert!(matches!( + &state.popup, + Some(Popup::ConfirmPodcastShare { + already_shared: true, + .. + }) + )); + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Char('x')), + set_shared(false, None) + ); + // Or confirms it (sharing is already on; the confirmation is new). + state.podcasts.share_busy.clear(); + podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(true, false)), + ); + let popup = state.popup.take().unwrap(); + assert_eq!( + podcasts::share_confirmation_key(&mut state, popup, KeyCode::Char('y')), + set_shared(true, Some(urls())) + ); + // Held without warnings any more: just reconcile again. + state.podcasts.share_busy.clear(); + assert_eq!( + podcasts::share_prepared( + &mut state, + podcast_id(), + "Show".into(), + Ok(check(false, false)) + ), + Some(Effect::PodcastPublicationSync) + ); + } + + #[test] + fn share_errors_and_publication_failures_are_shown() { + let mut state = ready(vec![fx::row("Show", 0, 1, 0)]); + update(&mut state, Action::TogglePodcastShare); + assert_eq!( + podcasts::share_prepared(&mut state, podcast_id(), "Show".into(), Err("boom".into())), + None + ); + assert!(state.podcasts.share_busy.is_empty()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("cannot share: boom")) + ); + assert_eq!( + podcasts::share_set( + &mut state, + podcast_id(), + "Show".into(), + true, + Err("db".into()) + ), + None + ); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("cannot change sharing: db")) + ); + + state.status_message = None; + let mut report = PublicationReport { + discovery: Discovery::Running { peers: 3 }, + ..PublicationReport::default() + }; + report + .podcasts + .insert(podcast_id(), Publication::Failed("signing failed".into())); + podcasts::publication_reported(&mut state, report.clone()); + assert!( + state + .status_message + .as_deref() + .is_some_and(|m| m.contains("cannot publish \"Show\": signing failed")) + ); + // The same failure again is not repeated in the status bar. + state.status_message = None; + podcasts::publication_reported(&mut state, report); + assert_eq!(state.status_message, None); + assert_eq!( + state.podcasts.publication.discovery, + Discovery::Running { peers: 3 } + ); + } + + #[test] + fn share_actions_outside_the_tab_only_explain() { + let mut state = fx::ready_state(vec![fx::row("Show", 0, 1, 0)]); + state.active_tab = Tab::Global; + assert_eq!(update(&mut state, Action::TogglePodcastShare), None); + assert!(state.podcasts.share_busy.is_empty()); + let mut opening = AppState { + active_tab: Tab::Podcasts, + ..AppState::default() + }; + assert_eq!(update(&mut opening, Action::TogglePodcastShare), None); + } } /// Podcast episodes in the shared queue: identity by episode id (never the @@ -2220,3 +2549,267 @@ mod podcast_queue { assert!(crate::app::update::podcasts::synced(&mut opening, Vec::new(), true).is_empty()); } } + +mod podcast_discovery { + use crossterm::event::KeyCode; + use music_dht::podcast::{FeedUrl, PodcastId}; + use music_dht::podcast_dht::ClaimedFeedUrl; + + use super::*; + use crate::app::podcast_fixtures::{self as fx, podcast_id}; + use crate::app::state::{Loadable, PODCAST_ACTION_ROWS, Popup}; + use crate::app::update::podcast_discovery::{card_key, preview_loaded}; + use crate::podcasts::discovery::{FeedPreview, PodcastHit, test_hit}; + + const ORIGIN: &str = "https://feeds.example.test/origin.xml"; + const CLAIMED: &str = "https://elsewhere.example.test/claimed.xml"; + + /// A card whose publisher also claims the feed moved elsewhere. + fn claimed_hit() -> PodcastHit { + let mut hit = test_hit(ORIGIN, "Rust Radio"); + let claimed = FeedUrl::parse(CLAIMED).unwrap(); + hit.claimed_feed_urls = vec![ClaimedFeedUrl { + feed_url: claimed.clone(), + publishers: hit.publishers.clone(), + }]; + hit.card.feed_url = claimed; + hit.card.id_feed_url = Some(FeedUrl::parse(ORIGIN).unwrap()); + PodcastHit::new(hit) + } + + /// The search view with one local track, a federated artist and the + /// given podcast hits; the podcast store is ready with `rows`. + fn search_state(rows: Vec, hits: Vec) -> AppState { + let mut state = fx::ready_state(rows); + state.active_tab = Tab::Global; + state.search.query = "rust radio".into(); + state.search.results = Some(crate::library::models::SearchResults { + tracks: vec![test_track(1)], + ..Default::default() + }); + state.search.fed_artists = vec![crate::federation::FedArtistHit { + name: "Rust Radio Band".into(), + peers: 2, + }]; + state.search.fed_podcasts = hits; + state.global.stack.push(GlobalView::Search { cursor: 0 }); + state + } + + fn open_card_at(state: &mut AppState, cursor: usize) -> Popup { + if let Some(GlobalView::Search { cursor: at }) = state.global.stack.last_mut() { + *at = cursor; + } + assert_eq!(update(state, Action::Select), None); + state.popup.take().expect("the card opens") + } + + #[test] + fn the_cursor_reaches_podcast_hits_after_the_music_results() { + let mut state = search_state(Vec::new(), vec![claimed_hit()]); + assert_eq!(state.search.total_len(), 3); + update(&mut state, Action::SelectLast); + assert!(matches!( + state.global.stack.last(), + Some(GlobalView::Search { cursor: 2 }) + )); + // Enter on a podcast opens its card; nothing is played or queued. + assert_eq!(update(&mut state, Action::Select), None); + assert!(state.player.queue.is_empty()); + assert!(matches!( + &state.popup, + Some(Popup::PodcastCard { hit, preview: None, scroll: 0, .. }) if hit.title() == "Rust Radio" + )); + // The music rows keep their behavior. + let mut state = search_state(Vec::new(), vec![claimed_hit()]); + assert_eq!( + update(&mut state, Action::Select), + Some(Effect::PlayCurrent) + ); + } + + #[test] + fn subscribing_from_a_card_uses_the_origin_url_only() { + let mut state = search_state(Vec::new(), vec![claimed_hit()]); + let popup = open_card_at(&mut state, 2); + let effect = card_key(&mut state, popup, KeyCode::Enter); + let Some(Effect::PodcastSubscribeFromCard(hit)) = effect else { + panic!("expected a subscription, got {effect:?}"); + }; + assert_eq!(hit.feed_url.as_str(), ORIGIN); + assert_eq!( + hit.podcast_id, + PodcastId::from_feed_url(&FeedUrl::parse(ORIGIN).unwrap()) + ); + assert!(state.podcasts.subscribing); + assert!(matches!(state.popup, Some(Popup::PodcastCard { .. }))); + + // One subscription at a time. + let popup = state.popup.take().unwrap(); + assert_eq!(card_key(&mut state, popup, KeyCode::Char('s')), None); + assert!( + state + .status_message + .as_deref() + .is_some_and(|message| message.contains("already being added")) + ); + + // Esc closes without anything else. + state.podcasts.subscribing = false; + let popup = state.popup.take().unwrap(); + assert_eq!(card_key(&mut state, popup, KeyCode::Esc), None); + assert!(state.popup.is_none()); + + // Without a podcast store nothing is requested. + let mut state = search_state(Vec::new(), vec![claimed_hit()]); + state.podcasts.store = crate::app::state::PodcastStoreStatus::Opening; + let popup = open_card_at(&mut state, 2); + assert_eq!(card_key(&mut state, popup, KeyCode::Enter), None); + assert!(!state.podcasts.subscribing); + } + + #[test] + fn already_subscribed_cards_open_the_podcast_instead() { + // Subscribed under the card's own id. + let mut hit = test_hit("https://example.test/feed.xml", "Test Show"); + hit.card.episode_count = None; + let mut state = search_state( + vec![fx::row("Test Show", 0, 3, 0)], + vec![PodcastHit::new(hit)], + ); + assert!(state.podcasts.subscribed_to(&state.search.fed_podcasts[0])); + let popup = open_card_at(&mut state, 2); + let effect = card_key(&mut state, popup, KeyCode::Enter); + assert!( + matches!(&effect, Some(Effect::PodcastLoadEpisodes { query, .. }) if query.podcast_id == podcast_id()), + "{effect:?}" + ); + assert_eq!(state.active_tab, Tab::Podcasts); + assert_eq!(state.podcasts.selected, PODCAST_ACTION_ROWS); + assert!(state.popup.is_none()); + assert!(!state.podcasts.subscribing); + + // Subscribed under another id: the card's origin URL is a feed + // alias of that podcast here (resolved by the search task). + let mut aliased = claimed_hit(); + assert_ne!(aliased.hit.podcast_id, podcast_id()); + aliased.local_id = podcast_id(); + let mut state = search_state(vec![fx::row("Test Show", 0, 3, 0)], vec![aliased]); + assert!(state.podcasts.subscribed_to(&state.search.fed_podcasts[0])); + let popup = open_card_at(&mut state, 2); + let effect = card_key(&mut state, popup, KeyCode::Enter); + assert!( + !matches!(effect, Some(Effect::PodcastSubscribeFromCard(_))), + "{effect:?}" + ); + assert_eq!(state.active_tab, Tab::Podcasts); + + // Subscribed under the newer address the card's feed moved to: + // known once the feed was read (`e`), then Enter opens it too. + let unknown = claimed_hit(); + assert!(!unknown.ids().contains(&&podcast_id())); + let mut state = search_state(vec![fx::row("Test Show", 0, 3, 0)], vec![unknown]); + assert!(!state.podcasts.subscribed_to(&state.search.fed_podcasts[0])); + let popup = open_card_at(&mut state, 2); + let card_id = state.search.fed_podcasts[0].hit.podcast_id.clone(); + assert!(matches!( + card_key(&mut state, popup, KeyCode::Char('e')), + Some(Effect::PodcastPreview { .. }) + )); + preview_loaded( + &mut state, + card_id, + Ok(Box::new(FeedPreview { + title: "Test Show".into(), + episode_count: 1, + seasons: Vec::new(), + episodes: Vec::new(), + moved_to: Some(FeedUrl::parse("https://example.test/feed.xml").unwrap()), + subscribed_as: Some(podcast_id()), + })), + ); + let popup = state.popup.take().unwrap(); + let effect = card_key(&mut state, popup, KeyCode::Enter); + assert!( + matches!(&effect, Some(Effect::PodcastLoadEpisodes { query, .. }) if query.podcast_id == podcast_id()), + "{effect:?}" + ); + assert!(!state.podcasts.subscribing); + + // Not subscribed: an unsubscribed podcast is not in the rows. + let state = search_state(Vec::new(), vec![claimed_hit()]); + assert!(!state.podcasts.subscribed_to(&state.search.fed_podcasts[0])); + } + + #[test] + fn the_episode_preview_is_requested_once_and_folded_in() { + let mut state = search_state(Vec::new(), vec![claimed_hit()]); + let popup = open_card_at(&mut state, 2); + let effect = card_key(&mut state, popup, KeyCode::Char('e')); + let Some(Effect::PodcastPreview { + podcast_id: card_id, + feed_url, + }) = effect + else { + panic!("expected a preview request, got {effect:?}"); + }; + assert_eq!(feed_url.as_str(), ORIGIN, "never the claimed URL"); + let popup = state.popup.take().unwrap(); + assert_eq!(card_key(&mut state, popup, KeyCode::Char('e')), None); + assert!(matches!( + state.popup, + Some(Popup::PodcastCard { + preview: Some(Loadable::Loading), + .. + }) + )); + // Scrolling keeps the card. + let popup = state.popup.take().unwrap(); + assert_eq!(card_key(&mut state, popup, KeyCode::Down), None); + assert!(matches!( + state.popup, + Some(Popup::PodcastCard { scroll: 1, .. }) + )); + + let feed = FeedPreview { + title: "Rust Radio".into(), + episode_count: 0, + seasons: Vec::new(), + episodes: Vec::new(), + moved_to: None, + subscribed_as: None, + }; + // A preview of another card is ignored. + preview_loaded(&mut state, podcast_id(), Ok(Box::new(feed.clone()))); + assert!(matches!( + state.popup, + Some(Popup::PodcastCard { + preview: Some(Loadable::Loading), + .. + }) + )); + preview_loaded( + &mut state, + card_id.clone(), + Err("HTTP 404 Not Found".into()), + ); + assert!(matches!( + state.popup, + Some(Popup::PodcastCard { + preview: Some(Loadable::Failed(_)), + .. + }) + )); + // A failed preview may be asked again. + let popup = state.popup.take().unwrap(); + assert!(matches!( + card_key(&mut state, popup, KeyCode::Char('e')), + Some(Effect::PodcastPreview { .. }) + )); + preview_loaded(&mut state, card_id, Ok(Box::new(feed.clone()))); + assert!(matches!( + &state.popup, + Some(Popup::PodcastCard { preview: Some(Loadable::Ready(ready)), .. }) if *ready == feed + )); + } +} diff --git a/src/config/default_keymap.toml b/src/config/default_keymap.toml index 3935b3b..d544b68 100644 --- a/src/config/default_keymap.toml +++ b/src/config/default_keymap.toml @@ -319,3 +319,8 @@ context = "podcasts" key_sequence = "b" command = "RestartEpisode" context = "podcasts" + +[[keymaps]] +key_sequence = "shift-f" +command = "TogglePodcastShare" +context = "podcasts" diff --git a/src/config/keymap.rs b/src/config/keymap.rs index c6e6fe4..569bcc9 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -904,6 +904,7 @@ mod tests { (key!(m), Action::ToggleEpisodePlayed), (shift('U'), Action::UnsubscribePodcast), (key!(b), Action::RestartEpisode), + (shift('F'), Action::TogglePodcastShare), ]; for (key, action) in cases { assert_eq!( diff --git a/src/devices.rs b/src/devices.rs index 9e35cff..5ff0c01 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -142,6 +142,9 @@ struct PodcastLink { /// Other podcast state (subscriptions, shares, feed URLs, sessions) /// changed since the last notification. changed: bool, + /// Whether the podcast registers are current enough to publish public + /// cards from ([`DeviceSync::podcast_registers_current`]). + registers_current: tokio::sync::watch::Sender, } /// Converts queue items to and from the shared wire form @@ -844,13 +847,76 @@ impl DeviceSync { tracing::info!(applied, "applied podcast operations received earlier"); } self.flush_podcast_changes(); - Ok(()) + self.check_podcast_registers_without_peers() } fn podcast_store(&self) -> Option> { lock(&self.podcasts).store.clone() } + /// Becomes `true` once the share and subscription registers in the + /// attached podcast store are current enough to publish this device's + /// public podcast cards from, and stays `true` for the process. + /// + /// A device that was closed or offline may hold a "share" decision that + /// another trusted device revoked meanwhile. Its registers count as + /// current once it exchanged sync messages (in either direction) with a + /// trusted device that syncs podcasts (protocol v4 or newer) after the + /// store was attached, or when it has no such device it can reach (no + /// device in its group, only older ones, or none with an address): then + /// nobody can bring it a newer decision. Until then + /// `podcasts::publication` only revokes and publishes nothing. + pub fn podcast_registers_current(&self) -> tokio::sync::watch::Receiver { + lock(&self.podcasts).registers_current.subscribe() + } + + fn set_podcast_registers_current(&self, why: &str) { + let link = lock(&self.podcasts); + let opened = link.registers_current.send_if_modified(|current| { + let opened = !*current; + *current = true; + opened + }); + if opened { + tracing::info!("podcast share decisions are current: {why}"); + } + } + + /// A sync exchange with `device_id` was applied. `store_attached`: the + /// podcast store was attached before the received state was applied, so + /// the received podcast operations and snapshot sections reached it. + fn podcast_exchange_applied(&self, device_id: &str, store_attached: bool) -> Result<()> { + if store_attached + && music_dht::device_sync::supports_podcasts(self.peer_protocol_version(device_id)?) + { + self.set_podcast_registers_current("synced with a trusted device"); + } + Ok(()) + } + + /// Marks the podcast registers current when the store is attached and + /// no trusted device that syncs podcasts can be reached to bring newer + /// decisions (see [`DeviceSync::podcast_registers_current`]). + fn check_podcast_registers_without_peers(&self) -> Result<()> { + { + let link = lock(&self.podcasts); + if link.store.is_none() || *link.registers_current.borrow() { + return Ok(()); + } + } + for device in self.active_remote_devices()? { + if !device.endpoint_ticket.trim().is_empty() + && music_dht::device_sync::supports_podcasts( + self.peer_protocol_version(&device.device_id)?, + ) + { + return Ok(()); + } + } + self.set_podcast_registers_current("no trusted device syncs podcasts"); + Ok(()) + } + /// Merges a received podcast operation into the podcast store, or keeps /// it for [`DeviceSync::attach_podcast_store`]. A podcast store problem /// never fails music sync: the operation stays in the log either way. @@ -1097,7 +1163,9 @@ impl DeviceSync { } } self.apply_device_profiles(&devices, Some(&invite.device_id))?; + let store_attached = self.podcast_store().is_some(); self.apply_received(snapshot, ops)?; + self.podcast_exchange_applied(&invite.device_id, store_attached)?; self.record_local_op(SyncOpPayload::DeviceTrusted { target_device_id: invite.device_id.clone(), })?; @@ -1600,7 +1668,9 @@ impl DeviceSync { if let Some(playback) = playback { self.apply_playback_snapshot(&device.device_id, playback)?; } + let store_attached = self.podcast_store().is_some(); self.apply_received(snapshot, ops)?; + self.podcast_exchange_applied(&device.device_id, store_attached)?; self.note_peer_vector(&device.device_id, &vector)?; self.mark_seen(&device.device_id, Some(peer.to_string()))?; self.set_last_sync(Some(format!("synced {}", short_id(&device.device_id))))?; @@ -3404,6 +3474,10 @@ pub async fn sync_loop( if let Err(err) = sync.gc_tombstones() { tracing::debug!("personal sync cleanup failed: {err:#}"); } + // A device removed from the group is no longer waited for. + if let Err(err) = sync.check_podcast_registers_without_peers() { + tracing::debug!("podcast sync state not checked: {err:#}"); + } } Some(completed) = polls.join_next_with_id(), if !polls.is_empty() => { let (task, result) = match completed { @@ -3646,7 +3720,9 @@ async fn handle_pair_request( if let Some(playback) = playback { sync.apply_playback_snapshot(&profile.device_id, playback)?; } + let store_attached = sync.podcast_store().is_some(); sync.apply_received(snapshot, ops)?; + sync.podcast_exchange_applied(&profile.device_id, store_attached)?; sync.note_peer_vector(&profile.device_id, &vector)?; { let conn = lock(&sync.conn); @@ -3740,7 +3816,9 @@ async fn handle_hello( if let Some(playback) = playback { sync.apply_playback_snapshot(&profile.device_id, playback)?; } + let store_attached = sync.podcast_store().is_some(); sync.apply_received(snapshot, ops)?; + sync.podcast_exchange_applied(&profile.device_id, store_attached)?; sync.note_peer_vector(&profile.device_id, &vector)?; sync.mark_seen(&profile.device_id, Some(stream.peer_id.to_string()))?; sync.set_last_sync(Some(format!("synced {}", short_id(&profile.device_id))))?; diff --git a/src/devices/podcast_sync_tests.rs b/src/devices/podcast_sync_tests.rs index e17e718..e06bba3 100644 --- a/src/devices/podcast_sync_tests.rs +++ b/src/devices/podcast_sync_tests.rs @@ -111,7 +111,10 @@ pub(super) fn receive(to: &DeviceSync, json: &str) { to.apply_playback_snapshot(&profile.device_id, playback) .unwrap(); } + let store_attached = to.podcast_store().is_some(); to.apply_received(snapshot, ops).unwrap(); + to.podcast_exchange_applied(&profile.device_id, store_attached) + .unwrap(); to.note_peer_vector(&profile.device_id, &vector).unwrap(); } @@ -441,6 +444,83 @@ fn operations_received_before_the_store_opens_are_applied_when_it_does() { assert_eq!(pending, 0); } +/// Trusts `b` on `a` and the other way round like [`pair`], with an +/// address for each, so either can sync with the other. +fn pair_reachable(a: &Device, b: &Device) { + b.sync + .set_group_id(&a.sync.ensure_identity().unwrap().group_id) + .unwrap(); + for (left, right) in [(a, b), (b, a)] { + let mut profile = right.sync.own_profile("").unwrap(); + profile.endpoint_ticket = format!("ticket-of-{}", right.id); + left.sync + .apply_device_profile( + &profile, + true, + VersionReport::SelfReported(Some(profile.protocol_version)), + ) + .unwrap(); + } +} + +fn current(registers: &tokio::sync::watch::Receiver) -> bool { + *registers.borrow() +} + +/// Interop scenario 7c (a device restarted with a "share" decision another +/// device revoked meanwhile): the publication reconciler waits for +/// `podcast_registers_current`, which needs a sync with a device of the +/// group that syncs podcasts, unless there is none to wait for. +#[test] +fn podcast_registers_are_current_after_a_sync_with_the_group() { + // A device alone: current as soon as its store is attached. + let alone = device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), false); + let registers = alone.sync.podcast_registers_current(); + assert!(!current(®isters), "not before the store is attached"); + alone + .sync + .attach_podcast_store(Arc::clone(&alone.store)) + .unwrap(); + assert!(current(®isters)); + + // Only an older device (v3) in the group: it never brings podcast + // decisions, so it is not waited for. + let with_old = device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), false); + trust_old_device(&with_old); + with_old + .sync + .attach_podcast_store(Arc::clone(&with_old.store)) + .unwrap(); + assert!(current(&with_old.sync.podcast_registers_current())); + + // B restarts in a group with A: it waits for a sync with A. + let a = device(); + let b = device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), false); + pair_reachable(&a, &b); + let registers = b.sync.podcast_registers_current(); + // A sync that reaches B before its store is attached leaves the + // podcast snapshot sections out: it does not count. + deliver(&a, &b); + b.sync.attach_podcast_store(Arc::clone(&b.store)).unwrap(); + assert!(!current(®isters), "A can be reached: B waits for it"); + b.sync.check_podcast_registers_without_peers().unwrap(); + assert!(!current(®isters)); + // A's sync with B (`handle_hello`; B's own `sync_device` does the same + // with A's response) now merges A's decisions into the store. + deliver(&a, &b); + assert!(current(®isters)); + + // A device of the group removed meanwhile is no longer waited for. + let d = device_with_store(Arc::new(PodcastStore::open_in_memory().unwrap()), false); + pair_reachable(&a, &d); + d.sync.attach_podcast_store(Arc::clone(&d.store)).unwrap(); + let registers = d.sync.podcast_registers_current(); + assert!(!current(®isters)); + d.sync.revoke_device(&a.id).unwrap(); + d.sync.check_podcast_registers_without_peers().unwrap(); + assert!(current(®isters)); +} + #[test] fn podcast_operations_are_relayed_to_devices_that_were_not_online() { let a = device(); diff --git a/src/federation/capabilities.rs b/src/federation/capabilities.rs index 016eb48..dca64ea 100644 --- a/src/federation/capabilities.rs +++ b/src/federation/capabilities.rs @@ -10,6 +10,7 @@ use music_dht::capabilities::{ CAPABILITIES_PROTOCOL_VERSION, CapabilityManifest, CapabilityMessage, read_message, write_message, }; +use music_dht::podcast_dht::PodcastDht; use music_dht::{ByteStream, EndpointId, MusicDhtService, StreamAcceptor}; const PROBE_INTERVAL: Duration = Duration::from_secs(30); @@ -30,8 +31,10 @@ pub struct NewerProtocol { } impl ProtocolVersions { - pub fn snapshot(observed: &ObservedVersions) -> Self { - let local = local_manifest().protocols; + /// `podcast_dht`: whether this node runs the podcast-discovery overlay + /// (it is advertised only then). + pub fn snapshot(observed: &ObservedVersions, podcast_dht: bool) -> Self { + let local = local_manifest(podcast_dht).protocols; let observed_versions = lock(&observed.versions).clone(); let newer = observed_versions .iter() @@ -59,27 +62,35 @@ pub struct ObservedVersions { peers: Mutex>, } -fn local_manifest() -> CapabilityManifest { - CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION")) - .with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION) +/// This node's manifest. The podcast-discovery overlay is opt-in in FRID: +/// it is advertised only when this node serves it (`podcast_dht`), so +/// peers never dial an overlay that failed to open here. +fn local_manifest(podcast_dht: bool) -> CapabilityManifest { + let manifest = CapabilityManifest::frid("furumi", env!("CARGO_PKG_VERSION")) + .with_protocol("audio", super::audio::AUDIO_PROTOCOL_VERSION); + if podcast_dht { + manifest.with_podcast_dht() + } else { + manifest + } } -pub async fn serve(mut acceptor: StreamAcceptor) { +pub async fn serve(mut acceptor: StreamAcceptor, podcast_dht: bool) { while let Some(stream) = acceptor.accept().await { tokio::spawn(async move { - if let Err(error) = serve_one(stream).await { + if let Err(error) = serve_one(stream, podcast_dht).await { tracing::debug!("capability stream failed: {error:#}"); } }); } } -async fn serve_one(mut stream: ByteStream) -> Result<()> { +async fn serve_one(mut stream: ByteStream, podcast_dht: bool) -> Result<()> { let response = match read_message(&mut stream).await? { CapabilityMessage::Get { version: CAPABILITIES_PROTOCOL_VERSION, } => CapabilityMessage::Manifest { - manifest: local_manifest(), + manifest: local_manifest(podcast_dht), }, CapabilityMessage::Get { version } => CapabilityMessage::Error { message: format!("unsupported capability protocol {version}"), @@ -94,7 +105,14 @@ async fn serve_one(mut stream: ByteStream) -> Result<()> { Ok(()) } -pub async fn probe_loop(service: Arc, observed: Arc) { +/// Polls the manifests of known peers. With the podcast-discovery overlay +/// running, every manifest is handed to `PodcastDht::note_peer_capabilities`, +/// which stops dialing peers that do not serve the overlay. +pub async fn probe_loop( + service: Arc, + observed: Arc, + podcast_dht: Option>, +) { let mut interval = tokio::time::interval(PROBE_INTERVAL); loop { interval.tick().await; @@ -109,7 +127,8 @@ pub async fn probe_loop(service: Arc, observed: Arc>(); for peer in peers { - if let Err(error) = probe_peer(&service, peer, &observed).await { + if let Err(error) = probe_peer(&service, peer, &observed, podcast_dht.as_deref()).await + { tracing::trace!(%peer, "peer capability probe unavailable: {error:#}"); } } @@ -120,6 +139,7 @@ async fn probe_peer( service: &MusicDhtService, peer: EndpointId, observed: &ObservedVersions, + podcast_dht: Option<&PodcastDht>, ) -> Result<()> { let mut stream = service.open_stream(peer, CAPABILITIES_ALPN).await?; write_message( @@ -137,6 +157,11 @@ async fn probe_peer( anyhow::bail!("peer did not return a capability manifest"); }; manifest.validate()?; + if let Some(podcast_dht) = podcast_dht + && let Err(error) = podcast_dht.note_peer_capabilities(peer, &manifest).await + { + tracing::debug!(%peer, "podcast DHT capability note failed: {error:#}"); + } { let mut versions = lock(&observed.versions); for (id, version) in manifest.protocols { @@ -162,7 +187,7 @@ mod tests { #[test] fn local_manifest_lists_every_player_protocol() { - let manifest = local_manifest(); + let manifest = local_manifest(true); for id in [ "federation_net", "ticket", @@ -174,10 +199,31 @@ mod tests { "similarity_dht", "device_sync", "jam", + "podcast_dht", ] { assert!(manifest.protocols.contains_key(id), "missing {id}"); } manifest.validate().unwrap(); + assert!(manifest.supports_podcast_dht()); + } + + #[test] + fn podcast_discovery_is_advertised_only_when_it_runs() { + let manifest = local_manifest(false); + assert!(!manifest.supports_podcast_dht()); + assert!(!manifest.protocols.contains_key("podcast_dht")); + manifest.validate().unwrap(); + let observed = ObservedVersions::default(); + assert!( + !ProtocolVersions::snapshot(&observed, false) + .local + .contains_key("podcast_dht") + ); + assert!( + ProtocolVersions::snapshot(&observed, true) + .local + .contains_key("podcast_dht") + ); } #[test] @@ -185,7 +231,7 @@ mod tests { let observed = ObservedVersions::default(); lock(&observed.versions).insert("music_dht".to_string(), 99); lock(&observed.versions).insert("jam".to_string(), crate::jam::PROTOCOL_VERSION); - let snapshot = ProtocolVersions::snapshot(&observed); + let snapshot = ProtocolVersions::snapshot(&observed, true); assert_eq!(snapshot.newer.len(), 1); assert_eq!(snapshot.newer[0].id, "music_dht"); } diff --git a/src/federation/mod.rs b/src/federation/mod.rs index 019d857..0bea3f7 100644 --- a/src/federation/mod.rs +++ b/src/federation/mod.rs @@ -25,6 +25,7 @@ use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering}; use std::time::{Duration, Instant}; use anyhow::{Context, Result}; +use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht}; use music_dht::similarity_dht::SimilarityDht; use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN; use music_dht::{ @@ -44,6 +45,10 @@ pub use capabilities::ProtocolVersions; pub use catalog::{CATALOG_ALPN, FedAppearsOn, FedArtistCard, FedCardTrack, FedRelease}; pub use similarity::SIMILARITY_ALPN; +/// Cache of the public podcast-discovery overlay (`music_dht::podcast_dht`), +/// next to the similarity overlay's cache in the federation data directory. +pub const PODCAST_DHT_CACHE_FILE: &str = "podcast-dht.sqlite3"; + /// How often the published library is re-synchronized with the local index. const SYNC_INTERVAL: Duration = Duration::from_secs(60); /// How often the application checks whether the shared transport needs a @@ -412,6 +417,9 @@ pub struct NetworkLibrarySource { struct Running { service: Arc, similarity_dht: Arc, + /// Public podcast discovery; `None` when its cache could not be opened + /// (discovery is then off and the overlay is not advertised). + podcast_dht: Option>, network_name: String, network_id: NetworkId, tasks: Vec>, @@ -438,6 +446,9 @@ pub struct Federation { last_error: std::sync::Mutex>, transport_stats: Arc, observed_protocols: Arc, + /// The running podcast-discovery overlay, for the podcast publication + /// reconciler (`podcasts::publication`); `None` while federation is off. + podcast_dht: tokio::sync::watch::Sender>>, } #[derive(Debug, Clone)] @@ -572,9 +583,17 @@ impl Federation { last_error: std::sync::Mutex::new(initial_error), transport_stats: Arc::new(TransportStats::default()), observed_protocols: Arc::new(capabilities::ObservedVersions::default()), + podcast_dht: tokio::sync::watch::Sender::new(None), }) } + /// Follows the podcast-discovery overlay of the running node: `Some` + /// while federation runs with the overlay, `None` otherwise. Every + /// (re)start publishes a new value. + pub fn podcast_dht(&self) -> tokio::sync::watch::Receiver>> { + self.podcast_dht.subscribe() + } + pub fn settings(&self) -> FedSettings { lock(&self.settings).clone() } @@ -730,6 +749,7 @@ impl Federation { } else if recovery_only { return Ok(false); } + self.podcast_dht.send_replace(None); stop_running(guard.take()).await; } else if recovery_only { tracing::warn!(network = %network_name, "retrying stopped federation service"); @@ -756,6 +776,8 @@ impl Federation { .stream_protocol(crate::jam::JAM_ALPN) // Signed LSH summaries form their own upgrade-safe DHT overlay. .schema_independent_stream_protocol(SIMILARITY_DHT_ALPN) + // Public cards of podcasts the user chose to share. + .schema_independent_stream_protocol(PODCAST_DHT_ALPN) // Informational application/protocol versions. .schema_independent_stream_protocol(capabilities::CAPABILITIES_ALPN) .build() @@ -788,6 +810,18 @@ impl Federation { Arc::clone(&self.similarity), Arc::clone(&self.library), )); + // Public podcast discovery: serving, bootstrap, republishing and + // expiry run here; what to publish is decided by the podcast + // publication reconciler from the user's share decisions. + let mut podcast_dht_tasks = Vec::new(); + let podcast_dht = + open_podcast_dht(&service, &self.data_dir) + .await + .map(|(dht, acceptor)| { + podcast_dht_tasks.push(tokio::spawn(Arc::clone(&dht).serve(acceptor))); + podcast_dht_tasks.push(tokio::spawn(Arc::clone(&dht).maintenance())); + dht + }); // Drain DHT events into the log; the channel is bounded. let event_task = tokio::spawn(async move { @@ -861,33 +895,41 @@ impl Federation { let capabilities_acceptor = service .stream_acceptor(capabilities::CAPABILITIES_ALPN) .map_err(|err| anyhow::anyhow!("failed to take capabilities acceptor: {err}"))?; - let capabilities_serve_task = tokio::spawn(capabilities::serve(capabilities_acceptor)); + let capabilities_serve_task = tokio::spawn(capabilities::serve( + capabilities_acceptor, + podcast_dht.is_some(), + )); let capabilities_probe_task = tokio::spawn(capabilities::probe_loop( Arc::clone(&service), Arc::clone(&self.observed_protocols), + podcast_dht.clone(), )); + let mut tasks = podcast_dht_tasks; + tasks.extend([ + event_task, + sync_task, + audio_task, + catalog_task, + similarity_task, + device_sync_task, + device_tick_task, + jam_serve_task, + jam_poll_task, + capabilities_serve_task, + capabilities_probe_task, + similarity_dht_serve_task, + similarity_dht_maintenance_task, + similarity_dht_sync_task, + ]); + self.podcast_dht.send_replace(podcast_dht.clone()); *guard = Some(Running { service, similarity_dht, + podcast_dht, network_name, network_id, - tasks: vec![ - event_task, - sync_task, - audio_task, - catalog_task, - similarity_task, - device_sync_task, - device_tick_task, - jam_serve_task, - jam_poll_task, - capabilities_serve_task, - capabilities_probe_task, - similarity_dht_serve_task, - similarity_dht_maintenance_task, - similarity_dht_sync_task, - ], + tasks, }); self.set_error(None); Ok(true) @@ -895,6 +937,7 @@ impl Federation { async fn stop(&self) { let mut guard = self.running.lock().await; + self.podcast_dht.send_replace(None); stop_running(guard.take()).await; } @@ -1088,7 +1131,12 @@ impl Federation { recovery_count: self.recovery_count.load(Ordering::Relaxed), last_sync: lock(&self.last_sync).clone(), last_error: lock(&self.last_error).clone(), - protocols: ProtocolVersions::snapshot(&self.observed_protocols), + protocols: ProtocolVersions::snapshot( + &self.observed_protocols, + guard + .as_ref() + .is_some_and(|running| running.podcast_dht.is_some()), + ), ..FedStatus::default() }; if let Some(running) = guard.as_ref() { @@ -1214,6 +1262,10 @@ impl Federation { /// Searches the federated network: matching tracks plus the artists a /// card can be assembled for (from artist records and from the artist /// names of matching tracks/releases). + /// + /// Public podcast cards are searched separately + /// ([`Federation::search_podcasts`]), so the music results never wait + /// for the discovery overlay. pub async fn search(&self, query: &str) -> Result { let service = self.service().await?; let normalized = music_dht::normalize_name(query); @@ -1309,6 +1361,24 @@ impl Federation { Ok(FedSearchResults { artists, tracks }) } + /// Public podcast cards matching `query` in the discovery overlay + /// (explicit type `podcast`), bounded by + /// [`crate::podcasts::discovery::PODCAST_SEARCH_TIMEOUT`] including the + /// overlay's bootstrap. Empty while the overlay is not running, on an + /// error or on timeout. + pub async fn search_podcasts( + &self, + query: &str, + ) -> Vec { + let podcast_dht = self.podcast_dht.borrow().clone(); + crate::podcasts::discovery::search( + podcast_dht.as_deref(), + query, + crate::podcasts::discovery::PODCAST_SEARCH_TIMEOUT, + ) + .await + } + /// Bounded fan-out to known peers using the exact model/profile /// fingerprint carried with the query. No DHT records are written. pub async fn search_similar( @@ -2804,6 +2874,31 @@ async fn similarity_route_sync_loop( } } +/// Opens the podcast-discovery overlay and takes its stream acceptor. A +/// failure (unreadable or newer cache, …) is logged and only disables public +/// podcast discovery: the acceptor is dropped so the overlay's ALPN refuses +/// streams, and the overlay is not advertised. +async fn open_podcast_dht( + service: &Arc, + data_dir: &Path, +) -> Option<(Arc, music_dht::StreamAcceptor)> { + let acceptor = match service.stream_acceptor(PODCAST_DHT_ALPN) { + Ok(acceptor) => acceptor, + Err(err) => { + tracing::warn!("podcast discovery is off: cannot take its acceptor: {err}"); + return None; + } + }; + match PodcastDht::open(Arc::clone(service), data_dir.join(PODCAST_DHT_CACHE_FILE)).await { + Ok(dht) => Some((dht, acceptor)), + Err(err) => { + tracing::warn!("podcast discovery is off: cannot open its cache: {err}"); + drop(acceptor); + None + } + } +} + async fn stop_running(running: Option) { let Some(running) = running else { return }; for task in &running.tasks { diff --git a/src/federation/tests.rs b/src/federation/tests.rs index 742ee2e..040df6c 100644 --- a/src/federation/tests.rs +++ b/src/federation/tests.rs @@ -135,3 +135,35 @@ fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() { ); } } + +/// The podcast-discovery overlay opens next to the other overlay caches; a +/// cache that cannot be opened only turns discovery off. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn podcast_discovery_opens_or_turns_itself_off() { + async fn service(dir: &Path) -> Arc { + let config = MusicDhtConfig::builder() + .data_dir(dir) + .network_id(NetworkId::from_name("furumi-tui-podcast-dht-open-test")) + .schema_independent_stream_protocol(PODCAST_DHT_ALPN) + .build() + .unwrap(); + let (service, mut events) = MusicDhtService::start(config).await.unwrap(); + tokio::spawn(async move { while events.recv().await.is_some() {} }); + Arc::new(service) + } + + let ok_dir = tempfile::tempdir().unwrap(); + let ok = service(ok_dir.path()).await; + let opened = open_podcast_dht(&ok, ok_dir.path()).await; + assert!(opened.is_some()); + assert!(ok_dir.path().join(PODCAST_DHT_CACHE_FILE).is_file()); + drop(opened); + ok.shutdown().await.unwrap(); + + // A directory where the cache file belongs cannot be opened as SQLite. + let broken_dir = tempfile::tempdir().unwrap(); + std::fs::create_dir(broken_dir.path().join(PODCAST_DHT_CACHE_FILE)).unwrap(); + let broken = service(broken_dir.path()).await; + assert!(open_podcast_dht(&broken, broken_dir.path()).await.is_none()); + broken.shutdown().await.unwrap(); +} diff --git a/src/podcasts/discovery.rs b/src/podcasts/discovery.rs new file mode 100644 index 0000000..489a605 --- /dev/null +++ b/src/podcasts/discovery.rs @@ -0,0 +1,257 @@ +//! Public podcast cards found by federated search: their order next to the +//! music results, whether this device is already subscribed, an episode +//! preview read from the feed itself, and subscribing from a card. +//! +//! A card is a publisher's claim. Only its origin feed URL +//! ([`PodcastSearchHit::feed_url`]) is bound to the podcast id, so it is the +//! only URL this module ever fetches or subscribes with; the claimed moved +//! URLs are shown, never used. Nothing from the card is stored: subscribing +//! reads the feed from the podcast's own host like any other subscription, +//! and the new subscription is private. + +use anyhow::{Result, bail}; +use furumi_library::podcasts::PodcastStore; +use music_dht::LibraryItem; +use music_dht::podcast::{FeedUrl, PodcastId}; +use music_dht::podcast_dht::{ + FederatedSearchHit, PodcastDht, PodcastSearchHit, merge_search_results, +}; + +use super::fetch::{FeedBody, Fetcher, Validators}; +use super::refresh::{self, Services, SubscribeReport}; + +/// Podcast hits asked from the discovery overlay per search. +pub const PODCAST_SEARCH_LIMIT: usize = 20; +/// Upper bound of one podcast search, the overlay's bootstrap included (the +/// overlay bounds only its lookups). The music lookup deadline. +pub const PODCAST_SEARCH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15); +/// Episodes kept in a preview (newest first). +pub const PREVIEW_EPISODES: usize = 200; + +/// One public podcast in the search results. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PodcastHit { + pub hit: PodcastSearchHit, + /// The id this device gives the hit's origin URL + /// (`PodcastStore::resolve_podcast_id`): the card's id, unless the URL + /// is a known alias of a podcast this device already has. + pub local_id: PodcastId, +} + +impl PodcastHit { + /// A hit not yet resolved against the local store. + pub fn new(hit: PodcastSearchHit) -> Self { + let local_id = hit.podcast_id.clone(); + Self { hit, local_id } + } + + /// Ids under which this device may know the podcast. + pub fn ids(&self) -> [&PodcastId; 2] { + [&self.local_id, &self.hit.podcast_id] + } + + pub fn title(&self) -> &str { + &self.hit.card.title + } +} + +/// The podcast hits of a federated search, in the order of +/// [`merge_search_results`] (exact title matches first). The TUI keeps its +/// own sections for the music results and searches podcasts separately; +/// the order of the podcast entries does not depend on the music items +/// (the merge is a stable exact-title partition), so [`search`] passes none. +pub fn podcast_hits( + query: &str, + music: Vec, + podcasts: Vec, +) -> Vec { + merge_search_results(query, music, podcasts) + .into_iter() + .filter_map(|hit| match hit { + FederatedSearchHit::Podcast(hit) => Some(PodcastHit::new(hit)), + FederatedSearchHit::Music(_) => None, + }) + .collect() +} + +/// Searches the discovery overlay for public podcast cards, independently +/// of the music search. Bounded by `timeout` (the overlay bootstraps first +/// when it knows no peers, which its own lookup deadline does not cover). +/// No overlay, an error or a timeout give no hits; the warning names +/// neither the query nor any URL. +pub async fn search( + dht: Option<&PodcastDht>, + query: &str, + timeout: std::time::Duration, +) -> Vec { + let Some(dht) = dht else { + return Vec::new(); + }; + match tokio::time::timeout(timeout, dht.search(query, PODCAST_SEARCH_LIMIT)).await { + Ok(Ok(hits)) => podcast_hits(query, Vec::new(), hits), + Ok(Err(err)) => { + tracing::warn!("podcast discovery search failed: {err}"); + Vec::new() + } + Err(_) => { + tracing::warn!( + timeout_s = timeout.as_secs(), + "podcast discovery search timed out" + ); + Vec::new() + } + } +} + +/// Resolves each hit's origin URL against the local store (feed aliases +/// included). Blocking. +pub fn resolve_local_ids(store: &PodcastStore, hits: &mut [PodcastHit]) -> Result<()> { + for hit in hits { + hit.local_id = store.resolve_podcast_id(&hit.hit.feed_url)?; + } + Ok(()) +} + +/// Subscribes to the podcast of a public card with its origin URL, never a +/// claimed one. The feed is read from its own host (following its own +/// redirects and `itunes:new-feed-url`), the subscription is private and +/// none of the card's metadata is stored. +pub async fn subscribe_from_hit( + services: &Services, + hit: &PodcastSearchHit, + now_ms: i64, +) -> Result { + if PodcastId::from_feed_url(&hit.feed_url) != hit.podcast_id { + bail!("the card's feed URL does not match its podcast id"); + } + refresh::subscribe_url(services, hit.feed_url.clone(), now_ms).await +} + +/// One episode of a preview. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PreviewEpisode { + pub title: String, + pub season: Option, + pub episode_number: Option, + pub published_at_ms: Option, + pub duration_ms: Option, +} + +/// What a podcast's own feed says, read without subscribing. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FeedPreview { + /// Channel title in the feed (may differ from the card's claim). + pub title: String, + pub episode_count: usize, + /// Season number (`None`: episodes without one) and episode count, + /// seasons in ascending order, unnumbered last. + pub seasons: Vec<(Option, usize)>, + /// Newest first, at most [`PREVIEW_EPISODES`]. + pub episodes: Vec, + /// The feed answered from, or announced, another address. + pub moved_to: Option, + /// This device is subscribed to the podcast under `moved_to` (it + /// followed the feed's newer address, the card names the older one): + /// the card shows «Уже подписан». Set by [`preview_for`]. + pub subscribed_as: Option, +} + +/// Reads the feed at `url` (a card's origin URL) for a preview. Nothing is +/// stored and no fetch state is recorded. +pub async fn preview(fetcher: &Fetcher, url: &FeedUrl) -> Result { + let response = fetcher.fetch_feed(url, Validators::default()).await?; + let FeedBody::Modified { body, .. } = response.body else { + bail!("the server answered 304 Not Modified to an unconditional request"); + }; + let feed = refresh::parse(body, &response.final_url).await?; + let announced = feed + .new_feed_url + .as_deref() + .and_then(|url| FeedUrl::parse(url).ok()); + let moved_to = response + .moved_permanently_to + .or(announced) + .filter(|moved| moved != url); + + let mut seasons: std::collections::BTreeMap<(bool, u32), usize> = Default::default(); + for episode in &feed.episodes { + let key = match episode.season { + Some(season) => (false, season), + None => (true, 0), + }; + *seasons.entry(key).or_default() += 1; + } + let mut episodes: Vec = feed + .episodes + .iter() + .map(|episode| PreviewEpisode { + title: episode.title.clone(), + season: episode.season, + episode_number: episode.episode_number, + published_at_ms: episode.published_at_ms, + duration_ms: episode.duration_ms, + }) + .collect(); + episodes.sort_by_key(|episode| std::cmp::Reverse(episode.published_at_ms)); + episodes.truncate(PREVIEW_EPISODES); + Ok(FeedPreview { + title: feed.title.clone(), + episode_count: feed.episodes.len(), + seasons: seasons + .into_iter() + .map(|((unnumbered, season), count)| ((!unnumbered).then_some(season), count)) + .collect(), + episodes, + moved_to, + subscribed_as: None, + }) +} + +/// [`preview`], plus whether this device already follows the podcast under +/// the newer address the feed moved to (`subscribed_as`). The store is only +/// read. +pub async fn preview_for(services: &Services, url: &FeedUrl) -> Result { + let mut preview = preview(&services.fetcher, url).await?; + if let Some(moved) = preview.moved_to.clone() { + let store = std::sync::Arc::clone(&services.store); + preview.subscribed_as = + tokio::task::spawn_blocking(move || refresh::subscribed_podcast(&store, &moved)) + .await??; + } + Ok(preview) +} + +/// A search hit for `feed_url` as one publisher would announce it (tests of +/// the search UI and of subscribing from a card). +#[cfg(test)] +pub(crate) fn test_hit(feed_url: &str, title: &str) -> PodcastSearchHit { + let feed_url = FeedUrl::parse(feed_url).unwrap(); + let podcast_id = PodcastId::from_feed_url(&feed_url); + let publisher = music_dht::SecretKey::from_bytes(&[7; 32]).public(); + PodcastSearchHit { + podcast_id: podcast_id.clone(), + feed_url: feed_url.clone(), + card: music_dht::podcast_dht::PodcastCard { + version: 1, + network_id: music_dht::NetworkId::from_name("furumi-tui-discovery-test"), + publisher, + podcast_id, + feed_url, + id_feed_url: None, + title: title.to_string(), + author: Some("Ferris Crab".to_string()), + description: Some("Weekly news about Rust.".to_string()), + image_url: None, + link: None, + language: Some("en".to_string()), + categories: vec!["Technology".to_string()], + episode_count: Some(12), + latest_episode_at_ms: Some(1_718_445_600_000), + revision: 1, + issued_at_ms: 1, + expires_at_ms: 2, + }, + publishers: vec![publisher], + claimed_feed_urls: Vec::new(), + } +} diff --git a/src/podcasts/discovery_tests.rs b/src/podcasts/discovery_tests.rs new file mode 100644 index 0000000..534da12 --- /dev/null +++ b/src/podcasts/discovery_tests.rs @@ -0,0 +1,500 @@ +//! Public podcast cards: their order in the search results, the "already +//! subscribed" mark, the feed preview, subscribing from a card, and an +//! end-to-end run over two loopback discovery nodes with a local feed +//! server (no internet, temp directories only). + +use std::sync::Arc; +use std::time::Duration; + +use furumi_library::podcasts::PodcastListFilter; +use music_dht::NetworkId; +use music_dht::podcast::{FeedUrl, PodcastId}; +use music_dht::podcast_dht::{ClaimedFeedUrl, FederatedSearchHit, SearchHitKind}; + +use super::discovery::{ + PODCAST_SEARCH_TIMEOUT, PodcastHit, podcast_hits, preview, preview_for, resolve_local_ids, + search, subscribe_from_hit, test_hit, +}; +use super::publication::{Publication, check_share, reconcile, set_shared}; +use super::publication_tests::{Node, wait_for}; +use super::refresh::{refresh, subscribe}; +use super::test_server::Route; +use super::tests::{NOW, fixture, rss}; + +#[test] +fn podcast_hits_put_exact_titles_first_and_keep_only_podcasts() { + let other = test_hit("https://feeds.example.test/weekly.xml", "Rust Radio Weekly"); + let exact = test_hit("https://feeds.example.test/radio.xml", "Rust Radio"); + let hits = podcast_hits("rust radio", Vec::new(), vec![other.clone(), exact.clone()]); + let ids: Vec<&PodcastId> = hits.iter().map(|hit| &hit.hit.podcast_id).collect(); + assert_eq!(ids, [&exact.podcast_id, &other.podcast_id]); + // Unresolved hits use the card's id; the type marker is "podcast". + assert!(hits.iter().all(|hit| hit.local_id == hit.hit.podcast_id)); + assert_eq!(hits[0].hit.kind(), SearchHitKind::Podcast); + assert_eq!( + FederatedSearchHit::Podcast(exact).kind().as_str(), + "podcast" + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn podcast_search_is_bounded_and_empty_without_the_overlay() { + // Federation off (no overlay): no hits, nothing to wait for. + assert!( + search(None, "rust radio", PODCAST_SEARCH_TIMEOUT) + .await + .is_empty() + ); + + // An overlay that knows no overlay peers bootstraps from its music-DHT + // contacts before it searches, outside its own lookup deadline. With a + // contact that no longer answers, the search still ends at the bound. + let temp = tempfile::tempdir().unwrap(); + let network = NetworkId::from_name("furumi-tui-podcast-search-bound-test"); + let a = Node::start(&temp.path().join("a"), network).await; + let gone = Node::start(&temp.path().join("gone"), network).await; + a.service + .connect(gone.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("a music DHT contact", async || { + (!a.service.known_peers().is_empty()).then_some(()) + }) + .await; + gone.stop().await; + assert_eq!(a.dht.known_peers(), 0); + let bound = Duration::from_millis(500); + let started = std::time::Instant::now(); + let hits = search(Some(&a.dht), "rust radio", bound).await; + let elapsed = started.elapsed(); + assert!(hits.is_empty()); + assert!(elapsed < bound + Duration::from_secs(1), "took {elapsed:?}"); + a.stop().await; +} + +#[tokio::test] +async fn subscribing_from_a_card_matches_subscribing_by_url() { + let by_url = fixture(); + let from_card = fixture(); + by_url.server.set( + "/feed.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ), + ); + let url = by_url.server.url("/feed.xml"); + + let typed = subscribe(&by_url.services, &url, NOW).await.unwrap(); + let hit = test_hit(&url, "A card title that is only a claim"); + let report = subscribe_from_hit(&from_card.services, &hit, NOW) + .await + .unwrap(); + assert_eq!(report.podcast_id, typed.podcast_id); + assert_eq!(report.podcast_id, hit.podcast_id); + assert_eq!(report.new_episodes, 2); + // Metadata comes from the feed, never from the card. + assert_eq!(report.title, "Test Show"); + // A private subscription: no share decision was written. + assert_eq!(from_card.log.kinds(), ["podcast_subscription_set"]); + let store = &from_card.services.store; + assert_eq!(store.share(&report.podcast_id).unwrap(), None); + let rows = store.list_podcasts(PodcastListFilter::Subscribed).unwrap(); + assert_eq!(rows.len(), 1); + assert!(!rows[0].shared); + assert_eq!(rows[0].meta.title, "Test Show"); + // Subscribing again from the card changes nothing. + let again = subscribe_from_hit(&from_card.services, &hit, NOW + 1) + .await + .unwrap(); + assert!(again.already_subscribed); + assert_eq!(from_card.log.kinds().len(), 1); +} + +#[tokio::test] +async fn subscribing_from_a_card_never_fetches_a_claimed_url() { + let f = fixture(); + // The origin URL moved permanently; a publisher also claims a URL of its + // own choosing. + f.server.set( + "/origin.xml", + Route::redirect(301, &f.server.url("/moved.xml")), + ); + f.server.set( + "/moved.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + f.server.set( + "/claimed.xml", + Route::new(rss(&[("x", "Imposter")]), "application/rss+xml"), + ); + let origin = FeedUrl::parse(&f.server.url("/origin.xml")).unwrap(); + let moved = FeedUrl::parse(&f.server.url("/moved.xml")).unwrap(); + let claimed = FeedUrl::parse(&f.server.url("/claimed.xml")).unwrap(); + let mut hit = test_hit(origin.as_str(), "Rust Radio"); + hit.claimed_feed_urls = vec![ClaimedFeedUrl { + feed_url: claimed.clone(), + publishers: hit.publishers.clone(), + }]; + hit.card.feed_url = claimed.clone(); + hit.card.id_feed_url = Some(origin.clone()); + + let report = subscribe_from_hit(&f.services, &hit, NOW).await.unwrap(); + assert_eq!(report.podcast_id, PodcastId::from_feed_url(&origin)); + assert!(f.server.requests("/claimed.xml").is_empty()); + let aliases = f.services.store.feed_aliases(&report.podcast_id).unwrap(); + assert!( + aliases.contains(&moved), + "the feed's own redirect is an alias" + ); + assert!( + !aliases.contains(&claimed), + "a claim never becomes an alias" + ); + + // A card whose id is not the hash of its origin URL is refused before + // anything is fetched or stored. + let other = fixture(); + let mut forged = test_hit(&f.server.url("/moved.xml"), "Rust Radio"); + forged.podcast_id = hit.podcast_id.clone(); + let requests = f.server.requests("/moved.xml").len(); + assert!( + subscribe_from_hit(&other.services, &forged, NOW) + .await + .is_err() + ); + assert_eq!(f.server.requests("/moved.xml").len(), requests); + assert!(other.log.kinds().is_empty()); +} + +#[tokio::test] +async fn hits_resolve_to_the_podcast_a_feed_alias_belongs_to() { + let f = fixture(); + f.server + .set("/old.xml", Route::redirect(301, &f.server.url("/new.xml"))); + f.server.set( + "/new.xml", + Route::new(rss(&[("a", "Pilot")]), "application/rss+xml"), + ); + let subscribed = subscribe(&f.services, &f.server.url("/old.xml"), NOW) + .await + .unwrap(); + // Somebody else subscribed with the new address and shares that card. + let mut hits = vec![ + PodcastHit::new(test_hit(&f.server.url("/new.xml"), "Test Show")), + PodcastHit::new(test_hit( + "https://feeds.example.test/unknown.xml", + "Unknown", + )), + ]; + assert_ne!(hits[0].hit.podcast_id, subscribed.podcast_id); + resolve_local_ids(&f.services.store, &mut hits).unwrap(); + assert_eq!(hits[0].local_id, subscribed.podcast_id); + assert!(hits[0].ids().contains(&&subscribed.podcast_id)); + assert_eq!(hits[1].local_id, hits[1].hit.podcast_id); +} + +#[tokio::test] +async fn a_card_for_an_older_address_finds_the_subscription_under_the_newer_one() { + use furumi_library::podcasts::PodcastListFilter; + // The user follows the feed under its newer address; a long-standing + // publisher's card names the older one (the card's origin). + let f = fixture(); + let feed = |announce: Option<&str>| { + let announce = announce + .map(|url| format!("{url}")) + .unwrap_or_default(); + format!( + "\ + Test Show{announce}{}", + item("a", "Pilot", 1, "Sat, 01 Jun 2024 10:00:00 GMT"), + ) + }; + let subscribed_rows = || { + f.services + .store + .list_podcasts(PodcastListFilter::Subscribed) + .unwrap() + .len() + }; + + // 1. The old address redirects permanently to the new one. + f.server + .set("/old.xml", Route::redirect(301, &f.server.url("/new.xml"))); + f.server + .set("/new.xml", Route::new(feed(None), "application/rss+xml")); + let followed = subscribe(&f.services, &f.server.url("/new.xml"), NOW) + .await + .unwrap(); + let card = test_hit(&f.server.url("/old.xml"), "Test Show"); + let url = card.feed_url.clone(); + let preview = preview_for(&f.services, &url).await.unwrap(); + assert_eq!(preview.subscribed_as.as_ref(), Some(&followed.podcast_id)); + let report = subscribe_from_hit(&f.services, &card, NOW).await.unwrap(); + assert!(report.already_subscribed); + assert_eq!(report.podcast_id, followed.podcast_id); + assert_eq!(subscribed_rows(), 1); + + // 2. The old address only announces the new one (itunes:new-feed-url, + // no redirect): no second subscription for the same podcast. + let new_url = f.server.url("/new2.xml"); + f.server + .set("/new2.xml", Route::new(feed(None), "application/rss+xml")); + f.server.set( + "/old2.xml", + Route::new(feed(Some(&new_url)), "application/rss+xml"), + ); + let followed = subscribe(&f.services, &new_url, NOW).await.unwrap(); + let kinds = f.log.kinds().len(); + let card = test_hit(&f.server.url("/old2.xml"), "Test Show"); + let url = card.feed_url.clone(); + let preview = preview_for(&f.services, &url).await.unwrap(); + assert_eq!(preview.moved_to, Some(FeedUrl::parse(&new_url).unwrap())); + assert_eq!(preview.subscribed_as.as_ref(), Some(&followed.podcast_id)); + let report = subscribe_from_hit(&f.services, &card, NOW).await.unwrap(); + assert!(report.already_subscribed); + assert_eq!(report.podcast_id, followed.podcast_id); + assert_eq!(subscribed_rows(), 2, "one row per podcast"); + assert_eq!(f.log.kinds().len(), kinds, "nothing was written"); + + // A card whose feed moved to an address this device does not follow + // is not marked. + f.server.set( + "/elsewhere.xml", + Route::new( + feed(Some("https://feeds.example.test/unknown.xml")), + "application/rss+xml", + ), + ); + let url = FeedUrl::parse(&f.server.url("/elsewhere.xml")).unwrap(); + let preview = preview_for(&f.services, &url).await.unwrap(); + assert!(preview.moved_to.is_some()); + assert_eq!(preview.subscribed_as, None); +} + +#[tokio::test] +async fn preview_reads_seasons_and_episodes_without_storing_anything() { + let f = fixture(); + let new_url = "https://feeds.example.test/new-home.xml"; + let feed = format!( + "\ + Feed Title{new_url}\ + {}{}{}{}", + item("a", "One", 1, "Sat, 01 Jun 2024 10:00:00 GMT"), + item("b", "Two", 1, "Sat, 08 Jun 2024 10:00:00 GMT"), + item("c", "Three", 2, "Sat, 15 Jun 2024 10:00:00 GMT"), + "Bonusd\ + Sat, 22 Jun 2024 10:00:00 GMT\ + ", + ); + f.server + .set("/feed.xml", Route::new(feed, "application/rss+xml")); + let url = FeedUrl::parse(&f.server.url("/feed.xml")).unwrap(); + let preview = preview(&f.services.fetcher, &url).await.unwrap(); + assert_eq!(preview.title, "Feed Title"); + assert_eq!(preview.episode_count, 4); + assert_eq!(preview.seasons, [(Some(1), 2), (Some(2), 1), (None, 1)]); + let titles: Vec<&str> = preview.episodes.iter().map(|e| e.title.as_str()).collect(); + assert_eq!(titles, ["Bonus", "Three", "Two", "One"]); + assert_eq!(preview.episodes[1].season, Some(2)); + assert_eq!(preview.episodes[1].duration_ms, Some(45 * 60 * 1000)); + assert_eq!( + preview.moved_to, + Some(FeedUrl::parse(new_url).unwrap()), + "the feed announced a new address" + ); + // Nothing was stored: no podcast, no fetch state, no operation. + let store = &f.services.store; + assert!( + store + .list_podcasts(PodcastListFilter::All) + .unwrap() + .is_empty() + ); + assert!(f.log.kinds().is_empty()); +} + +fn item(guid: &str, title: &str, season: u32, date: &str) -> String { + format!( + "{title}{guid}{date}\ + {season}45:00\ + " + ) +} + +// --------------------------------------------------------------------------- +// Two loopback nodes and a local feed server + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_stranger_finds_a_shared_card_subscribes_and_refreshes_from_the_feed() { + tokio::time::timeout(Duration::from_secs(240), stranger_subscribes()) + .await + .unwrap(); +} + +async fn stranger_subscribes() { + let temp = tempfile::tempdir().unwrap(); + let network = NetworkId::from_name("furumi-tui-podcast-discovery-test"); + // A shares a podcast. B is another user's TUI outside A's + // trusted-device group: no device sync between them, only the overlay. + let a = Node::start(&temp.path().join("a"), network).await; + let b = Node::start(&temp.path().join("b"), network).await; + b.service + .connect(a.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("music DHT contacts", async || { + (!a.service.known_peers().is_empty() && !b.service.known_peers().is_empty()).then_some(()) + }) + .await; + for node in [&a, &b] { + node.dht.bootstrap().await.unwrap(); + } + wait_for("overlay contacts", async || { + (a.dht.known_peers() >= 1 && b.dht.known_peers() >= 1).then_some(()) + }) + .await; + + // The podcast's own host (a local HTTP server). + let user_a = fixture(); + let user_b = fixture(); + let feed = &user_a.server; + feed.set( + "/show.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second")]), + "application/rss+xml", + ), + ); + let feed_url = FeedUrl::parse(&feed.url("/show.xml")).unwrap(); + let private_url = feed.url("/diary.xml"); + feed.set( + "/diary.xml", + Route::new(rss(&[("p", "Private entry")]), "application/rss+xml"), + ); + + // A subscribes to two podcasts and shares one. The local address looks + // private, so A confirms exactly these URLs first (the dialog). + let shared = subscribe(&user_a.services, feed_url.as_str(), NOW) + .await + .unwrap(); + let private = subscribe(&user_a.services, &private_url, NOW) + .await + .unwrap(); + let store_a = Arc::clone(&user_a.services.store); + let confirmations = Arc::clone(&user_a.services.confirmations); + let check = check_share(&store_a, &confirmations, &shared.podcast_id).unwrap(); + set_shared( + &store_a, + &confirmations, + &shared.podcast_id, + true, + check.needs_confirmation().then_some(check.urls), + || { + user_a + .services + .log + .record(&mut |stamps| store_a.set_shared(stamps, &shared.podcast_id, true)) + }, + ) + .unwrap(); + let report = reconcile( + &a.dht, + Some(Arc::clone(&store_a)), + Arc::clone(&confirmations), + ) + .await; + assert_eq!(report.error, None); + assert_eq!( + report.podcasts.get(&shared.podcast_id), + Some(&Publication::Published) + ); + + // B searches like the search view does: overlay search, merged order, + // resolved against B's own store. + let mut hits = wait_for("the shared card", async || { + let found = search(Some(&b.dht), "test show", PODCAST_SEARCH_TIMEOUT).await; + (!found.is_empty()).then_some(found) + }) + .await; + assert_eq!(hits.len(), 1, "the private subscription is not found"); + resolve_local_ids(&user_b.services.store, &mut hits).unwrap(); + let hit = hits.remove(0); + assert_eq!(hit.hit.kind(), SearchHitKind::Podcast); + assert_eq!(hit.hit.podcast_id, shared.podcast_id); + assert_eq!(hit.hit.feed_url, feed_url); + assert_eq!(hit.hit.publishers, vec![a.service.endpoint_id()]); + assert_eq!(hit.local_id, shared.podcast_id); + assert!( + b.dht + .lookup_podcast(&private.podcast_id) + .await + .unwrap() + .is_none() + ); + + // "Подписаться": B reads the feed from its host, not from A. + let before = feed.requests("/show.xml").len(); + let subscribed = subscribe_from_hit(&user_b.services, &hit.hit, NOW) + .await + .unwrap(); + assert_eq!(subscribed.podcast_id, shared.podcast_id, "same podcast id"); + assert_eq!(subscribed.new_episodes, 2); + assert_eq!(feed.requests("/show.xml").len(), before + 1); + let store_b = &user_b.services.store; + assert_eq!(store_b.share(&subscribed.podcast_id).unwrap(), None); + assert_eq!(user_b.log.kinds(), ["podcast_subscription_set"]); + assert!( + b.dht.published().await.unwrap().is_empty(), + "B publishes nothing" + ); + + // B refreshes on its own from the feed. + feed.set( + "/show.xml", + Route::new( + rss(&[("a", "Pilot"), ("b", "Second"), ("c", "Third")]), + "application/rss+xml", + ), + ); + let refreshed = refresh(&user_b.services, &subscribed.podcast_id, &feed_url, NOW + 1) + .await + .unwrap(); + assert_eq!(refreshed.new_episodes, 1); + + // A stops sharing: the card disappears for B, B's subscription stays. + user_a + .services + .log + .record(&mut |stamps| store_a.set_shared(stamps, &shared.podcast_id, false)) + .unwrap(); + let report = reconcile(&a.dht, Some(Arc::clone(&store_a)), confirmations).await; + assert_eq!(report.podcasts.get(&shared.podcast_id), None); + assert!( + b.dht + .lookup_podcast(&shared.podcast_id) + .await + .unwrap() + .is_none() + ); + assert!( + search(Some(&b.dht), "test show", PODCAST_SEARCH_TIMEOUT) + .await + .is_empty() + ); + let rows = store_b + .list_podcasts(PodcastListFilter::Subscribed) + .unwrap(); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].podcast_id, shared.podcast_id); + assert!(!rows[0].shared); + let refreshed = refresh(&user_b.services, &subscribed.podcast_id, &feed_url, NOW + 2) + .await + .unwrap(); + assert_eq!(refreshed.new_episodes, 0); + + a.stop().await; + b.stop().await; +} diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs index aafa3f2..2030bbb 100644 --- a/src/podcasts/mod.rs +++ b/src/podcasts/mod.rs @@ -18,19 +18,30 @@ //! 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. +//! sessions in order on its own thread; +//! * [`publication`] publishes the podcasts the user chose to share to the +//! public discovery overlay (`music_dht::podcast_dht`) and revokes them; +//! * [`discovery`] handles public cards found by search: their order, the +//! "already subscribed" mark, an episode preview read from the feed and +//! subscribing from a card. //! //! Only RSS metadata is stored. Audio stays on the publisher's server. +pub mod discovery; pub mod fetch; pub mod playback; pub mod progress; +pub mod publication; pub mod refresh; pub mod stream; +#[cfg(test)] +mod discovery_tests; #[cfg(test)] mod playback_tests; #[cfg(test)] +mod publication_tests; +#[cfg(test)] mod stream_tests; #[cfg(test)] mod test_server; @@ -111,10 +122,13 @@ pub fn open_services(db_path: &Path, covers_dir: PathBuf, log: Arc) - Ok(_) => {} Err(err) => tracing::debug!("reading the podcast import quarantine failed: {err:#}"), } + let confirmations = + publication::ShareConfirmations::open(publication::confirmations_path(db_path)); Ok(Services { store: Arc::new(store), fetcher: Arc::new(Fetcher::new(covers_dir)?), log, + confirmations: Arc::new(confirmations), }) } diff --git a/src/podcasts/publication.rs b/src/podcasts/publication.rs new file mode 100644 index 0000000..3811b19 --- /dev/null +++ b/src/podcasts/publication.rs @@ -0,0 +1,1257 @@ +//! Public podcast discovery: publishing the podcasts the user chose to share. +//! +//! The share decision itself is synced personal state +//! (`PodcastStore::set_shared`, operation `podcast_share_set`); this module +//! turns the merged registers into this device's own publications in the +//! FRID overlay `music_dht::podcast_dht`: +//! +//! * [`share_plan`] builds one `PodcastShareState` per subscription, the +//! integration pattern FRID documents for SQLite clients: the merged +//! subscription and share registers, a card whose `feed_url` is the URL +//! this device fetches (catalog) and whose `id_feed_url` is the origin +//! URL, and the local privacy confirmation. A card is only built for a +//! podcast that is effectively shared; private subscriptions contribute +//! their registers only, and `PodcastDht::sync_shares` never publishes +//! them. +//! * [`reconcile`] hands those states to `PodcastDht::sync_shares`, which is +//! declarative and idempotent: it publishes what is shared and tombstones +//! every own publication that is not (share turned off or unsubscribed on +//! any trusted device). A stale device cannot republish, because its +//! registers lose to the newer share decision once they converge. +//! * [`spawn`] runs the reconciler off the UI thread: right after local +//! share-off and unsubscribe actions (quitting waits for those, +//! [`Reconciler::flush`]), after other local share actions, received +//! podcast operations and feed refreshes (card metadata), when the overlay +//! (re)starts, and periodically. Until the device synced with another +//! device of its group after starting, a pass only revokes +//! ([`revoke_unshared`]): its registers may still hold a "share" decision +//! revoked there meanwhile, and `sync_shares` would send that card again. +//! * [`ShareConfirmations`] remembers, on this device only, that the user +//! confirmed publishing feed URLs that look private +//! (`PodcastCardSpec::privacy_warnings`). FRID leaves this confirmation to +//! the client and does not sync it: every device asks its own user before +//! it publishes such a URL under its own identity. A confirmation is bound +//! to the exact URLs shown, so a feed that moves to another URL asks again, +//! and to the share decision it was given for (the share register's +//! operation), so a decision taken anywhere later — sharing turned off, +//! unsubscribing, sharing again — asks again too. A podcast whose card +//! looks private and is not confirmed for the current decision is held +//! back by [`share_plan`] itself, and a card this device published for an +//! earlier decision is revoked (`sync_shares` alone would keep publishing +//! feed URLs it already publishes). +//! +//! Logging stays free of private data: only counts are logged, never feed +//! URLs, titles or ids of podcasts that are not shared. + +use std::collections::{BTreeMap, BTreeSet}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +use anyhow::{Context, Result}; +use furumi_library::podcasts::{PodcastListFilter, PodcastStore, PodcastSummary}; +use music_dht::podcast::{FeedUrl, PodcastId, ShareRecord, SubscriptionRecord, effective_share}; +use music_dht::podcast_dht::{ + FeedUrlPrivacyWarning, PodcastCardSpec, PodcastDht, PodcastDhtSyncStats, PodcastPrivacyHold, + PodcastShareState, PodcastSyncFailure, feed_url_privacy_warning, +}; +use serde::{Deserialize, Serialize}; +use tokio::sync::{Notify, watch}; + +/// Local file with the privacy confirmations, next to `podcasts.sqlite3`. +pub const CONFIRMATIONS_FILE: &str = "podcast-share-confirmations.json"; +/// Format version of [`CONFIRMATIONS_FILE`]. Version 1 (URLs only, never +/// released) is read as empty: the user is asked again. +const CONFIRMATIONS_VERSION: u32 = 2; +/// Periodic reconciliation (on top of the triggers). Cards are re-signed by +/// the overlay's own maintenance every 10 minutes; this pass catches share +/// decisions that changed while the overlay was busy or unreachable. +pub const RECONCILE_INTERVAL: Duration = Duration::from_secs(5 * 60); +/// Triggers within this window are handled by one pass (a "check all" +/// refresh finishes many feeds in a row). +pub const RECONCILE_DEBOUNCE: Duration = Duration::from_secs(1); +/// How often the overlay's peer count is checked between passes, so the +/// Podcasts tab does not show a stale "no discovery peers yet". +pub const PEER_REFRESH_INTERVAL: Duration = Duration::from_secs(10); + +/// The feed URLs a privacy confirmation covers: exactly the URLs of the card +/// the user was shown. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ConfirmedUrls { + pub feed_url: FeedUrl, + pub id_feed_url: Option, +} + +impl ConfirmedUrls { + pub fn of(card: &PodcastCardSpec) -> Self { + Self { + feed_url: card.feed_url.clone(), + id_feed_url: card.id_feed_url.clone(), + } + } +} + +/// The share decision a confirmation was given for. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "state", rename_all = "snake_case")] +enum Decision { + /// Confirmed before this device recorded its share decision: covers the + /// first share decision newer than the share register seen then + /// (`after`, its order key; `None`: no register yet). + Pending { after: Option<(u64, i64, String)> }, + /// Covers exactly the share decision written by operation `op_id`. + Bound { op_id: String }, +} + +/// One confirmation: the URLs shown and the decision it was given for. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +struct Confirmation { + urls: ConfirmedUrls, + decision: Decision, +} + +impl Confirmation { + /// Whether the confirmation covers the merged share register `share`. + fn covers(&self, share: Option<&ShareRecord>) -> bool { + let Some(share) = share.filter(|share| share.shared) else { + return false; + }; + match &self.decision { + Decision::Bound { op_id } => *op_id == share.op_id, + Decision::Pending { after } => is_after(after.as_ref(), share), + } + } + + /// A newer share decision than the confirmed one exists: sharing was + /// turned off, or turned on again after a revocation, here or on + /// another trusted device. Unsubscribing never changes the share + /// register, but sharing after subscribing again writes a new decision. + fn superseded(&self, share: Option<&ShareRecord>) -> bool { + match (&self.decision, share) { + (Decision::Bound { op_id }, Some(share)) => *op_id != share.op_id, + (Decision::Pending { after }, Some(share)) => { + !share.shared && is_after(after.as_ref(), share) + } + (_, None) => false, + } + } +} + +/// `share` is newer than the register order key `after` (`None`: there was +/// no register). +fn is_after(after: Option<&(u64, i64, String)>, share: &ShareRecord) -> bool { + after.is_none_or(|(generation, hlc, op)| share.order_key() > (*generation, *hlc, op.as_str())) +} + +#[derive(Debug, Default, Serialize, Deserialize)] +struct ConfirmationsFile { + version: u32, + confirmed: BTreeMap, +} + +/// Device-local record of "publish this feed URL although it looks +/// private" (`PodcastShareState::privacy_confirmed`). Not synced. +/// +/// A confirmation covers the URLs shown and one share decision: it is +/// recorded before the decision is written (pending), bound to the share +/// register's operation once written ([`ShareConfirmations::bind`]), and +/// dropped when a newer decision supersedes it — including decisions taken +/// on other trusted devices ([`ShareConfirmations::prune`]). +#[derive(Debug)] +pub struct ShareConfirmations { + /// `None`: in memory only (tests, or the podcast store is unavailable). + path: Option, + confirmed: Mutex>, +} + +impl ShareConfirmations { + /// Loads the confirmations at `path`. A missing file is empty; an + /// unreadable or unknown file is treated as empty too (the user is asked + /// again, which is the safe direction) and replaced on the next change. + pub fn open(path: PathBuf) -> Self { + let confirmed = match std::fs::read(&path) { + Ok(bytes) => match serde_json::from_slice::(&bytes) { + Ok(file) if file.version == CONFIRMATIONS_VERSION => file.confirmed, + Ok(file) => { + tracing::warn!( + version = file.version, + "podcast share confirmations have an unknown format; asking again" + ); + BTreeMap::new() + } + Err(err) => { + tracing::warn!( + "podcast share confirmations are unreadable; asking again: {err}" + ); + BTreeMap::new() + } + }, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => BTreeMap::new(), + Err(err) => { + tracing::warn!("cannot read the podcast share confirmations; asking again: {err}"); + BTreeMap::new() + } + }; + // A pending confirmation only lives between `confirm` and `bind` + // within one `set_shared` call; one found at start belongs to a + // call that never finished (the decision may not have been written) + // and must not cover a later decision taken elsewhere. + let mut confirmed = confirmed; + confirmed + .retain(|_, confirmation| !matches!(confirmation.decision, Decision::Pending { .. })); + Self { + path: Some(path), + confirmed: Mutex::new(confirmed), + } + } + + pub fn in_memory() -> Self { + Self { + path: None, + confirmed: Mutex::new(BTreeMap::new()), + } + } + + /// Whether the user confirmed publishing exactly these feed URLs for + /// the current share decision `share` (the merged share register). + pub fn is_confirmed(&self, card: &PodcastCardSpec, share: Option<&ShareRecord>) -> bool { + self.lock() + .get(&card.podcast_id) + .is_some_and(|confirmation| { + confirmation.urls == ConfirmedUrls::of(card) && confirmation.covers(share) + }) + } + + /// Records the confirmation of `urls` for the next share decision after + /// `share` (the merged share register now, before this device writes + /// its decision), replacing an older one. Blocking (writes the file). + pub fn confirm( + &self, + podcast_id: &PodcastId, + urls: ConfirmedUrls, + share: Option<&ShareRecord>, + ) -> Result<()> { + let after = share.map(|share| { + let (generation, hlc, op) = share.order_key(); + (generation, hlc, op.to_string()) + }); + self.update(podcast_id, |entry| { + *entry = Some(Confirmation { + urls, + decision: Decision::Pending { after }, + }); + }) + } + + /// Binds the confirmation of a podcast to the share decision `share` + /// this device just wrote (or found already written, when it confirms + /// a podcast shared elsewhere). No-op without a confirmation or when + /// `share` does not share. Blocking. + pub fn bind(&self, podcast_id: &PodcastId, share: &ShareRecord) -> Result<()> { + if !share.shared { + return Ok(()); + } + self.update(podcast_id, |entry| { + if let Some(confirmation) = entry { + confirmation.decision = Decision::Bound { + op_id: share.op_id.clone(), + }; + } + }) + } + + /// Drops a confirmation that a newer share decision superseded (taken + /// here or on another trusted device), and binds a pending one to the + /// decision it now covers. Blocking. + pub fn prune(&self, podcast_id: &PodcastId, share: Option<&ShareRecord>) -> Result<()> { + self.update(podcast_id, |entry| { + let Some(confirmation) = entry else { + return; + }; + if confirmation.superseded(share) { + *entry = None; + } else if let (Decision::Pending { .. }, Some(share)) = (&confirmation.decision, share) + && confirmation.covers(Some(share)) + { + confirmation.decision = Decision::Bound { + op_id: share.op_id.clone(), + }; + } + }) + } + + /// Drops the confirmation of a podcast (sharing turned off or + /// unsubscribed here): sharing it again asks again. Blocking. + pub fn forget(&self, podcast_id: &PodcastId) -> Result<()> { + self.update(podcast_id, |entry| *entry = None) + } + + /// The confirmation of a podcast as it is now, to put back with + /// [`ShareConfirmations::restore`]. + fn entry(&self, podcast_id: &PodcastId) -> Option { + self.lock().get(podcast_id).cloned() + } + + /// Puts back the confirmation `previous` of a podcast (the decision it + /// was given for could not be written). Takes effect in memory even + /// when the file cannot be saved: a pending confirmation left in the + /// file is dropped when it is read again ([`ShareConfirmations::open`]). + /// Blocking. + fn restore(&self, podcast_id: &PodcastId, previous: Option) -> Result<()> { + let mut confirmed = self.lock(); + if confirmed.get(podcast_id) == previous.as_ref() { + return Ok(()); + } + match previous { + Some(confirmation) => confirmed.insert(podcast_id.clone(), confirmation), + None => confirmed.remove(podcast_id), + }; + self.save(&confirmed) + } + + /// Applies `change` to the entry of `podcast_id` and saves the file when + /// the entry changed. + fn update( + &self, + podcast_id: &PodcastId, + change: impl FnOnce(&mut Option), + ) -> Result<()> { + let mut confirmed = self.lock(); + let current = confirmed.get(podcast_id).cloned(); + let mut entry = current.clone(); + change(&mut entry); + if entry == current { + return Ok(()); + } + let mut next = confirmed.clone(); + match entry { + Some(confirmation) => next.insert(podcast_id.clone(), confirmation), + None => next.remove(podcast_id), + }; + self.save(&next)?; + *confirmed = next; + Ok(()) + } + + fn save(&self, confirmed: &BTreeMap) -> Result<()> { + let Some(path) = &self.path else { + return Ok(()); + }; + let file = ConfirmationsFile { + version: CONFIRMATIONS_VERSION, + confirmed: confirmed.clone(), + }; + write_private(path, &serde_json::to_vec_pretty(&file)?) + .with_context(|| format!("saving {}", path.display())) + } + + fn lock(&self) -> std::sync::MutexGuard<'_, BTreeMap> { + self.confirmed + .lock() + .unwrap_or_else(PoisonError::into_inner) + } +} + +/// Atomically replaces `path`; the file holds feed URLs, so it is readable +/// by the user only (like the podcast database). +fn write_private(path: &Path, bytes: &[u8]) -> Result<()> { + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let mut temp = tempfile::NamedTempFile::new_in(path.parent().unwrap_or(Path::new(".")))?; + std::io::Write::write_all(&mut temp, bytes)?; + temp.as_file().sync_all()?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + temp.as_file() + .set_permissions(std::fs::Permissions::from_mode(0o600))?; + } + temp.persist(path)?; + Ok(()) +} + +/// The confirmations file of the podcast store at `db_path`. +pub fn confirmations_path(db_path: &Path) -> PathBuf { + db_path.with_file_name(CONFIRMATIONS_FILE) +} + +// --------------------------------------------------------------------------- +// Cards and share states + +/// The card this device publishes for a subscription: channel metadata and +/// counters from the local catalog (refreshed from the feed), `feed_url` the +/// URL this device fetches, `id_feed_url` the origin URL the podcast id was +/// derived from. Bounded to the card limits. +/// +/// The artwork and web page URLs are left out when they look like they +/// carry an access key themselves ([`looks_private`]) or when the feed URLs +/// look private: member feeds serve signed artwork and item links, and the +/// share dialog shows the feed URLs only. +fn catalog_card( + summary: &PodcastSummary, + subscription: &SubscriptionRecord, +) -> Result { + // Finds the origin URL in the register's alias set (FRID keeps it). + let derived = PodcastCardSpec::from_subscription(subscription)?; + let origin = derived + .id_feed_url + .clone() + .unwrap_or_else(|| derived.feed_url.clone()); + let meta = &summary.meta; + let mut card = PodcastCardSpec { + podcast_id: subscription.podcast_id.clone(), + feed_url: meta.feed_url.clone(), + id_feed_url: Some(origin), + title: meta.title.clone(), + author: meta.author.clone(), + description: meta.description.clone(), + image_url: meta.image_url.clone(), + link: meta.link.clone(), + language: None, + categories: Vec::new(), + episode_count: Some(u32::try_from(summary.episode_count).unwrap_or(u32::MAX)), + latest_episode_at_ms: summary.latest_published_at_ms, + }; + let private_feed = !card.privacy_warnings().is_empty(); + for url in [&mut card.image_url, &mut card.link] { + if private_feed || url.as_deref().is_some_and(looks_private) { + *url = None; + } + } + let card = card.bounded(); + card.validate()?; + Ok(card) +} + +/// An artwork or web page URL that must not be published: it carries what +/// looks like an access key, or it is not a plain http(s) URL the rules can +/// check. +/// +/// The feed URL rules apply except the random-looking path segment: public +/// podcast hosts and CDNs name artwork and pages by UUIDs and content +/// hashes, so that rule would drop the artwork of most public podcasts. +/// Signed and tokenized artwork carries its key in the query, which the +/// other rules catch (user info is refused by `FeedUrl::parse`), and a +/// member feed's artwork is left out with its feed anyway. +/// `feed_url_privacy_warning` checks the path last, so a path warning means +/// no other rule matched. +fn looks_private(url: &str) -> bool { + FeedUrl::parse(url).map_or(true, |url| { + !matches!( + feed_url_privacy_warning(&url), + None | Some(FeedUrlPrivacyWarning::HighEntropyPathSegment { .. }) + ) + }) +} + +/// The card [`share_plan`] would publish for `podcast_id` (whether or not +/// it is shared yet), for the share confirmation. `None` when there is no +/// subscription. +pub fn share_card(store: &PodcastStore, podcast_id: &PodcastId) -> Result> { + let (Some(summary), Some(subscription)) = + (store.podcast(podcast_id)?, store.subscription(podcast_id)?) + else { + return Ok(None); + }; + catalog_card(&summary, &subscription).map(Some) +} + +/// What enabling "share" would publish and whether it needs the user's +/// confirmation first. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ShareCheck { + pub urls: ConfirmedUrls, + /// `PodcastCardSpec::privacy_warnings` of the card. + pub warnings: Vec, + /// The user already confirmed exactly these URLs on this device. + pub confirmed: bool, +} + +impl ShareCheck { + /// Sharing must be confirmed in a dialog first. + pub fn needs_confirmation(&self) -> bool { + !self.warnings.is_empty() && !self.confirmed + } +} + +/// Builds the [`ShareCheck`] of a subscribed podcast. Blocking. +pub fn check_share( + store: &PodcastStore, + confirmations: &ShareConfirmations, + podcast_id: &PodcastId, +) -> Result { + let card = share_card(store, podcast_id)? + .with_context(|| "the podcast is not subscribed".to_string())?; + let share = store.share(podcast_id)?; + Ok(ShareCheck { + urls: ConfirmedUrls::of(&card), + warnings: card.privacy_warnings(), + confirmed: confirmations.is_confirmed(&card, share.as_ref()), + }) +} + +/// Writes a share decision with `record` (the synced `podcast_share_set` +/// operation). The confirmation of `confirm` is recorded first (pending: it +/// covers the next share decision), so a reconciler pass the decision +/// triggers already sees it, and is then bound to the decision in force — +/// the one just written, or, for a podcast shared elsewhere and held back +/// here, the one already there (`set_shared` writes nothing then). Turning +/// sharing off forgets it. Blocking. +pub fn set_shared( + store: &PodcastStore, + confirmations: &ShareConfirmations, + podcast_id: &PodcastId, + shared: bool, + confirm: Option, + record: impl FnOnce() -> Result<()>, +) -> Result<()> { + // What to put back if the decision cannot be written: a pending + // confirmation must not outlive this call, or it would cover the next + // share decision taken on any device, which this user never saw. + let mut previous = None; + if let Some(urls) = confirm { + let share = store.share(podcast_id)?; + previous = Some(confirmations.entry(podcast_id)); + confirmations.confirm(podcast_id, urls, share.as_ref())?; + } + if let Err(err) = record() { + if let Some(previous) = previous + && let Err(restore) = confirmations.restore(podcast_id, previous) + { + tracing::warn!("podcast share confirmation not restored: {restore:#}"); + } + return Err(err); + } + if !shared { + return confirmations.forget(podcast_id); + } + match store.share(podcast_id)? { + Some(share) => confirmations.bind(podcast_id, &share), + None => Ok(()), + } +} + +/// What this device hands to `PodcastDht::sync_shares`: the share states, +/// and the shared podcasts it holds back itself. +#[derive(Debug, Default)] +pub struct SharePlan { + /// One state per subscription, except the held-back podcasts. + pub states: Vec, + /// Shared podcasts whose card has privacy warnings this device's user + /// has not confirmed for the current share decision. They are left out + /// of [`SharePlan::states`], so `sync_shares` revokes a card this device + /// published for them earlier. + pub held: BTreeMap>, +} + +/// One `PodcastShareState` per podcast with a subscription register, the +/// input of `PodcastDht::sync_shares`, and the podcasts held back. Blocking. +/// +/// Only podcasts for which `effective_share` holds get a card; for every +/// other podcast `card` is `None` and `privacy_confirmed` false, so no +/// private metadata is prepared for publication at all. Confirmations that +/// a newer share decision superseded (on any device) are dropped here. +/// +/// A shared podcast whose card looks private and is not confirmed for the +/// current decision is held back here rather than by `sync_shares`: FRID +/// keeps publishing a card whose feed URLs this device already publishes +/// even without the confirmation, so a device that missed an intermediate +/// "share off" (closed, offline, or within the debounce) would go on +/// publishing a private-looking URL nobody confirmed for the new decision. +/// Leaving the podcast out makes `sync_shares` revoke that card instead. +pub fn share_plan(store: &PodcastStore, confirmations: &ShareConfirmations) -> Result { + let mut plan = SharePlan::default(); + for summary in store.list_podcasts(PodcastListFilter::All)? { + let podcast_id = &summary.podcast_id; + let Some(subscription) = store.subscription(podcast_id)? else { + continue; + }; + let share = store.share(podcast_id)?; + if let Err(err) = confirmations.prune(podcast_id, share.as_ref()) { + // Not dropping it only keeps a confirmation that no longer + // covers the current decision (`is_confirmed` checks that). + tracing::warn!("podcast share confirmation not updated: {err:#}"); + } + let shared = effective_share(Some(&subscription), share.as_ref()); + // A card that cannot be built from the catalog falls back to the + // one `sync_shares` derives from the register (and fails there, + // reported, if that is impossible too). + let card = if shared { + catalog_card(&summary, &subscription) + .inspect_err(|err| tracing::debug!("podcast card from the catalog: {err:#}")) + .ok() + } else { + None + }; + let privacy_confirmed = card + .as_ref() + .is_some_and(|card| confirmations.is_confirmed(card, share.as_ref())); + if shared && !privacy_confirmed { + let warnings = match &card { + Some(card) => card.privacy_warnings(), + None => PodcastCardSpec::from_subscription(&subscription) + .map(|card| card.privacy_warnings()) + .unwrap_or_default(), + }; + if !warnings.is_empty() { + plan.held.insert(podcast_id.clone(), warnings); + continue; + } + } + plan.states.push(PodcastShareState { + subscription, + share, + card, + privacy_confirmed, + }); + } + Ok(plan) +} + +/// The share states of [`share_plan`] (held-back podcasts left out). +/// Blocking. +#[cfg(test)] +pub(super) fn share_states( + store: &PodcastStore, + confirmations: &ShareConfirmations, +) -> Result> { + share_plan(store, confirmations).map(|plan| plan.states) +} + +// --------------------------------------------------------------------------- +// Reconciliation + +/// Public discovery as this device runs it. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum Discovery { + /// No reconciliation finished yet. + #[default] + Starting, + /// Nothing is published from this device; the reason. + Off(String), + /// The overlay runs; `peers` overlay contacts are known. + Running { peers: usize }, +} + +/// Publication of one shared podcast from this device. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Publication { + /// This device's card is published (signed, stored by the replicas it + /// reached and republished every 10 minutes). + Published, + /// Shared, but not yet published here (for example the overlay has not + /// accepted it yet). + Pending, + /// Shared as far as this device knows, but not published from it until + /// it synced with another device of its group: the decision may have + /// been revoked there meanwhile (see [`spawn`]). + WaitingForDevices, + /// Shared, but held back on this device until its user confirms the + /// feed URLs that look private. + HeldBack(Vec), + /// The card could not be built, signed or published. + Failed(String), +} + +/// Result of one reconciliation, for the Podcasts tab. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct PublicationReport { + pub discovery: Discovery, + /// Shared podcasts only. + pub podcasts: BTreeMap, + /// Podcasts that are no longer shared but whose card from this device is + /// still live: the revocation is pending, failed (retried every pass), + /// or waits for the overlay (federation off). In memory only; never + /// logged. + pub unrevoked: BTreeSet, + /// The pass as a whole failed (store or overlay error), or cards could + /// not be revoked. + pub error: Option, +} + +impl PublicationReport { + /// Sharing of `podcast_id` was turned off (or it was unsubscribed) on + /// this device; `was_shared`: it was shared just before. Until the next + /// pass says otherwise, a card this device may have published counts as + /// not revoked yet. + /// + /// While the overlay runs, the last pass says whether a card was + /// published (a held-back one never was). Without a pass of the running + /// overlay (federation off, or not started yet) this device cannot tell + /// whether a card from an earlier session is still live, so a podcast + /// that was shared counts as not revoked. + pub fn note_revocation(&mut self, podcast_id: &PodcastId, was_shared: bool) { + let publication = self.podcasts.remove(podcast_id); + let maybe_live = match self.discovery { + Discovery::Running { .. } => publication.map_or(was_shared, |publication| { + !matches!(publication, Publication::HeldBack(_)) + }), + Discovery::Off(_) | Discovery::Starting => was_shared, + }; + if maybe_live { + self.unrevoked.insert(podcast_id.clone()); + } + } +} + +/// Reconciles this device's publications with the share registers of +/// `store` once. Without a store (it could not be opened) every own +/// publication is revoked: this device can no longer tell what the user +/// still shares, and revoking is the privacy-safe direction. The same holds +/// when the registers cannot be read. +pub async fn reconcile( + dht: &PodcastDht, + store: Option>, + confirmations: Arc, +) -> PublicationReport { + let mut report = PublicationReport { + discovery: Discovery::Running { + peers: dht.known_peers(), + }, + ..PublicationReport::default() + }; + let (SharePlan { states, held }, revoke_all) = read_plan(store, confirmations).await; + let shared = shared_ids(&states, &held); + let stats = match dht.sync_shares(states).await { + Ok(mut stats) => { + stats + .held_back + .extend( + held.into_iter() + .map(|(podcast_id, warnings)| PodcastPrivacyHold { + podcast_id, + warnings, + }), + ); + stats + } + Err(err) => { + tracing::warn!("podcast publication failed: {err}"); + report.error = Some(format!("publication failed: {err}")); + for podcast_id in shared { + report + .podcasts + .insert(podcast_id, Publication::Failed(err.to_string())); + } + return report; + } + }; + if stats.published + stats.unpublished + stats.failed.len() > 0 { + tracing::info!( + published = stats.published, + unchanged = stats.unchanged, + unpublished = stats.unpublished, + held_back = stats.held_back.len(), + failed = stats.failed.len(), + remote_nodes = stats.push.remote_nodes, + "podcast publications reconciled" + ); + } + finish_pass(dht, &mut report, revoke_all, shared, stats).await; + report +} + +/// A pass before this device's share registers are current +/// ([`crate::devices::DeviceSync::podcast_registers_current`]): it has +/// other devices in its group and has not synced with one since it started, +/// so a "share" decision it holds may have been revoked there meanwhile. +/// +/// Such a pass only revokes. Every own card of a podcast that the local +/// registers no longer share (or every own card, when the registers cannot +/// be read) is unpublished on its own (`PodcastDht::unpublish`); nothing is +/// published and `PodcastDht::sync_shares` is not called, so FRID keeps the +/// cards an earlier session published out of its republishing and its +/// answers until the first full [`reconcile`]. Shared podcasts are reported +/// as [`Publication::WaitingForDevices`] (or held back, or failed). +pub async fn revoke_unshared( + dht: &PodcastDht, + store: Option>, + confirmations: Arc, +) -> PublicationReport { + let mut report = PublicationReport { + discovery: Discovery::Running { + peers: dht.known_peers(), + }, + ..PublicationReport::default() + }; + let (SharePlan { states, held }, revoke_all) = read_plan(store, confirmations).await; + let shared = shared_ids(&states, &held); + let desired: BTreeSet<&PodcastId> = states + .iter() + .filter(|state| state.is_shared()) + .map(|state| &state.subscription.podcast_id) + .collect(); + let own = match dht.published().await { + Ok(own) => own, + Err(err) => { + tracing::warn!("podcast publication failed: {err}"); + report.error = Some(format!("cannot read own publications: {err}")); + for podcast_id in shared { + report + .podcasts + .insert(podcast_id, Publication::Failed(err.to_string())); + } + return report; + } + }; + let mut stats = PodcastDhtSyncStats::default(); + for publication in own { + if !publication.active || desired.contains(&publication.podcast_id) { + continue; + } + match dht.unpublish(&publication.podcast_id).await { + Ok(push) => { + stats.unpublished += 1; + stats.push.records += push.records; + stats.push.remote_nodes = stats.push.remote_nodes.max(push.remote_nodes); + } + Err(err) => stats.failed.push(PodcastSyncFailure { + podcast_id: publication.podcast_id, + error: err.to_string(), + }), + } + } + stats.held_back.extend( + held.into_iter() + .map(|(podcast_id, warnings)| PodcastPrivacyHold { + podcast_id, + warnings, + }), + ); + if stats.unpublished + stats.failed.len() > 0 { + tracing::info!( + unpublished = stats.unpublished, + failed = stats.failed.len(), + remote_nodes = stats.push.remote_nodes, + "podcast publications revoked before the first device sync" + ); + } + finish_pass(dht, &mut report, revoke_all, shared, stats).await; + // Nothing is sent for a shared podcast yet (a card of an earlier + // session stays withheld by FRID). + for publication in report.podcasts.values_mut() { + if matches!(publication, Publication::Published | Publication::Pending) { + *publication = Publication::WaitingForDevices; + } + } + report +} + +/// The share plan of `store`, and why every own card is revoked when the +/// registers are unknown (no store, or they cannot be read). +async fn read_plan( + store: Option>, + confirmations: Arc, +) -> (SharePlan, Option) { + let Some(store) = store else { + return ( + SharePlan::default(), + Some("the podcast database is unavailable".into()), + ); + }; + match tokio::task::spawn_blocking(move || share_plan(&store, &confirmations)).await { + Ok(Ok(plan)) => (plan, None), + Ok(Err(err)) => ( + SharePlan::default(), + Some(format!("cannot read share decisions ({err:#})")), + ), + Err(err) => ( + SharePlan::default(), + Some(format!("cannot read share decisions ({err})")), + ), + } +} + +/// The shared podcasts of a plan: those it publishes and those it holds +/// back. +fn shared_ids( + states: &[PodcastShareState], + held: &BTreeMap>, +) -> Vec { + states + .iter() + .filter(|state| state.is_shared()) + .map(|state| state.subscription.podcast_id.clone()) + .chain(held.keys().cloned()) + .collect() +} + +/// Logs the failures of a pass, reports a revocation of every own card +/// (`revoke_all`: why), and folds the result and this device's active +/// publications into `report` ([`summarize`]). +async fn finish_pass( + dht: &PodcastDht, + report: &mut PublicationReport, + revoke_all: Option, + shared: Vec, + stats: PodcastDhtSyncStats, +) { + for failure in &stats.failed { + // Failures may concern revoked (now private) podcasts: no ids. + tracing::warn!(error = %failure.error, "podcast publication step failed"); + } + if let Some(reason) = revoke_all { + // Worth telling the user only when something was published. + if stats.unpublished > 0 || !stats.failed.is_empty() { + tracing::warn!( + unpublished = stats.unpublished, + "podcast publication: {reason}; own public cards revoked" + ); + report.error = Some(format!("{reason}; own public cards revoked")); + } else { + tracing::debug!("podcast publication: {reason}; nothing is published"); + } + } + let active: BTreeSet = match dht.published().await { + Ok(published) => published + .into_iter() + .filter(|publication| publication.active) + .map(|publication| publication.podcast_id) + .collect(), + Err(err) => { + report.error = Some(format!("cannot read own publications: {err}")); + BTreeSet::new() + } + }; + summarize(report, shared, stats, &active); +} + +/// Folds one `sync_shares` result and the set of this device's active +/// publications into `report`: the state of each shared podcast, the cards +/// of private podcasts that are still live (`unrevoked`), and an error when +/// revocations failed (counts only, no ids). +pub(super) fn summarize( + report: &mut PublicationReport, + shared: Vec, + stats: PodcastDhtSyncStats, + active: &BTreeSet, +) { + let shared_set: BTreeSet<&PodcastId> = shared.iter().collect(); + let revoke_failed: BTreeSet = stats + .failed + .iter() + .filter(|failure| !shared_set.contains(&failure.podcast_id)) + .map(|failure| failure.podcast_id.clone()) + .collect(); + // Cards of podcasts that are private now but still live from here. + report.unrevoked = active + .iter() + .filter(|podcast_id| !shared_set.contains(podcast_id)) + .cloned() + .chain(revoke_failed.iter().cloned()) + .collect(); + if !revoke_failed.is_empty() && report.error.is_none() { + let count = revoke_failed.len(); + tracing::warn!(count, "podcast publication: public cards not revoked yet"); + report.error = Some(format!( + "{count} public card{} could not be revoked yet; retrying", + if count == 1 { "" } else { "s" } + )); + } + let held: BTreeMap> = stats + .held_back + .into_iter() + .map(|hold| (hold.podcast_id, hold.warnings)) + .collect(); + let failed: BTreeMap = stats + .failed + .into_iter() + .map(|failure| (failure.podcast_id, failure.error)) + .collect(); + for podcast_id in shared { + let publication = if let Some(warnings) = held.get(&podcast_id) { + Publication::HeldBack(warnings.clone()) + } else if let Some(error) = failed.get(&podcast_id) { + Publication::Failed(error.clone()) + } else if active.contains(&podcast_id) { + Publication::Published + } else { + Publication::Pending + }; + report.podcasts.insert(podcast_id, publication); + } +} + +/// Wakes the running reconciler. +#[derive(Debug, Clone)] +pub struct Reconciler { + /// Debounced passes (feed refreshes, received operations). + wake: Arc, + /// Immediate passes (revocations). + urgent: Arc, + /// Number of revocations requested so far. + revocations: Arc, + /// Revocations whose decision is still being written + /// ([`Reconciler::begin_revocation`]). + in_flight: Arc>, + /// Podcasts revoked here that were shared just before, not yet seen by + /// a pass. A pass without the overlay keeps them as not revoked. + revoked: Arc>>, + /// The latest finished pass. + covered: watch::Receiver, +} + +/// What the latest finished pass covered. +#[derive(Debug, Clone, Copy, Default)] +struct Covered { + /// Revocations requested before it started. + revocations: u64, + /// Cards of private podcasts it found (or kept as) not revoked. + unrevoked: usize, +} + +/// A share-off or unsubscribe being written: quitting waits for it +/// ([`Reconciler::flush`]). Dropping it without [`RevocationTicket::done`] +/// means the decision was not written. +#[derive(Debug)] +pub struct RevocationTicket { + reconciler: Reconciler, +} + +impl RevocationTicket { + /// The decision was written: revoke now ([`Reconciler::revoke_now`]). + pub fn done(self, was_shared: Option<&PodcastId>) { + self.reconciler.revoke_now(was_shared); + } +} + +impl Drop for RevocationTicket { + fn drop(&mut self) { + self.reconciler + .in_flight + .send_modify(|in_flight| *in_flight = in_flight.saturating_sub(1)); + } +} + +impl Reconciler { + /// Requests a reconciliation soon (after [`RECONCILE_DEBOUNCE`]). A + /// request during a pass is kept and runs another pass afterwards. + pub fn wake(&self) { + self.wake.notify_one(); + } + + /// A share-off or unsubscribe is about to be written (off the UI + /// thread): from now on, quitting waits for it to be written and + /// revoked. Call before starting the write, so a quit that is handled + /// before the write finishes still waits. + pub fn begin_revocation(&self) -> RevocationTicket { + self.in_flight.send_modify(|in_flight| *in_flight += 1); + RevocationTicket { + reconciler: self.clone(), + } + } + + /// Requests a reconciliation now, without the debounce: sharing was + /// turned off or a podcast unsubscribed, and its card must be revoked + /// before the user may quit. `was_shared`: the podcast, when it was + /// shared just before (a card from this device may be live). See + /// [`Reconciler::flush`]. + pub fn revoke_now(&self, was_shared: Option<&PodcastId>) { + if let Some(podcast_id) = was_shared { + self.revoked + .lock() + .unwrap_or_else(PoisonError::into_inner) + .insert(podcast_id.clone()); + } + self.revocations.fetch_add(1, Ordering::SeqCst); + self.urgent.notify_one(); + } + + /// Waits, at most `timeout`, until the revocations being written + /// ([`Reconciler::begin_revocation`]) are written and a pass started + /// after every [`Reconciler::revoke_now`] so far finished (the + /// tombstones were signed and pushed, or the overlay is off and nothing + /// can be sent). Returns at once when nothing is pending. `true` when + /// that pass left no card of a private podcast live from this device; + /// `false` when one may be (revocation failed, or the federation is off + /// and cards of an earlier session may be live), on timeout, or when + /// the reconciler stopped. + pub async fn flush(&self, timeout: Duration) -> bool { + let wait = async { + let mut in_flight = self.in_flight.subscribe(); + if in_flight + .wait_for(|in_flight| *in_flight == 0) + .await + .is_err() + { + return false; + } + let target = self.revocations.load(Ordering::SeqCst); + let mut covered = self.covered.clone(); + // `revoke_now` already asked for the pass. + match covered + .wait_for(|covered| covered.revocations >= target) + .await + { + Ok(covered) => covered.unrevoked == 0, + Err(_) => false, + } + }; + tokio::time::timeout(timeout, wait).await.unwrap_or(false) + } +} + +/// The podcasts of `unrevoked` that are not shared now (a podcast shared +/// again is no longer "private · not revoked"). Without a store, all. +async fn still_private( + store: Option<&Arc>, + unrevoked: BTreeSet, +) -> BTreeSet { + let Some(store) = store.cloned() else { + return unrevoked; + }; + if unrevoked.is_empty() { + return unrevoked; + } + let fallback = unrevoked.clone(); + tokio::task::spawn_blocking(move || { + unrevoked + .into_iter() + .filter(|podcast_id| { + !matches!(store.podcast(podcast_id), Ok(Some(summary)) if summary.shared) + }) + .collect() + }) + .await + .unwrap_or(fallback) +} + +/// Runs the reconciler until the overlay watch closes. `dht` follows the +/// federation node; while it is `None` nothing is published from this +/// device and `report` says so, keeping as not revoked the cards known not +/// to be revoked yet and those of podcasts that were shared and revoked +/// here meanwhile (`Reconciler::revoke_now`): they are revoked once the +/// overlay runs again. Between passes the overlay's peer count is +/// refreshed every [`PEER_REFRESH_INTERVAL`]. +/// +/// `registers` says whether the share registers are current +/// ([`crate::devices::DeviceSync::podcast_registers_current`]). Until it +/// turns `true` a pass only revokes ([`revoke_unshared`]): a device that +/// comes back with a "share" decision another device revoked meanwhile does +/// not publish it again. A pass runs as soon as it turns `true`. +pub fn spawn( + mut dht: watch::Receiver>>, + mut registers: watch::Receiver, + store: Option>, + confirmations: Arc, + report: impl Fn(PublicationReport) + Send + 'static, +) -> Reconciler { + let wake = Arc::new(Notify::new()); + let urgent = Arc::new(Notify::new()); + let revocations = Arc::new(AtomicU64::new(0)); + let revoked = Arc::new(Mutex::new(BTreeSet::new())); + let (covered_tx, covered) = watch::channel(Covered::default()); + let reconciler = Reconciler { + wake: Arc::clone(&wake), + urgent: Arc::clone(&urgent), + revocations: Arc::clone(&revocations), + in_flight: Arc::new(watch::channel(0).0), + revoked: Arc::clone(&revoked), + covered, + }; + tokio::spawn(async move { + let mut last = PublicationReport::default(); + // The registers watch stays as it is once its sender is gone. + let mut registers_open = true; + let mut peers = tokio::time::interval(PEER_REFRESH_INTERVAL); + peers.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + peers.reset(); + loop { + // Read before the revoked podcasts: `revoke_now` records the + // podcast before it counts the revocation. + let requested = revocations.load(Ordering::SeqCst); + let revoked_here = + std::mem::take(&mut *revoked.lock().unwrap_or_else(PoisonError::into_inner)); + let current = dht.borrow_and_update().clone(); + let registers_current = *registers.borrow_and_update(); + last = match current { + // The pass sees this device's own publications: it revokes + // and reports exactly what is still live. + Some(dht) if registers_current => { + reconcile(&dht, store.clone(), Arc::clone(&confirmations)).await + } + Some(dht) => revoke_unshared(&dht, store.clone(), Arc::clone(&confirmations)).await, + None => { + let mut unrevoked = std::mem::take(&mut last.unrevoked); + unrevoked.extend(revoked_here); + PublicationReport { + discovery: Discovery::Off( + "federation is off or podcast discovery is unavailable".into(), + ), + unrevoked: still_private(store.as_ref(), unrevoked).await, + ..PublicationReport::default() + } + } + }; + report(last.clone()); + covered_tx.send_replace(Covered { + revocations: requested, + unrevoked: last.unrevoked.len(), + }); + let periodic = tokio::time::sleep(RECONCILE_INTERVAL); + tokio::pin!(periodic); + loop { + tokio::select! { + _ = urgent.notified() => break, + _ = wake.notified() => { + tokio::time::sleep(RECONCILE_DEBOUNCE).await; + // Wakes during the debounce are covered by this + // pass: drop the permit they left. + let _ = futures_util::FutureExt::now_or_never(wake.notified()); + break; + } + changed = dht.changed() => { + if changed.is_err() { + return; + } + break; + } + changed = registers.changed(), if registers_open => { + if changed.is_err() { + registers_open = false; + continue; + } + break; + } + _ = &mut periodic => break, + _ = peers.tick() => { + // Keep the peer count live (the first bootstrap + // usually finishes after the first pass). + let Discovery::Running { peers: shown } = &mut last.discovery else { + continue; + }; + let Some(known) = dht.borrow().as_ref().map(|dht| dht.known_peers()) else { + continue; + }; + if *shown != known { + *shown = known; + report(last.clone()); + } + } + } + } + } + }); + reconciler +} + +/// Russian text of a privacy warning, for the share confirmation dialog. +pub fn warning_ru(warning: &FeedUrlPrivacyWarning) -> String { + match warning { + FeedUrlPrivacyWarning::SecretQueryParameter { name } => { + format!("в адресе ленты есть параметр «{name}», похожий на ключ доступа") + } + FeedUrlPrivacyWarning::PrivateFeedHost { host } => { + format!("{host} обычно выдаёт личные ленты для платных подписчиков") + } + FeedUrlPrivacyWarning::HighEntropyQueryValue { name } => { + format!("значение параметра «{name}» похоже на секретный токен") + } + FeedUrlPrivacyWarning::HighEntropyPathSegment { index } => { + format!("часть пути адреса №{} похожа на секретный токен", index + 1) + } + other => other.to_string(), + } +} diff --git a/src/podcasts/publication_tests.rs b/src/podcasts/publication_tests.rs new file mode 100644 index 0000000..ad0bf06 --- /dev/null +++ b/src/podcasts/publication_tests.rs @@ -0,0 +1,1213 @@ +//! Publication of shared podcasts: share states built from the store, the +//! local privacy confirmations, and a two-node loopback test of the real +//! discovery overlay (no internet, temp directories only). + +use std::sync::Arc; +use std::time::Duration; + +use anyhow::Result; +use furumi_library::podcasts::{OpStamp, PodcastLocalOp, PodcastStore, StampSource}; +use music_dht::device_sync::SyncOpWire; +use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta}; +use music_dht::podcast_dht::{ + FeedUrlPrivacyWarning, PODCAST_DHT_ALPN, PodcastDht, PodcastDhtSyncStats, PodcastSyncFailure, +}; +use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId}; + +use super::publication::{ + Discovery, Publication, PublicationReport, ShareConfirmations, check_share, reconcile, + set_shared, share_card, share_plan, share_states, summarize, warning_ru, +}; + +const SHOW_URL: &str = "https://feeds.example.test/rust-radio.xml"; +const SECRET_URL: &str = "https://feeds.example.test/members.xml?token=0123456789abcdef"; +const PRIVATE_URL: &str = "https://feeds.example.test/diary.xml"; +const NEWS_URL: &str = "https://feeds.example.test/crab-news.xml"; + +/// Stamps of one device's local actions; keeps the operations they emit. +pub(super) struct Device { + id: &'static str, + seq: i64, + ops: Vec, +} + +struct Stamps<'a> { + device: &'a str, + next: i64, +} + +impl StampSource for Stamps<'_> { + fn device_id(&self) -> &str { + self.device + } + + fn next_stamp(&mut self) -> Result { + self.next += 1; + Ok(OpStamp::new( + format!("{}:{}", self.device, self.next), + self.device, + 1_700_000_000_000 + self.next, + )) + } +} + +impl Device { + pub(super) fn new(id: &'static str) -> Self { + Self { + id, + seq: 0, + ops: Vec::new(), + } + } + + fn act( + &mut self, + action: impl FnOnce(&mut dyn StampSource) -> Result>, + ) -> Vec { + let mut stamps = Stamps { + device: self.id, + next: self.seq, + }; + let ops = action(&mut stamps).unwrap(); + let mut wires = Vec::new(); + for op in ops { + self.seq += 1; + wires.push(op.into_wire(self.seq)); + } + self.ops.extend(wires.iter().cloned()); + wires + } + + fn subscribe(&mut self, store: &PodcastStore, url: &str, title: &str) -> PodcastId { + let meta = PodcastMeta { + feed_url: FeedUrl::parse(url).unwrap(), + title: title.to_string(), + author: Some("Ferris Crab".to_string()), + description: Some("Weekly news.".to_string()), + link: None, + image_url: None, + }; + self.subscribe_meta(store, &meta) + } + + fn subscribe_meta(&mut self, store: &PodcastStore, meta: &PodcastMeta) -> PodcastId { + let mut id = None; + self.act(|stamps| { + let outcome = store.subscribe(stamps, meta)?; + id = Some(outcome.podcast_id); + Ok(outcome.ops) + }); + id.unwrap() + } + + pub(super) fn set_shared( + &mut self, + store: &PodcastStore, + id: &PodcastId, + shared: bool, + ) -> Vec { + self.act(|stamps| store.set_shared(stamps, id, shared)) + } + + fn unsubscribe(&mut self, store: &PodcastStore, id: &PodcastId) { + self.act(|stamps| store.unsubscribe(stamps, id)); + } +} + +/// The user confirms the URLs the dialog shows for a podcast that is +/// already shared (held back here): no new decision is written, the +/// confirmation is bound to the one in force (as the runtime does). +fn confirm_shown(store: &PodcastStore, confirmations: &ShareConfirmations, id: &PodcastId) { + let check = check_share(store, confirmations, id).unwrap(); + set_shared(store, confirmations, id, true, Some(check.urls), || Ok(())).unwrap(); +} + +/// Whether this device would publish `id` with its privacy confirmed. +fn confirmed_here( + store: &PodcastStore, + confirmations: &ShareConfirmations, + id: &PodcastId, +) -> bool { + share_states(store, confirmations) + .unwrap() + .into_iter() + .find(|state| state.subscription.podcast_id == *id) + .is_some_and(|state| state.is_shared() && state.privacy_confirmed) +} + +fn shared_ids(store: &PodcastStore, confirmations: &ShareConfirmations) -> Vec { + share_states(store, confirmations) + .unwrap() + .into_iter() + .filter(|state| state.is_shared()) + .map(|state| state.subscription.podcast_id) + .collect() +} + +#[test] +fn share_states_carry_cards_only_for_shared_podcasts() { + let store = PodcastStore::open_in_memory().unwrap(); + let confirmations = ShareConfirmations::in_memory(); + let mut device = Device::new("device-a"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + let diary = device.subscribe(&store, PRIVATE_URL, "Secret Garden Diaries"); + + // A new subscription is private: no state is shared, no card is built. + let states = share_states(&store, &confirmations).unwrap(); + assert_eq!(states.len(), 2); + assert!(states.iter().all(|state| !state.is_shared())); + assert!(states.iter().all(|state| state.card.is_none())); + + device.set_shared(&store, &show, true); + let states = share_states(&store, &confirmations).unwrap(); + let shown = states + .iter() + .find(|state| state.subscription.podcast_id == show) + .unwrap(); + assert!(shown.is_shared()); + let card = shown.card.clone().unwrap(); + assert_eq!(card.feed_url, FeedUrl::parse(SHOW_URL).unwrap()); + assert_eq!(card.id_feed_url, None, "the feed did not move"); + assert_eq!(card.title, "Rust Radio Weekly"); + assert_eq!(card.episode_count, Some(0)); + assert!(!shown.privacy_confirmed); + let private = states + .iter() + .find(|state| state.subscription.podcast_id == diary) + .unwrap(); + assert!(!private.is_shared()); + assert!(private.card.is_none()); + assert_eq!(shared_ids(&store, &confirmations), vec![show.clone()]); + + // Share off and unsubscribe both make the podcast private again. + device.set_shared(&store, &show, false); + assert!(shared_ids(&store, &confirmations).is_empty()); + device.set_shared(&store, &show, true); + assert_eq!(shared_ids(&store, &confirmations), vec![show.clone()]); + device.unsubscribe(&store, &show); + assert!(shared_ids(&store, &confirmations).is_empty()); + // Subscribing again is private until shared again. + device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + assert!(shared_ids(&store, &confirmations).is_empty()); +} + +#[test] +fn a_stale_share_decision_from_another_device_does_not_republish() { + let store = PodcastStore::open_in_memory().unwrap(); + let confirmations = ShareConfirmations::in_memory(); + let mut device = Device::new("device-a"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + let shared = device.set_shared(&store, &show, true); + + // Another trusted device turns sharing off; its operation arrives here. + let other = PodcastStore::open_in_memory().unwrap(); + for op in &device.ops { + other.apply_remote_op(op).unwrap(); + } + let mut device_b = Device::new("device-b"); + let revoke = device_b.set_shared(&other, &show, false); + for op in &revoke { + store.apply_remote_op(op).unwrap(); + } + assert!(shared_ids(&store, &confirmations).is_empty()); + + // The old "share" decision delivered again (a late device) loses. + for op in &shared { + store.apply_remote_op(op).unwrap(); + } + assert!(shared_ids(&store, &confirmations).is_empty()); +} + +#[test] +fn privacy_confirmations_are_local_and_bound_to_the_urls() { + let temp = tempfile::tempdir().unwrap(); + let path = temp.path().join("podcast-share-confirmations.json"); + let store = PodcastStore::open_in_memory().unwrap(); + let mut device = Device::new("device-a"); + let secret = device.subscribe(&store, SECRET_URL, "Members Only"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + + let confirmations = ShareConfirmations::open(path.clone()); + let check = check_share(&store, &confirmations, &show).unwrap(); + assert!(check.warnings.is_empty()); + assert!(!check.needs_confirmation()); + + let check = check_share(&store, &confirmations, &secret).unwrap(); + assert!(check.needs_confirmation()); + assert!(matches!( + check.warnings.as_slice(), + [FeedUrlPrivacyWarning::SecretQueryParameter { name }] if name == "token" + )); + assert_eq!( + warning_ru(&check.warnings[0]), + "в адресе ленты есть параметр «token», похожий на ключ доступа" + ); + // The user confirms in the dialog; the decision is written after the + // (pending) confirmation, which is then bound to it. + set_shared( + &store, + &confirmations, + &secret, + true, + Some(check.urls.clone()), + || { + device.set_shared(&store, &secret, true); + Ok(()) + }, + ) + .unwrap(); + + // Persisted on this device, read again after a restart. + let reopened = ShareConfirmations::open(path.clone()); + let check = check_share(&store, &reopened, &secret).unwrap(); + assert!(check.confirmed); + assert!(!check.needs_confirmation()); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + let mode = std::fs::metadata(&path).unwrap().permissions().mode(); + assert_eq!(mode & 0o077, 0, "only the user may read the feed URLs"); + } + + // Other URLs (the feed moved) are not covered. + let card = share_card(&store, &secret).unwrap().unwrap(); + let moved = music_dht::podcast_dht::PodcastCardSpec { + feed_url: FeedUrl::parse("https://feeds.example.test/members.xml?token=ffffffffffffffff") + .unwrap(), + id_feed_url: Some(card.feed_url.clone()), + ..card.clone() + }; + let share = store.share(&secret).unwrap(); + assert!(reopened.is_confirmed(&card, share.as_ref())); + assert!(!reopened.is_confirmed(&moved, share.as_ref())); + + // The share state uses the confirmation. + assert!(confirmed_here(&store, &reopened, &secret)); + + reopened.forget(&secret).unwrap(); + assert!(!ShareConfirmations::open(path.clone()).is_confirmed(&card, share.as_ref())); + + // A corrupt file asks again instead of failing. + std::fs::write(&path, b"{not json").unwrap(); + assert!(!ShareConfirmations::open(path).is_confirmed(&card, share.as_ref())); +} + +#[test] +fn a_confirmation_does_not_outlive_a_revocation_on_another_device() { + let temp = tempfile::tempdir().unwrap(); + let store = PodcastStore::open_in_memory().unwrap(); + let confirmations = ShareConfirmations::open(temp.path().join("confirmations.json")); + let mut device = Device::new("device-a"); + let secret = device.subscribe(&store, SECRET_URL, "Members Only"); + let other = PodcastStore::open_in_memory().unwrap(); + let mut device_b = Device::new("device-b"); + let sync = |from: &[SyncOpWire], to: &PodcastStore| { + for op in from { + to.apply_remote_op(op).unwrap(); + } + }; + + // A: the dialog, confirmed; the confirmation covers A's decision. + let check = check_share(&store, &confirmations, &secret).unwrap(); + assert!(check.needs_confirmation()); + set_shared( + &store, + &confirmations, + &secret, + true, + Some(check.urls), + || { + device.set_shared(&store, &secret, true); + Ok(()) + }, + ) + .unwrap(); + assert!(confirmed_here(&store, &confirmations, &secret)); + + // B turns sharing off and on again (confirming on B). A must ask its + // user again: the confirmation was given for the revoked decision. + sync(&device.ops, &other); + let ops = [ + device_b.set_shared(&other, &secret, false), + device_b.set_shared(&other, &secret, true), + ] + .concat(); + sync(&ops, &store); + assert!( + !confirmed_here(&store, &confirmations, &secret), + "held back again" + ); + let check = check_share(&store, &confirmations, &secret).unwrap(); + assert!(check.needs_confirmation()); + assert!( + !ShareConfirmations::open(temp.path().join("confirmations.json")).is_confirmed( + &share_card(&store, &secret).unwrap().unwrap(), + store.share(&secret).unwrap().as_ref() + ), + "the superseded confirmation is dropped from the file" + ); + + // A confirms again (no new decision: B's is in force) ... + confirm_shown(&store, &confirmations, &secret); + assert!(confirmed_here(&store, &confirmations, &secret)); + // ... and B only turns sharing off: when it is shared again later + // (from either device), A asks again. + let off = device_b.set_shared(&other, &secret, false); + sync(&off, &store); + assert!(shared_ids(&store, &confirmations).is_empty()); + let check = check_share(&store, &confirmations, &secret).unwrap(); + assert!(check.needs_confirmation(), "sharing again asks again"); + + // Shared again on B and confirmed on A; then B unsubscribes, subscribes + // again and shares: a new decision, A asks again. + let on = device_b.set_shared(&other, &secret, true); + sync(&on, &store); + confirm_shown(&store, &confirmations, &secret); + assert!(confirmed_here(&store, &confirmations, &secret)); + device_b.unsubscribe(&other, &secret); + device_b.subscribe(&other, SECRET_URL, "Members Only"); + let on = device_b.set_shared(&other, &secret, true); + assert!(!on.is_empty()); + sync(&device_b.ops, &store); + assert!(!confirmed_here(&store, &confirmations, &secret)); + + // A pending confirmation (the decision is not written yet) covers + // nothing until a newer share decision exists. + let fresh = device.subscribe( + &store, + "https://feeds.example.test/vip.xml?key=0123456789abcdef", + "VIP", + ); + let check = check_share(&store, &confirmations, &fresh).unwrap(); + confirmations + .confirm(&fresh, check.urls, store.share(&fresh).unwrap().as_ref()) + .unwrap(); + assert!( + !check_share(&store, &confirmations, &fresh) + .unwrap() + .confirmed + ); + device.set_shared(&store, &fresh, true); + assert!( + confirmed_here(&store, &confirmations, &fresh), + "covers the next decision" + ); +} + +#[test] +fn a_confirmation_for_a_decision_that_was_not_written_is_dropped() { + let temp = tempfile::tempdir().unwrap(); + let path = temp.path().join("confirmations.json"); + let store = PodcastStore::open_in_memory().unwrap(); + let confirmations = ShareConfirmations::open(path.clone()); + let mut device = Device::new("device-a"); + let secret = device.subscribe(&store, SECRET_URL, "Members Only"); + let other = PodcastStore::open_in_memory().unwrap(); + let mut device_b = Device::new("device-b"); + + // The user confirms, but the decision cannot be written (for example + // the podcast was unsubscribed by a sync meanwhile, or the database + // failed): nothing of the confirmation is left. + let check = check_share(&store, &confirmations, &secret).unwrap(); + assert!(check.needs_confirmation()); + let result = set_shared( + &store, + &confirmations, + &secret, + true, + Some(check.urls.clone()), + || anyhow::bail!("not subscribed"), + ); + assert!(result.is_err()); + assert!(shared_ids(&store, &confirmations).is_empty()); + + // Days later sharing is turned on on device B: A was never asked for + // that decision, so it holds the podcast back. + for op in &device.ops { + other.apply_remote_op(op).unwrap(); + } + for op in &device_b.set_shared(&other, &secret, true) { + store.apply_remote_op(op).unwrap(); + } + assert!(!confirmed_here(&store, &confirmations, &secret)); + assert!( + share_plan(&store, &confirmations) + .unwrap() + .held + .contains_key(&secret), + "held back on A" + ); + assert!( + check_share(&store, &confirmations, &secret) + .unwrap() + .needs_confirmation() + ); + assert!(!ShareConfirmations::open(path.clone()).is_confirmed( + &share_card(&store, &secret).unwrap().unwrap(), + store.share(&secret).unwrap().as_ref() + )); + + // A pending confirmation left in the file by a call that never + // finished (the app was killed before the decision was written) is + // dropped when the file is read again. + let fresh = device.subscribe( + &store, + "https://feeds.example.test/vip.xml?key=0123456789abcdef", + "VIP", + ); + let check = check_share(&store, &confirmations, &fresh).unwrap(); + confirmations + .confirm(&fresh, check.urls, store.share(&fresh).unwrap().as_ref()) + .unwrap(); + let reopened = ShareConfirmations::open(path); + device.set_shared(&store, &fresh, true); + assert!(confirmed_here(&store, &confirmations, &fresh)); + assert!(!confirmed_here(&store, &reopened, &fresh)); +} + +#[test] +fn cards_leave_out_artwork_and_links_that_look_private() { + let store = PodcastStore::open_in_memory().unwrap(); + let mut device = Device::new("device-a"); + let meta = |feed: &str, image: &str, link: &str| PodcastMeta { + feed_url: FeedUrl::parse(feed).unwrap(), + title: "Art Show".to_string(), + author: None, + description: None, + image_url: Some(image.to_string()), + link: Some(link.to_string()), + }; + // A public feed: plain URLs stay, a signed one is left out. + let show = device.subscribe_meta( + &store, + &meta( + SHOW_URL, + "https://cdn.example.test/art.jpg?token=0123456789abcdef", + "https://rust-radio.example.test/", + ), + ); + let card = share_card(&store, &show).unwrap().unwrap(); + assert_eq!( + card.image_url, None, + "the signed artwork URL is not published" + ); + assert_eq!( + card.link.as_deref(), + Some("https://rust-radio.example.test/") + ); + assert!(card.privacy_warnings().is_empty()); + + // Public hosts name artwork and pages by UUIDs and content hashes in + // the path: kept (a key in the query is not). + for public in [ + "https://megaphone.imgix.net/podcasts/1f2e3d4c-5b6a-11ee-8c99-0242ac120002/image/art.jpg", + "https://image.simplecastcdn.com/images/4c3b2a19-8d7e-4f6a-b5c4-d3e2f1a0b9c8/3a2b1c0d-9e8f-4a7b-6c5d-4e3f2a1b0c9d/3000x3000/art.jpg", + "https://is1-ssl.mzstatic.com/image/thumb/Podcasts126/v4/0a/1b/2c/0a1b2c3d-4e5f-6a7b-8c9d-0e1f2a3b4c5d/mza_1234567890123456789.jpg/600x600bb.jpg", + "https://d3t3ozftmdmh3i.cloudfront.net/production/podcast_uploaded/1234567/1234567-1600000000000-0123456789abc.jpg", + ] { + assert!( + FeedUrl::parse(public) + .ok() + .and_then(|url| music_dht::podcast_dht::feed_url_privacy_warning(&url)) + .is_some(), + "the feed URL rules flag {public}" + ); + let show = device.subscribe_meta( + &store, + &PodcastMeta { + feed_url: FeedUrl::parse(&format!( + "https://feeds.example.test/{}.xml", + public.len() + )) + .unwrap(), + ..meta(SHOW_URL, public, public) + }, + ); + let card = share_card(&store, &show).unwrap().unwrap(); + assert_eq!(card.image_url.as_deref(), Some(public)); + assert_eq!(card.link.as_deref(), Some(public)); + } + for signed in [ + "https://cdn.example.test/art.jpg?sig=Zm9vYmFyYmF6cXV4cXV1eDEyMzQ1Njc4OQ", + "https://www.patreon.com/file?h=1234&i=5678", + "ftp://cdn.example.test/art.jpg", + ] { + let show = device.subscribe_meta( + &store, + &PodcastMeta { + feed_url: FeedUrl::parse(&format!( + "https://feeds.example.test/signed-{}.xml", + signed.len() + )) + .unwrap(), + ..meta(SHOW_URL, signed, signed) + }, + ); + let card = share_card(&store, &show).unwrap().unwrap(); + assert_eq!((card.image_url, card.link), (None, None), "{signed}"); + } + + // A member feed (its URL needs the dialog): artwork and page are left + // out whatever they look like, the dialog shows the feed URLs only. + let secret = device.subscribe_meta( + &store, + &meta( + SECRET_URL, + "https://cdn.example.test/members-art.jpg", + "https://members.example.test/show", + ), + ); + let card = share_card(&store, &secret).unwrap().unwrap(); + assert!(!card.privacy_warnings().is_empty()); + assert_eq!((card.image_url, card.link), (None, None)); +} + +#[test] +fn revocations_that_fail_are_reported_without_ids() { + let show = PodcastId::from_feed_url(&FeedUrl::parse(SHOW_URL).unwrap()); + let diary = PodcastId::from_feed_url(&FeedUrl::parse(PRIVATE_URL).unwrap()); + let secret = PodcastId::from_feed_url(&FeedUrl::parse(SECRET_URL).unwrap()); + + // The tombstone of the private diary could not be pushed: its card is + // still live and the user is told, without the podcast's id or URL. + let mut report = PublicationReport::default(); + let stats = PodcastDhtSyncStats { + failed: vec![PodcastSyncFailure { + podcast_id: diary.clone(), + error: "no replica reached".into(), + }], + ..PodcastDhtSyncStats::default() + }; + let active = [show.clone(), diary.clone()].into_iter().collect(); + summarize(&mut report, vec![show.clone()], stats, &active); + assert_eq!(report.podcasts.get(&show), Some(&Publication::Published)); + assert_eq!( + report.podcasts.get(&diary), + None, + "private: not listed as shared" + ); + assert_eq!(report.unrevoked, [diary.clone()].into_iter().collect()); + let error = report.error.clone().unwrap(); + assert_eq!(error, "1 public card could not be revoked yet; retrying"); + assert!(!error.contains(diary.as_str())); + + // A private podcast whose card is still active (not revoked yet) is + // unrevoked too, without an error; a failed shared one is Failed. + let mut report = PublicationReport::default(); + let stats = PodcastDhtSyncStats { + failed: vec![PodcastSyncFailure { + podcast_id: show.clone(), + error: "signing failed".into(), + }], + ..PodcastDhtSyncStats::default() + }; + let active = [secret.clone()].into_iter().collect(); + summarize(&mut report, vec![show.clone()], stats, &active); + assert_eq!( + report.podcasts.get(&show), + Some(&Publication::Failed("signing failed".into())) + ); + assert_eq!(report.unrevoked, [secret.clone()].into_iter().collect()); + assert_eq!(report.error, None); + + // Turning sharing off in the UI counts a published card as not revoked + // until the next pass; a held-back one was never published here. + let mut report = PublicationReport::default(); + report.podcasts.insert(show.clone(), Publication::Published); + report + .podcasts + .insert(secret.clone(), Publication::HeldBack(Vec::new())); + report.discovery = Discovery::Running { peers: 1 }; + report.note_revocation(&show, true); + report.note_revocation(&secret, true); + assert!(report.podcasts.is_empty()); + assert_eq!(report.unrevoked, [show.clone()].into_iter().collect()); + + // Without a pass of the running overlay nothing tells whether a card + // of an earlier session is live: a podcast that was shared counts. + for discovery in [ + Discovery::Off("federation is off".into()), + Discovery::Starting, + ] { + let mut report = PublicationReport { + discovery, + ..PublicationReport::default() + }; + report.note_revocation(&show, true); + report.note_revocation(&diary, false); + assert_eq!(report.unrevoked, [show.clone()].into_iter().collect()); + } +} + +// --------------------------------------------------------------------------- +// Two loopback nodes + +pub(super) struct Node { + pub(super) service: Arc, + pub(super) dht: Arc, + tasks: Vec>, +} + +impl Node { + pub(super) async fn start(directory: &std::path::Path, network: NetworkId) -> Self { + std::fs::create_dir_all(directory).unwrap(); + let config = MusicDhtConfig::builder() + .data_dir(directory) + .network_id(network) + .request_timeout(Duration::from_secs(2)) + .lookup_timeout(Duration::from_secs(5)) + .transport_timeout(Duration::from_secs(5)) + .dial_timeout(Duration::from_secs(2)) + .schema_independent_stream_protocol(PODCAST_DHT_ALPN) + .build() + .unwrap(); + let (service, mut events) = MusicDhtService::start(config).await.unwrap(); + let service = Arc::new(service); + let events = tokio::spawn(async move { while events.recv().await.is_some() {} }); + // Same wiring as `federation::open_podcast_dht`. + let acceptor = service.stream_acceptor(PODCAST_DHT_ALPN).unwrap(); + let dht = PodcastDht::open( + Arc::clone(&service), + directory.join(crate::federation::PODCAST_DHT_CACHE_FILE), + ) + .await + .unwrap(); + let serve = tokio::spawn(Arc::clone(&dht).serve(acceptor)); + Self { + service, + dht, + tasks: vec![events, serve], + } + } + + pub(super) async fn stop(self) { + for task in &self.tasks { + task.abort(); + } + self.service.shutdown().await.unwrap(); + } +} + +pub(super) async fn wait_for(what: &str, mut check: impl AsyncFnMut() -> Option) -> T { + let started = std::time::Instant::now(); + loop { + if let Some(value) = check().await { + return value; + } + assert!( + started.elapsed() < Duration::from_secs(60), + "timed out waiting for {what}" + ); + tokio::time::sleep(Duration::from_millis(300)).await; + } +} + +async fn found(node: &Node, id: &PodcastId) -> bool { + node.dht.lookup_podcast(id).await.unwrap().is_some() +} + +fn publication(report: &PublicationReport, id: &PodcastId) -> Option { + report.podcasts.get(id).cloned() +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn shared_podcast_is_found_by_a_stranger_and_revoked_by_share_off() { + tokio::time::timeout(Duration::from_secs(240), two_nodes()) + .await + .unwrap(); +} + +async fn two_nodes() { + let temp = tempfile::tempdir().unwrap(); + let network = NetworkId::from_name("furumi-tui-podcast-publication-test"); + // A: this TUI with its podcast store. C: a stranger outside A's + // trusted-device group (no device sync at all). + let a = Node::start(&temp.path().join("a"), network).await; + let c = Node::start(&temp.path().join("c"), network).await; + c.service + .connect(a.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("music DHT contacts", async || { + (!a.service.known_peers().is_empty() && !c.service.known_peers().is_empty()).then_some(()) + }) + .await; + for node in [&a, &c] { + node.dht.bootstrap().await.unwrap(); + } + wait_for("overlay contacts", async || { + (a.dht.known_peers() >= 1 && c.dht.known_peers() >= 1).then_some(()) + }) + .await; + + let store = Arc::new(PodcastStore::open_in_memory().unwrap()); + let confirmations = Arc::new(ShareConfirmations::open( + temp.path().join("podcast-share-confirmations.json"), + )); + let mut device = Device::new("device-a"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + let diary = device.subscribe(&store, PRIVATE_URL, "Secret Garden Diaries"); + let secret = device.subscribe(&store, SECRET_URL, "Members Only Show"); + + // Nothing is shared yet: nothing is published. + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(report.error, None); + assert!(report.podcasts.is_empty()); + assert!(a.dht.published().await.unwrap().is_empty()); + + // A shares two podcasts; the one with a private-looking URL is held + // back until confirmed on this device. + device.set_shared(&store, &show, true); + device.set_shared(&store, &secret, true); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(report.error, None); + assert!(matches!(report.discovery, Discovery::Running { peers } if peers >= 1)); + assert_eq!(publication(&report, &show), Some(Publication::Published)); + assert!(matches!( + publication(&report, &secret), + Some(Publication::HeldBack(warnings)) if !warnings.is_empty() + )); + assert_eq!(publication(&report, &diary), None); + + // The stranger finds the shared card by text and by id ... + let hits = wait_for("the shared card", async || { + let hits = c.dht.search("rust radio", 10).await.unwrap(); + (!hits.is_empty()).then_some(hits) + }) + .await; + assert_eq!(hits.len(), 1); + assert_eq!(hits[0].podcast_id, show); + assert_eq!(hits[0].feed_url, FeedUrl::parse(SHOW_URL).unwrap()); + assert_eq!(hits[0].publishers, vec![a.service.endpoint_id()]); + assert!(found(&c, &show).await); + // ... but neither the private subscription nor the held-back one. + assert!(!found(&c, &diary).await); + assert!(c.dht.search("secret garden", 10).await.unwrap().is_empty()); + assert!(!found(&c, &secret).await); + assert!(c.dht.search("members only", 10).await.unwrap().is_empty()); + + // Confirming exactly the URLs shown publishes the held-back podcast. + confirm_shown(&store, &confirmations, &secret); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(publication(&report, &secret), Some(Publication::Published)); + wait_for("the confirmed card", async || { + found(&c, &secret).await.then_some(()) + }) + .await; + + // Another trusted device B turns sharing off and on again while A does + // not reconcile in between (closed, offline, or within the debounce). + // The confirmation was given for the revoked decision: A holds the + // podcast back and revokes its card, although it already publishes the + // same feed URL (FRID alone would keep publishing it). + let other = PodcastStore::open_in_memory().unwrap(); + for op in &device.ops { + other.apply_remote_op(op).unwrap(); + } + let mut device_b = Device::new("device-b"); + let off_on = [ + device_b.set_shared(&other, &secret, false), + device_b.set_shared(&other, &secret, true), + ] + .concat(); + for op in &off_on { + store.apply_remote_op(op).unwrap(); + } + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert!( + matches!( + publication(&report, &secret), + Some(Publication::HeldBack(warnings)) if !warnings.is_empty() + ), + "{report:?}" + ); + assert!( + check_share(&store, &confirmations, &secret) + .unwrap() + .needs_confirmation() + ); + assert!( + a.dht + .published() + .await + .unwrap() + .iter() + .any(|publication| publication.podcast_id == secret && !publication.active), + "the card confirmed for the earlier decision is revoked" + ); + assert!(!found(&c, &secret).await); + // A second pass changes nothing; confirming again publishes it. + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert!(matches!( + publication(&report, &secret), + Some(Publication::HeldBack(_)) + )); + assert!(!found(&c, &secret).await); + confirm_shown(&store, &confirmations, &secret); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(publication(&report, &secret), Some(Publication::Published)); + wait_for("the card confirmed again", async || { + found(&c, &secret).await.then_some(()) + }) + .await; + + // Sharing off: the tombstone wins over the card at the stranger. + let shared_op = device.ops.clone(); + device.set_shared(&store, &show, false); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(publication(&report, &show), None); + assert!(!found(&c, &show).await, "the tombstone hides the card"); + assert!(c.dht.search("rust radio", 10).await.unwrap().is_empty()); + let own = a.dht.published().await.unwrap(); + assert!( + own.iter() + .any(|publication| publication.podcast_id == show && !publication.active) + ); + + // A late copy of the old "share" operation does not republish it. + for op in &shared_op { + store.apply_remote_op(op).unwrap(); + } + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(publication(&report, &show), None); + assert!(!found(&c, &show).await); + + // Unsubscribing revokes too. + device.unsubscribe(&store, &secret); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert!(report.podcasts.is_empty()); + assert!(!found(&c, &secret).await); + + // Without a podcast store (it failed to open) every own card is revoked. + device.subscribe(&store, SECRET_URL, "Members Only Show"); + device.set_shared(&store, &secret, true); + confirm_shown(&store, &confirmations, &secret); + let report = reconcile(&a.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + assert_eq!(publication(&report, &secret), Some(Publication::Published)); + wait_for("the republished card", async || { + found(&c, &secret).await.then_some(()) + }) + .await; + let report = reconcile(&a.dht, None, Arc::clone(&confirmations)).await; + assert!(report.error.is_some()); + assert!(report.podcasts.is_empty()); + // Nothing left to revoke: no error to show again. + let report = reconcile(&a.dht, None, Arc::clone(&confirmations)).await; + assert_eq!(report.error, None); + assert!(!found(&c, &secret).await); + assert!( + a.dht + .published() + .await + .unwrap() + .iter() + .all(|publication| !publication.active) + ); + + a.stop().await; + c.stop().await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_reconciler_follows_the_overlay_and_revokes_without_the_debounce() { + tokio::time::timeout(Duration::from_secs(180), reconciler_lifecycle()) + .await + .unwrap(); +} + +/// `publication::spawn` as the runtime drives it: the federation watch, +/// debounced wakes, immediate revocations with the quit-time flush, the live +/// peer count, and the end of the task with the watch. +async fn reconciler_lifecycle() { + use super::publication::{PEER_REFRESH_INTERVAL, RECONCILE_DEBOUNCE, spawn}; + + let temp = tempfile::tempdir().unwrap(); + let network = NetworkId::from_name("furumi-tui-podcast-reconciler-test"); + let store = Arc::new(PodcastStore::open_in_memory().unwrap()); + let confirmations = Arc::new(ShareConfirmations::in_memory()); + let mut device = Device::new("device-a"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + device.set_shared(&store, &show, true); + + let (overlay, watched) = tokio::sync::watch::channel(None); + let (_registers, current) = tokio::sync::watch::channel(true); + let (tx, mut reports) = tokio::sync::mpsc::unbounded_channel(); + let reconciler = spawn( + watched, + current, + Some(Arc::clone(&store)), + confirmations, + move |report| { + let _ = tx.send(report); + }, + ); + let mut next = async || reports.recv().await.expect("a report"); + + // Federation off: nothing is published, nothing to wait for on quit. + let report = next().await; + assert!(matches!(report.discovery, Discovery::Off(_))); + assert!(report.podcasts.is_empty()); + assert!(reconciler.flush(Duration::from_millis(10)).await); + + // Sharing off while the federation is off (a card of an earlier + // session may be live). Quitting while the decision is still being + // written waits for the write and the pass, then says the card may not + // be revoked; the Off reports keep it as not revoked. + let ticket = reconciler.begin_revocation(); + assert!( + !reconciler.flush(Duration::from_millis(50)).await, + "the write is still running" + ); + let flush = tokio::spawn({ + let reconciler = reconciler.clone(); + async move { reconciler.flush(Duration::from_secs(5)).await } + }); + tokio::time::sleep(Duration::from_millis(100)).await; + assert!(!flush.is_finished(), "quitting waits for the write"); + device.set_shared(&store, &show, false); + ticket.done(Some(&show)); + assert!(!flush.await.unwrap(), "the card may not be revoked yet"); + let report = next().await; + assert!(matches!(report.discovery, Discovery::Off(_))); + assert_eq!(report.unrevoked, [show.clone()].into_iter().collect()); + reconciler.wake(); + let report = next().await; + assert_eq!( + report.unrevoked, + [show.clone()].into_iter().collect(), + "kept while the federation is off" + ); + // A write that failed (the ticket is dropped) is not waited for. + drop(reconciler.begin_revocation()); + // Shared again: no longer "private · not revoked". + device.set_shared(&store, &show, true); + reconciler.wake(); + let report = next().await; + assert!(report.unrevoked.is_empty()); + assert!(reconciler.flush(Duration::from_millis(10)).await); + + // The overlay starts (two nodes, the overlay not bootstrapped yet): a + // pass publishes right away. + let a = Node::start(&temp.path().join("a"), network).await; + let c = Node::start(&temp.path().join("c"), network).await; + c.service + .connect(a.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("music DHT contacts", async || { + (!a.service.known_peers().is_empty()).then_some(()) + }) + .await; + overlay.send_replace(Some(Arc::clone(&a.dht))); + let report = next().await; + assert!(matches!(report.discovery, Discovery::Running { .. })); + assert_eq!(publication(&report, &show), Some(Publication::Published)); + + // The peer count follows the overlay without another pass. + a.dht.bootstrap().await.unwrap(); + c.dht.bootstrap().await.unwrap(); + let mut report = report; + let started = std::time::Instant::now(); + while !matches!(report.discovery, Discovery::Running { peers } if peers >= 1) { + assert!( + started.elapsed() < PEER_REFRESH_INTERVAL * 3, + "the peer count is refreshed" + ); + report = next().await; + } + assert_eq!(publication(&report, &show), Some(Publication::Published)); + + // Wakes within the debounce are handled by one pass. + reconciler.wake(); + reconciler.wake(); + reconciler.wake(); + let report = next().await; + assert_eq!(publication(&report, &show), Some(Publication::Published)); + assert!( + tokio::time::timeout(RECONCILE_DEBOUNCE * 2, reports.recv()) + .await + .is_err(), + "one pass for several wakes" + ); + + // Sharing off: the revocation does not wait for the debounce, and the + // quit-time flush returns once the tombstone was sent. + device.set_shared(&store, &show, false); + reconciler.revoke_now(Some(&show)); + assert!( + reconciler.flush(RECONCILE_DEBOUNCE * 9 / 10).await, + "revoked before the debounce would have ended" + ); + let report = reports.recv().await.unwrap(); + assert!(report.podcasts.is_empty()); + assert!(report.unrevoked.is_empty()); + assert!( + a.dht + .published() + .await + .unwrap() + .iter() + .all(|publication| !publication.active) + ); + assert!(!found(&c, &show).await); + + // The overlay stops: off again; the task ends with the watch. + overlay.send_replace(None); + let mut report = reports.recv().await.unwrap(); + while !matches!(report.discovery, Discovery::Off(_)) { + // A peer count refresh may still come in between. + assert!(report.podcasts.is_empty()); + report = reports.recv().await.unwrap(); + } + drop(overlay); + assert!(reports.recv().await.is_none(), "the reconciler stopped"); + + a.stop().await; + c.stop().await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_restarted_device_publishes_only_after_syncing_with_its_group() { + tokio::time::timeout(Duration::from_secs(240), restart_before_device_sync()) + .await + .unwrap(); +} + +/// Interop scenario 7c: device B was closed while another device of its +/// group turned sharing of a podcast off. Restarted with that stale "share" +/// decision, B does not send the card again while it waits for device sync +/// (`DeviceSync::podcast_registers_current`), still revokes what its own +/// registers no longer share, and publishes once its registers are current. +async fn restart_before_device_sync() { + use super::publication::spawn; + + let temp = tempfile::tempdir().unwrap(); + let network = NetworkId::from_name("furumi-tui-podcast-restart-test"); + let store = Arc::new(PodcastStore::open_in_memory().unwrap()); + let confirmations = Arc::new(ShareConfirmations::in_memory()); + let mut device = Device::new("device-b"); + let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly"); + let news = device.subscribe(&store, NEWS_URL, "Crab News"); + let diary = device.subscribe(&store, PRIVATE_URL, "Ferris Diary"); + for id in [&show, &news, &diary] { + device.set_shared(&store, id, true); + } + + // The earlier session, alone: the three cards are published (and kept + // only in B's own cache). + let b = Node::start(&temp.path().join("b"), network).await; + let report = reconcile(&b.dht, Some(Arc::clone(&store)), Arc::clone(&confirmations)).await; + for id in [&show, &news, &diary] { + assert_eq!(publication(&report, id), Some(Publication::Published)); + } + b.stop().await; + + // While B is closed, device A turns sharing of `news` off. + let other = PodcastStore::open_in_memory().unwrap(); + for op in &device.ops { + other.apply_remote_op(op).unwrap(); + } + let share_off = Device::new("device-a").set_shared(&other, &news, false); + + // B starts again from the same directory, next to a stranger C. + let b = Node::start(&temp.path().join("b"), network).await; + let c = Node::start(&temp.path().join("c"), network).await; + c.service + .connect(b.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("overlay contacts", async || { + b.dht.bootstrap().await.ok()?; + c.dht.bootstrap().await.ok()?; + (b.dht.known_peers() >= 1 && c.dht.known_peers() >= 1).then_some(()) + }) + .await; + + let (_overlay, watched) = tokio::sync::watch::channel(Some(Arc::clone(&b.dht))); + let (registers, current) = tokio::sync::watch::channel(false); + let (tx, mut reports) = tokio::sync::mpsc::unbounded_channel(); + let reconciler = spawn( + watched, + current, + Some(Arc::clone(&store)), + Arc::clone(&confirmations), + move |report| { + let _ = tx.send(report); + }, + ); + let mut next = async |what: &str, check: &dyn Fn(&PublicationReport) -> bool| loop { + let report = tokio::time::timeout(Duration::from_secs(30), reports.recv()) + .await + .unwrap_or_else(|_| panic!("timed out waiting for {what}")) + .expect("a report"); + if check(&report) { + return report; + } + }; + + // Before device sync: nothing is published or sent, not even by the + // overlay's own republishing. + let report = next("the first pass", &|report| { + matches!(report.discovery, Discovery::Running { .. }) + }) + .await; + for id in [&show, &news, &diary] { + assert!(!found(&c, id).await, "a card was sent before device sync"); + } + assert_eq!(b.dht.republish().await.unwrap().records, 0); + for id in [&show, &news, &diary] { + assert!( + !found(&c, id).await, + "a card was republished before device sync" + ); + assert_eq!( + publication(&report, id), + Some(Publication::WaitingForDevices) + ); + } + assert!(report.unrevoked.is_empty()); + + // Sharing turned off here is revoked at once all the same. + device.set_shared(&store, &diary, false); + reconciler.revoke_now(Some(&diary)); + assert!(reconciler.flush(Duration::from_secs(20)).await); + let report = next("the revocation", &|report| { + !report.podcasts.contains_key(&diary) + }) + .await; + assert!(report.unrevoked.is_empty()); + assert_eq!( + publication(&report, &news), + Some(Publication::WaitingForDevices) + ); + assert!( + b.dht + .published() + .await + .unwrap() + .iter() + .any(|publication| publication.podcast_id == diary && !publication.active) + ); + + // Device sync brings A's share-off, then the registers are current: + // `news` is revoked, `show` published. + for op in &share_off { + store.apply_remote_op(op).unwrap(); + } + registers.send_replace(true); + let report = next("the full pass", &|report| { + report.podcasts.get(&show) == Some(&Publication::Published) + }) + .await; + assert_eq!(publication(&report, &news), None); + assert!(report.unrevoked.is_empty()); + wait_for("the shared card", async || { + found(&c, &show).await.then_some(()) + }) + .await; + assert!(!found(&c, &news).await); + assert!(!found(&c, &diary).await); + + b.stop().await; + c.stop().await; +} diff --git a/src/podcasts/refresh.rs b/src/podcasts/refresh.rs index f4ddfd9..20a03af 100644 --- a/src/podcasts/refresh.rs +++ b/src/podcasts/refresh.rs @@ -30,6 +30,8 @@ pub struct Services { pub store: Arc, pub fetcher: Arc, pub log: Arc, + /// Device-local privacy confirmations for sharing (never synced). + pub confirmations: Arc, } impl std::fmt::Debug for Services { @@ -93,24 +95,22 @@ fn duration_ms(duration: Duration) -> i64 { /// saved progress and history. The subscription is private. pub async fn subscribe(services: &Services, input: &str, now_ms: i64) -> Result { let url = FeedUrl::from_user_input(input).map_err(|err| anyhow!("{err}"))?; - let store = Arc::clone(&services.store); - let lookup = url.clone(); - let existing = blocking(move || { - let id = store.resolve_podcast_id(&lookup)?; - let subscribed = store - .subscription(&id)? - .is_some_and(|record| record.subscribed); - let title = store.podcast(&id)?.map(|summary| summary.meta.title); - Ok(subscribed.then_some((id, title))) - }) - .await?; - if let Some((podcast_id, title)) = existing { - return Ok(SubscribeReport { - podcast_id, - title: title.unwrap_or_default(), - already_subscribed: true, - new_episodes: 0, - }); + subscribe_url(services, url, now_ms).await +} + +/// Subscribes to `url` exactly as given (see [`subscribe`]). +/// +/// Subscribing from a public card passes the card's origin URL here +/// (`podcasts::discovery::subscribe_from_hit`): the id then matches the +/// card's `podcast_id` unless this device already knows the URL as an alias +/// of another podcast, and everything stored comes from the feed itself. +pub async fn subscribe_url( + services: &Services, + url: FeedUrl, + now_ms: i64, +) -> Result { + if let Some(report) = already_subscribed(services, &url).await? { + return Ok(report); } let response = services @@ -130,6 +130,18 @@ pub async fn subscribe(services: &Services, input: &str, now_ms: i64) -> Result< // The feed's own address: where its permanent redirects lead. let moved_permanently = response.moved_permanently_to.is_some(); let landed = response.moved_permanently_to.unwrap_or(final_url); + // An older address of a feed this device follows under the newer one it + // announces (`itunes:new-feed-url`, no redirect): the same podcast, not + // a second subscription. Nothing is written. + if let Some(announced) = feed + .new_feed_url + .as_deref() + .and_then(|announced| FeedUrl::parse(announced).ok()) + .filter(|announced| *announced != url && *announced != landed) + && let Some(report) = already_subscribed(services, &announced).await? + { + return Ok(report); + } let store = Arc::clone(&services.store); let log = Arc::clone(&services.log); @@ -191,6 +203,38 @@ pub async fn subscribe(services: &Services, input: &str, now_ms: i64) -> Result< Ok(report) } +/// The subscription `url` belongs to on this device (directly or as a +/// known feed alias), as an "already subscribed" report. +async fn already_subscribed(services: &Services, url: &FeedUrl) -> Result> { + let store = Arc::clone(&services.store); + let lookup = url.clone(); + blocking(move || { + let Some(podcast_id) = subscribed_podcast(&store, &lookup)? else { + return Ok(None); + }; + let title = store + .podcast(&podcast_id)? + .map(|summary| summary.meta.title); + Ok(Some(SubscribeReport { + podcast_id, + title: title.unwrap_or_default(), + already_subscribed: true, + new_episodes: 0, + })) + }) + .await +} + +/// The podcast `url` resolves to on this device (feed aliases included), +/// when it is subscribed. Blocking. +pub(crate) fn subscribed_podcast(store: &PodcastStore, url: &FeedUrl) -> Result> { + let id = store.resolve_podcast_id(url)?; + let subscribed = store + .subscription(&id)? + .is_some_and(|record| record.subscribed); + Ok(subscribed.then_some(id)) +} + fn knows(store: &PodcastStore, url: &FeedUrl) -> Result { let id = store.resolve_podcast_id(url)?; Ok(store.podcast(&id)?.is_some() || store.subscription(&id)?.is_some()) @@ -324,7 +368,7 @@ async fn fetch_and_apply( /// Parses a feed document on the blocking pool; relative URLs resolve /// against where the document was served from. -async fn parse(body: Vec, base: &FeedUrl) -> Result { +pub(crate) async fn parse(body: Vec, base: &FeedUrl) -> Result { let base = base.as_str().to_string(); blocking(move || { parse_feed_with_base(&body, Some(&base)).map_err(|err| anyhow!("unusable feed: {err}")) diff --git a/src/ui/global.rs b/src/ui/global.rs index 3f9ead9..0874040 100644 --- a/src/ui/global.rs +++ b/src/ui/global.rs @@ -947,12 +947,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { let empty_results = SearchResults::default(); let results = match &search.results { Some(results) => results, - None if !state.search.fed_tracks.is_empty() - || !state.search.fed_artists.is_empty() - || state.search.fed_loading => - { - &empty_results - } + None if state.search.has_federated() => &empty_results, None => { let hint = if search.query.is_empty() { "type to search artists, releases and tracks" @@ -967,11 +962,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { return centered_line(frame, inner, line); } }; - if results.is_empty() - && state.search.fed_tracks.is_empty() - && state.search.fed_artists.is_empty() - && !state.search.fed_loading - { + if results.is_empty() && !state.search.has_federated() { return centered_line(frame, inner, Line::styled("nothing found", theme::dim())); } @@ -1037,15 +1028,12 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { } } // Federated section: artists whose card can be assembled, then tracks - // (both marked with the owning peers). - if !state.search.fed_tracks.is_empty() - || !state.search.fed_artists.is_empty() - || state.search.fed_loading - { + // (both marked with the owning peers), then public podcast cards. + if state.search.has_federated() { if !rows.is_empty() { rows.push((Line::default(), None, None)); } - let header = if state.search.fed_loading { + let header = if state.search.fed_loading || state.search.fed_podcasts_loading { "Federation · searching…" } else { "Federation" @@ -1100,6 +1088,23 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { )); index += 1; } + // Podcasts carry an explicit type marker: they are not music and + // Enter opens their public card, never plays anything. + if !state.search.fed_podcasts.is_empty() { + rows.push(( + Line::styled("Podcasts · public cards", theme::header_for(state)), + None, + None, + )); + } + for hit in &state.search.fed_podcasts { + rows.push(( + podcast_hit_line(state, hit), + Some(podcast_hit_meta(state, hit)), + Some(index), + )); + index += 1; + } } // Absolute cursor indices covered by an active Shift-V range in the @@ -1145,6 +1150,47 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { } } +/// One public podcast in the search results: the `podcast` type marker, +/// the title and the claimed author. +fn podcast_hit_line( + state: &AppState, + hit: &crate::podcasts::discovery::PodcastHit, +) -> Line<'static> { + let card = &hit.hit.card; + let mut detail = String::from(" "); + if let Some(author) = &card.author { + detail.push_str(author); + detail.push_str(" · "); + } + detail.push_str("open the card"); + Line::from(vec![ + Span::styled( + format!("[{}] ", hit.hit.kind().as_str()), + theme::accent_for(state), + ), + Span::raw(card.title.clone()), + Span::styled(detail, theme::dim()), + ]) +} + +/// Right column of a podcast hit: subscribed here, or how many devices +/// publish the card (not a subscriber count). +fn podcast_hit_meta(state: &AppState, hit: &crate::podcasts::discovery::PodcastHit) -> String { + if state.podcasts.subscribed_to(hit) { + return "✓ subscribed".to_string(); + } + let mut meta = match hit.hit.card.episode_count { + Some(count) => format!("{count} ep · "), + None => String::new(), + }; + let publishers = hit.hit.publishers.len(); + meta.push_str(&format!( + "{publishers} publisher{}", + if publishers == 1 { "" } else { "s" } + )); + meta +} + fn draw_similarity_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) { let search = &state.search; let status = if search.fed_loading { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 96777cb..815133c 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -4,6 +4,7 @@ mod federation; pub(crate) mod global; mod logs; mod playlists; +mod podcast_card; mod podcasts; mod popup; #[cfg(test)] @@ -181,6 +182,297 @@ mod tests { } } + #[test] + fn podcast_share_state_is_shown_in_the_list_and_details() { + use crate::podcasts::publication::{Discovery, Publication}; + let mut state = + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 3, 0)]); + state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS; + let screen = render(&state, 110, 30); + assert!(screen.contains("private (not shared"), "{screen}"); + assert!(!screen.contains("shared · 3 ep"), "{screen}"); + + let mut row = podcast_fixtures::row("Test Show", 0, 3, 0); + row.summary.shared = true; + let mut state = podcast_fixtures::ready_state(vec![row]); + state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS; + state.podcasts.publication.discovery = Discovery::Running { peers: 2 }; + state + .podcasts + .publication + .podcasts + .insert(podcast_fixtures::podcast_id(), Publication::Published); + let screen = render(&state, 110, 30); + assert!(screen.contains("shared · 3 ep"), "{screen}"); + assert!(screen.contains("shared · published from"), "{screen}"); + + state.podcasts.publication.podcasts.insert( + podcast_fixtures::podcast_id(), + Publication::HeldBack(Vec::new()), + ); + let screen = render(&state, 110, 30); + assert!(screen.contains("held back"), "{screen}"); + + // Restarted with other devices in the group: not published before + // a sync with one of them. + state.podcasts.publication.podcasts.insert( + podcast_fixtures::podcast_id(), + Publication::WaitingForDevices, + ); + let screen = render(&state, 110, 30); + assert!(screen.contains("waiting for a sync"), "{screen}"); + assert!(!screen.contains("published from"), "{screen}"); + + state.podcasts.publication.discovery = Discovery::Off("federation is off".into()); + let screen = render(&state, 110, 30); + assert!(screen.contains("not published:"), "{screen}"); + + // Sharing turned off here: until a pass revoked the card, the panel + // does not claim the podcast is simply private. + state.podcasts.publication.discovery = Discovery::Running { peers: 2 }; + state + .podcasts + .publication + .podcasts + .insert(podcast_fixtures::podcast_id(), Publication::Published); + state + .podcasts + .publication + .note_revocation(&podcast_fixtures::podcast_id(), true); + let screen = render(&state, 110, 30); + assert!(screen.contains("private · revoking the"), "{screen}"); + if let Some(crate::app::state::Loadable::Ready(rows)) = &mut state.podcasts.list { + rows[0].summary.shared = false; + } + let screen = render(&state, 110, 30); + assert!(screen.contains("private · revoking the"), "{screen}"); + assert!(!screen.contains("private (not shared"), "{screen}"); + state.podcasts.publication.discovery = Discovery::Off("federation is off".into()); + let screen = render(&state, 110, 30); + assert!(screen.contains("may not be revoked yet:"), "{screen}"); + assert!(screen.contains("federation is off"), "{screen}"); + state.podcasts.publication.unrevoked.clear(); + let screen = render(&state, 110, 30); + assert!(screen.contains("private (not shared"), "{screen}"); + } + + #[test] + fn the_podcast_card_scrolls_exactly_to_its_last_line() { + use crate::app::state::Popup; + use crate::app::update::podcast_discovery::card_key; + use crossterm::event::KeyCode; + let mut state = search_with_podcasts(); + let mut hit = state.search.fed_podcasts[1].clone(); + // Long words word-wrap into far more rows than their character + // count suggests (one 50-character word per 90-column row). + let word = "x".repeat(50); + hit.hit.card.description = Some(vec![word.as_str(); 30].join(" ")); + state.popup = Some(Popup::PodcastCard { + hit: Box::new(hit), + preview: None, + scroll: 0, + max_scroll: std::cell::Cell::new(u16::MAX), + }); + let last_line = "e — прочитать ленту"; + let screen = render(&state, 110, 30); + assert!(!screen.contains(last_line), "below the fold:\n{screen}"); + + // Paging far past the end stops at the last line ... + for _ in 0..20 { + let popup = state.popup.take().unwrap(); + assert_eq!(card_key(&mut state, popup, KeyCode::PageDown), None); + } + let screen = render(&state, 110, 30); + assert!(screen.contains(last_line), "{screen}"); + let Some(Popup::PodcastCard { + scroll, max_scroll, .. + }) = &state.popup + else { + panic!("the card is open"); + }; + assert_eq!(*scroll, max_scroll.get()); + let end = *scroll; + // ... and one step back moves the view at once. + let popup = state.popup.take().unwrap(); + card_key(&mut state, popup, KeyCode::Up); + assert!(matches!( + &state.popup, + Some(Popup::PodcastCard { scroll, .. }) if *scroll == end - 1 + )); + } + + /// Search view with a federated artist and two public podcast cards; + /// the first podcast is subscribed on this device. + fn search_with_podcasts() -> AppState { + use crate::podcasts::discovery::{PodcastHit, test_hit}; + let mut state = + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 3, 0)]); + state.active_tab = Tab::Global; + state.search.query = "show".into(); + state.search.results = Some(Default::default()); + state.search.fed_artists = vec![crate::federation::FedArtistHit { + name: "Show Band".into(), + peers: 2, + }]; + let mut other = test_hit("https://feeds.example.test/other.xml", "Other Show"); + other.card.author = None; + state.search.fed_podcasts = vec![ + PodcastHit::new(test_hit("https://example.test/feed.xml", "Test Show")), + PodcastHit::new(other), + ]; + state.global.stack.push(GlobalView::Search { cursor: 1 }); + state + } + + #[test] + fn search_marks_podcast_hits_apart_from_music() { + let screen = render(&search_with_podcasts(), 120, 20); + assert!(screen.contains("Federation"), "{screen}"); + assert!( + screen.contains("Show Band artist · open the card"), + "{screen}" + ); + assert!(screen.contains("Podcasts · public cards"), "{screen}"); + assert!( + screen.contains("[podcast] Test Show Ferris Crab · open the card"), + "{screen}" + ); + assert!( + screen.contains("[podcast] Other Show open the card"), + "{screen}" + ); + assert!(screen.contains("✓ subscribed"), "{screen}"); + assert!(screen.contains("12 ep · 1 publisher"), "{screen}"); + // The podcasts come after the music rows. + let artist = screen.find("Show Band").unwrap(); + let podcast = screen.find("[podcast]").unwrap(); + assert!(artist < podcast, "{screen}"); + } + + #[test] + fn podcast_card_offers_subscribe_or_shows_already_subscribed() { + use crate::app::state::{Loadable, Popup}; + use crate::podcasts::discovery::{FeedPreview, PreviewEpisode}; + let mut state = search_with_podcasts(); + let mut hit = state.search.fed_podcasts[1].clone(); + hit.hit.claimed_feed_urls = vec![music_dht::podcast_dht::ClaimedFeedUrl { + feed_url: music_dht::podcast::FeedUrl::parse("https://elsewhere.test/claimed.xml") + .unwrap(), + publishers: hit.hit.publishers.clone(), + }]; + state.popup = Some(Popup::PodcastCard { + hit: Box::new(hit.clone()), + preview: None, + scroll: 0, + max_scroll: std::cell::Cell::new(u16::MAX), + }); + let screen = render(&state, 110, 40); + assert!(screen.contains("[podcast] Other Show"), "{screen}"); + assert!(screen.contains("они не проверены"), "{screen}"); + assert!(screen.contains("Категории: Technology"), "{screen}"); + assert!( + screen.contains("Выпусков: 12 · последний 2024-06-15"), + "{screen}" + ); + assert!( + screen.contains("Опубликовали: 1 устройство (не число подписчиков"), + "{screen}" + ); + assert!( + screen.contains("Лента: https://feeds.example.test/other.xml"), + "{screen}" + ); + assert!( + screen.contains("(не проверен, не используется): https://elsewhere.test/claimed.xml"), + "{screen}" + ); + assert!(screen.contains("[ Подписаться ]"), "{screen}"); + assert!( + screen.contains("e — прочитать ленту без подписки"), + "{screen}" + ); + assert!(screen.contains("enter подписаться"), "{screen}"); + assert!(!screen.contains("Уже подписан"), "{screen}"); + + state.popup = Some(Popup::PodcastCard { + hit: Box::new(hit), + preview: Some(Loadable::Ready(FeedPreview { + title: "Other Show (feed)".into(), + episode_count: 3, + seasons: vec![(Some(1), 2), (None, 1)], + episodes: vec![PreviewEpisode { + title: "Pilot".into(), + season: Some(1), + episode_number: Some(1), + published_at_ms: Some(1_718_445_600_000), + duration_ms: Some(2_700_000), + }], + moved_to: None, + subscribed_as: None, + })), + scroll: 0, + max_scroll: std::cell::Cell::new(u16::MAX), + }); + let screen = render(&state, 110, 40); + assert!( + screen.contains("Название в ленте: Other Show (feed)"), + "{screen}" + ); + assert!( + screen.contains("В ленте: 3 выпуска · сезон 1: 2, без сезона: 1"), + "{screen}" + ); + assert!( + screen.contains("• Pilot S1E1 · 2024-06-15 · 45:00"), + "{screen}" + ); + assert!(screen.contains("… и ещё 2"), "{screen}"); + + // The subscribed podcast: «Уже подписан», Enter opens it. + state.popup = Some(Popup::PodcastCard { + hit: Box::new(state.search.fed_podcasts[0].clone()), + preview: None, + scroll: 0, + max_scroll: std::cell::Cell::new(u16::MAX), + }); + let screen = render(&state, 110, 40); + assert!(screen.contains("✓ Уже подписан"), "{screen}"); + assert!(screen.contains("enter открыть"), "{screen}"); + assert!(!screen.contains("[ Подписаться ]"), "{screen}"); + } + + #[test] + fn podcast_share_confirmation_lists_the_warnings_in_russian() { + let mut state = + podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 1, 0)]); + state.popup = Some(crate::app::state::Popup::ConfirmPodcastShare { + podcast_id: podcast_fixtures::podcast_id(), + title: "Test Show".into(), + urls: crate::podcasts::publication::ConfirmedUrls { + feed_url: music_dht::podcast::FeedUrl::parse( + "https://example.test/feed.xml?token=abc", + ) + .unwrap(), + id_feed_url: None, + }, + warnings: vec!["в адресе ленты есть параметр «token», похожий на ключ доступа".into()], + already_shared: false, + }); + let screen = render(&state, 100, 30); + assert!( + screen.contains("Поделиться подкастом «Test Show»"), + "{screen}" + ); + assert!(screen.contains("Адрес похож на личный"), "{screen}"); + assert!(screen.contains("параметр «token»"), "{screen}"); + assert!(screen.contains("feed.xml?token=abc"), "{screen}"); + assert!( + screen.contains("enter/y опубликовать · esc/n отмена"), + "{screen}" + ); + assert!(!screen.contains("x не делиться"), "{screen}"); + } + #[test] fn podcast_seasons_and_episodes_render_progress() { use music_dht::podcast::ProgressStatus; diff --git a/src/ui/podcast_card.rs b/src/ui/podcast_card.rs new file mode 100644 index 0000000..f9a6606 --- /dev/null +++ b/src/ui/podcast_card.rs @@ -0,0 +1,284 @@ +//! The public podcast card opened from the search results. In Russian, like +//! the share confirmation: «Подписаться» / «Уже подписан» are the actions +//! the requirements name. +//! +//! Everything but the origin feed URL is a publisher's claim and is shown as +//! such; the episode preview comes from the feed itself. + +use ratatui::Frame; +use ratatui::layout::{Alignment, Constraint, Layout}; +use ratatui::style::{Color, Modifier, Style}; +use ratatui::text::{Line, Span}; +use ratatui::widgets::{Block, Clear, Paragraph, Wrap}; + +use super::podcasts::{format_date, format_duration_ms}; +use super::popup::centered; +use super::theme; +use crate::app::state::{AppState, Loadable}; +use crate::podcasts::discovery::{FeedPreview, PodcastHit}; + +pub(super) fn draw( + frame: &mut Frame, + state: &AppState, + hit: &PodcastHit, + preview: Option<&Loadable>, + scroll: u16, + max_scroll: &std::cell::Cell, +) { + let subscribed = + crate::app::update::podcast_discovery::subscribed_id(state, hit, preview).is_some(); + let subscribing = state.podcasts.subscribing; + let lines = card_lines(state, hit, preview, subscribed, subscribing); + + let width = 92.min(frame.area().width.saturating_sub(4)).max(30); + let inner_width = width.saturating_sub(2).max(1); + let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }); + // The height of the word-wrapped text, as it is rendered. + let body_height = u16::try_from(paragraph.line_count(inner_width)).unwrap_or(u16::MAX); + let height = body_height + .saturating_add(4) + .min(frame.area().height.saturating_sub(2)); + let area = centered(frame.area(), width, height); + let block = Block::bordered() + .title(" Подкаст · публичная карточка ") + .title_style(theme::header_for(state)) + .border_style(theme::strong_border_for(state)); + let inner = block.inner(area); + frame.render_widget(Clear, area); + frame.render_widget(block, area); + let [body, footer_area] = + Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner); + let last = body_height.saturating_sub(body.height); + max_scroll.set(last); + frame.render_widget(paragraph.scroll((scroll.min(last), 0)), body); + let footer = if subscribed { + "enter открыть · e выпуски · ↑↓ прокрутка · esc закрыть" + } else { + "enter подписаться · e выпуски · ↑↓ прокрутка · esc закрыть" + }; + frame.render_widget( + Paragraph::new(Line::styled(footer, theme::dim())).alignment(Alignment::Center), + footer_area, + ); +} + +fn card_lines( + state: &AppState, + hit: &PodcastHit, + preview: Option<&Loadable>, + subscribed: bool, + subscribing: bool, +) -> Vec> { + let card = &hit.hit.card; + let bold = Style::new().add_modifier(Modifier::BOLD); + let mut lines = vec![ + Line::from(vec![ + Span::styled("[podcast] ", theme::accent_for(state)), + Span::styled(card.title.clone(), bold), + ]), + Line::styled( + "Сведения ниже указали издатели карточки; они не проверены.", + theme::dim(), + ), + Line::raw(""), + ]; + if let Some(author) = &card.author { + lines.push(field("Автор", author.clone())); + } + if !card.categories.is_empty() { + lines.push(field("Категории", card.categories.join(", "))); + } + if let Some(language) = &card.language { + lines.push(field("Язык", language.clone())); + } + let mut episodes = match card.episode_count { + Some(count) => format!("{count}"), + None => "не указано".to_string(), + }; + if let Some(latest) = card.latest_episode_at_ms { + episodes.push_str(&format!( + " · последний {}", + format_date(latest.div_euclid(1000)) + )); + } + lines.push(field("Выпусков", episodes)); + let publishers = hit.hit.publishers.len(); + lines.push(field( + "Опубликовали", + format!( + "{publishers} {} (не число подписчиков и не знак доверия)", + plural_ru(publishers, "устройство", "устройства", "устройств") + ), + )); + lines.push(field("Лента", hit.hit.feed_url.as_str().to_string())); + for claim in &hit.hit.claimed_feed_urls { + lines.push(Line::styled( + format!( + "Заявленный новый адрес (не проверен, не используется): {}", + claim.feed_url.as_str() + ), + theme::dim(), + )); + } + if let Some(link) = &card.link { + lines.push(field("Сайт", link.clone())); + } + if let Some(description) = &card.description { + lines.push(Line::raw("")); + lines.extend( + description + .lines() + .map(|line| Line::raw(line.replace('\t', " "))), + ); + } + + lines.push(Line::raw("")); + if subscribed { + lines.push(Line::from(vec![ + Span::styled( + "✓ Уже подписан", + Style::new().fg(Color::Green).add_modifier(Modifier::BOLD), + ), + Span::styled( + " · enter откроет подкаст во вкладке Podcasts", + theme::dim(), + ), + ])); + } else if subscribing { + lines.push(Line::styled( + "Подписка… читаем ленту", + theme::accent_for(state), + )); + } else { + lines.push(Line::from(vec![ + Span::styled( + "[ Подписаться ]", + theme::accent_for(state).add_modifier(Modifier::BOLD), + ), + Span::styled( + " лента будет прочитана с её сервера; подписка личная и не публикуется", + theme::dim(), + ), + ])); + } + + lines.push(Line::raw("")); + lines.push(Line::styled( + "Сезоны и выпуски (из RSS-ленты)", + theme::header_for(state), + )); + match preview { + None => lines.push(Line::styled( + "e — прочитать ленту без подписки (запрос к серверу ленты)", + theme::dim(), + )), + Some(Loadable::Loading) => { + lines.push(Line::styled("читаем ленту…", theme::accent_for(state))) + } + Some(Loadable::Failed(err)) => lines.push(Line::styled( + format!("не удалось прочитать ленту: {err}"), + Style::new().fg(Color::Red), + )), + Some(Loadable::Ready(feed)) => preview_lines(&mut lines, card.title.as_str(), feed), + } + lines +} + +fn preview_lines(lines: &mut Vec>, card_title: &str, feed: &FeedPreview) { + if feed.title != card_title { + lines.push(field("Название в ленте", feed.title.clone())); + } + if let Some(moved) = &feed.moved_to { + lines.push(field("Лента переехала", moved.as_str().to_string())); + } + let seasons: Vec = feed + .seasons + .iter() + .map(|(season, count)| match season { + Some(season) => format!("сезон {season}: {count}"), + None => format!("без сезона: {count}"), + }) + .collect(); + lines.push(field( + "В ленте", + format!( + "{} {}{}", + feed.episode_count, + plural_ru(feed.episode_count, "выпуск", "выпуска", "выпусков"), + if seasons.is_empty() { + String::new() + } else { + format!(" · {}", seasons.join(", ")) + } + ), + )); + for episode in &feed.episodes { + let mut meta = Vec::new(); + match (episode.season, episode.episode_number) { + (Some(season), Some(number)) => meta.push(format!("S{season}E{number}")), + (Some(season), None) => meta.push(format!("S{season}")), + (None, Some(number)) => meta.push(format!("#{number}")), + (None, None) => {} + } + if let Some(published) = episode.published_at_ms { + meta.push(format_date(published.div_euclid(1000))); + } + if let Some(duration) = episode.duration_ms { + meta.push(format_duration_ms(duration)); + } + lines.push(Line::from(vec![ + Span::raw(format!("• {}", episode.title)), + Span::styled( + if meta.is_empty() { + String::new() + } else { + format!(" {}", meta.join(" · ")) + }, + theme::dim(), + ), + ])); + } + if feed.episodes.len() < feed.episode_count { + lines.push(Line::styled( + format!("… и ещё {}", feed.episode_count - feed.episodes.len()), + theme::dim(), + )); + } +} + +fn field(label: &str, value: String) -> Line<'static> { + Line::from(vec![ + Span::styled(format!("{label}: "), theme::dim()), + Span::raw(value), + ]) +} + +/// Russian plural form for `n`. +fn plural_ru(n: usize, one: &'static str, few: &'static str, many: &'static str) -> &'static str { + let (tens, units) = (n % 100, n % 10); + if (11..=14).contains(&tens) { + many + } else if units == 1 { + one + } else if (2..=4).contains(&units) { + few + } else { + many + } +} + +#[cfg(test)] +mod tests { + use super::plural_ru; + + #[test] + fn russian_plurals() { + let forms = |n| plural_ru(n, "устройство", "устройства", "устройств"); + assert_eq!(forms(1), "устройство"); + assert_eq!(forms(3), "устройства"); + assert_eq!(forms(5), "устройств"); + assert_eq!(forms(11), "устройств"); + assert_eq!(forms(21), "устройство"); + assert_eq!(forms(0), "устройств"); + } +} diff --git a/src/ui/podcasts.rs b/src/ui/podcasts.rs index c7e6cff..413e824 100644 --- a/src/ui/podcasts.rs +++ b/src/ui/podcasts.rs @@ -17,6 +17,7 @@ use crate::app::state::{ PodcastStoreStatus, SeasonRow, }; use crate::app::update::podcasts::{played_label, sort_label}; +use crate::podcasts::publication::{Discovery, Publication}; /// Width below which the side panel with the cover is dropped. const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80; @@ -219,7 +220,7 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static> Span::raw(" ") }; let summary = &podcast.summary; - let meta = if summary.unplayed_count > 0 { + let mut meta = if summary.unplayed_count > 0 { format!( "{} unplayed · {} ep", summary.unplayed_count, summary.episode_count @@ -227,6 +228,9 @@ fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static> } else { format!("{} ep", summary.episode_count) }; + if summary.shared { + meta = format!("shared · {meta}"); + } let title_width = usize::from(width).saturating_sub(2 + meta.width() + 2); let left = Line::from(vec![ marker, @@ -293,7 +297,7 @@ fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Opt counts.push_str(&format!(" · {} seasons", summary.season_count)); } lines.push(Line::styled(counts, theme::dim())); - lines.push(Line::styled("private", theme::dim())); + lines.push(share_status_line(state, podcast)); lines.push(fetch_status_line(state, podcast)); if let Some(description) = &summary.meta.description { lines.push(Line::raw("")); @@ -302,6 +306,62 @@ fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Opt frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area); } +/// Public discovery state of one podcast: private, or shared and whether +/// this device publishes its card. +pub(crate) fn share_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> { + let tab = &state.podcasts; + if tab.share_busy.contains(podcast.id()) { + return loading_line(state, "saving the share setting…"); + } + let report = &tab.publication; + // A card from this device that is still live although the podcast is + // private now (revocation pending, failed, or federation off). + let unrevoked = report.unrevoked.contains(podcast.id()); + if !podcast.summary.shared || (unrevoked && !report.podcasts.contains_key(podcast.id())) { + if !unrevoked { + return Line::styled("private (not shared in the federation)", theme::dim()); + } + if let Discovery::Off(_) = report.discovery { + return Line::styled( + "private · public card may not be revoked yet: federation is off", + Style::new().fg(Color::Yellow), + ); + } + return loading_line( + state, + "private · revoking the public card from this device…", + ); + } + let (text, style) = match (&report.discovery, report.podcasts.get(podcast.id())) { + (Discovery::Off(reason), _) => (format!("shared · not published: {reason}"), theme::dim()), + (_, Some(Publication::HeldBack(_))) => ( + "shared · held back on this device: the feed URL looks private, \ + press Shift+F to confirm" + .to_string(), + Style::new().fg(Color::Yellow), + ), + (_, Some(Publication::Failed(error))) => { + (format!("shared · not published: {error}"), error_style()) + } + (Discovery::Running { peers: 0 }, Some(Publication::Published)) => ( + "shared · published from this device (no discovery peers yet)".to_string(), + theme::accent_for(state), + ), + (_, Some(Publication::Published)) => ( + "shared · published from this device".to_string(), + theme::accent_for(state), + ), + (_, Some(Publication::WaitingForDevices)) => ( + "shared · not published yet: waiting for a sync with your other devices".to_string(), + theme::dim(), + ), + (Discovery::Starting, _) | (_, Some(Publication::Pending) | None) => { + return loading_line(state, "shared · publishing…"); + } + }; + Line::styled(text, style) +} + /// Refresh status of one podcast: checking, the last error, or when it was /// last checked. fn fetch_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> { diff --git a/src/ui/popup.rs b/src/ui/popup.rs index f4a779a..5478664 100644 --- a/src/ui/popup.rs +++ b/src/ui/popup.rs @@ -28,6 +28,19 @@ pub fn draw(frame: &mut Frame, state: &AppState) { Some(Popup::ConfirmPodcastUnsubscribe { title, .. }) => { draw_confirm_podcast_unsubscribe(frame, state, title) } + Some(Popup::PodcastCard { + hit, + preview, + scroll, + max_scroll, + }) => super::podcast_card::draw(frame, state, hit, preview.as_ref(), *scroll, max_scroll), + Some(Popup::ConfirmPodcastShare { + title, + urls, + warnings, + already_shared, + .. + }) => draw_confirm_podcast_share(frame, state, title, urls, warnings, *already_shared), Some(Popup::SimilarityPrivacyConsent { .. }) => { draw_similarity_privacy_consent(frame, state) } @@ -1318,6 +1331,85 @@ fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title: ); } +/// Sharing a podcast whose feed URL looks private. In Russian: the warning +/// is about the user's own secret, so it must not be missed. +fn draw_confirm_podcast_share( + frame: &mut Frame, + state: &AppState, + title: &str, + urls: &crate::podcasts::publication::ConfirmedUrls, + warnings: &[String], + already_shared: bool, +) { + let width = 78.min(frame.area().width.saturating_sub(4)).max(30); + let mut lines = vec![ + Line::styled( + format!("Поделиться подкастом «{title}» в федерации?"), + ratatui::style::Style::new().add_modifier(ratatui::style::Modifier::BOLD), + ), + Line::raw(""), + Line::raw( + "Карточка с адресом RSS-ленты станет видна всем участникам федерации, \ + и любой из них сможет читать ленту по этому адресу.", + ), + Line::raw(""), + Line::styled( + "Адрес похож на личный:", + ratatui::style::Style::new().fg(ratatui::style::Color::Yellow), + ), + ]; + lines.extend(warnings.iter().map(|warning| { + Line::styled( + format!("• {warning}"), + ratatui::style::Style::new().fg(ratatui::style::Color::Yellow), + ) + })); + lines.push(Line::raw("")); + lines.push(Line::styled( + format!("Лента: {}", urls.feed_url.as_str()), + theme::dim(), + )); + if let Some(origin) = &urls.id_feed_url { + lines.push(Line::styled( + format!("Исходный адрес: {}", origin.as_str()), + theme::dim(), + )); + } + lines.push(Line::raw("")); + lines.push(Line::styled( + "Публикацию можно отозвать, но копии, которые уже получили другие, \ + отозвать нельзя. Подтверждение хранится только на этом устройстве.", + theme::dim(), + )); + let footer = if already_shared { + "enter/y опубликовать · x не делиться · esc/n отмена" + } else { + "enter/y опубликовать · esc/n отмена" + }; + let inner_width = width.saturating_sub(2).max(1); + let body = Paragraph::new(lines).wrap(Wrap { trim: false }); + // The height of the word-wrapped text, as it is rendered. + let body_height = u16::try_from(body.line_count(inner_width)).unwrap_or(u16::MAX); + let height = body_height + .saturating_add(4) + .min(frame.area().height.saturating_sub(2)); + let area = centered(frame.area(), width, height); + let block = Block::bordered() + .title(" Поделиться в федерации ") + .title_style(theme::header_for(state)) + .border_style(theme::strong_border_for(state)); + let inner = block.inner(area); + frame.render_widget(Clear, area); + frame.render_widget(block, area); + let [body_area, footer_area] = + Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner); + frame.render_widget(body, body_area); + frame.render_widget( + Paragraph::new(Line::styled(footer, theme::dim())).alignment(Alignment::Center), + footer_area, + ); +} + fn draw_log_detail(frame: &mut Frame, state: &AppState, entry: &crate::config::logging::LogEntry) { let width = 90.min(frame.area().width.saturating_sub(4)).max(40); let height = 18.min(frame.area().height.saturating_sub(2)).max(7); @@ -1736,7 +1828,7 @@ fn draw_name_entry( frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer); } -fn centered(area: Rect, width: u16, height: u16) -> Rect { +pub(super) fn centered(area: Rect, width: u16, height: u16) -> Rect { let [rect] = Layout::horizontal([Constraint::Length(width.min(area.width))]) .flex(Flex::Center) .areas(area);