Add TrackItem.podcast for podcast queue items
TrackItem gains `podcast: Option<PodcastEpisodeRef>` with `TrackItem::podcast_episode`, `is_podcast` and `podcast_episode_ref`. Library queries keep returning music only (`podcast: None`); the library database is unchanged. Music-only code paths must check `is_podcast`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65d6ca2f14
commit
35115ecc98
2 files changed
+130
No files matched your search
@@ -3283,6 +3283,7 @@ fn query_tracks(
|
||||
artists: Vec::new(),
|
||||
featured_artists: Vec::new(),
|
||||
fed: None,
|
||||
podcast: None,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
@@ -3348,6 +3349,7 @@ fn pending_track(fed: &FederatedTrack) -> TrackItem {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: Some(fed.clone()),
|
||||
podcast: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Data shapes the views render. They mirror what the furumusic API used to
|
||||
//! return, but every field is now filled from the local SQLite library.
|
||||
|
||||
use music_dht::podcast::PodcastEpisodeRef;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Which catalog sources a client wants library queries to include.
|
||||
@@ -120,6 +121,22 @@ pub struct ArtistRef {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
/// A playable queue item: a library track, a pending federated track or a
|
||||
/// podcast episode.
|
||||
///
|
||||
/// Podcast episodes ([`TrackItem::podcast_episode`]) carry their
|
||||
/// [`PodcastEpisodeRef`] in [`TrackItem::podcast`]: `id` and `release_id` are
|
||||
/// 0, `file_path` is empty, `content_id`, `fed` and `cover_path` are `None`,
|
||||
/// and the display fields mirror the reference (episode title, podcast title
|
||||
/// as artist and release). The audio URL is only in the reference: it is
|
||||
/// never a local path, never content-addressed and never offered for P2P
|
||||
/// transfer.
|
||||
///
|
||||
/// **Music-only code paths must check [`TrackItem::is_podcast`]** (or
|
||||
/// `podcast.is_none()`) before treating an item as music: likes, playlists,
|
||||
/// listening history, content-id lookups, federation downloads, similarity
|
||||
/// and file operations do not apply to episodes. Library queries never
|
||||
/// return podcast items; the library database stores none.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TrackItem {
|
||||
pub id: i64,
|
||||
@@ -149,9 +166,59 @@ pub struct TrackItem {
|
||||
/// carries everything needed to download them from the owning peer.
|
||||
/// With an empty `file_path` the player resolves the track on demand.
|
||||
pub fed: Option<FederatedTrack>,
|
||||
/// Set for podcast episodes (see the type documentation); `None` for music.
|
||||
pub podcast: Option<PodcastEpisodeRef>,
|
||||
}
|
||||
|
||||
impl TrackItem {
|
||||
/// Queue item for a podcast episode streamed from the publisher.
|
||||
///
|
||||
/// Display fields mirror `PlaybackTrack::podcast_episode` of
|
||||
/// `music_dht::device_sync` (the episode title, the podcast title as
|
||||
/// artist and release, the announced duration; no track or disc number)
|
||||
/// and add the publication year. Season and episode number stay in the
|
||||
/// reference.
|
||||
pub fn podcast_episode(episode: PodcastEpisodeRef) -> Self {
|
||||
Self {
|
||||
id: 0,
|
||||
title: episode.episode_title.clone(),
|
||||
track_number: None,
|
||||
disc_number: None,
|
||||
duration_seconds: episode
|
||||
.duration_ms
|
||||
.map_or(0.0, |ms| ms.max(0) as f64 / 1_000.0),
|
||||
artists: vec![ArtistRef {
|
||||
id: 0,
|
||||
name: episode.podcast_title.clone(),
|
||||
}],
|
||||
featured_artists: Vec::new(),
|
||||
release_id: 0,
|
||||
release_title: episode.podcast_title.clone(),
|
||||
release_year: episode.published_at_ms.map(year_of_unix_ms),
|
||||
file_path: String::new(),
|
||||
content_id: None,
|
||||
cover_path: None,
|
||||
audio_format: None,
|
||||
audio_bitrate: None,
|
||||
audio_sample_rate: None,
|
||||
audio_bit_depth: None,
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: Some(episode),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this item is a podcast episode (not music).
|
||||
pub fn is_podcast(&self) -> bool {
|
||||
self.podcast.is_some()
|
||||
}
|
||||
|
||||
/// The episode reference of a podcast item.
|
||||
pub fn podcast_episode_ref(&self) -> Option<&PodcastEpisodeRef> {
|
||||
self.podcast.as_ref()
|
||||
}
|
||||
|
||||
/// A federated track that still needs downloading before playback.
|
||||
pub fn is_fed_pending(&self) -> bool {
|
||||
self.fed.is_some() && self.file_path.is_empty()
|
||||
@@ -202,6 +269,21 @@ impl TrackItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// Proleptic Gregorian year of a unix timestamp in milliseconds (UTC).
|
||||
fn year_of_unix_ms(ms: i64) -> i32 {
|
||||
// Howard Hinnant's civil_from_days.
|
||||
let days = ms.div_euclid(86_400_000);
|
||||
let z = days + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z - era * 146_097;
|
||||
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let year = yoe + era * 400 + i64::from(month <= 2);
|
||||
i32::try_from(year).unwrap_or(if year < 0 { i32::MIN } else { i32::MAX })
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ReleaseCard {
|
||||
pub id: i64,
|
||||
@@ -340,8 +422,54 @@ mod tests {
|
||||
file_size_bytes: None,
|
||||
play_count: 0,
|
||||
fed: None,
|
||||
podcast: None,
|
||||
};
|
||||
|
||||
assert_eq!(track.artist_line(), "Main feat. Guest, Other");
|
||||
assert!(!track.is_podcast());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn year_of_unix_ms_handles_epoch_and_leap_years() {
|
||||
assert_eq!(year_of_unix_ms(0), 1970);
|
||||
assert_eq!(year_of_unix_ms(-1), 1969);
|
||||
// 2024-02-29T12:00:00Z and 2024-12-31T23:59:59Z.
|
||||
assert_eq!(year_of_unix_ms(1_709_208_000_000), 2024);
|
||||
assert_eq!(year_of_unix_ms(1_735_689_599_000), 2024);
|
||||
assert_eq!(year_of_unix_ms(1_735_689_600_000), 2025);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_track_mirrors_the_episode_reference() {
|
||||
use music_dht::podcast::{EpisodeId, FeedUrl, PodcastId};
|
||||
let feed_url = FeedUrl::parse("https://example.test/feed.xml").unwrap();
|
||||
let podcast_id = PodcastId::from_feed_url(&feed_url);
|
||||
let episode = PodcastEpisodeRef {
|
||||
episode_id: EpisodeId::from_guid(&podcast_id, "e1").unwrap(),
|
||||
podcast_id,
|
||||
feed_url,
|
||||
podcast_title: "Show".into(),
|
||||
episode_title: "Pilot".into(),
|
||||
audio_url: "https://cdn.example.test/1.mp3".into(),
|
||||
mime: Some("audio/mpeg".into()),
|
||||
duration_ms: Some(90_500),
|
||||
published_at_ms: Some(1_709_208_000_000),
|
||||
season: Some(2),
|
||||
episode_number: Some(7),
|
||||
image_url: None,
|
||||
progress_generation: None,
|
||||
};
|
||||
let track = TrackItem::podcast_episode(episode.clone());
|
||||
assert!(track.is_podcast());
|
||||
assert_eq!(track.podcast_episode_ref(), Some(&episode));
|
||||
assert_eq!(track.id, 0);
|
||||
assert!(track.file_path.is_empty());
|
||||
assert!(track.content_id.is_none() && track.fed.is_none());
|
||||
assert!(!track.is_fed_pending());
|
||||
assert_eq!(track.title, "Pilot");
|
||||
assert_eq!(track.artist_line(), "Show");
|
||||
assert_eq!(track.release_title, "Show");
|
||||
assert_eq!(track.release_year, Some(2024));
|
||||
assert_eq!(track.duration_label(), "1:31");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user