diff --git a/AGENTS.md b/AGENTS.md index f5f7357..1469119 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -72,7 +72,10 @@ update it only when dependencies change. 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). Public cards found by search are publisher claims: + `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` diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index f1a11f9..096845f 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -415,7 +415,13 @@ The TUI owns what the contract leaves to clients: 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. Publication runs from each device's own identity. Privacy + 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index f6670cf..61acca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 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" diff --git a/README.md b/README.md index a926937..827f3d8 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,14 @@ Sharing a podcast (public discovery): 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. + "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. diff --git a/src/app/podcast_runtime.rs b/src/app/podcast_runtime.rs index 4ccb9f4..3df4139 100644 --- a/src/app/podcast_runtime.rs +++ b/src/app/podcast_runtime.rs @@ -132,7 +132,9 @@ fn spawn_scheduler(tx: tokio::sync::mpsc::UnboundedSender) { } /// Starts the publication reconciler. Without a store every own public -/// card is revoked (see `publication::reconcile`). +/// 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>, @@ -141,6 +143,7 @@ fn spawn_publication( let tx = runtime.event_tx.clone(); publication::spawn( runtime.federation.podcast_dht(), + runtime.devices.podcast_registers_current(), store, confirmations, move |report| { 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/podcasts/publication.rs b/src/podcasts/publication.rs index 1beafdc..3811b19 100644 --- a/src/podcasts/publication.rs +++ b/src/podcasts/publication.rs @@ -22,7 +22,10 @@ //! 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. +//! (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 @@ -51,7 +54,7 @@ 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, feed_url_privacy_warning, + PodcastShareState, PodcastSyncFailure, feed_url_privacy_warning, }; use serde::{Deserialize, Serialize}; use tokio::sync::{Notify, watch}; @@ -641,6 +644,10 @@ pub enum Publication { /// 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), @@ -705,33 +712,8 @@ pub async fn reconcile( }, ..PublicationReport::default() }; - // Why every own card is revoked, when the registers are unknown. - let mut revoke_all: Option = None; - let SharePlan { states, held } = match store { - Some(store) => { - match tokio::task::spawn_blocking(move || share_plan(&store, &confirmations)).await { - Ok(Ok(plan)) => plan, - Ok(Err(err)) => { - revoke_all = Some(format!("cannot read share decisions ({err:#})")); - SharePlan::default() - } - Err(err) => { - revoke_all = Some(format!("cannot read share decisions ({err})")); - SharePlan::default() - } - } - } - None => { - revoke_all = Some("the podcast database is unavailable".into()); - SharePlan::default() - } - }; - let shared: Vec = states - .iter() - .filter(|state| state.is_shared()) - .map(|state| state.subscription.podcast_id.clone()) - .chain(held.keys().cloned()) - .collect(); + 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 @@ -767,6 +749,145 @@ pub async fn reconcile( "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"); @@ -794,8 +915,7 @@ pub async fn reconcile( BTreeSet::new() } }; - summarize(&mut report, shared, stats, &active); - report + summarize(report, shared, stats, &active); } /// Folds one `sync_shares` result and the set of this device's active @@ -1005,8 +1125,15 @@ async fn still_private( /// 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, @@ -1026,6 +1153,8 @@ pub fn spawn( }; 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(); @@ -1036,10 +1165,14 @@ pub fn spawn( 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) => reconcile(&dht, store.clone(), Arc::clone(&confirmations)).await, + 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); @@ -1075,6 +1208,13 @@ pub fn spawn( } 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 diff --git a/src/podcasts/publication_tests.rs b/src/podcasts/publication_tests.rs index 8bcc502..ad0bf06 100644 --- a/src/podcasts/publication_tests.rs +++ b/src/podcasts/publication_tests.rs @@ -22,6 +22,7 @@ use super::publication::{ 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 { @@ -929,9 +930,11 @@ async fn reconciler_lifecycle() { 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| { @@ -1062,3 +1065,149 @@ async fn reconciler_lifecycle() { 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/ui/mod.rs b/src/ui/mod.rs index 9df781d..815133c 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -213,6 +213,16 @@ mod tests { 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}"); diff --git a/src/ui/podcasts.rs b/src/ui/podcasts.rs index b51b852..413e824 100644 --- a/src/ui/podcasts.rs +++ b/src/ui/podcasts.rs @@ -351,6 +351,10 @@ pub(crate) fn share_status_line(state: &AppState, podcast: &PodcastRow) -> Line< "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…"); }