From c7785b6488e7235287329752e2833c0562e5f600 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Tue, 3 Feb 2026 19:53:22 -0500 Subject: [PATCH] Fix playlist track count to show actual available tracks - Changed ChildCount to reflect tracks actually in Jellyfin (local + external matched) - Previously was incorrectly adding missing tracks to the count - Now clients see the correct number of playable tracks - Uses spotify:matched:ordered cache to count external matched tracks --- allstarr/Controllers/JellyfinController.cs | 58 +++++++++++++--------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/allstarr/Controllers/JellyfinController.cs b/allstarr/Controllers/JellyfinController.cs index 4059c60..8cfecb6 100644 --- a/allstarr/Controllers/JellyfinController.cs +++ b/allstarr/Controllers/JellyfinController.cs @@ -2628,18 +2628,28 @@ public class JellyfinController : ControllerBase if (playlistConfig != null) { var playlistName = playlistConfig.Name; - var missingTracksKey = $"spotify:missing:{playlistName}"; - var missingTracks = await _cache.GetAsync>(missingTracksKey); - _logger.LogInformation("Cache lookup for {Key}: {Count} tracks", - missingTracksKey, missingTracks?.Count ?? 0); + // Get matched external tracks (tracks that were successfully downloaded/matched) + var matchedTracksKey = $"spotify:matched:ordered:{playlistName}"; + var matchedTracks = await _cache.GetAsync>(matchedTracksKey); - // Fallback to file cache - if (missingTracks == null || missingTracks.Count == 0) + _logger.LogInformation("Cache lookup for {Key}: {Count} matched tracks", + matchedTracksKey, matchedTracks?.Count ?? 0); + + // Fallback to legacy cache format + if (matchedTracks == null || matchedTracks.Count == 0) { - _logger.LogInformation("Trying file cache for {Name}", playlistName); - missingTracks = await LoadMissingTracksFromFile(playlistName); - _logger.LogInformation("File cache result: {Count} tracks", missingTracks?.Count ?? 0); + var legacyKey = $"spotify:matched:{playlistName}"; + var legacySongs = await _cache.GetAsync>(legacyKey); + if (legacySongs != null && legacySongs.Count > 0) + { + matchedTracks = legacySongs.Select((s, i) => new MatchedTrack + { + Position = i, + MatchedSong = s + }).ToList(); + _logger.LogInformation("Loaded {Count} tracks from legacy cache", matchedTracks.Count); + } } // Get local tracks count from Jellyfin @@ -2655,7 +2665,7 @@ public class JellyfinController : ControllerBase localTracksResponse.RootElement.TryGetProperty("Items", out var localItems)) { localTracksCount = localItems.GetArrayLength(); - _logger.LogInformation("Found {Count} local tracks in Jellyfin playlist {Name}", + _logger.LogInformation("Found {Count} total items in Jellyfin playlist {Name}", localTracksCount, playlistName); } } @@ -2664,26 +2674,28 @@ public class JellyfinController : ControllerBase _logger.LogWarning(ex, "Failed to get local tracks count for {Name}", playlistName); } - if (missingTracks != null && missingTracks.Count > 0) + // Count external matched tracks (not local) + var externalMatchedCount = 0; + if (matchedTracks != null) { - // Update ChildCount to show total tracks (local + external) - var totalCount = localTracksCount + missingTracks.Count; - itemDict["ChildCount"] = totalCount; - modified = true; - _logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Total} ({Local} local + {External} external)", - playlistName, totalCount, localTracksCount, missingTracks.Count); + externalMatchedCount = matchedTracks.Count(t => t.MatchedSong != null && !t.MatchedSong.IsLocal); } - else if (localTracksCount > 0) + + // Total available tracks = what's actually in Jellyfin (local + external matched) + // This is what clients should see as the track count + var totalAvailableCount = localTracksCount; + + if (totalAvailableCount > 0) { - // No external tracks, but we have local tracks - itemDict["ChildCount"] = localTracksCount; + // Update ChildCount to show actual available tracks + itemDict["ChildCount"] = totalAvailableCount; modified = true; - _logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Count} (local only, no external tracks)", - playlistName, localTracksCount); + _logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Total} (actual tracks in Jellyfin)", + playlistName, totalAvailableCount); } else { - _logger.LogWarning("No tracks found for {Name} (neither local nor external)", playlistName); + _logger.LogWarning("No tracks found in Jellyfin for {Name}", playlistName); } } }