feat: recently played tracks on Home
Home lists the last ten different tracks played on any device, read from the listening history and re-read on each visit. A failed reload keeps the previous list, and a failed first load hides the section.
This commit is contained in:
1 parent
51e7a4f465
commit
b099946fb3
3 files changed
+108
-1
No files matched your search
@@ -39,6 +39,28 @@ fn tag_playlist_ids(raw: &serde_json::Value) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `trackId:albumId` for the newest `limit` distinct tracks in the history (days, then
|
||||
/// the contexts played that day, then their tracks, each newest first).
|
||||
fn recent_track_ids(history: &yamuse::models::MusicHistory, limit: usize) -> Vec<String> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
history
|
||||
.history_tabs
|
||||
.iter()
|
||||
.flat_map(|day| &day.items)
|
||||
.flat_map(|group| &group.tracks)
|
||||
.filter_map(|item| {
|
||||
let id = item.data.as_ref()?.item_id.as_ref()?;
|
||||
let track = id.track_id.as_deref().filter(|t| !t.is_empty())?;
|
||||
Some(match id.album_id.as_deref().filter(|a| !a.is_empty()) {
|
||||
Some(album) => format!("{track}:{album}"),
|
||||
None => track.to_owned(),
|
||||
})
|
||||
})
|
||||
.filter(|id| seen.insert(id.split(':').next().unwrap_or(id).to_owned()))
|
||||
.take(limit)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A queue id, unless it's the all-zero placeholder Yandex returns for a queue it didn't keep.
|
||||
fn real_queue_id(id: Option<String>) -> Option<String> {
|
||||
id.filter(|id| !id.is_empty() && !id.bytes().all(|b| b == b'0'))
|
||||
@@ -310,6 +332,12 @@ impl YmClient {
|
||||
Ok(home)
|
||||
}
|
||||
|
||||
/// The last `limit` different tracks the user played, newest first, on any device.
|
||||
pub async fn recently_played(&self, limit: usize) -> Result<Vec<Track>> {
|
||||
let history = self.inner.music_history(0).await?;
|
||||
self.tracks(recent_track_ids(&history, limit)).await
|
||||
}
|
||||
|
||||
pub async fn chart(&self) -> Result<Vec<ChartEntry>> {
|
||||
let info = self.inner.chart().await?;
|
||||
Ok(info
|
||||
@@ -745,6 +773,25 @@ mod tests {
|
||||
assert_eq!(tag_playlist_ids(&wrapped), vec!["9:8"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_ids_walk_days_and_skip_repeats() {
|
||||
let history: yamuse::models::MusicHistory = serde_json::from_value(serde_json::json!({"historyTabs": [
|
||||
{"date": "2026-10-15", "items": [{"context": {"type": "wave"}, "tracks": [
|
||||
{"type": "track", "data": {"itemId": {"trackId": "1", "albumId": "10"}}},
|
||||
{"type": "track", "data": {"itemId": {"trackId": "2"}}},
|
||||
]}]},
|
||||
{"date": "2026-10-14", "items": [{"tracks": [
|
||||
{"type": "track", "data": {"itemId": {"trackId": "1", "albumId": "10"}}},
|
||||
{"type": "track", "data": {}},
|
||||
{"type": "track", "data": {"itemId": {"trackId": "3", "albumId": "30"}}},
|
||||
{"type": "track", "data": {"itemId": {"trackId": "4", "albumId": "40"}}},
|
||||
]}]},
|
||||
]}))
|
||||
.unwrap();
|
||||
assert_eq!(recent_track_ids(&history, 3), ["1:10", "2", "3:30"]);
|
||||
assert_eq!(recent_track_ids(&history, 10).len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placeholder_queue_ids_are_dropped() {
|
||||
assert_eq!(real_queue_id(Some("000000000000000000000000".into())), None);
|
||||
|
||||
Reference in new issue
Block a user