forked from Furumi/furumi-tui
Compare commits
3
Commits
v0.3.4
..
federation
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
292f17fee1 | ||
|
|
3e46f21804 | ||
|
|
7f0ca2984f |
No files matched your search
@@ -123,6 +123,14 @@ The DHT is the distributed index. Nodes publish compact searchable
|
||||
descriptions of their local library and query the network without contacting a
|
||||
central search service.
|
||||
|
||||
Opening a local artist or release card in a network-enabled library view
|
||||
enriches it asynchronously. DHT matches identify likely catalog owners, while
|
||||
a bounded set of known peers can also answer directly when the index is
|
||||
incomplete. Card results are replaceable snapshots: reopening a card or
|
||||
periodic maintenance retries discovery, with at most one request per card in
|
||||
flight. A failed refresh keeps the last usable snapshot, and never prevents
|
||||
browsing the local tracks.
|
||||
|
||||
Similarity discovery uses a separate schema-independent DHT overlay. A node
|
||||
derives a deterministic 256-bit routing signature from every local embedding,
|
||||
groups them into fixed two-level LSH buckets, and publishes compact summaries
|
||||
|
||||
Generated
+1
-1
@@ -1715,7 +1715,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "furumi_tui"
|
||||
version = "0.3.4"
|
||||
version = "0.3.5"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumi_tui"
|
||||
version = "0.3.4"
|
||||
version = "0.3.5"
|
||||
edition = "2024"
|
||||
rust-version = "1.97"
|
||||
description = "A federated P2P player for personal music libraries"
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
//! Scheduling for local cards enriched by replaceable federation data.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::state::{AppState, GlobalView, Loadable, Tab};
|
||||
|
||||
const REFRESH_INTERVAL: Duration = Duration::from_secs(30);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub(super) enum Card {
|
||||
Artist(i64),
|
||||
Release(i64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct RefreshState {
|
||||
visible: Option<Card>,
|
||||
/// None means a request is in flight; Some is its completion time.
|
||||
requests: HashMap<Card, Option<Instant>>,
|
||||
}
|
||||
|
||||
impl RefreshState {
|
||||
pub(super) fn is_loading(&self, card: Card) -> bool {
|
||||
matches!(self.requests.get(&card), Some(None))
|
||||
}
|
||||
|
||||
fn begin(&mut self, visible: Option<Card>, now: Instant) -> bool {
|
||||
let reopened = self.visible != visible;
|
||||
self.visible = visible;
|
||||
let Some(card) = visible else { return false };
|
||||
match self.requests.get(&card) {
|
||||
Some(None) => return false,
|
||||
Some(Some(completed))
|
||||
if !reopened && now.duration_since(*completed) < REFRESH_INTERVAL =>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.requests.insert(card, None);
|
||||
true
|
||||
}
|
||||
|
||||
pub(super) fn complete(&mut self, card: Card, now: Instant) {
|
||||
self.requests.insert(card, Some(now));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) enum Request {
|
||||
Artist {
|
||||
id: i64,
|
||||
name: String,
|
||||
},
|
||||
Release {
|
||||
id: i64,
|
||||
title: String,
|
||||
artists: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Called on navigation and maintenance ticks. A cached success or failure
|
||||
/// must not suppress discovery for the rest of the session.
|
||||
pub(super) fn prepare(state: &mut AppState, now: Instant) -> Option<Request> {
|
||||
let visible = if state.federation.settings.enabled
|
||||
&& state.global.filters.source_mode.includes_network()
|
||||
&& state.active_tab == Tab::Global
|
||||
{
|
||||
match state.global.stack.last() {
|
||||
Some(GlobalView::Artist { id, .. }) => Some(Card::Artist(*id)),
|
||||
Some(GlobalView::Release { id, .. }) => Some(Card::Release(*id)),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let request = match visible {
|
||||
Some(Card::Artist(id)) => match state.artist_views.get(&id) {
|
||||
Some(Loadable::Ready(detail)) => Some(Request::Artist {
|
||||
id,
|
||||
name: detail.name.clone(),
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
Some(Card::Release(id)) => match state.release_views.get(&id) {
|
||||
Some(Loadable::Ready(detail)) => {
|
||||
let mut artists = Vec::new();
|
||||
for artist in &detail.artists {
|
||||
if !artists.iter().any(|name: &String| {
|
||||
music_dht::normalize_name(name) == music_dht::normalize_name(&artist.name)
|
||||
}) {
|
||||
artists.push(artist.name.clone());
|
||||
}
|
||||
}
|
||||
(!artists.is_empty()).then(|| Request::Release {
|
||||
id,
|
||||
title: detail.title.clone(),
|
||||
artists,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
if !state
|
||||
.card_enrichment
|
||||
.begin(visible.filter(|_| request.is_some()), now)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
match &request {
|
||||
Some(Request::Artist { id, .. }) => mark_loading(&mut state.artist_fed_views, *id),
|
||||
Some(Request::Release { id, .. }) => mark_loading(&mut state.release_fed_views, *id),
|
||||
None => {}
|
||||
}
|
||||
request
|
||||
}
|
||||
|
||||
fn mark_loading<T>(views: &mut HashMap<i64, Loadable<T>>, id: i64) {
|
||||
if !matches!(views.get(&id), Some(Loadable::Ready(_))) {
|
||||
views.insert(id, Loadable::Loading);
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep the last usable snapshot if a refresh fails while peers are offline.
|
||||
pub(super) fn store_result<T>(
|
||||
views: &mut HashMap<i64, Loadable<T>>,
|
||||
id: i64,
|
||||
result: Result<T, String>,
|
||||
) {
|
||||
match result {
|
||||
Ok(value) => {
|
||||
views.insert(id, Loadable::Ready(value));
|
||||
}
|
||||
Err(message) if !matches!(views.get(&id), Some(Loadable::Ready(_))) => {
|
||||
views.insert(id, Loadable::Failed(message));
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
+30
-67
@@ -1,6 +1,7 @@
|
||||
pub mod action;
|
||||
mod cmdline;
|
||||
pub mod command;
|
||||
mod enrichment;
|
||||
pub mod event;
|
||||
pub mod input;
|
||||
pub(crate) mod popup;
|
||||
@@ -310,54 +311,16 @@ fn spawn_release_federation_enrichment(
|
||||
});
|
||||
}
|
||||
|
||||
fn maybe_enrich_open_artist(state: &mut AppState, runtime: &Runtime) {
|
||||
if !state.federation.settings.enabled || state.active_tab != state::Tab::Global {
|
||||
return;
|
||||
}
|
||||
let Some(state::GlobalView::Artist { id, .. }) = state.global.stack.last().copied() else {
|
||||
return;
|
||||
};
|
||||
if state.artist_fed_views.contains_key(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(state::Loadable::Ready(detail)) = state.artist_views.get(&id) else {
|
||||
return;
|
||||
};
|
||||
let name = detail.name.clone();
|
||||
state.artist_fed_views.insert(id, state::Loadable::Loading);
|
||||
spawn_artist_federation_enrichment(runtime, id, name);
|
||||
}
|
||||
|
||||
fn maybe_enrich_open_release(state: &mut AppState, runtime: &Runtime) {
|
||||
if !state.federation.settings.enabled
|
||||
|| !state.global.filters.source_mode.includes_network()
|
||||
|| state.active_tab != state::Tab::Global
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(state::GlobalView::Release { id, .. }) = state.global.stack.last().copied() else {
|
||||
return;
|
||||
};
|
||||
if state.release_fed_views.contains_key(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(state::Loadable::Ready(detail)) = state.release_views.get(&id) else {
|
||||
return;
|
||||
};
|
||||
let mut artists = Vec::new();
|
||||
for artist in &detail.artists {
|
||||
if !artists.iter().any(|name: &String| {
|
||||
music_dht::normalize_name(name) == music_dht::normalize_name(&artist.name)
|
||||
}) {
|
||||
artists.push(artist.name.clone());
|
||||
fn maybe_enrich_open_card(state: &mut AppState, runtime: &Runtime) {
|
||||
match enrichment::prepare(state, std::time::Instant::now()) {
|
||||
Some(enrichment::Request::Artist { id, name }) => {
|
||||
spawn_artist_federation_enrichment(runtime, id, name);
|
||||
}
|
||||
Some(enrichment::Request::Release { id, title, artists }) => {
|
||||
spawn_release_federation_enrichment(runtime, id, title, artists);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
if artists.is_empty() {
|
||||
return;
|
||||
}
|
||||
let title = detail.title.clone();
|
||||
state.release_fed_views.insert(id, state::Loadable::Loading);
|
||||
spawn_release_federation_enrichment(runtime, id, title, artists);
|
||||
}
|
||||
|
||||
fn spawn_content_id_backfill(runtime: &Runtime) {
|
||||
@@ -1195,8 +1158,7 @@ fn maintenance(state: &mut AppState, runtime: &mut Runtime) {
|
||||
|
||||
maybe_refresh_network_library(state, runtime);
|
||||
maybe_fetch_network_artist_images(state, runtime);
|
||||
maybe_enrich_open_artist(state, runtime);
|
||||
maybe_enrich_open_release(state, runtime);
|
||||
maybe_enrich_open_card(state, runtime);
|
||||
|
||||
// Liked ids load once per session — markers are shown everywhere.
|
||||
if !state.likes_loaded {
|
||||
@@ -3804,26 +3766,27 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
||||
}
|
||||
AppEvent::ArtistFederationLoaded { id, name, result } => {
|
||||
let cursor_anchor = state::artist_cursor_anchor(state, id);
|
||||
let entry = match result {
|
||||
Ok(card) => state::Loadable::Ready(card),
|
||||
Err(message) => {
|
||||
tracing::debug!(artist = id, %name, %message, "artist federation enrichment failed");
|
||||
state::Loadable::Failed(message)
|
||||
}
|
||||
};
|
||||
state.artist_fed_views.insert(id, entry);
|
||||
state
|
||||
.card_enrichment
|
||||
.complete(enrichment::Card::Artist(id), std::time::Instant::now());
|
||||
if let Err(message) = &result {
|
||||
tracing::debug!(artist = id, %name, %message, "artist federation enrichment failed");
|
||||
}
|
||||
enrichment::store_result(&mut state.artist_fed_views, id, result);
|
||||
state::restore_artist_cursor_anchor(state, id, cursor_anchor);
|
||||
}
|
||||
AppEvent::ReleaseFederationLoaded { id, result } => {
|
||||
let entry = match result {
|
||||
Ok(Some(release)) => state::Loadable::Ready(release),
|
||||
Ok(None) => state::Loadable::Ready(crate::federation::FedRelease::default()),
|
||||
Err(message) => {
|
||||
tracing::debug!(release = id, %message, "release federation enrichment failed");
|
||||
state::Loadable::Failed(message)
|
||||
}
|
||||
};
|
||||
state.release_fed_views.insert(id, entry);
|
||||
state
|
||||
.card_enrichment
|
||||
.complete(enrichment::Card::Release(id), std::time::Instant::now());
|
||||
if let Err(message) = &result {
|
||||
tracing::debug!(release = id, %message, "release federation enrichment failed");
|
||||
}
|
||||
enrichment::store_result(
|
||||
&mut state.release_fed_views,
|
||||
id,
|
||||
result.map(Option::unwrap_or_default),
|
||||
);
|
||||
}
|
||||
AppEvent::NetworkArtistCacheUpdated { source_id, count } => {
|
||||
tracing::debug!(source = %source_id, count, "network artist cache updated");
|
||||
@@ -3937,7 +3900,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
||||
}
|
||||
};
|
||||
state.artist_views.insert(id, entry);
|
||||
maybe_enrich_open_artist(state, runtime);
|
||||
maybe_enrich_open_card(state, runtime);
|
||||
}
|
||||
AppEvent::ReleaseViewLoaded { id, result } => {
|
||||
let entry = match result {
|
||||
@@ -3948,7 +3911,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
||||
}
|
||||
};
|
||||
state.release_views.insert(id, entry);
|
||||
maybe_enrich_open_release(state, runtime);
|
||||
maybe_enrich_open_card(state, runtime);
|
||||
// A Shift-J jump was waiting for this release: focus its track.
|
||||
if let Some((release_id, track_id)) = state.pending_release_focus
|
||||
&& release_id == id
|
||||
|
||||
@@ -1680,6 +1680,7 @@ pub struct AppState {
|
||||
pub release_views: HashMap<i64, Loadable<ReleaseDetail>>,
|
||||
/// Federated tracks that enrich an already-open local release view.
|
||||
pub release_fed_views: HashMap<i64, Loadable<crate::federation::FedRelease>>,
|
||||
pub(super) card_enrichment: super::enrichment::RefreshState,
|
||||
pub playlists: PlaylistsTab,
|
||||
pub playlist_views: HashMap<i64, Loadable<PlaylistDetail>>,
|
||||
/// Liked local-library content ids, for the ♥ markers everywhere tracks are shown.
|
||||
@@ -1717,6 +1718,18 @@ pub struct AppState {
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn artist_federation_loading(&self, id: i64) -> bool {
|
||||
self.card_enrichment
|
||||
.is_loading(super::enrichment::Card::Artist(id))
|
||||
|| matches!(self.artist_fed_views.get(&id), Some(Loadable::Loading))
|
||||
}
|
||||
|
||||
pub fn release_federation_loading(&self, id: i64) -> bool {
|
||||
self.card_enrichment
|
||||
.is_loading(super::enrichment::Card::Release(id))
|
||||
|| matches!(self.release_fed_views.get(&id), Some(Loadable::Loading))
|
||||
}
|
||||
|
||||
pub fn advance_spinner(&mut self) {
|
||||
self.spinner_frame = self.spinner_frame.wrapping_add(1);
|
||||
}
|
||||
|
||||
@@ -1073,3 +1073,276 @@ fn view_toggle() {
|
||||
update(&mut state, Action::ToggleViewMode);
|
||||
assert_eq!(state.global.view, ViewMode::Tiles);
|
||||
}
|
||||
|
||||
fn enrichment_state(release: bool) -> AppState {
|
||||
use crate::library::models::{ArtistRef, ReleaseDetail};
|
||||
let mut state = AppState::default();
|
||||
state.federation.settings.enabled = true;
|
||||
state.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Global;
|
||||
state.artist_views.insert(
|
||||
1,
|
||||
Loadable::Ready(ArtistDetail {
|
||||
id: 1,
|
||||
name: "Artist".into(),
|
||||
image_path: None,
|
||||
total_track_count: 1,
|
||||
total_play_count: 0,
|
||||
top_tracks: vec![test_track(1)],
|
||||
featured_tracks: vec![],
|
||||
releases: vec![],
|
||||
}),
|
||||
);
|
||||
state.release_views.insert(
|
||||
1,
|
||||
Loadable::Ready(ReleaseDetail {
|
||||
id: 1,
|
||||
title: "Release".into(),
|
||||
release_type: "album".into(),
|
||||
year: None,
|
||||
cover_path: None,
|
||||
artists: vec![ArtistRef {
|
||||
id: 1,
|
||||
name: "Artist".into(),
|
||||
}],
|
||||
tracks: vec![test_track(1)],
|
||||
}),
|
||||
);
|
||||
state.global.stack.push(if release {
|
||||
GlobalView::Release { id: 1, cursor: 0 }
|
||||
} else {
|
||||
GlobalView::Artist { id: 1, cursor: 0 }
|
||||
});
|
||||
state
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_enrichment_retries_startup_failure_without_reopening() {
|
||||
use crate::app::enrichment::{self, Card, Request};
|
||||
use std::time::{Duration, Instant};
|
||||
for release in [false, true] {
|
||||
let mut state = enrichment_state(release);
|
||||
let now = Instant::now();
|
||||
let request = enrichment::prepare(&mut state, now).unwrap();
|
||||
let card = match request {
|
||||
Request::Artist { id: 1, ref name } if name == "Artist" => Card::Artist(1),
|
||||
Request::Release {
|
||||
id: 1,
|
||||
ref title,
|
||||
ref artists,
|
||||
} if title == "Release" && artists == &["Artist"] => Card::Release(1),
|
||||
_ => panic!("wrong enrichment request: {request:?}"),
|
||||
};
|
||||
// Slow requests are never duplicated, even after the refresh interval.
|
||||
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(60)).is_none());
|
||||
let completed = now + Duration::from_secs(60);
|
||||
state.card_enrichment.complete(card, completed);
|
||||
if release {
|
||||
enrichment::store_result(
|
||||
&mut state.release_fed_views,
|
||||
1,
|
||||
Err("federation is not running".into()),
|
||||
);
|
||||
} else {
|
||||
enrichment::store_result(
|
||||
&mut state.artist_fed_views,
|
||||
1,
|
||||
Err("federation is not running".into()),
|
||||
);
|
||||
}
|
||||
assert!(enrichment::prepare(&mut state, completed + Duration::from_secs(1)).is_none());
|
||||
assert_eq!(
|
||||
enrichment::prepare(&mut state, completed + Duration::from_secs(30)),
|
||||
Some(request)
|
||||
);
|
||||
assert!(matches!(
|
||||
state.artist_views.get(&1),
|
||||
Some(Loadable::Ready(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
state.release_views.get(&1),
|
||||
Some(Loadable::Ready(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_enrichment_reopens_cached_cards_and_keeps_success_during_failed_refresh() {
|
||||
use crate::app::enrichment::{self, Card};
|
||||
use std::time::{Duration, Instant};
|
||||
for release in [false, true] {
|
||||
let mut state = enrichment_state(release);
|
||||
let now = Instant::now();
|
||||
let request = enrichment::prepare(&mut state, now).unwrap();
|
||||
state.card_enrichment.complete(
|
||||
if release {
|
||||
Card::Release(1)
|
||||
} else {
|
||||
Card::Artist(1)
|
||||
},
|
||||
now,
|
||||
);
|
||||
if release {
|
||||
enrichment::store_result(
|
||||
&mut state.release_fed_views,
|
||||
1,
|
||||
Ok(crate::federation::FedRelease {
|
||||
title: "Release".into(),
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
enrichment::store_result(
|
||||
&mut state.artist_fed_views,
|
||||
1,
|
||||
Ok(crate::federation::FedArtistCard {
|
||||
name: "Artist".into(),
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
}
|
||||
// Successful but local-only/empty snapshots must also be refreshed.
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
let view = state.global.stack.pop().unwrap();
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
state.global.stack.push(view);
|
||||
assert_eq!(
|
||||
enrichment::prepare(&mut state, now + Duration::from_secs(1)),
|
||||
Some(request)
|
||||
);
|
||||
if release {
|
||||
assert!(matches!(
|
||||
state.release_fed_views.get(&1),
|
||||
Some(Loadable::Ready(_))
|
||||
));
|
||||
enrichment::store_result(&mut state.release_fed_views, 1, Err("offline".into()));
|
||||
assert!(
|
||||
matches!(state.release_fed_views.get(&1), Some(Loadable::Ready(card)) if card.title == "Release")
|
||||
);
|
||||
} else {
|
||||
assert!(matches!(
|
||||
state.artist_fed_views.get(&1),
|
||||
Some(Loadable::Ready(_))
|
||||
));
|
||||
enrichment::store_result(&mut state.artist_fed_views, 1, Err("offline".into()));
|
||||
assert!(
|
||||
matches!(state.artist_fed_views.get(&1), Some(Loadable::Ready(card)) if card.name == "Artist")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_enrichment_respects_network_mode_and_deduplicates_navigation() {
|
||||
use crate::app::enrichment;
|
||||
use crate::config::settings::LibrarySourceMode;
|
||||
use std::time::Instant;
|
||||
for release in [false, true] {
|
||||
let mut state = enrichment_state(release);
|
||||
let now = Instant::now();
|
||||
state.federation.settings.enabled = false;
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
state.federation.settings.enabled = true;
|
||||
state.global.filters.source_mode = LibrarySourceMode::Local;
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
state.global.filters.source_mode = LibrarySourceMode::Global;
|
||||
assert!(enrichment::prepare(&mut state, now).is_some());
|
||||
state.active_tab = Tab::Queue;
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
state.active_tab = Tab::Global;
|
||||
assert!(enrichment::prepare(&mut state, now).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_enrichment_refreshes_local_only_success_while_still_open() {
|
||||
use crate::app::enrichment::{self, Card};
|
||||
use std::time::{Duration, Instant};
|
||||
for release in [false, true] {
|
||||
let mut state = enrichment_state(release);
|
||||
let now = Instant::now();
|
||||
let request = enrichment::prepare(&mut state, now).unwrap();
|
||||
state.card_enrichment.complete(
|
||||
if release {
|
||||
Card::Release(1)
|
||||
} else {
|
||||
Card::Artist(1)
|
||||
},
|
||||
now,
|
||||
);
|
||||
if release {
|
||||
enrichment::store_result(&mut state.release_fed_views, 1, Ok(Default::default()));
|
||||
} else {
|
||||
enrichment::store_result(&mut state.artist_fed_views, 1, Ok(Default::default()));
|
||||
}
|
||||
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(29)).is_none());
|
||||
assert_eq!(
|
||||
enrichment::prepare(&mut state, now + Duration::from_secs(30)),
|
||||
Some(request)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_enrichment_card(state: &AppState) -> String {
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
let mut terminal = Terminal::new(TestBackend::new(160, 40)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| crate::ui::global::draw(frame, frame.area(), state))
|
||||
.unwrap();
|
||||
terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content
|
||||
.iter()
|
||||
.map(|cell| cell.symbol())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_card_explains_source_filter_instead_of_waiting_for_a_request() {
|
||||
use crate::app::enrichment;
|
||||
let mut state = enrichment_state(false);
|
||||
state.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Local;
|
||||
assert!(enrichment::prepare(&mut state, std::time::Instant::now()).is_none());
|
||||
let text = render_enrichment_card(&state);
|
||||
assert!(
|
||||
!text.contains("federation: pending"),
|
||||
"Local mode must not promise a request that is disabled"
|
||||
);
|
||||
assert!(
|
||||
text.contains("Local"),
|
||||
"the card must explain why network results are hidden"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_refresh_animates_while_previous_results_stay_visible() {
|
||||
use crate::app::enrichment::{self, Card};
|
||||
use std::time::{Duration, Instant};
|
||||
for release in [false, true] {
|
||||
let mut state = enrichment_state(release);
|
||||
let now = Instant::now();
|
||||
assert!(enrichment::prepare(&mut state, now).is_some());
|
||||
let card = if release {
|
||||
Card::Release(1)
|
||||
} else {
|
||||
Card::Artist(1)
|
||||
};
|
||||
state.card_enrichment.complete(card, now);
|
||||
if release {
|
||||
enrichment::store_result(&mut state.release_fed_views, 1, Ok(Default::default()));
|
||||
} else {
|
||||
enrichment::store_result(&mut state.artist_fed_views, 1, Ok(Default::default()));
|
||||
}
|
||||
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(30)).is_some());
|
||||
let first = render_enrichment_card(&state);
|
||||
assert!(first.contains(&format!("{} searching peers", state.spinner())));
|
||||
state.advance_spinner();
|
||||
let next = render_enrichment_card(&state);
|
||||
assert!(next.contains(&format!("{} searching peers", state.spinner())));
|
||||
assert_ne!(first, next);
|
||||
state
|
||||
.card_enrichment
|
||||
.complete(card, now + Duration::from_secs(31));
|
||||
assert!(!render_enrichment_card(&state).contains("searching peers"));
|
||||
}
|
||||
}
|
||||
+57
-22
@@ -1486,31 +1486,38 @@ impl Federation {
|
||||
let own = service.endpoint_id();
|
||||
let own_hex = own.to_string();
|
||||
let normalized = music_dht::normalize_name(name);
|
||||
let outcome = service
|
||||
.search_network(name)
|
||||
.await
|
||||
.map_err(|err| anyhow::anyhow!("federated search failed: {err}"))?;
|
||||
// A partial/unavailable DHT index must not prevent direct catalog
|
||||
// requests to peers we already know. Keep discovery bounded so an
|
||||
// open card cannot remain in Loading indefinitely.
|
||||
let network_results = match tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
service.search_network(name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(outcome)) => outcome.network_results,
|
||||
Ok(Err(error)) => {
|
||||
tracing::debug!(artist = %name, %error, "artist discovery failed; trying known peers");
|
||||
Vec::new()
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::debug!(artist = %name, "artist discovery timed out; trying known peers");
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
let cached_metadata = self.cached_metadata_snapshot();
|
||||
let has_cached_artist = cached_metadata
|
||||
.iter()
|
||||
.any(|cached| cached_has_artist(cached, &normalized));
|
||||
let owners: std::collections::HashSet<EndpointId> = outcome
|
||||
.network_results
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|
||||
|| item
|
||||
.artist_names
|
||||
.iter()
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
|| item
|
||||
.featured_artist_names
|
||||
.iter()
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
})
|
||||
.map(|item| item.owner)
|
||||
.filter(|owner| *owner != own)
|
||||
.collect();
|
||||
let owners = artist_catalog_owners(
|
||||
&network_results,
|
||||
&normalized,
|
||||
own,
|
||||
service
|
||||
.connected_peers()
|
||||
.into_iter()
|
||||
.chain(service.known_peers().into_iter().map(|peer| peer.peer_id)),
|
||||
);
|
||||
|
||||
let local_library = Arc::clone(&self.library);
|
||||
let local_name = name.to_string();
|
||||
@@ -1565,7 +1572,7 @@ impl Federation {
|
||||
}
|
||||
}
|
||||
let mut card = catalog::merge_catalogs(name, catalogs);
|
||||
add_dht_appearance_hits(&mut card, &outcome.network_results, &normalized, name);
|
||||
add_dht_appearance_hits(&mut card, &network_results, &normalized, name);
|
||||
add_cached_appearance_hits(&mut card, &cached_metadata, &normalized, name);
|
||||
anyhow::ensure!(
|
||||
!card.releases.is_empty() || !card.appears_on.is_empty(),
|
||||
@@ -2406,6 +2413,34 @@ fn cached_has_artist(cached: &CachedTrackMetadata, normalized_artist: &str) -> b
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized_artist)
|
||||
}
|
||||
|
||||
/// Prefer advertised owners, then ask a bounded set of known peers directly.
|
||||
/// Catalog lookup is useful even before those peers' DHT records arrive.
|
||||
fn artist_catalog_owners(
|
||||
items: &[LibraryItem],
|
||||
normalized: &str,
|
||||
own: EndpointId,
|
||||
known: impl IntoIterator<Item = EndpointId>,
|
||||
) -> Vec<EndpointId> {
|
||||
const MAX_CATALOG_PEERS: usize = 32;
|
||||
let advertised = items
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|
||||
|| item
|
||||
.artist_names
|
||||
.iter()
|
||||
.chain(&item.featured_artist_names)
|
||||
.any(|artist| music_dht::normalize_name(artist) == normalized)
|
||||
})
|
||||
.map(|item| item.owner);
|
||||
let mut seen = std::collections::HashSet::from([own]);
|
||||
advertised
|
||||
.chain(known)
|
||||
.filter(|owner| seen.insert(*owner))
|
||||
.take(MAX_CATALOG_PEERS)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn rank_fed_search_results(
|
||||
artists: &mut [FedArtistHit],
|
||||
tracks: &mut [FedTrack],
|
||||
|
||||
@@ -93,3 +93,45 @@ fn federation_search_ranks_exact_names_first() {
|
||||
assert_eq!(artists[0].name, "Ежемесячные");
|
||||
assert_eq!(tracks[0].title, "Ежемесячные");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_catalog_discovery_uses_known_peers_without_dht_hits() {
|
||||
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
|
||||
let peer = test_owner();
|
||||
assert_eq!(
|
||||
artist_catalog_owners(&[], "artist", own, [own, peer, peer]),
|
||||
vec![peer]
|
||||
);
|
||||
// Unrelated records must not send the card request to their owners.
|
||||
assert!(artist_catalog_owners(&[dht_track(&["Other"], &[])], "artist", own, []).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() {
|
||||
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
|
||||
let peer = test_owner();
|
||||
let known: Vec<_> = (10..60)
|
||||
.map(|seed| music_dht::SecretKey::from_bytes(&[seed; 32]).public())
|
||||
.collect();
|
||||
for item in [
|
||||
dht_track(&["Artist"], &[]),
|
||||
dht_track(&["Host"], &["Artist"]),
|
||||
] {
|
||||
let owners = artist_catalog_owners(
|
||||
&[item],
|
||||
"artist",
|
||||
own,
|
||||
known.iter().copied().chain([own, peer]),
|
||||
);
|
||||
assert_eq!(owners.len(), 32);
|
||||
assert_eq!(owners[0], peer);
|
||||
assert!(!owners.contains(&own));
|
||||
assert_eq!(
|
||||
owners
|
||||
.iter()
|
||||
.collect::<std::collections::HashSet<_>>()
|
||||
.len(),
|
||||
owners.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -537,6 +537,17 @@ fn draw_artist(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor:
|
||||
about.push_str(&format!(" · appears on {}", detail.featured_tracks.len()));
|
||||
}
|
||||
let federation_line = match state.artist_fed_views.get(&id) {
|
||||
_ if state.federation.settings.enabled
|
||||
&& !state.global.filters.source_mode.includes_network() =>
|
||||
{
|
||||
Some(Line::styled(
|
||||
"federation: hidden by Local filter",
|
||||
theme::dim(),
|
||||
))
|
||||
}
|
||||
_ if state.artist_federation_loading(id) => {
|
||||
Some(super::loading_line(state, "searching peers…"))
|
||||
}
|
||||
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
|
||||
Some(Loadable::Ready(card)) => {
|
||||
let tracks: usize = card
|
||||
@@ -839,6 +850,17 @@ fn draw_release(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor
|
||||
let year = detail.year.map(|y| format!(" · {y}")).unwrap_or_default();
|
||||
let tracks = crate::app::state::release_tracks(state, id, detail);
|
||||
let federation_line = match state.release_fed_views.get(&id) {
|
||||
_ if state.federation.settings.enabled
|
||||
&& !state.global.filters.source_mode.includes_network() =>
|
||||
{
|
||||
Some(Line::styled(
|
||||
"federation: hidden by Local filter",
|
||||
theme::dim(),
|
||||
))
|
||||
}
|
||||
_ if state.release_federation_loading(id) => {
|
||||
Some(super::loading_line(state, "searching peers…"))
|
||||
}
|
||||
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
|
||||
Some(Loadable::Ready(release)) if !release.tracks.is_empty() => Some(Line::styled(
|
||||
format!("federation: +{} tracks", release.tracks.len()),
|
||||
|
||||
+40
-15
@@ -1,7 +1,7 @@
|
||||
mod additional_settings;
|
||||
pub mod art;
|
||||
mod federation;
|
||||
mod global;
|
||||
pub(crate) mod global;
|
||||
mod logs;
|
||||
mod playlists;
|
||||
mod popup;
|
||||
@@ -400,10 +400,12 @@ fn player_right_line(state: &AppState, width: u16) -> (Line<'static>, u16) {
|
||||
format!(" {} ", state.device_playback.role.label()),
|
||||
theme::role_pill(state.device_playback.role),
|
||||
));
|
||||
spans.push(Span::styled(
|
||||
format!(" · online {}", state.device_playback.online_devices.max(1)),
|
||||
theme::dim(),
|
||||
));
|
||||
if width >= 160 {
|
||||
spans.push(Span::styled(
|
||||
format!(" · online {}", state.device_playback.online_devices.max(1)),
|
||||
theme::dim(),
|
||||
));
|
||||
}
|
||||
}
|
||||
// Keep a gap between the flags and the username block to the right.
|
||||
spans.push(Span::raw(" "));
|
||||
@@ -413,14 +415,34 @@ fn player_right_line(state: &AppState, width: u16) -> (Line<'static>, u16) {
|
||||
)
|
||||
}
|
||||
|
||||
/// Reserve the source-mode badge first so playback details cannot clip it.
|
||||
fn player_row_areas(row: Rect, state: &AppState) -> [Rect; 3] {
|
||||
let badge_width = (format!(" federation: {} ", state.global.filters.source_mode.label()).len()
|
||||
as u16)
|
||||
.min(row.width);
|
||||
let content = Rect::new(
|
||||
row.x,
|
||||
row.y,
|
||||
row.width.saturating_sub(badge_width),
|
||||
row.height,
|
||||
);
|
||||
let badge = Rect::new(content.right(), row.y, badge_width, row.height);
|
||||
let (line, _) = player_right_line(state, content.width);
|
||||
let right_width = line.width().min(usize::from(content.width)) as u16;
|
||||
let [title, right] = Layout::horizontal([
|
||||
Constraint::Min(8.min(content.width)),
|
||||
Constraint::Length(right_width),
|
||||
])
|
||||
.areas(content);
|
||||
[title, right, badge]
|
||||
}
|
||||
|
||||
/// Shared geometry for rendering and background visualization requests.
|
||||
fn footer_areas(area: Rect, state: &AppState) -> [Rect; 3] {
|
||||
let [player_row, message_row] =
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
|
||||
let (line, volume_offset) = player_right_line(state, area.width);
|
||||
let right_width = line.width().min(usize::from(area.width)) as u16;
|
||||
let [_, right] =
|
||||
Layout::horizontal([Constraint::Min(8), Constraint::Length(right_width)]).areas(player_row);
|
||||
let [_, right, badge] = player_row_areas(player_row, state);
|
||||
let (_, volume_offset) = player_right_line(state, area.width.saturating_sub(badge.width));
|
||||
let version_width = (env!("CARGO_PKG_VERSION").len() as u16 + 1).min(message_row.width);
|
||||
let version = Rect::new(
|
||||
message_row.x + message_row.width.saturating_sub(version_width) / 2,
|
||||
@@ -469,11 +491,8 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
// Layout: track title left, time/progress/flags on the right. The
|
||||
// right block is built first and gets a fixed width; the title
|
||||
// truncates into whatever is left.
|
||||
let (center, _) = player_right_line(state, area.width);
|
||||
let center_width = (center.width() as u16).min(area.width);
|
||||
let [title_area, right_area] =
|
||||
Layout::horizontal([Constraint::Min(8), Constraint::Length(center_width)])
|
||||
.areas(player_row);
|
||||
let [title_area, right_area, mode_area] = player_row_areas(player_row, state);
|
||||
let (center, _) = player_right_line(state, area.width.saturating_sub(mode_area.width));
|
||||
|
||||
let mut spans = Vec::new();
|
||||
match &player.current {
|
||||
@@ -498,6 +517,12 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
}
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), title_area);
|
||||
frame.render_widget(Paragraph::new(center), right_area);
|
||||
let mode = state.global.filters.source_mode;
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!(" federation: {} ", mode.label()))
|
||||
.style(theme::source_mode_pill(mode)),
|
||||
mode_area,
|
||||
);
|
||||
draw_version(frame, version_area);
|
||||
if crate::visualizer::status::visible(state) {
|
||||
state.status_visualizer.draw(frame, visual_area);
|
||||
@@ -550,7 +575,7 @@ fn active_artist_peer_search(state: &AppState) -> Option<String> {
|
||||
let Some(GlobalView::Artist { id, .. }) = state.global.stack.last() else {
|
||||
return None;
|
||||
};
|
||||
if !matches!(state.artist_fed_views.get(id), Some(Loadable::Loading)) {
|
||||
if !state.artist_federation_loading(*id) {
|
||||
return None;
|
||||
}
|
||||
let name = match state.artist_views.get(id) {
|
||||
|
||||
@@ -108,3 +108,35 @@ fn status_animation_requires_local_unpaused_playback() {
|
||||
state.device_playback.role = crate::app::state::DevicePlaybackRole::Control;
|
||||
assert!(!crate::visualizer::status::playing(&state));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn federation_mode_badge_stays_visible_at_the_right_edge() {
|
||||
use crate::config::settings::LibrarySourceMode;
|
||||
use ratatui::style::Color;
|
||||
let mut state = playing_state();
|
||||
for (mode, background) in [
|
||||
(LibrarySourceMode::Local, Color::Yellow),
|
||||
(LibrarySourceMode::My, Color::Cyan),
|
||||
(LibrarySourceMode::Global, Color::Green),
|
||||
] {
|
||||
state.global.filters.source_mode = mode;
|
||||
for width in [20, 40, 80, 160, 200] {
|
||||
for playing in [false, true] {
|
||||
state.player.playing = playing;
|
||||
state.cmdline.active = !playing;
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
|
||||
terminal
|
||||
.draw(|frame| draw_status(frame, frame.area(), &state))
|
||||
.unwrap();
|
||||
let label = format!(" federation: {} ", mode.label());
|
||||
let start = width - label.len() as u16;
|
||||
for (offset, ch) in label.chars().enumerate() {
|
||||
let cell = &terminal.backend().buffer()[(start + offset as u16, 0)];
|
||||
assert_eq!(cell.symbol(), ch.to_string());
|
||||
assert_eq!(cell.bg, background);
|
||||
assert_eq!(cell.fg, Color::Black);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,19 @@ pub fn role_pill(role: DevicePlaybackRole) -> Style {
|
||||
.add_modifier(Modifier::BOLD)
|
||||
}
|
||||
|
||||
pub fn source_mode_pill(mode: crate::config::settings::LibrarySourceMode) -> Style {
|
||||
use crate::config::settings::LibrarySourceMode;
|
||||
let bg = match mode {
|
||||
LibrarySourceMode::Local => Color::Yellow,
|
||||
LibrarySourceMode::My => Color::Cyan,
|
||||
LibrarySourceMode::Global => Color::Green,
|
||||
};
|
||||
Style::new()
|
||||
.fg(Color::Black)
|
||||
.bg(bg)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
}
|
||||
|
||||
fn accent_color_for(state: &AppState) -> Color {
|
||||
match state.device_playback.role {
|
||||
DevicePlaybackRole::Jam => JAM_ACCENT,
|
||||
|
||||
Reference in new issue
Block a user