Reconcile own podcast publications with the share registers

podcasts::publication builds one PodcastShareState per subscription (cards
from the catalog for shared podcasts only, the device-local privacy
confirmation) and hands them to PodcastDht::sync_shares, which publishes
what is shared and tombstones the rest. Without a readable store every own
card is revoked. Privacy confirmations live in a 0600 JSON file next to
podcasts.sqlite3 and cover exactly the confirmed URLs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:13:07 +02:00
1 parent c453b0a52f
commit 8c90035fa2
4 files changed
+1054 -1

No files matched your search

+9 -1
View File
@@ -18,19 +18,24 @@
//! 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.
//!
//! Only RSS metadata is stored. Audio stays on the publisher's server.
pub mod fetch;
pub mod playback;
pub mod progress;
pub mod publication;
pub mod refresh;
pub mod stream;
#[cfg(test)]
mod playback_tests;
#[cfg(test)]
mod publication_tests;
#[cfg(test)]
mod stream_tests;
#[cfg(test)]
mod test_server;
@@ -111,10 +116,13 @@ pub fn open_services(db_path: &Path, covers_dir: PathBuf, log: Arc<dyn OpLog>) -
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),
})
}
+567
View File
@@ -0,0 +1,567 @@
//! 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_states`] 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: after local share and
//! unsubscribe actions, after received podcast operations, after feed
//! refreshes (card metadata), when the overlay (re)starts, and
//! periodically.
//! * [`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.
//!
//! 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::{Arc, Mutex, PoisonError};
use std::time::Duration;
use anyhow::{Context, Result};
use furumi_library::podcasts::{PodcastListFilter, PodcastStore, PodcastSummary};
use music_dht::podcast::{FeedUrl, PodcastId, SubscriptionRecord, effective_share};
use music_dht::podcast_dht::{
FeedUrlPrivacyWarning, PodcastCardSpec, PodcastDht, PodcastShareState,
};
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`].
const CONFIRMATIONS_VERSION: u32 = 1;
/// 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);
/// 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<FeedUrl>,
}
impl ConfirmedUrls {
pub fn of(card: &PodcastCardSpec) -> Self {
Self {
feed_url: card.feed_url.clone(),
id_feed_url: card.id_feed_url.clone(),
}
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
struct ConfirmationsFile {
version: u32,
confirmed: BTreeMap<PodcastId, ConfirmedUrls>,
}
/// Device-local record of "publish this feed URL although it looks
/// private" (`PodcastShareState::privacy_confirmed`). Not synced.
#[derive(Debug)]
pub struct ShareConfirmations {
/// `None`: in memory only (tests, or the podcast store is unavailable).
path: Option<PathBuf>,
confirmed: Mutex<BTreeMap<PodcastId, ConfirmedUrls>>,
}
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::<ConfirmationsFile>(&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()
}
};
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.
pub fn is_confirmed(&self, card: &PodcastCardSpec) -> bool {
self.lock()
.get(&card.podcast_id)
.is_some_and(|urls| *urls == ConfirmedUrls::of(card))
}
/// Records the confirmation of `urls` (replacing an older one).
/// Blocking (writes the file).
pub fn confirm(&self, podcast_id: &PodcastId, urls: ConfirmedUrls) -> Result<()> {
let mut confirmed = self.lock();
if confirmed.get(podcast_id) == Some(&urls) {
return Ok(());
}
let mut next = confirmed.clone();
next.insert(podcast_id.clone(), urls);
self.save(&next)?;
*confirmed = next;
Ok(())
}
/// 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<()> {
let mut confirmed = self.lock();
if !confirmed.contains_key(podcast_id) {
return Ok(());
}
let mut next = confirmed.clone();
next.remove(podcast_id);
self.save(&next)?;
*confirmed = next;
Ok(())
}
fn save(&self, confirmed: &BTreeMap<PodcastId, ConfirmedUrls>) -> 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<PodcastId, ConfirmedUrls>> {
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.
fn catalog_card(
summary: &PodcastSummary,
subscription: &SubscriptionRecord,
) -> Result<PodcastCardSpec> {
// 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 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,
}
.bounded();
card.validate()?;
Ok(card)
}
/// The card [`share_states`] 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<Option<PodcastCardSpec>> {
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<FeedUrlPrivacyWarning>,
/// 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<ShareCheck> {
let card = share_card(store, podcast_id)?
.with_context(|| "the podcast is not subscribed".to_string())?;
Ok(ShareCheck {
urls: ConfirmedUrls::of(&card),
warnings: card.privacy_warnings(),
confirmed: confirmations.is_confirmed(&card),
})
}
/// One `PodcastShareState` per podcast with a subscription register, the
/// input of `PodcastDht::sync_shares`. 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.
pub fn share_states(
store: &PodcastStore,
confirmations: &ShareConfirmations,
) -> Result<Vec<PodcastShareState>> {
let mut states = Vec::new();
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)?;
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));
states.push(PodcastShareState {
subscription,
share,
card,
privacy_confirmed,
});
}
Ok(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, but held back on this device until its user confirms the
/// feed URLs that look private.
HeldBack(Vec<FeedUrlPrivacyWarning>),
/// 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<PodcastId, Publication>,
/// The pass as a whole failed (store or overlay error).
pub error: Option<String>,
}
/// 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<Arc<PodcastStore>>,
confirmations: Arc<ShareConfirmations>,
) -> PublicationReport {
let mut report = PublicationReport {
discovery: Discovery::Running {
peers: dht.known_peers(),
},
..PublicationReport::default()
};
// Why every own card is revoked, when the registers are unknown.
let mut revoke_all: Option<String> = None;
let states = match store {
Some(store) => {
match tokio::task::spawn_blocking(move || share_states(&store, &confirmations)).await {
Ok(Ok(states)) => states,
Ok(Err(err)) => {
revoke_all = Some(format!("cannot read share decisions ({err:#})"));
Vec::new()
}
Err(err) => {
revoke_all = Some(format!("cannot read share decisions ({err})"));
Vec::new()
}
}
}
None => {
revoke_all = Some("the podcast database is unavailable".into());
Vec::new()
}
};
let shared: Vec<PodcastId> = states
.iter()
.filter(|state| state.is_shared())
.map(|state| state.subscription.podcast_id.clone())
.collect();
let stats = match dht.sync_shares(states).await {
Ok(stats) => 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"
);
}
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<PodcastId> = 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()
}
};
let held: BTreeMap<PodcastId, Vec<FeedUrlPrivacyWarning>> = stats
.held_back
.into_iter()
.map(|hold| (hold.podcast_id, hold.warnings))
.collect();
let failed: BTreeMap<PodcastId, String> = 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);
}
report
}
/// Wakes the running reconciler.
#[derive(Debug, Clone)]
pub struct Reconciler {
wake: Arc<Notify>,
}
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();
}
}
/// 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.
pub fn spawn(
mut dht: watch::Receiver<Option<Arc<PodcastDht>>>,
store: Option<Arc<PodcastStore>>,
confirmations: Arc<ShareConfirmations>,
report: impl Fn(PublicationReport) + Send + 'static,
) -> Reconciler {
let wake = Arc::new(Notify::new());
let reconciler = Reconciler {
wake: Arc::clone(&wake),
};
tokio::spawn(async move {
loop {
let current = dht.borrow_and_update().clone();
match current {
Some(dht) => {
let pass = reconcile(&dht, store.clone(), Arc::clone(&confirmations)).await;
report(pass);
}
None => report(PublicationReport {
discovery: Discovery::Off(
"federation is off or podcast discovery is unavailable".into(),
),
..PublicationReport::default()
}),
}
tokio::select! {
_ = wake.notified() => tokio::time::sleep(RECONCILE_DEBOUNCE).await,
changed = dht.changed() => {
if changed.is_err() {
return;
}
}
_ = tokio::time::sleep(RECONCILE_INTERVAL) => {}
}
}
});
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(),
}
}
+476
View File
@@ -0,0 +1,476 @@
//! 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};
use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId};
use super::publication::{
Discovery, Publication, PublicationReport, ShareConfirmations, check_share, reconcile,
share_card, share_states, 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";
/// Stamps of one device's local actions; keeps the operations they emit.
struct Device {
id: &'static str,
seq: i64,
ops: Vec<SyncOpWire>,
}
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<OpStamp> {
self.next += 1;
Ok(OpStamp::new(
format!("{}:{}", self.device, self.next),
self.device,
1_700_000_000_000 + self.next,
))
}
}
impl Device {
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<PodcastLocalOp>>,
) -> Vec<SyncOpWire> {
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,
};
let mut id = None;
self.act(|stamps| {
let outcome = store.subscribe(stamps, &meta)?;
id = Some(outcome.podcast_id);
Ok(outcome.ops)
});
id.unwrap()
}
fn set_shared(
&mut self,
store: &PodcastStore,
id: &PodcastId,
shared: bool,
) -> Vec<SyncOpWire> {
self.act(|stamps| store.set_shared(stamps, id, shared))
}
fn unsubscribe(&mut self, store: &PodcastStore, id: &PodcastId) {
self.act(|stamps| store.unsubscribe(stamps, id));
}
}
fn shared_ids(store: &PodcastStore, confirmations: &ShareConfirmations) -> Vec<PodcastId> {
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», похожий на ключ доступа"
);
confirmations.confirm(&secret, check.urls.clone()).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()
};
assert!(reopened.is_confirmed(&card));
assert!(!reopened.is_confirmed(&moved));
// Shared but held: the confirmation is used by the share state.
device.set_shared(&store, &secret, true);
let state = share_states(&store, &reopened)
.unwrap()
.into_iter()
.find(|state| state.subscription.podcast_id == secret)
.unwrap();
assert!(state.privacy_confirmed);
reopened.forget(&secret).unwrap();
assert!(!ShareConfirmations::open(path.clone()).is_confirmed(&card));
// A corrupt file asks again instead of failing.
std::fs::write(&path, b"{not json").unwrap();
assert!(!ShareConfirmations::open(path).is_confirmed(&card));
}
// ---------------------------------------------------------------------------
// Two loopback nodes
struct Node {
service: Arc<MusicDhtService>,
dht: Arc<PodcastDht>,
tasks: Vec<tokio::task::JoinHandle<()>>,
}
impl Node {
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],
}
}
async fn stop(self) {
for task in &self.tasks {
task.abort();
}
self.service.shutdown().await.unwrap();
}
}
async fn wait_for<T>(what: &str, mut check: impl AsyncFnMut() -> Option<T>) -> 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<Publication> {
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.
let check = check_share(&store, &confirmations, &secret).unwrap();
confirmations.confirm(&secret, check.urls).unwrap();
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;
// 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);
confirmations
.confirm(
&secret,
check_share(&store, &confirmations, &secret).unwrap().urls,
)
.unwrap();
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;
}
+2
View File
@@ -30,6 +30,8 @@ pub struct Services {
pub store: Arc<PodcastStore>,
pub fetcher: Arc<Fetcher>,
pub log: Arc<dyn OpLog>,
/// Device-local privacy confirmations for sharing (never synced).
pub confirmations: Arc<super::publication::ShareConfirmations>,
}
impl std::fmt::Debug for Services {