diff --git a/src/federation/client.rs b/src/federation/client.rs index cc93363..e1b99a6 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -282,7 +282,13 @@ impl Federation { } /// The `federation.podcast` events of a search for `user_id`, numbered - /// after the music events (see [`podcast_events`]). + /// after the music events (see [`podcast_events`]), and the + /// `podcast_discovery` state for `search.completed`. + /// + /// Never fails: when the hits cannot be prepared for the account (a + /// database error) they are dropped and the state is `failed`, so the + /// music results stand and the browser can say the podcast search + /// failed. pub async fn podcast_search_events( &self, search_id: &str, @@ -290,28 +296,46 @@ impl Federation { user_id: i64, music: &MusicSearch, podcasts: PodcastSearch, - ) -> Result> { + ) -> (&'static str, Vec) { + let state = podcasts.state(); let PodcastSearch::Found(hits) = podcasts else { - return Ok(Vec::new()); + return (state, Vec::new()); }; - let pool = self.pool().await?; - let first_sequence = music.events.len() as u64 + 1; - let events = podcast_events( - &pool, - user_id, - search_id, - query.trim(), - &music.items, - first_sequence, - hits, - ) - .await?; - tracing::info!( - search_id, - podcasts = events.len(), - "public podcast hits ready" - ); - Ok(events) + if hits.is_empty() { + return (state, Vec::new()); + } + let events = async { + let pool = self.pool().await?; + let first_sequence = music.events.len() as u64 + 1; + podcast_events( + &pool, + user_id, + search_id, + query.trim(), + &music.items, + first_sequence, + hits, + ) + .await + }; + match events.await { + Ok(events) => { + tracing::info!( + search_id, + podcasts = events.len(), + "public podcast hits ready" + ); + (state, events) + } + Err(error) => { + // Only the error: never the query or the hits. + tracing::warn!( + search_id, + "public podcast hits could not be prepared: {error:#}" + ); + ("failed", Vec::new()) + } + } } } diff --git a/src/i18n/phrases.rs b/src/i18n/phrases.rs index 4fcc952..ee2fb71 100644 --- a/src/i18n/phrases.rs +++ b/src/i18n/phrases.rs @@ -465,6 +465,7 @@ translations! { player_podcast_request_failed: "Request failed", "Запрос не выполнен"; player_podcast_found_title: "Podcasts", "Подкасты"; player_podcast_type: "Podcast", "Подкаст"; + player_podcast_found_failed: "The podcast search did not answer this time; music results are complete.", "Поиск подкастов сейчас не ответил; результаты по музыке полные."; player_podcast_found_subscribed: "Already subscribed", "Уже подписан"; player_podcast_found_open: "Open", "Открыть"; player_podcast_found_subscribing: "Subscribing…", "Подписываемся…"; diff --git a/src/player/mod.rs b/src/player/mod.rs index b683e0c..4e30b9e 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -7507,11 +7507,11 @@ async fn federation_search_events_handler( let podcasts = podcasts .await .unwrap_or(crate::federation::client::PodcastSearch::Failed); - let podcast_discovery = podcasts.state(); - let podcast_events = federation + // A podcast search or ranking failure only drops the + // podcast hits: `search.completed` reports it. + let (podcast_discovery, podcast_events) = federation .podcast_search_events(&body_search_id, &q, user_id, &music, podcasts) - .await - .map_err(|err| cot::Error::internal(err.to_string()))?; + .await; for event in &podcast_events { last_sequence = event.sequence; yield search_event_frame(event)?; diff --git a/src/podcasts/search.rs b/src/podcasts/search.rs index 45b3822..4b1c8c8 100644 --- a/src/podcasts/search.rs +++ b/src/podcasts/search.rs @@ -367,6 +367,40 @@ mod tests { assert_eq!(http_url(None), None); } + #[tokio::test] + async fn podcast_hits_that_cannot_be_prepared_are_reported_as_failed() { + use crate::federation::client::{MusicSearch, PodcastSearch}; + // The federation handle of a unit test has no database: ranking the + // hits for the account fails. The search still completes. + let federation = crate::federation::handle(); + let music = MusicSearch { + events: Vec::new(), + items: vec![music("Night Radio Theme")], + }; + let (state, events) = federation + .podcast_search_events( + "search", + "night radio", + 1, + &music, + PodcastSearch::Found(vec![hit("a.xml", "Night Radio")]), + ) + .await; + assert_eq!(state, "failed"); + assert!(events.is_empty()); + for (search, expected) in [ + (PodcastSearch::Found(Vec::new()), "running"), + (PodcastSearch::Off, "off"), + (PodcastSearch::Failed, "failed"), + ] { + let (state, events) = federation + .podcast_search_events("search", "night radio", 1, &music, search) + .await; + assert_eq!(state, expected); + assert!(events.is_empty()); + } + } + #[test] fn a_preview_lists_seasons_and_the_newest_episodes() { let xml = r#" diff --git a/templates/player/scripts.html b/templates/player/scripts.html index 79f7c16..19a889a 100644 --- a/templates/player/scripts.html +++ b/templates/player/scripts.html @@ -4353,7 +4353,7 @@ document.addEventListener('alpine:init', () => { similaritySearchLabel: '', similaritySearchError: '', similaritySearchStats: { loading: false, tracks: 0, artists: 0, peers: 0, elapsed_ms: 0 }, - federationSearch: { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [] }, + federationSearch: { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' }, artistFederation: { loading: false, error: '', releases: [], tracks: [] }, federationPreparing: {}, federationFailures: {}, @@ -5293,7 +5293,7 @@ document.addEventListener('alpine:init', () => { this.similaritySearchError = ''; this.searchLoading = true; this.searchResults = null; - this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [] }; + this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' }; this.similaritySearchStats = { loading: true, tracks: 0, artists: 0, peers: 0, elapsed_ms: 0 }; Alpine.store('info').close(); try { @@ -5435,12 +5435,12 @@ document.addEventListener('alpine:init', () => { this._federationSearchSource.close(); this._federationSearchSource = null; } - this.federationSearch = { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [] }; + this.federationSearch = { loading: false, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' }; }, startFederationSearch(query) { this.stopFederationSearch(); - this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [] }; + this.federationSearch = { loading: true, error: '', artists: [], releases: [], tracks: [], podcasts: [], podcastDiscovery: '' }; const source = new EventSource(`/api/player/federation/search/events?q=${encodeURIComponent(query)}`); this._federationSearchSource = source; const updateResults = (kind, keyFor, labelFor, entity) => { @@ -5507,10 +5507,17 @@ document.addEventListener('alpine:init', () => { podcasts.sort((left, right) => left.rank - right.rank); this.federationSearch = { ...this.federationSearch, podcasts }; }); - source.addEventListener('search.completed', () => { + source.addEventListener('search.completed', event => { window.clearTimeout(watchdog); + // `running`, `off` or `failed`: a failed podcast search must + // not look like "no podcasts found". + let podcastDiscovery = ''; + try { + const state = JSON.parse(event?.data)?.entity?.podcast_discovery; + if (['running', 'off', 'failed'].includes(state)) podcastDiscovery = state; + } catch {} if (this._federationSearchSource === source) { - this.federationSearch = { ...this.federationSearch, loading: false }; + this.federationSearch = { ...this.federationSearch, loading: false, podcastDiscovery }; } source.close(); if (this._federationSearchSource === source) this._federationSearchSource = null; diff --git a/templates/player/shell.html b/templates/player/shell.html index 8d3dc75..c514007 100644 --- a/templates/player/shell.html +++ b/templates/player/shell.html @@ -657,6 +657,9 @@ +
{{ t.player_podcast_found_failed }}