feat: My server, browse and play a Navidrome library

Adds a "My server" sidebar item (⌘5) for Navidrome and other
Subsonic-compatible servers: albums, artists, playlists, starred songs
and search, with album, artist and playlist pages. Server songs share the
player and queue with Yandex tracks and scrobble back to the server.

- New subsonic-api crate: token auth, envelope and error parsing, and a
  stream plan that keeps files the player decodes and asks the server to
  transcode the rest to MP3.
- ym_api::Track gains a Subsonic source; the player fetches, reports and
  scrobbles by source. Server errors never sign out of Yandex, and the
  Yandex client never fetches server audio or covers.
- Server credentials live in the keychain; connect and disconnect from
  the page or Settings.
- Server tracks have no likes; lyrics come from LRCLIB.
This commit is contained in:
yuriy.p committed 2026-11-16 12:21:02 +06:00
1 parent 391a554736
commit 5dbdc04cd4
32 files changed
+2906 -150

No files matched your search

+5 -2
View File
@@ -701,9 +701,12 @@ impl YmClient {
// ---- Lyrics ----
/// Synced lyrics when Yandex has them, else plain text, else whatever LRCLIB
/// has; `None` when neither has any.
/// has; `None` when neither has any. Non-Yandex tracks go straight to LRCLIB.
pub async fn lyrics(&self, track: &Track) -> Result<Option<Lyrics>> {
if let Some(lyrics) = self.yandex_lyrics(&track.id).await? {
// Tracks from other libraries have no Yandex id to ask about.
if track.is_yandex()
&& let Some(lyrics) = self.yandex_lyrics(&track.id).await?
{
return Ok(Some(lyrics));
}
crate::lrclib::lyrics(self.inner.transport().http(), track).await
+4 -1
View File
@@ -20,7 +20,10 @@ pub use library::{
AlbumKind, AlbumPage, AlbumSummary, ArtistPage, ArtistSummary, LibraryPlaylists, PlaylistId,
PlaylistPage, PlaylistSummary,
};
pub use models::{AlbumRef, ArtistRef, Stream, Track};
pub use models::{
AlbumRef, ArtistRef, SUBSONIC_COVER_PREFIX, Source, Stream, SubsonicSong, Track, hash_str, hues_for,
parse_subsonic_cover, subsonic_cover_url,
};
pub use wave::{
Character, Language, MY_WAVE, Mood, PlayReport, Station, WaveBatch, WaveEvent, WaveSettings,
};
+2 -2
View File
@@ -55,14 +55,14 @@ pub(crate) async fn lyrics(http: &reqwest::Client, track: &Track) -> Result<Opti
let Some(artist) = track.artists.first().map(|a| a.name.as_str()) else { return Ok(None) };
let secs = track.duration.as_secs_f64();
if let Some(album) = &track.album
if let Some(album) = track.album_title()
&& secs > 0.0
{
let duration = secs.round().to_string();
let query = [
("track_name", track.title.as_str()),
("artist_name", artist),
("album_name", album.title.as_str()),
("album_name", album),
("duration", duration.as_str()),
];
if let Some(record) = get::<Record>(http, "get", &query).await?
+139 -6
View File
@@ -31,6 +31,48 @@ pub struct Track {
pub available: bool,
pub explicit: bool,
pub lyrics_available: bool,
/// Where the audio and cover come from.
pub source: Source,
}
/// Where a track lives. Yandex tracks come from this crate; the app builds
/// others from another library and plays them through that library's client.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Source {
#[default]
Yandex,
/// A song on a Subsonic-compatible server, such as Navidrome.
Subsonic(Box<SubsonicSong>),
}
/// What the app needs to stream a server song and link to its album and artist.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubsonicSong {
pub song_id: String,
pub album_id: Option<String>,
pub album_title: Option<String>,
pub artist_id: Option<String>,
/// Id for the server's `getCoverArt`.
pub cover_art: Option<String>,
/// File extension on the server: "mp3", "flac", "opus"…
pub suffix: String,
pub bit_rate: u32,
}
/// Cover URLs of server songs: `subsonic://cover/{id}?size={n}`. They carry no
/// credentials, so they're safe as cache keys; the app resolves them itself.
pub const SUBSONIC_COVER_PREFIX: &str = "subsonic://cover/";
/// The cover id and size in a [`SUBSONIC_COVER_PREFIX`] URL.
pub fn parse_subsonic_cover(url: &str) -> Option<(&str, u32)> {
let rest = url.strip_prefix(SUBSONIC_COVER_PREFIX)?;
let (id, size) = rest.split_once("?size=")?;
Some((id, size.parse().ok()?)).filter(|(id, _)| !id.is_empty())
}
/// A [`SUBSONIC_COVER_PREFIX`] URL for cover `id` at `size` pixels.
pub fn subsonic_cover_url(id: &str, size: u32) -> String {
format!("{SUBSONIC_COVER_PREFIX}{id}?size={size}")
}
impl Track {
@@ -66,9 +108,48 @@ impl Track {
Some(i) => i.has_available_sync_lyrics.unwrap_or(false) || i.has_available_text_lyrics.unwrap_or(false),
None => t.lyrics_available.unwrap_or(false),
},
source: Source::Yandex,
})
}
/// A song from a Subsonic server. Its id is `sub-{song_id}`: unique next to
/// Yandex ids and free of the `:` that [`Track::full_id`] splits on.
pub fn from_subsonic(title: String, artist: Option<String>, duration: Duration, song: SubsonicSong) -> Self {
Self {
id: format!("sub-{}", song.song_id),
title,
version: None,
artists: artist.into_iter().map(|name| ArtistRef { id: None, name }).collect(),
album: None,
duration,
cover_uri: None,
available: true,
explicit: false,
lyrics_available: false,
source: Source::Subsonic(Box::new(song)),
}
}
pub fn is_yandex(&self) -> bool {
matches!(self.source, Source::Yandex)
}
pub fn subsonic(&self) -> Option<&SubsonicSong> {
match &self.source {
Source::Subsonic(song) => Some(song),
Source::Yandex => None,
}
}
/// The album's title, whichever library the track is from.
pub fn album_title(&self) -> Option<&str> {
let title = match &self.source {
Source::Yandex => self.album.as_ref().map(|a| a.title.as_str()),
Source::Subsonic(song) => song.album_title.as_deref(),
};
title.filter(|t| !t.is_empty())
}
/// Decodes a track from the API's JSON, e.g. a recorded fixture.
pub fn from_json(json: &str) -> crate::Result<Self> {
let raw = serde_json::from_str(json).map_err(|e| crate::Error::Api(e.to_string()))?;
@@ -98,15 +179,19 @@ impl Track {
/// Absolute cover URL at `size`×`size` pixels.
pub fn cover_url(&self, size: u32) -> Option<String> {
cover_url(self.cover_uri.as_deref(), size)
match &self.source {
Source::Yandex => cover_url(self.cover_uri.as_deref(), size),
Source::Subsonic(song) => song.cover_art.as_deref().map(|id| subsonic_cover_url(id, size)),
}
}
/// Two stable hues for placeholder art, derived from the album (or track) id.
pub fn hues(&self) -> (f32, f32) {
hues_for(self.album.as_ref().map_or_else(
|| self.id.bytes().fold(7u64, |h, b| h.wrapping_mul(31).wrapping_add(u64::from(b))),
|a| a.id as u64,
))
if let Some(album) = &self.album {
return hues_for(album.id as u64);
}
let seed = self.subsonic().and_then(|s| s.album_id.as_deref()).unwrap_or(&self.id);
hues_for(hash_str(seed))
}
}
@@ -116,8 +201,13 @@ pub(crate) fn cover_url(uri: Option<&str>, size: u32) -> Option<String> {
Some(yamuse::models::normalize_image_url(uri, &format!("{size}x{size}")))
}
/// A stable hash of a text id, for [`hues_for`].
pub fn hash_str(s: &str) -> u64 {
s.bytes().fold(7u64, |h, b| h.wrapping_mul(31).wrapping_add(u64::from(b)))
}
/// Two hues for placeholder art; equal seeds give equal art.
pub(crate) fn hues_for(seed: u64) -> (f32, f32) {
pub fn hues_for(seed: u64) -> (f32, f32) {
((seed.wrapping_mul(47) % 360) as f32, (seed.wrapping_mul(131).wrapping_add(90) % 360) as f32)
}
@@ -247,6 +337,49 @@ mod tests {
assert!((0.0..360.0).contains(&h1) && (0.0..360.0).contains(&h2));
}
fn server_song() -> Track {
Track::from_subsonic(
"Kukushka".into(),
Some("Kino".into()),
Duration::from_secs(402),
SubsonicSong {
song_id: "s1".into(),
album_id: Some("al1".into()),
album_title: Some("Chorny Albom".into()),
artist_id: Some("ar1".into()),
cover_art: Some("al-al1".into()),
suffix: "flac".into(),
bit_rate: 1000,
},
)
}
#[test]
fn server_songs_keep_their_own_ids_and_credential_free_covers() {
let t = server_song();
assert_eq!(t.id, "sub-s1");
assert_eq!(t.full_id(), "sub-s1", "no album id appended");
assert!(!t.is_yandex() && t.album.is_none() && t.artists[0].id.is_none());
assert_eq!(t.album_title(), Some("Chorny Albom"));
assert!(!t.lyrics_available);
let cover = t.cover_url(200).unwrap();
assert_eq!(cover, "subsonic://cover/al-al1?size=200");
assert_eq!(parse_subsonic_cover(&cover), Some(("al-al1", 200)));
assert_eq!(parse_subsonic_cover("https://avatars.yandex.net/x"), None);
assert_eq!(parse_subsonic_cover("subsonic://cover/?size=1"), None);
let mut other = server_song();
other.id = "sub-s2".into();
assert_eq!(t.hues(), other.hues(), "same album, same art");
}
#[test]
fn yandex_album_titles_come_from_the_album() {
let t = track(r#"{"id":1,"albums":[{"id":5,"title":"A"}]}"#);
assert!(t.is_yandex() && t.subsonic().is_none());
assert_eq!(t.album_title(), Some("A"));
assert_eq!(track(r#"{"id":1,"albums":[{"id":5,"title":""}]}"#).album_title(), None);
}
#[test]
fn best_variant_prefers_full_mp3_at_top_bitrate() {
let v: Vec<yamuse::models::DownloadInfo> = serde_json::from_str(