diff --git a/src/federation/mod.rs b/src/federation/mod.rs index 9459e0d..912ce52 100644 --- a/src/federation/mod.rs +++ b/src/federation/mod.rs @@ -302,6 +302,8 @@ pub struct Federation { /// The running podcast-discovery overlay, readable without waiting for /// `running` (held while the node starts). podcast_dht: std::sync::Mutex>>, + /// The node runs, but the podcast-discovery overlay failed to start. + podcast_discovery_failed: AtomicBool, sync_lock: tokio::sync::Mutex<()>, supervisor_started: AtomicBool, recovery_count: AtomicU64, @@ -338,6 +340,7 @@ pub fn handle() -> Arc { pool: tokio::sync::OnceCell::new(), running: tokio::sync::Mutex::new(None), podcast_dht: std::sync::Mutex::new(None), + podcast_discovery_failed: AtomicBool::new(false), sync_lock: tokio::sync::Mutex::new(()), supervisor_started: AtomicBool::new(false), recovery_count: AtomicU64::new(0), @@ -488,6 +491,8 @@ impl Federation { return Ok(false); } *lock(&self.podcast_dht) = None; + self.podcast_discovery_failed + .store(false, Ordering::Relaxed); stop_running(guard.take()).await; } else if recovery_only { tracing::warn!(network = %network_name, "retrying stopped federation service"); @@ -642,6 +647,8 @@ impl Federation { tasks.extend(discovery.tasks); } *lock(&self.podcast_dht) = podcast_dht.clone(); + self.podcast_discovery_failed + .store(podcast_dht.is_none(), Ordering::Relaxed); *guard = Some(Running { service, similarity_dht, @@ -692,6 +699,8 @@ impl Federation { async fn stop(&self) { let mut guard = self.running.lock().await; *lock(&self.podcast_dht) = None; + self.podcast_discovery_failed + .store(false, Ordering::Relaxed); stop_running(guard.take()).await; } @@ -724,6 +733,12 @@ impl Federation { lock(&self.podcast_dht).clone() } + /// Whether the node runs but its podcast-discovery overlay could not + /// start (`podcast_discovery()` is then `None` too). + pub fn podcast_discovery_unavailable(&self) -> bool { + self.podcast_discovery_failed.load(Ordering::Relaxed) + } + async fn spawn_sync_soon(self: &Arc) { if let Ok(service) = self.service().await { let fed = Arc::clone(self); diff --git a/src/i18n/phrases.rs b/src/i18n/phrases.rs index 096689f..4fcc952 100644 --- a/src/i18n/phrases.rs +++ b/src/i18n/phrases.rs @@ -412,12 +412,15 @@ translations! { player_podcast_share: "Share in the federation", "Поделиться в федерации"; player_podcast_unshare: "Stop sharing", "Перестать делиться"; player_podcast_sharing: "Saving…", "Сохраняем…"; - player_podcast_shared_badge: "Shared in the federation", "Опубликован в федерации"; + player_podcast_shared_badge: "Sharing on", "Публикация включена"; player_podcast_share_private: "Private: only your own devices see this subscription. Sharing publishes the podcast card (title, description, artwork, feed URL) from this server so that other federation members can find it and subscribe. Your progress, history and account are never published.", "Приватно: подписку видят только ваши устройства. «Поделиться» публикует карточку подкаста (название, описание, обложку, адрес RSS-ленты) от имени этого сервера, чтобы другие участники федерации могли найти его и подписаться. Ваш прогресс, история и аккаунт не публикуются."; - player_podcast_share_published: "Published: other federation members can find this podcast and subscribe. Your progress, history and account stay private.", "Опубликовано: участники федерации могут найти этот подкаст и подписаться. Ваш прогресс, история и аккаунт остаются приватными."; + player_podcast_share_published: "Published by this server: other federation members can find this podcast and subscribe. Your progress, history and account stay private.", "Опубликовано этим сервером: участники федерации могут найти этот подкаст и подписаться. Ваш прогресс, история и аккаунт остаются приватными."; player_podcast_share_publishing: "Shared: the server is publishing the card…", "Включено: сервер публикует карточку…"; - player_podcast_share_discovery_off: "Shared: the card will be published when federation is enabled on this server.", "Включено: карточка будет опубликована, когда на этом сервере включат федерацию."; - player_podcast_share_pending: "Sharing is on, but the feed URL may contain private data. It is not published until you confirm.", "Публикация включена, но адрес ленты может содержать личные данные. Пока вы не подтвердите, он не публикуется."; + player_podcast_share_discovery_off: "Shared: this server publishes the card once federation is enabled on it.", "Включено: этот сервер опубликует карточку, когда на нём включат федерацию."; + player_podcast_share_discovery_unavailable: "Shared, but this server cannot publish the card: public podcast discovery failed to start (see the federation status).", "Включено, но этот сервер не может опубликовать карточку: публичный поиск подкастов не запустился (см. статус федерации)."; + player_podcast_share_over_limit: "Shared, but not published: this server already publishes as many podcasts as it can, and your part of them is used. The card is published when a place frees up.", "Включено, но не опубликовано: сервер уже публикует столько подкастов, сколько может, и ваша доля занята. Карточка появится, когда освободится место."; + player_podcast_share_failed: "Shared, but this server could not publish the card. It tries again regularly.", "Включено, но этот сервер не смог опубликовать карточку. Он регулярно пробует снова."; + player_podcast_share_pending: "Sharing is on, but the feed URL may contain private data. This server does not publish it until you confirm here; your other players where you already confirmed may publish it themselves.", "Публикация включена, но адрес ленты может содержать личные данные. Этот сервер не опубликует его, пока вы не подтвердите здесь; ваши другие плееры, где вы уже подтвердили, могут публиковать его сами."; player_podcast_share_review: "Review and publish", "Проверить и опубликовать"; player_podcast_share_unavailable: "This podcast cannot be published", "Этот подкаст нельзя опубликовать"; player_podcast_share_dialog_title: "Publish this feed URL?", "Опубликовать адрес ленты?"; diff --git a/src/podcasts/api.rs b/src/podcasts/api.rs index 07944e0..b1d622f 100644 --- a/src/podcasts/api.rs +++ b/src/podcasts/api.rs @@ -24,10 +24,11 @@ use music_dht::podcast::{ EpisodeId, EpisodeType, FeedUrl, ListenSession, PodcastEpisodeRef, PodcastId, PodcastMeta, ProgressRecord, ProgressStatus, reaches_completion, }; -use music_dht::podcast_dht::{FeedUrlPrivacyWarning, PodcastDht}; +use music_dht::podcast_dht::FeedUrlPrivacyWarning; use serde::{Deserialize, Serialize}; use serde_json::Value; +use super::discovery::Discovery; use super::fetch::{FeedFetch, FeedFetcher, FetchErrorKind}; use super::runner::{self, SubscribeUrlError}; use super::search; @@ -387,12 +388,12 @@ pub(crate) struct PodcastDetail { } /// One podcast with its season groups and its public discovery state -/// (`discovery`: the server's overlay, when it runs). +/// (`discovery`: whether the server's overlay runs). pub(crate) async fn podcast_detail( store: &PodcastStore, user_id: i64, podcast_id: &PodcastId, - discovery: Option<&PodcastDht>, + discovery: Discovery<'_>, ) -> ApiResult { let podcast = podcast_view(store, user_id, podcast_id).await?; let share = share_view(store, user_id, podcast_id, discovery).await?; @@ -513,12 +514,20 @@ pub(crate) struct ShareView { pub(crate) feed_url: Option, /// Origin URL the podcast id comes from, when the feed moved. pub(crate) id_feed_url: Option, - /// `running` when the server's node runs public discovery, `off` - /// otherwise (federation disabled or the overlay failed to start). + /// `running` when the server's node runs public discovery, + /// `unavailable` when the node runs but the overlay failed to start, + /// `off` when federation is not running. pub(crate) discovery: &'static str, - /// The server currently publishes a card of this podcast and this - /// account shares it. + /// This server's node currently publishes a card of this podcast and + /// this account shares it. Only this server's own publication: another + /// player of the account (a TUI) publishes its own card from its own + /// node, which this flag does not show. pub(crate) published: bool, + /// Why the server does not publish a shared, publishable podcast after + /// its last round: `over_limit` (beyond this account's part of the + /// server's publication budget) or `failed` (the card could not be + /// signed or stored). `null` otherwise. + pub(crate) held: Option<&'static str>, } /// The account's [`ShareView`] of a podcast. @@ -526,7 +535,7 @@ pub(crate) async fn share_view( store: &PodcastStore, user_id: i64, podcast_id: &PodcastId, - discovery: Option<&PodcastDht>, + discovery: Discovery<'_>, ) -> ApiResult { let account = store.account_share(user_id, podcast_id).await?; let mut view = ShareView { @@ -537,12 +546,9 @@ pub(crate) async fn share_view( warnings: Vec::new(), feed_url: None, id_feed_url: None, - discovery: if discovery.is_some() { - "running" - } else { - "off" - }, + discovery: discovery.state(), published: false, + held: None, }; let Some(account) = account else { return Ok(view); @@ -558,15 +564,23 @@ pub(crate) async fn share_view( } Err(error) => view.card_error = Some(error.clone()), } - if let Some(dht) = discovery + if let Some(dht) = discovery.dht() && account.publishable() { - view.published = dht - .published() - .await - .map_err(|error| anyhow::anyhow!("podcast publications unavailable: {error}"))? - .iter() - .any(|publication| publication.active && publication.podcast_id == *podcast_id); + // The overlay's cache is not essential to the podcast page: when it + // cannot be read, the card shows as not published yet. + view.published = match dht.published().await { + Ok(publications) => publications + .iter() + .any(|publication| publication.active && publication.podcast_id == *podcast_id), + Err(error) => { + tracing::warn!("podcast publications unavailable: {error}"); + false + } + }; + if !view.published { + view.held = super::discovery::held_reason(podcast_id); + } } Ok(view) } @@ -619,7 +633,7 @@ pub(crate) async fn set_share( user_id: i64, podcast_id: &PodcastId, body: Value, - discovery: Option<&PodcastDht>, + discovery: Discovery<'_>, now_ms: i64, ) -> ApiResult { let request: ShareRequest = decode(body)?; diff --git a/src/podcasts/discovery.rs b/src/podcasts/discovery.rs index 265056c..4889eee 100644 --- a/src/podcasts/discovery.rs +++ b/src/podcasts/discovery.rs @@ -100,6 +100,38 @@ pub(crate) fn request_reconcile() { notifier().notify_one(); } +/// Whether the server's federation node runs public discovery, as shown +/// to the accounts. +#[derive(Clone, Copy)] +pub(crate) enum Discovery<'a> { + /// Federation is not running on this server. + Off, + /// The federation node runs, but the overlay could not start (its + /// cache could not be opened): see the federation status. + Unavailable, + /// The overlay runs. + Running(&'a PodcastDht), +} + +impl<'a> Discovery<'a> { + /// The overlay, when it runs. + pub(crate) fn dht(self) -> Option<&'a PodcastDht> { + match self { + Self::Running(dht) => Some(dht), + Self::Off | Self::Unavailable => None, + } + } + + /// `running`, `unavailable` or `off`. + pub(crate) fn state(self) -> &'static str { + match self { + Self::Running(_) => "running", + Self::Unavailable => "unavailable", + Self::Off => "off", + } + } +} + /// A confirmation that an account may publish a card despite its privacy /// warnings. #[derive(Debug, Clone, PartialEq, Eq)] @@ -585,6 +617,21 @@ fn memory() -> std::sync::MutexGuard<'static, RoundMemory> { .unwrap_or_else(std::sync::PoisonError::into_inner) } +/// Why the last round did not publish a podcast that is shared and +/// publishable: `over_limit` (beyond the account's part of +/// [`MAX_SERVER_SHARED_PODCASTS`]) or `failed` (the overlay could not sign +/// or store its card). `None` otherwise or before the first round. +pub(crate) fn held_reason(podcast_id: &PodcastId) -> Option<&'static str> { + let memory = memory(); + if memory.over_limit.contains(podcast_id) { + Some("over_limit") + } else if memory.failed.contains(podcast_id) { + Some("failed") + } else { + None + } +} + /// One round: load the shared podcasts of every account (or of `users`), /// aggregate them against what the overlay publishes now and let it /// publish and revoke. @@ -997,6 +1044,23 @@ mod tests { assert_eq!(aggregation.representatives[&id("a.xml")], 3); } + #[test] + fn the_last_round_says_why_a_shared_podcast_is_not_published() { + let over = id("held-over-limit.xml"); + let failed = id("held-failed.xml"); + { + let mut memory = memory(); + memory.over_limit.insert(over.clone()); + memory.failed.insert(failed.clone()); + } + assert_eq!(held_reason(&over), Some("over_limit")); + assert_eq!(held_reason(&failed), Some("failed")); + assert_eq!(held_reason(&id("held-none.xml")), None); + let mut memory = memory(); + memory.over_limit.remove(&over); + memory.failed.remove(&failed); + } + #[test] fn a_card_never_carries_personal_state() { let share = account(1, "a.xml", true, 10); diff --git a/src/podcasts/discovery_tests.rs b/src/podcasts/discovery_tests.rs index 33eb0fb..09ff812 100644 --- a/src/podcasts/discovery_tests.rs +++ b/src/podcasts/discovery_tests.rs @@ -17,7 +17,7 @@ use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId}; use serde_json::json; use super::api::{self, ShareOutcome}; -use super::discovery::{self, aggregate_share_states, load_shared}; +use super::discovery::{self, Discovery, aggregate_share_states, load_shared}; use super::postgres_tests::{cleanup, random_user, test_pool}; use super::*; @@ -70,7 +70,7 @@ async fn share( podcast_id: &PodcastId, body: serde_json::Value, ) -> Result { - api::set_share(store, user, podcast_id, body, None, now_ms()).await + api::set_share(store, user, podcast_id, body, Discovery::Off, now_ms()).await } fn done(outcome: Result) -> Box { @@ -149,7 +149,7 @@ async fn postgres_share_toggle_asks_before_publishing_a_private_looking_feed() { let podcast_id = subscribe(&store, user, &secret, "Members Only").await; // Private by default; the dialog needs the warnings and the URL. - let view = api::share_view(&store, user, &podcast_id, None) + let view = api::share_view(&store, user, &podcast_id, Discovery::Off) .await .unwrap(); assert!(!view.shared && !view.pending_confirmation && view.can_share); @@ -212,7 +212,7 @@ async fn postgres_share_toggle_asks_before_publishing_a_private_looking_feed() { store.apply_remote_op(user, &remote).await.unwrap(), ApplyOutcome::Changed ); - let view = api::share_view(&store, user, &podcast_id, None) + let view = api::share_view(&store, user, &podcast_id, Discovery::Off) .await .unwrap(); assert!(view.shared && view.pending_confirmation); @@ -238,7 +238,7 @@ async fn postgres_share_toggle_asks_before_publishing_a_private_looking_feed() { .execute(&pool) .await .unwrap(); - let view = api::share_view(&store, user, &podcast_id, None) + let view = api::share_view(&store, user, &podcast_id, Discovery::Off) .await .unwrap(); assert!(view.shared && view.pending_confirmation); @@ -248,7 +248,7 @@ async fn postgres_share_toggle_asks_before_publishing_a_private_looking_feed() { // Unsubscribing revokes and forgets; the podcast is private again. api::unsubscribe(&store, user, &podcast_id).await.unwrap(); - let view = api::share_view(&store, user, &podcast_id, None) + let view = api::share_view(&store, user, &podcast_id, Discovery::Off) .await .unwrap(); assert!(!view.shared && !view.can_share); @@ -324,7 +324,9 @@ async fn postgres_shared_podcasts_aggregate_across_accounts() { store.apply_remote_op(a, &stale).await.unwrap(), ApplyOutcome::Unchanged ); - let view = api::share_view(&store, a, &podcast_id, None).await.unwrap(); + let view = api::share_view(&store, a, &podcast_id, Discovery::Off) + .await + .unwrap(); assert!(!view.shared); // The last sharing account unsubscribes: the card is revoked, and @@ -335,7 +337,9 @@ async fn postgres_shared_podcasts_aggregate_across_accounts() { assert!(published_ids(&pool, &users).await.is_empty()); // Share decisions of C never existed; the private podcast of A stays // private throughout. - let view = api::share_view(&store, a, &private_id, None).await.unwrap(); + let view = api::share_view(&store, a, &private_id, Discovery::Off) + .await + .unwrap(); assert!(!view.shared); remove_accounts(&pool, &users).await; @@ -483,10 +487,10 @@ async fn loopback_scenario() { assert_eq!(report.aggregation.states.len(), 1, "one card per podcast"); assert_eq!(report.stats.published, 1); assert!(report.stats.held_back.is_empty() && report.stats.failed.is_empty()); - let view = api::share_view(&store, u1, &podcast_id, Some(&web.dht)) + let view = api::share_view(&store, u1, &podcast_id, Discovery::Running(&web.dht)) .await .unwrap(); - assert!(view.published); + assert!(view.published && view.held.is_none()); assert_eq!(view.discovery, "running"); let hit = wait_for("the shared card", async || { @@ -564,7 +568,7 @@ async fn loopback_scenario() { .iter() .all(|hit| hit.podcast_id != podcast_id) ); - let view = api::share_view(&store, u2, &podcast_id, Some(&web.dht)) + let view = api::share_view(&store, u2, &podcast_id, Discovery::Running(&web.dht)) .await .unwrap(); assert!(!view.shared && !view.published); diff --git a/src/podcasts/http.rs b/src/podcasts/http.rs index 64904ce..669d44a 100644 --- a/src/podcasts/http.rs +++ b/src/podcasts/http.rs @@ -60,6 +60,7 @@ use music_dht::podcast::{EpisodeId, PodcastId}; use serde::{Deserialize, Serialize}; use super::api::{self, ApiError, EpisodeAction, EpisodeListParams}; +use super::discovery::Discovery; use super::fetch::FeedFetcher; use super::limits::{self, ConcurrencyLimit, Permit}; use super::proxy::AudioProxy; @@ -556,6 +557,18 @@ async fn found_handler( } } +/// Whether public discovery runs on this server's federation node. +fn discovery_state<'a>( + federation: &crate::federation::Federation, + dht: Option<&'a music_dht::podcast_dht::PodcastDht>, +) -> Discovery<'a> { + match dht { + Some(dht) => Discovery::Running(dht), + None if federation.podcast_discovery_unavailable() => Discovery::Unavailable, + None => Discovery::Off, + } +} + /// What a `/feeds/{podcast_id}...` route does. #[derive(Debug, Clone, Copy)] enum PodcastRoute { @@ -582,10 +595,10 @@ async fn podcast_handler( let store = state.store().await?; match route { PodcastRoute::Detail => { - let discovery = crate::federation::handle().podcast_discovery(); - api_response( - api::podcast_detail(&store, user_id, &podcast_id, discovery.as_deref()).await, - ) + let federation = crate::federation::handle(); + let dht = federation.podcast_discovery(); + let discovery = discovery_state(&federation, dht.as_deref()); + api_response(api::podcast_detail(&store, user_id, &podcast_id, discovery).await) } PodcastRoute::Subscribe => { api_response(api::resubscribe(&store, user_id, &podcast_id).await) @@ -624,13 +637,14 @@ async fn share_handler( Err(error) => return Ok(api_error(error)), }; let store = state.store().await?; - let discovery = crate::federation::handle().podcast_discovery(); + let federation = crate::federation::handle(); + let dht = federation.podcast_discovery(); match api::set_share( &store, user_id, &podcast_id, body, - discovery.as_deref(), + discovery_state(&federation, dht.as_deref()), now_ms(), ) .await diff --git a/src/podcasts/postgres_tests.rs b/src/podcasts/postgres_tests.rs index aa91a4d..eccddcd 100644 --- a/src/podcasts/postgres_tests.rs +++ b/src/podcasts/postgres_tests.rs @@ -16,6 +16,7 @@ use music_dht::podcast::{ ProgressStatus, ShareRecord, SubscriptionRecord, }; +use super::discovery::Discovery; use super::*; use crate::player::queue_items::test_episode; @@ -2061,13 +2062,13 @@ async fn postgres_podcast_api_subscribes_plays_and_keeps_progress() { .is_empty() ); assert_eq!( - api::podcast_detail(&store, other, &podcast_id, None) + api::podcast_detail(&store, other, &podcast_id, Discovery::Off) .await .unwrap_err() .status, 404 ); - let detail = api::podcast_detail(&store, user, &podcast_id, None) + let detail = api::podcast_detail(&store, user, &podcast_id, Discovery::Off) .await .unwrap(); let seasons: Vec> = detail.seasons.iter().map(|season| season.season).collect(); @@ -2252,7 +2253,7 @@ async fn postgres_podcast_api_subscribes_plays_and_keeps_progress() { .await .unwrap(); assert_eq!((replay.position_ms, replay.status), (0, "completed")); - let history = api::podcast_detail(&store, user, &podcast_id, None) + let history = api::podcast_detail(&store, user, &podcast_id, Discovery::Off) .await .unwrap() .history; diff --git a/templates/player/scripts.html b/templates/player/scripts.html index 50d00a0..79f7c16 100644 --- a/templates/player/scripts.html +++ b/templates/player/scripts.html @@ -79,6 +79,9 @@ const T = { podcastSharePublished: "{{ t.player_podcast_share_published }}", podcastSharePublishing: "{{ t.player_podcast_share_publishing }}", podcastShareDiscoveryOff: "{{ t.player_podcast_share_discovery_off }}", + podcastShareDiscoveryUnavailable: "{{ t.player_podcast_share_discovery_unavailable }}", + podcastShareOverLimit: "{{ t.player_podcast_share_over_limit }}", + podcastShareFailed: "{{ t.player_podcast_share_failed }}", podcastSharePending: "{{ t.player_podcast_share_pending }}", podcastShareUnavailable: "{{ t.player_podcast_share_unavailable }}", podcastWarningSecretQuery: "{{ t.player_podcast_warning_secret_query }}", @@ -3972,8 +3975,13 @@ document.addEventListener('alpine:init', () => { if (!share) return ''; if (!share.shared) return share.card_error ? T.podcastShareUnavailable : T.podcastSharePrivate; if (share.pending_confirmation) return T.podcastSharePending; + if (share.discovery === 'unavailable') return T.podcastShareDiscoveryUnavailable; if (share.discovery !== 'running') return T.podcastShareDiscoveryOff; - return share.published ? T.podcastSharePublished : T.podcastSharePublishing; + if (share.published) return T.podcastSharePublished; + // Not published by this server after its last round, and why. + if (share.held === 'over_limit') return T.podcastShareOverLimit; + if (share.held === 'failed') return T.podcastShareFailed; + return T.podcastSharePublishing; }, warningText(warning) { diff --git a/tests/player_podcasts.test.cjs b/tests/player_podcasts.test.cjs index 0bfb1c2..440d2e2 100644 --- a/tests/player_podcasts.test.cjs +++ b/tests/player_podcasts.test.cjs @@ -856,6 +856,8 @@ test('sharing a feed URL that looks private asks first and sends back exactly th T: { podcastSharePrivate: 'private', podcastSharePublished: 'published', podcastSharePublishing: 'publishing', podcastShareDiscoveryOff: 'discovery off', podcastSharePending: 'pending', podcastShareUnavailable: 'unavailable', + podcastShareDiscoveryUnavailable: 'discovery unavailable', podcastShareOverLimit: 'over limit', + podcastShareFailed: 'publication failed', podcastWarningSecretQuery: 'parameter «{name}»', podcastWarningPrivateHost: 'host {host}', podcastWarningEntropyQuery: 'value «{name}»', podcastWarningEntropyPath: 'path', podcastWarningOther: 'other', }, @@ -913,8 +915,16 @@ test('sharing a feed URL that looks private asks first and sends back exactly th assert.equal(store.shareStatus(), 'pending'); store.share = Object.assign({}, privateShare, {shared: true, discovery: 'off'}); assert.equal(store.shareStatus(), 'discovery off'); + store.share = Object.assign({}, privateShare, {shared: true, discovery: 'unavailable'}); + assert.equal(store.shareStatus(), 'discovery unavailable'); store.share = Object.assign({}, privateShare, {shared: true}); assert.equal(store.shareStatus(), 'publishing'); + store.share = Object.assign({}, privateShare, {shared: true, held: 'over_limit'}); + assert.equal(store.shareStatus(), 'over limit'); + store.share = Object.assign({}, privateShare, {shared: true, held: 'failed'}); + assert.equal(store.shareStatus(), 'publication failed'); + store.share = Object.assign({}, privateShare, {shared: true, published: true, held: null}); + assert.equal(store.shareStatus(), 'published'); // A public feed is shared directly; a failure is shown. store.share = Object.assign({}, privateShare, {warnings: []});